unit FMXTee.Editor.CustomShape;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls,
  FMX.Forms, FMX.Edit, FMX.ListBox, FMX.Dialogs, FMX.TabControl, FMXTee.Procs,
  FMXTee.Editor.Font;

type
  TTeeShapeEditor = class(TForm)
    PC1: TTabControl;
    TabFormat: TTabItem;
    Label4: TLabel;
    BBackColor: TButton;
    Button6: TButton;
    CBTransparent: TCheckBox;
    UDTransp: TSpinBox;
    TabBorder: TTabItem;
    Label2: TLabel;
    Label3: TLabel;
    CBBevel: TComboBox;
    UDBevW: TSpinBox;
    GroupBox1: TGroupBox;
    Label1: TLabel;
    CBRound: TCheckBox;
    UDRoundSize: TSpinBox;
    Button4: TButton;
    TabText: TTabItem;
    TabGradient: TTabItem;
    TabShadow: TTabItem;
    TabEmboss: TTabItem;
    TabImage: TTabItem;
    procedure PC1Change(Sender: TObject);
    procedure BBrushClick(Sender: TObject);
    procedure CBBevelChange(Sender: TObject);
    procedure EBevWidthChange(Sender: TObject);
    procedure ERoundSizeChange(Sender: TObject);
    procedure CBTransparentChange(Sender: TObject);
    procedure CBRoundChange(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure UDBevWChangeTracking(Sender: TObject);
    procedure UDRoundSizeChangeTracking(Sender: TObject);
    procedure BBackColorClick(Sender: TObject);
    procedure UDTranspChangeTracking(Sender: TObject);
  private
   { Private declarations }
    Shape : TTeeCustomShape;

    FFontEditor : TFontEditor;

    FPictureEditor,
    FGradientEditor,
    FEmbossEditor,
    FShadowEditor : TForm;
  public
   { Public declarations }

    procedure CBVisibleChange(Sender: TObject);
    class function CreateEditor(AOwner:TComponent; AShape:TTeeCustomShape):TTeeShapeEditor;
    class function Edit(AOwner:TComponent; AShape:TTeeCustomShape):Boolean;
    class function Embedd(AOwner:TComponent; AParent:TFmxObject):TTeeShapeEditor;
    procedure RefreshShape(AShape:TTeeCustomShape);
  end;

implementation

{$R *.fmx}

uses
  FMXTee.Canvas, FMXTee.Editor.Shadow, FMXTee.Editor.Brush, FMXTee.Editor.Stroke,
  FMXTee.Editor.Gradient, FMXTee.Editor.Picture;

procedure TTeeShapeEditor.PC1Change(Sender: TObject);
var tmp : TTabItem;
begin
  tmp:=TTabControl(Sender).ActiveTab;

  if (tmp=TabText) and (not Assigned(FFontEditor)) then
     FFontEditor:=TFontEditor.Embedd(Self,Shape.Font,TabText)
  else
  if (tmp=TabShadow) and (not Assigned(FShadowEditor)) then
  begin
    FShadowEditor:=TShadowEditor.CreateEmbedd(Self,Shape.Shadow,TabShadow);
    // FMX Bug, avoid clash name with Emboss editor
    FShadowEditor.Name:=FShadowEditor.Name+'Shadow';
  end
  else
  if (tmp=TabGradient) and (not Assigned(FGradientEditor)) then
     FGradientEditor:=TGradientEditor.CreateEmbedd(Self,Shape.Gradient,TabGradient)
  else
  if (tmp=TabEmboss) and (not Assigned(FEmbossEditor)) then
  begin
    FEmbossEditor:=TShadowEditor.CreateEmbedd(Self,Shape.Emboss,TabEmboss);

    // FMX Bug, avoid clash name with Shadow editor
    FEmbossEditor.Name:=FEmbossEditor.Name+'Emboss';
  end
  else
  if (tmp=TabImage) and (not Assigned(FPictureEditor)) then
     FPictureEditor:=TBackImageEditor.CreateEmbedd(Self,Shape.Picture,TabImage);
end;

type
  TShapeAccess=class(TTeeCustomShape);

procedure TTeeShapeEditor.RefreshShape(AShape: TTeeCustomShape);
begin
  Shape:=AShape;

  CBTransparent.IsChecked:=Shape.Transparent;
  UDTransp.Value:=TShapeAccess(Shape).Transparency;
  CBRound.IsChecked:=(Shape.ShapeStyle=fosRoundRectangle);
  UDRoundSize.Value:=Shape.RoundSize;

  CBBevel.ItemIndex:=Ord(Shape.Bevel);

  if Assigned(FFontEditor) then
     FFontEditor.RefreshFont(Shape.Font);
end;

procedure TTeeShapeEditor.UDBevWChangeTracking(Sender: TObject);
begin
  if Assigned(Shape) then
     Shape.BevelWidth:=TBevelWidth(Round(UDBevW.Value));
end;

procedure TTeeShapeEditor.UDRoundSizeChangeTracking(Sender: TObject);
begin
  if Assigned(Shape) then
     Shape.RoundSize:=Round(UDRoundSize.Value);
end;

procedure TTeeShapeEditor.UDTranspChangeTracking(Sender: TObject);
begin
  if Assigned(Shape) then
     TShapeAccess(Shape).Transparency:=Round(UDTransp.Value);
end;

procedure TTeeShapeEditor.BBackColorClick(Sender: TObject);
begin
  Shape.Color:=EditColor(Self, Shape.Color);
end;

procedure TTeeShapeEditor.BBrushClick(Sender: TObject);
begin
  TBrushEditor.Edit(Self, Shape.Brush);
end;

procedure TTeeShapeEditor.CBTransparentChange(Sender: TObject);
begin
  Shape.Transparent:=CBTransparent.IsChecked;
end;

procedure TTeeShapeEditor.CBVisibleChange(Sender: TObject);
begin
  Shape.Visible:=TCheckBox(Sender).IsChecked;
end;

class function TTeeShapeEditor.CreateEditor(AOwner: TComponent;
  AShape: TTeeCustomShape): TTeeShapeEditor;
begin
  result:=TTeeShapeEditor.Create(AOwner);
  result.RefreshShape(AShape);
end;

procedure TTeeShapeEditor.Button4Click(Sender: TObject);
begin
  TStrokeEditor.Edit(Self, Shape.Pen);
end;

procedure TTeeShapeEditor.CBBevelChange(Sender: TObject);
begin
  Shape.Bevel:=TPanelBevel(CBBevel.ItemIndex);
end;

procedure TTeeShapeEditor.EBevWidthChange(Sender: TObject);
begin
// TODO
end;

class function TTeeShapeEditor.Edit(AOwner: TComponent;
  AShape: TTeeCustomShape): Boolean;
begin
  with TTeeShapeEditor.CreateEditor(AOwner,AShape) do
  try
    result:=ShowModal=mrOk;
  finally
    Free;
  end;
end;

class function TTeeShapeEditor.Embedd(AOwner: TComponent;
                                      AParent: TFmxObject): TTeeShapeEditor;
begin
  result:=TTeeShapeEditor.Create(AOwner);

  while result.PC1.TabCount>0 do
        result.PC1.Tabs[0].Parent:=AParent;
end;

procedure TTeeShapeEditor.CBRoundChange(Sender: TObject);
begin
  if CBRound.IsChecked then
     Shape.ShapeStyle:=fosRoundRectangle
  else
     Shape.ShapeStyle:=fosRectangle;
end;

procedure TTeeShapeEditor.ERoundSizeChange(Sender: TObject);
begin
// TODO
end;

end.
