forked from UnknownX7/QoLBar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Legacy.cs
436 lines (389 loc) · 13.9 KB
/
Legacy.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
using System;
using System.Numerics;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using Newtonsoft.Json;
using Dalamud.Interface;
using Dalamud.Interface.Utility;
using ImGuiNET;
#pragma warning disable CS0612 // Type or member is obsolete
namespace QoLBar;
public class BarConfig
{
[DefaultValue("")] public string Title = string.Empty;
[DefaultValue(null)] public List<Shortcut> ShortcutList = new();
[DefaultValue(false)] public bool Hidden = false;
public enum VisibilityMode
{
Slide,
Immediate,
Always
}
[DefaultValue(VisibilityMode.Always)] public VisibilityMode Visibility = VisibilityMode.Always;
public enum BarAlign
{
LeftOrTop,
Center,
RightOrBottom
}
[DefaultValue(BarAlign.Center)] public BarAlign Alignment = BarAlign.Center;
public enum BarDock
{
Top,
Left,
Bottom,
Right,
UndockedH,
UndockedV
}
[DefaultValue(BarDock.Bottom)] public BarDock DockSide = BarDock.Bottom;
[DefaultValue(false)] public bool Hint = false;
[DefaultValue(100)] public int ButtonWidth = 100;
[DefaultValue(false)] public bool HideAdd = false;
public Vector2 Position = Vector2.Zero;
[DefaultValue(false)] public bool LockedPosition = false;
public Vector2 Offset = Vector2.Zero;
[DefaultValue(1.0f)] public float Scale = 1.0f;
[DefaultValue(1.0f)] public float CategoryScale = 1.0f;
[DefaultValue(1.0f)] public float RevealAreaScale = 1.0f;
[DefaultValue(1.0f)] public float FontScale = 1.0f;
[DefaultValue(1.0f)] public float CategoryFontScale = 1.0f;
[DefaultValue(8)] public int Spacing = 8;
public Vector2 CategorySpacing = new(8, 4);
[DefaultValue(false)] public bool NoBackground = false;
[DefaultValue(false)] public bool NoCategoryBackgrounds = false;
[DefaultValue(false)] public bool OpenCategoriesOnHover = false;
[DefaultValue(false)] public bool OpenSubcategoriesOnHover = false;
[DefaultValue(-1)] public int ConditionSet = -1;
public BarCfg Upgrade()
{
var window = ImGuiHelpers.MainViewport.Size;
var oldPos = Position / window;
var oldOffset = Offset * ImGuiHelpers.GlobalScale;
var add = 0f;
switch (Alignment)
{
case BarAlign.LeftOrTop:
add = 22 + ImGui.GetFontSize();
break;
case BarAlign.RightOrBottom:
add = -22 - ImGui.GetFontSize();
break;
}
switch (DockSide)
{
case BarDock.Top:
case BarDock.Bottom:
oldOffset.X += add;
break;
case BarDock.Left:
case BarDock.Right:
oldOffset.Y += add;
break;
}
oldOffset /= window;
var bar = new BarCfg
{
Name = Title,
Hidden = Hidden,
Visibility = (BarCfg.BarVisibility)Visibility,
Hint = Hint,
ButtonWidth = ButtonWidth,
Editing = !HideAdd,
Position = (DockSide == BarDock.UndockedH || DockSide == BarDock.UndockedV) ? new[] { oldPos.X, oldPos.Y } : new[] { oldOffset.X, oldOffset.Y },
LockedPosition = DockSide != BarDock.UndockedH && DockSide != BarDock.UndockedV || LockedPosition,
Scale = Scale,
RevealAreaScale = RevealAreaScale,
FontScale = FontScale,
Spacing = new[] { Spacing, Spacing },
NoBackground = NoBackground,
ConditionSet = ConditionSet
};
bar.DockSide = DockSide switch
{
BarDock.Top => BarCfg.BarDock.Top,
BarDock.Left => BarCfg.BarDock.Left,
BarDock.Bottom => BarCfg.BarDock.Bottom,
BarDock.Right => BarCfg.BarDock.Right,
BarDock.UndockedH => BarCfg.BarDock.Top,
BarDock.UndockedV => BarCfg.BarDock.Top,
_ => BarCfg.BarDock.Top
};
bar.Alignment = DockSide switch
{
BarDock.UndockedH => BarCfg.BarAlign.LeftOrTop,
BarDock.UndockedV => BarCfg.BarAlign.LeftOrTop,
_ => (BarCfg.BarAlign)Alignment
};
switch (DockSide)
{
case BarDock.Top:
case BarDock.Bottom:
case BarDock.UndockedH:
bar.Columns = 0;
break;
case BarDock.Left:
case BarDock.Right:
case BarDock.UndockedV:
bar.Columns = 1;
break;
}
foreach (var sh in ShortcutList)
bar.ShortcutList.Add(sh.Upgrade(this, false));
return bar;
}
}
public class Shortcut
{
[DefaultValue("")] public string Name = string.Empty;
public enum ShortcutType
{
Command,
Multiline_DEPRECATED,
Category,
Spacer
}
[DefaultValue(ShortcutType.Command)] public ShortcutType Type = ShortcutType.Command;
[DefaultValue("")] public string Command = string.Empty;
[DefaultValue(0)] public int Hotkey = 0;
[DefaultValue(false)] public bool KeyPassthrough = false;
[DefaultValue(null)] public List<Shortcut> SubList;
[DefaultValue(false)] public bool HideAdd = false;
public enum ShortcutMode
{
Default,
Incremental,
Random
}
[DefaultValue(ShortcutMode.Default)] public ShortcutMode Mode = ShortcutMode.Default;
[DefaultValue(140)] public int CategoryWidth = 140;
[DefaultValue(false)] public bool CategoryStaysOpen = false;
[DefaultValue(1)] public int CategoryColumns = 1;
[DefaultValue(1.0f)] public float IconZoom = 1.0f;
public Vector2 IconOffset = Vector2.Zero;
public Vector4 IconTint = Vector4.One;
[JsonIgnore] public int _i = 0;
[JsonIgnore] public Shortcut _parent = null;
[JsonIgnore] public bool _activated = false;
public ShCfg Upgrade(BarConfig bar, bool sub)
{
var sh = new ShCfg
{
Name = Name,
Type = Type switch
{
ShortcutType.Category => ShCfg.ShortcutType.Category,
ShortcutType.Spacer => ShCfg.ShortcutType.Spacer,
_ => ShCfg.ShortcutType.Command
},
Command = Command,
Hotkey = Hotkey,
KeyPassthrough = KeyPassthrough,
Mode = (ShCfg.ShortcutMode)Mode,
Color = ImGui.ColorConvertFloat4ToU32(IconTint),
IconZoom = IconZoom,
IconOffset = new[] { IconOffset.X, IconOffset.Y },
CategoryWidth = CategoryWidth,
CategoryStaysOpen = CategoryStaysOpen,
CategoryColumns = Math.Max(CategoryColumns, 1),
CategorySpacing = new[] { (int)bar.CategorySpacing.X, (int)bar.CategorySpacing.Y },
CategoryScale = bar.CategoryScale,
CategoryFontScale = bar.CategoryFontScale,
CategoryNoBackground = bar.NoCategoryBackgrounds,
CategoryOnHover = !sub ? bar.OpenCategoriesOnHover : bar.OpenSubcategoriesOnHover
};
if (IconTint.W > 1)
sh.ColorAnimation = (int)Math.Round(IconTint.W * 255) - 255;
if (SubList != null)
{
sh.SubList ??= new List<ShCfg>();
foreach (var s in SubList)
sh.SubList.Add(s.Upgrade(bar, true));
}
return sh;
}
}
public class DisplayCondition
{
public enum ConditionType
{
Logic,
ConditionFlag,
Job,
Role,
Misc,
Zone,
ConditionSet
}
public ConditionType Type = ConditionType.Logic;
public int Condition = 0;
public dynamic Arg = 0;
public CndCfg Upgrade()
{
var cndCfg = new CndCfg { Arg = Arg };
switch (Type)
{
case ConditionType.ConditionFlag:
cndCfg.ID = new Conditions.ConditionFlagCondition().ID;
cndCfg.Arg = Condition;
break;
case ConditionType.Job:
cndCfg.ID = new Conditions.JobCondition().ID;
cndCfg.Arg = Condition;
break;
case ConditionType.Role:
cndCfg.ID = new Conditions.RoleCondition().ID;
cndCfg.Arg = Condition;
break;
case ConditionType.Zone:
cndCfg.ID = new Conditions.ZoneCondition().ID;
cndCfg.Arg = Condition;
break;
case ConditionType.ConditionSet:
cndCfg.ID = new Conditions.ConditionSetCondition().ID;
cndCfg.Arg = Condition;
break;
case ConditionType.Misc:
cndCfg.ID = Condition switch
{
0 => new Conditions.LoggedInCondition().ID,
1 => new Conditions.CharacterCondition().ID,
2 => new Conditions.TargetCondition().ID,
3 => new Conditions.TargetCondition().ID,
4 => new Conditions.WeaponDrawnCondition().ID,
5 => new Conditions.EorzeaTimespanCondition().ID,
6 => new Conditions.LocalTimespanCondition().ID,
7 => new Conditions.HUDLayoutCondition().ID,
8 => new Conditions.AddonExistsCondition().ID,
9 => new Conditions.AddonVisibleCondition().ID,
10 => new Conditions.PluginCondition().ID,
_ => throw new ApplicationException($"Unrecognized misc condition: {Condition}")
};
if (Condition == 3)
cndCfg.Arg = 1;
break;
}
return cndCfg;
}
}
public class DisplayConditionSet
{
public string Name = string.Empty;
public readonly List<DisplayCondition> Conditions = new();
public CndSetCfg Upgrade()
{
var set = new CndSetCfg { Name = Name };
var not = false;
var op = ConditionManager.BinaryOperator.AND;
foreach (var condition in Conditions)
{
switch (condition.Type)
{
case DisplayCondition.ConditionType.Logic:
if (condition.Condition != 2 && op != ConditionManager.BinaryOperator.AND)
{
op = ConditionManager.BinaryOperator.AND;
break;
}
switch (condition.Condition)
{
case 0:
op = ConditionManager.BinaryOperator.OR;
break;
case 1:
op = ConditionManager.BinaryOperator.XOR;
break;
case 2:
not ^= true;
break;
case 3:
op = ConditionManager.BinaryOperator.EQUALS;
break;
}
break;
default:
var cndCfg = condition.Upgrade();
cndCfg.Operator = op;
cndCfg.Negate = not;
op = ConditionManager.BinaryOperator.AND;
not = false;
set.Conditions.Add(cndCfg);
break;
}
}
return set;
}
}
public static class Legacy
{
private static readonly Dictionary<string, Action<Configuration, Importing.ExportInfo>> upgradeActions = new()
{
["1.3.2.0"] = (config, import) =>
{
static void DeleteRecursive(Shortcut sh)
{
if (sh.Type != Shortcut.ShortcutType.Category) return;
sh.Command = string.Empty;
if (sh.SubList == null) return;
foreach (var sh2 in sh.SubList)
DeleteRecursive(sh2);
}
if (config != null)
{
foreach (var sh in config.BarConfigs.SelectMany(bar => bar.ShortcutList))
DeleteRecursive(sh);
}
if (import != null)
{
if (import.b1?.ShortcutList != null)
{
foreach (var sh in import.b1.ShortcutList)
DeleteRecursive(sh);
}
if (import.s1 != null)
DeleteRecursive(import.s1);
}
},
["2.2.0.1"] = (config, import) =>
{
static void FixScaleRecursive(ShCfg sh)
{
if (sh.Type != ShCfg.ShortcutType.Category) return;
sh.CategoryScale *= sh.CategoryScale;
if (sh.SubList == null) return;
foreach (var sh2 in sh.SubList)
FixScaleRecursive(sh2);
}
if (config != null)
{
foreach (var sh in config.BarCfgs.SelectMany(bar => bar.ShortcutList))
FixScaleRecursive(sh);
}
if (import != null)
{
if (import.b2?.ShortcutList != null)
{
foreach (var sh in import.b2?.ShortcutList)
FixScaleRecursive(sh);
}
if (import.s2 != null)
FixScaleRecursive(import.s2);
}
}
};
public static void UpdateConfig(Configuration config)
{
if (string.IsNullOrEmpty(config.PluginVersion)) return;
var v = new Version(config.PluginVersion);
foreach (var kv in upgradeActions.Where(kv => v <= new Version(kv.Key)))
kv.Value(config, null);
}
public static void UpdateImport(Importing.ExportInfo import)
{
if (string.IsNullOrEmpty(import.v)) return;
var v = new Version(import.v);
foreach (var kv in upgradeActions.Where(kv => v <= new Version(kv.Key)))
kv.Value(null, import);
}
}