{*********************************************}
{  TeeChart Charting Library for FireMonkey   }
{  Copyright (c) 2012-2020 by Steema Software }
{  All Rights Reserved                        }
{*********************************************}
unit FMXTee.Editor.CustomShape;
{$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.Edit, FMX.ListBox, FMX.Dialogs, FMX.TabControl, FMXTee.Procs,
  FMXTee.Editor.Font, FMX.Ani, FMX.Colors,
  FMXTee.Editor.Shadow, FMXTee.Editor.Brush, FMXTee.Editor.Stroke,
  {$IFDEF D21}
  FMX.ComboTrackBar, FMX.SpinBox,
  {$ENDIF}
  FMXTee.Editor.Gradient, FMXTee.Editor.Picture;

type
  TTeeShapeEditor = class(TForm)
    PC1: TTabControl;
    TabFormat: TTabItem;
    Label4: TLabel;
    CBTransparent: TCheckBox;
    TabBorder: TTabItem;
    Label2: TLabel;
    Label3: TLabel;
    CBBevel: TComboBox;
    UDBevW: TSpinBox;
    Label1: TLabel;
    CBRound: TCheckBox;
    UDRoundSize: TSpinBox;
    TabFont: TTabItem;
    TabPattern: TTabItem;
    TabShadow: TTabItem;
    TabEmboss: TTabItem;
    TabImage: TTabItem;
    TBTransp: TTrackBar;
    ColorAnimation1: TColorAnimation;
    TabControl1: TTabControl;
    TabCorners: TTabItem;
    TabFrame: TTabItem;
    TabCallout: TTabItem;
    TabBevel: TTabItem;
    BBackColor: TComboColorBox;
    LTransp: TLabel;
    CBVisible: TCheckBox;
    GroupBox1: TGroupBox;
    CBLeftTop: TComboBox;
    CBLeftBottom: TComboBox;
    CBRightTop: TComboBox;
    CBRightBottom: TComboBox;
    Label5: TLabel;
    CBSide: TComboBox;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    TBSize: TComboTrackBar;
    TBPosition: TComboTrackBar;
    TBWidth: TComboTrackBar;
    procedure PC1Change(Sender: TObject);
    procedure CBBevelChange(Sender: TObject);
    procedure CBTransparentChange(Sender: TObject);
    procedure CBRoundChange(Sender: TObject);
    procedure UDBevWChangeTracking(Sender: TObject);
    procedure UDRoundSizeChangeTracking(Sender: TObject);
    procedure TBTranspChange(Sender: TObject);
    procedure TabControl1Change(Sender: TObject);
    procedure BBackColorChange(Sender: TObject);
    procedure CBVisibleChange(Sender: TObject);
    procedure TBWidthChangeTracking(Sender: TObject);
    procedure TBPositionChangeTracking(Sender: TObject);
    procedure TBSizeChangeTracking(Sender: TObject);
    procedure CBSideChange(Sender: TObject);
  private
   { Private declarations }
    Shape : TTeeCustomShape;

    FFontEditor    : TFontEditor;
    FPenEditor     : TStrokeEditor;
    FPictureEditor : TBackImageEditor;
    FBrushEditor   : TBrushEditor;
    FEmbossEditor  : TShadowEditor;
    FShadowEditor  : TShadowEditor;
  public
   { Public declarations }

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

implementation

{$R *.fmx}

uses
  FMXTee.Canvas;

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

  if tmp=TabBorder then
     TabControl1Change(Self)
  else
  if (tmp=TabFont) and (not Assigned(FFontEditor)) then
     FFontEditor:=TFontEditor.Embedd(Self,Shape.Font,TabFont)
  else
  if (tmp=TabShadow) and (not Assigned(FShadowEditor)) then
     FShadowEditor:=TShadowEditor.InsertForm(Shape.Shadow,TabShadow)
  else
  if (tmp=TabPattern) and (not Assigned(FBrushEditor)) then
     FBrushEditor:=TBrushEditor.InsertForm(Shape.Brush,TabPattern)
  else
  if (tmp=TabEmboss) and (not Assigned(FEmbossEditor)) then
     FEmbossEditor:=TShadowEditor.InsertForm(Shape.Emboss,TabEmboss)
  else
  if (tmp=TabImage) and (not Assigned(FPictureEditor)) then
     FPictureEditor:=TBackImageEditor.InsertForm(Shape.Picture,TabImage);
end;

type
  TShapeAccess=class(TTeeCustomShape);

procedure TTeeShapeEditor.RefreshShape(const AShape: TTeeCustomShape);

  procedure SetCallout(const ACallout:TShapeCallout);
  begin
    CBSide.ItemIndex:=Ord(ACallout.Side);
    TBSize.Value:=ACallout.Size;
    TBPosition.Value:=ACallout.Position;
    TBWidth.Value:=ACallout.Width;
  end;

begin
  Shape:=AShape;

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

  CBVisible.IsChecked:=Shape.Visible;

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

  BBackColor.Color:=Shape.Color;

  TBTransp.Value:=TShapeAccess(Shape).Transparency;
  LTransp.Text:=IntToStr(TShapeAccess(Shape).Transparency);

  SetCallout(Shape.ShapeCallout);

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

  if Assigned(FPenEditor) then
     FPenEditor.RefreshPen(Shape.Pen);

  if Assigned(FPictureEditor) then
     FPictureEditor.RefreshPicture(Shape.Picture);

  if Assigned(FBrushEditor) then
     FBrushEditor.RefreshBrush(Shape.Brush);

  if Assigned(FEmbossEditor) then
     FEmbossEditor.RefreshShadow(Shape.Emboss);

  if Assigned(FShadowEditor) then
     FShadowEditor.RefreshShadow(Shape.Shadow);
end;

procedure TTeeShapeEditor.TabControl1Change(Sender: TObject);
begin
  if (TabControl1.ActiveTab=TabFrame) and (not Assigned(FPenEditor)) then
     FPenEditor:=TStrokeEditor.InsertForm(Shape.Pen,TabFrame);
end;

procedure TTeeShapeEditor.TBPositionChangeTracking(Sender: TObject);
begin
  Shape.ShapeCallout.Position:=Round(TBPosition.Value);
end;

procedure TTeeShapeEditor.TBSizeChangeTracking(Sender: TObject);
begin
  Shape.ShapeCallout.Size:=Round(TBSize.Value);
end;

procedure TTeeShapeEditor.TBTranspChange(Sender: TObject);
begin
  if Assigned(Shape) then
  begin
    TShapeAccess(Shape).Transparency:=Round(TBTransp.Value);
    LTransp.Text:=IntToStr(TShapeAccess(Shape).Transparency);
  end;
end;

procedure TTeeShapeEditor.TBWidthChangeTracking(Sender: TObject);
begin
  Shape.ShapeCallout.Width:=Round(TBWidth.Value);
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.CBTransparentChange(Sender: TObject);
begin
  Shape.Transparent:=CBTransparent.IsChecked;
end;

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

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

procedure TTeeShapeEditor.BBackColorChange(Sender: TObject);
begin
  Shape.Color:=BBackColor.Color;
end;

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

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

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

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

class function TTeeShapeEditor.InsertForm(const AShape: TTeeCustomShape;
  const AParent: TFmxObject): TTeeShapeEditor;
begin
  result:=CreateEditor(AParent.Owner,AShape);
  EmbeddForm(result,AParent);
end;

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

procedure TTeeShapeEditor.CBSideChange(Sender: TObject);
begin
  Shape.ShapeCallout.Side:=TShapeCalloutSide(CBSide.ItemIndex);
end;

end.
