{*********************************************}
{  TeeChart Charting Library for FireMonkey   }
{  Copyright (c) 2012-2020 by Steema Software }
{  All Rights Reserved                        }
{*********************************************}
unit FMXTee.Editor.Shadow;
{$I TeeDefs.inc}

interface

uses
  SysUtils, Types, System.UITypes, Classes, Variants,
  FMX.Types, FMX.Controls, FMX.Forms,
  {$IFNDEF UPTOXE3}
  FMX.StdCtrls,
  {$ENDIF}
  {$IFDEF D21}
  FMX.SpinBox,
  {$ENDIF}
  FMX.Edit, FMX.Dialogs, FMXTee.Canvas, FMX.ListBox, FMX.Colors, FMX.TabControl;

type
  TShadowEditor = class(TForm)
    PanelButtons: TPanel;
    BOK: TButton;
    TabControl1: TTabControl;
    TabFormat: TTabItem;
    TabOptions: TTabItem;
    CBSmooth: TCheckBox;
    Label2: TLabel;
    TBBlur: TTrackBar;
    LBlur: TLabel;
    CBClip: TCheckBox;
    CBVisible: TCheckBox;
    LTransp: TLabel;
    BShadowColor: TComboColorBox;
    TBTransp: TTrackBar;
    GroupBox1: TGroupBox;
    Label1: TLabel;
    Label5: TLabel;
    UDShadowSize: TSpinBox;
    UDShaVert: TSpinBox;
    TBSize: TTrackBar;
    Label3: TLabel;
    CBDirection: TComboBox;
    ListBoxItem1: TListBoxItem;
    ListBoxItem2: TListBoxItem;
    ListBoxItem3: TListBoxItem;
    ListBoxItem4: TListBoxItem;
    ListBoxItem5: TListBoxItem;
    procedure BOKClick(Sender: TObject);
    procedure TBBlurChange(Sender: TObject);
    procedure CBVisibleChange(Sender: TObject);
    procedure CBSmoothChange(Sender: TObject);
    procedure CBClipChange(Sender: TObject);
    procedure UDShaVertChange(Sender: TObject);
    procedure UDShadowSizeChange(Sender: TObject);
    procedure BShadowColorChange(Sender: TObject);
    procedure TBTranspChange(Sender: TObject);
    procedure TBSizeChange(Sender: TObject);
    procedure CBDirectionChange(Sender: TObject);
  private
   { Private declarations }

    IChanging : Boolean;
    Shadow : TTeeShadow;

    function CanChange:Boolean;
    procedure SetTrackBarSize;
  public
   { Public declarations }
    class function Edit(const AOwner:TComponent; const AShadow:TTeeShadow):Boolean;
    class function InsertForm(const AShadow:TTeeShadow; const AParent:TFmxObject):TShadowEditor;
    procedure RefreshShadow(const AShadow:TTeeShadow);
  end;

implementation

{$R *.fmx}

uses
  {$IFDEF D17}
  System.UIConsts,
  {$ENDIF}
  FMXTee.Procs, FMXTee.Editor.Stroke;

procedure TShadowEditor.RefreshShadow(const AShadow: TTeeShadow);
begin
  Shadow:=AShadow;

  IChanging:=True;

  CBClip.IsChecked:=Shadow.Clip;
  CBSmooth.IsChecked:=Shadow.Smooth;
  CBVisible.IsChecked:=Shadow.Visible;
  BShadowColor.Color:=Shadow.Color;
  UDShadowSize.Value:=Shadow.HorizSize;
  UDShaVert.Value:=Shadow.VertSize;

  CBDirection.ItemIndex:=Ord(Shadow.Direction);

  TBTransp.Value:=Shadow.Transparency;

  TBBlur.Value:=Shadow.SmoothBlur;
  LBlur.Text:=IntToStr(Shadow.SmoothBlur);

  SetTrackBarSize;

  IChanging:=False;
end;

procedure TShadowEditor.BOKClick(Sender: TObject);
begin
  ModalResult:=mrOk;
end;

procedure TShadowEditor.CBSmoothChange(Sender: TObject);
begin
  Shadow.Smooth:=CBSmooth.IsChecked;
end;

Function TShadowEditor.CanChange:Boolean;
begin
  result:=Assigned(Shadow);
end;

class function TShadowEditor.Edit(const AOwner: TComponent;
  const AShadow: TTeeShadow): Boolean;
begin
  with TShadowEditor.Create(AOwner) do
  try
    RefreshShadow(AShadow);
    result:=ShowModal=mrOk;
  finally
    Free;
  end;
end;

class function TShadowEditor.InsertForm(const AShadow: TTeeShadow;
  const AParent: TFmxObject): TShadowEditor;
begin
  result:=TShadowEditor.Create(AParent.Owner);
  //result.Name:=AOwner.ClassName+AParent.Name;
  result.PanelButtons.Visible:=False;

  result.RefreshShadow(AShadow);

  EmbeddForm(result, AParent);
end;

procedure TShadowEditor.CBVisibleChange(Sender: TObject);
begin
  Shadow.Visible:=CBVisible.IsChecked;
end;

procedure TShadowEditor.TBBlurChange(Sender: TObject);
begin
  Shadow.SmoothBlur:=Round(TBBlur.Value);
  LBlur.Text:=IntToStr(Shadow.SmoothBlur);
end;

procedure TShadowEditor.SetTrackBarSize;
var tmp : Integer;
begin
  tmp:=Abs(Shadow.HorizSize);

  if tmp>TBSize.Max then
  begin
    TBSize.Min:= -2*tmp;
    TBSize.Max:=  2*tmp;
    TBSize.Frequency:=Round((TBSize.Max-TBSize.Min)/12);
  end;

  IChanging:=True;
  TBSize.Value:=Shadow.HorizSize;
  IChanging:=False;
end;

procedure TShadowEditor.TBSizeChange(Sender: TObject);
begin
  if (not IChanging) and CanChange then
  begin
    Shadow.Size:=Round(TBSize.Value);

    UDShadowSize.Value:=Shadow.HorizSize;
    UDShaVert.Value:=Shadow.VertSize;
  end;
end;

procedure TShadowEditor.TBTranspChange(Sender: TObject);
begin
  Shadow.Transparency:=Round(TBTransp.Value);
end;

procedure TShadowEditor.UDShadowSizeChange(Sender: TObject);
begin
  if CanChange then
  begin
    Shadow.HorizSize:=Round(UDShadowSize.Value);
    SetTrackBarSize;
  end;
end;

procedure TShadowEditor.UDShaVertChange(Sender: TObject);
begin
  Shadow.VertSize:=Round(UDShaVert.Value);
end;

procedure TShadowEditor.BShadowColorChange(Sender: TObject);
begin
  if not IChanging then
     Shadow.Color:=BShadowColor.Color;
end;

procedure TShadowEditor.CBClipChange(Sender: TObject);
begin
  Shadow.Clip:=CBClip.IsChecked;
end;

procedure TShadowEditor.CBDirectionChange(Sender: TObject);
begin
  if (not IChanging) and CanChange then
     Shadow.Direction:=TShadowDirection(CBDirection.ItemIndex);
end;

end.
