forked from bgrabitmap/bgracontrols
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bcmaterialdesignbutton.pas
522 lines (470 loc) · 14 KB
/
bcmaterialdesignbutton.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
// SPDX-License-Identifier: LGPL-3.0-linking-exception
{******************************* CONTRIBUTOR(S) ******************************
- Edivando S. Santos Brasil | [email protected]
(Compatibility with delphi VCL 11/2018)
***************************** END CONTRIBUTOR(S) *****************************}
unit BCMaterialDesignButton;
{$I bgracontrols.inc}
interface
uses
Classes, SysUtils, {$IFDEF FPC}LResources,{$ENDIF}
Types, Controls, Graphics, ExtCtrls, BCBaseCtrls, BGRABitmap, BGRABitmapTypes;
type
{ TBCMaterialDesignButton }
TBCMaterialDesignButton = class(TBGRAGraphicCtrl)
private
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;
FBGRA: TBGRABitmap;
FBGRAShadow: TBGRABitmap;
FMousePos: TPoint;
FCircleSize: single;
FCircleAlpha: byte;
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 Paint; override;
procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
X, Y: integer); override;
class function GetControlClassDefaultSize: TSize; override;
procedure TextChanged; override;
procedure UpdateShadow;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property RoundBorders: single read FRoundBorders write SetFRoundBorders {$IFDEF FPC}default 5{$ENDIF};
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;
{$IFDEF FPC} //#
property OnChangeBounds;
{$ENDIF}
property Caption;
property Constraints;
property DragCursor;
property DragKind;
property DragMode;
property Enabled;
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;
{$IFDEF FPC}procedure Register;{$ENDIF}
implementation
function DrawTextShadow(AWidth, AHeight: integer; AText: string;
AFontHeight: integer; ATextColor, AShadowColor: TBGRAPixel;
AOffSetX, AOffSetY: integer; ARadius: integer = 0; AFontStyle: TFontStyles = [];
AFontName: string = 'Default'; AShowShadow: boolean = True;
AFontQuality: TBGRAFontQuality = fqFineAntialiasing): TBGRACustomBitmap;
var
bmpOut, bmpSdw: TBGRABitmap;
begin
bmpOut := TBGRABitmap.Create(AWidth, AHeight);
bmpOut.FontAntialias := True;
bmpOut.FontHeight := AFontHeight;
bmpOut.FontStyle := AFontStyle;
bmpOut.FontName := AFontName;
bmpOut.FontQuality := AFontQuality;
if AShowShadow then
begin
bmpSdw := TBGRABitmap.Create(AWidth, AHeight);
bmpSdw.FontAntialias := True;
bmpSdw.FontHeight := AFontHeight;
bmpSdw.FontStyle := AFontStyle;
bmpSdw.FontName := AFontName;
bmpSdw.FontQuality := AFontQuality;
bmpSdw.TextRect(Rect(0, 0, bmpSdw.Width, bmpSdw.Height), AText, taCenter, tlCenter, AShadowColor);
BGRAReplace(bmpSdw, bmpSdw.FilterBlurRadial(ARadius, rbFast));
bmpOut.PutImage(0 + AOffSetX, 0 + AOffSetY, bmpSdw,
dmDrawWithTransparency);
bmpSdw.Free;
end;
bmpOut.TextRect(Rect(0, 0, bmpOut.Width, bmpOut.Height), AText, taCenter, tlCenter, ATextColor);
Result := bmpOut;
end;
{$IFDEF FPC}procedure Register;
begin
//{$I icons\bcmaterialdesignbutton_icon.lrs}
RegisterComponents('BGRA Button Controls', [TBCMaterialDesignButton]);
end;
{$ENDIF}
{ TBCMaterialDesignButton }
procedure TBCMaterialDesignButton.SetFRoundBorders(AValue: single);
begin
if FRoundBorders = AValue then
Exit;
FRoundBorders := AValue;
UpdateShadow;
Invalidate;
end;
procedure TBCMaterialDesignButton.SetFShadow(AValue: boolean);
begin
if FShadow = AValue then
Exit;
FShadow := AValue;
InvalidatePreferredSize;
AdjustSize;
UpdateShadow;
Invalidate;
end;
procedure TBCMaterialDesignButton.SetFShadowColor(AValue: TColor);
begin
if FShadowColor = AValue then
Exit;
FShadowColor := AValue;
UpdateShadow;
Invalidate;
end;
procedure TBCMaterialDesignButton.SetFShadowSize(AValue: integer);
begin
if FShadowSize = AValue then
Exit;
FShadowSize := AValue;
InvalidatePreferredSize;
AdjustSize;
UpdateShadow;
Invalidate;
end;
procedure TBCMaterialDesignButton.SetFTextColor(AValue: TColor);
begin
if FTextColor = AValue then
Exit;
FTextColor := AValue;
Invalidate;
end;
procedure TBCMaterialDesignButton.SetFTextFont(AValue: string);
begin
if FTextFont = AValue then
Exit;
FTextFont := AValue;
InvalidatePreferredSize;
AdjustSize;
Invalidate;
end;
procedure TBCMaterialDesignButton.SetFTextQuality(AValue: TBGRAFontQuality);
begin
if FTextQuality = AValue then
Exit;
FTextQuality := AValue;
InvalidatePreferredSize;
AdjustSize;
Invalidate;
end;
procedure TBCMaterialDesignButton.SetFTextShadow(AValue: boolean);
begin
if FTextShadow = AValue then
Exit;
FTextShadow := AValue;
InvalidatePreferredSize;
AdjustSize;
Invalidate;
end;
procedure TBCMaterialDesignButton.SetFTextShadowColor(AValue: TColor);
begin
if FTextShadowColor = AValue then
Exit;
FTextShadowColor := AValue;
UpdateShadow;
Invalidate;
end;
procedure TBCMaterialDesignButton.SetFTextShadowOffsetX(AValue: integer);
begin
if FTextShadowOffsetX = AValue then
Exit;
FTextShadowOffsetX := AValue;
InvalidatePreferredSize;
AdjustSize;
Invalidate;
end;
procedure TBCMaterialDesignButton.SetFTextShadowOffsetY(AValue: integer);
begin
if FTextShadowOffsetY = AValue then
Exit;
FTextShadowOffsetY := AValue;
InvalidatePreferredSize;
AdjustSize;
Invalidate;
end;
procedure TBCMaterialDesignButton.SetFTextShadowSize(AValue: integer);
begin
if FTextShadowSize = AValue then
Exit;
FTextShadowSize := AValue;
InvalidatePreferredSize;
AdjustSize;
Invalidate;
end;
procedure TBCMaterialDesignButton.SetFTextSize(AValue: integer);
begin
if FTextSize = AValue then
Exit;
FTextSize := AValue;
InvalidatePreferredSize;
AdjustSize;
Invalidate;
end;
procedure TBCMaterialDesignButton.SetFTextStyle(AValue: TFontStyles);
begin
if FTextStyle = AValue then
Exit;
FTextStyle := AValue;
InvalidatePreferredSize;
AdjustSize;
Invalidate;
end;
procedure TBCMaterialDesignButton.CalculatePreferredSize(
var PreferredWidth, PreferredHeight: integer; WithThemeSpace: boolean);
var
ts: TSize;
begin
inherited CalculatePreferredSize(PreferredWidth, PreferredHeight,
WithThemeSpace);
if Caption <> '' then
begin
FBGRA.FontQuality := FTextQuality;
FBGRA.FontName := FTextFont;
FBGRA.FontStyle := FTextStyle;
FBGRA.FontHeight := FTextSize;
FBGRA.FontAntialias := True;
ts := FBGRA.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;
end;
procedure TBCMaterialDesignButton.SetFNormalColor(AValue: TColor);
begin
if FNormalColor = AValue then
Exit;
FNormalColor := AValue;
Invalidate;
end;
procedure TBCMaterialDesignButton.SetFNormalColorEffect(AValue: TColor);
begin
if FNormalColorEffect = AValue then
Exit;
FNormalColorEffect := AValue;
Invalidate;
end;
procedure TBCMaterialDesignButton.OnStartTimer(Sender: TObject);
begin
FCircleAlpha := 255;
FCircleSize := 5;
end;
procedure TBCMaterialDesignButton.OnTimer(Sender: TObject);
begin
FCircleSize := FCircleSize + 8;
if FCircleAlpha - 10 > 0 then
FCircleAlpha := FCircleAlpha - 10
else
FCircleAlpha := 0;
if FCircleAlpha <= 0 then
FTimer.Enabled := False;
Invalidate;
end;
procedure TBCMaterialDesignButton.Paint;
var
temp: TBGRABitmap;
round_rect_left: integer;
round_rect_width: integer;
round_rect_height: integer;
text_height: integer;
begin
if (FBGRA.Width <> Width) or (FBGRA.Height <> Height) then
begin
FBGRA.SetSize(Width, Height);
FBGRAShadow.SetSize(Width, Height);
UpdateShadow;
end;
FBGRA.FillTransparent;
if FShadow then
FBGRA.PutImage(0, 0, FBGRAShadow, dmDrawWithTransparency);
temp := TBGRABitmap.Create(Width, Height, FNormalColor);
temp.EllipseAntialias(FMousePos.X, FMousePos.Y, FCircleSize, FCircleSize,
ColorToBGRA(FNormalColorEffect, FCircleAlpha), 1,
ColorToBGRA(FNormalColorEffect, FCircleAlpha));
if FShadow then
begin
round_rect_left := FShadowSize;
round_rect_width := Width - FShadowSize;
round_rect_height := Height - FShadowSize;
end
else
begin
round_rect_left := 0;
round_rect_width := width;
round_rect_height := height;
end;
FBGRA.FillRoundRectAntialias(round_rect_left, 0, round_rect_width, round_rect_height,
FRoundBorders, FRoundBorders, temp, [rrDefault], False);
temp.Free;
if Caption <> '' then
begin
if FShadow then
text_height := Height - FShadowSize
else
text_height := Height;
temp := DrawTextShadow(Width, text_height, Caption,
FTextSize, FTextColor, FTextShadowColor, FTextShadowOffsetX,
FTextShadowOffsetY, FTextShadowSize, FTextStyle, FTextFont,
FTextShadow, FTextQuality) as TBGRABitmap;
FBGRA.PutImage(0, 0, temp, dmDrawWithTransparency);
temp.Free;
end;
FBGRA.Draw(Canvas, 0, 0, False);
end;
procedure TBCMaterialDesignButton.MouseDown(Button: TMouseButton;
Shift: TShiftState; X, Y: integer);
begin
FTimer.Enabled := False;
FMousePos := Point(X, Y);
FTimer.Enabled := True;
inherited MouseDown(Button, Shift, X, Y);
end;
class function TBCMaterialDesignButton.GetControlClassDefaultSize: TSize;
begin
Result.CX := 123;
Result.CY := 33;
end;
procedure TBCMaterialDesignButton.TextChanged;
begin
InvalidatePreferredSize;
AdjustSize;
Invalidate;
end;
procedure TBCMaterialDesignButton.UpdateShadow;
begin
FBGRAShadow.FillTransparent;
if FShadow then
begin
FBGRAShadow.RoundRectAntialias(FShadowSize, FShadowSize, Width - FShadowSize,
Height - FShadowSize, FRoundBorders, FRoundBorders,
FShadowColor, 1, FShadowColor, [rrDefault]);
BGRAReplace(FBGRAShadow, FBGRAShadow.FilterBlurRadial(FShadowSize,
FShadowSize, rbFast) as TBGRABitmap);
end;
end;
constructor TBCMaterialDesignButton.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;
{$IFDEF FPC}//#
FTimer.OnStartTimer := OnStartTimer;
{$ENDIF}
FTimer.OnTimer := OnTimer;
FBGRA := TBGRABitmap.Create(Width, Height);
FBGRAShadow := TBGRABitmap.Create(Width, Height);
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;
end;
destructor TBCMaterialDesignButton.Destroy;
begin
FTimer.Enabled := False;
{$IFDEF FPC}//#
FTimer.OnStartTimer := nil;
{$ENDIF}
FTimer.OnTimer := nil;
FreeAndNil(FBGRA);
FreeAndNil(FBGRAShadow);
inherited Destroy;
end;
end.