generated from kianzarrin/NodeController
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUUIHelpers.cs
505 lines (447 loc) · 23.7 KB
/
UUIHelpers.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("NetworkMultitool")]
[assembly: InternalsVisibleTo("NodeController")]
[assembly: InternalsVisibleTo("NodeMarkup")]
[assembly: InternalsVisibleTo("BuildingSpawnPoints")]
[assembly: InternalsVisibleTo("NoBigTruck")] // is it needed?
namespace UnifiedUI.Helpers {
using ColossalFramework;
using ColossalFramework.Plugins;
using ColossalFramework.UI;
using ICities;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using UnityEngine;
using KianCommons.UI;
public static class UUIHelpers {
#region macsurgey compatibility
[Obsolete("use UnifiedUI.Helpers.UUISprites instead", error:true)]
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
internal struct UUISprites {
public UITextureAtlas Atlas;
public string NormalSprite, HoveredSprite, PressedSprite, DisabledSprite;
public Helpers.UUISprites Convert() {
return new Helpers.UUISprites {
Atlas = Atlas,
NormalSprite = NormalSprite, HoveredSprite = HoveredSprite, PressedSprite = PressedSprite, DisabledSprite = DisabledSprite
};
}
}
[Obsolete("use UnifiedUI.Helpers.UUISprites instead", error: true)]
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
internal static UIComponent RegisterToolButton(
string name, string groupName, string tooltip, UUISprites sprites, ToolBase tool,
SavedInputKey activationKey, IEnumerable<SavedInputKey> activeKeys) {
var hotkeys = new UUIHotKeys { ActivationKey = activationKey };
foreach (var item in activeKeys)
hotkeys.AddInToolKey(item);
return RegisterToolButton(
name: name,
groupName: groupName,
tooltip: tooltip,
tool: tool,
sprites: sprites.Convert(),
hotkeys: hotkeys);
}
#endregion
const string UUI_NAME = "UnifiedUI.API.UUIAPI";
const string ASSEMLY_NAME = "UnifiedUILib";
/// <typeparam name="TDelegate">delegate type</typeparam>
/// <returns>Type[] representing arguments of the delegate.</returns>
internal static Type[] GetParameterTypes<TDelegate>()
where TDelegate : Delegate =>
typeof(TDelegate)
.GetMethod("Invoke")
.GetParameters()
.Select(p => p.ParameterType)
.ToArray();
/// <summary>
/// Gets directly declared method based on a delegate that has
/// the same name as the target method
/// </summary>
/// <param name="type">the class/type where the method is declared</param>
/// <param name="name">the name of the method</param>
internal static MethodInfo GetMethod<TDelegate>(this Type type, string name) where TDelegate : Delegate {
return type.GetMethod(
name,
types: GetParameterTypes<TDelegate>())
?? throw new Exception("could not find method " + name);
}
internal static TDelegate CreateDelegate<TDelegate>(Type type, string name) where TDelegate : Delegate {
var method = type.GetMethod<TDelegate>(name);
return (TDelegate)Delegate.CreateDelegate(typeof(TDelegate), method);
}
internal static IEnumerable<PluginManager.PluginInfo> Plugins => PluginManager.instance.GetPluginsInfo();
internal static PluginManager.PluginInfo GetUUIPlugin() =>
Plugins.FirstOrDefault(p => p.IsUUIMod());
static bool IsUUIMod(this PluginManager.PluginInfo p) =>
p?.userModInstance?.GetType()?.Assembly?.GetType(UUI_NAME) != null;
static bool IsUUILib(this Assembly assembly) => assembly.GetName().Name == ASSEMLY_NAME;
internal static Assembly GetUUILib() {
Assembly ret = null;
if (IsUUIEnabled()) {
ret = GetUUIPlugin().GetAssemblies().First(IsUUILib);
Debug.Log($"using {ret} from UnifiedUI Mod");
return ret;
}
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (var assembly in assemblies.Where(IsUUILib)) {
if (ret == null || ret.GetName().Version < assembly.GetName().Version)
ret = assembly;
}
if (ret == null ) {
string sAssemblies = string.Join("\n", assemblies.Select(asm => asm.ToString()).ToArray());
throw new Exception($"failed to get latest {ASSEMLY_NAME}. assemblies are:\n" + sAssemblies);
}
Debug.Log($"using latest {ASSEMLY_NAME} version: {ret}");
return ret;
}
internal static Type GetUUI() =>
GetUUILib().GetType(UUI_NAME, throwOnError: true);
#region register with FileName
internal delegate UIComponent RegisterCustomHandler
(string name, string groupName, string tooltip, string spritefile,
Action<bool> onToggle, Action<ToolBase> onToolChanged,
SavedInputKey activationKey, Dictionary<SavedInputKey, Func<bool>> activeKeys);
internal delegate UIComponent RegisterToolHandler
(string name, string groupName, string tooltip, string spritefile, ToolBase tool,
SavedInputKey activationKey, Dictionary<SavedInputKey, Func<bool>> activeKeys);
/// <summary>
/// register a button to tie to the given tool.
/// </summary>
/// <param name="name">game object name for button</param>
/// <param name="groupName">the group under which button will be added. use null to add to the default group.</param>
/// <param name="spritefile">full path to the file that contains 4 40x40x button sprites(see example)</param>
/// <param name="tool">the tool to tie the button to.</param>
/// <param name="activationKey">hot key to trigger the button</param>
/// <param name="activeKeys">turn off these hotkeys in other mods</param>
/// <returns>component containing the button. you can hide this component if necessary.</returns>
[Obsolete]
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
public static UIComponent RegisterToolButton(
string name, string groupName, string tooltip, string spritefile, ToolBase tool,
SavedInputKey activationKey = null, Dictionary<SavedInputKey, Func<bool>> activeKeys = null) {
var Register = CreateDelegate<RegisterToolHandler>(GetUUI(), "Register");
return Register(
name: name,
groupName: groupName,
tooltip: tooltip,
spritefile: spritefile,
tool: tool,
activationKey: activationKey,
activeKeys: activeKeys);
}
/// <summary>
/// register a button to tie to the given tool.
/// </summary>
/// <param name="name">game object name for button</param>
/// <param name="groupName">the group under which button will be added. use null to add to the default group.</param>
/// <param name="spritefile">full path to the file that contains 4 40x40x button sprites(see example)</param>
/// <param name="tool">the tool to tie the button to.</param>
/// <param name="activationKey">hot key to trigger the button</param>
/// <param name="activeKeys">turn off these hotkeys in other mods</param>
/// <returns>component containing the button. you can hide this component if necessary.</returns>
[Obsolete]
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
public static UIComponent RegisterToolButton(
string name, string groupName, string tooltip, string spritefile, ToolBase tool,
SavedInputKey activationKey, IEnumerable<SavedInputKey> activeKeys) {
var activeKeys2 = new Dictionary<SavedInputKey, Func<bool>>();
foreach (var key in activeKeys)
activeKeys2[key] = null;
return RegisterToolButton(
name: name,
groupName: groupName,
tooltip: tooltip,
spritefile: spritefile,
tool: tool,
activationKey: activationKey,
activeKeys: activeKeys2);
}
/// <summary>
/// register a custom button .
/// </summary>
/// <param name="name">game object name for button</param>
/// <param name="groupName">the group under which button will be added. use null to add to the default group.</param>
/// <param name="spritefile">full path to the file that contains 4 40x40x button sprites(see example)</param>
/// <param name="onToggle">call-back for when the button is activated/deactivated</param>
/// <param name="onToolChanged">call-back for when any active tool changes.</param>
/// <param name="activationKey">hot key to trigger the button</param>
/// <param name="activeKeys">hotkey->active dictionary. turns off these hotkeys in other mods while active</param>
/// <returns>wrapper for the button which you can use to change the its state.</returns>
[Obsolete]
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
public static UUICustomButton RegisterCustomButton(
string name, string groupName, string tooltip, string spritefile,
Action<bool> onToggle, Action<ToolBase> onToolChanged = null,
SavedInputKey activationKey = null, Dictionary<SavedInputKey, Func<bool>> activeKeys = null) {
var Register = CreateDelegate<RegisterCustomHandler>(GetUUI(), "Register");
UIComponent component = Register(
name: name,
groupName: groupName,
tooltip: tooltip,
spritefile: spritefile,
onToggle: onToggle,
onToolChanged: onToolChanged,
activationKey: activationKey,
activeKeys: activeKeys);
return new UUICustomButton(component);
}
/// <summary>
/// register a custom button .
/// </summary>
/// <param name="name">game object name for button</param>
/// <param name="groupName">the group under which button will be added. use null to add to the default group.</param>
/// <param name="spritefile">full path to the file that contains 4 40x40x button sprites(see example)</param>
/// <param name="onToggle">call-back for when the button is activated/deactivated</param>
/// <param name="onToolChanged">call-back for when any active tool changes.</param>
/// <param name="activationKey">hot key to trigger the button</param>
/// <param name="activeKeys">hotkey->active dictionary. turns off these hotkeys in other mods while active</param>
/// <returns>wrapper for the button which you can use to change the its state.</returns>
[Obsolete]
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
public static UUICustomButton RegisterCustomButton(
string name, string groupName, string tooltip, string spritefile,
Action<bool> onToggle, Action<ToolBase> onToolChanged,
SavedInputKey activationKey, IEnumerable<SavedInputKey> activeKeys) {
var activeKeys2 = new Dictionary<SavedInputKey, Func<bool>>();
foreach (var key in activeKeys)
activeKeys2[key] = null;
return RegisterCustomButton(
name: name,
groupName: groupName,
tooltip: tooltip,
spritefile: spritefile,
onToggle: onToggle,
onToolChanged: onToolChanged,
activationKey: activationKey,
activeKeys: activeKeys2);
}
#endregion
#region Register with atlas
internal delegate UIComponent RegisterCustomHandler2
(string name, string groupName, string tooltip,
UITextureAtlas atlas, string[] spriteNames,
Action<bool> onToggle, Action<ToolBase> onToolChanged,
SavedInputKey activationKey, Dictionary<SavedInputKey, Func<bool>> activeKeys);
internal delegate UIComponent RegisterToolHandler2
(string name, string groupName, string tooltip, ToolBase tool,
UITextureAtlas atlas, string[] spriteNames,
SavedInputKey activationKey, Dictionary<SavedInputKey, Func<bool>> activeKeys);
/// <summary>
/// register a button to tie to the given tool.
/// </summary>
/// <param name="name">game object name for button</param>
/// <param name="groupName">the group under which button will be added. use null to add to the default group.</param>
/// <param name="tool">the tool to tie the button to.</param>
/// <returns>component containing the button. you can hide this component if necessary.</returns>
[Obsolete]
public static UIComponent RegisterToolButton(
string name, string groupName, string tooltip, ToolBase tool, Helpers.UUISprites sprites, UUIHotKeys hotkeys = null) {
var Register = CreateDelegate<RegisterToolHandler2>(GetUUI(), "Register");
return Register(
name: name,
groupName: groupName,
tooltip: tooltip,
tool: tool,
atlas: sprites.Atlas,
spriteNames: sprites.SpriteNames,
activationKey: hotkeys?.ActivationKey,
activeKeys: hotkeys?.InToolKeys);
}
/// <summary>
/// register a custom button .
/// </summary>
/// <param name="name">game object name for button</param>
/// <param name="groupName">the group under which button will be added. use null to add to the default group.</param>
/// <param name="onToggle">call-back for when the button is activated/deactivated</param>
/// <param name="onToolChanged">call-back for when any active tool changes.</param>
/// <returns>wrapper for the button which you can use to change the its state.</returns>
[Obsolete]
public static UUICustomButton RegisterCustomButton(
string name, string groupName, string tooltip, Helpers.UUISprites sprites,
Action<bool> onToggle, Action<ToolBase> onToolChanged = null,
UUIHotKeys hotkeys = null) {
var Register = CreateDelegate<RegisterCustomHandler2>(GetUUI(), "Register");
UIComponent component = Register(
name: name,
groupName: groupName,
tooltip: tooltip,
atlas: sprites.Atlas,
spriteNames: sprites.SpriteNames,
onToggle: onToggle,
onToolChanged: onToolChanged,
activationKey: hotkeys?.ActivationKey,
activeKeys: hotkeys?.InToolKeys);
return new UUICustomButton(component);
}
#endregion
#region Register with Icon
internal delegate UIComponent RegisterToolHandler3
(string name, string groupName, string tooltip, ToolBase tool,
Texture2D texture,
SavedInputKey activationKey, Dictionary<SavedInputKey, Func<bool>> activeKeys);
internal delegate UIComponent RegisterCustomHandler3
(string name, string groupName, string tooltip, Texture2D texture,
Action<bool> onToggle, Action<ToolBase> onToolChanged,
SavedInputKey activationKey, Dictionary<SavedInputKey, Func<bool>> activeKeys);
/// <summary>
/// register a button to tie to the given tool.
/// </summary>
/// <param name="name">game object name for button. must be unique.</param>
/// <param name="groupName">the group under which button will be added. use null to add to the default group.</param>
/// <param name="tool">the tool to tie the button to.</param>
/// <returns>component containing the button. you can hide this component if necessary.</returns>
public static UIComponent RegisterToolButton(
string name, string groupName, string tooltip, ToolBase tool, Texture2D icon, UUIHotKeys hotkeys = null) {
var Register = CreateDelegate<RegisterToolHandler3>(GetUUI(), "Register");
return Register(
name: name,
groupName: groupName,
tooltip: tooltip,
tool: tool,
texture: icon,
activationKey: hotkeys?.ActivationKey,
activeKeys: hotkeys?.InToolKeys);
}
/// <summary>
/// register a custom button .
/// </summary>
/// <param name="name">game object name for button</param>
/// <param name="groupName">the group under which button will be added. use null to add to the default group.</param>
/// <param name="onToggle">call-back for when the button is activated/deactivated</param>
/// <param name="onToolChanged">call-back for when any active tool changes.</param>
/// <returns>wrapper for the button which you can use to change the its state.</returns>
public static UUICustomButton RegisterCustomButton(
string name, string groupName, string tooltip, Texture2D icon,
Action<bool> onToggle, Action<ToolBase> onToolChanged = null,
UUIHotKeys hotkeys = null) {
var Register = CreateDelegate<RegisterCustomHandler3>(GetUUI(), "Register");
UIComponent component = Register(
name: name,
groupName: groupName,
tooltip: tooltip,
texture: icon,
onToggle: onToggle,
onToolChanged: onToolChanged,
activationKey: hotkeys?.ActivationKey,
activeKeys: hotkeys?.InToolKeys);
return new UUICustomButton(component);
}
#endregion
internal delegate void AttachAlienHandler(UIComponent alien, string groupName);
public static void AttachAlien(UIComponent alien, string groupName) {
var attachAlien = CreateDelegate<AttachAlienHandler>(GetUUI(), "AttachAlien");
attachAlien(alien: alien, groupName: groupName);
}
internal delegate void RegisterHotkeysHandler(Action onToggle,
SavedInputKey activationKey, Dictionary<SavedInputKey, Func<bool>> activeKeys);
/// <summary>
/// register hotkeys.
/// </summary>
/// <param name="onToggle">call back for when activationKey is pressed.</param>
/// <param name="onToolChanged">call-back for when any active tool changes.</param>
/// <param name="activationKey">hot key to toggle</param>
/// <param name="activeKeys">hotkey->active dictionary. turns off these hotkeys in other mods while active</param>
public static void RegisterHotkeys(
Action onToggle, SavedInputKey activationKey = null, Dictionary<SavedInputKey, Func<bool>> activeKeys = null) {
var Register = CreateDelegate<RegisterHotkeysHandler>(GetUUI(), "Register");
Register(
onToggle: onToggle,
activationKey: activationKey,
activeKeys: activeKeys);
}
/// <summary>
/// Destroy all gameObjects, components, and children
/// </summary>
public static void Destroy(this Component button) {
if (button) GameObject.Destroy(button.gameObject);
}
/// <summary>
/// Gets the path to the mod that has the user mod instance.
/// </summary>
/// <param name="userModInstance">instance of IUserMod</param>
/// <returns>path to mod</returns>
public static string GetModPath(this IUserMod userModInstance) =>
Plugins.FirstOrDefault(p => p?.userModInstance == userModInstance)?.modPath;
public static string GetModPath<UserModT>() where UserModT : IUserMod =>
Plugins.FirstOrDefault(p => p?.userModInstance is UserModT)?.modPath;
/// <summary>
/// Gets the full path to a file from the input mod
/// </summary>
/// <param name="userModInstance">instance of IUserMod</param>
/// <param name="paths">directories,file names to combine</param>
/// <returns>full path to file.</returns>
public static string GetFullPath(this IUserMod userModInstance, params string[] paths) {
string ret = userModInstance.GetModPath();
foreach (string path in paths)
ret = Path.Combine(ret, path);
return ret;
}
/// <summary>
/// Gets the full path to a file from the input mod
/// </summary>
/// <typeparam name="UserModT">user mod type</typeparam>
/// <param name="paths">directories/files to combine</param>
/// <returns>full path to file.</returns>
public static string GetFullPath<UserModT>(params string[] paths) where UserModT : IUserMod {
string ret = GetModPath<UserModT>();
foreach(string path in paths)
ret = Path.Combine(ret, path);
return ret;
}
public static Texture2D LoadTexture(string filePath) {
var t = TextureUtil.GetTextureFromFile(filePath);
t.name = Path.GetFileNameWithoutExtension(filePath);
return t;
}
/// <summary>
/// test if UnifiedUI is present and enable.
/// </summary>
public static bool IsUUIEnabled() {
var uui = GetUUIPlugin();
return uui != null && uui.isEnabled;
}
#region key activated
private static void Reset() {
keyActivatedDelegate_ = null;
PluginManager.instance.eventPluginsStateChanged -= Reset;
PluginManager.instance.eventPluginsChanged -= Reset;
LoadingManager.instance.m_levelPreLoaded -= Reset;
}
internal static KeyActivatedDelegate GetKeyActivatedDelegate() {
if (keyActivatedDelegate_ != null) {
return keyActivatedDelegate_; // return cached uui_
}
keyActivatedDelegate_ = CreateDelegate<KeyActivatedDelegate>(GetUUI(), "KeyActivated");
if (keyActivatedDelegate_ != null) {
// if uui_ is cached, then reset it if something changed.
// useful for hot-reload.
PluginManager.instance.eventPluginsStateChanged -= Reset;
PluginManager.instance.eventPluginsChanged -= Reset;
LoadingManager.instance.m_levelPreLoaded -= Reset;
PluginManager.instance.eventPluginsStateChanged += Reset;
PluginManager.instance.eventPluginsChanged += Reset;
LoadingManager.instance.m_levelPreLoaded += Reset;
}
return keyActivatedDelegate_;
}
internal delegate bool KeyActivatedDelegate(SavedInputKey key);
private static KeyActivatedDelegate keyActivatedDelegate_;
/// <summary>
/// checks if hotkey is activated on key Down/Up depending on UUI's user settings.
/// </summary>
public static bool KeyActivated(this SavedInputKey key) {
var KeyActivated = GetKeyActivatedDelegate();
if (KeyActivated != null) {
return KeyActivated(key);
} else {
return key.IsKeyUp();
}
}
#endregion key activated
}
}