{ Apophysis AV "Phoenix Edition" Copyright (C) 2021 Alice V. Koryagina }

unit varHandkerchief;

interface
uses
  BaseVariation, XFormMan;

type
  TVariationHandkerchief = class(TBaseVariation)
  private
    old: byte;
	procedure CalcPlugin;
  public
    constructor Create;

    class function GetName: string; override;
    class function GetInstance: TBaseVariation; override;

    function GetNrVariables: integer; override;
    function GetVariableNameAt(const Index: integer): string; override;

    function SetVariable(const Name: string; var value: double): boolean; override;
    function GetVariable(const Name: string; var value: double): boolean; override;
    function ResetVariable(const Name: string): boolean; override;

    procedure CalcFunction; override;
	procedure GetCalcFunction(var f: TCalcFunction); override;
  end;


implementation

uses
  Math;
////////////////////////
procedure TVariationHandkerchief.GetCalcFunction(var f: TCalcFunction);
begin
	if old = 1 then f := CalcFunction
	else f := CalcPlugin;
end;

procedure TVariationHandkerchief.CalcFunction;
var
  r, FAngle: double;
begin
  FAngle := arctan2(FTx^, FTy^);
  r := sqrt(sqr(FTx^) + sqr(FTy^));
  FPx^ := FPx^ + vvar * sin(FAngle + r) * r;
  FPy^ := FPy^ + vvar * cos(FAngle - r) * r;
end;

procedure TVariationHandkerchief.CalcPlugin;
var
  r, FAngle: double;
begin
  FAngle := arctan2(FTy^, FTx^);
  r := sqrt(sqr(FTx^) + sqr(FTy^));
  FPx^ := FPx^ + vvar * sin(FAngle + r) * r;
  FPy^ := FPy^ + vvar * cos(FAngle - r) * r;
end;

constructor TVariationHandkerchief.Create;
begin
  old := 1;
end;

class function TVariationHandkerchief.GetInstance: TBaseVariation;
begin
   Result := TVariationHandkerchief.Create;
end;

class function TVariationHandkerchief.GetName: string;
begin
   Result := 'handkerchief';
end;

function TVariationHandkerchief.GetNrVariables: integer;
begin
   Result := 1;
end;

function TVariationHandkerchief.GetVariable(const Name: string;
  var value: double): boolean;
begin
  Result := False;
  if Name = 'handkerchief_old' then begin
    value := old;
    Result := True;
  end;
end;

function TVariationHandkerchief.GetVariableNameAt(const Index: integer): string;
begin
  case Index of
    0: Result := 'handkerchief_old';
  else
    Result := '';
  end;
end;

function TVariationHandkerchief.SetVariable(const Name: string;
  var value: double): boolean;
begin
  Result := False;
  if Name = 'handkerchief_old' then begin
    if Value < 0 then Value := 0;
	if Value > 1 then Value := 1;
    old := Round(value);
    Result := True;
  end;
end;

function TVariationHandkerchief.ResetVariable(const Name: string): boolean;
begin
  Result := False;
  if Name = 'handkerchief_old' then begin
    old := 1;
    Result := True;
  end;
end;

//////////////////////////////
initialization
  RegisterVariation(TVariationClassLoader.Create(TVariationHandkerchief), false, false);

end.
