unit FMXTee.Editor.Animation;
{$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.ListBox, FMX.Edit,
  FMX.TabControl, FMXTee.Animate;

type
  TAnimationEditor = class(TForm)
    TabControl1: TTabControl;
    TabOptions: TTabItem;
    TabTiming: TTabItem;
    Label1: TLabel;
    EDuration: TComboTrackBar;
    Label2: TLabel;
    Label3: TLabel;
    EStartTime: TComboTrackBar;
    Label4: TLabel;
    CBEnabled: TCheckBox;
    CBLoop: TCheckBox;
    Label5: TLabel;
    ETitle: TEdit;
    Panel1: TPanel;
    Label6: TLabel;
    CBTiming: TComboBox;
    Gallery: TChartGalleryPanel;
    procedure CBTimingChange(Sender: TObject);
    procedure GalleryChangeChart(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure TabControl1Change(Sender: TObject);
    procedure EDurationChangeTracking(Sender: TObject);
    procedure EStartTimeChangeTracking(Sender: TObject);
    procedure CBEnabledChange(Sender: TObject);
    procedure CBLoopChange(Sender: TObject);
  private
    { Private declarations }
    Animation : TTeeAnimation;
    FOnChanged : TNotifyEvent;

    procedure DoChanged;
  public
    { Public declarations }
    procedure RefreshAnimation(const AAnimation:TTeeAnimation);
  end;

implementation

{$R *.fmx}

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

procedure TAnimationEditor.CBEnabledChange(Sender: TObject);
begin
  Animation.Enabled:=CBEnabled.IsChecked;
end;

procedure TAnimationEditor.CBLoopChange(Sender: TObject);
begin
  Animation.Loop:=CBLoop.IsChecked;
end;

procedure TAnimationEditor.CBTimingChange(Sender: TObject);
begin
  Animation.Timing:=TAnimationTiming(CBTiming.ItemIndex);

  if Gallery.Charts.Count>0 then
     Gallery.SelectedChart:=Gallery.Charts[CBTiming.ItemIndex];
end;

procedure TAnimationEditor.EDurationChangeTracking(Sender: TObject);
begin
  Animation.Duration:=StrToIntDef(EDuration.Text,Animation.Duration);
  DoChanged;
end;

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

procedure TAnimationEditor.GalleryChangeChart(Sender: TObject);
begin
  CBTiming.ItemIndex:=Gallery.Charts.IndexOf(Gallery.SelectedChart);
  Animation.Timing:=TAnimationTiming(CBTiming.ItemIndex);
end;

procedure TAnimationEditor.EStartTimeChangeTracking(Sender: TObject);
begin
  Animation.StartTime:=StrToIntDef(EStartTime.Text,Animation.StartTime);
  DoChanged;
end;

procedure TAnimationEditor.DoChanged;
begin
  if Assigned(FOnChanged) then
     FOnChanged(Animation);
end;

type
  TAnimationAccess=class(TTeeAnimation);

procedure TAnimationEditor.RefreshAnimation(const AAnimation: TTeeAnimation);
begin
  Animation:=AAnimation;

  if Assigned(Animation) then
  with Animation do
  begin
    EDuration.Text:=IntToStr(Duration);

    if Assigned(Parent) and TAnimationAccess(Parent).IsSequence then
       EStartTime.Enabled:=False;

    EStartTime.Text:=IntToStr(StartTime);
    CBTiming.ItemIndex:=Ord(Timing);

    CBEnabled.IsChecked:=Enabled;
    CBLoop.IsChecked:=Loop;
  end;
end;

procedure TAnimationEditor.TabControl1Change(Sender: TObject);
var
  tmpAnim : TTeeAnimation;

  procedure CreateChart(const ATitle:String; ATiming:TAnimationTiming);
  var tmp : TGalleryChart;
      t   : Integer;
  begin
    tmp:=TGalleryChart.Create(Self);
    tmp.View3D:=False;
    tmp.Axes.Hide;
    tmp.Walls.Back.Show;

    tmp.AddSeries(TFastLineSeries);
    TFastLineSeries(tmp[0]).DrawStyle:=flAll;

    tmpAnim.Timing:=ATiming;

    for t:=0 to 100 do
        tmp[0].Add(tmpAnim.TimingFunction(t*0.01));

    tmp.Title.Caption:=ATitle;

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

var t : Integer;
begin
  if TabControl1.ActiveTab=TabTiming then
     if Gallery.Charts.Count=0 then
     begin
       tmpAnim:=TTeeAnimation.Create(nil);
       try
         //tmpAnim.TimingStyle:=TAnimationTimingStyle(CBEasing.ItemIndex);

         for t:=0 to CBTiming.Items.Count-1 do
             CreateChart(CBTiming.Items[t],TAnimationTiming(t));
       finally
         tmpAnim.Free; 
       end;

       Gallery.ResizeCharts;

       Gallery.SelectedChart:=Gallery.Charts[CBTiming.ItemIndex];
     end;
end;

initialization
  RegisterClass(TAnimationEditor);
end.
