-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfxmaterialbutton.pas
622 lines (564 loc) Β· 16.6 KB
/
fxmaterialbutton.pas
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
unit FXMaterialButton;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Types, LResources, Forms, Controls, Graphics, Dialogs,
BGRABitmap, BGRABitmapTypes, BGRAOpenGL, ExtCtrls, FXMaterialColors,
FXGraphicControl;
type
{ TFXMaterialButton }
TFXMaterialButton = class(TFXGraphicControl)
private
FColorKind: TMaterialColor;
FFontColorAutomatic: boolean;
FNormalColor: TColor;
FNormalColorEffect: TColor;
FRoundBorders: single;
FShadow: boolean;
FShadowColor: TColor;
FShadowSize: integer;
FTextColor: TColor;
FTextFont: string;
FTextQuality: TBGRAFontQuality;
FTextShadow: boolean;
FTextShadowColor: TColor;
FTextShadowOffsetX: integer;
FTextShadowOffsetY: integer;
FTextShadowSize: integer;
FTextSize: integer;
FTextStyle: TFontStyles;
FTimer: TTimer;
FMousePos: TPoint;
FCircleSize: single;
FCircleAlpha: byte;
FCircular: boolean;
FNeedDraw: boolean;
procedure SetFCircular(AValue: boolean);
procedure SetFColorKind(AValue: TMaterialColor);
procedure SetFFontColorAutomatic(AValue: boolean);
procedure SetFNormalColor(AValue: TColor);
procedure SetFNormalColorEffect(AValue: TColor);
procedure SetFRoundBorders(AValue: single);
procedure SetFShadow(AValue: boolean);
procedure SetFShadowColor(AValue: TColor);
procedure SetFShadowSize(AValue: integer);
procedure SetFTextColor(AValue: TColor);
procedure SetFTextFont(AValue: string);
procedure SetFTextQuality(AValue: TBGRAFontQuality);
procedure SetFTextShadow(AValue: boolean);
procedure SetFTextShadowColor(AValue: TColor);
procedure SetFTextShadowOffsetX(AValue: integer);
procedure SetFTextShadowOffsetY(AValue: integer);
procedure SetFTextShadowSize(AValue: integer);
procedure SetFTextSize(AValue: integer);
procedure SetFTextStyle(AValue: TFontStyles);
protected
procedure CalculatePreferredSize(var PreferredWidth, PreferredHeight: integer;
{%H-}WithThemeSpace: boolean); override;
procedure OnStartTimer({%H-}Sender: TObject);
procedure OnTimer({%H-}Sender: TObject);
procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
X, Y: integer); override;
class function GetControlClassDefaultSize: TSize; override;
procedure TextChanged; override;
procedure UpdateShadow;
procedure DrawTextShadow(AHeight: integer; ATextColor: TColor);
protected
procedure Draw; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property Circular: boolean read FCircular write SetFCircular default false;
property RoundBorders: single read FRoundBorders write SetFRoundBorders default 5;
property ColorKind: TMaterialColor read FColorKind write SetFColorKind;
property FontColorAutomatic: boolean read FFontColorAutomatic
write SetFFontColorAutomatic;
property NormalColor: TColor read FNormalColor write SetFNormalColor default clWhite;
property NormalColorEffect: TColor read FNormalColorEffect
write SetFNormalColorEffect default clSilver;
property Shadow: boolean read FShadow write SetFShadow default True;
property ShadowColor: TColor read FShadowColor write SetFShadowColor default clGray;
property ShadowSize: integer read FShadowSize write SetFShadowSize default 5;
property TextColor: TColor read FTextColor write SetFTextColor default clBlack;
property TextSize: integer read FTextSize write SetFTextSize default 16;
property TextShadow: boolean read FTextShadow write SetFTextShadow default True;
property TextShadowColor: TColor read FTextShadowColor
write SetFTextShadowColor default clBlack;
property TextShadowSize: integer read FTextShadowSize
write SetFTextShadowSize default 2;
property TextShadowOffsetX: integer read FTextShadowOffsetX
write SetFTextShadowOffsetX default 0;
property TextShadowOffsetY: integer read FTextShadowOffsetY
write SetFTextShadowOffsetY default 0;
property TextStyle: TFontStyles read FTextStyle write SetFTextStyle default [];
property TextFont: string read FTextFont write SetFTextFont;
property TextQuality: TBGRAFontQuality
read FTextQuality write SetFTextQuality default fqFineAntialiasing;
published
property Action;
property Align;
property Anchors;
property AutoSize;
property BidiMode;
property BorderSpacing;
property Caption;
property Constraints;
property DragCursor;
property DragKind;
property DragMode;
property Enabled;
property OnChangeBounds;
property OnClick;
property OnContextPopup;
property OnDragDrop;
property OnDragOver;
property OnEndDrag;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnMouseEnter;
property OnMouseLeave;
property OnMouseWheel;
property OnMouseWheelDown;
property OnMouseWheelUp;
property OnResize;
property OnStartDrag;
property ParentBidiMode;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ShowHint;
property Visible;
end;
procedure Register;
implementation
procedure Register;
begin
{$I icons\fxmaterialbutton_icon.lrs}
RegisterComponents('BGRA Controls FX', [TFXMaterialButton]);
end;
{ TFXMaterialButton }
procedure TFXMaterialButton.SetFRoundBorders(AValue: single);
begin
if FRoundBorders = AValue then
Exit;
FRoundBorders := AValue;
UpdateShadow;
Invalidate;
end;
procedure TFXMaterialButton.SetFShadow(AValue: boolean);
begin
if FShadow = AValue then
Exit;
FShadow := AValue;
InvalidatePreferredSize;
AdjustSize;
UpdateShadow;
Invalidate;
end;
procedure TFXMaterialButton.SetFShadowColor(AValue: TColor);
begin
if FShadowColor = AValue then
Exit;
FShadowColor := AValue;
UpdateShadow;
Invalidate;
end;
procedure TFXMaterialButton.SetFShadowSize(AValue: integer);
begin
if FShadowSize = AValue then
Exit;
FShadowSize := AValue;
InvalidatePreferredSize;
AdjustSize;
UpdateShadow;
Invalidate;
end;
procedure TFXMaterialButton.SetFTextColor(AValue: TColor);
begin
if FTextColor = AValue then
Exit;
FTextColor := AValue;
FNeedDraw := True;
Invalidate;
end;
procedure TFXMaterialButton.SetFTextFont(AValue: string);
begin
if FTextFont = AValue then
Exit;
FTextFont := AValue;
InvalidatePreferredSize;
AdjustSize;
FNeedDraw := True;
Invalidate;
end;
procedure TFXMaterialButton.SetFTextQuality(AValue: TBGRAFontQuality);
begin
if FTextQuality = AValue then
Exit;
FTextQuality := AValue;
InvalidatePreferredSize;
AdjustSize;
FNeedDraw := True;
Invalidate;
end;
procedure TFXMaterialButton.SetFTextShadow(AValue: boolean);
begin
if FTextShadow = AValue then
Exit;
FTextShadow := AValue;
InvalidatePreferredSize;
AdjustSize;
FNeedDraw := True;
Invalidate;
end;
procedure TFXMaterialButton.SetFTextShadowColor(AValue: TColor);
begin
if FTextShadowColor = AValue then
Exit;
FTextShadowColor := AValue;
UpdateShadow;
Invalidate;
end;
procedure TFXMaterialButton.SetFTextShadowOffsetX(AValue: integer);
begin
if FTextShadowOffsetX = AValue then
Exit;
FTextShadowOffsetX := AValue;
InvalidatePreferredSize;
AdjustSize;
FNeedDraw := True;
Invalidate;
end;
procedure TFXMaterialButton.SetFTextShadowOffsetY(AValue: integer);
begin
if FTextShadowOffsetY = AValue then
Exit;
FTextShadowOffsetY := AValue;
InvalidatePreferredSize;
AdjustSize;
FNeedDraw := True;
Invalidate;
end;
procedure TFXMaterialButton.SetFTextShadowSize(AValue: integer);
begin
if FTextShadowSize = AValue then
Exit;
FTextShadowSize := AValue;
InvalidatePreferredSize;
AdjustSize;
FNeedDraw := True;
Invalidate;
end;
procedure TFXMaterialButton.SetFTextSize(AValue: integer);
begin
if FTextSize = AValue then
Exit;
FTextSize := AValue;
InvalidatePreferredSize;
AdjustSize;
FNeedDraw := True;
Invalidate;
end;
procedure TFXMaterialButton.SetFTextStyle(AValue: TFontStyles);
begin
if FTextStyle = AValue then
Exit;
FTextStyle := AValue;
InvalidatePreferredSize;
AdjustSize;
FNeedDraw := True;
Invalidate;
end;
procedure TFXMaterialButton.CalculatePreferredSize(
var PreferredWidth, PreferredHeight: integer; WithThemeSpace: boolean);
var
ts: TSize;
begin
inherited CalculatePreferredSize(PreferredWidth, PreferredHeight,
WithThemeSpace);
if Caption <> '' then
begin
FXLayers[1].BGRA.FontQuality := FTextQuality;
FXLayers[1].BGRA.FontName := FTextFont;
FXLayers[1].BGRA.FontStyle := FTextStyle;
FXLayers[1].BGRA.FontHeight := FTextSize;
FXLayers[1].BGRA.FontAntialias := True;
ts := FXLayers[1].BGRA.TextSize(Caption);
Inc(PreferredWidth, ts.cx + 26);
Inc(PreferredHeight, ts.cy + 10);
end;
if FShadow then
begin
Inc(PreferredWidth, FShadowSize * 2);
Inc(PreferredHeight, FShadowSize * 2);
end;
if FCircular then
begin
PreferredHeight := PreferredWidth;
end;
end;
procedure TFXMaterialButton.SetFNormalColor(AValue: TColor);
begin
if FNormalColor = AValue then
Exit;
FNormalColor := AValue;
FNeedDraw := True;
Invalidate;
end;
procedure TFXMaterialButton.SetFColorKind(AValue: TMaterialColor);
begin
if FColorKind = AValue then
Exit;
FColorKind := AValue;
FNeedDraw := True;
Invalidate;
end;
procedure TFXMaterialButton.SetFCircular(AValue: boolean);
begin
if FCircular=AValue then Exit;
FCircular:=AValue;
InvalidatePreferredSize;
AdjustSize;
UpdateShadow;
Invalidate;
end;
procedure TFXMaterialButton.SetFFontColorAutomatic(AValue: boolean);
begin
if FFontColorAutomatic = AValue then
Exit;
FFontColorAutomatic := AValue;
FNeedDraw := True;
Invalidate;
end;
procedure TFXMaterialButton.SetFNormalColorEffect(AValue: TColor);
begin
if FNormalColorEffect = AValue then
Exit;
FNormalColorEffect := AValue;
FNeedDraw := True;
Invalidate;
end;
procedure TFXMaterialButton.OnStartTimer(Sender: TObject);
begin
FCircleAlpha := 255;
FCircleSize := 0;
end;
procedure TFXMaterialButton.OnTimer(Sender: TObject);
begin
FNeedDraw := True;
if Width > Height then
FCircleSize := FCircleSize + (Width div 15)
else
FCircleSize := FCircleSize + (Height div 15);
if FCircleAlpha - 10 > 0 then
FCircleAlpha := FCircleAlpha - 10
else
FCircleAlpha := 0;
if FCircleAlpha <= 0 then
FTimer.Enabled := False;
Invalidate;
end;
procedure TFXMaterialButton.MouseDown(Button: TMouseButton;
Shift: TShiftState; X, Y: integer);
begin
if Button = mbLeft then
begin
FTimer.Enabled := False;
FMousePos := Point(X, Y);
FTimer.Enabled := True;
end;
inherited MouseDown(Button, Shift, X, Y);
end;
class function TFXMaterialButton.GetControlClassDefaultSize: TSize;
begin
Result.CX := 123;
Result.CY := 33;
end;
procedure TFXMaterialButton.TextChanged;
begin
InvalidatePreferredSize;
AdjustSize;
FNeedDraw := True;
Invalidate;
end;
procedure TFXMaterialButton.UpdateShadow;
var
temp: TBGRABitmap;
begin
FXLayers[0].BGRA.FillTransparent;
if FShadow then
begin
if not FCircular then
FXLayers[0].BGRA.RoundRectAntialias(FShadowSize, FShadowSize, Width - FShadowSize,
Height - FShadowSize, FRoundBorders, FRoundBorders,
FShadowColor, 1, FShadowColor, [rrDefault])
else
FXLayers[0].BGRA.FillEllipseAntialias(Width div 2, Height div 2, (Width div 2) - FShadowSize, (Width div 2) - FShadowSize, FShadowColor);
temp := FXLayers[0].BGRA.FilterBlurRadial(FShadowSize /
sqrt(2), FShadowSize / sqrt(2), rbBox) as TBGRABitmap;
FXLayers[0].BGRA.Assign(temp);
temp.Free;
end;
FNeedDraw := True;
FXLayers[0].Texture := nil;
end;
procedure TFXMaterialButton.DrawTextShadow(AHeight: integer; ATextColor: TColor);
var
bmpSdw: TBGRABitmap;
OutTxtSize: TSize;
OutX, OutY: integer;
begin
FXLayers[1].BGRA.FontAntialias := True;
FXLayers[1].BGRA.FontHeight := TextSize;
FXLayers[1].BGRA.FontStyle := TextStyle;
FXLayers[1].BGRA.FontName := TextFont;
FXLayers[1].BGRA.FontQuality := TextQuality;
OutTxtSize := FXLayers[1].BGRA.TextSize(Caption);
OutX := Round(FXLayers[1].BGRA.Width / 2) - Round(OutTxtSize.cx / 2);
OutY := Round(AHeight / 2) - Round(OutTxtSize.cy / 2);
if FTextShadow then
begin
bmpSdw := TBGRABitmap.Create(OutTxtSize.cx + 2 * FTextShadowSize,
OutTxtSize.cy + 2 * FTextShadowSize);
bmpSdw.FontAntialias := True;
bmpSdw.FontHeight := TextSize;
bmpSdw.FontStyle := TextStyle;
bmpSdw.FontName := TextFont;
bmpSdw.FontQuality := TextQuality;
bmpSdw.TextOut(FTextShadowSize, FTextShadowSize, Caption, FTextShadowColor);
BGRAReplace(bmpSdw, bmpSdw.FilterBlurRadial(FTextShadowSize /
sqrt(2), FTextShadowSize / sqrt(2), rbBox));
FXLayers[1].BGRA.PutImage(OutX + FTextShadowOffsetX - FTextShadowSize, OutY +
FTextShadowOffSetY - FTextShadowSize, bmpSdw,
dmDrawWithTransparency);
bmpSdw.Free;
end;
FXLayers[1].BGRA.TextOut(OutX, OutY, Caption, ATextColor);
end;
procedure TFXMaterialButton.Draw;
var
temp: TBGRABitmap;
round_rect_left: integer;
round_rect_width: integer;
round_rect_height: integer;
text_height: integer;
color_normal: TColor;
color_effect: TColor;
begin
if (ClientWidth - 1 > FRoundBorders * 2) and (ClientHeight - 1 > FRoundBorders * 2) then
begin
if (FXLayers[1].BGRA.Width <> ClientWidth) or (FXLayers[1].BGRA.Height <> ClientHeight) then
begin
FXLayers[1].BGRA.SetSize(ClientWidth, ClientHeight);
FXLayers[0].BGRA.SetSize(ClientWidth, ClientHeight);
UpdateShadow;
end;
if FNeedDraw then
begin
FXLayers[1].BGRA.FillTransparent;
if ColorKind = mcDefault then
begin
color_normal := FNormalColor;
color_effect := FNormalColorEffect;
end
else
begin
color_normal := MaterialColorsList.KeyData[MaterialColorStr[ColorKind]].M500;
color_effect := MaterialColorsList.KeyData[MaterialColorStr[ColorKind]].M50;
end;
temp := TBGRABitmap.Create(ClientWidth, ClientHeight, color_normal);
temp.EllipseAntialias(FMousePos.X, FMousePos.Y, FCircleSize, FCircleSize,
ColorToBGRA(color_effect, FCircleAlpha), 1,
ColorToBGRA(color_effect, FCircleAlpha));
if FShadow then
begin
round_rect_left := FShadowSize;
round_rect_width := ClientWidth - FShadowSize;
round_rect_height := ClientHeight - FShadowSize;
if FCircular then
begin
round_rect_width := (Width div 2) - FShadowSize;
round_rect_height := (Height - FShadowSize) div 2;
end;
end
else
begin
round_rect_left := 0;
round_rect_width := ClientWidth;
round_rect_height := ClientHeight;
if FCircular then
begin
round_rect_width := Width div 2;
round_rect_height := Height div 2;
end;
end;
if not FCircular then
FXLayers[1].BGRA.FillRoundRectAntialias(round_rect_left, 0, round_rect_width,
round_rect_height,
FRoundBorders, FRoundBorders, temp, [rrDefault], False)
else
FXLayers[1].BGRA.FillEllipseAntialias(Width div 2, round_rect_height, round_rect_width, round_rect_width, temp);
temp.Free;
if Caption <> '' then
begin
if FShadow then
text_height := ClientHeight - FShadowSize
else
text_height := ClientHeight;
if FontColorAutomatic then
DrawTextShadow(text_height, GetContrastColor(color_normal))
else
DrawTextShadow(text_height, FTextColor);
end;
FNeedDraw := False;
FXLayers[1].Texture := nil;
end;
end
else
begin
FXLayers[0].BGRA.FillTransparent;
FXLayers[0].Texture := nil;
FXLayers[1].BGRA.FillTransparent;
FXLayers[1].Texture := nil;
end;
end;
constructor TFXMaterialButton.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
with GetControlClassDefaultSize do
SetInitialBounds(0, 0, CX, CY);
FTimer := TTimer.Create(Self);
FTimer.Interval := 15;
FTimer.Enabled := False;
FTimer.OnStartTimer := @OnStartTimer;
FTimer.OnTimer := @OnTimer;
FXLayers.Insert(0, TFXLayer.Create);
FRoundBorders := 5;
FNormalColor := clWhite;
FNormalColorEffect := clSilver;
FShadow := True;
FShadowColor := clGray;
FShadowSize := 5;
FTextColor := clBlack;
FTextSize := 16;
FTextShadow := True;
FTextShadowColor := clBlack;
FTextShadowSize := 2;
FTextShadowOffsetX := 0;
FTextShadowOffsetY := 0;
FTextStyle := [];
FTextFont := 'default';
FTextQuality := fqFineAntialiasing;
FFontColorAutomatic := True;
end;
destructor TFXMaterialButton.Destroy;
begin
FTimer.Enabled := False;
FTimer.OnStartTimer := nil;
FTimer.OnStopTimer := nil;
FTimer.OnTimer := nil;
inherited Destroy;
end;
end.