Skip to content

Commit

Permalink
Added fxmaterialcolors.pas
Browse files Browse the repository at this point in the history
  • Loading branch information
lainz committed Oct 23, 2016
1 parent 1060d81 commit 6845180
Show file tree
Hide file tree
Showing 6 changed files with 532 additions and 76 deletions.
9 changes: 7 additions & 2 deletions bgracontrolsfx.lpk
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</CodeGeneration>
</CompilerOptions>
<Version Minor="1"/>
<Files Count="3">
<Files Count="4">
<Item1>
<Filename Value="fxbutton.pas"/>
<HasRegisterProc Value="True"/>
Expand All @@ -30,10 +30,15 @@
<UnitName Value="fxcontainer"/>
</Item2>
<Item3>
<Filename Value="fxmaterialcolors.pas"/>
<AddToUsesPkgSection Value="False"/>
<UnitName Value="FXMaterialColors"/>
</Item3>
<Item4>
<Filename Value="fxmaterialdesignbutton.pas"/>
<HasRegisterProc Value="True"/>
<UnitName Value="FXMaterialDesignButton"/>
</Item3>
</Item4>
</Files>
<RequiredPkgs Count="4">
<Item1>
Expand Down
174 changes: 144 additions & 30 deletions fxbutton.pas
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ interface

uses
Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, Types,
FXContainer, BGRABitmap, BGRABitmapTypes, BGRAOpenGL;
FXContainer, BGRABitmap, BGRABitmapTypes, BGRAOpenGL, FXMaterialColors;

type

Expand All @@ -18,9 +18,21 @@ interface
TCustomFXButton = class(TGraphicControl, IFXDrawable)
private
FBGRA: TBGRABitmap;
FColorActive: TColor;
FColorDisabled: TColor;
FColorHover: TColor;
FColorKind: TMaterialColor;
FColorNormal: TColor;
FFontColorAutomatic: boolean;
FTexture: IBGLTexture;
FState: TFXButtonStates;
FNeedDraw: boolean;
procedure SetFColorActive(AValue: TColor);
procedure SetFColorDisabled(AValue: TColor);
procedure SetFColorHover(AValue: TColor);
procedure SetFColorKind(AValue: TMaterialColor);
procedure SetFColorNormal(AValue: TColor);
procedure SetFFontColorAutomatic(AValue: boolean);
protected
procedure CalculatePreferredSize(var PreferredWidth, PreferredHeight: integer;
{%H-}WithThemeSpace: boolean); override;
Expand All @@ -38,9 +50,17 @@ TCustomFXButton = class(TGraphicControl, IFXDrawable)
procedure FXPreview(var aCanvas: TCanvas);
procedure Draw;
procedure Paint; override;
function GetFillColor: TColor;
public
constructor Create(TheOwner: TComponent); override;
destructor Destroy; override;
property FontColorAutomatic: boolean read FFontColorAutomatic
write SetFFontColorAutomatic;
property ColorKind: TMaterialColor read FColorKind write SetFColorKind;
property ColorNormal: TColor read FColorNormal write SetFColorNormal;
property ColorHover: TColor read FColorHover write SetFColorHover;
property ColorActive: TColor read FColorActive write SetFColorActive;
property ColorDisabled: TColor read FColorDisabled write SetFColorDisabled;
end;

TFXButton = class(TCustomFXButton)
Expand Down Expand Up @@ -93,6 +113,66 @@ procedure Register;

{ TCustomFXButton }

procedure TCustomFXButton.SetFColorActive(AValue: TColor);
begin
if FColorActive = AValue then
Exit;
FColorActive := AValue;
FNeedDraw := True;
if not (csLoading in ComponentState) then
FXInvalidate;
end;

procedure TCustomFXButton.SetFColorDisabled(AValue: TColor);
begin
if FColorDisabled = AValue then
Exit;
FColorDisabled := AValue;
FNeedDraw := True;
if not (csLoading in ComponentState) then
FXInvalidate;
end;

procedure TCustomFXButton.SetFColorHover(AValue: TColor);
begin
if FColorHover = AValue then
Exit;
FColorHover := AValue;
FNeedDraw := True;
if not (csLoading in ComponentState) then
FXInvalidate;
end;

procedure TCustomFXButton.SetFColorKind(AValue: TMaterialColor);
begin
if FColorKind = AValue then
Exit;
FColorKind := AValue;
FNeedDraw := True;
if not (csLoading in ComponentState) then
FXInvalidate;
end;

