Browse Source

Added AlphaTest and AlphaTestValue in BCImageButton. Added LoadFromBitmapResource in BGRASpriteAnimation

lainz 7 years ago
parent
commit
e855c85d36

+ 65 - 7
bcimagebutton.pas

@@ -45,7 +45,7 @@ uses
   { BGRAControls }
   { BGRAControls }
   BCBaseCtrls, BCEffect,
   BCBaseCtrls, BCEffect,
   { BGRABitmap }
   { BGRABitmap }
-  BGRABitmap, BGRABitmapTypes, BGRASliceScaling;
+  BGRABitmap, BGRABitmapTypes, BGRASliceScaling, Dialogs;
 
 
 {off $DEFINE DEBUG}
 {off $DEFINE DEBUG}
 
 
@@ -75,6 +75,7 @@ type
     procedure DoMouseUp; virtual;
     procedure DoMouseUp; virtual;
     procedure DoMouseEnter; virtual;
     procedure DoMouseEnter; virtual;
     procedure DoMouseLeave; virtual;
     procedure DoMouseLeave; virtual;
+    procedure DoMouseMove(x, y: integer); virtual;
   protected
   protected
     procedure Click; override;
     procedure Click; override;
     procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
     procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
@@ -82,6 +83,7 @@ type
     procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: integer); override;
     procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: integer); override;
     procedure MouseEnter; override;
     procedure MouseEnter; override;
     procedure MouseLeave; override;
     procedure MouseLeave; override;
+    procedure MouseMove(Shift: TShiftState; X,Y: Integer); override;
   public
   public
     property ModalResult: TModalResult
     property ModalResult: TModalResult
       read FModalResult write FModalResult default mrNone;
       read FModalResult write FModalResult default mrNone;
@@ -228,6 +230,8 @@ type
 
 
   TBCCustomImageButton = class(TBCGraphicButton)
   TBCCustomImageButton = class(TBCGraphicButton)
   private
   private
+    FAlphaTest: boolean;
+    FAlphaTestValue: byte;
     { Private declarations }
     { Private declarations }
     {$IFDEF DEBUG}
     {$IFDEF DEBUG}
     FDrawCount: integer;
     FDrawCount: integer;
@@ -244,6 +248,9 @@ type
     FBitmapFile: string;
     FBitmapFile: string;
     FTextVisible: boolean;
     FTextVisible: boolean;
     FToggle: boolean;
     FToggle: boolean;
+    FMouse: TPoint;
+    procedure SetFAlphaTest(AValue: boolean);
+    procedure SetFAlphaTestValue(AValue: byte);
     procedure SetFAnimation(AValue: boolean);
     procedure SetFAnimation(AValue: boolean);
     procedure SetFBitmapFile(AValue: string);
     procedure SetFBitmapFile(AValue: string);
     procedure SetFBitmapOptions(AValue: TBCImageButtonSliceScalingOptions);
     procedure SetFBitmapOptions(AValue: TBCImageButtonSliceScalingOptions);
@@ -265,9 +272,13 @@ type
     procedure DoMouseUp; override;
     procedure DoMouseUp; override;
     procedure DoMouseEnter; override;
     procedure DoMouseEnter; override;
     procedure DoMouseLeave; override;
     procedure DoMouseLeave; override;
+    procedure DoMouseMove(x, y: integer); override;
     procedure Click; override;
     procedure Click; override;
   public
   public
     { Public declarations }
     { Public declarations }
+    property AlphaTest: boolean read FAlphaTest write SetFAlphaTest default True;
+    property AlphaTestValue: byte
+      read FAlphaTestValue write SetFAlphaTestValue default 255;
     property Toggle: boolean read FToggle write SetFToggle default False;
     property Toggle: boolean read FToggle write SetFToggle default False;
     property Pressed: boolean read FPressed write SetFPressed default False;
     property Pressed: boolean read FPressed write SetFPressed default False;
     //property State: TBCGraphicButtonState read FState;
     //property State: TBCGraphicButtonState read FState;
