unit AnimationrigValuePreset;

interface

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

type
  TAnimrigPreset = class
    protected
      fList : TStringList;
    public
      constructor Create;
      destructor Destroy; override;
      procedure Clear;

      function LoadFromStream(ms:TmemoryStream):boolean;

      function SetPreset(input:TAnimrigStateInput; const name:string; duration:single=0.0; mode:TAnimrigMisc.Easing=aneInOutQuad; beta:single=1.0):boolean;
      procedure SetDefault(input:TAnimrigStateInput; duration:single=0.0; mode:TAnimrigMisc.Easing=aneInOutQuad; beta:single=1.0);
      procedure SetClear(input:TAnimrigStateInput; duration:single=0.0; mode:TAnimrigMisc.Easing=aneInOutQuad; beta:single=1.0);

      property List:TStringList read fList;
  end;

implementation

{ TAnimrigPreset }

procedure TAnimrigPreset.Clear;
begin
  fList.Clear;
end;

constructor TAnimrigPreset.Create;
begin
  fList := TStringList.Create;
end;

destructor TAnimrigPreset.Destroy;
begin
  fList.Free;

  inherited;
end;

procedure TAnimrigPreset.SetClear(input:TAnimrigStateInput; duration:single; mode:TAnimrigMisc.Easing; beta:single);
var
  i,n : integer;
begin
  n := input.ValueCount;
  for i:=0 to n-1 do
    input.SetValue(i , 0.0,duration , mode,beta);
end;

procedure TAnimrigPreset.SetDefault(input:TAnimrigStateInput; duration:single; mode:TAnimrigMisc.Easing; beta:single);
begin
  input.SetDefaults(duration,mode,beta);
end;

function TAnimrigPreset.SetPreset(input: TAnimrigStateInput; const name: string; duration:single; mode:TAnimrigMisc.Easing; beta:single): boolean;
  procedure inItems(const s:string);
  var
    p : integer;
    v1,v2 : string;
    no : integer;
    f : single;
  begin
    p := pos(':',s);
    if p < 1 then exit;

    v1 := copy(s , 1,p-1);
    v2 := copy(s , p+1,4096);

    no := StrToIntDef(v1,-1);
    f := StrToFloatDef(v2,0.0);
    if no = -1 then exit;

    input.SetValue(no , f,duration , mode,beta);
  end;
var
  s : string;
  sl : TStringList;
begin
  result := false;

  s := fList.Values[name];
  if s = '' then exit;

  sl := TAnimrigMisc.DivideString(',',s);
  for s in sl do
  begin
    inItems(s);
  end;
  sl.Free;
end;

function TAnimrigPreset.LoadFromStream(ms: TmemoryStream):boolean;
begin
  result := false;
  Clear;

  try
    fList.LoadFromStream(ms);
  except
    exit;
  end;
  result := true;
end;

end.
