|
1
|
1 |
Rkns.Renderers.Paper = Rkns.Utils.inherit(Rkns.Renderers._Base); |
|
|
2 |
|
|
|
3 |
Rkns.Renderers.Paper__Controllers = {} |
|
|
4 |
|
|
|
5 |
Rkns.Renderers.Paper__Controllers._Base = function(_renderer, _element) { |
|
|
6 |
if (typeof _renderer !== "undefined") { |
|
|
7 |
this._renderer = _renderer; |
|
|
8 |
this._element = _element; |
|
2
|
9 |
this._element.__controller = this; |
|
1
|
10 |
} |
|
|
11 |
} |
|
|
12 |
|
|
|
13 |
Rkns.Renderers.Paper__Controllers.Node = Rkns.Utils.inherit(Rkns.Renderers.Paper__Controllers._Base); |
|
|
14 |
|
|
|
15 |
Rkns.Renderers.Paper__Controllers.Node.prototype._init = function() { |
|
2
|
16 |
this._renderer.node_layer.activate(); |
|
|
17 |
this.type = "node"; |
|
1
|
18 |
this.node_circle = new paper.Path.Circle([0, 0], 20); |
|
2
|
19 |
this.node_circle.fillColor = '#ffffff'; |
|
|
20 |
this.node_circle.__controller = this; |
|
|
21 |
this.node_text = new paper.PointText([0,0]); |
|
|
22 |
this.node_text.characterStyle = { |
|
|
23 |
fontSize: 14, |
|
|
24 |
fillColor: 'black' |
|
|
25 |
}; |
|
|
26 |
this.node_text.paragraphStyle.justification = 'center'; |
|
|
27 |
this.node_text.__controller = this; |
|
1
|
28 |
this.redraw(); |
|
|
29 |
} |
|
|
30 |
|
|
|
31 |
Rkns.Renderers.Paper__Controllers.Node.prototype.redraw = function() { |
|
3
|
32 |
this.node_model_coords = new paper.Point(this._element.position); |
|
2
|
33 |
this.node_paper_coords = this._renderer.toPaperCoords(this.node_model_coords); |
|
|
34 |
this.node_circle.position = this.node_paper_coords; |
|
|
35 |
this.node_text.content = this._element.title; |
|
|
36 |
this.node_text.position = this.node_paper_coords.add([0, 35]); |
|
1
|
37 |
this.node_circle.strokeColor = this._element.created_by.color; |
|
|
38 |
} |
|
|
39 |
|
|
2
|
40 |
Rkns.Renderers.Paper__Controllers.Node.prototype.paperShift = function(_delta) { |
|
3
|
41 |
this._element.setPosition(Rkns._FROM_GRAPHICS, this._renderer.toModelCoords(this.node_paper_coords.add(_delta))); |
|
2
|
42 |
this._renderer.redraw(); |
|
|
43 |
} |
|
|
44 |
|
|
|
45 |
/* */ |
|
|
46 |
|
|
|
47 |
Rkns.Renderers.Paper__Controllers.Edge = Rkns.Utils.inherit(Rkns.Renderers.Paper__Controllers._Base); |
|
|
48 |
|
|
|
49 |
|
|
|
50 |
Rkns.Renderers.Paper__Controllers.Edge.prototype._init = function() { |
|
|
51 |
this._renderer.edge_layer.activate(); |
|
|
52 |
this.type = "edge"; |
|
|
53 |
this.from_node_controller = this._element.from.__controller; |
|
|
54 |
this.to_node_controller = this._element.to.__controller; |
|
|
55 |
this.edge_line = new paper.Path(); |
|
|
56 |
this.edge_line.add([0,0],[0,0]); |
|
|
57 |
this.edge_line.__controller = this; |
|
|
58 |
this.edge_text = new paper.PointText(); |
|
|
59 |
this.edge_text.characterStyle = { |
|
|
60 |
fontSize: 10, |
|
|
61 |
fillColor: 'black' |
|
|
62 |
}; |
|
|
63 |
this.edge_text.paragraphStyle.justification = 'center'; |
|
|
64 |
this.edge_text.__controller = this; |
|
|
65 |
this.edge_angle = 0; |
|
|
66 |
} |
|
|
67 |
|
|
|
68 |
Rkns.Renderers.Paper__Controllers.Edge.prototype.redraw = function() { |
|
|
69 |
this.edge_line.strokeColor = this._element.created_by.color; |
|
|
70 |
var _p0 = this.from_node_controller.node_paper_coords, |
|
|
71 |
_p1 = this.to_node_controller.node_paper_coords, |
|
|
72 |
_a = _p1.subtract(_p0).angle; |
|
|
73 |
this.edge_line.segments[0].point = _p0; |
|
|
74 |
this.edge_line.segments[1].point = _p1; |
|
|
75 |
this.edge_text.content = this._element.title; |
|
|
76 |
this.edge_text.position = _p0.add(_p1).divide(2); |
|
|
77 |
if (_a > 90) { |
|
|
78 |
_a -= 180; |
|
|
79 |
} |
|
|
80 |
if (_a < -90) { |
|
|
81 |
_a += 180; |
|
|
82 |
} |
|
|
83 |
this.edge_text.rotate(_a - this.edge_angle); |
|
|
84 |
this.edge_angle = _a; |
|
|
85 |
} |
|
|
86 |
|
|
|
87 |
Rkns.Renderers.Paper__Controllers.Edge.prototype.paperShift = function(_delta) { |
|
|
88 |
this.from_node_controller.paperShift(_delta); |
|
|
89 |
this.to_node_controller.paperShift(_delta); |
|
|
90 |
this._renderer.redraw(); |
|
|
91 |
} |
|
|
92 |
|
|
|
93 |
/* */ |
|
1
|
94 |
|
|
|
95 |
Rkns.Renderers.Paper.prototype._init = function() { |
|
|
96 |
paper.setup(document.getElementById(this._project._opts.canvas_id)); |
|
2
|
97 |
this.scale = 1; |
|
|
98 |
this.offset = paper.view.center; |
|
|
99 |
this.totalScroll = 0; |
|
|
100 |
this.dragging_target = null; |
|
|
101 |
this.edge_layer = new paper.Layer(); |
|
|
102 |
this.node_layer = new paper.Layer(); |
|
|
103 |
var _tool = new paper.Tool(), |
|
|
104 |
_this = this; |
|
|
105 |
_tool.onMouseDown = function(_event) { |
|
|
106 |
_this.onMouseDown(_event); |
|
|
107 |
} |
|
|
108 |
_tool.onMouseDrag = function(_event) { |
|
|
109 |
_this.onMouseDrag(_event); |
|
|
110 |
} |
|
|
111 |
Rkns.$("#"+this._project._opts.canvas_id).mousewheel(function(_event, _delta) { |
|
|
112 |
_this.onScroll(_event, _delta); |
|
|
113 |
}) |
|
|
114 |
paper.view.onResize = function(_event) { |
|
|
115 |
_this.offset = _this.offset.add(_event.delta.divide(2)); |
|
|
116 |
_this.redraw(); |
|
|
117 |
} |
|
|
118 |
} |
|
|
119 |
|
|
|
120 |
Rkns.Renderers.Paper.prototype.toPaperCoords = function(_point) { |
|
|
121 |
return _point.multiply(this.scale).add(this.offset); |
|
|
122 |
} |
|
|
123 |
|
|
|
124 |
|
|
|
125 |
Rkns.Renderers.Paper.prototype.toModelCoords = function(_point) { |
|
|
126 |
return _point.subtract(this.offset).divide(this.scale); |
|
1
|
127 |
} |
|
|
128 |
|
|
|
129 |
Rkns.Renderers.Paper.prototype.draw = function() { |
|
2
|
130 |
var _this = this, |
|
|
131 |
_xx = this._project.nodes.map(function(_node) { return _node.position.x }), |
|
|
132 |
_yy = this._project.nodes.map(function(_node) { return _node.position.y }), |
|
|
133 |
_minx = Math.min.apply(Math, _xx), |
|
|
134 |
_miny = Math.min.apply(Math, _yy), |
|
|
135 |
_maxx = Math.max.apply(Math, _xx), |
|
|
136 |
_maxy = Math.max.apply(Math, _yy); |
|
3
|
137 |
this.scale = Math.min((paper.view.size.width - 160) / (_maxx - _minx), (paper.view.size.height - 100) / (_maxy - _miny)); |
|
2
|
138 |
this.offset = paper.view.center.subtract(new paper.Point([(_maxx + _minx) / 2, (_maxy + _miny) / 2]).multiply(this.scale)); |
|
1
|
139 |
this.nodes = this._project.nodes.map(function(_node) { |
|
|
140 |
return new Rkns.Renderers.Paper__Controllers.Node(_this, _node); |
|
|
141 |
}); |
|
2
|
142 |
this.edges = this._project.edges.map(function(_edge) { |
|
|
143 |
return new Rkns.Renderers.Paper__Controllers.Edge(_this, _edge); |
|
|
144 |
}); |
|
|
145 |
|
|
|
146 |
this.redraw(); |
|
1
|
147 |
} |
|
|
148 |
|
|
|
149 |
Rkns.Renderers.Paper.prototype.redraw = function() { |
|
|
150 |
Rkns._(this.nodes).each(function(_node) { |
|
|
151 |
_node.redraw(); |
|
2
|
152 |
}); |
|
|
153 |
Rkns._(this.edges).each(function(_edge) { |
|
|
154 |
_edge.redraw(); |
|
|
155 |
}); |
|
|
156 |
paper.view.draw(); |
|
|
157 |
} |
|
|
158 |
|
|
|
159 |
Rkns.Renderers.Paper.prototype.onMouseDown = function(_event) { |
|
|
160 |
var _hitResult = paper.project.hitTest(_event.point); |
|
|
161 |
if (_hitResult && typeof _hitResult.item.__controller !== "undefined") { |
|
|
162 |
this.dragging_target = _hitResult.item.__controller; |
|
|
163 |
} else { |
|
|
164 |
this.dragging_target = null; |
|
|
165 |
} |
|
1
|
166 |
} |
|
2
|
167 |
|
|
|
168 |
Rkns.Renderers.Paper.prototype.onMouseDrag = function(_event) { |
|
|
169 |
if (this.dragging_target && typeof this.dragging_target.paperShift === "function") { |
|
|
170 |
this.dragging_target.paperShift(_event.delta); |
|
|
171 |
} else { |
|
|
172 |
this.offset = this.offset.add(_event.delta); |
|
|
173 |
this.redraw(); |
|
|
174 |
} |
|
|
175 |
} |
|
|
176 |
|
|
3
|
177 |
Rkns.Renderers.Paper.prototype.onScroll = function(_event, _scrolldelta) { |
|
|
178 |
this.totalScroll += _scrolldelta; |
|
2
|
179 |
if (Math.abs(this.totalScroll) >= 1) { |
|
3
|
180 |
var _off = Rkns.$("#"+this._project._opts.canvas_id).offset(), |
|
|
181 |
_delta = new paper.Point([ |
|
|
182 |
_event.pageX - _off.left, |
|
|
183 |
_event.pageY - _off.top |
|
|
184 |
]).subtract(this.offset).multiply( Math.SQRT2 - 1 ); |
|
2
|
185 |
if (this.totalScroll > 0) { |
|
3
|
186 |
this.offset = this.offset.subtract(_delta); |
|
2
|
187 |
this.scale *= Math.SQRT2; |
|
|
188 |
} else { |
|
3
|
189 |
this.offset = this.offset.add(_delta.divide( Math.SQRT2 )); |
|
2
|
190 |
this.scale *= Math.SQRT1_2; |
|
|
191 |
} |
|
|
192 |
this.totalScroll = 0; |
|
|
193 |
this.redraw(); |
|
|
194 |
} |
|
|
195 |
} |