pivot point control in scripter

This commit is contained in:
zueuk 2006-12-01 16:52:43 +00:00
parent 0f4b4afc6a
commit 59986c697a
2 changed files with 177 additions and 38 deletions

View File

@ -358,7 +358,7 @@ type
GraphZoom: double;
TriangleCaught, CornerCaught, EdgeCaught: boolean;
LocalAxisLocked: boolean;
// SelectedTriangle: integer; // outside only for scripting (??)
// SelectedTriangle: integer; // outside only for scripting
oldSelected: integer;
SelectedCorner: integer;
HasChanged: boolean;
@ -370,8 +370,8 @@ type
MemTriangle: TTriangle;
oldx, oldy, olddist: double;
Pivot, LocalPivot, WorldPivot: TSPoint;
PivotMode: (pivotLocal, pivotWorld);
Pivot: TSPoint;
VarsCache: array[0..64] of double; // hack: to prevent slow valuelist redraw
pnlDragMode: boolean;
@ -398,7 +398,10 @@ type
cp: TControlPoint;
Render: TRenderer;
// Accessible from scripter
SelectedTriangle: integer;
PivotMode: (pivotLocal, pivotWorld);
LocalPivot, WorldPivot: TSPoint;
procedure UpdatePreview;
procedure UpdateDisplay(PreviewOnly: boolean = false); //(?)
@ -406,6 +409,8 @@ type
function GetTriangleColor(n: integer): TColor;
function LastTriangle: integer;
function InsideTriangle(x, y: double): integer;
procedure ScriptGetPivot(var px, py: double);
end;
const
@ -423,7 +428,7 @@ function RotateTriangle(t: TTriangle; rad: double): TTriangle;
function OffsetTriangle(t: TTriangle; range: double): TTriangle;
function ScaleTriangle(t: TTriangle; scale: double): TTriangle;
function RotateTriangleCenter(t: TTriangle; rad: double): TTriangle;
function RotateTrianglePoint(t: TTriangle; x, y, rad: double): TTriangle;
function RotateTrianglePoint(t: TTriangle; xr, yr: double; rad: double): TTriangle;
function Centroid(t: TTriangle): TSPoint;
function OffsetTriangleRandom(t: TTriangle): TTriangle;
function ScaleTriangleCenter(t: TTriangle; scale: double): TTriangle;
@ -545,26 +550,19 @@ begin
yr := z.y;
for i := 0 to 2 do
begin
Result.x[i] := xr + (t.x[i] - xr) * cos(rad) -
(t.y[i] - yr) * sin(rad);
Result.y[i] := yr + (t.x[i] - xr) * sin(rad) +
(t.y[i] - yr) * cos(rad);
Result.x[i] := xr + (t.x[i] - xr) * cos(rad) - (t.y[i] - yr) * sin(rad);
Result.y[i] := yr + (t.x[i] - xr) * sin(rad) + (t.y[i] - yr) * cos(rad);
end;
end;
function RotateTrianglePoint(t: TTriangle; x, y, rad: double): TTriangle;
function RotateTrianglePoint(t: TTriangle; xr, yr: double; rad: double): TTriangle;
var
i: integer;
xr, yr: double;
begin
xr := x;
yr := y;
for i := 0 to 2 do
begin
Result.x[i] := xr + (t.x[i] - xr) * cos(rad) -
(t.y[i] - yr) * sin(rad);
Result.y[i] := yr + (t.x[i] - xr) * sin(rad) +
(t.y[i] - yr) * cos(rad);
Result.x[i] := xr + (t.x[i] - xr) * cos(rad) - (t.y[i] - yr) * sin(rad);
Result.y[i] := yr + (t.x[i] - xr) * sin(rad) + (t.y[i] - yr) * cos(rad);
end;
end;
@ -827,10 +825,14 @@ begin
if PivotMode = pivotLocal then begin
editPivotX.Text := Format('%.6g', [LocalPivot.x]);
editPivotY.Text := Format('%.6g', [LocalPivot.y]);
btnPivotMode.Caption := 'Local Pivot';
tbPivotMode.Down := false;
end
else begin
editPivotX.Text := Format('%.6g', [WorldPivot.x]);
editPivotY.Text := Format('%.6g', [WorldPivot.y]);
btnPivotMode.Caption := 'World Pivot';
tbPivotMode.Down := true;
end;
PageControl.Refresh;
@ -3237,6 +3239,19 @@ begin
end;
end;
procedure TEditForm.ScriptGetPivot(var px, py: double);
begin
if (PivotMode = pivotLocal) then
with MainTriangles[SelectedTriangle] do begin
px := x[1] + (x[0] - x[1])*LocalPivot.x + (x[2] - x[1])*LocalPivot.y;
py := y[1] + (y[0] - y[1])*LocalPivot.x + (y[2] - y[1])*LocalPivot.y;
end
else begin
px := WorldPivot.x;
py := WorldPivot.y;
end;
end;
procedure TEditForm.btTrgRotateLeftClick(Sender: TObject);
var
angle: double;
@ -4140,16 +4155,18 @@ end;
procedure TEditForm.btnPivotModeClick(Sender: TObject);
begin
if PivotMode <> pivotLocal then
with MainTriangles[SelectedTriangle] do begin
// with MainTriangles[SelectedTriangle] do
begin
PivotMode := pivotLocal;
btnPivotMode.Caption := 'Local Pivot';
tbPivotMode.Down := false;
// btnPivotMode.Caption := 'Local Pivot';
// tbPivotMode.Down := false;
end
else
with MainTriangles[SelectedTriangle] do begin
// with MainTriangles[SelectedTriangle] do
begin
PivotMode := pivotWorld;
btnPivotMode.Caption := 'World Pivot';
tbPivotMode.Down := true;
// btnPivotMode.Caption := 'World Pivot';
// tbPivotMode.Down := true;
end;
TriangleView.Invalidate;

