Browse Source

Improved speed of Material design button

lainz 9 years ago
parent
commit
63101c5a61

+ 23 - 13
bcmaterialdesignbutton.pas

@@ -30,6 +30,7 @@ type
     FTextStyle: TFontStyles;
     FTextStyle: TFontStyles;
     FTimer: TTimer;
     FTimer: TTimer;
     FBGRA: TBGRABitmap;
     FBGRA: TBGRABitmap;
+    FBGRAShadow: TBGRABitmap;
     FMousePos: TPoint;
     FMousePos: TPoint;
     FCircleSize: single;
     FCircleSize: single;
     FCircleAlpha: byte;
     FCircleAlpha: byte;
@@ -54,6 +55,7 @@ type
       X, Y: integer); override;
       X, Y: integer); override;
     class function GetControlClassDefaultSize: TSize; override;
     class function GetControlClassDefaultSize: TSize; override;
     procedure TextChanged; override;
     procedure TextChanged; override;
+    procedure UpdateShadow;
   public
   public
     constructor Create(AOwner: TComponent); override;
     constructor Create(AOwner: TComponent); override;
     destructor Destroy; override;
     destructor Destroy; override;
@@ -139,6 +141,7 @@ begin
   if FShadowColor = AValue then
   if FShadowColor = AValue then
     Exit;
     Exit;
   FShadowColor := AValue;
   FShadowColor := AValue;
+  UpdateShadow;
   Invalidate;
   Invalidate;
 end;
 end;
 
 
@@ -147,6 +150,7 @@ begin
   if FShadowSize = AValue then
   if FShadowSize = AValue then
     Exit;
     Exit;
   FShadowSize := AValue;
   FShadowSize := AValue;
+  UpdateShadow;
   Invalidate;
   Invalidate;
 end;
 end;
 
 
@@ -171,6 +175,7 @@ begin
   if FTextShadowColor = AValue then
   if FTextShadowColor = AValue then
     Exit;
     Exit;
   FTextShadowColor := AValue;
   FTextShadowColor := AValue;
+  UpdateShadow;
   Invalidate;
   Invalidate;
 end;
 end;
 
 
@@ -250,22 +255,16 @@ var
   temp: TBGRABitmap;
   temp: TBGRABitmap;
 begin
 begin
   if (FBGRA.Width <> Width) or (FBGRA.Height <> Height) then
   if (FBGRA.Width <> Width) or (FBGRA.Height <> Height) then
+  begin
     FBGRA.SetSize(Width, Height);
     FBGRA.SetSize(Width, Height);
+    FBGRAShadow.SetSize(Width, Height);
+    UpdateShadow;
+  end;
 
 
-  { Shadow }
-  FBGRA.Fill(BGRAPixelTransparent);
-  FBGRA.RoundRectAntialias(FShadowSize, FShadowSize, Width - FShadowSize,
-    Height - FShadowSize, FRoundBorders, FRoundBorders,
-    ColorToBGRA(FShadowColor), 1, ColorToBGRA(FShadowColor), [rrDefault]);
-
-  temp := FBGRA.FilterBlurRadial(FShadowSize, FShadowSize, rbFast) as TBGRABitmap;
-  FBGRA.Fill(BGRAPixelTransparent);
-  FBGRA.PutImage(0, 0, temp, dmDrawWithTransparency);
-  temp.Free;
+  FBGRA.FillTransparent;
+  FBGRA.PutImage(0, 0, FBGRAShadow, dmDrawWithTransparency);
 
 
-  { Round Rectangle }
   temp := TBGRABitmap.Create(Width, Height, FNormalColor);
   temp := TBGRABitmap.Create(Width, Height, FNormalColor);
-  { Circle Effect }
   temp.EllipseAntialias(FMousePos.X, FMousePos.Y, FCircleSize, FCircleSize,
   temp.EllipseAntialias(FMousePos.X, FMousePos.Y, FCircleSize, FCircleSize,
     ColorToBGRA(FNormalColorEffect, FCircleAlpha), 1,
     ColorToBGRA(FNormalColorEffect, FCircleAlpha), 1,
     ColorToBGRA(FNormalColorEffect, FCircleAlpha));
     ColorToBGRA(FNormalColorEffect, FCircleAlpha));
@@ -273,7 +272,6 @@ begin
     FRoundBorders, FRoundBorders, temp, [rrDefault], False);
     FRoundBorders, FRoundBorders, temp, [rrDefault], False);
   temp.Free;
   temp.Free;
 
 
