|
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") { |
|
4
|
7 |
this.id = Rkns.Utils.getUID('controller'); |
|
1
|
8 |
this._renderer = _renderer; |
|
|
9 |
this._element = _element; |
|
2
|
10 |
this._element.__controller = this; |
|
1
|
11 |
} |
|
|
12 |
} |
|
|
13 |
|
|
4
|
14 |
Rkns.Renderers.Paper__Controllers._Base.prototype.select = function() {} |
|
|
15 |
|
|
|
16 |
Rkns.Renderers.Paper__Controllers._Base.prototype.unselect = function() {} |
|
|
17 |
|
|
1
|
18 |
Rkns.Renderers.Paper__Controllers.Node = Rkns.Utils.inherit(Rkns.Renderers.Paper__Controllers._Base); |
|
|
19 |
|
|
|
20 |
Rkns.Renderers.Paper__Controllers.Node.prototype._init = function() { |
|
2
|
21 |
this._renderer.node_layer.activate(); |
|
|
22 |
this.type = "node"; |
|
4
|
23 |
this.node_circle = new paper.Path.Circle([0, 0], Rkns._NODE_RADIUS); |
|
2
|
24 |
this.node_circle.fillColor = '#ffffff'; |
|
|
25 |
this.node_circle.__controller = this; |
|
|
26 |
this.node_text = new paper.PointText([0,0]); |
|
|
27 |
this.node_text.characterStyle = { |
|
4
|
28 |
fontSize: Rkns._NODE_FONT_SIZE, |
|
2
|
29 |
fillColor: 'black' |
|
|
30 |
}; |
|
|
31 |
this.node_text.paragraphStyle.justification = 'center'; |
|
|
32 |
this.node_text.__controller = this; |
|
1
|
33 |
} |
|
|
34 |
|
|
|
35 |
Rkns.Renderers.Paper__Controllers.Node.prototype.redraw = function() { |
|
3
|
36 |
this.node_model_coords = new paper.Point(this._element.position); |
|
2
|
37 |
this.node_paper_coords = this._renderer.toPaperCoords(this.node_model_coords); |
|
|
38 |
this.node_circle.position = this.node_paper_coords; |
|
|
39 |
this.node_text.content = this._element.title; |
|
4
|
40 |
this.node_text.position = this.node_paper_coords.add([0, 2 * Rkns._NODE_RADIUS]); |
|
1
|
41 |
this.node_circle.strokeColor = this._element.created_by.color; |
|
|
42 |
} |
|
|
43 |
|
|
2
|
44 |
Rkns.Renderers.Paper__Controllers.Node.prototype.paperShift = function(_delta) { |
|
4
|
45 |
this._element.setPosition(this._renderer.toModelCoords(this.node_paper_coords.add(_delta))); |
|
|
46 |
this._renderer._project.serializer.save(); |
|
2
|
47 |
this._renderer.redraw(); |
|
|
48 |
} |
|
|
49 |
|
|
4
|
50 |
Rkns.Renderers.Paper__Controllers.Node.prototype.select = function(_delta) { |
|
|
51 |
this.node_circle.strokeWidth = 3; |
|
|
52 |
} |
|
|
53 |
|
|
|
54 |
Rkns.Renderers.Paper__Controllers.Node.prototype.unselect = function(_delta) { |
|
|
55 |
this.node_circle.strokeWidth = 1; |
|
|
56 |
} |
|
|
57 |
|
|
2
|
58 |
/* */ |
|
|
59 |
|
|
|
60 |
Rkns.Renderers.Paper__Controllers.Edge = Rkns.Utils.inherit(Rkns.Renderers.Paper__Controllers._Base); |
|
|
61 |
|
|
|
62 |
Rkns.Renderers.Paper__Controllers.Edge.prototype._init = function() { |
|
|
63 |
this._renderer.edge_layer.activate(); |
|
|
64 |
this.type = "edge"; |
|
|
65 |
this.from_node_controller = this._element.from.__controller; |
|
|
66 |
this.to_node_controller = this._element.to.__controller; |
|
|
67 |
this.edge_line = new paper.Path(); |
|
|
68 |
this.edge_line.add([0,0],[0,0]); |
|
|
69 |
this.edge_line.__controller = this; |
|
4
|
70 |
this.edge_arrow = new paper.Path(); |
|
|
71 |
this.edge_arrow.add([0,0],[Rkns._ARROW_LENGTH,Rkns._ARROW_WIDTH / 2],[0,Rkns._ARROW_WIDTH]); |
|
|
72 |
this.edge_arrow.__controller = this; |
|
2
|
73 |
this.edge_text = new paper.PointText(); |
|
|
74 |
this.edge_text.characterStyle = { |
|
4
|
75 |
fontSize: Rkns._EDGE_FONT_SIZE, |
|
2
|
76 |
fillColor: 'black' |
|
|
77 |
}; |
|
|
78 |
this.edge_text.paragraphStyle.justification = 'center'; |
|
|
79 |
this.edge_text.__controller = this; |
|
4
|
80 |
this.edge_text_angle = 0; |
|
|
81 |
this.edge_arrow_angle = 0; |
|
2
|
82 |
} |
|
|
83 |
|
|
|
84 |
Rkns.Renderers.Paper__Controllers.Edge.prototype.redraw = function() { |
|
|
85 |
var _p0 = this.from_node_controller.node_paper_coords, |
|
|
86 |
_p1 = this.to_node_controller.node_paper_coords, |
|
4
|
87 |
_a = _p1.subtract(_p0).angle, |
|
|
88 |
_center = _p0.add(_p1).divide(2), |
|
|
89 |
_color = this._element.created_by.color; |
|
|
90 |
this.edge_line.strokeColor = _color; |
|
2
|
91 |
this.edge_line.segments[0].point = _p0; |
|
|
92 |
this.edge_line.segments[1].point = _p1; |
|
4
|
93 |
this.edge_arrow.rotate(_a - this.edge_arrow_angle); |
|
|
94 |
this.edge_arrow.fillColor = _color; |
|
|
95 |
this.edge_arrow.position = _center; |
|
|
96 |
this.edge_arrow_angle = _a; |
|
2
|
97 |
if (_a > 90) { |
|
|
98 |
_a -= 180; |
|
|
99 |
} |
|
|
100 |
if (_a < -90) { |
|
|
101 |
_a += 180; |
|
|
102 |
} |
|
4
|
103 |
this.edge_text.rotate(_a - this.edge_text_angle); |
|
|
104 |
this.edge_text.content = this._element.title; |
|
|
105 |
this.edge_text.position = _center; |
|
|
106 |
this.edge_text_angle = _a; |
|
|
107 |
} |
|
|
108 |
|
|
|
109 |
Rkns.Renderers.Paper__Controllers.Edge.prototype.select = function(_delta) { |
|
|
110 |
this.edge_line.strokeWidth = 3; |
|
|
111 |
} |
|
|
112 |
|
|
|
113 |
Rkns.Renderers.Paper__Controllers.Edge.prototype.unselect = function(_delta) { |
|
|
114 |
this.edge_line.strokeWidth = 1; |
|
2
|
115 |
} |
|
|
116 |
|
|
|
117 |
Rkns.Renderers.Paper__Controllers.Edge.prototype.paperShift = function(_delta) { |
|
|
118 |
this.from_node_controller.paperShift(_delta); |
|
|
119 |
this.to_node_controller.paperShift(_delta); |
|
|
120 |
this._renderer.redraw(); |
|
|
121 |
} |
|
4
|
122 |
/* */ |
|
|
123 |
|
|
|
124 |
Rkns.Renderers.Paper__Controllers.TempEdge = Rkns.Utils.inherit(Rkns.Renderers.Paper__Controllers._Base); |
|
|
125 |
|
|
|
126 |
Rkns.Renderers.Paper__Controllers.TempEdge.prototype._init = function() { |
|
|
127 |
this._renderer.edge_layer.activate(); |
|
|
128 |
this.type = "temp-edge"; |
|
|
129 |
var _color = this._renderer._project.current_user.color; |
|
|
130 |
this.edge_line = new paper.Path(); |
|
|
131 |
this.edge_line.strokeColor = _color; |
|
|
132 |
this.edge_line.add([0,0],[0,0]); |
|
|
133 |
this.edge_line.__controller = this; |
|
|
134 |
this.edge_arrow = new paper.Path(); |
|
|
135 |
this.edge_arrow.fillColor = _color; |
|
|
136 |
this.edge_arrow.add([0,0],[Rkns._ARROW_LENGTH,Rkns._ARROW_WIDTH / 2],[0,Rkns._ARROW_WIDTH]); |
|
|
137 |
this.edge_arrow.__controller = this; |
|
|
138 |
this.edge_arrow_angle = 0; |
|
|
139 |
} |
|
|
140 |
|
|
|
141 |
Rkns.Renderers.Paper__Controllers.TempEdge.prototype.redraw = function() { |
|
|
142 |
var _p0 = this.from_node_controller.node_paper_coords, |
|
|
143 |
_p1 = this.end_pos, |
|
|
144 |
_a = _p1.subtract(_p0).angle, |
|
|
145 |
_c = _p0.add(_p1).divide(2); |
|
|
146 |
this.edge_line.segments[0].point = _p0; |
|
|
147 |
this.edge_line.segments[1].point = _p1; |
|
|
148 |
this.edge_arrow.rotate(_a - this.edge_arrow_angle); |
|
|
149 |
this.edge_arrow.position = _c; |
|
|
150 |
this.edge_arrow_angle = _a; |
|
|
151 |
} |
|
|
152 |
|
|
|
153 |
Rkns.Renderers.Paper__Controllers.TempEdge.prototype.paperShift = function(_delta) { |
|
|
154 |
this.end_pos = this.end_pos.add(_delta); |
|
|
155 |
this._renderer.onMouseMove({point: this.end_pos}); |
|
|
156 |
this.redraw(); |
|
|
157 |
} |
|
|
158 |
|
|
|
159 |
Rkns.Renderers.Paper__Controllers.TempEdge.prototype.finishEdge = function(_event) { |
|
|
160 |
var _hitResult = paper.project.hitTest(_event.point); |
|
|
161 |
if (_hitResult && typeof _hitResult.item.__controller !== "undefined") { |
|
|
162 |
var _target = _hitResult.item.__controller; |
|
|
163 |
if (_target.type === "node" && this.from_node_controller._element.id !== _target._element.id) { |
|
|
164 |
this._renderer._project.addEdge({ |
|
|
165 |
from: this.from_node_controller._element.id, |
|
|
166 |
to: _target._element.id |
|
|
167 |
}, Rkns._RENDER_AND_SAVE) |
|
|
168 |
} |
|
|
169 |
} |
|
|
170 |
this.edge_arrow.remove(); |
|
|
171 |
this.edge_line.remove(); |
|
|
172 |
this._renderer.controllers.removeId(this.id); |
|
|
173 |
} |
|
2
|
174 |
|
|
|
175 |
/* */ |
|
1
|
176 |
|
|
|
177 |
Rkns.Renderers.Paper.prototype._init = function() { |
|
4
|
178 |
this._MARGIN_X = 80; |
|
|
179 |
this._MARGIN_Y = 50; |
|
|
180 |
var _canvas_id = this._project._opts.canvas_id; |
|
|
181 |
this.$ = Rkns.$("#"+_canvas_id) |
|
|
182 |
paper.setup(document.getElementById(_canvas_id)); |
|
2
|
183 |
this.scale = 1; |
|
|
184 |
this.offset = paper.view.center; |
|
|
185 |
this.totalScroll = 0; |
|
|
186 |
this.dragging_target = null; |
|
4
|
187 |
this.selected_target = null; |
|
2
|
188 |
this.edge_layer = new paper.Layer(); |
|
|
189 |
this.node_layer = new paper.Layer(); |
|
|
190 |
var _tool = new paper.Tool(), |
|
|
191 |
_this = this; |
|
4
|
192 |
_tool.onMouseMove = function(_event) { |
|
|
193 |
_this.onMouseMove(_event); |
|
|
194 |
} |
|
2
|
195 |
_tool.onMouseDown = function(_event) { |
|
|
196 |
_this.onMouseDown(_event); |
|
|
197 |
} |
|
|
198 |
_tool.onMouseDrag = function(_event) { |
|
|
199 |
_this.onMouseDrag(_event); |
|
|
200 |
} |
|
4
|
201 |
_tool.onMouseUp = function(_event) { |
|
|
202 |
_this.onMouseUp(_event); |
|
|
203 |
} |
|
|
204 |
this.$.mousewheel(function(_event, _delta) { |
|
2
|
205 |
_this.onScroll(_event, _delta); |
|
|
206 |
}) |
|
4
|
207 |
this.$.dblclick(function(_event) { |
|
|
208 |
_this.onDoubleClick(_event); |
|
|
209 |
}) |
|
2
|
210 |
paper.view.onResize = function(_event) { |
|
|
211 |
_this.offset = _this.offset.add(_event.delta.divide(2)); |
|
|
212 |
_this.redraw(); |
|
|
213 |
} |
|
|
214 |
} |
|
|
215 |
|
|
|
216 |
Rkns.Renderers.Paper.prototype.toPaperCoords = function(_point) { |
|
|
217 |
return _point.multiply(this.scale).add(this.offset); |
|
|
218 |
} |
|
|
219 |
|
|
|
220 |
|
|
|
221 |
Rkns.Renderers.Paper.prototype.toModelCoords = function(_point) { |
|
|
222 |
return _point.subtract(this.offset).divide(this.scale); |
|
1
|
223 |
} |
|
|
224 |
|
|
|
225 |
Rkns.Renderers.Paper.prototype.draw = function() { |
|
2
|
226 |
var _this = this, |
|
|
227 |
_xx = this._project.nodes.map(function(_node) { return _node.position.x }), |
|
|
228 |
_yy = this._project.nodes.map(function(_node) { return _node.position.y }), |
|
|
229 |
_minx = Math.min.apply(Math, _xx), |
|
|
230 |
_miny = Math.min.apply(Math, _yy), |
|
|
231 |
_maxx = Math.max.apply(Math, _xx), |
|
|
232 |
_maxy = Math.max.apply(Math, _yy); |
|
4
|
233 |
this.scale = Math.min((paper.view.size.width - 2 * this._MARGIN_X) / (_maxx - _minx), (paper.view.size.height - 2 * this._MARGIN_Y) / (_maxy - _miny)); |
|
2
|
234 |
this.offset = paper.view.center.subtract(new paper.Point([(_maxx + _minx) / 2, (_maxy + _miny) / 2]).multiply(this.scale)); |
|
4
|
235 |
this.controllers = new Rkns.Model.List(); |
|
|
236 |
this._project.nodes.forEach(function(_node) { |
|
|
237 |
_this.addElement("Node", _node); |
|
1
|
238 |
}); |
|
4
|
239 |
this._project.edges.forEach(function(_edge) { |
|
|
240 |
_this.addElement("Edge", _edge); |
|
2
|
241 |
}); |
|
|
242 |
|
|
|
243 |
this.redraw(); |
|
1
|
244 |
} |
|
|
245 |
|
|
4
|
246 |
Rkns.Renderers.Paper.prototype.addElement = function(_type, _element) { |
|
|
247 |
var _el = new Rkns.Renderers.Paper__Controllers[_type](this, _element); |
|
|
248 |
this.controllers.push(_el); |
|
|
249 |
return _el; |
|
|
250 |
} |
|
|
251 |
|
|
1
|
252 |
Rkns.Renderers.Paper.prototype.redraw = function() { |
|
4
|
253 |
this.controllers.forEach(function(_controller) { |
|
|
254 |
_controller.redraw(); |
|
2
|
255 |
}); |
|
|
256 |
paper.view.draw(); |
|
|
257 |
} |
|
|
258 |
|
|
4
|
259 |
Rkns.Renderers.Paper.prototype.onMouseMove = function(_event) { |
|
|
260 |
var _hitResult = paper.project.hitTest(_event.point); |
|
|
261 |
if (_hitResult && typeof _hitResult.item.__controller !== "undefined") { |
|
|
262 |
if (this.selected_target !== _hitResult.item.__controller) { |
|
|
263 |
if (this.selected_target) { |
|
|
264 |
this.selected_target.unselect(); |
|
|
265 |
} |
|
|
266 |
this.selected_target = _hitResult.item.__controller; |
|
|
267 |
this.selected_target.select(); |
|
|
268 |
} |
|
|
269 |
} else { |
|
|
270 |
if (this.selected_target) { |
|
|
271 |
this.selected_target.unselect(); |
|
|
272 |
} |
|
|
273 |
this.selected_target = null; |
|
|
274 |
} |
|
|
275 |
} |
|
|
276 |
|
|
2
|
277 |
Rkns.Renderers.Paper.prototype.onMouseDown = function(_event) { |
|
|
278 |
var _hitResult = paper.project.hitTest(_event.point); |
|
|
279 |
if (_hitResult && typeof _hitResult.item.__controller !== "undefined") { |
|
|
280 |
this.dragging_target = _hitResult.item.__controller; |
|
4
|
281 |
if (this.dragging_target.type === "node" && _hitResult.type === "stroke") { |
|
|
282 |
var _tmpEdge = this.addElement("TempEdge",{}); |
|
|
283 |
_tmpEdge.end_pos = _event.point; |
|
|
284 |
_tmpEdge.from_node_controller = this.dragging_target; |
|
|
285 |
_tmpEdge.redraw(); |
|
|
286 |
this.dragging_target = _tmpEdge; |
|
|
287 |
} |
|
2
|
288 |
} else { |
|
|
289 |
this.dragging_target = null; |
|
|
290 |
} |
|
1
|
291 |
} |
|
2
|
292 |
|
|
|
293 |
Rkns.Renderers.Paper.prototype.onMouseDrag = function(_event) { |
|
|
294 |
if (this.dragging_target && typeof this.dragging_target.paperShift === "function") { |
|
|
295 |
this.dragging_target.paperShift(_event.delta); |
|
|
296 |
} else { |
|
|
297 |
this.offset = this.offset.add(_event.delta); |
|
|
298 |
this.redraw(); |
|
|
299 |
} |
|
|
300 |
} |
|
|
301 |
|
|
4
|
302 |
Rkns.Renderers.Paper.prototype.onMouseUp = function(_event) { |
|
|
303 |
if (this.dragging_target && this.dragging_target.type === "temp-edge") { |
|
|
304 |
this.dragging_target.finishEdge(_event); |
|
|
305 |
} |
|
|
306 |
this.dragging_target = null; |
|
|
307 |
} |
|
|
308 |
|
|
3
|
309 |
Rkns.Renderers.Paper.prototype.onScroll = function(_event, _scrolldelta) { |
|
|
310 |
this.totalScroll += _scrolldelta; |
|
2
|
311 |
if (Math.abs(this.totalScroll) >= 1) { |
|
4
|
312 |
var _off = this.$.offset(), |
|
3
|
313 |
_delta = new paper.Point([ |
|
|
314 |
_event.pageX - _off.left, |
|
|
315 |
_event.pageY - _off.top |
|
|
316 |
]).subtract(this.offset).multiply( Math.SQRT2 - 1 ); |
|
2
|
317 |
if (this.totalScroll > 0) { |
|
3
|
318 |
this.offset = this.offset.subtract(_delta); |
|
2
|
319 |
this.scale *= Math.SQRT2; |
|
|
320 |
} else { |
|
3
|
321 |
this.offset = this.offset.add(_delta.divide( Math.SQRT2 )); |
|
2
|
322 |
this.scale *= Math.SQRT1_2; |
|
|
323 |
} |
|
|
324 |
this.totalScroll = 0; |
|
|
325 |
this.redraw(); |
|
|
326 |
} |
|
|
327 |
} |
|
4
|
328 |
|
|
|
329 |
Rkns.Renderers.Paper.prototype.onDoubleClick = function(_event) { |
|
|
330 |
var _off = this.$.offset(), |
|
|
331 |
_point = new paper.Point([ |
|
|
332 |
_event.pageX - _off.left, |
|
|
333 |
_event.pageY - _off.top |
|
|
334 |
]); |
|
|
335 |
var _hitResult = paper.project.hitTest(_point); |
|
|
336 |
if (!_hitResult || typeof _hitResult.item.__controller === "undefined") { |
|
|
337 |
var _coords = this.toModelCoords(_point); |
|
|
338 |
this._project.addNode({ |
|
|
339 |
position: { |
|
|
340 |
x: _coords.x, |
|
|
341 |
y: _coords.y |
|
|
342 |
} |
|
|
343 |
}, Rkns._RENDER_AND_SAVE); |
|
|
344 |
} |
|
|
345 |
paper.view.draw(); |
|
|
346 |
} |