-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathMODStyleManager.cs
301 lines (262 loc) · 8.56 KB
/
MODStyleManager.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
namespace MOD.Scripts.UI
{
public class MODStyleManager
{
private static MODStyleManager _instance;
/// <summary>
/// Only call this function from within an OnGUI() context, as Unity gives an error if you try to create styles elsewhere.
/// </summary>
public static MODStyleManager OnGUIInstance => _instance ?? (_instance = new MODStyleManager());
public class StyleGroup
{
public int menuHeight;
public int menuWidth;
public float toolTipShrinkage;
public float toastWidth;
public GUIStyle modMenuSelectionGrid; // Used for SelectionGrid widgets
public GUIStyle errorLabel;
public GUIStyle button;
public GUIStyle selectedButton;
public GUIStyle label; //Used for normal Label widgets
public GUIStyle labelRightAlign;
public GUIStyle headingLabel;
public GUIStyle upperLeftHeadingLabel;
public GUIStyle bigToastLabelStyle; // Styles used for Toasts (text popups)
public GUIStyle smallToastLabelStyle;
public GUIStyle textField;
}
// Styles used for the Mod menu
public GUIStyle modMenuAreaStyle; //Used for Area widgets
public GUIStyle modMenuAreaStyleLight;
public GUIStyle modMenuAreaStyleTransparent;
public Texture2D modGUIBackgroundTexture;
public Texture2D modGUIBackgroundTextureTransparent;
public Texture2D modGUIBackgroundTextureLight;
// These style groups use the naming convention:
// style[VERTICAL_SCREEN_HEIGHT]_[MENU_WIDTH]x[MENU_HEIGHT]
StyleGroup style1080_1200x950;
StyleGroup style720_1000x660;
StyleGroup style720_960x660;
StyleGroup style480_850x480;
StyleGroup style480_640x480;
StyleGroup style2160_2400x1900;
public StyleGroup Group
{
get
{
if (Screen.height >= 2160 && Screen.width >= 2400)
{
return style2160_2400x1900;
}
else if (Screen.height >= 1080 && Screen.width >= 1200)
{
return style1080_1200x950;
}
else if (Screen.height >= 720 && Screen.width >= 1000)
{
return style720_1000x660;
}
else if(Screen.height >= 720 && Screen.width >= 960)
{
return style720_960x660;
}
else if(Screen.height >= 480 && Screen.width >= 850)
{
return style480_850x480;
}
else
{
return style480_640x480;
}
}
}
public float baseFontSize = 14;
private MODStyleManager()
{
int width = 10;
int height = 10;
Color[] pix = new Color[width * height];
for (int i = 0; i < pix.Length; i++)
{
pix[i] = new Color(0.0f, 0.0f, 0.0f, 0.0f);
}
modGUIBackgroundTextureTransparent = new Texture2D(width, height);
modGUIBackgroundTextureTransparent.SetPixels(pix);
modGUIBackgroundTextureTransparent.Apply();
for (int i = 0; i < pix.Length; i++)
{
pix[i] = new Color(0.7f, 0.7f, 0.7f);
}
for (int y = 2; y < height-2; y++)
{
for (int x = 2; x < width-2; x++)
{
pix[x + y * width] = new Color(0.0f, 0.0f, 0.0f, 0.9f);
}
}
modGUIBackgroundTexture = new Texture2D(width, height);
modGUIBackgroundTexture.SetPixels(pix);
modGUIBackgroundTexture.Apply();
for (int i = 0; i < pix.Length; i++)
{
pix[i] = new Color(1.0f, 1.0f, 1.0f, 0.7f);
}
for (int y = 2; y < height-2; y++)
{
for (int x = 2; x < width-2; x++)
{
pix[x + y * width] = new Color(0.0f, 0.0f, 0.0f, 0.7f);
}
}
modGUIBackgroundTextureLight = new Texture2D(width, height);
modGUIBackgroundTextureLight.SetPixels(pix);
modGUIBackgroundTextureLight.Apply();
style480_640x480 = GenerateWidgetStyles(
menuWidth: 640,
menuHeight: 480,
guiScale: .8f,
margin: new RectOffset(0, 0, 0, 0),
padding: new RectOffset(0, 0, 0, 0),
toolTipShrinkage: .15f
);
style480_850x480 = GenerateWidgetStyles(
menuWidth: 850,
menuHeight: 480,
guiScale: .9f,
margin: new RectOffset(0, 0, 0, 0),
padding: new RectOffset(0, 0, 0, 0)
);
style720_960x660 = GenerateWidgetStyles(
menuWidth: 960,
menuHeight: 660,
guiScale: 1.1f,
margin: new RectOffset(1, 1, 1, 1),
padding: new RectOffset(1, 1, 1, 1)
);
style720_1000x660 = GenerateWidgetStyles(
menuWidth: 1000,
menuHeight: 660,
guiScale: 1.1f,
margin: new RectOffset(1, 1, 1, 1),
padding: new RectOffset(1, 1, 1, 1)
);
style1080_1200x950 = GenerateWidgetStyles(
menuWidth: 1200,
menuHeight: 950,
guiScale: 1.25f,
margin: new RectOffset(2, 2, 2, 2),
padding: new RectOffset(2, 2, 2, 2)
);
style2160_2400x1900 = GenerateWidgetStyles(
menuWidth: 2400,
menuHeight: 1900,
guiScale: 2.5f,
margin: new RectOffset(4, 4, 4, 4),
padding: new RectOffset(4, 4, 4, 4)
);
//OnGUIGetLabelStyle();
OnGUIGetModStyle();
}
private void OnGUIGetModStyle()
{
modMenuAreaStyle = new GUIStyle(GUI.skin.box) //Copy the default style for 'box' as a base
{
//alignment = TextAnchor.UpperCenter,
//fontSize = 40,
//fontStyle = FontStyle.Bold,
};
modMenuAreaStyle.normal.background = modGUIBackgroundTexture;
modMenuAreaStyle.normal.textColor = Color.white;
modMenuAreaStyle.wordWrap = true;
modMenuAreaStyleLight = new GUIStyle(modMenuAreaStyle);
modMenuAreaStyleLight.normal.background = modGUIBackgroundTextureLight;
modMenuAreaStyleTransparent = new GUIStyle(modMenuAreaStyle);
modMenuAreaStyleTransparent.normal.background = modGUIBackgroundTextureTransparent;
}
private StyleGroup GenerateWidgetStyles(int menuWidth, int menuHeight, float guiScale, RectOffset margin, RectOffset padding, float toolTipShrinkage = 0)
{
// Button style
GUIStyle buttonStyle = new GUIStyle(GUI.skin.button);
buttonStyle.fontSize = Mathf.RoundToInt(guiScale * baseFontSize);
// Selected button style (to show if a the option a button represents is activated)
GUIStyle selectedButtonStyle = new GUIStyle(buttonStyle);
selectedButtonStyle.normal.textColor = Color.green;
selectedButtonStyle.hover.textColor = Color.green;
// Label style
GUIStyle labelStyle = new GUIStyle(GUI.skin.label)
{
fontSize = Mathf.RoundToInt(guiScale * baseFontSize),
margin = margin,
padding = padding,
};
GUIStyle labelStyleRightAlign = new GUIStyle(labelStyle)
{
alignment = TextAnchor.MiddleRight,
};
// Textfield style
GUIStyle textFieldStyle = new GUIStyle(GUI.skin.textField)
{
fontSize = Mathf.RoundToInt(guiScale * baseFontSize),
margin = margin,
padding = padding,
};
// Heading text style
GUIStyle headingLabelStyle = new GUIStyle(labelStyle)
{
fontStyle = FontStyle.Bold,
alignment = TextAnchor.LowerCenter,
};
headingLabelStyle.padding.top *= 5;
GUIStyle upperLeftHeadingLabelStyle = new GUIStyle(headingLabelStyle);
upperLeftHeadingLabelStyle.alignment = TextAnchor.UpperLeft;
// Menu selection grid/radio
GUIStyle modMenuSelectionGrid = new GUIStyle(GUI.skin.button) //Copy the default style for 'box' as a base
{
//alignment = TextAnchor.UpperCenter,
fontSize = Mathf.RoundToInt(guiScale * baseFontSize),
};
modMenuSelectionGrid.onHover.textColor = Color.green;
modMenuSelectionGrid.onNormal.textColor = Color.green; // Color of a selected option
// Error label (this is used for the description on the right hand side if the game encounters an error
GUIStyle errorLabelStyle = new GUIStyle(GUI.skin.label);
errorLabelStyle.normal.textColor = Color.red;
errorLabelStyle.fontSize = Mathf.RoundToInt(guiScale * baseFontSize);
// Toast popup styles
GUIStyle bigToastLabelStyle = new GUIStyle(GUI.skin.box) //Copy the default style for 'box' as a base
{
alignment = TextAnchor.UpperCenter,
fontSize = Mathf.RoundToInt(guiScale * 3 * baseFontSize),
fontStyle = FontStyle.Bold,
wordWrap = true,
};
bigToastLabelStyle.normal.background = modGUIBackgroundTexture;
bigToastLabelStyle.normal.textColor = Color.white;
GUIStyle smallToastLabelStyle = new GUIStyle(bigToastLabelStyle)
{
fontSize = bigToastLabelStyle.fontSize / 2,
};
return new StyleGroup()
{
menuWidth = menuWidth,
menuHeight = menuHeight,
toastWidth = menuWidth,
modMenuSelectionGrid = modMenuSelectionGrid,
errorLabel = errorLabelStyle,
button = buttonStyle,
selectedButton = selectedButtonStyle,
label = labelStyle,
labelRightAlign = labelStyleRightAlign,
headingLabel = headingLabelStyle,
upperLeftHeadingLabel = upperLeftHeadingLabelStyle,
toolTipShrinkage = toolTipShrinkage,
bigToastLabelStyle = bigToastLabelStyle,
smallToastLabelStyle = smallToastLabelStyle,
textField = textFieldStyle,
};
}
}
}