unit FMXTee.Editor.Gradient;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes,
  System.Variants, FMX.Types, FMX.Controls, FMX.Forms,
  FMX.Dialogs, FMX.ListBox, FMX.TabControl, FMXTee.Canvas, FMX.Colors;

type
  TGradientEditor = class(TForm)
    Panel1: TPanel;
    CBVisible: TCheckBox;
    Label1: TLabel;
    CBDirection: TComboBox;
    ColorButton1: TComboColorBox;
    ColorButton2: TComboColorBox;
    ColorButton3: TComboColorBox;
    CBUseMiddle: TCheckBox;
    ListBoxItem1: TListBoxItem;
    ListBoxItem2: TListBoxItem;
    ListBoxItem3: TListBoxItem;
    ListBoxItem4: TListBoxItem;
    ListBoxItem5: TListBoxItem;
    ListBoxItem6: TListBoxItem;
    ListBoxItem7: TListBoxItem;
    ListBoxItem8: TListBoxItem;
    ListBoxItem9: TListBoxItem;
    ListBoxItem10: TListBoxItem;
    Button1: TButton;
    procedure CBVisibleChange(Sender: TObject);
    procedure ColorButton1Change(Sender: TObject);
    procedure ColorButton2Change(Sender: TObject);
    procedure ColorButton3Change(Sender: TObject);
    procedure CBDirectionChange(Sender: TObject);
    procedure CBUseMiddleChange(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    Gradient : TCustomTeeGradient;

    procedure RefreshGradient(AGradient:TCustomTeeGradient);
  public
    { Public declarations }
    class function CreateEmbedd(AOwner:TComponent; AGradient:TCustomTeeGradient;
                                AParent:TFmxObject):TForm;
    class function Edit(AOwner:TComponent; AGradient:TCustomTeeGradient):Boolean;
  end;

implementation

{$R *.fmx}

uses
  FMXTee.Editor.Stroke;

procedure TGradientEditor.Button1Click(Sender: TObject);
var tmp : TColor;
begin
  tmp:=Gradient.StartColor;
  Gradient.StartColor:=Gradient.EndColor;
  Gradient.EndColor:=tmp;

  ColorButton1.Color:=Gradient.StartColor;
  ColorButton3.Color:=Gradient.EndColor;
end;

procedure TGradientEditor.CBDirectionChange(Sender: TObject);
begin
  Gradient.Direction:=TGradientDirection(CBDirection.ItemIndex);
end;

procedure TGradientEditor.CBUseMiddleChange(Sender: TObject);
begin
  if CBUseMiddle.IsChecked then
     Gradient.UseMiddleColor
  else
     Gradient.MidColor:=clNone;

  if Assigned(Gradient.Changed) then
     Gradient.Changed(Self);
end;

procedure TGradientEditor.CBVisibleChange(Sender: TObject);
begin
  Gradient.Visible:=CBVisible.IsChecked;
end;

procedure TGradientEditor.RefreshGradient(AGradient: TCustomTeeGradient);
begin
  Gradient:=AGradient;

  CBVisible.IsChecked:=Gradient.Visible;
  ColorButton1.Color:=Gradient.StartColor;
  ColorButton2.Color:=Gradient.MidColor;
  ColorButton3.Color:=Gradient.EndColor;
  CBUseMiddle.IsChecked:=Gradient.MidColor<>clNone;
  CBDirection.ItemIndex:=Ord(Gradient.Direction);
end;

procedure TGradientEditor.ColorButton1Change(Sender: TObject);
begin
  Gradient.StartColor:=ColorButton1.Color;
end;

procedure TGradientEditor.ColorButton2Change(Sender: TObject);
begin
  Gradient.MidColor:=ColorButton2.Color;
end;

procedure TGradientEditor.ColorButton3Change(Sender: TObject);
begin
  Gradient.EndColor:=ColorButton3.Color;
end;

class function TGradientEditor.CreateEmbedd(AOwner: TComponent;
  AGradient: TCustomTeeGradient; AParent: TFmxObject): TForm;
begin
  result:=TGradientEditor.Create(AOwner);
  TGradientEditor(result).RefreshGradient(AGradient);
  EmbeddForm(result,AParent);
end;

class function TGradientEditor.Edit(AOwner: TComponent;
  AGradient: TCustomTeeGradient): Boolean;
begin
  with TGradientEditor.Create(AOwner) do
  try
    RefreshGradient(AGradient);
    result:=ShowModal=mrOk;
  finally
    Free;
  end;
end;

end.
