-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
bcfluentprogressring.pas
252 lines (213 loc) · 5.93 KB
/
bcfluentprogressring.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
{
2024 by hedgehog
}
unit BCFluentProgressRing;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils, Controls, Graphics, ExtCtrls,
BGRAGraphicControl, BGRABitmapTypes;
type
{ TBCFluentProgressRing }
TBCFluentProgressRing = class(TBGRAGraphicControl)
private
FPeriod: Int64;
FIndeterminate: boolean;
FStartTickCount: QWord;
FAnimationTime: Int64;
FTimer: TTimer;
FMaxValue: integer;
FMinValue: integer;
FValue: integer;
FLineColor: TColor;
FLineBkgColor: TColor;
FLineWidth: integer;
procedure SetIndeterminate(AValue: boolean);
procedure SetLineBkgColor(AValue: TColor);
procedure SetLineColor(AValue: TColor);
procedure SetMaxValue(AValue: integer);
procedure SetMinValue(AValue: integer);
procedure SetValue(AValue: integer);
procedure SetLineWidth(AValue: integer);
protected
procedure SetEnabled(Value: Boolean); override;
procedure SetVisible(Value: Boolean); override;
procedure RedrawBitmapContent; override;
procedure TimerEvent({%H-}Sender: TObject);
procedure TimerStart({%H-}Sender: TObject);
public
constructor Create(AOwner: TComponent); override;
published
property MinValue: integer read FMinValue write SetMinValue default 0;
property MaxValue: integer read FMaxValue write SetMaxValue default 100;
property Value: integer read FValue write SetValue default 0;
property LineColor: TColor read FLineColor write SetLineColor default
TColor($009E5A00);
property LineBkgColor: TColor read FLineBkgColor write SetLineBkgColor default
TColor($00D3D3D3);
property LineWidth: integer read FLineWidth write SetLineWidth default 0;
property Indeterminate: boolean read FIndeterminate write SetIndeterminate default false;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('BGRA Controls', [TBCFluentProgressRing]);
end;
{ TBCFluentProgressRing }
procedure TBCFluentProgressRing.SetMaxValue(AValue: integer);
begin
if FMaxValue = AValue then
exit;
FMaxValue := AValue;
if FValue > FMaxValue then
FValue := FMaxValue;
if FMinValue > FMaxValue then
FMinValue := FMaxValue;
DiscardBitmap;
end;
procedure TBCFluentProgressRing.SetLineBkgColor(AValue: TColor);
begin
if FLineBkgColor = AValue then
Exit;
FLineBkgColor := AValue;
DiscardBitmap;
end;
procedure TBCFluentProgressRing.SetIndeterminate(AValue: boolean);
begin
if FIndeterminate=AValue then Exit;
FIndeterminate:=AValue;
if Enabled and Visible then
begin
FTimer.Enabled:= FIndeterminate;
DiscardBitmap;
end;
end;
procedure TBCFluentProgressRing.SetLineColor(AValue: TColor);
begin
if FLineColor = AValue then
Exit;
FLineColor := AValue;
DiscardBitmap;
end;
procedure TBCFluentProgressRing.SetMinValue(AValue: integer);
begin
if FMinValue = AValue then
exit;
FMinValue := AValue;
if FValue < FMinValue then
FValue := FMinValue;
if FMaxValue < FMinValue then
FMaxValue := FMinValue;
DiscardBitmap;
end;
procedure TBCFluentProgressRing.SetValue(AValue: integer);
begin
if FValue = AValue then
exit;
FValue := AValue;
if FValue < FMinValue then
FValue := FMinValue;
if FValue > FMaxValue then
FValue := FMaxValue;
DiscardBitmap;
end;
procedure TBCFluentProgressRing.SetLineWidth(AValue: integer);
begin
if FLineWidth = AValue then exit;
FLineWidth := AValue;
if Visible then DiscardBitmap;
end;
procedure TBCFluentProgressRing.SetEnabled(Value: Boolean);
begin
inherited SetEnabled(Value);
FTimer.Enabled := Value and Visible and FIndeterminate;
DiscardBitmap;
end;
procedure TBCFluentProgressRing.SetVisible(Value: Boolean);
begin
inherited SetVisible(Value);
FTimer.Enabled := Enabled and Value and FIndeterminate;
DiscardBitmap;
end;
procedure TBCFluentProgressRing.RedrawBitmapContent;
const
pi2= 2*pi;
pi15 = pi*1.5;
var
EffectiveSize: integer;
EffectiveLineWidth: single;
a, da, r: single;
procedure DoDrawArc(a, b: single; c: TColor);
begin
Bitmap.Canvas2D.strokeStyle(c);
Bitmap.Canvas2D.beginPath;
Bitmap.Canvas2D.arc(0, 0, r, a, b, false);
Bitmap.Canvas2D.stroke;
end;
begin
if Width< Height then
EffectiveSize:= Width
else
EffectiveSize:= Height;
if EffectiveSize<2 then exit;
Bitmap.Canvas2D.resetTransform;
Bitmap.Canvas2D.translate(Bitmap.Width/2, Bitmap.Height/2);
Bitmap.Canvas2D.rotate(pi15);
if FLineWidth=0 then
EffectiveLineWidth:=EffectiveSize / 12
else
EffectiveLineWidth:= FLineWidth;
r:= (EffectiveSize -EffectiveLineWidth)/2;
Bitmap.Canvas2D.lineWidth:= EffectiveLineWidth;
// background line
if (FValue < FMaxValue) and (FLineBkgColor<>clNone) then
DoDrawArc(0, pi2, FLineBkgColor);
Bitmap.Canvas2D.lineCapLCL:= pecRound;
if FIndeterminate and FTimer.Enabled then
begin
a:= 3*FAnimationTime*pi2/FPeriod - pi;
da:= 2*abs(1 - 2*FAnimationTime/FPeriod);
if da>0.005 then
DoDrawArc(a-da, a+da, FLineColor);
end
else if FValue > FMinValue then
begin
if Enabled then
DoDrawArc(0, pi2 * FValue / FMaxValue, FLineColor)
else
DoDrawArc(0, pi2 * FValue / FMaxValue, clGray);
end;
end;
procedure TBCFluentProgressRing.TimerEvent(Sender: TObject);
var
TickCount: QWord;
begin
TickCount:= GetTickCount64;
FAnimationTime:= (TickCount - FStartTickCount) mod FPeriod;
DiscardBitmap;
end;
procedure TBCFluentProgressRing.TimerStart(Sender: TObject);
begin
FStartTickCount:= GetTickCount64;
FAnimationTime:=0;
end;
constructor TBCFluentProgressRing.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FPeriod:= 2400;
FTimer:= TTimer.Create(self);
FTimer.Interval := 15;
FTimer.Enabled := false;
FTimer.OnTimer := @TimerEvent;
FTimer.OnStartTimer:= @TimerStart;
with GetControlClassDefaultSize do
SetInitialBounds(0, 0, 100, 100);
FMaxValue := 100;
FMinValue := 0;
FValue := 0;
FLineWidth:=0;
FLineColor := TColor($009E5A00);
FLineBkgColor := TColor($00D3D3D3);
end;
end.