-
Notifications
You must be signed in to change notification settings - Fork 0
/
DRRib.as
124 lines (92 loc) · 3.77 KB
/
DRRib.as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
class DRRib {
static var RIB_WIDTH = 100;
static var nextId = 0;
var id:Number;
var course:DRCourse;
var forwardWidth:Number;
var ratio:Number;
var angle:Number;
var centerHandle: DRControlPoint;
var forwardHandle: DRControlPoint;
var backwardHandle:DRControlPoint;
var clip:MovieClip;
function DRRib(course:DRCourse, x:Number, y:Number, angle:Number) {
this.id = nextId++;
this.course = course;
this.angle = angle;
this.forwardWidth = RIB_WIDTH / 2.0;
this.ratio = 1.0;
centerHandle = new DRControlPoint(this, course.drawCurve, x, y, DRControlPoint.CIRCLE);
if (angle || angle == 0.0) {
forwardHandle = new DRControlPoint(this, course.forwardCurve, x, y, DRControlPoint.BOX);
backwardHandle = new DRControlPoint(this, course.backwardCurve, x, y, DRControlPoint.BOX);
updateHandles();
course.forwardCurve.draw();
course.backwardCurve.draw();
}
}
function x():Number { return centerHandle.x; }
function y():Number { return centerHandle.y; }
function updateHandles() {
var forwardAngle = angle + Math.PI / 2.0;
var backwardAngle = angle - Math.PI / 2.0;
var forwardX = x() + forwardWidth * Math.cos(forwardAngle);
var forwardY = y() + forwardWidth * Math.sin(forwardAngle);
forwardHandle.shiftTo(forwardX, forwardY);
var backwardX = x() + ratio * forwardWidth * Math.cos(backwardAngle);
var backwardY = y() + ratio * forwardWidth * Math.sin(backwardAngle);
backwardHandle.shiftTo(backwardX, backwardY);
}
function display(ribs:MovieClip, controlPoints:MovieClip) {
clip = ribs.createEmptyMovieClip("Rib_" + id, id);
centerHandle.display(controlPoints);
forwardHandle.display(controlPoints);
backwardHandle.display(controlPoints);
draw();
}
function draw() {
clip.clear();
clip.lineStyle(1.0, 0x000000, 50);
clip.moveTo(forwardHandle.x, forwardHandle.y );
clip.lineTo(backwardHandle.x, backwardHandle.y);
forwardHandle.draw();
backwardHandle.draw();
centerHandle.draw();
}
function controlPointMoved(point:DRControlPoint, unevenWidth:Boolean) {
if (point == forwardHandle || point == backwardHandle) {
var dx = x() - point.x;
var dy = y() - point.y;
var length = Math.sqrt(dx * dx + dy * dy);
if (unevenWidth) {
if (point == forwardHandle) {
forwardWidth = length;
}
if (point == backwardHandle) {
ratio = length / forwardWidth;
}
} else {
if (point == forwardHandle) {
forwardWidth = length;
}
if (point == backwardHandle) {
forwardWidth = length / ratio;
}
}
var angleOffset = (point == forwardHandle) ? Math.PI / 2.0 : -Math.PI / 2.0;
angle = Math.atan2(dy, dx) + angleOffset;
}
updateHandles();
course.ribMoved(this);
}
function replaceControlPoint(point:DRControlPoint, otherPoint:DRControlPoint) {
if (point == forwardHandle) {
forwardHandle = otherPoint;
} else if (point == backwardHandle) {
backwardHandle = otherPoint;
} else if (point == centerHandle) {
centerHandle = otherPoint;
}
controlPointMoved(otherPoint, false);
}
}