@@ -279,8 +290,8 @@ type
     constructor Create(AOwner: TComponent); override;
     constructor Create(AOwner: TComponent); override;
     destructor Destroy; override;
     destructor Destroy; override;
     { It loads the 'BitmapFile' }
     { It loads the 'BitmapFile' }
-    procedure LoadFromBitmapResource(Resource: string; ResourceType: PChar); overload;
-    procedure LoadFromBitmapResource(Resource: string); overload;
+    procedure LoadFromBitmapResource(const Resource: string; ResourceType: PChar); overload;
+    procedure LoadFromBitmapResource(const Resource: string); overload;
     procedure LoadFromBitmapFile;
     procedure LoadFromBitmapFile;
     procedure Assign(Source: TPersistent); override;
     procedure Assign(Source: TPersistent); override;
     { Streaming }
     { Streaming }
@@ -295,6 +306,8 @@ type
 
 
   TBCImageButton = class(TBCCustomImageButton)
   TBCImageButton = class(TBCCustomImageButton)
   published
   published
+    property AlphaTest;
+    property AlphaTestValue;
     property Action;
     property Action;
     property Align;
     property Align;
     property Anchors;
     property Anchors;
@@ -853,6 +866,11 @@ begin
   end;
   end;
 end;
 end;
 
 
+procedure TBCGraphicButton.DoMouseMove(x, y: integer);
+begin
+  inherited;
+end;
+
 procedure TBCGraphicButton.Click;
 procedure TBCGraphicButton.Click;
 begin
 begin
   DoClick;
   DoClick;
@@ -886,6 +904,12 @@ begin
   DoMouseLeave;
   DoMouseLeave;
 end;
 end;
 
 
+procedure TBCGraphicButton.MouseMove(Shift: TShiftState; X, Y: Integer);
+begin
+  inherited MouseMove(Shift, X, Y);
+  DoMouseMove(X, Y);
+end;
+
 { TBCCustomImageButton }
 { TBCCustomImageButton }
 
 
 procedure TBCCustomImageButton.Fade(Sender: TObject);
 procedure TBCCustomImageButton.Fade(Sender: TObject);
@@ -933,10 +957,25 @@ begin
     Exit;
     Exit;
   FAnimation := AValue;
   FAnimation := AValue;
 
 
-  if csDesigning in ComponentState then Exit;
+  if csDesigning in ComponentState then
+    Exit;
   FTimer.Enabled := FAnimation;
   FTimer.Enabled := FAnimation;
 end;
 end;
 
 
+procedure TBCCustomImageButton.SetFAlphaTest(AValue: boolean);
+begin
+  if FAlphaTest = AValue then
+    Exit;
+  FAlphaTest := AValue;
+end;
+
+procedure TBCCustomImageButton.SetFAlphaTestValue(AValue: byte);
+begin
+  if FAlphaTestValue = AValue then
+    Exit;
+  FAlphaTestValue := AValue;
+end;
+
 procedure TBCCustomImageButton.SetFBitmapFile(AValue: string);
 procedure TBCCustomImageButton.SetFBitmapFile(AValue: string);
 begin
 begin
   if FBitmapFile = AValue then
   if FBitmapFile = AValue then
@@ -1107,7 +1146,8 @@ begin
       dmSet);
       dmSet);
     FBGRAActive.Rectangle(0, 0, Width, Height, BGRA(0, 84, 153), BGRA(204, 228, 247),
     FBGRAActive.Rectangle(0, 0, Width, Height, BGRA(0, 84, 153), BGRA(204, 228, 247),
       dmSet);
       dmSet);
-    FBGRADisabled.Rectangle(0, 0, Width, Height, BGRA(191, 191, 191), BGRA(204, 204, 204),
+    FBGRADisabled.Rectangle(0, 0, Width, Height, BGRA(191, 191, 191),
+      BGRA(204, 204, 204),
       dmSet);
       dmSet);
 
 
     { Draw Text }
     { Draw Text }
@@ -1156,6 +1196,8 @@ end;
 
 
 procedure TBCCustomImageButton.DoMouseDown;
 procedure TBCCustomImageButton.DoMouseDown;
 begin
 begin
