unit FMXTee.Editor.Animations.Transform;
{$I TeeDefs.inc}

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Rtti, System.Classes,
  System.Variants, FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs,
  FMX.Layouts, FMXTee.Chart.GalleryPanel, FMX.TabControl,
  FMX.ListBox, FMXTee.Animate, FMXTee.Animations.Tools;

type
  TTransformEditor = class(TForm)
    TabControl1: TTabControl;
    TabGallery: TTabItem;
    TabOptions: TTabItem;
    TabTarget: TTabItem;
    Gallery: TChartGalleryPanel;
    Label1: TLabel;
    CBTranslate: TComboBox;
    Label2: TLabel;
    CBZoom: TComboBox;
    Label3: TLabel;
    CBScale: TComboBox;
    CBFade: TCheckBox;
    CBRotate: TCheckBox;
    CBUseBounds: TCheckBox;
    CBClip: TCheckBox;
    Label4: TLabel;
    CBTarget: TComboBox;
    CBSeries: TComboBox;
    Label5: TLabel;
    CBAxes: TComboBox;
    Label6: TLabel;
    procedure GalleryChangeChart(Sender: TObject);
    procedure CBTranslateChange(Sender: TObject);
    procedure CBFadeChange(Sender: TObject);
    procedure CBRotateChange(Sender: TObject);
    procedure CBZoomChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure CBScaleChange(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure CBAxesChange(Sender: TObject);
    procedure CBClipChange(Sender: TObject);
    procedure CBUseBoundsChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure TabControl1Change(Sender: TObject);
    procedure CBTargetChange(Sender: TObject);
    procedure CBSeriesChange(Sender: TObject);
  private
    { Private declarations }
    IAnimate : TTeeAnimate;

    procedure CreateGallery;
    procedure EnableCombos;
    procedure PrepareControls;
  protected
    Animation : TTransformAnimation;
  public
    { Public declarations }
  end;

implementation

{$R *.fmx}

uses
  FMXTee.Engine, FMXTee.Constants, FMXTee.Procs, FMXTee.Series;

procedure TTransformEditor.CBAxesChange(Sender: TObject);
begin
  Animation.Axis:=TChartAxis(CBAxes.Items.Objects[CBAxes.ItemIndex]);
end;

procedure TTransformEditor.CBClipChange(Sender: TObject);
begin
  Animation.Clip:=CBClip.IsChecked;
end;

procedure TTransformEditor.CBFadeChange(Sender: TObject);
begin
//  Animation.Fade:=Ord(CBFade.ItemIndex);
end;

procedure TTransformEditor.CBRotateChange(Sender: TObject);
begin
  if CBRotate.IsChecked then
  begin
    Animation.RotateMin:=0;
    Animation.RotateMax:=360;
  end
  else
  begin
    Animation.RotateMin:=0;
    Animation.RotateMax:=0;
  end;
end;

procedure TTransformEditor.CBScaleChange(Sender: TObject);
begin
  Animation.Scale:=TTransformScale(CBScale.ItemIndex);
end;

procedure TTransformEditor.CBSeriesChange(Sender: TObject);
begin
  Animation.Series:=TChartSeries(CBSeries.Items.Objects[CBSeries.ItemIndex]);
end;

procedure TTransformEditor.EnableCombos;
begin
  CBSeries.Enabled:=(Animation.Target=cpSeries) or (Animation.Target=cpSeriesMarks);
  CBAxes.Enabled:=(Animation.Target=cpAxis) or (Animation.Target=cpAxisTitle);
end;

procedure TTransformEditor.CBTargetChange(Sender: TObject);
begin
  case CBTarget.ItemIndex of
    0: Animation.Target:=cpLegend;
    1: Animation.Target:=cpSeries;
    2: Animation.Target:=cpSeriesMarks;
    3: Animation.Target:=cpTitle;
    4: Animation.Target:=cpFoot;
    5: Animation.Target:=cpSubTitle;
    6: Animation.Target:=cpSubFoot;
    7: Animation.Target:=cpAxis;
  else
    Animation.Target:=cpAxisTitle;
  end;

  EnableCombos;
end;

procedure TTransformEditor.CBTranslateChange(Sender: TObject);
begin
  Animation.TranslateStyle:=TTransformTranslate(CBTranslate.ItemIndex);
end;

procedure TTransformEditor.CBUseBoundsChange(Sender: TObject);
begin
  Animation.UseBounds:=CBUseBounds.IsChecked;
end;

procedure TTransformEditor.CBZoomChange(Sender: TObject);
begin
  Animation.ZoomStyle:=TTransformZoom(CBZoom.ItemIndex);
  //SBMaxZoom.Visible:=Animation.ZoomStyle=tzOut;
end;

procedure TTransformEditor.FormCreate(Sender: TObject);
begin
  TabControl1.ActiveTab:=TabOptions;
end;

procedure TTransformEditor.FormDestroy(Sender: TObject);
begin
  IAnimate.Free;
end;

procedure TTransformEditor.FormShow(Sender: TObject);

  procedure FillAxes(const AList:TStrings; const AChart:TCustomAxisPanel);

    procedure AddAxis(const ATitle:String; const Axis:TChartAxis);
    begin
      AList.AddObject(ATitle,Axis);
    end;

  var t : Integer;
  begin
    AddAxis(TeeMsg_LeftAxis,AChart.Axes.Left);
    AddAxis(TeeMsg_RightAxis,AChart.Axes.Right);
    AddAxis(TeeMsg_TopAxis,AChart.Axes.Top);
    AddAxis(TeeMsg_BottomAxis,AChart.Axes.Bottom);
    AddAxis(TeeMsg_DepthTopAxis,AChart.Axes.DepthTop);
    AddAxis(TeeMsg_DepthAxis,AChart.Axes.Depth);

    for t:=0 to AChart.CustomAxes.Count-1 do
        AddAxis(TeeTranslateString('Custom axis')+' '+IntToStr(t),AChart.CustomAxes[t]);
  end;

begin
  Animation:=TTransformAnimation(Tag);

  CBSeries.Items.Clear;
  CBAxes.Items.Clear;

  if Animation<>nil then
  begin
    if Animation.Chart<>nil then
       FillSeriesItems(CBSeries.Items,Animation.Chart.SeriesList);

    CBSeries.Items.Insert(0,TeeMsg_All);
    CBSeries.ItemIndex:=CBSeries.Items.IndexOfObject(Animation.Series);

    FillAxes(CBAxes.Items,Animation.Chart);

    CBAxes.Items.Insert(0,TeeMsg_All);
    CBAxes.ItemIndex:=CBAxes.Items.IndexOfObject(Animation.Axis);

    PrepareControls;
  end;

  if not Assigned(IAnimate) then
  begin
    IAnimate:=TTeeAnimate.Create(Self);
    IAnimate.Speed:=20;
    IAnimate.Loop:=True;
  end;
end;

procedure TTransformEditor.GalleryChangeChart(Sender: TObject);
var tmp : Integer;
begin
  tmp:=Gallery.Charts.IndexOf(Gallery.SelectedChart);

  Animation.AssignTransform(TTransformAnimation(IAnimate.Animations[tmp]));

  PrepareControls;
end;

procedure TTransformEditor.TabControl1Change(Sender: TObject);
begin
  if TabControl1.ActiveTab=TabGallery then
  begin
    if Gallery.Charts.Count=0 then
       CreateGallery
    else
       IAnimate.Continue;
  end
  else
    IAnimate.Pause;
end;

procedure TTransformEditor.PrepareControls;
begin
  CBTranslate.ItemIndex:=Ord(Animation.TranslateStyle);
  CBZoom.ItemIndex:=Ord(Animation.ZoomStyle);

  {
  SBMaxZoom.Visible:=Animation.ZoomStyle=tzOut;
  SBMaxZoom.Position:=Animation.MaxZoom;
  }

  CBScale.ItemIndex:=Ord(Animation.Scale);

  CBClip.IsChecked:=Animation.Clip;
  CBUseBounds.IsChecked:=Animation.UseBounds;

  //CBFade.ItemIndex:=Ord(Animation.Fade);
  CBRotate.IsChecked:=Animation.RotateMax<>Animation.RotateMin;

  case Animation.Target of
    cpLegend      : CBTarget.ItemIndex:=0;
    cpSeries      : CBTarget.ItemIndex:=1;
    cpSeriesMarks : CBTarget.ItemIndex:=2;
    cpTitle       : CBTarget.ItemIndex:=3;
    cpFoot        : CBTarget.ItemIndex:=4;
    cpSubTitle    : CBTarget.ItemIndex:=5;
    cpSubFoot     : CBTarget.ItemIndex:=6;
    cpAxis        : CBTarget.ItemIndex:=7;
  else
    CBTarget.ItemIndex:=8; // cpAxisTitle
  end;

  EnableCombos;
end;

procedure TTransformEditor.CreateGallery;

  function Add(Translate:TTransformTranslate; const ATitle:String):TTransformAnimation;
  var tmp : TGalleryChart;
  begin
    tmp:=TGalleryChart.Create(Self);
    tmp.View3D:=False;
    tmp.ClipPoints:=True;

    tmp.Axes.Left.Labels:=False;
    tmp.Axes.Bottom.Labels:=False;

    tmp.AddSeries(TLineSeries);

    if Gallery.Charts.Count=0 then
       tmp[0].FillSampleValues
    else
       tmp[0].AssignValues(Gallery.Charts[0].Series[0]);

    Gallery.Charts.Add(tmp);

    tmp.Title.Caption:=ATitle;

    result:=TTransformAnimation.Create(Self);
    result.Target:=cpSeries;
    result.Series:=tmp[0];

    result.TranslateStyle:=Translate;

    result.StartTime:=300; // Small delay

    IAnimate.Animations.Add(result);

    tmp.Parent:=Gallery;

    Gallery.FinishAdd(tmp);
  end;

var t : Integer;
begin
  Gallery.View3D:=False;

  // Translate Gallery
  for t:=0 to CBTranslate.Items.Count-2 do
      Add(TTransformTranslate(t),CBTranslate.Items[t]);

  Add(ttNone,'Zoom In').ZoomStyle:=tzIn;

  with Add(ttNone,'Zoom Out') do
  begin
    ZoomStyle:=tzOut;
    MaxZoom:=600;
  end;

  Add(ttNone,'Rotate').RotateMax:=360;

  with Add(ttNone,'Scale Horiz.') do
  begin
    Scale:=tsHorizontal;
    Animation.Timing:=atSquare;
  end;

  with Add(ttNone,'Scale Vert.') do
  begin
    Scale:=tsVertical;
    Animation.Timing:=atSquare;
  end;

  Gallery.ResizeCharts;

  IAnimate.Play;
end;

initialization
  RegisterClass(TTransformEditor);
end.
