bclearingslider.pas 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. {
  2. *****************************************************************************
  3. See the file COPYING.modifiedLGPL.txt, included in this distribution,
  4. for details about the license.
  5. *****************************************************************************
  6. Author: Boban Spasic
  7. Credits to: hedgehog, circular and lainz from Lazarus forum
  8. Based on TFluentProgressRing from hedgehog
  9. 2024-11-20 Massimo Magnano Added Draw of Caption and TextLayouts
  10. }
  11. unit BCLeaRingSlider;
  12. {$mode ObjFPC}{$H+}
  13. interface
  14. uses
  15. Classes, SysUtils, Controls, Graphics, ExtCtrls, LResources,
  16. BGRABitmapTypes, BGRABitmap, BGRATextFX, BGRAGradients, BCLeaTheme, BCLeaTypes;
  17. type
  18. { TBCLeaRingSlider }
  19. TBCLeaRingSlider = class(TCustomControl)
  20. private
  21. FBitmap: TBGRABitmap;
  22. FTheme: TBCLeaTheme;
  23. FOnChangeValue: TNotifyEvent;
  24. FMaxValue: integer;
  25. FMinValue: integer;
  26. FOffset: integer;
  27. FValue: integer;
  28. FLineColor: TColor;
  29. FLineBkgColor: TColor;
  30. FLineWidth: integer;
  31. FVerticalPos: single;
  32. FDeltaPos: single;
  33. FDirection: integer;
  34. FPrevCurrPosition: single;
  35. FSettingVerticalPos: boolean;
  36. FSensitivity: integer;
  37. FMinAngle: integer;
  38. FMaxAngle: integer;
  39. FFontShadowColor: TColor;
  40. FFontShadowOffsetX: integer;
  41. FFontShadowOffsetY: integer;
  42. FFontShadowRadius: integer;
  43. FDrawText: boolean;
  44. FDrawPointer: boolean;
  45. FBkgColor: TColor;
  46. FStyle: TZStyle;
  47. FDrawTextPhong: boolean;
  48. FPointerColor: TColor;
  49. FPointerSize: integer;
  50. FAltitude: integer;
  51. //global intensity of the light
  52. FLightSourceIntensity: single;
  53. //minimum distance always added (positive value)
  54. FLightSourceDistanceTerm: single;
  55. //how much actual distance is taken into account (usually 0 or 1)
  56. FLightSourceDistanceFactor: single;
  57. //how much the location of the lightened pixel is taken into account (usually 0 or 1)
  58. FLightDestFactor: single;
  59. //color of the light reflection
  60. FLightColor: TColor;
  61. //how much light is reflected (0..1)
  62. FSpecularFactor: single;
  63. //how concentrated reflected light is (positive value)
  64. FSpecularIndex: single;
  65. //ambiant lighting whereever the point is (0..1)
  66. FAmbientFactor: single;
  67. //diffusion, i.e. how much pixels are lightened by light source (0..1)
  68. FDiffusionFactor: single;
  69. //how much hidden surface are darkened (0..1)
  70. FNegativeDiffusionFactor: single;
  71. //when diffusion saturates, use light color to show it
  72. FDiffuseSaturation: boolean;
  73. FLightPositionX: integer;
  74. FLightPositionY: integer;
  75. FLightPositionZ: integer;
  76. rCaptionLayout: TTextLayout;
  77. rDrawCaption: Boolean;
  78. rDrawCaptionPhong: Boolean;
  79. rTextLayout: TTextLayout;
  80. procedure SetCaptionLayout(AValue: TTextLayout);
  81. procedure SetDrawCaption(AValue: Boolean);
  82. procedure SetDrawCaptionPhong(AValue: Boolean);
  83. procedure SetLineBkgColor(AValue: TColor);
  84. procedure SetLineColor(AValue: TColor);
  85. procedure SetMaxValue(AValue: integer);
  86. procedure SetMinValue(AValue: integer);
  87. procedure SetTextLayout(AValue: TTextLayout);
  88. procedure SetValue(AValue: integer);
  89. procedure SetLineWidth(AValue: integer);
  90. procedure UpdateVerticalPos(X, Y: integer);
  91. procedure SetSensitivity(AValue: integer);
  92. procedure SetMinAngle(AValue: integer);
  93. procedure SetMaxAngle(AValue: integer);
  94. procedure SetDrawText(AValue: boolean);
  95. procedure SetDrawPointer(AValue: boolean);
  96. procedure SetBkgColor(AValue: TColor);
  97. procedure SetFFontShadowColor(AValue: TColor);
  98. procedure SetFFontShadowOffsetX(AValue: integer);
  99. procedure SetFFontShadowOffsetY(AValue: integer);
  100. procedure SetFFontShadowRadius(AValue: integer);
  101. procedure SetStyle(AValue: TZStyle);
  102. procedure SetDrawTextPhong(AValue: boolean);
  103. procedure SetPointerColor(AValue: TColor);
  104. procedure SetPointerSize(AValue: integer);
  105. procedure SetAltitude(AValue: integer);
  106. procedure SetTheme(AValue: TBCLeaTheme);
  107. protected
  108. procedure SetEnabled(Value: boolean); override;
  109. procedure SetVisible(Value: boolean); override;
  110. procedure Paint; override;
  111. procedure Resize; override;
  112. procedure Redraw;
  113. procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: integer); override;
  114. procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: integer); override;
  115. procedure MouseMove(Shift: TShiftState; X, Y: integer); override;
  116. public
  117. constructor Create(AOwner: TComponent); override;
  118. destructor Destroy; override;
  119. procedure UpdateTheme;
  120. procedure ApplyTheme;
  121. procedure SaveThemeToFile(AFileName: string);
  122. procedure LoadThemeFromFile(AFileName: string);
  123. procedure ApplyDefaultTheme;
  124. published
  125. property Align;
  126. property BorderSpacing;
  127. property Caption;
  128. property Color;
  129. property Cursor;
  130. property Enabled;
  131. property Font;
  132. property ParentColor;
  133. property ParentFont;
  134. property ParentShowHint;
  135. property PopupMenu;
  136. property ShowHint;
  137. property TabOrder;
  138. property TabStop default True;
  139. property Anchors;
  140. property Constraints;
  141. property Visible;
  142. property OnClick;
  143. property OnDblClick;
  144. property OnEnter;
  145. property OnExit;
  146. property OnMouseMove;
  147. property OnMouseDown;
  148. property OnMouseUp;
  149. property OnMouseWheel;
  150. property OnMouseWheelDown;
  151. property OnMouseWheelUp;
  152. property OnKeyDown;
  153. property OnKeyUp;
  154. property OnKeyPress;
  155. property OnConTextPopup;
  156. property MinValue: integer read FMinValue write SetMinValue default 0;
  157. property MaxValue: integer read FMaxValue write SetMaxValue default 100;
  158. property Value: integer read FValue write SetValue default 0;
  159. property LineColor: TColor read FLineColor write SetLineColor default TColor($009E5A00);
  160. property LineBkgColor: TColor read FLineBkgColor write SetLineBkgColor default TColor($00D3D3D3);
  161. property LineWidth: integer read FLineWidth write SetLineWidth default 8;
  162. property OnChangeValue: TNotifyEvent read FOnChangeValue write FOnChangeValue;
  163. //Greater value is less sensitive
  164. property Sensitivity: integer read FSensitivity write SetSensitivity default 10;
  165. property MinAngle: integer read FMinAngle write SetMinAngle default 20;
  166. property MaxAngle: integer read FMaxAngle write SetMaxAngle default 340;
  167. property FontShadowColor: TColor read FFontShadowColor write SetFFontShadowColor default clBlack;
  168. property FontShadowOffsetX: integer read FFontShadowOffsetX write SetFFontShadowOffsetX default 2;
  169. property FontShadowOffsetY: integer read FFontShadowOffsetY write SetFFontShadowOffsetY default 2;
  170. property FontShadowRadius: integer read FFontSHadowRadius write SetFFontShadowRadius default 4;
  171. property DrawText: boolean read FDrawText write SetDrawText default True;
  172. property DrawPointer: boolean read FDrawPointer write SetDrawPointer default False;
  173. property BackgroundColor: TColor read FBkgColor write SetBkgColor default clBtnFace;
  174. property Style: TZStyle read FStyle write SetStyle default zsRaised;
  175. property DrawTextPhong: boolean read FDrawTextPhong write SetDrawTextPhong default False;
  176. property PointerColor: TColor read FPointerColor write SetPointerColor default TColor($00FF9C15);
  177. property PointerSize: integer read FPointerSize write SetPointerSize default 2;
  178. property Altitude: integer read FAltitude write SetAltitude default 2;
  179. property Theme: TBCLeaTheme read FTheme write SetTheme;
  180. property TextLayout: TTextLayout read rTextLayout write SetTextLayout default tlCenter;
  181. property DrawCaption: Boolean read rDrawCaption write SetDrawCaption default False;
  182. property DrawCaptionPhong: Boolean read rDrawCaptionPhong write SetDrawCaptionPhong default False;
  183. property CaptionLayout: TTextLayout read rCaptionLayout write SetCaptionLayout default tlBottom;
  184. end;
  185. procedure Register;
  186. implementation
  187. procedure Register;
  188. begin
  189. RegisterComponents('BGRA Controls', [TBCLeaRingSlider]);
  190. end;
  191. { TBCLeaRingSlider }
  192. procedure TBCLeaRingSlider.SetMaxAngle(AValue: integer);
  193. begin
  194. if FMaxAngle = AValue then
  195. exit;
  196. FMaxAngle := AValue;
  197. if FMaxAngle > 350 then
  198. FMaxAngle := 350;
  199. if FMinAngle > FMaxAngle then
  200. FMaxAngle := FMinAngle;
  201. Invalidate;
  202. end;
  203. procedure TBCLeaRingSlider.SetMinAngle(AValue: integer);
  204. begin
  205. if FMinAngle = AValue then
  206. exit;
  207. FMinAngle := AValue;
  208. if FMinAngle < 10 then
  209. FMinAngle := 10;
  210. if FMinAngle > FMaxAngle then
  211. FMinAngle := FMaxAngle;
  212. Invalidate;
  213. end;
  214. procedure TBCLeaRingSlider.SetSensitivity(AValue: integer);
  215. begin
  216. if FSensitivity = AValue then
  217. exit;
  218. if AValue <> 0 then
  219. FSensitivity := AValue
  220. else
  221. FSensitivity := 10;
  222. end;
  223. procedure TBCLeaRingSlider.SetMaxValue(AValue: integer);
  224. begin
  225. if FMaxValue = AValue then
  226. exit;
  227. FMaxValue := AValue;
  228. if FValue > FMaxValue then
  229. FValue := FMaxValue;
  230. if FMinValue > FMaxValue then
  231. FMinValue := FMaxValue;
  232. Invalidate;
  233. end;
  234. procedure TBCLeaRingSlider.SetLineBkgColor(AValue: TColor);
  235. begin
  236. if FLineBkgColor = AValue then
  237. Exit;
  238. FLineBkgColor := AValue;
  239. Invalidate;
  240. end;
  241. procedure TBCLeaRingSlider.SetCaptionLayout(AValue: TTextLayout);
  242. begin
  243. if rCaptionLayout=AValue then Exit;
  244. rCaptionLayout:=AValue;
  245. Invalidate;
  246. end;
  247. procedure TBCLeaRingSlider.SetDrawCaption(AValue: Boolean);
  248. begin
  249. if rDrawCaption=AValue then Exit;
  250. rDrawCaption:=AValue;
  251. Invalidate;
  252. end;
  253. procedure TBCLeaRingSlider.SetDrawCaptionPhong(AValue: Boolean);
  254. begin
  255. if rDrawCaptionPhong=AValue then Exit;
  256. rDrawCaptionPhong:=AValue;
  257. Invalidate;
  258. end;
  259. procedure TBCLeaRingSlider.SetLineColor(AValue: TColor);
  260. begin
  261. if FLineColor = AValue then
  262. Exit;
  263. FLineColor := AValue;
  264. Invalidate;
  265. end;
  266. procedure TBCLeaRingSlider.SetMinValue(AValue: integer);
  267. begin
  268. if FMinValue = AValue then
  269. exit;
  270. FMinValue := AValue;
  271. if FMinValue <> 0 then FOffset := -FMinValue;
  272. if FValue < FMinValue then
  273. FValue := FMinValue;
  274. if FMaxValue < FMinValue then
  275. FMaxValue := FMinValue;
  276. Invalidate;
  277. end;
  278. procedure TBCLeaRingSlider.SetTextLayout(AValue: TTextLayout);
  279. begin
  280. if rTextLayout=AValue then Exit;
  281. rTextLayout:=AValue;
  282. Invalidate;
  283. end;
  284. procedure TBCLeaRingSlider.SetValue(AValue: integer);
  285. begin
  286. if FValue = AValue then
  287. exit;
  288. FValue := AValue;
  289. if FValue < FMinValue then
  290. FValue := FMinValue;
  291. if FValue > FMaxValue then
  292. FValue := FMaxValue;
  293. Invalidate;
  294. end;
  295. procedure TBCLeaRingSlider.SetLineWidth(AValue: integer);
  296. begin
  297. if FLineWidth = AValue then exit;
  298. FLineWidth := AValue;
  299. if Visible then Redraw;
  300. end;
  301. procedure TBCLeaRingSlider.SetEnabled(Value: boolean);
  302. begin
  303. inherited SetEnabled(Value);
  304. Invalidate;
  305. end;
  306. procedure TBCLeaRingSlider.SetVisible(Value: boolean);
  307. begin
  308. inherited SetVisible(Value);
  309. Invalidate;
  310. end;
  311. procedure TBCLeaRingSlider.Paint;
  312. begin
  313. inherited Paint;
  314. Redraw;
  315. end;
  316. procedure TBCLeaRingSlider.Resize;
  317. begin
  318. inherited Resize;
  319. {$IFDEF LCLgtk2} Invalidate; {$ENDIF}
  320. end;
  321. procedure TBCLeaRingSlider.Redraw;
  322. const
  323. pi15 = pi * 1.5;
  324. var
  325. TextBmp: TBGRABitmap;
  326. TextStr: string;
  327. EffectiveSize, ScaledPhongSize: integer;
  328. EffectiveLineWidth: single;
  329. r: single;
  330. RMinAngle, RMaxAngle: single;
  331. RValue: single;
  332. Blur: TBGRABitmap;
  333. Mask, Mask2: TBGRABitmap;
  334. Phong: TPhongShading;
  335. TextSize: TSize;
  336. procedure DoDrawArc(a, b: single; c: TColor);
  337. begin
  338. FBitmap.Canvas2D.strokeStyle(c);
  339. FBitmap.Canvas2D.beginPath;
  340. FBitmap.Canvas2D.arc(0, 0, r, a, b, False);
  341. FBitmap.Canvas2D.stroke;
  342. end;
  343. procedure DoDrawPointer(a: single; c: TColor);
  344. begin
  345. FBitmap.Canvas2D.strokeStyle(c);
  346. FBitmap.Canvas2D.beginPath;
  347. FBitmap.Canvas2D.arc(0, 0, r, a - (FPointerSize / 100), a + (FPointerSize / 100), False);
  348. FBitmap.Canvas2D.stroke;
  349. end;
  350. procedure DoDrawTick(a, b: single; c: TColor);
  351. begin
  352. FBitmap.Canvas2D.strokeStyle(c);
  353. FBitmap.Canvas2D.lineWidth := 5;
  354. FBitmap.Canvas2D.beginPath;
  355. FBitmap.Canvas2D.lineTo(0 - a, 0 - b);
  356. FBitmap.Canvas2D.lineTo(5 - a, -2 - b);
  357. FBitmap.Canvas2D.lineTo(5 - a, 2 - b);
  358. FBitmap.Canvas2D.lineTo(0 - a, 0 - b);
  359. FBitmap.Canvas2D.stroke;
  360. end;
  361. begin
  362. FBitmap.SetSize(Width, Height);
  363. FBitmap.Fill(FBkgColor);
  364. if Width < Height then
  365. EffectiveSize := Width
  366. else
  367. EffectiveSize := Height;
  368. if EffectiveSize < 2 then exit;
  369. FBitmap.Canvas2D.resetTransform;
  370. FBitmap.Canvas2D.translate((FBitmap.Width-1)/2, (FBitmap.Height-1)/2);
  371. FBitmap.Canvas2D.rotate(pi15);
  372. if FLineWidth = 0 then
  373. EffectiveLineWidth := EffectiveSize / 12
  374. else
  375. EffectiveLineWidth := FLineWidth;
  376. r := (EffectiveSize - EffectiveLineWidth) / 2;
  377. FBitmap.Canvas2D.lineWidth := EffectiveLineWidth;
  378. RMinAngle := (180 + FMinAngle) * pi / 180;
  379. RMaxAngle := ((180 + FMaxAngle) * pi / 180);
  380. RValue := ((180 + FMinAngle + ((FMaxAngle - FMinAngle) / FMaxValue * FValue)) * pi / 180);
  381. FBitmap.Canvas2D.lineCapLCL := pecRound;
  382. // background line
  383. if FLineBkgColor <> clNone then
  384. DoDrawArc(RMinAngle, RMaxAngle, FLineBkgColor);
  385. if Enabled then
  386. begin
  387. if FValue > FMinValue then
  388. begin
  389. DoDrawArc(RMinAngle, RValue, FLineColor);
  390. if FDrawPointer then
  391. DoDrawPointer(RValue, FPointerColor);
  392. end;
  393. end
  394. else
  395. DoDrawArc(RMinAngle, RMaxAngle, clGray);
  396. if FDrawText and FDrawTextPhong then
  397. begin
  398. TextStr := IntToStr(FValue);
  399. TextBmp := TextShadow(FBitmap.Width, FBitmap.Height, TextStr, Font.Height,
  400. Font.Color, FontShadowColor, FontShadowOffsetX,
  401. FontShadowOffsetY, FontShadowRadius, Font.Style, Font.Name) as TBGRABitmap;
  402. TextSize:= TextBmp.TextSize(TextStr);
  403. TextSize.cy:= TextSize.cy+FontShadowOffsetY; //+2*FontShadowRadius ?
  404. Case rTextLayout of
  405. tlTop: FBitmap.PutImage(0, -(HalfUp(((EffectiveSize-TextSize.cy) / 2))-Trunc(EffectiveLineWidth)),
  406. TextBmp, dmDrawWithTransparency);
  407. tlCenter: FBitmap.PutImage(0, 0, TextBmp, dmDrawWithTransparency);
  408. tlBottom: FBitmap.PutImage(0, +(HalfUp(((EffectiveSize-TextSize.cy) / 2))-Trunc(EffectiveLineWidth)),
  409. TextBmp, dmDrawWithTransparency);
  410. end;
  411. TextBmp.Free;
  412. end;
  413. if rDrawCaption and rDrawCaptionPhong then
  414. begin
  415. TextBmp := TextShadow(FBitmap.Width, FBitmap.Height, Caption, Font.Height,
  416. Font.Color, FontShadowColor, FontShadowOffsetX,
  417. FontShadowOffsetY, FontShadowRadius, Font.Style, Font.Name) as TBGRABitmap;
  418. TextSize:= TextBmp.TextSize(Caption);
  419. TextSize.cy:= TextSize.cy+FontShadowOffsetY; //+2*FontShadowRadius ?
  420. Case rCaptionLayout of
  421. tlTop: FBitmap.PutImage(0, -(HalfUp(((EffectiveSize-TextSize.cy) / 2))-Trunc(EffectiveLineWidth)),
  422. TextBmp, dmDrawWithTransparency);
  423. tlCenter: FBitmap.PutImage(0, 0, TextBmp, dmDrawWithTransparency);
  424. tlBottom: FBitmap.PutImage(0, +(HalfUp(((EffectiveSize-TextSize.cy) / 2))-Trunc(EffectiveLineWidth)),
  425. TextBmp, dmDrawWithTransparency);
  426. end;
  427. TextBmp.Free;
  428. end;
  429. if (FStyle = zsRaised) or (FStyle = zsLowered) then
  430. begin
  431. ScaledPhongSize := round(EffectiveLineWidth / 2);
  432. Mask := FBitmap.FilterGrayscale as TBGRABitmap;
  433. if FStyle = zsRaised then
  434. Mask.Negative;
  435. Blur := Mask.FilterBlurRadial(ScaledPhongSize, ScaledPhongSize, rbFast) as TBGRABitmap;
  436. Blur.FillMask(0, 0, Mask, BGRAPixelTransparent, dmSet);
  437. Mask.Free;
  438. Phong := TPhongShading.Create;
  439. begin
  440. Phong.AmbientFactor := FAmbientFactor;
  441. Phong.SpecularIndex := FSpecularIndex;
  442. Phong.LightDestFactor := FLightDestFactor;
  443. Phong.LightPosition := Point(FLightPositionX + (FBitmap.Width - EffectiveSize) div 2,
  444. FLightPositionY + (FBitmap.Height - EffectiveSize) div 2);
  445. Phong.LightPositionZ := FLightPositionZ;
  446. Phong.LightSourceIntensity := FLightSourceIntensity;
  447. Phong.LightSourceDistanceTerm := FLightSourceDistanceTerm;
  448. Phong.LightSourceDistanceFactor := FLightSourceDistanceFactor;
  449. Phong.NegativeDiffusionFactor := FNegativeDiffusionFactor;
  450. Phong.SpecularFactor := FSpecularFactor;
  451. Phong.DiffusionFactor := FDiffusionFactor;
  452. Phong.DiffuseSaturation := FDiffuseSaturation;
  453. Phong.LightColor := FLightColor;
  454. end;
  455. Phong.Draw(FBitmap, Blur, FAltitude, 0, 0, FBitmap);
  456. Phong.Free;
  457. Blur.Free;
  458. Mask := TBGRABitmap.Create(FBitmap.Width, FBitmap.Height, BGRABlack);
  459. Mask.FillEllipseAntialias((FBitmap.Width-1)/2, (FBitmap.Height-1)/2, EffectiveSize div 2, EffectiveSize div 2, BGRAWhite);
  460. Mask2 := TBGRABitmap.Create(FBitmap.Width, FBitmap.Height, ColorToBGRA(ColorToRGB(FBkgColor)));
  461. Mask2.PutImage(0, 0, FBitmap, dmSet);
  462. Mask2.ApplyMask(Mask);
  463. Mask.Free;
  464. FBitmap.Fill(FBkgColor);
  465. FBitmap.PutImage(0, 0, Mask2, dmDrawWithTransparency);
  466. Mask2.Free;
  467. end;
  468. if FDrawText and not FDrawTextPhong then
  469. begin
  470. TextStr := IntToStr(FValue);
  471. TextBmp := TextShadow(FBitmap.Width, FBitmap.Height, TextStr, Font.Height,
  472. Font.Color, FontShadowColor, FontShadowOffsetX,
  473. FontShadowOffsetY, FontShadowRadius, Font.Style, Font.Name) as TBGRABitmap;
  474. TextSize:= TextBmp.TextSize(TextStr);
  475. TextSize.cy:= TextSize.cy+FontShadowOffsetY; //+2*FontShadowRadius ?
  476. Case rTextLayout of
  477. tlTop: FBitmap.PutImage(0, -(HalfUp(((EffectiveSize-TextSize.cy) / 2))-Trunc(EffectiveLineWidth)),
  478. TextBmp, dmDrawWithTransparency);
  479. tlCenter: FBitmap.PutImage(0, 0, TextBmp, dmDrawWithTransparency);
  480. tlBottom: FBitmap.PutImage(0, +(HalfUp(((EffectiveSize-TextSize.cy) / 2))-Trunc(EffectiveLineWidth)),
  481. TextBmp, dmDrawWithTransparency);
  482. end;
  483. TextBmp.Free;
  484. end;
  485. if rDrawCaption and not(rDrawCaptionPhong) then
  486. begin
  487. TextBmp := TextShadow(FBitmap.Width, FBitmap.Height, Caption, Font.Height,
  488. Font.Color, FontShadowColor, FontShadowOffsetX,
  489. FontShadowOffsetY, FontShadowRadius, Font.Style, Font.Name) as TBGRABitmap;
  490. TextSize:= TextBmp.TextSize(Caption);
  491. TextSize.cy:= TextSize.cy+FontShadowOffsetY; //+2*FontShadowRadius ?
  492. Case rCaptionLayout of
  493. tlTop: FBitmap.PutImage(0, -(HalfUp(((EffectiveSize-TextSize.cy) / 2))-Trunc(EffectiveLineWidth)),
  494. TextBmp, dmDrawWithTransparency);
  495. tlCenter: FBitmap.PutImage(0, 0, TextBmp, dmDrawWithTransparency);
  496. tlBottom: FBitmap.PutImage(0, +(HalfUp(((EffectiveSize-TextSize.cy) / 2))-Trunc(EffectiveLineWidth)),
  497. TextBmp, dmDrawWithTransparency);
  498. end;
  499. TextBmp.Free;
  500. end;
  501. FBitmap.Draw(Canvas, 0, 0, True);
  502. end;
  503. constructor TBCLeaRingSlider.Create(AOwner: TComponent);
  504. begin
  505. inherited Create(AOwner);
  506. with GetControlClassDefaultSize do
  507. SetInitialBounds(0, 0, 100, 100);
  508. TabStop := True;
  509. FMaxValue := 100;
  510. FMinValue := 0;
  511. FOffset := 0;
  512. FMinAngle := 20;
  513. FMaxAngle := 340;
  514. FValue := 0;
  515. FDeltaPos := 0;
  516. FDirection := 0;
  517. FSensitivity := 10;
  518. Font.Color := clBlack;
  519. Font.Height := 20;
  520. FDrawText := True;
  521. rTextLayout:= tlCenter;
  522. FDrawPointer := False;
  523. rDrawCaption:= False;
  524. rCaptionLayout:= tlBottom;
  525. ApplyDefaultTheme;
  526. FBitmap := TBGRABitmap.Create(Width, Height, FBkgColor);
  527. end;
  528. destructor TBCLeaRingSlider.Destroy;
  529. begin
  530. FreeAndNil(FBitmap);
  531. inherited Destroy;
  532. end;
  533. procedure TBCLeaRingSlider.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: integer);
  534. begin
  535. inherited MouseDown(Button, Shift, X, Y);
  536. if Button = mbLeft then
  537. begin
  538. FDeltaPos := ((ClientHeight / FSensitivity) - (Y / FSensitivity)) * (FMaxValue / ClientHeight);
  539. FDirection := 0;
  540. FPrevCurrPosition := 0;
  541. FVerticalPos := FValue;
  542. FSettingVerticalPos := True;
  543. end;
  544. end;
  545. procedure TBCLeaRingSlider.MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: integer);
  546. begin
  547. inherited MouseUp(Button, Shift, X, Y);
  548. if Button = mbLeft then
  549. FSettingVerticalPos := False;
  550. end;
  551. procedure TBCLeaRingSlider.MouseMove(Shift: TShiftState; X, Y: integer);
  552. begin
  553. inherited MouseMove(Shift, X, Y);
  554. if FSettingVerticalPos then
  555. UpdateVerticalPos(X, Y);
  556. end;
  557. procedure TBCLeaRingSlider.UpdateVerticalPos(X, Y: integer);
  558. var
  559. FPreviousPos: single;
  560. FCurrPos: single;
  561. FNewDirection: integer;
  562. begin
  563. {The whole code here is for beter control of the slider with the mouse movements}
  564. FPreviousPos := FVerticalPos;
  565. FCurrPos := ((ClientHeight / FSensitivity) - (Y / FSensitivity)) * (FMaxValue / ClientHeight);
  566. if FPrevCurrPosition <> 0 then
  567. begin
  568. if FCurrPos < FPrevCurrPosition then FNewDirection := -1;
  569. if FCurrPos > FPrevCurrPosition then FNewDirection := 1;
  570. if FNewDirection <> FDirection then
  571. begin
  572. FDirection := FNewDirection;
  573. FDeltaPos := ((ClientHeight / FSensitivity) - (Y / FSensitivity)) * (FMaxValue / ClientHeight);
  574. end;
  575. end;
  576. FPrevCurrPosition := FCurrPos;
  577. FVerticalPos := FVerticalPos - FDeltaPos + FCurrPos;
  578. if FVerticalPos < FMinValue then FVerticalPos := FMinValue;
  579. if FVerticalPos > FMaxValue then FVerticalPos := FMaxValue;
  580. FValue := round(FVerticalPos);
  581. if FValue < FMinValue then
  582. FValue := FMinValue;
  583. if FValue > FMaxValue then
  584. FValue := FMaxValue;
  585. Redraw;
  586. if (FPreviousPos <> FVerticalPos) and Assigned(FOnChangeValue) then
  587. FOnChangeValue(Self);
  588. end;
  589. procedure TBCLeaRingSlider.SetFFontShadowColor(AValue: TColor);
  590. begin
  591. if FFontShadowColor = AValue then
  592. Exit;
  593. FFontShadowColor := AValue;
  594. Invalidate;
  595. end;
  596. procedure TBCLeaRingSlider.SetDrawText(AValue: boolean);
  597. begin
  598. if FDrawText = AValue then Exit;
  599. FDrawText := AValue;
  600. Invalidate;
  601. end;
  602. procedure TBCLeaRingSlider.SetFFontShadowOffsetX(AValue: integer);
  603. begin
  604. if FFontShadowOffsetX = AValue then
  605. Exit;
  606. FFontShadowOffsetX := AValue;
  607. Invalidate;
  608. end;
  609. procedure TBCLeaRingSlider.SetFFontShadowOffsetY(AValue: integer);
  610. begin
  611. if FFontShadowOffsetY = AValue then
  612. Exit;
  613. FFontShadowOffsetY := AValue;
  614. Invalidate;
  615. end;
  616. procedure TBCLeaRingSlider.SetFFontShadowRadius(AValue: integer);
  617. begin
  618. if FFontSHadowRadius = AValue then
  619. Exit;
  620. FFontSHadowRadius := AValue;
  621. Invalidate;
  622. end;
  623. procedure TBCLeaRingSlider.SetBkgColor(AValue: TColor);
  624. begin
  625. if FBkgColor = AValue then
  626. Exit;
  627. FBkgColor := AValue;
  628. Invalidate;
  629. end;
  630. procedure TBCLeaRingSlider.SetDrawPointer(AValue: boolean);
  631. begin
  632. if FDrawPointer = AValue then
  633. Exit;
  634. FDrawPointer := AValue;
  635. Invalidate;
  636. end;
  637. procedure TBCLeaRingSlider.SetStyle(AValue: TZStyle);
  638. begin
  639. if FStyle = AValue then
  640. Exit;
  641. FStyle := AValue;
  642. Invalidate;
  643. end;
  644. procedure TBCLeaRingSlider.SetDrawTextPhong(AValue: boolean);
  645. begin
  646. if FDrawTextPhong = AValue then
  647. Exit;
  648. FDrawTextPhong := AValue;
  649. Invalidate;
  650. end;
  651. procedure TBCLeaRingSlider.SetPointerColor(AValue: TColor);
  652. begin
  653. if FPointerColor = AValue then
  654. Exit;
  655. FPointerColor := AValue;
  656. Invalidate;
  657. end;
  658. procedure TBCLeaRingSlider.SetPointerSize(AValue: integer);
  659. begin
  660. if FPointerSize = AValue then
  661. Exit;
  662. FPointerSize := AValue;
  663. Invalidate;
  664. end;
  665. procedure TBCLeaRingSlider.SetAltitude(AValue: integer);
  666. begin
  667. if FAltitude = AValue then
  668. Exit;
  669. FAltitude := AValue;
  670. Invalidate;
  671. end;
  672. procedure TBCLeaRingSlider.SetTheme(AValue: TBCLeaTheme);
  673. begin
  674. if FTheme = AValue then
  675. Exit;
  676. if Assigned(FTheme) then
  677. FTheme := nil;
  678. FTheme := AValue;
  679. ApplyTheme;
  680. end;
  681. procedure TBCLeaRingSlider.UpdateTheme;
  682. begin
  683. if Assigned(FTheme) then
  684. begin
  685. FTheme.RS_LineWidth := FLineWidth;
  686. FTheme.RS_LineColor := FLineColor;
  687. FTheme.RS_LineBkgColor := FLineBkgColor;
  688. FTheme.RS_BkgColor := FBkgColor;
  689. FTheme.RS_FontShadowColor := FFontShadowColor;
  690. FTheme.RS_FontShadowOffsetX := FFontShadowOffsetX;
  691. FTheme.RS_FontShadowOffsetY := FFontShadowOffsetY;
  692. FTheme.RS_FontShadowRadius := FFontShadowRadius;
  693. FTheme.RS_PointerSize := FPointerSize;
  694. FTheme.RS_PointerColor := FPointerColor;
  695. FTheme.RS_Style := FStyle;
  696. FTheme.RS_DrawTextPhong := FDrawTextPhong;
  697. FTheme.RS_Altitude := FAltitude;
  698. end;
  699. end;
  700. procedure TBCLeaRingSlider.ApplyTheme;
  701. begin
  702. if Assigned(FTheme) then
  703. begin
  704. FLineWidth := FTheme.RS_LineWidth;
  705. FLineColor := FTheme.RS_LineColor;
  706. FLineBkgColor := FTheme.RS_LineBkgColor;
  707. FBkgColor := FTheme.RS_BkgColor;
  708. FFontShadowColor := FTheme.RS_FontShadowColor;
  709. FFontShadowOffsetX := FTheme.RS_FontShadowOffsetX;
  710. FFontShadowOffsetY := FTheme.RS_FontShadowOffsetY;
  711. FFontShadowRadius := FTheme.RS_FontShadowRadius;
  712. FPointerSize := FTheme.RS_PointerSize;
  713. FPointerColor := Ftheme.RS_PointerColor;
  714. FStyle := FTheme.RS_Style;
  715. FDrawTextPhong := FTheme.RS_DrawTextPhong;
  716. FAltitude := FTheme.RS_Altitude;
  717. FLightSourceIntensity := FTheme.COM_LightSourceIntensity;
  718. FLightSourceDistanceTerm := FTheme.COM_LightSourceDistanceTerm;
  719. FLightSourceDistanceFactor := FTheme.COM_LightSourceDistanceFactor;
  720. FLightDestFactor := FTheme.COM_LightDestFactor;
  721. FLightColor := FTheme.COM_LightColor;
  722. FSpecularFactor := FTheme.COM_SpecularFactor;
  723. FSpecularIndex := FTheme.COM_SpecularIndex;
  724. FAmbientFactor := FTheme.COM_AmbientFactor;
  725. FDiffusionFactor := FTheme.COM_DiffusionFactor;
  726. FNegativeDiffusionFactor := FTheme.COM_NegativeDiffusionFactor;
  727. FDiffuseSaturation := FTheme.COM_DiffuseSaturation;
  728. FLightPositionX := FTheme.COM_LightPositionX;
  729. FLightPositionY := FTheme.COM_LightPositionY;
  730. FLightPositionZ := FTheme.COM_LightPositionZ;
  731. Invalidate;
  732. end
  733. else
  734. begin
  735. ApplyDefaultTheme;
  736. end;
  737. end;
  738. procedure TBCLeaRingSlider.SaveThemeToFile(AFileName: string);
  739. begin
  740. if Assigned(FTheme) then
  741. FTheme.SaveThemeToFile(AFileName);
  742. end;
  743. procedure TBCLeaRingSlider.LoadThemeFromFile(AFileName: string);
  744. begin
  745. if Assigned(FTheme) then
  746. FTheme.LoadThemeFromFile(AFileName);
  747. end;
  748. procedure TBCLeaRingSlider.ApplyDefaultTheme;
  749. begin
  750. FLineWidth := 8;
  751. FLineColor := TColor($009E5A00);
  752. FLineBkgColor := TColor($00D3D3D3);
  753. FBkgColor := clBtnFace;
  754. FFontShadowColor := clBlack;
  755. FFontShadowOffsetX := 2;
  756. FFontShadowOffsetY := 2;
  757. FFontShadowRadius := 4;
  758. FPointerSize := 3;
  759. FPointerColor := TColor($00FF9C15);
  760. FStyle := zsRaised;
  761. FDrawTextPhong := False;
  762. FAltitude := 2;
  763. FAmbientFactor := 0.3;
  764. FSpecularIndex := 10;
  765. FSpecularFactor := 0.6;
  766. FLightDestFactor := 1;
  767. FLightPositionX := -100;
  768. FLightPositionY := -100;
  769. FLightPositionZ := 100;
  770. FLightSourceIntensity := 500;
  771. FLightSourceDistanceTerm := 150;
  772. FLightSourceDistanceFactor := 1;
  773. FNegativeDiffusionFactor := 0.1;
  774. FLightColor := clWhite;
  775. FDiffuseSaturation := False;
  776. FDiffusionFactor := 0.9;
  777. end;
  778. end.