{***********************************************}
{   TeeChart Animations Gallery Demos           }
{   Copyright (c) 2006-2020 by Steema Software  }
{***********************************************}
unit TeeAnimationsGalleryDemos;
{$I TeeDefs.inc}

interface

implementation

uses
  Classes,
  {$IFDEF D6}
  Types, Variants,
  {$ENDIF}

  {$IFDEF FMX}
  {$IFDEF D18}
  FMX.StdCtrls,
  {$ENDIF}
  FMX.Controls,
  FMXTee.Engine, FMXTee.Chart, FMXTee.Series, FMXTee.Tools.Gallery,
  FMXTee.Tools.GalleryDemos, FMXTee.Animations.Gallery, FMXTee.Animations,
  FMXTee.Animate, FMXTee.Editor.Animations, FMXTee.Editor.Animations.Transform,
  FMXTee.Animations.Tools,
  {$ELSE}

  Controls, Graphics, StdCtrls, Buttons, Dialogs,

  TeEngine, Chart, Series,
  TeeToolsGallery, TeeToolsGalleryDemos, TeeAnimationsGallery, TeeAnimations,
  TeeAnimate, TeeTransformAnimationEditor, TeeSeriesAnimEdit,
  {$ENDIF}

  Math;

type
  TAnimationDemos=class(TToolDemosBase)
  private
    IButton : TButton;

    procedure AnimationStopped(Sender:TObject);
    procedure BeginSeriesTransition(Sender:TObject);
    function CreateChart(Anim:TTeeCustomAnimationClass;
                         const ATitle:String='';
                         const ASeries:TChartSeriesClass=nil):TChart;
    procedure TransformStop(Sender:TObject);
  protected
    procedure CreateGallery; override;
  public
    procedure Animation(Sender:TObject);
  end;

function TAnimationDemos.CreateChart(Anim:TTeeCustomAnimationClass;
                     const ATitle:String='';
                     const ASeries:TChartSeriesClass=nil):TChart;
begin
  result:=NewChart(ATitle,ASeries);

  if Anim<>nil then
     result.Animations.Add(Anim);
end;

procedure TAnimationDemos.CreateGallery;

  procedure AddDelay(const Chart:TChart; ADuration:Integer);
  var tmpDelay : TTeeAnimation;
  begin
    tmpDelay:=TTeeAnimation.Create(Chart);
    tmpDelay.Duration:=ADuration;

    TTeeAnimationTool(Chart.Animations[0]).Animations.Add(tmpDelay);
  end;

var tmp : TTeeCustomAnimationClass;
    c   : TChart;
    a   : TTeeAnimation;
    tmpS : String;
    t    : Integer;
    DoAddButton : Boolean;
begin
  DoAddButton:=True;

  tmp:=TTeeCustomAnimationClass(Gallery.SelectedTool);

  if tmp=TExpandAnimation then
  with CreateChart(tmp,'Move mouse over points to see animation') do
  begin
    with (Animations[0] as TExpandAnimation) do
    begin
      View3D:=False;
      Target:=cpSeriesPointer;
      Trigger:=atMouseOver;
    end;

    TLineSeries(SeriesList[0]).Pointer.Visible:=True;
    TLineSeries(SeriesList[0]).Pointer.Style:=psCircle;
  end
  else
  if tmp=TTeeAnimationTool then
  begin
    tmpS:=Gallery.LBTool.Items[Gallery.LBTool.ItemIndex];

    if tmpS=TTransformAnimation.Description then
    begin
      c:=CreateChart(tmp,TTransformAnimation.Description);

      a:=TTransformAnimation.Create(c);
      a.Duration:=750;


      TTransformAnimation(a).Chart:=c;
      TTransformAnimation(a).Target:=cpSeries;

      TTransformAnimation(a).MaxZoom:=400;
      TTransformAnimation(a).Clip:=False;
      TTransformAnimation(a).Fade:=tfIn;

      a.OnStop:=TransformStop;

      AddDelay(c,1500);
      DoAddButton:=False;
    end
    else
    if tmpS=TRandomPointsAnimation.Description then
    begin
      c:=CreateChart(tmp,TRandomPointsAnimation.Description,TPointSeries);

      a:=TRandomPointsAnimation.Create(c);
      TRandomPointsAnimation(a).Series:=c[0];

      c.View3D:=False;

      TPointSeries(c[0]).ColorEachPoint:=True;
      TPointSeries(c[0]).Pointer.Style:=psCircle;
    end
    else
    if tmpS=TRotate3DAnimation.Description then
    begin
      c:=CreateChart(tmp,TRotate3DAnimation.Description,TBarSeries);
      a:=TRotate3DAnimation.Create(c);
      TRotate3DAnimation(a).Series:=c[0];
    end
    else
    if tmpS=TLineAnimation.Description then
    begin
      c:=CreateChart(tmp,TLineAnimation.Description,TLineSeries);

      c.AddSeries(TLineSeries).FillSampleValues;
      c[1].Color:=c.GetDefaultColor(3);

      a:=TLineAnimation.Create(c);
      TLineAnimation(a).Chart:=c;

      a.Duration:=3000;
      a.Timing:=atSquare;

      c.View3D:=False;

      for t:=0 to c.SeriesCount-1 do
      begin
        c[t].Pen.Width:=3;
        TCustomSeries(c[t]).DrawStyle:=dsCurve;
      end;

      DoAddButton:=False;
    end
    else
    if tmpS=TSeriesTransitionAnimation.Description then
    begin
      c:=CreateChart(tmp,TSeriesTransitionAnimation.Description,TBarSeries);

      c.AddSeries(TBarSeries).FillSampleValues;
      c[1].Color:=c.GetDefaultColor(3);

      a:=TSeriesTransitionAnimation.Create(c);
      TSeriesTransitionAnimation(a).Chart:=c;

      a.Duration:=300;
      a.Timing:=atCubic;
      a.TimingStyle:=atInOut;

      a.OnBegin:=BeginSeriesTransition;

      AddDelay(c,2000);

      DoAddButton:=False;
    end
    else
    begin
      c:=nil;
      a:=nil;
    end;

    if Assigned(c) then
    begin
      TTeeAnimationTool(c.Animations[0]).Animations.Add(a);

      if DoAddButton then
         AddButton(c,TeeMsg_Animate,Animation,c.Animations[0])
      else
      begin
        TTeeAnimationTool(c.Animations[0]).Loop:=True;
        c.Animations[0].Play;
      end;
    end;
  end
  else
  if tmp=TSeriesAnimationTool then
  begin
    c:=CreateChart(tmp,TSeriesAnimationTool.Description,TBarSeries);

    with c do
    begin
      Series[0].ColorEachPoint:=True;

      (Series[0] as TBarSeries).Shadow.Visible:=True;
      (Series[0] as TBarSeries).Shadow.Size:=5;

      (Animations[0] as TSeriesAnimationTool).Series:=Series[0];

      with (Animations[0] as TSeriesAnimationTool) do
      begin
        //Loop:=salCircular;
        StartValue:=0;
        StartAtMin:=False;
        Steps:=100;
        DrawEvery:=1;
      end;
    end;

    AddButton(c,TeeMsg_Animate,Animation,c.Animations[0]);
  end