+  if FAlphaTest and (FBGRANormal.GetPixel(FMouse.X, FMouse.Y).alpha < FAlphaTestValue) then
+    Exit;
   FFade.Mode := fmFadeOut;
   FFade.Mode := fmFadeOut;
 
 
   if Animation then
   if Animation then
@@ -1170,6 +1212,8 @@ procedure TBCCustomImageButton.DoMouseUp;
 var
 var
   Ctrl: TControl;
   Ctrl: TControl;
 begin
 begin
+  if FAlphaTest and (FBGRANormal.GetPixel(FMouse.X, FMouse.Y).alpha < FAlphaTestValue) then
+    Exit;
   FFade.Mode := fmFadeIn;
   FFade.Mode := fmFadeIn;
 
 
   if Animation then
   if Animation then
@@ -1210,8 +1254,20 @@ begin
   inherited DoMouseLeave;
   inherited DoMouseLeave;
 end;
 end;
 
 
+procedure TBCCustomImageButton.DoMouseMove(x, y: integer);
+begin
+  FMouse := Point(X, Y);
+  if FAlphaTest then
+    if FBGRANormal.GetPixel(X, Y).alpha >= FAlphaTestValue then
+      DoMouseEnter
+    else
+      DoMouseLeave;
+end;
+
 procedure TBCCustomImageButton.Click;
 procedure TBCCustomImageButton.Click;
 begin
 begin
+  if FAlphaTest and (FBGRANormal.GetPixel(FMouse.X, FMouse.Y).alpha < FAlphaTestValue) then
+    Exit;
   inherited Click;
   inherited Click;
   if (Toggle) then
   if (Toggle) then
   begin
   begin
@@ -1241,6 +1297,8 @@ begin
     FBitmapOptions.Bitmap.SetPixel(0,2,BGRA(0,0,255,255));
     FBitmapOptions.Bitmap.SetPixel(0,2,BGRA(0,0,255,255));
     FBitmapOptions.Bitmap.SetPixel(0,3,BGRA(100,100,100,255));}
     FBitmapOptions.Bitmap.SetPixel(0,3,BGRA(100,100,100,255));}
 
 
+    FAlphaTest := True;
+    FAlphaTestValue := 255;
     FFade.Step := 15;
     FFade.Step := 15;
     FFade.Mode := fmFadeOut;
     FFade.Mode := fmFadeOut;
     FTimer := TTimer.Create(Self);
     FTimer := TTimer.Create(Self);
@@ -1277,7 +1335,7 @@ begin
   inherited Destroy;
   inherited Destroy;
 end;
 end;
 
 
-procedure TBCCustomImageButton.LoadFromBitmapResource(Resource: string;
+procedure TBCCustomImageButton.LoadFromBitmapResource(const Resource: string;
   ResourceType: PChar);
   ResourceType: PChar);
 var
 var
   res: TResourceStream;
   res: TResourceStream;
@@ -1291,7 +1349,7 @@ begin
   res.Free;
   res.Free;
 end;
 end;
 
 
-procedure TBCCustomImageButton.LoadFromBitmapResource(Resource: string);
+procedure TBCCustomImageButton.LoadFromBitmapResource(const Resource: string);
 begin
 begin
   LoadFromBitmapResource(Resource, RT_RCDATA);
   LoadFromBitmapResource(Resource, RT_RCDATA);
 end;
 end;

+ 7 - 1
bgraspriteanimation.pas

@@ -113,7 +113,8 @@ type
   public
   public
     { Public declarations }
     { Public declarations }
     procedure GifImageToSprite(Gif: TBGRAAnimatedGif);//FreeMan35 added
     procedure GifImageToSprite(Gif: TBGRAAnimatedGif);//FreeMan35 added
-    procedure LoadFromResourceName(Instance: THandle; const ResName: string);
+    procedure LoadFromResourceName(Instance: THandle; const ResName: string); overload;
+    procedure LoadFromBitmapResource(const Resource: string); overload;
     //FreeMan35 added
     //FreeMan35 added
     procedure AnimatedGifToSprite(Filename: string);
     procedure AnimatedGifToSprite(Filename: string);
     constructor Create(AOwner: TComponent); override;
     constructor Create(AOwner: TComponent); override;
