bceffect.pas 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // SPDX-License-Identifier: LGPL-3.0-linking-exception
  2. {
  3. Created by BGRA Controls Team
  4. Dibo, Circular, lainz (007) and contributors.
  5. For detailed information see readme.txt
  6. Site: https://sourceforge.net/p/bgra-controls/
  7. Wiki: http://wiki.lazarus.freepascal.org/BGRAControls
  8. Forum: http://forum.lazarus.freepascal.org/index.php/board,46.0.html
  9. }
  10. {******************************* CONTRIBUTOR(S) ******************************
  11. - Edivando S. Santos Brasil | [email protected]
  12. (Compatibility with delphi VCL 11/2018)
  13. ***************************** END CONTRIBUTOR(S) *****************************}
  14. unit BCEffect;
  15. {$I bgracontrols.inc}
  16. {$IFDEF FPC}
  17. {$modeswitch advancedrecords}
  18. {$ENDIF}
  19. interface
  20. uses
  21. Classes, SysUtils, {$IFDEF FPC}LCLProc, LazUTF8, {$ELSE}Types, BGRAGraphics, GraphType, FPImage, {$ENDIF} BGRABitmapTypes;
  22. {-- Fading --}
  23. type
  24. TFadingMode = (fmSuspended, fmFadeIn, fmFadeOut, fmFadeInCycle, fmFadeOutCycle, fmFadeInOut, fmFadeOutIn);
  25. const
  26. FadingModeStr: array[TFadingMode] of string = ('Suspended', 'Fade In', 'Fade Out', 'Fade In Cycle','Fade Out Cycle', 'Fade In Out', 'Fade Out In');
  27. function StrToTFadingMode(const s: ansistring): TFadingMode;
  28. procedure FadingModeStrList(s: TStrings);
  29. type
  30. { TFading }
  31. TFading = record
  32. private
  33. FAlpha: byte;
  34. FMode: TFadingMode;
  35. FAlphaStep: byte;
  36. FDuration: integer;
  37. FPrevDate: TDateTime;
  38. FElapsedMsAccumulator: integer;
  39. public
  40. procedure SetFAlpha(AValue: byte);
  41. procedure SetFMode(AValue: TFadingMode);
  42. procedure SetFAlphaStep(AValue: byte);
  43. procedure SetFDuration(AValue: integer);
  44. public
  45. function Execute(AStepCount: integer= 1): byte; // execute and return new alpha
  46. function Reset: byte; // reset and return new alpha
  47. procedure PutImage(ADestination: TBGRACustomBitmap; AX,AY: integer; ASource: TBGRACustomBitmap);
  48. procedure FillRect(ADestination: TBGRACustomBitmap; ARect: TRect; AColor: TBGRAPixel);
  49. public
  50. property Alpha: byte read FAlpha write SetFAlpha;
  51. property Mode: TFadingMode read FMode write SetFMode;
  52. property Step: byte read FAlphaStep write SetFAlphaStep;
  53. property Duration: integer read FDuration write SetFDuration;
  54. end;
  55. {-- Fading --}
  56. implementation
  57. {-- Fading --}
  58. function StrToTFadingMode(const s: ansistring): TFadingMode;
  59. var
  60. fm: TFadingMode;
  61. ls: ansistring;
  62. begin
  63. ls := {$IFDEF FPC}UTF8LowerCase{$ELSE}LowerCase{$ENDIF}(s);
  64. for fm := low(TFadingMode) to high(TFadingMode) do
  65. if ls = {$IFDEF FPC}UTF8LowerCase{$ELSE}LowerCase{$ENDIF}(FadingModeStr[fm]) then
  66. begin
  67. Result := fm;
  68. break;
  69. end;
  70. Result := fm;
  71. end;
  72. procedure FadingModeStrList(s: TStrings);
  73. var
  74. fm: TFadingMode;
  75. begin
  76. for fm := low(TFadingMode) to high(TFadingMode) do
  77. s.Add(FadingModeStr[fm]);
  78. end;
  79. { TFading }
  80. procedure TFading.SetFAlpha(AValue: byte);
  81. begin
  82. if FAlpha = AValue then
  83. Exit;
  84. FAlpha := AValue;
  85. end;
  86. procedure TFading.SetFMode(AValue: TFadingMode);
  87. begin
  88. if FMode = AValue then
  89. Exit;
  90. FMode := AValue;
  91. FPrevDate:= 0;
  92. end;
  93. procedure TFading.SetFAlphaStep(AValue: byte);
  94. begin
  95. if FAlphaStep = AValue then
  96. Exit
  97. else
  98. FAlphaStep := AValue;
  99. end;
  100. procedure TFading.SetFDuration(AValue: integer);
  101. begin
  102. FDuration:= AValue;
  103. end;
  104. function TFading.Execute(AStepCount: integer= 1): byte;
  105. var curDate: TDateTime;
  106. alphaStep: byte;
  107. timeGrain: integer;
  108. begin
  109. if FAlphaStep <= 0 then
  110. alphaStep := 1
  111. else
  112. alphaStep := FAlphaStep;
  113. if FDuration > 0 then
  114. begin
  115. curDate := Now;
  116. if FPrevDate = 0 then
  117. begin
  118. FPrevDate := curDate;
  119. FElapsedMsAccumulator := 0;
  120. result := FAlpha;
  121. exit;
  122. end;
  123. inc(FElapsedMsAccumulator, round((curDate-FPrevDate)*(24*60*60*1000)) );
  124. timeGrain := round(FDuration*alphaStep/255);
  125. if timeGrain <= 0 then timeGrain := 1;
  126. AStepCount := FElapsedMsAccumulator div timeGrain;
  127. FElapsedMsAccumulator:= FElapsedMsAccumulator mod timeGrain;
  128. FPrevDate := curDate;
  129. end;
  130. if AStepCount < 0 then AStepCount := 0
  131. else if AStepCount > 255 then AStepCount := 255;
  132. case FMode of
  133. fmFadeIn, fmFadeInOut, fmFadeInCycle:
  134. begin
  135. if (FAlpha = 255) and (FMode = fmFadeInCycle) then
  136. FAlpha := 0
  137. else
  138. if FAlpha + alphaStep*AStepCount >= 255 then
  139. begin
  140. FAlpha := 255;
  141. if FMode = fmFadeInOut then
  142. FMode := fmFadeOutIn
  143. else if FMode <> fmFadeInCycle then
  144. FMode := fmSuspended;
  145. end
  146. else
  147. FAlpha := FAlpha + (alphaStep*AStepCount);
  148. end;
  149. fmFadeOut,fmFadeOutIn, fmFadeOutCycle:
  150. begin
  151. if (FAlpha = 0) and (FMode = fmFadeOutCycle) then
  152. FAlpha := 255
  153. else
  154. if FAlpha - alphaStep*AStepCount <= 0 then
  155. begin
  156. FAlpha := 0;
  157. if FMode = fmFadeOutIn then
  158. FMode := fmFadeInOut
  159. else if FMode <> fmFadeOutCycle then
  160. FMode := fmSuspended;
  161. end
  162. else
  163. FAlpha := FAlpha - (alphaStep*AStepCount);
  164. end;
  165. end;
  166. Result := FAlpha;
  167. end;
  168. function TFading.Reset: byte;
  169. begin
  170. case FMode of
  171. fmFadeIn, fmFadeInOut:
  172. begin
  173. FAlpha := 0;
  174. end;
  175. fmFadeOut,fmFadeOutIn:
  176. begin
  177. FAlpha := 255;
  178. end;
  179. end;
  180. Result := FAlpha;
  181. FPrevDate := 0;
  182. end;
  183. procedure TFading.PutImage(ADestination: TBGRACustomBitmap; AX, AY: integer;
  184. ASource: TBGRACustomBitmap);
  185. begin
  186. ADestination.PutImage(AX,AY,ASource,dmDrawWithTransparency,Alpha);
  187. end;
  188. procedure TFading.FillRect(ADestination: TBGRACustomBitmap; ARect: TRect;
  189. AColor: TBGRAPixel);
  190. begin
  191. ADestination.FillRect(ARect, BGRA(AColor.red,AColor.green,AColor.blue,AColor.alpha*Alpha div 255),dmDrawWithTransparency);
  192. end;
  193. {-- Fading --}
  194. end.