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

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs, FMXTee.Editor.Tools.Series,
  {$IFNDEF UPTOXE3}
  FMX.StdCtrls,
  {$ENDIF}
  {$IFDEF D21}
  FMX.SpinBox,
  {$ENDIF}
  FMXTee.Procs, FMX.ListBox, FMX.Edit, FMXTee.Tools, FMX.TabControl,
  FMXTee.Animations;

type
  TSeriesAnimToolEditor = class(TSeriesToolEditor)
    Label2: TLabel;
    CBLoop: TComboBox;
    ListBoxItem1: TListBoxItem;
    ListBoxItem2: TListBoxItem;
    ListBoxItem3: TListBoxItem;
    CBStartMin: TCheckBox;
    Label3: TLabel;
    UDDelay: TSpinBox;
    Label4: TLabel;
    BExecute: TButton;
    TBSteps: TTrackBar;
    Label5: TLabel;
    UDEvery: TSpinBox;
    Label6: TLabel;
    EStart: TEdit;
    Label7: TLabel;
    procedure FormActivate(Sender: TObject);
    procedure CBLoopChange(Sender: TObject);
    procedure CBStartMinChange(Sender: TObject);
    procedure BExecuteClick(Sender: TObject);
    procedure UDDelayChange(Sender: TObject);
    procedure EStartChange(Sender: TObject);
    procedure TBStepsChange(Sender: TObject);
    procedure CBSeriesChange(Sender: TObject);
    procedure UDEveryChange(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  private
    { Private declarations }
    SeriesAnimation : TSeriesAnimationTool;

    procedure DoStopAnimation;
  public
    { Public declarations }
  end;

implementation

{$R *.fmx}

uses
  FMXTee.ProConstants, FMXTee.Constants;

procedure TSeriesAnimToolEditor.FormActivate(Sender: TObject);
begin
  inherited;

  if not Assigned(SeriesAnimation) then
  begin
    SeriesAnimation:=TSeriesAnimationTool(TagObject);

    if Assigned(SeriesAnimation) then
    With SeriesAnimation do
    begin
      TBSteps.Value:=Steps;
      Label7.Text:=IntToStr(Steps);
      CBStartMin.IsChecked:=StartAtMin;
      EStart.Text:=FloatToStr(StartValue);
      EStart.Enabled:=not CBStartMin.IsChecked;
      UDEvery.Value:=DrawEvery;
      UDDelay.Value:=Delay;

      BExecute.Enabled:=Assigned(Series);

      if Loop=salNo then CBLoop.ItemIndex:=0
      else
      if Loop=salOneWay then CBLoop.ItemIndex:=1
      else
         CBLoop.ItemIndex:=2;

      if SeriesAnimation.Running then
      begin
        BExecute.Tag:=1;
        BExecute.Text:=TeeMsg_Stop;
      end;
    end;
  end;
end;

procedure TSeriesAnimToolEditor.FormCloseQuery(Sender: TObject;
  var CanClose: Boolean);
begin
  inherited;
  CanClose:=(not Assigned(SeriesAnimation)) or (not SeriesAnimation.Running);
end;

procedure TSeriesAnimToolEditor.DoStopAnimation;
begin
  SeriesAnimation.Stop;
  BExecute.Tag:=0;
  BExecute.Text:=TeeMsg_Execute;
end;

procedure TSeriesAnimToolEditor.BExecuteClick(Sender: TObject);
begin
  if Assigned(SeriesAnimation) then
    if BExecute.Tag=1 then DoStopAnimation
    else
    begin
      BExecute.Tag:=1;
      BExecute.Text:=TeeMsg_Stop;
      SeriesAnimation.Execute;
      DoStopAnimation;
    end;
end;

procedure TSeriesAnimToolEditor.CBSeriesChange(Sender: TObject);
begin
  inherited;
  BExecute.Enabled:=Assigned(SeriesAnimation) and Assigned(SeriesAnimation.Series);
end;

procedure TSeriesAnimToolEditor.CBStartMinChange(Sender: TObject);
begin
  if Assigned(SeriesAnimation) then
    SeriesAnimation.StartAtMin:=CBStartMin.IsChecked;
  EStart.Enabled:=not CBStartMin.IsChecked;
end;

procedure TSeriesAnimToolEditor.EStartChange(Sender: TObject);
begin
  if Assigned(SeriesAnimation) then
    SeriesAnimation.StartValue:=StrToFloatDef(EStart.Text,SeriesAnimation.StartValue);
end;

procedure TSeriesAnimToolEditor.TBStepsChange(Sender: TObject);
begin
  if Assigned(SeriesAnimation) then
  begin
    SeriesAnimation.Steps:=Round(TBSteps.Value);
    Label7.Text:=IntToStr(SeriesAnimation.Steps);
  end;
end;

procedure TSeriesAnimToolEditor.UDDelayChange(Sender: TObject);
begin
  if Assigned(SeriesAnimation) then
    SeriesAnimation.Delay:=Round(UDDelay.Value);
end;

procedure TSeriesAnimToolEditor.UDEveryChange(Sender: TObject);
begin
  if Assigned(SeriesAnimation) then
    SeriesAnimation.DrawEvery:=Round(UDEvery.Value);
end;

procedure TSeriesAnimToolEditor.CBLoopChange(Sender: TObject);
begin
  if Assigned(SeriesAnimation) then
    case CBLoop.ItemIndex of
      0: SeriesAnimation.Loop:=salNo;
      1: SeriesAnimation.Loop:=salOneWay;
    else
      SeriesAnimation.Loop:=salCircular;
  end;
end;

initialization
  RegisterSeriesEditor(TSeriesAnimToolEditor);
end.
