-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFancyTextComponent.cs
More file actions
599 lines (523 loc) · 23.1 KB
/
Copy pathFancyTextComponent.cs
File metadata and controls
599 lines (523 loc) · 23.1 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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Xml;
using LiveSplit.Model;
using LiveSplit.UI;
namespace LiveSplit.UI.Components
{
public enum FancyTextColorMode
{
Solid,
Gradient
}
public enum FancyTextGradientMode
{
ExistingColors,
NewGradient
}
public enum FancyTextGradientDirection
{
Horizontal,
Vertical,
DiagonalDown,
DiagonalUp
}
public enum FancyTextAlignment
{
Left,
Center,
Right
}
public enum FancyTextScopeMode
{
ThisComponentOnly,
AllComponents,
SelectedComponents
}
public enum FancyTextControllerMode
{
Unset,
Text,
Background
}
public enum FancyTextBackgroundShadowMode
{
Behind,
OutsideOnly,
InsideOnly
}
public class FancyTextComponent : IComponent
{
private readonly FancyTextSettings _settings;
public FancyTextComponent(LiveSplitState state)
{
_settings = new FancyTextSettings(state, this);
FancyTextRuntime.InstallHooks(state);
}
internal FancyTextComponent(LiveSplitState state, FancyTextSettings settings)
{
_settings = settings ?? new FancyTextSettings(state, this);
_settings.AttachOwner(this);
FancyTextRuntime.InstallHooks(state);
}
public string ComponentName
{
get
{
string name = _settings.InstanceName;
string baseName;
switch (_settings.ControllerMode)
{
case FancyTextControllerMode.Text:
baseName = "Fancy Text";
break;
case FancyTextControllerMode.Background:
baseName = "Fancy Background";
break;
default:
baseName = "FancyText";
break;
}
return string.IsNullOrWhiteSpace(name)
? baseName
: baseName + " - " + name;
}
}
// FancyText is a controller. It should not consume layout space unless
// it needs one tiny draw slot for the full-layout background/border.
public float VerticalHeight { get { return HasBackgroundLayer ? 0.01f : 0f; } }
public float MinimumHeight { get { return 0f; } }
public float HorizontalWidth { get { return HasBackgroundLayer ? 0.01f : 0f; } }
public float MinimumWidth { get { return 0f; } }
public float PaddingTop { get { return 0f; } }
public float PaddingBottom { get { return 0f; } }
public float PaddingLeft { get { return 0f; } }
public float PaddingRight { get { return 0f; } }
public IDictionary<string, Action> ContextMenuControls { get { return null; } }
private bool HasBackgroundLayer
{
get
{
return _settings.ControllerMode == FancyTextControllerMode.Background
&& _settings.LayoutBoxEnabled
&& _settings.ScopeMode != FancyTextScopeMode.ThisComponentOnly;
}
}
public Control GetSettingsControl(LayoutMode mode)
{
_settings.NotifyLayoutMode(mode);
return _settings;
}
public XmlNode GetSettings(XmlDocument document)
{
return _settings.GetSettings(document);
}
public void SetSettings(XmlNode settings)
{
_settings.SetSettings(settings);
}
public void Update(IInvalidator invalidator, LiveSplitState state, float width, float height, LayoutMode mode)
{
FancyTextRuntime.InstallHooks(state);
FancyTextRuntime.Publish(this, state, _settings);
if (HasBackgroundLayer)
{
FancyTextRuntime.ScheduleControllerFirst(state, this);
}
invalidator?.Invalidate(0, 0, width, height);
}
public void DrawHorizontal(Graphics g, LiveSplitState state, float height, Region clipRegion)
{
DrawControllerLayer(g, state);
}
public void DrawVertical(Graphics g, LiveSplitState state, float width, Region clipRegion)
{
DrawControllerLayer(g, state);
}
public void Dispose()
{
FancyTextRuntime.Unpublish(this);
}
private void DrawControllerLayer(Graphics g, LiveSplitState state)
{
FancyTextRuntime.InstallHooks(state);
FancyTextRuntime.Publish(this, state, _settings);
if (HasBackgroundLayer)
{
FancyTextRuntime.ScheduleControllerFirst(state, this);
}
if (!HasBackgroundLayer)
{
return;
}
DrawLayoutBox(g, state);
}
private void DrawLayoutBox(Graphics g, LiveSplitState state)
{
Form form = state != null ? state.Form : null;
if (form == null)
{
return;
}
float width = Math.Max(1f, form.ClientSize.Width > 0 ? form.ClientSize.Width : form.Width);
float height = Math.Max(1f, form.ClientSize.Height > 0 ? form.ClientSize.Height : form.Height);
float left = _settings.BoxPaddingLeft;
float top = _settings.BoxPaddingTop;
float right = _settings.BoxPaddingRight;
float bottom = _settings.BoxPaddingBottom;
var rect = RectangleF.FromLTRB(-left, -top, width + right, height + bottom);
if (rect.Width <= 1f || rect.Height <= 1f)
{
return;
}
GraphicsState saved = g.Save();
try
{
g.ResetTransform();
g.ResetClip();
g.SmoothingMode = SmoothingMode.AntiAlias;
if (_settings.BoxShadowEnabled && _settings.BoxShadowColor.A > 0)
DrawBoxShadow(g, rect);
if (_settings.BoxBackgroundEnabled && HasVisibleBoxBrush(_settings.BoxBackgroundColorMode, _settings.BoxBackgroundColor, _settings.BoxBackgroundColor2, _settings.BoxBackgroundColor3))
{
using (GraphicsPath bgPath = CreateRoundRectPath(rect, _settings.BoxRadiusTopLeft, _settings.BoxRadiusTopRight, _settings.BoxRadiusBottomRight, _settings.BoxRadiusBottomLeft))
using (Brush brush = CreateBoxBrush(
rect,
_settings.BoxBackgroundColorMode,
_settings.BoxBackgroundColor,
_settings.BoxBackgroundColor2,
_settings.BoxBackgroundColor3,
_settings.BoxBackgroundGradientDirection))
{
g.FillPath(brush, bgPath);
}
}
if (_settings.BoxBorderSize > 0f && HasVisibleBoxBrush(_settings.BoxBorderColorMode, _settings.BoxBorderColor, _settings.BoxBorderColor2, _settings.BoxBorderColor3))
{
float inset = _settings.BoxBorderSize / 2f;
var borderRect = rect;
borderRect.Inflate(-inset, -inset);
using (GraphicsPath borderPath = CreateRoundRectPath(borderRect, _settings.BoxRadiusTopLeft, _settings.BoxRadiusTopRight, _settings.BoxRadiusBottomRight, _settings.BoxRadiusBottomLeft))
using (Brush borderBrush = CreateBoxBrush(
borderRect,
_settings.BoxBorderColorMode,
_settings.BoxBorderColor,
_settings.BoxBorderColor2,
_settings.BoxBorderColor3,
_settings.BoxBorderGradientDirection))
using (var pen = new Pen(borderBrush, _settings.BoxBorderSize))
{
pen.LineJoin = LineJoin.Round;
g.DrawPath(pen, borderPath);
}
}
}
finally
{
g.Restore(saved);
}
}
private void DrawBoxShadow(Graphics g, RectangleF rect)
{
if (_settings.BoxShadowNormalEnabled)
DrawBoxShadowLayer(g, rect, FancyTextBackgroundShadowMode.Behind);
if (_settings.BoxShadowOutsideEnabled)
DrawBoxShadowLayer(g, rect, FancyTextBackgroundShadowMode.OutsideOnly);
if (_settings.BoxShadowInsideEnabled)
DrawBoxShadowLayer(g, rect, FancyTextBackgroundShadowMode.InsideOnly);
}
private void DrawBoxShadowLayer(Graphics g, RectangleF rect, FancyTextBackgroundShadowMode mode)
{
float blur = Math.Max(0f, _settings.BoxShadowBlur);
if (blur > 0f)
{
DrawBlurredBoxShadow(g, rect, blur, mode);
return;
}
int passes = Math.Max(1, _settings.BoxShadowPasses);
using (GraphicsPath bgPath = CreateRoundRectPath(rect, _settings.BoxRadiusTopLeft, _settings.BoxRadiusTopRight, _settings.BoxRadiusBottomRight, _settings.BoxRadiusBottomLeft))
{
for (int i = passes; i >= 1; i--)
{
float t = (float)i / passes;
float expand = _settings.BoxShadowSize * t;
int alpha = (int)(_settings.BoxShadowColor.A * (0.10f + 0.28f * t));
alpha = Math.Max(0, Math.Min(255, alpha));
var shadowRect = rect;
shadowRect.Offset(_settings.BoxShadowOffsetX, _settings.BoxShadowOffsetY);
shadowRect.Inflate(expand, expand);
using (GraphicsPath shadowPath = CreateRoundRectPath(shadowRect, _settings.BoxRadiusTopLeft + expand, _settings.BoxRadiusTopRight + expand, _settings.BoxRadiusBottomRight + expand, _settings.BoxRadiusBottomLeft + expand))
using (var brush = new SolidBrush(Color.FromArgb(alpha, _settings.BoxShadowColor)))
{
FillShadowPath(g, shadowPath, bgPath, brush, mode);
}
}
}
}
private void DrawBlurredBoxShadow(Graphics g, RectangleF rect, float blur, FancyTextBackgroundShadowMode mode)
{
float spread = Math.Max(0f, _settings.BoxShadowSize);
RectangleF shadowRect = rect;
shadowRect.Offset(_settings.BoxShadowOffsetX, _settings.BoxShadowOffsetY);
shadowRect.Inflate(spread, spread);
RectangleF bounds = RectangleF.Union(rect, shadowRect);
int padding = Math.Max(4, (int)Math.Ceiling(blur * 3f) + 4);
bounds.Inflate(padding, padding);
int bitmapWidth = Math.Max(1, (int)Math.Ceiling(bounds.Width));
int bitmapHeight = Math.Max(1, (int)Math.Ceiling(bounds.Height));
if (bitmapWidth <= 1 || bitmapHeight <= 1 || bitmapWidth > 4096 || bitmapHeight > 4096)
{
DrawBoxShadowWithoutBlur(g, rect, mode);
return;
}
using (Bitmap shadowBitmap = new Bitmap(bitmapWidth, bitmapHeight, PixelFormat.Format32bppArgb))
{
using (Graphics shadowGraphics = Graphics.FromImage(shadowBitmap))
using (var shadowBrush = new SolidBrush(_settings.BoxShadowColor))
{
shadowGraphics.SmoothingMode = SmoothingMode.AntiAlias;
RectangleF localShadowRect = new RectangleF(
shadowRect.Left - bounds.Left,
shadowRect.Top - bounds.Top,
shadowRect.Width,
shadowRect.Height);
using (GraphicsPath shadowPath = CreateRoundRectPath(
localShadowRect,
_settings.BoxRadiusTopLeft + spread,
_settings.BoxRadiusTopRight + spread,
_settings.BoxRadiusBottomRight + spread,
_settings.BoxRadiusBottomLeft + spread))
{
shadowGraphics.FillPath(shadowBrush, shadowPath);
}
}
BlurAlpha(shadowBitmap, blur, _settings.BoxShadowColor, Math.Max(1, Math.Min(8, _settings.BoxShadowPasses)));
using (GraphicsPath boxPath = CreateRoundRectPath(rect, _settings.BoxRadiusTopLeft, _settings.BoxRadiusTopRight, _settings.BoxRadiusBottomRight, _settings.BoxRadiusBottomLeft))
{
DrawShadowImage(g, shadowBitmap, bounds, bitmapWidth, bitmapHeight, boxPath, mode);
}
}
}
private void DrawBoxShadowWithoutBlur(Graphics g, RectangleF rect, FancyTextBackgroundShadowMode mode)
{
int savedPasses = Math.Max(1, _settings.BoxShadowPasses);
using (GraphicsPath bgPath = CreateRoundRectPath(rect, _settings.BoxRadiusTopLeft, _settings.BoxRadiusTopRight, _settings.BoxRadiusBottomRight, _settings.BoxRadiusBottomLeft))
{
var shadowRect = rect;
shadowRect.Offset(_settings.BoxShadowOffsetX, _settings.BoxShadowOffsetY);
shadowRect.Inflate(_settings.BoxShadowSize, _settings.BoxShadowSize);
using (GraphicsPath shadowPath = CreateRoundRectPath(shadowRect, _settings.BoxRadiusTopLeft + _settings.BoxShadowSize, _settings.BoxRadiusTopRight + _settings.BoxShadowSize, _settings.BoxRadiusBottomRight + _settings.BoxShadowSize, _settings.BoxRadiusBottomLeft + _settings.BoxShadowSize))
using (var brush = new SolidBrush(Color.FromArgb(Math.Max(0, Math.Min(255, _settings.BoxShadowColor.A / savedPasses)), _settings.BoxShadowColor)))
{
for (int i = 0; i < savedPasses; i++)
FillShadowPath(g, shadowPath, bgPath, brush, mode);
}
}
}
private static void DrawShadowImage(Graphics g, Image image, RectangleF bounds, int width, int height, GraphicsPath boxPath, FancyTextBackgroundShadowMode mode)
{
if (mode == FancyTextBackgroundShadowMode.Behind)
{
g.DrawImage(image, bounds.Left, bounds.Top, width, height);
return;
}
GraphicsState saved = g.Save();
try
{
g.SetClip(boxPath, mode == FancyTextBackgroundShadowMode.OutsideOnly ? CombineMode.Exclude : CombineMode.Intersect);
g.DrawImage(image, bounds.Left, bounds.Top, width, height);
}
finally
{
g.Restore(saved);
}
}
private static void FillShadowPath(Graphics g, GraphicsPath shadowPath, GraphicsPath boxPath, Brush brush, FancyTextBackgroundShadowMode mode)
{
if (mode == FancyTextBackgroundShadowMode.Behind)
{
g.FillPath(brush, shadowPath);
return;
}
using (var region = mode == FancyTextBackgroundShadowMode.OutsideOnly ? new Region(shadowPath) : new Region(boxPath))
{
if (mode == FancyTextBackgroundShadowMode.OutsideOnly)
region.Exclude(boxPath);
else
region.Intersect(shadowPath);
g.FillRegion(brush, region);
}
}
private static Brush CreateBoxBrush(RectangleF rect, FancyTextColorMode mode, Color color1, Color color2, Color color3, FancyTextGradientDirection direction)
{
if (mode != FancyTextColorMode.Gradient)
{
return new SolidBrush(color1);
}
PointF start;
PointF end;
GetGradientPoints(rect, direction, out start, out end);
if (start == end)
end = new PointF(start.X, start.Y + 1f);
var brush = new LinearGradientBrush(start, end, color1, color3);
brush.InterpolationColors = new ColorBlend
{
Positions = new[] { 0f, 0.5f, 1f },
Colors = new[] { color1, color2, color3 }
};
return brush;
}
private static bool HasVisibleBoxBrush(FancyTextColorMode mode, Color color1, Color color2, Color color3)
{
if (color1.A > 0)
return true;
return mode == FancyTextColorMode.Gradient && (color2.A > 0 || color3.A > 0);
}
private static void GetGradientPoints(RectangleF rect, FancyTextGradientDirection direction, out PointF start, out PointF end)
{
switch (direction)
{
case FancyTextGradientDirection.Horizontal:
start = new PointF(rect.Left, rect.Top);
end = new PointF(rect.Right, rect.Top);
break;
case FancyTextGradientDirection.DiagonalDown:
start = new PointF(rect.Left, rect.Top);
end = new PointF(rect.Right, rect.Bottom);
break;
case FancyTextGradientDirection.DiagonalUp:
start = new PointF(rect.Left, rect.Bottom);
end = new PointF(rect.Right, rect.Top);
break;
default:
start = new PointF(rect.Left, rect.Top);
end = new PointF(rect.Left, rect.Bottom);
break;
}
}
private static GraphicsPath CreateRoundRectPath(RectangleF rect, float topLeft, float topRight, float bottomRight, float bottomLeft)
{
float maxRadius = Math.Max(0f, Math.Min(rect.Width, rect.Height) / 2f);
topLeft = Math.Min(Math.Max(0f, topLeft), maxRadius);
topRight = Math.Min(Math.Max(0f, topRight), maxRadius);
bottomRight = Math.Min(Math.Max(0f, bottomRight), maxRadius);
bottomLeft = Math.Min(Math.Max(0f, bottomLeft), maxRadius);
var path = new GraphicsPath();
AddCorner(path, rect.Left, rect.Top, topLeft, 180f);
AddCorner(path, rect.Right - (topRight * 2f), rect.Top, topRight, 270f);
AddCorner(path, rect.Right - (bottomRight * 2f), rect.Bottom - (bottomRight * 2f), bottomRight, 0f);
AddCorner(path, rect.Left, rect.Bottom - (bottomLeft * 2f), bottomLeft, 90f);
path.CloseFigure();
return path;
}
private static void AddCorner(GraphicsPath path, float x, float y, float radius, float startAngle)
{
if (radius <= 0f)
{
if (path.PointCount == 0)
{
path.StartFigure();
}
path.AddLine(x, y, x, y);
return;
}
path.AddArc(x, y, radius * 2f, radius * 2f, startAngle, 90f);
}
private static void BlurAlpha(Bitmap bitmap, float radius, Color color, int passes)
{
Rectangle rect = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
BitmapData data = bitmap.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
try
{
int stride = data.Stride;
int height = bitmap.Height;
int width = bitmap.Width;
byte[] pixels = new byte[stride * height];
Marshal.Copy(data.Scan0, pixels, 0, pixels.Length);
byte[] originalAlpha = new byte[width * height];
for (int y = 0; y < height; y++)
{
int row = y * stride;
int alphaRow = y * width;
for (int x = 0; x < width; x++)
originalAlpha[alphaRow + x] = pixels[row + (x * 4) + 3];
}
int intRadius = Math.Max(1, (int)Math.Ceiling(radius));
float blend = Math.Max(0f, Math.Min(1f, radius / intRadius));
byte[] blurredAlpha = originalAlpha;
for (int pass = 0; pass < passes; pass++)
blurredAlpha = BoxBlur(blurredAlpha, width, height, intRadius);
for (int y = 0; y < height; y++)
{
int row = y * stride;
int alphaRow = y * width;
for (int x = 0; x < width; x++)
{
int pixel = row + (x * 4);
int original = originalAlpha[alphaRow + x];
int blurred = blurredAlpha[alphaRow + x];
int finalAlpha = (int)(original + ((blurred - original) * blend));
pixels[pixel] = color.B;
pixels[pixel + 1] = color.G;
pixels[pixel + 2] = color.R;
pixels[pixel + 3] = (byte)Math.Max(0, Math.Min(255, finalAlpha));
}
}
Marshal.Copy(pixels, 0, data.Scan0, pixels.Length);
}
finally
{
bitmap.UnlockBits(data);
}
}
private static byte[] BoxBlur(byte[] source, int width, int height, int radius)
{
if (radius <= 0)
return source;
byte[] horizontal = new byte[source.Length];
byte[] output = new byte[source.Length];
int window = (radius * 2) + 1;
for (int y = 0; y < height; y++)
{
int row = y * width;
int sum = 0;
for (int x = -radius; x <= radius; x++)
sum += source[row + Clamp(x, 0, width - 1)];
for (int x = 0; x < width; x++)
{
horizontal[row + x] = (byte)(sum / window);
int remove = Clamp(x - radius, 0, width - 1);
int add = Clamp(x + radius + 1, 0, width - 1);
sum += source[row + add] - source[row + remove];
}
}
for (int x = 0; x < width; x++)
{
int sum = 0;
for (int y = -radius; y <= radius; y++)
sum += horizontal[(Clamp(y, 0, height - 1) * width) + x];
for (int y = 0; y < height; y++)
{
output[(y * width) + x] = (byte)(sum / window);
int remove = Clamp(y - radius, 0, height - 1);
int add = Clamp(y + radius + 1, 0, height - 1);
sum += horizontal[(add * width) + x] - horizontal[(remove * width) + x];
}
}
return output;
}
private static int Clamp(int value, int min, int max)
{
if (value < min)
return min;
if (value > max)
return max;
return value;
}
}
}