-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFancyBackgroundRuntime.cs
More file actions
630 lines (547 loc) · 21 KB
/
Copy pathFancyBackgroundRuntime.cs
File metadata and controls
630 lines (547 loc) · 21 KB
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using System.Xml;
using LiveSplit.Model;
using LiveSplit.UI;
namespace LiveSplit.UI.Components
{
internal struct FancyBackgroundPadding
{
public static readonly FancyBackgroundPadding Empty = new FancyBackgroundPadding(0f, 0f, 0f, 0f);
public readonly float Top;
public readonly float Right;
public readonly float Bottom;
public readonly float Left;
public FancyBackgroundPadding(float top, float right, float bottom, float left)
{
Top = Math.Max(0f, top);
Right = Math.Max(0f, right);
Bottom = Math.Max(0f, bottom);
Left = Math.Max(0f, left);
}
public float Horizontal { get { return Left + Right; } }
public float Vertical { get { return Top + Bottom; } }
public FancyBackgroundPadding Add(FancyBackgroundPadding other)
{
return new FancyBackgroundPadding(Top + other.Top, Right + other.Right, Bottom + other.Bottom, Left + other.Left);
}
public bool NearlyEquals(FancyBackgroundPadding other)
{
return NearlyEquals(Top, other.Top)
&& NearlyEquals(Right, other.Right)
&& NearlyEquals(Bottom, other.Bottom)
&& NearlyEquals(Left, other.Left);
}
private static bool NearlyEquals(float left, float right)
{
return Math.Abs(left - right) < 0.001f;
}
}
internal static class FancyBackgroundRuntime
{
private static readonly object Sync = new object();
private static readonly Dictionary<FancyBackgroundComponent, FancyBackgroundSettings> ActiveInstances =
new Dictionary<FancyBackgroundComponent, FancyBackgroundSettings>();
private static readonly HashSet<FancyBackgroundComponent> PendingReorders =
new HashSet<FancyBackgroundComponent>();
public static void Publish(FancyBackgroundComponent owner, LiveSplitState state, FancyBackgroundSettings settings)
{
if (owner == null || settings == null)
{
return;
}
lock (Sync)
{
PruneInactiveInstances(state);
if (settings.IsLayerActive)
{
ActiveInstances[owner] = settings;
}
else
{
ActiveInstances.Remove(owner);
}
}
}
public static void Unpublish(FancyBackgroundComponent owner)
{
if (owner == null)
{
return;
}
lock (Sync)
{
ActiveInstances.Remove(owner);
PendingReorders.Remove(owner);
}
}
public static bool IsActiveLayoutOwner(LiveSplitState state, FancyBackgroundComponent owner)
{
return ReferenceEquals(GetActiveOwner(state), owner);
}
public static FancyBackgroundPadding GetActivePadding(LiveSplitState state)
{
FancyBackgroundSettings settings = GetActiveSettings(state);
return settings != null ? settings.GetTotalPadding() : FancyBackgroundPadding.Empty;
}
public static float GetActiveScale(LiveSplitState state, LayoutMode mode)
{
Form form = state != null ? state.Form : null;
if (form == null || form.IsDisposed)
{
return 1f;
}
float overallSize = CalculateOverallSize(state, mode);
float primarySize = mode == LayoutMode.Vertical ? form.Height : form.Width;
if (overallSize <= 0.001f || primarySize <= 0f)
{
return 1f;
}
return primarySize / overallSize;
}
public static void UpdateLayoutProxies(LiveSplitState state, LayoutMode mode)
{
FancyBackgroundSettings settings = GetActiveSettings(state);
bool enabled = settings != null
&& settings.NeedsContentBoundsProxy(mode);
if (enabled)
{
InstallLayoutProxies(state);
}
else
{
RemoveLayoutProxies(state);
}
}
private static void InstallLayoutProxies(LiveSplitState state)
{
ILayout layout = state != null ? state.Layout : null;
if (layout == null || layout.LayoutComponents == null)
{
return;
}
foreach (ILayoutComponent layoutComponent in layout.LayoutComponents)
{
if (layoutComponent == null || layoutComponent.Component == null)
{
continue;
}
IComponent component = layoutComponent.Component;
IComponent unwrapped = Unwrap(component);
if (component is FancyBackgroundComponentProxy
|| unwrapped is FancyBackgroundComponent
|| IsFancyTextController(component)
|| IsFancyTextController(unwrapped))
{
continue;
}
if (IsFancyTextProxy(component))
{
IComponent inner;
if (TryGetKnownProxyInner(component, out inner)
&& !(inner is FancyBackgroundComponentProxy)
&& !(Unwrap(inner) is FancyBackgroundComponent))
{
IComponent recreatedProxy;
if (TryCreateFancyTextProxy(component, new FancyBackgroundComponentProxy(inner), out recreatedProxy))
{
layoutComponent.Component = recreatedProxy;
}
}
continue;
}
layoutComponent.Component = new FancyBackgroundComponentProxy(component);
}
}
private static void RemoveLayoutProxies(LiveSplitState state)
{
ILayout layout = state != null ? state.Layout : null;
if (layout == null || layout.LayoutComponents == null)
{
return;
}
foreach (ILayoutComponent layoutComponent in layout.LayoutComponents)
{
if (layoutComponent == null || layoutComponent.Component == null)
{
continue;
}
IComponent component = layoutComponent.Component;
FancyBackgroundComponentProxy proxy = component as FancyBackgroundComponentProxy;
if (proxy != null)
{
layoutComponent.Component = proxy.Inner;
continue;
}
if (IsFancyTextProxy(component))
{
IComponent inner;
if (TryGetKnownProxyInner(component, out inner))
{
FancyBackgroundComponentProxy innerProxy = inner as FancyBackgroundComponentProxy;
if (innerProxy != null)
{
IComponent recreatedProxy;
if (TryCreateFancyTextProxy(component, innerProxy.Inner, out recreatedProxy))
{
layoutComponent.Component = recreatedProxy;
}
}
}
}
}
}
public static void ScheduleControllerFirst(LiveSplitState state, FancyBackgroundComponent owner)
{
if (state == null || owner == null || state.Layout == null || state.Layout.LayoutComponents == null)
{
return;
}
IList<ILayoutComponent> components = state.Layout.LayoutComponents;
int ownerIndex = IndexOfComponent(components, owner);
if (ownerIndex <= 0)
{
return;
}
lock (Sync)
{
if (!PendingReorders.Add(owner))
{
return;
}
}
Form form = state.Form;
if (form == null || form.IsDisposed || !form.IsHandleCreated)
{
try
{
ReorderControllerFirst(state, owner);
}
finally
{
lock (Sync)
{
PendingReorders.Remove(owner);
}
}
return;
}
form.BeginInvoke(new Action(() =>
{
try
{
ReorderControllerFirst(state, owner);
Invalidate(state);
}
finally
{
lock (Sync)
{
PendingReorders.Remove(owner);
}
}
}));
}
public static void Invalidate(LiveSplitState state)
{
Form form = state != null ? state.Form : null;
if (form == null || form.IsDisposed)
{
return;
}
if (form.InvokeRequired)
{
form.BeginInvoke(new Action(() => Invalidate(state)));
return;
}
form.Invalidate();
}
internal static IComponent Unwrap(IComponent component)
{
int depth = 0;
while (component != null && depth < 16)
{
depth++;
FancyBackgroundComponentProxy proxy = component as FancyBackgroundComponentProxy;
if (proxy != null)
{
component = proxy.Inner;
continue;
}
IComponent knownProxyInner;
if (TryGetKnownProxyInner(component, out knownProxyInner))
{
component = knownProxyInner;
continue;
}
break;
}
return component;
}
private static FancyBackgroundSettings GetActiveSettings(LiveSplitState state)
{
FancyBackgroundComponent owner = GetActiveOwner(state);
if (owner == null)
{
return null;
}
lock (Sync)
{
FancyBackgroundSettings settings;
return ActiveInstances.TryGetValue(owner, out settings) ? settings : null;
}
}
private static FancyBackgroundComponent GetActiveOwner(LiveSplitState state)
{
ILayout layout = state != null ? state.Layout : null;
if (layout == null || layout.LayoutComponents == null)
{
return null;
}
lock (Sync)
{
PruneInactiveInstances(state);
foreach (ILayoutComponent layoutComponent in layout.LayoutComponents)
{
IComponent component = layoutComponent != null ? Unwrap(layoutComponent.Component) : null;
FancyBackgroundComponent owner = component as FancyBackgroundComponent;
if (owner != null && ActiveInstances.ContainsKey(owner))
{
return owner;
}
}
return null;
}
}
private static void ReorderControllerFirst(LiveSplitState state, FancyBackgroundComponent owner)
{
if (state == null || owner == null || state.Layout == null || state.Layout.LayoutComponents == null)
{
return;
}
IList<ILayoutComponent> components = state.Layout.LayoutComponents;
int ownerIndex = IndexOfComponent(components, owner);
if (ownerIndex < 0)
{
return;
}
ILayoutComponent item = components[ownerIndex];
components.RemoveAt(ownerIndex);
components.Insert(0, item);
}
private static int IndexOfComponent(IList<ILayoutComponent> components, IComponent target)
{
if (components == null || target == null)
{
return -1;
}
for (int index = 0; index < components.Count; index++)
{
IComponent component = components[index] != null ? Unwrap(components[index].Component) : null;
if (ReferenceEquals(component, target))
{
return index;
}
}
return -1;
}
private static void PruneInactiveInstances(LiveSplitState state)
{
if (state == null || state.Layout == null || state.Layout.LayoutComponents == null)
{
return;
}
HashSet<IComponent> layoutComponents = new HashSet<IComponent>(
state.Layout.LayoutComponents
.Where(x => x != null && x.Component != null)
.Select(x => Unwrap(x.Component)));
List<FancyBackgroundComponent> stale = ActiveInstances.Keys
.Where(x => !layoutComponents.Contains(x))
.ToList();
foreach (FancyBackgroundComponent owner in stale)
{
ActiveInstances.Remove(owner);
PendingReorders.Remove(owner);
}
}
private static float CalculateOverallSize(LiveSplitState state, LayoutMode mode)
{
if (state == null || state.Layout == null || state.Layout.Components == null)
{
return 1f;
}
List<IComponent> components = state.Layout.Components
.Where(component => component != null)
.ToList();
float totalSize = 0f;
for (int index = 0; index < components.Count; index++)
{
if (mode == LayoutMode.Vertical)
{
float bottomPadding = Math.Min(GetPaddingBelow(components, index), components[index].PaddingBottom) / 2f;
totalSize += components[index].VerticalHeight - (bottomPadding * 2f);
}
else
{
float rightPadding = Math.Min(GetPaddingToRight(components, index), components[index].PaddingRight) / 2f;
totalSize += components[index].HorizontalWidth - (rightPadding * 2f);
}
}
return Math.Max(totalSize, 1f);
}
private static float GetPaddingBelow(List<IComponent> components, int index)
{
while (index < components.Count - 1)
{
index++;
IComponent component = components[index];
if (component.VerticalHeight != 0f)
{
return component.PaddingTop;
}
}
return 0f;
}
private static float GetPaddingToRight(List<IComponent> components, int index)
{
while (index < components.Count - 1)
{
index++;
IComponent component = components[index];
if (component.HorizontalWidth != 0f)
{
return component.PaddingRight;
}
}
return 0f;
}
private static bool IsFancyTextController(IComponent component)
{
return IsTypeName(component, "LiveSplit.UI.Components.FancyTextComponent");
}
private static bool IsFancyTextProxy(IComponent component)
{
return IsTypeName(component, "LiveSplit.UI.Components.FancyTextComponentProxy");
}
private static bool TryCreateFancyTextProxy(IComponent templateProxy, IComponent inner, out IComponent recreatedProxy)
{
recreatedProxy = null;
if (!IsFancyTextProxy(templateProxy) || inner == null)
{
return false;
}
try
{
recreatedProxy = Activator.CreateInstance(templateProxy.GetType(), inner) as IComponent;
}
catch
{
recreatedProxy = null;
}
return recreatedProxy != null;
}
private static bool TryGetKnownProxyInner(IComponent component, out IComponent inner)
{
inner = null;
if (!IsTypeName(component, "LiveSplit.UI.Components.FancyTextComponentProxy"))
{
return false;
}
PropertyInfo property = component.GetType().GetProperty("Inner",
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
try
{
inner = property != null ? property.GetValue(component, null) as IComponent : null;
}
catch
{
inner = null;
}
return inner != null && !ReferenceEquals(inner, component);
}
private static bool IsTypeName(IComponent component, string fullName)
{
return component != null
&& string.Equals(component.GetType().FullName, fullName, StringComparison.Ordinal);
}
}
internal sealed class FancyBackgroundComponentProxy : IDeactivatableComponent
{
public IComponent Inner { get; private set; }
public FancyBackgroundComponentProxy(IComponent inner)
{
Inner = inner;
}
public string ComponentName { get { return Inner.ComponentName; } }
public float HorizontalWidth { get { return Inner.HorizontalWidth; } }
public float MinimumHeight { get { return Inner.MinimumHeight; } }
public float VerticalHeight { get { return Inner.VerticalHeight; } }
public float MinimumWidth { get { return Inner.MinimumWidth; } }
public float PaddingTop { get { return Inner.PaddingTop; } }
public float PaddingBottom { get { return Inner.PaddingBottom; } }
public float PaddingLeft { get { return Inner.PaddingLeft; } }
public float PaddingRight { get { return Inner.PaddingRight; } }
public IDictionary<string, Action> ContextMenuControls { get { return Inner.ContextMenuControls; } }
public bool Activated
{
get
{
IDeactivatableComponent deactivatable = Inner as IDeactivatableComponent;
return deactivatable == null || deactivatable.Activated;
}
set
{
IDeactivatableComponent deactivatable = Inner as IDeactivatableComponent;
if (deactivatable != null)
{
deactivatable.Activated = value;
}
}
}
public void DrawHorizontal(Graphics g, LiveSplitState state, float height, Region clipRegion)
{
FancyBackgroundPadding padding = FancyBackgroundRuntime.GetActivePadding(state);
float contentHeight = Math.Max(1f, height - padding.Top - padding.Bottom);
Inner.DrawHorizontal(g, state, contentHeight, clipRegion);
}
public void DrawVertical(Graphics g, LiveSplitState state, float width, Region clipRegion)
{
FancyBackgroundPadding padding = FancyBackgroundRuntime.GetActivePadding(state);
float contentWidth = Math.Max(1f, width - padding.Left - padding.Right);
Inner.DrawVertical(g, state, contentWidth, clipRegion);
}
public Control GetSettingsControl(LayoutMode mode)
{
return Inner.GetSettingsControl(mode);
}
public XmlNode GetSettings(XmlDocument document)
{
return Inner.GetSettings(document);
}
public void SetSettings(XmlNode settings)
{
Inner.SetSettings(settings);
}
public void Update(IInvalidator invalidator, LiveSplitState state, float width, float height, LayoutMode mode)
{
FancyBackgroundPadding padding = FancyBackgroundRuntime.GetActivePadding(state);
float scale = FancyBackgroundRuntime.GetActiveScale(state, mode);
float contentWidth = mode == LayoutMode.Vertical
? Math.Max(1f, width - ((padding.Left + padding.Right) * scale))
: width;
float contentHeight = mode == LayoutMode.Horizontal
? Math.Max(1f, height - ((padding.Top + padding.Bottom) * scale))
: height;
Inner.Update(invalidator, state, contentWidth, contentHeight, mode);
}
public void Dispose()
{
Inner.Dispose();
}
}
}