@@ -559,6 +560,11 @@ begin
   TempGif.Free;
   TempGif.Free;
 end;
 end;
 
 
+procedure TBGRASpriteAnimation.LoadFromBitmapResource(const Resource: string);
+begin
+  LoadFromResourceName(HInstance, Resource);
+end;
+
 procedure TBGRASpriteAnimation.AnimatedGifToSprite(Filename: string);
 procedure TBGRASpriteAnimation.AnimatedGifToSprite(Filename: string);
 var
 var
   TempGif: TBGRAAnimatedGif;
   TempGif: TBGRAAnimatedGif;

BIN
test/test_bcimagebutton_alpha/button.png


BIN
test/test_bcimagebutton_alpha/button2.png


BIN
test/test_bcimagebutton_alpha/test.ico


+ 86 - 0
test/test_bcimagebutton_alpha/test.lpi

@@ -0,0 +1,86 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<CONFIG>
+  <ProjectOptions>
+    <Version Value="11"/>
+    <PathDelim Value="\"/>
+    <General>
+      <SessionStorage Value="InProjectDir"/>
+      <MainUnit Value="0"/>
+      <Title Value="test"/>
+      <Scaled Value="True"/>
+      <ResourceType Value="res"/>
+      <UseXPManifest Value="True"/>
+      <XPManifest>
+        <DpiAware Value="True"/>
+      </XPManifest>
+      <Resources Count="3">
+        <Resource_0 FileName="button.png" Type="RCDATA" ResourceName="BUTTON"/>
+        <Resource_1 FileName="button2.png" Type="RCDATA" ResourceName="BUTTON2"/>
+        <Resource_2 FileName="title_bath3.gif" Type="RCDATA" ResourceName="TITLE_BATH3"/>
+      </Resources>
+    </General>
+    <BuildModes Count="1">
+      <Item1 Name="Default" Default="True"/>
+    </BuildModes>
+    <PublishOptions>
+      <Version Value="2"/>
+      <UseFileFilters Value="True"/>
+    </PublishOptions>
+    <RunParams>
+      <FormatVersion Value="2"/>
+      <Modes Count="0"/>
+    </RunParams>
+    <RequiredPackages Count="2">
+      <Item1>
+        <PackageName Value="bgracontrols"/>
+      </Item1>
+      <Item2>
+        <PackageName Value="LCL"/>
+      </Item2>
+    </RequiredPackages>
+    <Units Count="2">
+      <Unit0>
+        <Filename Value="test.lpr"/>
+        <IsPartOfProject Value="True"/>
+      </Unit0>
+      <Unit1>
+        <Filename Value="umain.pas"/>
+        <IsPartOfProject Value="True"/>
+        <ComponentName Value="Form1"/>
+        <HasResources Value="True"/>
+        <ResourceBaseClass Value="Form"/>
+      </Unit1>
+    </Units>
+  </ProjectOptions>
+  <CompilerOptions>
+    <Version Value="11"/>
+    <PathDelim Value="\"/>
+    <Target>
+      <Filename Value="test"/>
+    </Target>
+    <SearchPaths>
+      <IncludeFiles Value="$(ProjOutDir)"/>
+      <UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/>
+    </SearchPaths>
+    <Linking>
+      <Options>
+        <Win32>
+          <GraphicApplication Value="True"/>
+        </Win32>
+      </Options>
+    </Linking>
+  </CompilerOptions>
+  <Debugging>
+    <Exceptions Count="3">
+      <Item1>
+        <Name Value="EAbort"/>
+      </Item1>
+      <Item2>
+        <Name Value="ECodetoolError"/>
+      </Item2>
+      <Item3>
+        <Name Value="EFOpenError"/>
+      </Item3>
+    </Exceptions>
+  </Debugging>
+</CONFIG>

+ 22 - 0
test/test_bcimagebutton_alpha/test.lpr

@@ -0,0 +1,22 @@
+program test;
+
+{$mode objfpc}{$H+}
+
+uses
+  {$IFDEF UNIX}{$IFDEF UseCThreads}
+  cthreads,
+  {$ENDIF}{$ENDIF}
+  Interfaces, // this includes the LCL widgetset
+  Forms, umain
+  { you can add units after this };
+
+{$R *.res}
+
+begin
+  RequireDerivedFormResource:=True;
+  Application.Scaled:=True;
+  Application.Initialize;
+  Application.CreateForm(TForm1, Form1);
+  Application.Run;
+end.
+