end;

procedure TAnimationDemos.AnimationStopped(Sender:TObject);
begin
  // VCL only: AV at TControl.Perform
  {$IFDEF FMX}
  if Assigned(IButton) then
     IButton.Text:=TeeMsg_Animate;
  {$ENDIF}
end;

procedure TAnimationDemos.TransformStop(Sender:TObject);
var tmpS : TTransformTranslate;
    tmpT : TTransformAnimation;
    tmpZ : TTransformZoom;
begin
  tmpT:=TTransformAnimation(Sender);

  tmpS:=tmpT.TranslateStyle;
  tmpZ:=tmpT.ZoomStyle;

  if tmpS=ttNone then
    if tmpZ=tzIn then
       tmpZ:=tzOut
    else
    begin
      tmpZ:=tzNone;
      tmpS:=ttFromLeft;
    end
  else
  if tmpS=ttFromBottomRight then
  begin
    tmpS:=ttNone;
    tmpZ:=tzIn;
  end
  else
     tmpS:=TTransformTranslate(Ord(tmpS)+1);

  tmpT.ZoomStyle:=tmpZ;
  tmpT.TranslateStyle:=tmpS;
end;

procedure TAnimationDemos.BeginSeriesTransition(Sender:TObject);
var s : TSeriesTransitionAnimation;
    b : TCustomBarSeries;
    old : TMultiBar;
begin
  s:=TSeriesTransitionAnimation(Sender);
  s.Before;

  b:=TCustomBarSeries(s.Chart[0]);

  old:=b.MultiBar;

  if old=High(TMultiBar) then
     b.MultiBar:=Low(TMultiBar)
  else
     b.MultiBar:=TMultiBar(Ord(old)+1);

  s.StoreValue;
end;

procedure TAnimationDemos.Animation(Sender:TObject);

  procedure StartStop(const ATool:TTeeAnimationTool);
  begin
    if ATool.Running then
    begin
      ATool.Stop;
      ATool.OnStop:=nil;
    end
    else
    begin
      ATool.OnStop:=AnimationStopped;
      IButton.{$IFDEF FMX}Text{$ELSE}Caption{$ENDIF}:=TeeMsg_Stop;
      ATool.Play;
    end;
  end;

var tmpOb : TObject;
begin
  IButton:=(Sender as TButton);

  tmpOb:=TObject(IButton.Tag);

  if tmpOb is TTeeAnimationTool then
     StartStop(TTeeAnimationTool(tmpOb));
end;

function GalleryHook(const Gallery:{$IFDEF FMX}TToolsGallery{$ELSE}TTeeToolsGallery{$ENDIF}):TObject;
begin
  result:=TAnimationDemos.Create(Gallery);
  TAnimationDemos(result).CreateGallery;
end;

initialization
  {$IFDEF FMX}
  TAnimationsGallery.GalleryHook
  {$ELSE}
  AnimationsGalleryHook
  {$ENDIF}
   :=GalleryHook;
finalization
  {$IFDEF FMX}
  TAnimationsGallery.GalleryHook
  {$ELSE}
  AnimationsGalleryHook
  {$ENDIF}
    :=nil;
end.
