bcradialprogressbar.pas 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. // SPDX-License-Identifier: LGPL-3.0-linking-exception
  2. {******************************* CONTRIBUTOR(S) ******************************
  3. - Edivando S. Santos Brasil | [email protected]
  4. (Compatibility with delphi VCL 11/2018)
  5. ***************************** END CONTRIBUTOR(S) *****************************}
  6. unit BCRadialProgressBar;
  7. {$I bgracontrols.inc}
  8. interface
  9. uses
  10. Classes, SysUtils, {$IFDEF FPC}LResources,{$ENDIF} Forms, Controls, Graphics, Dialogs, BCBaseCtrls,
  11. {$IFNDEF FPC}Types, BGRAGraphics, GraphType, FPImage, {$ENDIF}
  12. BGRABitmap, BGRABitmapTypes, BGRATextFX;
  13. type
  14. { TBCRadialProgressBar }
  15. TBCRadialProgressBar = class(TBCGraphicControl)
  16. private
  17. FDrawText: boolean;
  18. { Private declarations }
  19. FMaxValue: integer;
  20. FMinValue: integer;
  21. FRotation: single;
  22. FValue: integer;
  23. FBitmap: TBGRABitmap;
  24. FLineColor: TColor;
  25. FLineBkgColor: TColor;
  26. FFontShadowColor: TColor;
  27. FFontShadowOffsetX: integer;
  28. FFontShadowOffsetY: integer;
  29. FFontShadowRadius: integer;
  30. FLineWidth: single;
  31. procedure SetDrawText(AValue: boolean);
  32. procedure SetFFontShadowColor(AValue: TColor);
  33. procedure SetFFontShadowOffsetX(AValue: integer);
  34. procedure SetFFontShadowOffsetY(AValue: integer);
  35. procedure SetFFontShadowRadius(AValue: integer);
  36. procedure SetFLineBkgColor(AValue: TColor);
  37. procedure SetFLineColor(AValue: TColor);
  38. procedure SetMaxValue(AValue: integer);
  39. procedure SetMinValue(AValue: integer);
  40. procedure SetRotation(AValue: single);
  41. procedure SetValue(AValue: integer);
  42. procedure SetLineWidth(AValue: single);
  43. protected
  44. { Protected declarations }
  45. procedure CalculatePreferredSize(var PreferredWidth, PreferredHeight: integer;
  46. {%H-}WithThemeSpace: boolean); override;
  47. procedure DrawControl; override;
  48. procedure RenderControl; override;
  49. procedure SetColor(Value: TColor); override;
  50. public
  51. { Public declarations }
  52. constructor Create(AOwner: TComponent); override;
  53. destructor Destroy; override;
  54. property Rotation: single read FRotation write SetRotation default 0;
  55. property DrawText: boolean read FDrawText write SetDrawText default true;
  56. published
  57. { Published declarations }
  58. property Align;
  59. property Anchors;
  60. property MinValue: integer read FMinValue write SetMinValue default 0;
  61. property MaxValue: integer read FMaxValue write SetMaxValue default 100;
  62. property Value: integer read FValue write SetValue default 0;
  63. property OnClick;
  64. property OnMouseDown;
  65. property OnMouseEnter;
  66. property OnMouseLeave;
  67. property OnMouseMove;
  68. property OnMouseUp;
  69. property OnMouseWheel;
  70. property OnMouseWheelUp;
  71. property OnMouseWheelDown;
  72. property Color default clWhite;
  73. property LineColor: TColor read FLineColor write SetFLineColor default clBlack;
  74. property LineBkgColor: TColor read FLineBkgColor write SetFLineBkgColor default
  75. clSilver;
  76. property LineWidth: single read FLineWidth write SetLineWidth {$IFDEF FPC}default 4{$ENDIF};
  77. property FontShadowColor: TColor read FFontShadowColor
  78. write SetFFontShadowColor default clBlack;
  79. property FontShadowOffsetX: integer read FFontShadowOffsetX
  80. write SetFFontShadowOffsetX default 2;
  81. property FontShadowOffsetY: integer read FFontShadowOffsetY
  82. write SetFFontShadowOffsetY default 2;
  83. property FontShadowRadius: integer read FFontSHadowRadius
  84. write SetFFontShadowRadius default 4;
  85. property Font;
  86. end;
  87. {$IFDEF FPC}procedure Register;{$ENDIF}
  88. implementation
  89. {$IFDEF FPC}
  90. procedure Register;
  91. begin
  92. {$IFDEF FPC}
  93. {$I icons\bcradialprogressbar_icon.lrs}
  94. {$ENDIF}
  95. RegisterComponents('BGRA Controls', [TBCRadialProgressBar]);
  96. end;
  97. {$ENDIF}
  98. { TBCRadialProgressBar }
  99. procedure TBCRadialProgressBar.SetMaxValue(AValue: integer);
  100. begin
  101. if FMaxValue = AValue then
  102. exit;
  103. FMaxValue := AValue;
  104. if FValue > FMaxValue then
  105. FValue := FMaxValue;
  106. if FMinValue > FMaxValue then
  107. FMinValue := FMaxValue;
  108. RenderControl;
  109. Invalidate;
  110. end;
  111. procedure TBCRadialProgressBar.SetFLineBkgColor(AValue: TColor);
  112. begin
  113. if FLineBkgColor = AValue then
  114. Exit;
  115. FLineBkgColor := AValue;
  116. RenderControl;
  117. Invalidate;
  118. end;
  119. procedure TBCRadialProgressBar.SetFFontShadowColor(AValue: TColor);
  120. begin
  121. if FFontShadowColor = AValue then
  122. Exit;
  123. FFontShadowColor := AValue;
  124. RenderControl;
  125. Invalidate;
  126. end;
  127. procedure TBCRadialProgressBar.SetDrawText(AValue: boolean);
  128. begin
  129. if FDrawText=AValue then Exit;
  130. FDrawText:=AValue;
  131. RenderControl;
  132. Invalidate;
  133. end;
  134. procedure TBCRadialProgressBar.SetFFontShadowOffsetX(AValue: integer);
  135. begin
  136. if FFontShadowOffsetX = AValue then
  137. Exit;
  138. FFontShadowOffsetX := AValue;
  139. RenderControl;
  140. Invalidate;
  141. end;
  142. procedure TBCRadialProgressBar.SetFFontShadowOffsetY(AValue: integer);
  143. begin
  144. if FFontShadowOffsetY = AValue then
  145. Exit;
  146. FFontShadowOffsetY := AValue;
  147. RenderControl;
  148. Invalidate;
  149. end;
  150. procedure TBCRadialProgressBar.SetFFontShadowRadius(AValue: integer);
  151. begin
  152. if FFontSHadowRadius = AValue then
  153. Exit;
  154. FFontSHadowRadius := AValue;
  155. RenderControl;
  156. Invalidate;
  157. end;
  158. procedure TBCRadialProgressBar.SetFLineColor(AValue: TColor);
  159. begin
  160. if FLineColor = AValue then
  161. Exit;
  162. FLineColor := AValue;
  163. RenderControl;
  164. Invalidate;
  165. end;
  166. procedure TBCRadialProgressBar.SetMinValue(AValue: integer);
  167. begin
  168. if FMinValue = AValue then
  169. exit;
  170. FMinValue := AValue;
  171. if FValue < FMinValue then
  172. FValue := FMinValue;
  173. if FMaxValue < FMinValue then
  174. FMaxValue := FMinValue;
  175. RenderControl;
  176. Invalidate;
  177. end;
  178. procedure TBCRadialProgressBar.SetRotation(AValue: single);
  179. begin
  180. if FRotation=AValue then Exit;
  181. FRotation:=AValue;
  182. RenderControl;
  183. Invalidate;
  184. end;
  185. procedure TBCRadialProgressBar.SetValue(AValue: integer);
  186. begin
  187. if FValue = AValue then
  188. exit;
  189. FValue := AValue;
  190. if FValue < FMinValue then
  191. FValue := FMinValue;
  192. if FValue > FMaxValue then
  193. FValue := FMaxValue;
  194. RenderControl;
  195. Invalidate;
  196. end;
  197. procedure TBCRadialProgressBar.SetLineWidth(AValue: single);
  198. begin
  199. if (FLineWidth = AValue) then
  200. exit;
  201. FLineWidth := AValue;
  202. RenderControl;
  203. Invalidate;
  204. end;
  205. procedure TBCRadialProgressBar.CalculatePreferredSize(
  206. var PreferredWidth, PreferredHeight: integer; WithThemeSpace: boolean);
  207. begin
  208. PreferredWidth := 200;
  209. PreferredHeight := 200;
  210. end;
  211. procedure TBCRadialProgressBar.DrawControl;
  212. begin
  213. {$IFNDEF FPC}//# //@ IN DELPHI RenderControl NEDD. IF NO RenderControl BE BLACK AFTER INVALIDATE.
  214. RenderControl;
  215. {$ENDIF}
  216. FBitmap.Draw(Canvas, 0, 0, False);
  217. end;
  218. procedure TBCRadialProgressBar.RenderControl;
  219. var
  220. textBmp: TBGRABitmap;
  221. textStr: string;
  222. EffectiveLineWidth:single;
  223. begin
  224. FreeAndNil(FBitmap);
  225. FBitmap := TBGRABitmap.Create(Width, Height);
  226. FBitmap.Canvas2D.resetTransform;
  227. FBitmap.Canvas2D.translate(FBitmap.Width/2, FBitmap.Height/2);
  228. FBitmap.Canvas2D.rotate(FRotation*Pi/180);
  229. FBitmap.Canvas2D.beginPath;
  230. FBitmap.Canvas2D.arc(0, 0, Height / 2.5, 0, pi * 2, False);
  231. FBitmap.Canvas2D.fillStyle(Color);
  232. FBitmap.Canvas2D.fill;
  233. if LineWidth=0 then
  234. EffectiveLineWidth:=Height / 50
  235. else
  236. EffectiveLineWidth:=LineWidth;
  237. FBitmap.Canvas2D.lineWidth := EffectiveLineWidth;
  238. FBitmap.Canvas2D.strokeStyle(LineBkgColor);
  239. FBitmap.Canvas2D.stroke;
  240. FBitmap.Canvas2D.beginPath;
  241. if Value <> MinValue then
  242. FBitmap.Canvas2D.arc(0, 0, Height / 2.5, pi * 1.5,
  243. (pi * 1.5) + ((pi * 2) * Value / MaxValue), False);
  244. FBitmap.Canvas2D.fillStyle(BGRAPixelTransparent);
  245. FBitmap.Canvas2D.fill;
  246. FBitmap.Canvas2D.lineWidth := EffectiveLineWidth;
  247. FBitmap.Canvas2D.strokeStyle(LineColor);
  248. FBitmap.Canvas2D.stroke;
  249. if MaxValue = 0 then
  250. textStr := '0%'
  251. else
  252. textStr := FloatToStr(Round((Value / MaxValue) * 100)) + '%';
  253. if DrawText then
  254. begin
  255. textBmp := TextShadow(Width, Height, textStr, Font.Height,
  256. Font.Color, FontShadowColor, FontShadowOFfsetX,
  257. FontShadowOffsetY, FontSHadowRadius, Font.Style, Font.Name) as TBGRABitmap;
  258. FBitmap.PutImage(0, 0, textBmp, dmDrawWithTransparency);
  259. textBmp.Free;
  260. end;
  261. end;
  262. procedure TBCRadialProgressBar.SetColor(Value: TColor);
  263. begin
  264. inherited SetColor(Value);
  265. RenderControl;
  266. Invalidate;
  267. end;
  268. constructor TBCRadialProgressBar.Create(AOwner: TComponent);
  269. begin
  270. inherited Create(AOwner);
  271. with GetControlClassDefaultSize do
  272. SetInitialBounds(0, 0, 200, 200);
  273. FMaxValue := 100;
  274. FMinValue := 0;
  275. FValue := 0;
  276. FLineColor := clBlack;
  277. FLineBkgColor := clSilver;
  278. FLineWidth:=0;
  279. FFontShadowColor := clBlack;
  280. FFontShadowOffsetX := 2;
  281. FFontShadowOffsetY := 2;
  282. FFontShadowRadius := 4;
  283. Font.Color := clBlack;
  284. Font.Height := 20;
  285. Color := clWhite;
  286. FRotation := 0;
  287. FDrawText := True;
  288. end;
  289. destructor TBCRadialProgressBar.Destroy;
  290. begin
  291. FreeAndNil(FBitmap);
  292. inherited Destroy;
  293. end;
  294. end.