unit FMXTee.Editor.Brush;

interface

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

type
  TBrushEditor = class(TForm)
    TabControl1: TTabControl;
    TabColor: TTabItem;
    TabGradient: TTabItem;
    TabBitmap: TTabItem;
    Panel1: TPanel;
    Button1: TButton;
    Button2: TButton;
    ColorPanel1: TColorPanel;
    procedure ColorPanel1Change(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure TabControl1Change(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    FBrush : TChartBrush;
    IBack  : TChartBrush;

    FPictureEditor,
    FGradientEditor : TForm;
  public
    { Public declarations }
    class function Edit(AOwner:TComponent; ABrush:TChartBrush):Boolean;
    class function InsertForm(const ABrush:TChartBrush; const AParent:TControl):TBrushEditor;
    procedure RefreshBrush(const ABrush:TChartBrush);
  end;

implementation

{$R *.fmx}

uses
  FMXTee.Editor.Picture, FMXTee.Editor.Gradient, FMXTee.Editor.Stroke;

{ TBrushEditor }

procedure TBrushEditor.Button1Click(Sender: TObject);
begin
  ModalResult:=mrOk;
end;

procedure TBrushEditor.Button2Click(Sender: TObject);
begin
  FBrush.Assign(IBack);
  ModalResult:=mrCancel;
end;

procedure TBrushEditor.ColorPanel1Change(Sender: TObject);
begin
  if Assigned(FBrush) then
     FBrush.Color:=ColorPanel1.Color;
end;

class function TBrushEditor.InsertForm(const ABrush:TChartBrush; const AParent:TControl):TBrushEditor;
begin
  result:=TBrushEditor.Create(AParent.Owner);
  result.RefreshBrush(ABrush);
  result.Panel1.Visible:=False;
  EmbeddForm(result,AParent);
end;

procedure TBrushEditor.RefreshBrush(const ABrush:TChartBrush);
begin
  FBrush:=ABrush;
  IBack.Assign(FBrush);
  ColorPanel1.Color:=FBrush.Color;

//  if Assigned(FPictureEditor) then
//     FPictureEditor.RefreshPicture(FBrush.Image);

//  if Assigned(FGradientEditor) then
//     FGradientEditor.RefreshGradient(FBrush.Gradient);

  if FBrush.Gradient.Visible then
     TabControl1.ActiveTab:=TabGradient;
end;

class function TBrushEditor.Edit(AOwner: TComponent;
  ABrush: TChartBrush): Boolean;
begin
  with TBrushEditor.Create(AOwner) do
  try
    RefreshBrush(ABrush);
    result:=ShowModal=mrOk;
  finally
    Free;
  end;
end;

procedure TBrushEditor.FormCreate(Sender: TObject);
begin
  IBack:=TChartBrush.Create;
end;

procedure TBrushEditor.FormDestroy(Sender: TObject);
begin
  IBack.Free;
end;

procedure TBrushEditor.TabControl1Change(Sender: TObject);
begin
  if (TabControl1.ActiveTab=TabGradient) and (not Assigned(FGradientEditor)) then
     FGradientEditor:=TGradientEditor.CreateEmbedd(Self, FBrush.Gradient, TabGradient)
  else
  if (TabControl1.ActiveTab=TabBitmap) and (not Assigned(FPictureEditor)) then
     FPictureEditor:=TBackImageEditor.CreateEmbedd(Self,FBrush.Image,TabBitmap);
end;

end.