BIN
test/test_bcimagebutton_alpha/title_bath3.gif


+ 1 - 0
test/test_bcimagebutton_alpha/title_bath3.txt

@@ -0,0 +1 @@
+https://polycount.com/discussion/comment/1966572/#Comment_1966572

+ 66 - 0
test/test_bcimagebutton_alpha/umain.lfm

@@ -0,0 +1,66 @@
+object Form1: TForm1
+  Left = 397
+  Height = 240
+  Top = 119
+  Width = 320
+  Caption = 'My Epic Game Title Screen'
+  ClientHeight = 240
+  ClientWidth = 320
+  OnCreate = FormCreate
+  LCLVersion = '2.1.0.0'
+  object BGRASpriteAnimation1: TBGRASpriteAnimation
+    Left = 0
+    Height = 240
+    Top = 0
+    Width = 320
+    AnimInvert = False
+    AnimPosition = 1
+    AnimRepeat = 0
+    AnimRepeatLap = 0
+    AnimSpeed = 1000
+    AnimStatic = False
+    AutoSize = False
+    Center = True
+    Proportional = True
+    SpriteCount = 1
+    SpriteFillOpacity = 255
+    SpriteFlipMode = flNone
+    SpriteKeyColor = clNone
+    SpriteResampleFilter = rfLinear
+    SpriteResampleMode = rmSimpleStretch
+    SpriteRotation = rtNone
+    Stretch = True
+    Tile = False
+    Align = alClient
+    Caption = 'BGRASpriteAnimation1'
+  end
+  object BCImageButton1: TBCImageButton
+    Left = 184
+    Height = 128
+    Top = 104
+    Width = 128
+    Anchors = [akRight, akBottom]
+    Animation = False
+    BitmapFile = 'BUTTON'
+    BitmapOptions.Direction = sdVertical
+    Caption = 'Start'
+    Font.Color = clWhite
+    Font.Height = 30
+    OnMouseMove = BCImageButton1MouseMove
+    ParentFont = False
+  end
+  object BCImageButton2: TBCImageButton
+    Left = 48
+    Height = 128
+    Top = 8
+    Width = 128
+    AlphaTestValue = 100
+    Animation = False
+    BitmapFile = 'BUTTON2'
+    BitmapOptions.Direction = sdVertical
+    Font.Color = clWhite
+    Font.Height = 30
+    OnMouseMove = BCImageButton1MouseMove
+    ParentFont = False
+  end
+end

+ 51 - 0
test/test_bcimagebutton_alpha/umain.pas

@@ -0,0 +1,51 @@
+unit umain;
+
+{$mode objfpc}{$H+}
+
+interface
+
+uses
+  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls,
+  BCImageButton, BGRASpriteAnimation;
+
+type
+
+  { TForm1 }
+
+  TForm1 = class(TForm)
+    BCImageButton1: TBCImageButton;
+    BCImageButton2: TBCImageButton;
+    BGRASpriteAnimation1: TBGRASpriteAnimation;
+    procedure BCImageButton1MouseMove(Sender: TObject; Shift: TShiftState; X,
+      Y: Integer);
+    procedure FormCreate(Sender: TObject);
+  private
+
+  public
+
+  end;
+
+var
+  Form1: TForm1;
+
+implementation
+
+{$R *.lfm}
+
+{ TForm1 }
+
+procedure TForm1.FormCreate(Sender: TObject);
+begin
+  BCImageButton1.LoadFromBitmapResource(BCImageButton1.BitmapFile);
+  BCImageButton2.LoadFromBitmapResource(BCImageButton2.BitmapFile);
+  BGRASpriteAnimation1.LoadFromBitmapResource('TITLE_BATH3');
+end;
+
+procedure TForm1.BCImageButton1MouseMove(Sender: TObject; Shift: TShiftState;
+  X, Y: Integer);
+begin
+
+end;
+
+end.
+