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

unit varBlob;

interface

uses
  BaseVariation, XFormMan;

type
  TVariationBlob = class(TBaseVariation)
  private
    FLow, FHigh, FWaves: double;
    VLow, VHeight: double;
  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;

    procedure Prepare; override;
    procedure CalcFunction; override;
  end;

implementation

uses
  Math;

{ TVariationBlob }

///////////////////////////////////////////////////////////////////////////////
procedure TVariationBlob.Prepare;
begin
  VHeight := vvar * (FHigh - FLow) / 2;
  VLow := vvar * FLow + VHeight;
end;

procedure TVariationBlob.CalcFunction;
var
  r : double;
begin
  r := VLow + VHeight * sin(FWaves * arctan2(FTx^, FTy^));

  FPx^ := FPx^ + r * FTx^;
  FPy^ := FPy^ + r * FTy^;
end;

///////////////////////////////////////////////////////////////////////////////
class function TVariationBlob.GetName: string;
begin
  Result := 'blob';
end;

///////////////////////////////////////////////////////////////////////////////
function TVariationBlob.GetVariableNameAt(const Index: integer): string;
begin
  case Index Of
	0: Result := 'blob_low';
	1: Result := 'blob_high';
	2: Result := 'blob_waves';
  else
    Result := '';
  end
end;

///////////////////////////////////////////////////////////////////////////////
function TVariationBlob.GetNrVariables: integer;
begin
  Result := 3;
end;

///////////////////////////////////////////////////////////////////////////////
function TVariationBlob.SetVariable(const Name: string; var value: double): boolean;
begin
  Result := False;
  if Name = 'blob_low' then begin
    FLow := Value;
    Result := True;
  end else if Name = 'blob_high' then begin
    FHigh := Value;
    Result := True;
  end else if Name = 'blob_waves' then begin
    // Value := Round(Value);
    FWaves := Value;
    Result := True;
  end
end;

///////////////////////////////////////////////////////////////////////////////
function TVariationBlob.GetVariable(const Name: string; var value: double): boolean;
begin
  Result := False;
  if Name = 'blob_low' then begin
    Value := FLow;
    Result := True;
  end else if Name = 'blob_high' then begin
    Value := FHigh;
    Result := True;
  end else if Name = 'blob_waves' then begin
    Value := FWaves;
    Result := True;
  end
end;

///////////////////////////////////////////////////////////////////////////////
constructor TVariationBlob.Create;
begin
  inherited Create;

  FWaves := Round(2 + 5 * Random);
  FLow  := 0.2 + 0.5 * random;
  FHigh := 0.8 + 0.4 * random;
end;

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

///////////////////////////////////////////////////////////////////////////////
initialization
  RegisterVariation(TVariationClassLoader.Create(TVariationBlob), false, false);
end.
