This example provides the complete code for handling a dynamic object.
The basic graphic object is a Shape object. The script code modifies the graphic object dynamically to depict a star. The user can drag the yellow hook to vary the shape of the star.
The script must be attached to the graphic object as explained in Create dynamic symbols.
The job for this example is available in the file "dynamic star.clxjob" in the example files folder.
Example
Copy code | |
---|---|
function GetMessage()
{
return " (Move hook to modify number of rays.) (+SHIT: modify ray height)" +
" (+SHIFT+CTRL: modify ray width) (+CTRL: modify radius) (+ALT: modify rotation)";
}
function DrawShape(obj, hs, rays, height, radius, rotation, width)
{
rotation = (rotation / 180 * Math.PI);
width = (width / 180 * Math.PI) / 2;
var p0 = new DlxPoint();
var p1 = new DlxPoint();
var p2 = new DlxPoint();
var sa = Math.min(width / 2, Math.TWOPI / 36);
var dr = Math.TWOPI / rays;
var h = radius + height;
var ra = rotation;
var ba = rotation - width;
var a = ba;
var ri = 0;
var fig = obj.GetFigure();
fig.BeginShape();
while (a < (ba + Math.TWOPI - sa))
{
if ((a >= (ra - width)) && (ri < rays))
{
p0.x = hs.x + radius * Math.cos(ra - width);
p0.y = hs.y + radius * Math.sin(ra - width);
p1.x = hs.x + h * Math.cos(ra);
p1.y = hs.y + h * Math.sin(ra);
p2.x = hs.x + radius * Math.cos(ra + width);
p2.y = hs.y + radius * Math.sin(ra + width);
fig.AddPoint(p0);
fig.AddPoint(p1);
fig.AddPoint(p2);
a = ra + width;
ra += dr;
ri++;
}
else
{
p0.x = hs.x + radius * Math.cos(a);
p0.y = hs.y + radius * Math.sin(a);
fig.AddPoint(p0);
}
a += sa;
}
fig.EndShape(true);
}
function OnDynamicGetHooks(obj)
{
// Place the hook in the center of the shape.
obj.AddHook(obj.GetBoundingBox().CenterPoint());
}
function OnDynamicGetInfo(obj, index)
{
// Returns the message to be displayed in the status bar.
obj.SetHookMessage(GetMessage());
}
function OnDynamicMoveHook(obj, index, mode)
{
var view = obj.GetPage().GetActiveView()
if (!view.IsValid())
return;
if (obj.GetType() != DlxApp.OBJTYPE_SHAPE)
return;
if (mode & DlxApp.DYNAMICHOOK_BEGIN)
{
// Start of dragging the yellow hook. Load parameters
obj.SetHookParam(0, obj.GetParamNumber("dyndata/shape", 1, 8));
obj.SetHookParam(1, obj.GetParamNumber("dyndata/shape", 2, 5));
obj.SetHookParam(2, obj.GetParamNumber("dyndata/shape", 3, 30));
obj.SetHookParam(3, obj.GetParamNumber("dyndata/shape", 4, 0));
obj.SetHookParam(4, obj.GetParamNumber("dyndata/shape", 5, 180/36));
obj.SetHookParam(5, obj.GetBoundingBox().CenterPoint().x);
obj.SetHookParam(6, obj.GetBoundingBox().CenterPoint().y);
}
if (mode & DlxApp.DYNAMICHOOK_END)
{
// End of dragging the yellow hook. Save parameters
obj.SetParam("dyndata/shape", 1, obj.GetHookParam(0));
obj.SetParam("dyndata/shape", 2, obj.GetHookParam(1));
obj.SetParam("dyndata/shape", 3, obj.GetHookParam(2));
obj.SetParam("dyndata/shape", 4, obj.GetHookParam(3));
obj.SetParam("dyndata/shape", 5, obj.GetHookParam(4));
}
if (mode & DlxApp.DYNAMICHOOK_NEXT)
{
// New position of the yellow hook. Construction of the new star shape.
var rays = obj.GetHookParam(0);
var height = obj.GetHookParam(1);
var radius = obj.GetHookParam(2);
var rotation = obj.GetHookParam(3);
var width = obj.GetHookParam(4);
// movement amplitude in pixels
var dx = obj.GetHookPoint(DlxApp.DYNAMICHOOK_VIEWCURRENTPOINT).x-obj.GetHookPoint(DlxApp.DYNAMICHOOK_VIEWPREVIOUSPOINT).x;
var dy = obj.GetHookPoint(DlxApp.DYNAMICHOOK_VIEWPREVIOUSPOINT).y-obj.GetHookPoint(DlxApp.DYNAMICHOOK_VIEWCURRENTPOINT).y;
if (Math.abs(dx) > Math.abs(dy))
var delta = dx;
else delta = dy;
// update values
var msgCode = 0
if (mode & DlxApp.DYNAMICHOOK_SHIFT)
{
if (mode & DlxApp.DYNAMICHOOK_CTRL)
{
width += delta*0.5; // 0.5 degrees per pixel
msgCode = 4;
}
else
{
height += delta*0.5; // 0.5mm per pixel
msgCode = 1;
}
}
else if (mode & DlxApp.DYNAMICHOOK_CTRL)
{
radius += delta*0.5; // 0.5mm per pixel
msgCode = 2;
}
else if (mode & DlxApp.DYNAMICHOOK_ALT)
{
rotation = (Math.atan2(obj.GetHookPoint(DlxApp.DYNAMICHOOK_VIEWPICKEDPOINT).y-obj.GetHookPoint(DlxApp.DYNAMICHOOK_VIEWCURRENTPOINT).y,
obj.GetHookPoint(DlxApp.DYNAMICHOOK_VIEWCURRENTPOINT).x-obj.GetHookPoint(DlxApp.DYNAMICHOOK_VIEWPICKEDPOINT).x))/Math.PI*180;
msgCode = 3;
}
else
{
rays += Math.round(delta/5); // one point every 5 pixels
msgCode = 0;
}
// limit values
rays = Math.limit(rays, 3, 36);
radius = Math.limit(radius, 1, 100);
height = Math.limit(height, -radius, 10 * radius);
width = Math.limit(width, 1, 360 / rays);
// save parameters for next step
obj.SetHookParam(0, rays);
obj.SetHookParam(1, height);
obj.SetHookParam(2, radius);
obj.SetHookParam(3, rotation);
obj.SetHookParam(4, width);
// message
var info = "";
switch (msgCode)
{
case 0:
info = DlxApp.Format("Number of star rays: %i", rays);
break;
case 1:
info = DlxApp.Format("Star ray height: %s", view.LengthToString(height, true));
break;
case 2:
info = DlxApp.Format("Radius: %s", view.LengthToString(radius, true));
break;
case 3:
info = DlxApp.Format("Rotation: %s", view.AngleToString(rotation, true));
break;
case 4:
info = DlxApp.Format("Star ray width: %s", view.AngleToString(width, true));
break;
}
obj.SetHookMessage(info + GetMessage());
var hs = new DlxPoint(obj.GetHookParam(5), obj.GetHookParam(6));
DrawShape(obj, hs, rays, height, radius, rotation, width);
}
}
function OnDynamicGetMenu(obj)
{
// Fills the object's context menu.
obj.SetHookMenu("Star\nStar 5\nStar 6\nStar 12\nDialog...");
}
function OnDynamicRunMenu(obj, cmd)
{
// Executes the selected command.
if (obj.GetType() != DlxApp.OBJTYPE_SHAPE)
return;
// load parameters
var rays = obj.GetParamNumber("dyndata/shape", 1, 8);
var height = obj.GetParamNumber("dyndata/shape", 2, 5);
var radius = obj.GetParamNumber("dyndata/shape", 3, 30);
var rotation = obj.GetParamNumber("dyndata/shape", 4, 0);
var width = obj.GetParamNumber("dyndata/shape", 5, 180/36);
switch (cmd)
{
case 0 : // Star 5
rays = 5;
break;
case 1 : // Star 6
rays = 6;
break;
case 2 : // Star 12
rays = 12;
break;
case 3:
{
var dlg = new DlxDialog(300, 150, "Star Shape");
if (dlg.IsValid())
{
dlg.AddStaticText(20, 33, 60, "Rays:");
var ctrl = dlg.AddComboBox(90, 30, 180);
for (var i = 3; i <= 36; i++)
ctrl.AddString(i.toString() + " rays", i, i==5);
dlg.AddOkButton(180, 120, 50, 25);
dlg.AddCancelButton(240, 120, 50, 25);
if (!dlg.DoModal())
return;
rays = ctrl.GetNumber();
}
}; break;
default :
return;
}
// limit values
rays = Math.limit(rays, 3, 36);
radius = Math.limit(radius, 1, 100);
height = Math.limit(height, -radius, 10 * radius);
width = Math.limit(width, 1, 360 / rays);
// save parameters
obj.SetParam("dyndata/shape", 1, rays);
obj.SetParam("dyndata/shape", 2, height);
obj.SetParam("dyndata/shape", 3, radius);
obj.SetParam("dyndata/shape", 4, rotation);
obj.SetParam("dyndata/shape", 5, width);
var hs = obj.GetBoundingBox().CenterPoint();
DrawShape(obj, hs, rays, height, radius, rotation, width);
}
|