|
| 1 | +{ |
| 2 | + 2024 by hedgehog |
| 3 | +} |
| 4 | + |
| 5 | +unit BCFluentProgressRing; |
| 6 | + |
| 7 | +{$mode ObjFPC}{$H+} |
| 8 | + |
| 9 | +interface |
| 10 | + |
| 11 | +uses |
| 12 | + Classes, SysUtils, Controls, Graphics, ExtCtrls, |
| 13 | + BGRAGraphicControl, BGRABitmapTypes; |
| 14 | + |
| 15 | +type |
| 16 | + |
| 17 | + { TBCFluentProgressRing } |
| 18 | + |
| 19 | + TBCFluentProgressRing = class(TBGRAGraphicControl) |
| 20 | + private |
| 21 | + FPeriod: Int64; |
| 22 | + FIndeterminate: boolean; |
| 23 | + FStartTickCount: QWord; |
| 24 | + FAnimationTime: Int64; |
| 25 | + FTimer: TTimer; |
| 26 | + FMaxValue: integer; |
| 27 | + FMinValue: integer; |
| 28 | + FValue: integer; |
| 29 | + FLineColor: TColor; |
| 30 | + FLineBkgColor: TColor; |
| 31 | + FLineWidth: integer; |
| 32 | + |
| 33 | + procedure SetIndeterminate(AValue: boolean); |
| 34 | + procedure SetLineBkgColor(AValue: TColor); |
| 35 | + procedure SetLineColor(AValue: TColor); |
| 36 | + procedure SetMaxValue(AValue: integer); |
| 37 | + procedure SetMinValue(AValue: integer); |
| 38 | + procedure SetValue(AValue: integer); |
| 39 | + procedure SetLineWidth(AValue: integer); |
| 40 | + protected |
| 41 | + procedure SetEnabled(Value: Boolean); override; |
| 42 | + procedure SetVisible(Value: Boolean); override; |
| 43 | + procedure RedrawBitmapContent; override; |
| 44 | + |
| 45 | + procedure TimerEvent({%H-}Sender: TObject); |
| 46 | + procedure TimerStart({%H-}Sender: TObject); |
| 47 | + public |
| 48 | + constructor Create(AOwner: TComponent); override; |
| 49 | + published |
| 50 | + property MinValue: integer read FMinValue write SetMinValue default 0; |
| 51 | + property MaxValue: integer read FMaxValue write SetMaxValue default 100; |
| 52 | + property Value: integer read FValue write SetValue default 0; |
| 53 | + property LineColor: TColor read FLineColor write SetLineColor default |
| 54 | + TColor($009E5A00); |
| 55 | + property LineBkgColor: TColor read FLineBkgColor write SetLineBkgColor default |
| 56 | + TColor($00D3D3D3); |
| 57 | + property LineWidth: integer read FLineWidth write SetLineWidth default 0; |
| 58 | + property Indeterminate: boolean read FIndeterminate write SetIndeterminate default false; |
| 59 | + end; |
| 60 | + |
| 61 | +procedure Register; |
| 62 | + |
| 63 | +implementation |
| 64 | + |
| 65 | +procedure Register; |
| 66 | +begin |
| 67 | + RegisterComponents('BGRA Controls', [TBCFluentProgressRing]); |
| 68 | +end; |
| 69 | + |
| 70 | +{ TBCFluentProgressRing } |
| 71 | + |
| 72 | +procedure TBCFluentProgressRing.SetMaxValue(AValue: integer); |
| 73 | +begin |
| 74 | + if FMaxValue = AValue then |
| 75 | + exit; |
| 76 | + FMaxValue := AValue; |
| 77 | + if FValue > FMaxValue then |
| 78 | + FValue := FMaxValue; |
| 79 | + if FMinValue > FMaxValue then |
| 80 | + FMinValue := FMaxValue; |
| 81 | + DiscardBitmap; |
| 82 | +end; |
| 83 | + |
| 84 | +procedure TBCFluentProgressRing.SetLineBkgColor(AValue: TColor); |
| 85 | +begin |
| 86 | + if FLineBkgColor = AValue then |
| 87 | + Exit; |
| 88 | + FLineBkgColor := AValue; |
| 89 | + DiscardBitmap; |
| 90 | +end; |
| 91 | + |
| 92 | +procedure TBCFluentProgressRing.SetIndeterminate(AValue: boolean); |
| 93 | +begin |
| 94 | + if FIndeterminate=AValue then Exit; |
| 95 | + FIndeterminate:=AValue; |
| 96 | + if Enabled and Visible then |
| 97 | + begin |
| 98 | + FTimer.Enabled:= FIndeterminate; |
| 99 | + DiscardBitmap; |
| 100 | + end; |
| 101 | +end; |
| 102 | + |
| 103 | +procedure TBCFluentProgressRing.SetLineColor(AValue: TColor); |
| 104 | +begin |
| 105 | + if FLineColor = AValue then |
| 106 | + Exit; |
| 107 | + FLineColor := AValue; |
| 108 | + DiscardBitmap; |
| 109 | +end; |
| 110 | + |
| 111 | +procedure TBCFluentProgressRing.SetMinValue(AValue: integer); |
| 112 | +begin |
| 113 | + if FMinValue = AValue then |
| 114 | + exit; |
| 115 | + FMinValue := AValue; |
| 116 | + if FValue < FMinValue then |
| 117 | + FValue := FMinValue; |
| 118 | + if FMaxValue < FMinValue then |
| 119 | + FMaxValue := FMinValue; |
| 120 | + DiscardBitmap; |
| 121 | +end; |
| 122 | + |
| 123 | +procedure TBCFluentProgressRing.SetValue(AValue: integer); |
| 124 | +begin |
| 125 | + if FValue = AValue then |
| 126 | + exit; |
| 127 | + FValue := AValue; |
| 128 | + if FValue < FMinValue then |
| 129 | + FValue := FMinValue; |
| 130 | + if FValue > FMaxValue then |
| 131 | + FValue := FMaxValue; |
| 132 | + DiscardBitmap; |
| 133 | +end; |
| 134 | + |
| 135 | +procedure TBCFluentProgressRing.SetLineWidth(AValue: integer); |
| 136 | +begin |
| 137 | + if FLineWidth = AValue then exit; |
| 138 | + FLineWidth := AValue; |
| 139 | + if Visible then DiscardBitmap; |
| 140 | +end; |
| 141 | + |
| 142 | +procedure TBCFluentProgressRing.SetEnabled(Value: Boolean); |
| 143 | +begin |
| 144 | + inherited SetEnabled(Value); |
| 145 | + FTimer.Enabled := Value and Visible and FIndeterminate; |
| 146 | + DiscardBitmap; |
| 147 | +end; |
| 148 | + |
| 149 | +procedure TBCFluentProgressRing.SetVisible(Value: Boolean); |
| 150 | +begin |
| 151 | + inherited SetVisible(Value); |
| 152 | + FTimer.Enabled := Enabled and Value and FIndeterminate; |
| 153 | + DiscardBitmap; |
| 154 | +end; |
| 155 | + |
| 156 | +procedure TBCFluentProgressRing.RedrawBitmapContent; |
| 157 | +const |
| 158 | + pi2= 2*pi; |
| 159 | + pi15 = pi*1.5; |
| 160 | +var |
| 161 | + EffectiveSize: integer; |
| 162 | + EffectiveLineWidth: single; |
| 163 | + a, da, r: single; |
| 164 | + |
| 165 | + procedure DoDrawArc(a, b: single; c: TColor); |
| 166 | + begin |
| 167 | + Bitmap.Canvas2D.strokeStyle(c); |
| 168 | + Bitmap.Canvas2D.beginPath; |
| 169 | + Bitmap.Canvas2D.arc(0, 0, r, a, b, false); |
| 170 | + Bitmap.Canvas2D.stroke; |
| 171 | + end; |
| 172 | + |
| 173 | +begin |
| 174 | + if Width< Height then |
| 175 | + EffectiveSize:= Width |
| 176 | + else |
| 177 | + EffectiveSize:= Height; |
| 178 | + |
| 179 | + if EffectiveSize<2 then exit; |
| 180 | + |
| 181 | + |
| 182 | + Bitmap.Canvas2D.resetTransform; |
| 183 | + Bitmap.Canvas2D.translate(Bitmap.Width/2, Bitmap.Height/2); |
| 184 | + Bitmap.Canvas2D.rotate(pi15); |
| 185 | + |
| 186 | + |
| 187 | + if FLineWidth=0 then |
| 188 | + EffectiveLineWidth:=EffectiveSize / 12 |
| 189 | + else |
| 190 | + EffectiveLineWidth:= FLineWidth; |
| 191 | + r:= (EffectiveSize -EffectiveLineWidth)/2; |
| 192 | + |
| 193 | + Bitmap.Canvas2D.lineWidth:= EffectiveLineWidth; |
| 194 | + // background line |
| 195 | + if (FValue < FMaxValue) and (FLineBkgColor<>clNone) then |
| 196 | + DoDrawArc(0, pi2, FLineBkgColor); |
| 197 | + Bitmap.Canvas2D.lineCapLCL:= pecRound; |
| 198 | + |
| 199 | + if FIndeterminate and FTimer.Enabled then |
| 200 | + begin |
| 201 | + a:= 3*FAnimationTime*pi2/FPeriod - pi; |
| 202 | + da:= 2*abs(1 - 2*FAnimationTime/FPeriod); |
| 203 | + if da>0.005 then |
| 204 | + DoDrawArc(a-da, a+da, FLineColor); |
| 205 | + end |
| 206 | + else if FValue > FMinValue then |
| 207 | + begin |
| 208 | + if Enabled then |
| 209 | + DoDrawArc(0, pi2 * FValue / FMaxValue, FLineColor) |
| 210 | + else |
| 211 | + DoDrawArc(0, pi2 * FValue / FMaxValue, clGray); |
| 212 | + end; |
| 213 | +end; |
| 214 | + |
| 215 | +procedure TBCFluentProgressRing.TimerEvent(Sender: TObject); |
| 216 | +var |
| 217 | + TickCount: QWord; |
| 218 | +begin |
| 219 | + TickCount:= GetTickCount64; |
| 220 | + FAnimationTime:= (TickCount - FStartTickCount) mod FPeriod; |
| 221 | + DiscardBitmap; |
| 222 | +end; |
| 223 | + |
| 224 | +procedure TBCFluentProgressRing.TimerStart(Sender: TObject); |
| 225 | +begin |
| 226 | + FStartTickCount:= GetTickCount64; |
| 227 | + FAnimationTime:=0; |
| 228 | +end; |
| 229 | + |
| 230 | +constructor TBCFluentProgressRing.Create(AOwner: TComponent); |
| 231 | +begin |
| 232 | + inherited Create(AOwner); |
| 233 | + FPeriod:= 2400; |
| 234 | + FTimer:= TTimer.Create(self); |
| 235 | + FTimer.Interval := 15; |
| 236 | + FTimer.Enabled := false; |
| 237 | + FTimer.OnTimer := @TimerEvent; |
| 238 | + FTimer.OnStartTimer:= @TimerStart; |
| 239 | + |
| 240 | + with GetControlClassDefaultSize do |
| 241 | + SetInitialBounds(0, 0, 100, 100); |
| 242 | + FMaxValue := 100; |
| 243 | + FMinValue := 0; |
| 244 | + FValue := 0; |
| 245 | + FLineWidth:=0; |
| 246 | + FLineColor := TColor($009E5A00); |
| 247 | + FLineBkgColor := TColor($00D3D3D3); |
| 248 | +end; |
| 249 | + |
| 250 | + |
| 251 | +end. |
| 252 | + |
0 commit comments