// shortening variables: // create a short variable name to represent the mouse position var mouse = smartShape.currentMousePos; // create a short variable name to represent the control points array var cps = smartShape.elem.controlPoints; // create a short variable name to represent the Auto Shape's customData // property. This provides an object to store information and other variables // that will be retained even after the script has been completed var data = smartShape.elem.customData; // shape variables: // radius represents the approximate size of the shape from the center // (more specifically refers to node position from center) var radius = 30; // min and maxCount determine the range for the slider determining how many "arms" // the asterisk is allowed to have. At the bottom, the slider provides minCount // arms for the shape while at the top, it provides maxCount. var minCount = 3; var maxCount = 10; // create a new generic object to contain event handler functions operation = new Object(); // event handlers: // define both a BeginDragInsert event handler and an InsertSmartShapeAt event // handler that will create the shape for either of the events depending on how // the user adds the shape to the document (click and release or drag as tool) operation.BeginDragInsert = operation.InsertSmartShapeAt = function(){ // create 2 new control points in the control points array // control points are created before the shape here because the shape needs the // control points to draw with in the Draw function cps.length = 3; // set the first control point to a position of the mouse (center of shape) // with the name "Center." Tool tips are handled with UpdateToolTips SetControlPoint(cps[0], mouse, "Center"); // set the second control point, the slider, to a position to the left of the center that's // a litlte less than twice the length of the radius with the name "Count" SetControlPoint(cps[1], AddPoints(mouse, {x:-radius*1.75, y:radius}), "Count"); // set the third control point, for rotation,, to a position to the right of the center that's // represents the starting point for the asterisk drawing using the rotation for positioning // It has the name "Rotate" SetControlPoint(cps[2], AddPoints(mouse, {x:radius, y:0}), "Rotate"); // call the draw function that will draw out the shape based on the control points Draw(); // update the dynamic tool tips UpdateToolTips(GetArmCount()); } // define an BeginDragControlPoint event handler that will initiate // register move calls to handle control point and node positioning // when the control point is moved. operation.BeginDragControlPoint = function(){ // define a variable to hold a default RegisterMoveParms object var parms = smartShape.GetDefaultMoveParms(); // assign a short variable to represent the nodes array var nods = smartShape.elem.elements[0].contours[0].nodes; // assign a short variable to represent the currently clicked control point var cp = smartShape.currentControlPoint; // save the current arm count in a customData variable called lastCount data.lastCount = GetArmCount(); // set getsDragEvents to be true so that the DragControlPoint event will // run for the Auto Shape smartShape.getsDragEvents = true; // if the current control point name is "Count" (slider) if (cp.name == "Count"){ // min/max properties for RegisterLinearMove that restrict movement // along the line to not move past radius distance above the center point // and not move radius distance below the center point parms.minLinear = (cps[0].y - radius) - cp.y; parms.maxLinear = (cps[0].y + radius) - cp.y; // create a reference point for RegisterLinearMove that's directly // below the current control point so movement is up and down var vert = AddPoints(cp, {x:0, y:100}); // set the current control point to move linearly in a vertical // manner as specified in the area within the min/max parms cp.RegisterLinearMove(vert, parms); // otherwise of the control point name is "Rotate" (rotate handle) }else if (cp.name == "Rotate"){ // create a reference point for RegisterCircularMove that // represents the center of rotation (the center of the shape) var origin = {x:cps[0].x, y:cps[0].y}; // set the current control point to move around the previously // defined origin location using RegisterCircularMove cp.RegisterCircularMove(origin, parms); // call DrawRotatePreview which will create a preview element for rotation DrawRotatePreview(); // assign a short variable to represent a nodes in the preview shape's contour var previewNode = smartShape.elem.elements[1].contours[0].nodes[1]; // set the node in the preview to move with the control point (they share the // same location) previewNode.RegisterCircularMove(origin, parms); } } // define an DragControlPoint event handler that will be called // everytime the mouse moves when dragging a control point operation.DragControlPoint = function(){ // get the current arm count for the shape var currCount = GetArmCount(); // if the arm count has changed from the saved value if (data.lastCount != currCount){ // redraw the shape Draw(); } // update the dynamic tool tips UpdateToolTips(currCount); // save the current arm count in the lastCount variable so it // can be checked next time around data.lastCount = currCount; } // define an EndDragControlPoint event handler that will be called // as a result of releasing the mouse after clicking a control point operation.EndDragControlPoint = function(){ // remove the preview element by reducing elements length to 1 (only main) smartShape.elem.elements.length = 1; // call draw to draw the asterisk shape when the mouse released Draw(); } // end event handlers // custom functions: // (shape specific functions) /** * Draw creates the asterisk shape based on the locations of the shape's control points * Requires: GetArmCount, DrawArm */ Draw = function(){ // get the number of arms the asterisk has based on the slider control point var arms = GetArmCount(); // find the angle between control points 0 and 2 representing the user-set // angle for the shape var startAngle = AngleBetween(cps[0], cps[2]); // angleOffset represents the distance between each arm. This is from // the full rotation of a circle in radians (2*Math.PI) divided by the number of arms var angleOffset = 2*Math.PI/arms; // if there are currently no elements in the Auto Shape if (!smartShape.elem.elements.length){ // create an element in the Auto Shape. This should happen only // for an InsertSmartShapeAt event smartShape.elem.elements[0] = new Path(); } // set the number of contours for the shape path to equal the number of arms smartShape.elem.elements[0].contours.length = arms; // using a loop, cycle from 0 to the number of arms // this will process the loop block for each contour in the shape for (var i=0; i