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

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls,
  {$IFNDEF UPTOXE3}
  FMX.StdCtrls,
  {$ENDIF}
  FMX.Forms, FMX.Dialogs, FMX.TabControl, FMX.Colors, FMXTee.Canvas,
  FMXTee.Editor.Picture, FMXTee.Editor.Gradient, FMX.Layouts,
  FMXTee.Chart.GalleryPanel, FMX.Objects;

type
  TBrushEditor = class(TForm)
    TabControl1: TTabControl;
    TabColor: TTabItem;
    TabGradient: TTabItem;
    TabBitmap: TTabItem;
    PanelOk: TPanel;
    ColorPanel1: TColorPanel;
    TabTexture: TTabItem;
    TextureGallery: TChartGalleryPanel;
    Image1: TImage;
    Image2: TImage;
    Image3: TImage;
    Image4: TImage;
    LButtons: TLayout;
    Button1: TButton;
    Button2: TButton;
    procedure ColorPanel1Change(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure TabControl1Change(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure TextureGallerySelectedChart(Sender: TObject);
    procedure TextureGalleryChangeChart(Sender: TObject);
  private
    { Private declarations }
    FBrush : TTeeBrush;
    IBack  : TTeeBrush;

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

implementation

{$R *.fmx}

uses
  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;

procedure TBrushEditor.RefreshBrush(const ABrush:TTeeBrush);
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(const AOwner: TComponent;
  const ABrush: TTeeBrush): Boolean;
begin
  with TBrushEditor.Create(AOwner) do
  try
    RefreshBrush(ABrush);

    {$IFDEF ANDROID}
    ShowModal(procedure(Value:TModalResult)
    begin
      //result:=Value=mrOk;
    end);

    result:=False;
    {$ELSE}
    result:=ShowModal=mrOk;
    {$ENDIF}
  finally
    Free;
  end;
end;

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

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

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

procedure TBrushEditor.TabControl1Change(Sender: TObject);

  procedure CreateDefaultTexture(const AImage:TImage; const ATitle:String);
  var tmp : TGalleryChart;
  begin
    tmp:=TGalleryChart.Create(nil);
    tmp.View3D:=False;
    tmp.Walls.Back.Transparent:=False;
    tmp.Axes.Hide;

    tmp.Walls.Back.Picture.Bitmap:=AImage.Bitmap;

    tmp.Title.Caption:=ATitle;

    TextureGallery.Charts.Add(tmp);
    tmp.Parent:=TextureGallery;
    TextureGallery.FinishAdd(tmp);
  end;

begin
  if (TabControl1.ActiveTab=TabGradient) and (not Assigned(FGradientEditor)) then
     FGradientEditor:=TGradientEditor.InsertForm(FBrush.Gradient, TabGradient)
  else
  if (TabControl1.ActiveTab=TabBitmap) and (not Assigned(FPictureEditor)) then
     FPictureEditor:=TBackImageEditor.InsertForm(FBrush.Image,TabBitmap)
  else
  if (TabControl1.ActiveTab=TabTexture) and (TextureGallery.Charts.Count=0) then
  begin
    CreateDefaultTexture(Image1,'Wood');
    CreateDefaultTexture(Image2,'Carbon 1');
    CreateDefaultTexture(Image3,'Carbon 2');
    CreateDefaultTexture(Image4,'Paper');

    TextureGallery.ResizeCharts;
  end
end;

procedure TBrushEditor.TextureGalleryChangeChart(Sender: TObject);
begin
  if Assigned(FBrush) then
  begin
    FBrush.Image.Graphic:=TextureGallery.SelectedChart.Walls.Back.Brush.Image.Graphic;

    if Assigned(FPictureEditor) then
       FPictureEditor.Image1.Bitmap:=FBrush.Image.Graphic;
  end;
end;

procedure TBrushEditor.TextureGallerySelectedChart(Sender: TObject);
begin
  if PanelOk.Visible then
     ModalResult:=mrOk;
end;

end.