-  { Text }
   if Caption <> '' then
   if Caption <> '' then
   begin
   begin
     temp := TextShadow(Width, Height - FShadowSize, Caption, FTextSize,
     temp := TextShadow(Width, Height - FShadowSize, Caption, FTextSize,
@@ -307,6 +305,16 @@ begin
   Invalidate;
   Invalidate;
 end;
 end;
 
 
+procedure TBCMaterialDesignButton.UpdateShadow;
+begin
+  FBGRAShadow.FillTransparent;
+  FBGRAShadow.RoundRectAntialias(FShadowSize, FShadowSize, Width - FShadowSize,
+    Height - FShadowSize, FRoundBorders, FRoundBorders,
+    ColorToBGRA(FShadowColor), 1, ColorToBGRA(FShadowColor), [rrDefault]);
+  BGRAReplace(FBGRAShadow, FBGRAShadow.FilterBlurRadial(FShadowSize,
+    FShadowSize, rbFast) as TBGRABitmap);
+end;
+
 constructor TBCMaterialDesignButton.Create(AOwner: TComponent);
 constructor TBCMaterialDesignButton.Create(AOwner: TComponent);
 begin
 begin
   inherited Create(AOwner);
   inherited Create(AOwner);
@@ -318,6 +326,7 @@ begin
   FTimer.OnStartTimer := @OnStartTimer;
   FTimer.OnStartTimer := @OnStartTimer;
   FTimer.OnTimer := @OnTimer;
   FTimer.OnTimer := @OnTimer;
   FBGRA := TBGRABitmap.Create(Width, Height);
   FBGRA := TBGRABitmap.Create(Width, Height);
+  FBGRAShadow := TBGRABitmap.Create(Width, Height);
   FRoundBorders := 5;
   FRoundBorders := 5;
   FNormalColor := clWhite;
   FNormalColor := clWhite;
   FNormalColorEffect := clSilver;
   FNormalColorEffect := clSilver;
@@ -340,6 +349,7 @@ begin
   FTimer.OnStartTimer := nil;
   FTimer.OnStartTimer := nil;
   FTimer.OnTimer := nil;
   FTimer.OnTimer := nil;
   FreeAndNil(FBGRA);
   FreeAndNil(FBGRA);
+  FreeAndNil(FBGRAShadow);
   inherited Destroy;
   inherited Destroy;
 end;
 end;
 
 

+ 11 - 10
test/test_extra/material_design_animation/umain.lfm

@@ -1,11 +1,11 @@
 object Form1: TForm1
 object Form1: TForm1
   Left = 465
   Left = 465
-  Height = 240
+  Height = 343
   Top = 182
   Top = 182
-  Width = 320
+  Width = 538
   Caption = 'Form1'
   Caption = 'Form1'
-  ClientHeight = 240
-  ClientWidth = 320
+  ClientHeight = 343
+  ClientWidth = 538
   Color = clWhite
   Color = clWhite
   LCLVersion = '1.6.0.4'
   LCLVersion = '1.6.0.4'
   object Button1: TBGRAGraphicControl
   object Button1: TBGRAGraphicControl
@@ -13,7 +13,6 @@ object Form1: TForm1
     Height = 57
     Height = 57
     Top = 48
     Top = 48
     Width = 196
     Width = 196
-    Anchors = [akTop, akLeft, akRight, akBottom]
     OnRedraw = Button1Redraw
     OnRedraw = Button1Redraw
     Color = clWhite
     Color = clWhite
     ColorOpacity = 128
     ColorOpacity = 128
@@ -22,20 +21,21 @@ object Form1: TForm1
   end
   end
   object BCMaterialDesignButton1: TBCMaterialDesignButton
   object BCMaterialDesignButton1: TBCMaterialDesignButton
     Left = 48
     Left = 48
-    Height = 49
+    Height = 152
     Top = 112
     Top = 112
-    Width = 196
+    Width = 414
     TextColor = 8404992
     TextColor = 8404992
     TextSize = 22
     TextSize = 22
     TextStyle = [fsItalic]
     TextStyle = [fsItalic]
     TextFont = 'default'
     TextFont = 'default'
+    Anchors = [akTop, akLeft, akRight, akBottom]
     Caption = 'Material Design'
     Caption = 'Material Design'
     OnClick = BCMaterialDesignButton1Click
     OnClick = BCMaterialDesignButton1Click
   end
   end
   object BCMaterialDesignButton2: TBCMaterialDesignButton
   object BCMaterialDesignButton2: TBCMaterialDesignButton
-    Left = 240
-    Height = 58
-    Top = 176
+    Left = 458
+    Height = 53
+    Top = 284
     Width = 66
     Width = 66
     RoundBorders = 25
     RoundBorders = 25
     NormalColor = 8404992
     NormalColor = 8404992
@@ -45,6 +45,7 @@ object Form1: TForm1
     TextColor = clWhite
     TextColor = clWhite
     TextSize = 30
     TextSize = 30
     TextFont = 'default'
     TextFont = 'default'
+    Anchors = [akRight, akBottom]
     Caption = '@'
     Caption = '@'
   end
   end
   object Timer1: TTimer
   object Timer1: TTimer

+ 1 - 1
test/test_extra/material_design_animation/umain.pas

@@ -52,7 +52,7 @@ end;
 
 
 procedure TForm1.BCMaterialDesignButton1Click(Sender: TObject);
 procedure TForm1.BCMaterialDesignButton1Click(Sender: TObject);
 begin
 begin
-  ShowMessage('Click');
+  //ShowMessage('Click');
 end;
 end;
 
 
 procedure TForm1.Button1Redraw(Sender: TObject; Bitmap: TBGRABitmap);
 procedure TForm1.Button1Redraw(Sender: TObject; Bitmap: TBGRABitmap);