unit AnimationrigLogger;

interface

uses SysUtils,Classes,Contnrs,math,
     Animationrig,AnimationrigMisc,AnimationrigState;

type
  TAnimrigStateLogRenderer = class(TAnimrigStateRenderer)
    protected
    public
      name : string;

      constructor Create(dummyobjecttype:TAnimrigMisc.ObjectType);
      // common
      procedure Update(deltatime:double); override;
      // spine
      procedure SetAnimation(trackindex,usetrack: integer; const basename: string; loop:boolean; addanim:boolean; trackmix:TAnimrigMisc.MixBlend; duration:single; seekmode:boolean); override;
      procedure SetTrackBlendAlpha(trackindex,usetrack: integer; alpha1, alpha2, alpha3, alpha4: single; exists:boolean); override;
      procedure SetTrackSeekTime(trackindex,usetrack:integer; time1, time2, time3, time4: single; exists:boolean); override;
      procedure SetMixDuration(duration:single); override;
  end;
  TAnimrigLogger = class
    public
      type LogCallback = procedure (const s:string) of object;
      class var OnLog : LogCallback;
      class procedure Log(const s:string);
      class function FloatToStr(f:single):string;
      class function BoolToStr(b:boolean):string;
      class function FloatArrayToStr(const f:array of single; count:integer):string;
      class function MatrixToStr(const m:TAnimrigMisc.Matrix4):string;
      class function VectorToStr(const v:TAnimrigMisc.Vector3):string;
  end;

implementation

{ TAnimrigLogger }

class function TAnimrigLogger.BoolToStr(b: boolean): string;
begin
  if b then result := 'true'
       else result := 'false';
end;

class function TAnimrigLogger.FloatArrayToStr(const f: array of single; count: integer): string;
var
  i : integer;
begin
  result := '';
  for i:=0 to count-1 do
  begin
    if i > 0 then result := result + ' , ';
    result := result + FloatToStr(f[i]);
  end;
end;

class function TAnimrigLogger.FloatToStr(f: single): string;
begin
  result := format('%.2f',[f]);
  //f := floor(f * 1000 + 0.5);
  //result := SysUtils.FloatToStr(f / 100);
end;

class procedure TAnimrigLogger.Log(const s: string);
begin
  if Assigned(OnLog) then OnLog(s);
end;

class function TAnimrigLogger.MatrixToStr(const m: TAnimrigMisc.Matrix4): string;
var
  s : string;
begin
  s := FloatToStr(m._11) + ',' + FloatToStr(m._12) + ',' + FloatToStr(m._13) + ',' + FloatToStr(m._14);
  result := s;
  s := FloatToStr(m._21) + ',' + FloatToStr(m._22) + ',' + FloatToStr(m._23) + ',' + FloatToStr(m._24);
  result := result + ' , ' + s;
  s := FloatToStr(m._31) + ',' + FloatToStr(m._32) + ',' + FloatToStr(m._33) + ',' + FloatToStr(m._34);
  result := result + ' , ' + s;
  s := FloatToStr(m._41) + ',' + FloatToStr(m._42) + ',' + FloatToStr(m._43) + ',' + FloatToStr(m._44);
  result := result + ' , ' + s;
end;

class function TAnimrigLogger.VectorToStr(const v: TAnimrigMisc.Vector3): string;
begin
  result := FloatToStr(v.x) + ' , ' + FloatToStr(v.y) + ' , ' + FloatToStr(v.z);
end;

{ TAnimrigStateLogRenderer }

constructor TAnimrigStateLogRenderer.Create(dummyobjecttype:TAnimrigMisc.ObjectType);
begin
  fObjectType := dummyobjecttype;
end;

procedure TAnimrigStateLogRenderer.SetAnimation(trackindex,
  usetrack: integer; const basename: string; loop, addanim: boolean;
  trackmix: TAnimrigMisc.MixBlend; duration: single; seekmode: boolean);
begin
  TAnimrigLogger.Log(name+'.renderer.SetAnimation');
  TAnimrigLogger.Log('  track : ' + IntToStr(trackindex));
  TAnimrigLogger.Log('  name : ' + basename);
  TAnimrigLogger.Log('  loop : ' + TAnimrigLogger.BoolToStr(loop));
  TAnimrigLogger.Log('  addanimation : ' + TAnimrigLogger.BoolToStr(addanim));
  if trackmix = anmReplace then
    TAnimrigLogger.Log('  trackmix : replace')
  else
    TAnimrigLogger.Log('  trackmix : add');
  TAnimrigLogger.Log('  duration : ' + TAnimrigLogger.FloatToStr(duration));
  TAnimrigLogger.Log('  seekmode : ' + TAnimrigLogger.BoolToStr(seekmode));
end;

procedure TAnimrigStateLogRenderer.SetMixDuration(duration: single);
begin
  TAnimrigLogger.Log(name+'.renderer.SetMixDuration : ' + TAnimrigLogger.FloatToStr(duration));
end;

procedure TAnimrigStateLogRenderer.SetTrackBlendAlpha(trackindex,
  usetrack: integer; alpha1, alpha2, alpha3, alpha4: single;
  exists: boolean);
var
  s : string;
begin
  s := TAnimrigLogger.FloatToStr(alpha1) + ' , ' +
       TAnimrigLogger.FloatToStr(alpha2) + ' , ' +
       TAnimrigLogger.FloatToStr(alpha3) + ' , ' +
       TAnimrigLogger.FloatToStr(alpha4);
  TAnimrigLogger.Log(name+'.renderer.SetTrackBlendAlpha');
  TAnimrigLogger.Log('  layer exists : ' + TAnimrigLogger.BoolToStr(exists));
  TAnimrigLogger.Log('  track : ' + inttostr(trackindex));
  TAnimrigLogger.Log('  alpha : ' + s);
end;

procedure TAnimrigStateLogRenderer.SetTrackSeekTime(trackindex,
  usetrack: integer; time1, time2, time3, time4: single; exists: boolean);
var
  s : string;
begin
  s := TAnimrigLogger.FloatToStr(time1) + ' , ' +
       TAnimrigLogger.FloatToStr(time2) + ' , ' +
       TAnimrigLogger.FloatToStr(time3) + ' , ' +
       TAnimrigLogger.FloatToStr(time4);
  TAnimrigLogger.Log(name+'.renderer.SetTrackSeekTime');
  TAnimrigLogger.Log('  layer exists : ' + TAnimrigLogger.BoolToStr(exists));
  TAnimrigLogger.Log('  track : ' + inttostr(trackindex));
  TAnimrigLogger.Log('  time : ' + s);
end;

procedure TAnimrigStateLogRenderer.Update(deltatime: double);
begin
  TAnimrigLogger.Log(name+'.renderer.update : ' + TAnimrigLogger.FloatToStr(deltatime));
  TAnimrigLogger.Log(name+'.renderer.state');
  TAnimrigLogger.Log('  priority.z : ' + TAnimrigLogger.FloatToStr(DrawPriorityZ));
  TAnimrigLogger.Log('  priority.design : ' + TAnimrigLogger.FloatToStr(DrawPriorityDesign));
  TAnimrigLogger.Log('  enable : ' + TAnimrigLogger.BoolToStr(RendererEnable));
  TAnimrigLogger.Log('  visible : ' + TAnimrigLogger.BoolToStr(RendererVisible));
  TAnimrigLogger.Log('  color : ' + TAnimrigLogger.FloatArrayToStr(TransColor,4));
  TAnimrigLogger.Log('  matrix : ' + TAnimrigLogger.MatrixToStr(TransMatrix));
end;



end.
