
Код:
this.setShapeSelectorClass = function () {
delete this.setShapeSelectorClass;
var ShapeSelector = function () {
this.shift = this.shift;
this.points_array = [];
ASSetPropFlags (this, "shift", 1, 1);
};
// create (static)
ShapeSelector.create = function (thisObject, name, depth, initObject) {
var mc = thisObject.createEmptyMovieClip (name, depth);
mc.__proto__ = this.prototype;
for (var i in initObject) {
mc[i] = initObject[i];
}
this.call (mc);
return mc;
};
var tmp = ShapeSelector.prototype = new MovieClip ();
// private
tmp.shift = 0;
// properties. defaults
tmp.closed = true;
tmp.gap = 5;
tmp.line = 10;
tmp.speed = 1;
tmp.width = 0;
tmp.height = 0;
tmp.thickness = 0;
tmp.color = 0;
tmp.alpha = 100;
tmp.closeShape = function () {
this.closed = true;
};
tmp.addPoint = function (x, y) {
if (this.closed) {
this.closed = false;
this.points_array = [];
}
this.points_array.push ({_x:x, _y:y});
};
// redraw
tmp.onEnterFrame = function () {
this.clear (), this.lineStyle (this.thickness, this.color, this.alpha);
var shift = this.shift += this.speed;
var arr = this.points_array;
var len = arr.length;
if (len < 2) {
return;
}
for (var i = 1; i < len; i++) {
shift = dashFromTo (this, arr[i - 1]._x, arr[i - 1]._y, arr[i]._x, arr[i]._y, shift) || 0;
}
if (this.closed) {
dashFromTo (this, arr[len - 1]._x, arr[len - 1]._y, arr[0]._x, arr[0]._y, shift) || 0;
}
};
// private: shifted dashed line drawing
var dashFromTo = function (mc, sx, sy, ex, ey, shift) {
var line = mc.line, gap = mc.gap;
var dx = ex - sx, dy = ey - sy;
var dist = Math.sqrt (dx * dx + dy * dy);
var sin = dy / dist, cos = dx / dist;
var poz = 0, step = (line + gap);
shift = (shift % step + step) % step;
mc.moveTo (sx, sy);
if (shift) {
if (shift > gap) {
poz = (shift - gap);
if (poz > dist) {
mc.lineTo (dist * cos + sx, dist * sin + sy);
return poz + gap - dist;
}
mc.lineTo (poz * cos + sx, poz * sin + sy);
poz += gap;
} else {
poz = shift;
if (poz > dist) {
mc.moveTo (dist * cos + sx, dist * sin + sy);
return poz - dist;
}
}
mc.moveTo (poz * cos + sx, poz * sin + sy);
}
while (Math.abs (poz) < dist) {
poz += line;
if (poz > dist) {
mc.lineTo (dist * cos + sx, dist * sin + sy);
return poz + gap - dist;
}
mc.lineTo (poz * cos + sx, poz * sin + sy);
poz += gap;
if (poz > dist) {
mc.moveTo (dist * cos + sx, dist * sin + sy);
return poz - dist;
}
mc.moveTo (poz * cos + sx, poz * sin + sy);
}
return poz - dist;
};
_global.org = _global.org || {};
_global.org.dembicki = _global.org.dembicki || {};
_global.org.dembicki.ShapeSelector = ShapeSelector;
};
this.setShapeSelectorClass ();
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// USAGE EXAMPLE
// (AS1 and AS2 compatible)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// import org.dembicki.ShapeSelector;
trace ("Just draw something");
org.dembicki.ShapeSelector.create (this, "default_outline_mc", 0, {_x:sx, _y:sy});
this.onMouseDown = function () {
this.onMouseMove = function () {
this.default_outline_mc.addPoint (this._xmouse, this._ymouse);
};
this.onMouseUp = function () {
this.default_outline_mc.closeShape ();
delete this.onMouseMove;
delete this.onMouseUp;
};
};