-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathHudLayout.cs
415 lines (360 loc) · 11.2 KB
/
HudLayout.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
using System.Reflection;
using ElectronNET.API;
using ElectronNET.API.Entities;
using Newtonsoft.Json;
using ReHUD.Models;
namespace ReHUD;
public class HudLayout : JsonUserData
{
private static readonly Dictionary<string, HudLayout> layouts = new();
public static IEnumerable<HudLayout> Layouts => layouts.Values;
public static HudLayout? ActiveHudLayout { get; private set; }
public static HudLayout? ReplayHudLayout { get; private set; }
public static HudLayout? BeforeReplayHudLayout { get; private set; }
public string Name { get; private set; }
private bool active = false;
[JsonProperty("active")]
public bool Active
{
get
{
return active;
}
set
{
if (value && Startup.IsInEditMode) return;
if (value && !active)
{
SetActiveLayout(this, false);
}
active = value;
}
}
private bool isReplayLayout = false;
[JsonProperty("isReplayLayout")]
public bool IsReplayLayout {
get {
return isReplayLayout;
}
set {
if (value && !isReplayLayout)
{
SetReplayLayout(this, false);
}
isReplayLayout = value;
}
}
[JsonProperty]
private readonly Dictionary<string, HudElement> elements = new();
public static readonly string subPath = "LayoutPresets";
public static string FullPath => Path.Combine(dataPath, subPath);
public override string SubPath => subPath;
protected override string DataFilePath => Name + ".json";
// <summary>
// Loads an existing HUD layout
// </summary>
public HudLayout(string name)
{
Name = name;
Load();
}
// <summary>
// Creates a new HUD layout with a free name
// </summary>
public HudLayout(bool active)
{
Name = GetFreeId() ?? throw new InvalidOperationException("Failed to get free HUD layout ID");
this.active = active;
Save();
}
// <summary>
// Creates a new HUD layout with a specified name
// </summary>
public HudLayout(string name, bool active)
{
Name = name;
this.active = active;
Save();
}
public async Task<bool> Rename(string newName)
{
if (newName == Name) return true;
if (NameTaken(newName))
{
await Startup.ShowMessageBox(Startup.SettingsWindow, $"Layout with name {newName} already exists");
return false;
}
var oldPath = PathTo(Name);
VerifyDirectory();
if (File.Exists(oldPath))
{
File.Move(oldPath, PathTo(newName));
}
layouts.Remove(Name);
if (layouts.ContainsKey(newName))
{
layouts.Remove(newName);
}
Name = newName;
layouts.Add(newName, this);
return true;
}
public async Task<bool> DeleteLayout() {
var resp = await Startup.ShowMessageBox("Are you sure you want to delete '" + Name + "'?", new string[] { "Yes", "No" }, "Delete HUD layout", MessageBoxType.warning);
if (resp.Response == 0)
{
var res = Delete();
if (!res) return false;
layouts.Remove(Name);
if (ActiveHudLayout == this)
{
if (layouts.Count > 0)
{
SetActiveLayout(layouts.Values.First());
}
else
{
ActiveHudLayout = null;
}
}
return true;
}
return false;
}
protected override void Load(string? data)
{
if (data == null) return;
JsonConvert.PopulateObject(data, this);
}
public void Reset()
{
elements.Clear();
}
public void Cancel() {
Load();
}
public HudElement AddElement(string id, double? left, double? top, double scale, bool shown)
{
var element = new HudElement(left, top, scale, shown);
elements.Add(id, element);
return element;
}
public HudElement? GetElement(string id)
{
return elements.ContainsKey(id) ? elements[id] : null;
}
public HudElement? RemoveElement(string id)
{
if (!elements.ContainsKey(id)) return null;
var element = elements[id];
elements.Remove(id);
return element;
}
public void CopyFrom(HudLayout hudLayout)
{
if (this == hudLayout) return;
if (hudLayout.Name != Name)
{
Startup.logger.ErrorFormat("Attempted to copy HUD layout with different ID: {0} != {1}", hudLayout.Name, Name);
return;
}
IsReplayLayout = hudLayout.IsReplayLayout;
UpdateElements(hudLayout.elements);
}
public void UpdateElements(Dictionary<string, HudElement> elements)
{
if (this.elements == elements) return;
this.elements.Clear();
foreach (var element in elements)
{
this.elements.Add(element.Key, element.Value);
}
}
public string SerializeElements()
{
return JsonConvert.SerializeObject(elements);
}
public static string SerializeLayouts(bool includeName = false)
{
if (includeName)
{
return JsonConvert.SerializeObject(layouts.Values.Select(layout => new { name = layout.Name, active = layout.Active, isReplayLayout = layout.IsReplayLayout, layout.elements }));
}
return JsonConvert.SerializeObject(layouts);
}
public static void SaveAll()
{
foreach (var hudLayout in layouts.Values)
{
hudLayout.Save();
}
}
public static void VerifyDirectory()
{
Directory.CreateDirectory(Path.Join(dataPath, subPath));
}
public static void LoadHudLayouts()
{
VerifyDirectory();
foreach (var file in Directory.GetFiles(Path.Join(dataPath, subPath), "*.json"))
{
AddHudLayout(new HudLayout(Path.GetFileNameWithoutExtension(file)));
}
if (ActiveHudLayout == null && layouts.Count > 0)
{
SetActiveLayout(layouts.Values.First());
}
}
public static bool SetActiveLayout(HudLayout layout, bool setActive = true)
{
Startup.logger.InfoFormat("Setting active layout to {0}", layout.Name);
if (Startup.IsInEditMode) {
Startup.logger.Warn("Not setting active layout because we're in edit mode");
return false;
}
if (ExistsAndIsDifferent(layout) != null)
{
Startup.logger.WarnFormat("Attempted to set active layout to a layout different than the one in the list: {0}", layout.Name);
return false;
}
ActiveHudLayout = layout;
SetSingularBoolForLayout(layout, "active", setActive);
return true;
}
public static void SetReplayLayout(HudLayout layout, bool setReplay = true)
{
ReplayHudLayout = layout;
SetSingularBoolForLayout(layout, "isReplayLayout", setReplay);
}
public static void SetSingularBoolForLayout(HudLayout layout, string property, bool setValue = true)
{
var prop = typeof(HudLayout).GetField(property, BindingFlags.NonPublic | BindingFlags.Instance);
if (prop == null) {
Startup.logger.ErrorFormat("Failed to find property '{0}' in HudLayout", property);
return;
}
if (setValue)
{
prop.SetValue(layout, true);
}
foreach (var hudLayout in layouts.Values)
{
if (hudLayout != layout)
{
prop.SetValue(hudLayout, false);
hudLayout.Save();
}
}
layout.Save();
}
public static HudLayout? LoadReplayLayout()
{
if (ReplayHudLayout != null)
{
var before = ActiveHudLayout;
var res = SetActiveLayout(ReplayHudLayout, true);
if (!res) return null;
BeforeReplayHudLayout = before;
Startup.logger.InfoFormat("Loaded replay layout: {0}", ReplayHudLayout.Name);
return ReplayHudLayout;
}
return null;
}
public static HudLayout? UnloadReplayLayout()
{
if (BeforeReplayHudLayout != null)
{
var layout = BeforeReplayHudLayout;
var res = SetActiveLayout(layout);
if (!res) return null;
BeforeReplayHudLayout = null;
Startup.logger.InfoFormat("Loaded normal layout: {0}", layout.Name);
return layout;
}
return null;
}
private static HudLayout? ExistsAndIsDifferent(HudLayout hudLayout)
{
var existing = GetHudLayout(hudLayout.Name);
if (existing != hudLayout)
{
return existing;
}
return null;
}
public static HudLayout AddHudLayout(HudLayout hudLayout)
{
var existing = ExistsAndIsDifferent(hudLayout);
if (existing != null)
{
existing.CopyFrom(hudLayout);
return existing;
}
layouts.Add(hudLayout.Name, hudLayout);
if (hudLayout.Active || ActiveHudLayout == null)
{
var activeBefore = hudLayout.Active;
if (Startup.IsInEditMode) {
hudLayout.Active = false;
} else {
SetActiveLayout(hudLayout);
}
if (hudLayout.Active != activeBefore)
{
hudLayout.Save();
}
}
return hudLayout;
}
public static HudLayout? GetHudLayout(string name)
{
return layouts.ContainsKey(name) ? layouts[name] : null;
}
public static HudLayout UpdateActiveHudLayout(HudLayout newLayout)
{
HudLayout layout = ActiveHudLayout ?? throw new InvalidOperationException("No active HUD layout");
layout.CopyFrom(newLayout);
return layout;
}
private static new string PathTo(string name)
{
return Path.Combine(dataPath, subPath, $"{name}.json");
}
private static bool NameTaken(string name)
{
return layouts.ContainsKey(name) || File.Exists(PathTo(name));
}
public static string? GetFreeId()
{
for (int i = 1; i < 100; i++)
{
var layoutName = $"LayoutPreset{i}";
if (!NameTaken(layoutName))
{
Startup.logger.InfoFormat("Found free name for HUD layout: {0}", layoutName);
return layoutName;
}
}
_ = Startup.QuitApp($"You have too many HUD layouts. Please remove some of them from your '{FullPath}' folder.");
return null;
}
}
[JsonObject(MemberSerialization.OptIn)]
public class HudElement
{
[JsonProperty("left")]
public double? Left { get; set; }
[JsonProperty("top")]
public double? Top { get; set; }
[JsonProperty("scale")]
public double Scale { get; set; }
[JsonProperty("shown")]
public bool Shown { get; set; }
public HudElement(double? left, double? top, double scale, bool shown)
{
this.Left = left;
this.Top = top;
this.Scale = scale;
this.Shown = shown;
}
}