View File

@ -23,7 +23,6 @@ interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls, ControlPoint, Buttons, ComCtrls, ToolWin, Menus,
// Variants,
atScript, atPascal, AdvMemo, Advmps, XFormMan, XForm, GradientHlpr, cmap;
const NCPS = 10;
@ -168,22 +167,29 @@ type
procedure SetTransformEProc(AMachine: TatVirtualMachine);
procedure GetTransformFProc(AMachine: TatVirtualMachine);
procedure SetTransformFProc(AMachine: TatVirtualMachine);
procedure GetTransformVarProc(AMachine: TatVirtualMachine);
procedure SetTransformVarProc(AMachine: TatVirtualMachine);
procedure GetTransformColorProc(AMachine: TatVirtualMachine);
procedure SetTransformColorProc(AMachine: TatVirtualMachine);
procedure GetTransformWeightProc(AMachine: TatVirtualMachine);
procedure SetTransformWeightProc(AMachine: TatVirtualMachine);
procedure GetTransformVarProc(AMachine: TatVirtualMachine);
procedure SetTransformVarProc(AMachine: TatVirtualMachine);
procedure GetTransformSymProc(AMachine: TatVirtualMachine);
procedure SetTransformSymProc(AMachine: TatVirtualMachine);
procedure GetTransformVariationProc(AMachine: TatVirtualMachine);
procedure SetTransformVariationProc(AMachine: TatVirtualMachine);
procedure GetTransformVariableProc(AMachine: TatVirtualMachine);
procedure SetTransformVariableProc(AMachine: TatVirtualMachine);
procedure GetTransformCoefsProc(AMachine: TatVirtualMachine);
procedure SetTransformCoefsProc(AMachine: TatVirtualMachine);
procedure GetTransformPostCoefsProc(AMachine: TatVirtualMachine);
procedure SetTransformPostCoefsProc(AMachine: TatVirtualMachine);
procedure TransformClearProc(AMachine: TatVirtualMachine);
procedure TransformRotateProc(AMachine: TatVirtualMachine);
{ Render interface }
procedure GetRenderFilenameProc(AMachine: TatVirtualMachine);
procedure SetRenderFilenameProc(AMachine: TatVirtualMachine);
@ -223,6 +229,8 @@ type
procedure SetOversample(AMachine: TatVirtualMachine);
procedure GetFilterRadius(AMachine: TatVirtualMachine);
procedure SetFilterRadius(AMachine: TatVirtualMachine);
procedure GetTransparency(AMachine: TatVirtualMachine);
procedure SetTransparency(AMachine: TatVirtualMachine);
procedure GetLowQuality(AMachine: TatVirtualMachine);
procedure SetLowQuality(AMachine: TatVirtualMachine);
procedure GetMediumQuality(AMachine: TatVirtualMachine);
@ -389,8 +397,13 @@ type
procedure GetVariableStr(AMachine: TatVirtualMachine);
procedure SetVariableStr(AMachine: TatVirtualMachine);
}
procedure GetProgramVersionProc(AMachine: TatVirtualMachine);
procedure VariationSupportedProc(AMachine: TatVirtualMachine);
procedure GetPivotModeProc(AMachine: TatVirtualMachine);
procedure SetPivotModeProc(AMachine: TatVirtualMachine);
procedure GetPivotXProc(AMachine: TatVirtualMachine);
procedure GetPivotYProc(AMachine: TatVirtualMachine);
procedure SetPivotProc(AMachine: TatVirtualMachine);
procedure ResetPivotProc(AMachine: TatVirtualMachine);
procedure CalculateScale(AMachine: TatVirtualMachine);
procedure NormalizeVars(AMachine: TatVirtualMachine);
@ -623,6 +636,21 @@ begin
end;
end;
procedure TScriptEditor.GetTransparency(AMachine: TatVirtualMachine);
begin
AMachine.ReturnOutPutArg(PNGTransparency);
end;
procedure TScriptEditor.SetTransparency(AMachine: TatVirtualMachine);
var
v: double;
begin
if AMachine.GetInputArgAsInteger(0) = 0 then
PNGTransparency := 0
else
PNGTransparency := 1;
end;
procedure TScriptEditor.GetLowQuality(AMachine: TatVirtualMachine);
begin
with AMachine do
@ -1188,9 +1216,16 @@ begin
Scripter.DefineMethod('GetVariableStr', 1, tkFloat, nil, GetVariableStr);
Scripter.DefineMethod('SetVariableStr', 2, tkNone, nil, SetVariableStr);
}
Scripter.DefineMethod('ProgramVersion', 0, tkString, nil, GetProgramVersionProc);
Scripter.AddConstant('ProgramVersionString', AppVersionString);
Scripter.DefineMethod('VariationSupported', 1, tkInteger, nil, VariationSupportedProc);
Scripter.DefineMethod('GetPivotMode', 0, tkInteger, nil, GetPivotModeProc);
Scripter.DefineMethod('SetPivotMode', 1, tkNone, nil, SetPivotModeProc);
Scripter.DefineMethod('GetPivotX', 0, tkFloat, nil, GetPivotXProc);
Scripter.DefineMethod('GetPivotY', 0, tkFloat, nil, GetPivotYProc);
Scripter.DefineMethod('SetPivot', 2, tkNone, nil, SetPivotProc);
Scripter.DefineMethod('ResetPivot', 0, tkNone, nil, ResetPivotProc);
Scripter.DefineMethod('CalculateScale', 0, tkNone, nil, CalculateScale);
Scripter.DefineMethod('CalculateBounds', 0, tkNone, nil, CalculateBounds);
Scripter.DefineMethod('NormalizeVars', 0, tkNone, nil, NormalizeVars);
@ -1803,11 +1838,6 @@ begin
end;
*)
procedure TOperationLibrary.GetProgramVersionProc(AMachine: TatVirtualMachine);
begin
AMachine.ReturnOutputArg(AppVersionString);
end;
procedure TOperationLibrary.VariationSupportedProc(AMachine: TatVirtualMachine);
var
i: integer;
@ -2039,6 +2069,64 @@ begin
NormalizeVariations(ScriptEditor.cp);
end;
procedure TOperationLibrary.GetPivotModeProc(AMachine: TatVirtualMachine);
begin
AMachine.ReturnOutputArg(Integer(EditForm.PivotMode));
end;
procedure TOperationLibrary.SetPivotModeProc(AMachine: TatVirtualMachine);
var
n: integer;
begin
n := AMachine.GetInputArgAsInteger(0);
if n = 0 then
EditForm.PivotMode := pivotLocal
else
EditForm.PivotMode := pivotWorld;
end;
procedure TOperationLibrary.GetPivotXProc(AMachine: TatVirtualMachine);
var
px, py: double;
begin
// EditForm.ScriptGetPivot(px, py);
// AMachine.ReturnOutputArg(px);
if EditForm.PivotMode = pivotLocal then
AMachine.ReturnOutputArg(EditForm.LocalPivot.x)
else
AMachine.ReturnOutputArg(EditForm.WorldPivot.x);
end;
procedure TOperationLibrary.GetPivotYProc(AMachine: TatVirtualMachine);
var
px, py: double;
begin
// EditForm.ScriptGetPivot(px, py);
// AMachine.ReturnOutputArg(py);
if EditForm.PivotMode = pivotLocal then
AMachine.ReturnOutputArg(EditForm.LocalPivot.y)
else
AMachine.ReturnOutputArg(EditForm.WorldPivot.y);
end;
procedure TOperationLibrary.SetPivotProc(AMachine: TatVirtualMachine);
begin
with AMachine do begin
if EditForm.PivotMode = pivotLocal then begin
EditForm.LocalPivot.x := GetInputArgAsFloat(0);
EditForm.LocalPivot.y := GetInputArgAsFloat(1);
end
else begin
EditForm.WorldPivot.x := GetInputArgAsFloat(0);
EditForm.WorldPivot.y := GetInputArgAsFloat(1);
end;
end;
end;
procedure TOperationLibrary.ResetPivotProc(AMachine: TatVirtualMachine);
begin
EditForm.btnResetPivotClick(nil);
end;
{ ******************************** Math Library ****************************** }
@ -2763,6 +2851,34 @@ begin
end;
end;
procedure TScriptEditor.TransformClearProc(AMachine: TatVirtualMachine);
begin
cp.xform[ActiveTransform].Clear;
cp.xform[ActiveTransform].density := 0.5;
end;
// -- pivot-aware rotating --
procedure TScriptEditor.TransformRotateProc(AMachine: TatVirtualMachine);
var
Triangles: TTriangles;
px, py: double;
tx: TXForm;
begin
tx := TXForm.Create;
tx.Assign(scripteditor.cp.xform[NumTransforms]); // just in case (?)
EditForm.ScriptGetPivot(px, py);
cp.TrianglesFromCp(Triangles); // it's ugly but it works...
Triangles[ActiveTransform] :=
RotateTrianglePoint(Triangles[ActiveTransform], px, py, AMachine.GetInputArgAsFloat(0) * pi / 180);
cp.GetFromTriangles(Triangles, NumTransforms);
cp.xform[NumTransforms].Assign(tx);
tx.Free;
end;
{ *************************** Render interface ****************************** }
@ -2866,12 +2982,6 @@ begin
begin
DefineProp('coefs', tkFloat, GetTransformCoefsProc, SetTransformCoefsProc, nil, false, 2);
DefineProp('post', tkFloat, GetTransformPostCoefsProc, SetTransformPostCoefsProc, nil, false, 2);
DefineProp('a', tkFloat, GetTransformAProc, SetTransformAProc);
DefineProp('b', tkFloat, GetTransformBProc, SetTransformBProc);
DefineProp('c', tkFloat, GetTransformCProc, SetTransformCProc);
DefineProp('d', tkFloat, GetTransformDProc, SetTransformDProc);
DefineProp('e', tkFloat, GetTransformEProc, SetTransformEProc);
DefineProp('f', tkFloat, GetTransformFProc, SetTransformFProc);
DefineProp('Color', tkFloat, GetTransformColorProc, SetTransformColorProc);
DefineProp('Weight', tkFloat, GetTransformWeightProc, SetTransformWeightProc);
DefineProp('Symmetry', tkFloat, GetTransformSymProc, SetTransformSymProc);
@ -2880,6 +2990,15 @@ begin
for i:= 0 to GetNrVariableNames - 1 do
DefineProp(GetVariableNameAt(i), tkFloat, GetTransformVariableProc, SetTransformVariableProc);
DefineMethod('Clear', 0, tkNone, nil, TransformClearProc);
DefineMethod('Rotate', 1, tkNone, nil, TransformRotateProc);
DefineProp('a', tkFloat, GetTransformAProc, SetTransformAProc);
DefineProp('b', tkFloat, GetTransformBProc, SetTransformBProc);
DefineProp('c', tkFloat, GetTransformCProc, SetTransformCProc);
DefineProp('d', tkFloat, GetTransformDProc, SetTransformDProc);
DefineProp('e', tkFloat, GetTransformEProc, SetTransformEProc);
DefineProp('f', tkFloat, GetTransformFProc, SetTransformFProc);
DefineProp('Variation', tkFloat, GetTransformVarProc, SetTransformVarProc, nil, false, 1); // obsolete
end;
Scripter.AddObject('Transform', Transform);
@ -2900,6 +3019,7 @@ begin
DefineProp('Vibrancy', tkFloat, GetVibrancy, SetVibrancy);
DefineProp('Oversample', tkInteger, GetOversample, SetOversample);
DefineProp('FilterRadius', tkFloat, GetFilterRadius, SetFilterRadius);
DefineProp('Transparency', tkInteger, GetTransparency, SetTransparency);
DefineProp('PreviewLowQuality', tkFloat, GetLowQuality, SetLowQuality);
DefineProp('PreviewMediumQuality', tkFloat, GetMediumQuality, SetMediumQuality);
DefineProp('PreviewHighQuality', tkFloat, GetHighQuality, SetHighQuality);
@ -2985,6 +3105,7 @@ begin
Scripter.AddConstant('V_JULIASCOPE', 36);
Scripter.AddConstant('V_CURL', 37);
Scripter.AddConstant('V_RANDOM', -1);
(*
{ Variation parameters }
Scripter.AddConstant('RADIALBLUR_ANGLE', 0);
Scripter.AddConstant('RINGS2_VAL', 1);
@ -3005,6 +3126,7 @@ begin
Scripter.AddConstant('JULIASCOPE_DIST', 16);
Scripter.AddConstant('CURL_C1', 17);
Scripter.AddConstant('CURL_C2', 18);
*)
{ Variables }
Scripter.AddVariable('SelectedTransform', EditForm.SelectedTriangle);
Scripter.AddVariable('Compatibility', Compatibility);