-
Notifications
You must be signed in to change notification settings - Fork 11
/
UI.cs
231 lines (202 loc) · 9.63 KB
/
UI.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
namespace BetterContinents
{
public static class UI
{
private static Dictionary<string, Action> UICallbacks = new Dictionary<string, Action>();
private static readonly Color ValheimColor = new Color(1, 0.714f, 0.361f, 1);
private static Texture BorderTexture;
private static Texture FrontTexture;
private static Texture BackTexture;
private static GUIStyle BigTextStyle;
private static GUIStyle NormalTextStyle;
private static bool WindowVisible;
private const int Spacing = 10;
private const int ButtonHeight = 30;
private const int ButtonGap = 2;
private const int ButtonWidth = 150;
private static Rect windowRect = new Rect(ButtonWidth + Spacing * 2, 150, ButtonWidth + Spacing * 2, 500);
public static void Init()
{
// Always reset the UI callbacks on scene change
SceneManager.activeSceneChanged += (_, __) =>
{
ColorTextures.Clear();
UICallbacks.Clear();
// Only need these on the client
BorderTexture = CreateFillTexture(Color.Lerp(ValheimColor, Color.white, 0.25f));
FrontTexture = CreateFillTexture(Color.Lerp(ValheimColor, Color.black, 0.5f));
BackTexture = CreateFillTexture(Color.Lerp(ValheimColor, Color.black, 0.85f));
BigTextStyle = null; // We are "resetting" this in-case it got invalidated. We can only actually create it in a GUI function
NormalTextStyle = null;
UI.Add("Debug Mode", () =>
{
if (BetterContinents.AllowDebugActions)
{
UI.Text("Better Continents Debug Mode Enabled!", 10, 10, Color.red);
if (Menu.IsVisible() || Minimap.instance.m_mode == Minimap.MapMode.Large)
{
DoDebugMenu();
}
if (WindowVisible)
{
DebugUtils.Command.CmdUI.DrawSettingsWindow();
}
}
});
UI.Add("Active Hint", () =>
{
//if (Game.instance && Game.instance.WaitingForRespawn())//(Hud.instance?.m_loadingScreen.isActiveAndEnabled ?? false)
if(Menu.IsVisible() || Game.instance && Game.instance.WaitingForRespawn())
{
if(BetterContinents.Settings.EnabledForThisWorld)
{
UI.DisplayMessage($"<color=gray><size=20><b>{ModInfo.Name} v{ModInfo.Version}</b>: <color=green>ENABLED</color> for this world</size></color>");
}
else
{
UI.DisplayMessage($"<color=gray><size=20><b>{ModInfo.Name} v{ModInfo.Version}</b>: <color=#505050ff>DISABLED</color> for this world</size></color>");
}
}
});
};
}
private static void DoDebugMenu()
{
if (Event.current.type is EventType.KeyUp
&& Event.current.modifiers is (EventModifiers.Alt | EventModifiers.FunctionKey)
&& Event.current.keyCode is KeyCode.F8)
{
WindowVisible = !WindowVisible;
Event.current.Use();
}
if (UI.Button("Better Continents", Spacing, 150))
{
WindowVisible = !WindowVisible;
}
}
private static void Window(int windowId)
{
// Make the windows be draggable.
GUI.DragWindow(new Rect(0, 0, 10000, 20));
GUILayout.BeginVertical("Image maps", GUI.skin.window);
{
GUILayout.BeginVertical("Height", GUI.skin.window);
{
ShowOptionalButton(
BetterContinents.Settings.HasHeightmap || BetterContinents.Settings.HasRoughmap ||
BetterContinents.Settings.HasFlatmap, "Reload all height", "reload hm rm fm");
GUILayout.BeginHorizontal();
{
GUILayout.Space(Spacing);
GUILayout.BeginVertical();
{
ShowOptionalButton(BetterContinents.Settings.HasHeightmap, "Reload Heightmap", "reload hm");
ShowOptionalButton(BetterContinents.Settings.HasRoughmap, "Reload Roughmap", "reload rm");
ShowOptionalButton(BetterContinents.Settings.HasFlatmap, "Reload Flatmap", "reload fm");
}
GUILayout.EndVertical();
}
GUILayout.EndHorizontal();
}
GUILayout.EndVertical();
ShowOptionalButton(BetterContinents.Settings.HasBiomemap, "Reload Biomemap", "reload bm");
ShowOptionalButton(BetterContinents.Settings.HasForestmap, "Reload Forestmap", "reload fom");
ShowOptionalButton(BetterContinents.Settings.HasSpawnmap, "Reload Spawnmap", "reload sm");
}
GUILayout.EndVertical();
GUILayout.BeginVertical("Locations", GUI.skin.window);
{
ShowButton("Show Bosses", "bosses");
ShowButton("Show All", "show");
ShowButton("Hide All", "hide");
}
GUILayout.EndVertical();
GUILayout.BeginVertical("Utils", GUI.skin.window);
{
ShowButton("Toggle Minimap Clouds", "clouds");
ShowButton("Refresh", "refresh");
ShowButton("Regenerate Locs", "regenloc");
}
GUILayout.EndVertical();
}
private static void ShowOptionalButton(bool enabled, string name, string command)
{
GUI.enabled = enabled;
if (GUILayout.Button(name, GUILayout.ExpandWidth(false)))
{
DebugUtils.RunConsoleCommand("bc " + command);
}
GUI.enabled = true;
}
private static void ShowButton(string name, string command) => ShowOptionalButton(true, name, command);
public static void OnGUI()
{
foreach (var callback in UICallbacks.Values.ToList())
{
callback();
}
}
public static void Add(string key, Action action) => UICallbacks[key] = action;
public static bool Exists(string key) => UICallbacks.ContainsKey(key);
public static void Remove(string key) => UICallbacks.Remove(key);
private static readonly Dictionary<Color, Texture2D> ColorTextures = new();
public static Texture2D CreateFillTexture(Color color)
{
if (ColorTextures.TryGetValue(color, out var texture) && texture != null)
return texture;
texture = new Texture2D(1, 1);
texture.SetPixels(new []{ color });
texture.Apply(false);
ColorTextures[color] = texture;
return texture;
}
public static void ProgressBar(int percent, string text)
{
CreateTextStyle();
int yOffs = Screen.height - 75;
GUI.DrawTexture(new Rect(50 - 4, yOffs - 4, Screen.width - 100 + 8, 50 + 8), BorderTexture, ScaleMode.StretchToFill);
GUI.DrawTexture(new Rect(50, yOffs, Screen.width - 100, 50), BackTexture, ScaleMode.StretchToFill);
GUI.DrawTexture(new Rect(50, yOffs, (Screen.width - 100) * percent / 100f, 50), FrontTexture, ScaleMode.StretchToFill);
GUI.Label(new Rect(75, yOffs, Screen.width - 50, 50), text, BigTextStyle);
}
public static void DisplayMessage(string msg)
{
CreateTextStyle();
int yOffs = Screen.height - 75;
GUI.Label(new Rect(75, yOffs, Screen.width - 50, 50), msg, BigTextStyle);
}
public static void Text(string msg, int x, int y) => Text(msg, x, y, ValheimColor);
public static void Text(string msg, int x, int y, Color color)
{
CreateTextStyle();
NormalTextStyle.normal.textColor = color;
GUI.Label(new Rect(x, y, Screen.width - 50, 50), msg, NormalTextStyle);
}
public static bool Button(string label, int x, int y)
{
return GUI.Button(new Rect(x, y, ButtonWidth, ButtonHeight), label);
}
private static void CreateTextStyle()
{
if (BigTextStyle != null)
{
return;
}
BigTextStyle = new GUIStyle(GUI.skin.label) {fontSize = 40, fontStyle = FontStyle.Bold};
BigTextStyle.font = Resources.FindObjectsOfTypeAll<Text>()
.Select(t => t.font)
.FirstOrDefault(f => f.name == "AveriaSerifLibre-Bold") ?? BigTextStyle.font;
;
// Trying to assign alignment crashes with method not found exception
// BigTextStyle.alignment = TextAnchor.MiddleCenter;
BigTextStyle.normal.textColor = Color.Lerp(ValheimColor, Color.white, 0.75f);
NormalTextStyle = new GUIStyle(BigTextStyle) {fontSize = 20, fontStyle = FontStyle.Normal};
}
}
}