bgradrawerflashprogressbar.pas 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. unit BGRADrawerFlashProgressBar;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, {$IFDEF BGRABITMAP_USE_MSEGUI} mclasses, {$ENDIF} SysUtils, Types, BGRABitmap, BGRABitmapTypes, BGRAGraphics, BGRAGradients,
  6. Math;
  7. type
  8. TBGRAProgressBarRedrawEvent = procedure(Sender: TObject; Bitmap: TBGRABitmap; xpos: integer) of object;
  9. { TBGRADrawerFlashProgressBar }
  10. TBGRADrawerFlashProgressBar = class(TPersistent)
  11. private
  12. FBackgroundColor: TColor;
  13. FBackgroundRandomize: boolean;
  14. FBackgroundRandomizeMaxIntensity: word;
  15. FBackgroundRandomizeMinIntensity: word;
  16. FBarColor: TColor;
  17. FMaxValue: integer;
  18. FMinValue: integer;
  19. FOnChange: TNotifyEvent;
  20. FRandSeed: integer;
  21. FValue: integer;
  22. xpos: integer;
  23. procedure SetBackgroundRandomize(AValue: boolean);
  24. procedure SetBackgroundRandomizeMaxIntensity(AValue: word);
  25. procedure SetBackgroundRandomizeMinIntensity(AValue: word);
  26. procedure SetBarColor(AValue: TColor);
  27. procedure SetBackgroundColor(AValue: TColor);
  28. procedure SetMaxValue(AValue: integer);
  29. procedure SetMinValue(AValue: integer);
  30. procedure SetRandSeed(AValue: integer);
  31. procedure SetValue(AValue: integer);
  32. public
  33. procedure Draw(ABitmap: TBGRABitmap);
  34. public
  35. property OnChange: TNotifyEvent read FOnChange write FOnChange;
  36. property RandSeed: integer read FRandSeed write SetRandSeed;
  37. property BarColor: TColor read FBarColor write SetBarColor;
  38. property BackgroundColor: TColor read FBackgroundColor write SetBackgroundColor;
  39. property BackgroundRandomizeMinIntensity: word
  40. read FBackgroundRandomizeMinIntensity write SetBackgroundRandomizeMinIntensity;
  41. property BackgroundRandomizeMaxIntensity: word
  42. read FBackgroundRandomizeMaxIntensity write SetBackgroundRandomizeMaxIntensity;
  43. property BackgroundRandomize: boolean read FBackgroundRandomize
  44. write SetBackgroundRandomize;
  45. property XPosition: integer read xpos;
  46. public
  47. property MinValue: integer read FMinValue write SetMinValue;
  48. property MaxValue: integer read FMaxValue write SetMaxValue;
  49. property Value: integer read FValue write SetValue;
  50. end;
  51. implementation
  52. { TBGRADrawerFlashProgressBar }
  53. procedure TBGRADrawerFlashProgressBar.SetBarColor(AValue: TColor);
  54. begin
  55. if FBarColor = AValue then
  56. Exit;
  57. FBarColor := AValue;
  58. if Assigned(FOnChange) then
  59. FOnChange(Self);
  60. if Assigned(FOnChange) then
  61. FOnChange(Self);
  62. end;
  63. procedure TBGRADrawerFlashProgressBar.SetBackgroundRandomize(AValue: boolean);
  64. begin
  65. if FBackgroundRandomize = AValue then
  66. Exit;
  67. FBackgroundRandomize := AValue;
  68. if Assigned(FOnChange) then
  69. FOnChange(Self);
  70. end;
  71. procedure TBGRADrawerFlashProgressBar.SetBackgroundRandomizeMaxIntensity(AValue: word);
  72. begin
  73. if FBackgroundRandomizeMaxIntensity = AValue then
  74. Exit;
  75. FBackgroundRandomizeMaxIntensity := AValue;
  76. if Assigned(FOnChange) then
  77. FOnChange(Self);
  78. end;
  79. procedure TBGRADrawerFlashProgressBar.SetBackgroundRandomizeMinIntensity(AValue: word);
  80. begin
  81. if FBackgroundRandomizeMinIntensity = AValue then
  82. Exit;
  83. FBackgroundRandomizeMinIntensity := AValue;
  84. if Assigned(FOnChange) then
  85. FOnChange(Self);
  86. end;
  87. procedure TBGRADrawerFlashProgressBar.SetBackgroundColor(AValue: TColor);
  88. begin
  89. if FBackgroundColor = AValue then
  90. Exit;
  91. FBackgroundColor := AValue;
  92. if Assigned(FOnChange) then
  93. FOnChange(Self);
  94. end;
  95. procedure TBGRADrawerFlashProgressBar.SetMaxValue(AValue: integer);
  96. begin
  97. if FMaxValue = AValue then
  98. exit;
  99. FMaxValue := AValue;
  100. if FValue > FMaxValue then
  101. FValue := FMaxValue;
  102. if FMinValue > FMaxValue then
  103. FMinValue := FMaxValue;
  104. if Assigned(FOnChange) then
  105. FOnChange(Self);
  106. end;
  107. procedure TBGRADrawerFlashProgressBar.SetMinValue(AValue: integer);
  108. begin
  109. if FMinValue = AValue then
  110. exit;
  111. FMinValue := AValue;
  112. if FValue < FMinValue then
  113. FValue := FMinValue;
  114. if FMaxValue < FMinValue then
  115. FMaxValue := FMinValue;
  116. if Assigned(FOnChange) then
  117. FOnChange(Self);
  118. end;
  119. procedure TBGRADrawerFlashProgressBar.SetRandSeed(AValue: integer);
  120. begin
  121. if FRandSeed = AValue then
  122. Exit;
  123. FRandSeed := AValue;
  124. end;
  125. procedure TBGRADrawerFlashProgressBar.SetValue(AValue: integer);
  126. begin
  127. if FValue = AValue then
  128. exit;
  129. FValue := AValue;
  130. if FValue < FMinValue then
  131. FValue := FMinValue;
  132. if FValue > FMaxValue then
  133. FValue := FMaxValue;
  134. if Assigned(FOnChange) then
  135. FOnChange(Self);
  136. end;
  137. procedure TBGRADrawerFlashProgressBar.Draw(ABitmap: TBGRABitmap);
  138. var
  139. content: TRect;
  140. y, tx, ty: integer;
  141. bgColor: TBGRAPixel;
  142. function ApplyLightness(c: TBGRAPixel; lightness: word): TBGRAPixel;
  143. begin
  144. Result := GammaCompression(SetLightness(GammaExpansion(c), lightness));
  145. end;
  146. procedure DrawBar(bounds: TRect);
  147. var
  148. lCol: TBGRAPixel;
  149. begin
  150. lCol := BarColor;
  151. DoubleGradientAlphaFill(ABitmap, bounds,
  152. ApplyLightness(lCol, 37000), ApplyLightness(lCol, 29000),
  153. ApplyLightness(lCol, 26000), ApplyLightness(lCol, 18000),
  154. gdVertical, gdVertical, gdVertical, 0.53);
  155. InflateRect(bounds, -1, -1);
  156. DoubleGradientAlphaFill(ABitmap, bounds,
  157. ApplyLightness(lCol, 28000), ApplyLightness(lCol, 22000),
  158. ApplyLightness(lCol, 19000), ApplyLightness(lCol, 11000),
  159. gdVertical, gdVertical, gdVertical, 0.53);
  160. end;
  161. begin
  162. ABitmap.FillTransparent;
  163. tx := ABitmap.Width;
  164. ty := ABitmap.Height;
  165. ABitmap.Rectangle(0, 0, tx, ty, BGRA(255, 255, 255, 6), BackgroundColor, dmSet);
  166. if (tx > 2) and (ty > 2) then
  167. ABitmap.Rectangle(1, 1, tx - 1, ty - 1, BGRA(29, 29, 29), dmSet);
  168. if (tx > 4) and (ty > 4) then
  169. begin
  170. content := Rect(2, 2, tx - 2, ty - 2);
  171. randseed := FRandSeed;
  172. if BackgroundRandomize then
  173. for y := content.Top to content.Bottom - 1 do
  174. begin
  175. bgColor := BackgroundColor;
  176. bgColor.Intensity := RandomRange(BackgroundRandomizeMinIntensity, BackgroundRandomizeMaxIntensity);
  177. ABitmap.HorizLine(content.Left, y, content.Right - 1, bgColor, dmSet);
  178. end;
  179. if tx >= 6 then
  180. ABitmap.DrawVertLine(content.Right - 1, content.Top, content.Bottom - 1,
  181. BGRA(0, 0, 0, 32));
  182. if FMaxValue > FMinValue then
  183. begin
  184. xpos := round((FValue - FMinValue) / (FMaxValue - FMinValue) *
  185. (content.right - content.left)) + content.left;
  186. if xpos > content.left then
  187. begin
  188. DrawBar(rect(content.left, content.top, xpos, content.bottom));
  189. if xpos < content.right then
  190. begin
  191. ABitmap.SetPixel(xpos, content.top, BGRA(62, 62, 62));
  192. ABitmap.SetVertLine(xpos, content.top + 1, content.bottom - 1, BGRA(40, 40, 40));
  193. end;
  194. end;
  195. end;
  196. end;
  197. end;
  198. end.