procedure TCustomFXButton.SetFColorNormal(AValue: TColor);
begin
if FColorNormal = AValue then
Exit;
FColorNormal := AValue;
FNeedDraw := True;
if not (csLoading in ComponentState) then
FXInvalidate;
end;

procedure TCustomFXButton.SetFFontColorAutomatic(AValue: boolean);
begin
if FFontColorAutomatic = AValue then
Exit;
FFontColorAutomatic := AValue;
FNeedDraw := True;
if not (csLoading in ComponentState) then
FXInvalidate;
end;

procedure TCustomFXButton.CalculatePreferredSize(
var PreferredWidth, PreferredHeight: integer; WithThemeSpace: boolean);
begin
Expand Down Expand Up @@ -157,10 +237,10 @@ procedure TCustomFXButton.FXInvalidate;
Invalidate;

if Parent is TFXContainer then
begin
begin
if TFXContainer(Parent).ReceivePaintFrom = nil then
Parent.Invalidate;
end
end
else
Invalidate;
end;
Expand All @@ -185,6 +265,7 @@ procedure TCustomFXButton.FXPreview(var aCanvas: TCanvas);
procedure TCustomFXButton.Draw;
var
style: TTextStyle;
fill_color: TColor;
begin
if (Width <> FBGRA.Width) and (Height <> FBGRA.Height) then
begin
Expand All @@ -196,57 +277,90 @@ procedure TCustomFXButton.Draw;
begin
FBGRA.FillTransparent;

style.Alignment := taCenter;
style.Layout := tlCenter;
FBGRA.FontHeight := Font.GetTextHeight(Caption);
FBGRA.FontAntialias := True;

fill_color := GetFillColor;
FBGRA.Fill(fill_color);

if FontColorAutomatic then
FBGRA.TextRect(Rect(0, 0, Width, Height), 0, 0, Caption, style,
GetContrastColor(fill_color))
else
FBGRA.TextRect(Rect(0, 0, Width, Height), 0, 0, Caption, style, Font.Color);

FNeedDraw := False;
FTexture := nil;
end;
end;

procedure TCustomFXButton.Paint;
begin
if (Parent is TFXContainer) then
exit;
Draw;
FBGRA.Draw(Canvas, 0, 0, False);
end;

function TCustomFXButton.GetFillColor: TColor;
begin
if ColorKind = mcDefault then
begin
if Enabled then
begin
{ Button Down }
if fxbActive in FState then
begin
FBGRA.Fill(BGRA(50, 50, 50, 255));
end
Result := ColorActive
else
begin
{ Button Hovered }
if fxbHovered in FState then
begin
FBGRA.Fill(BGRA(200, 200, 200, 255));
end
Result := ColorHover
{ Button Normal }
else
begin
FBGRA.Fill(BGRA(125, 125, 125, 255));
end;
Result := ColorNormal;
end;
end
{ Button Disabled }
else
Result := ColorDisabled;
end
else
begin
if Enabled then
begin
FBGRA.Fill(BGRA(25, 25, 25, 255));
end;

style.Alignment := taCenter;
style.Layout := tlCenter;

FBGRA.TextRect(Rect(0, 0, Width, Height), 0, 0, Caption, style, Font.Color);

FNeedDraw := False;
FTexture := nil;
{ Button Down }
if fxbActive in FState then
Result := MaterialColorsList.KeyData[MaterialColorStr[ColorKind]].M100
else
begin
{ Button Hovered }
if fxbHovered in FState then
Result := MaterialColorsList.KeyData[MaterialColorStr[ColorKind]].M300
{ Button Normal }
else
Result := MaterialColorsList.KeyData[MaterialColorStr[ColorKind]].M500;
end;
end
{ Button Disabled }
else
Result := MaterialColorsList.KeyData[MaterialColorStr[ColorKind]].M900;
end;
end;

procedure TCustomFXButton.Paint;
begin
if (Parent is TFXContainer) then
exit;
Draw;
FBGRA.Draw(Canvas, 0, 0, False);
end;

constructor TCustomFXButton.Create(TheOwner: TComponent);
begin
inherited Create(TheOwner);
FBGRA := TBGRABitmap.Create;
with GetControlClassDefaultSize do
SetInitialBounds(0, 0, CX, CY);
FColorNormal := clWhite;
FColorHover := clSilver;
FColorActive := clMedGray;
FColorDisabled := clGray;
FFontColorAutomatic := True;
end;

destructor TCustomFXButton.Destroy;
Expand Down
Loading

0 comments on commit 6845180

Please sign in to comment.