|
1
|
1 |
Rkns.Renderers.Paper = Rkns.Utils.inherit(Rkns.Renderers._Base); |
|
|
2 |
|
|
5
|
3 |
Rkns.Renderers.Paper__Utils = { |
|
|
4 |
_EDITOR_ARROW_LENGTH : 20, |
|
|
5 |
_EDITOR_ARROW_WIDTH : 40, |
|
|
6 |
_EDITOR_MARGIN : 15, |
|
|
7 |
_EDITOR_PADDING : 10, |
|
6
|
8 |
_EDITOR_GRADIENT : new paper.Gradient(['#f0f0f0', '#d0d0d0']), |
|
5
|
9 |
drawEditBox : function(_coords, _path, _width, _height) { |
|
|
10 |
var _isLeft = (_coords.x < paper.view.center.x ? 1 : -1), |
|
|
11 |
_left = _coords.x + _isLeft * ( this._EDITOR_MARGIN + this._EDITOR_ARROW_LENGTH ), |
|
|
12 |
_right = _coords.x + _isLeft * ( this._EDITOR_MARGIN + this._EDITOR_ARROW_LENGTH + _width ), |
|
|
13 |
_top = _coords.y - _height / 2; |
|
|
14 |
if (_top < this._EDITOR_MARGIN) { |
|
|
15 |
_top = Math.min( this._EDITOR_MARGIN, _coords.y - this._EDITOR_ARROW_WIDTH / 2 ); |
|
|
16 |
} |
|
|
17 |
var _bottom = _top + _height; |
|
|
18 |
if (_bottom > (paper.view.size.height - this._EDITOR_MARGIN)) { |
|
|
19 |
_bottom = Math.max( paper.view.size.height - this._EDITOR_MARGIN, _coords.y + this._EDITOR_ARROW_WIDTH / 2 ); |
|
|
20 |
_top = _bottom - _height; |
|
|
21 |
} |
|
|
22 |
_path.segments[0].point |
|
|
23 |
= _path.segments[7].point |
|
|
24 |
= _coords.add([_isLeft * this._EDITOR_MARGIN, 0]); |
|
|
25 |
_path.segments[1].point.x |
|
|
26 |
= _path.segments[2].point.x |
|
|
27 |
= _path.segments[5].point.x |
|
|
28 |
= _path.segments[6].point.x |
|
|
29 |
= _left; |
|
|
30 |
_path.segments[3].point.x |
|
|
31 |
= _path.segments[4].point.x |
|
|
32 |
= _right; |
|
|
33 |
_path.segments[2].point.y |
|
|
34 |
= _path.segments[3].point.y |
|
|
35 |
= _top; |
|
|
36 |
_path.segments[4].point.y |
|
|
37 |
= _path.segments[5].point.y |
|
|
38 |
= _bottom; |
|
|
39 |
_path.segments[1].point.y = _coords.y - this._EDITOR_ARROW_WIDTH / 2; |
|
|
40 |
_path.segments[6].point.y = _coords.y + this._EDITOR_ARROW_WIDTH / 2; |
|
6
|
41 |
_path.fillColor = new paper.GradientColor(this._EDITOR_GRADIENT, [0,_top], [0, _bottom]) |
|
5
|
42 |
return { |
|
|
43 |
left: (this._EDITOR_PADDING + Math.min(_left, _right)), |
|
|
44 |
top: (this._EDITOR_PADDING + _top) |
|
|
45 |
} |
|
|
46 |
} |
|
|
47 |
} |
|
|
48 |
|
|
1
|
49 |
Rkns.Renderers.Paper__Controllers = {} |
|
|
50 |
|
|
|
51 |
Rkns.Renderers.Paper__Controllers._Base = function(_renderer, _element) { |
|
|
52 |
if (typeof _renderer !== "undefined") { |
|
4
|
53 |
this.id = Rkns.Utils.getUID('controller'); |
|
1
|
54 |
this._renderer = _renderer; |
|
5
|
55 |
this._project = _renderer._project; |
|
1
|
56 |
this._element = _element; |
|
2
|
57 |
this._element.__controller = this; |
|
1
|
58 |
} |
|
|
59 |
} |
|
|
60 |
|
|
4
|
61 |
Rkns.Renderers.Paper__Controllers._Base.prototype.select = function() {} |
|
|
62 |
|
|
|
63 |
Rkns.Renderers.Paper__Controllers._Base.prototype.unselect = function() {} |
|
|
64 |
|
|
5
|
65 |
Rkns.Renderers.Paper__Controllers._Base.prototype.destroy = function() {} |
|
|
66 |
|
|
1
|
67 |
Rkns.Renderers.Paper__Controllers.Node = Rkns.Utils.inherit(Rkns.Renderers.Paper__Controllers._Base); |
|
|
68 |
|
|
|
69 |
Rkns.Renderers.Paper__Controllers.Node.prototype._init = function() { |
|
2
|
70 |
this._renderer.node_layer.activate(); |
|
|
71 |
this.type = "node"; |
|
4
|
72 |
this.node_circle = new paper.Path.Circle([0, 0], Rkns._NODE_RADIUS); |
|
2
|
73 |
this.node_circle.fillColor = '#ffffff'; |
|
|
74 |
this.node_circle.__controller = this; |
|
|
75 |
this.node_text = new paper.PointText([0,0]); |
|
|
76 |
this.node_text.characterStyle = { |
|
4
|
77 |
fontSize: Rkns._NODE_FONT_SIZE, |
|
2
|
78 |
fillColor: 'black' |
|
|
79 |
}; |
|
|
80 |
this.node_text.paragraphStyle.justification = 'center'; |
|
|
81 |
this.node_text.__controller = this; |
|
1
|
82 |
} |
|
|
83 |
|
|
|
84 |
Rkns.Renderers.Paper__Controllers.Node.prototype.redraw = function() { |
|
3
|
85 |
this.node_model_coords = new paper.Point(this._element.position); |
|
2
|
86 |
this.node_paper_coords = this._renderer.toPaperCoords(this.node_model_coords); |
|
|
87 |
this.node_circle.position = this.node_paper_coords; |
|
|
88 |
this.node_text.content = this._element.title; |
|
4
|
89 |
this.node_text.position = this.node_paper_coords.add([0, 2 * Rkns._NODE_RADIUS]); |
|
1
|
90 |
this.node_circle.strokeColor = this._element.created_by.color; |
|
|
91 |
} |
|
|
92 |
|
|
2
|
93 |
Rkns.Renderers.Paper__Controllers.Node.prototype.paperShift = function(_delta) { |
|
5
|
94 |
var _coords = this._renderer.toModelCoords(this.node_paper_coords.add(_delta)), |
|
|
95 |
_data = { |
|
|
96 |
position: { |
|
|
97 |
x: _coords.x, |
|
|
98 |
y: _coords.y |
|
|
99 |
} |
|
|
100 |
}; |
|
|
101 |
this._project.updateElement(this._element, _data, Rkns._SAVE); |
|
2
|
102 |
this._renderer.redraw(); |
|
|
103 |
} |
|
|
104 |
|
|
4
|
105 |
Rkns.Renderers.Paper__Controllers.Node.prototype.select = function(_delta) { |
|
|
106 |
this.node_circle.strokeWidth = 3; |
|
|
107 |
} |
|
|
108 |
|
|
|
109 |
Rkns.Renderers.Paper__Controllers.Node.prototype.unselect = function(_delta) { |
|
|
110 |
this.node_circle.strokeWidth = 1; |
|
|
111 |
} |
|
|
112 |
|
|
2
|
113 |
/* */ |
|
|
114 |
|
|
|
115 |
Rkns.Renderers.Paper__Controllers.Edge = Rkns.Utils.inherit(Rkns.Renderers.Paper__Controllers._Base); |
|
|
116 |
|
|
|
117 |
Rkns.Renderers.Paper__Controllers.Edge.prototype._init = function() { |
|
|
118 |
this._renderer.edge_layer.activate(); |
|
|
119 |
this.type = "edge"; |
|
|
120 |
this.from_node_controller = this._element.from.__controller; |
|
|
121 |
this.to_node_controller = this._element.to.__controller; |
|
|
122 |
this.edge_line = new paper.Path(); |
|
|
123 |
this.edge_line.add([0,0],[0,0]); |
|
|
124 |
this.edge_line.__controller = this; |
|
4
|
125 |
this.edge_arrow = new paper.Path(); |
|
|
126 |
this.edge_arrow.add([0,0],[Rkns._ARROW_LENGTH,Rkns._ARROW_WIDTH / 2],[0,Rkns._ARROW_WIDTH]); |
|
|
127 |
this.edge_arrow.__controller = this; |
|
2
|
128 |
this.edge_text = new paper.PointText(); |
|
|
129 |
this.edge_text.characterStyle = { |
|
4
|
130 |
fontSize: Rkns._EDGE_FONT_SIZE, |
|
2
|
131 |
fillColor: 'black' |
|
|
132 |
}; |
|
|
133 |
this.edge_text.paragraphStyle.justification = 'center'; |
|
|
134 |
this.edge_text.__controller = this; |
|
4
|
135 |
this.edge_text_angle = 0; |
|
|
136 |
this.edge_arrow_angle = 0; |
|
2
|
137 |
} |
|
|
138 |
|
|
|
139 |
Rkns.Renderers.Paper__Controllers.Edge.prototype.redraw = function() { |
|
|
140 |
var _p0 = this.from_node_controller.node_paper_coords, |
|
|
141 |
_p1 = this.to_node_controller.node_paper_coords, |
|
4
|
142 |
_a = _p1.subtract(_p0).angle, |
|
|
143 |
_color = this._element.created_by.color; |
|
5
|
144 |
this.edge_paper_coords = _p0.add(_p1).divide(2); |
|
4
|
145 |
this.edge_line.strokeColor = _color; |
|
2
|
146 |
this.edge_line.segments[0].point = _p0; |
|
|
147 |
this.edge_line.segments[1].point = _p1; |
|
4
|
148 |
this.edge_arrow.rotate(_a - this.edge_arrow_angle); |
|
|
149 |
this.edge_arrow.fillColor = _color; |
|
5
|
150 |
this.edge_arrow.position = this.edge_paper_coords; |
|
4
|
151 |
this.edge_arrow_angle = _a; |
|
2
|
152 |
if (_a > 90) { |
|
|
153 |
_a -= 180; |
|
|
154 |
} |
|
|
155 |
if (_a < -90) { |
|
|
156 |
_a += 180; |
|
|
157 |
} |
|
4
|
158 |
this.edge_text.rotate(_a - this.edge_text_angle); |
|
|
159 |
this.edge_text.content = this._element.title; |
|
5
|
160 |
this.edge_text.position = this.edge_paper_coords; |
|
4
|
161 |
this.edge_text_angle = _a; |
|
|
162 |
} |
|
|
163 |
|
|
|
164 |
Rkns.Renderers.Paper__Controllers.Edge.prototype.select = function(_delta) { |
|
|
165 |
this.edge_line.strokeWidth = 3; |
|
|
166 |
} |
|
|
167 |
|
|
|
168 |
Rkns.Renderers.Paper__Controllers.Edge.prototype.unselect = function(_delta) { |
|
|
169 |
this.edge_line.strokeWidth = 1; |
|
2
|
170 |
} |
|
|
171 |
|
|
|
172 |
Rkns.Renderers.Paper__Controllers.Edge.prototype.paperShift = function(_delta) { |
|
|
173 |
this.from_node_controller.paperShift(_delta); |
|
|
174 |
this.to_node_controller.paperShift(_delta); |
|
|
175 |
this._renderer.redraw(); |
|
|
176 |
} |
|
5
|
177 |
|
|
4
|
178 |
/* */ |
|
|
179 |
|
|
|
180 |
Rkns.Renderers.Paper__Controllers.TempEdge = Rkns.Utils.inherit(Rkns.Renderers.Paper__Controllers._Base); |
|
|
181 |
|
|
|
182 |
Rkns.Renderers.Paper__Controllers.TempEdge.prototype._init = function() { |
|
|
183 |
this._renderer.edge_layer.activate(); |
|
|
184 |
this.type = "temp-edge"; |
|
5
|
185 |
var _color = this._project.current_user.color; |
|
4
|
186 |
this.edge_line = new paper.Path(); |
|
|
187 |
this.edge_line.strokeColor = _color; |
|
|
188 |
this.edge_line.add([0,0],[0,0]); |
|
|
189 |
this.edge_line.__controller = this; |
|
|
190 |
this.edge_arrow = new paper.Path(); |
|
|
191 |
this.edge_arrow.fillColor = _color; |
|
|
192 |
this.edge_arrow.add([0,0],[Rkns._ARROW_LENGTH,Rkns._ARROW_WIDTH / 2],[0,Rkns._ARROW_WIDTH]); |
|
|
193 |
this.edge_arrow.__controller = this; |
|
|
194 |
this.edge_arrow_angle = 0; |
|
|
195 |
} |
|
|
196 |
|
|
|
197 |
Rkns.Renderers.Paper__Controllers.TempEdge.prototype.redraw = function() { |
|
|
198 |
var _p0 = this.from_node_controller.node_paper_coords, |
|
|
199 |
_p1 = this.end_pos, |
|
|
200 |
_a = _p1.subtract(_p0).angle, |
|
|
201 |
_c = _p0.add(_p1).divide(2); |
|
|
202 |
this.edge_line.segments[0].point = _p0; |
|
|
203 |
this.edge_line.segments[1].point = _p1; |
|
|
204 |
this.edge_arrow.rotate(_a - this.edge_arrow_angle); |
|
|
205 |
this.edge_arrow.position = _c; |
|
|
206 |
this.edge_arrow_angle = _a; |
|
|
207 |
} |
|
|
208 |
|
|
|
209 |
Rkns.Renderers.Paper__Controllers.TempEdge.prototype.paperShift = function(_delta) { |
|
|
210 |
this.end_pos = this.end_pos.add(_delta); |
|
|
211 |
this._renderer.onMouseMove({point: this.end_pos}); |
|
|
212 |
this.redraw(); |
|
|
213 |
} |
|
|
214 |
|
|
|
215 |
Rkns.Renderers.Paper__Controllers.TempEdge.prototype.finishEdge = function(_event) { |
|
|
216 |
var _hitResult = paper.project.hitTest(_event.point); |
|
|
217 |
if (_hitResult && typeof _hitResult.item.__controller !== "undefined") { |
|
|
218 |
var _target = _hitResult.item.__controller; |
|
|
219 |
if (_target.type === "node" && this.from_node_controller._element.id !== _target._element.id) { |
|
5
|
220 |
this._project.addEdge({ |
|
4
|
221 |
from: this.from_node_controller._element.id, |
|
|
222 |
to: _target._element.id |
|
|
223 |
}, Rkns._RENDER_AND_SAVE) |
|
|
224 |
} |
|
|
225 |
} |
|
5
|
226 |
this._renderer.removeController(this); |
|
|
227 |
} |
|
|
228 |
|
|
|
229 |
Rkns.Renderers.Paper__Controllers.TempEdge.prototype.destroy = function() { |
|
4
|
230 |
this.edge_arrow.remove(); |
|
|
231 |
this.edge_line.remove(); |
|
5
|
232 |
} |
|
|
233 |
|
|
|
234 |
/* */ |
|
|
235 |
|
|
|
236 |
Rkns.Renderers.Paper__Controllers.NodeEditor = Rkns.Utils.inherit(Rkns.Renderers.Paper__Controllers._Base); |
|
|
237 |
|
|
|
238 |
Rkns.Renderers.Paper__Controllers.NodeEditor.prototype._init = function() { |
|
|
239 |
this._renderer.overlay_layer.activate(); |
|
|
240 |
this.type = "editor"; |
|
|
241 |
this.editor_block = new paper.Path(); |
|
|
242 |
var _pts = Rkns._(Rkns._.range(8)).map(function() {return [0,0]}); |
|
|
243 |
this.editor_block.add.apply(this.editor_block, _pts); |
|
|
244 |
this.editor_block.strokeWidth = 2; |
|
|
245 |
this.editor_block.strokeColor = "#999999"; |
|
|
246 |
this.editor_block.fillColor = "#e0e0e0"; |
|
|
247 |
this.editor_block.opacity = .8; |
|
|
248 |
this.editor_$ = Rkns.$('<div>') |
|
|
249 |
.appendTo('.Rk-Editor') |
|
|
250 |
.css({ |
|
|
251 |
position: "absolute", |
|
|
252 |
opacity: .8 |
|
|
253 |
}) |
|
|
254 |
.hide(); |
|
|
255 |
} |
|
|
256 |
|
|
|
257 |
Rkns.Renderers.Paper__Controllers.NodeEditor.prototype.template = Rkns._.template( |
|
|
258 |
'<h2><span class="Rk-CloseX">×</span><%=l10n.edit_node%></span></h2>' |
|
|
259 |
+ '<p><label><%=l10n.edit_title%></label><input class="Rk-Edit-Title" type="text" value="<%=node.title%>"/></p>' |
|
|
260 |
+ '<p><label><%=l10n.edit_uri%></label><input class="Rk-Edit-URI" type="text" value="<%=node.uri%>"/></p>' |
|
|
261 |
+ '<p><label><%=l10n.edit_description%></label><textarea class="Rk-Edit-Description"><%=node.description%></textarea></p>' |
|
|
262 |
+ '<p><label><%=l10n.created_by%></label> <span class="Rk-UserColor" style="background:<%=node.created_by.color%>;"></span> <%=node.created_by.title%></p>' |
|
|
263 |
); |
|
|
264 |
|
|
|
265 |
Rkns.Renderers.Paper__Controllers.NodeEditor.prototype.redraw = function() { |
|
|
266 |
var _coords = this.node_controller.node_paper_coords, |
|
|
267 |
_element = this.node_controller._element, |
|
|
268 |
_css = Rkns.Renderers.Paper__Utils.drawEditBox(_coords, this.editor_block, 250, 300); |
|
|
269 |
this.editor_$ |
|
|
270 |
.html(this.template({ |
|
|
271 |
node: _element, |
|
|
272 |
l10n: this._project.l10n |
|
|
273 |
})) |
|
|
274 |
.show() |
|
|
275 |
.css(_css); |
|
|
276 |
var _this = this; |
|
|
277 |
this.editor_$.find(".Rk-CloseX").click(function() { |
|
|
278 |
_this._renderer.removeController(_this); |
|
|
279 |
paper.view.draw(); |
|
|
280 |
}); |
|
|
281 |
this.editor_$.find("input, textarea").bind("keyup change", function() { |
|
|
282 |
var _data = { |
|
|
283 |
title: _this.editor_$.find(".Rk-Edit-Title").val(), |
|
|
284 |
description: _this.editor_$.find(".Rk-Edit-Description").val(), |
|
|
285 |
uri: _this.editor_$.find(".Rk-Edit-URI").val() |
|
|
286 |
} |
|
|
287 |
_this._project.updateElement( |
|
|
288 |
_element, |
|
|
289 |
_data, |
|
|
290 |
Rkns._SAVE |
|
|
291 |
); |
|
|
292 |
_this.node_controller.redraw(); |
|
|
293 |
paper.view.draw(); |
|
|
294 |
}); |
|
|
295 |
} |
|
|
296 |
|
|
|
297 |
Rkns.Renderers.Paper__Controllers.NodeEditor.prototype.destroy = function() { |
|
|
298 |
this.editor_block.remove(); |
|
|
299 |
this.editor_$.detach(); |
|
|
300 |
} |
|
|
301 |
|
|
|
302 |
|
|
|
303 |
/* */ |
|
|
304 |
|
|
|
305 |
Rkns.Renderers.Paper__Controllers.EdgeEditor = Rkns.Utils.inherit(Rkns.Renderers.Paper__Controllers._Base); |
|
|
306 |
|
|
|
307 |
Rkns.Renderers.Paper__Controllers.EdgeEditor.prototype._init = function() { |
|
|
308 |
this._renderer.overlay_layer.activate(); |
|
|
309 |
this.type = "editor"; |
|
|
310 |
this.editor_block = new paper.Path(); |
|
|
311 |
var _pts = Rkns._(Rkns._.range(8)).map(function() {return [0,0]}); |
|
|
312 |
this.editor_block.add.apply(this.editor_block, _pts); |
|
|
313 |
this.editor_block.strokeWidth = 2; |
|
|
314 |
this.editor_block.strokeColor = "#999999"; |
|
|
315 |
this.editor_block.fillColor = "#e0e0e0"; |
|
|
316 |
this.editor_block.opacity = .8; |
|
|
317 |
this.editor_$ = Rkns.$('<div>') |
|
|
318 |
.appendTo('.Rk-Editor') |
|
|
319 |
.css({ |
|
|
320 |
position: "absolute", |
|
|
321 |
opacity: .8 |
|
|
322 |
}) |
|
|
323 |
.hide(); |
|
|
324 |
} |
|
|
325 |
|
|
|
326 |
Rkns.Renderers.Paper__Controllers.EdgeEditor.prototype.template = Rkns._.template( |
|
|
327 |
'<h2><span class="Rk-CloseX">×</span><%=l10n.edit_edge%></span></h2>' |
|
|
328 |
+ '<p><label><%=l10n.edit_title%></label><input class="Rk-Edit-Title" type="text" value="<%=edge.title%>"/></p>' |
|
|
329 |
+ '<p><label><%=l10n.edit_uri%></label><input class="Rk-Edit-URI" type="text" value="<%=edge.uri%>"/></p>' |
|
|
330 |
+ '<p><label><%=l10n.edit_from%></label><span class="Rk-UserColor" style="background:<%=edge.from.created_by.color%>;"></span><%=edge.from.title%></p>' |
|
|
331 |
+ '<p><label><%=l10n.edit_to%></label><span class="Rk-UserColor" style="background:<%=edge.to.created_by.color%>;"></span><%=edge.to.title%></p>' |
|
|
332 |
+ '<p><label><%=l10n.created_by%> </label><span class="Rk-UserColor" style="background:<%=edge.created_by.color%>;"></span> <%=edge.created_by.title%></p>' |
|
|
333 |
); |
|
|
334 |
|
|
|
335 |
Rkns.Renderers.Paper__Controllers.EdgeEditor.prototype.redraw = function() { |
|
|
336 |
var _coords = this.edge_controller.edge_paper_coords, |
|
|
337 |
_element = this.edge_controller._element, |
|
|
338 |
_css = Rkns.Renderers.Paper__Utils.drawEditBox(_coords, this.editor_block, 250, 200); |
|
|
339 |
this.editor_$ |
|
|
340 |
.html(this.template({ |
|
|
341 |
edge: _element, |
|
|
342 |
l10n: this._project.l10n |
|
|
343 |
})) |
|
|
344 |
.show() |
|
|
345 |
.css(_css); |
|
|
346 |
var _this = this; |
|
|
347 |
this.editor_$.find(".Rk-CloseX").click(function() { |
|
|
348 |
_this._renderer.removeController(_this); |
|
|
349 |
paper.view.draw(); |
|
|
350 |
}); |
|
|
351 |
this.editor_$.find("input, textarea").bind("keyup change", function() { |
|
|
352 |
var _data = { |
|
|
353 |
title: _this.editor_$.find(".Rk-Edit-Title").val(), |
|
|
354 |
uri: _this.editor_$.find(".Rk-Edit-URI").val() |
|
|
355 |
} |
|
|
356 |
_this._project.updateElement( |
|
|
357 |
_element, |
|
|
358 |
_data, |
|
|
359 |
Rkns._SAVE |
|
|
360 |
); |
|
|
361 |
_this.edge_controller.redraw(); |
|
|
362 |
paper.view.draw(); |
|
|
363 |
}); |
|
|
364 |
} |
|
|
365 |
|
|
|
366 |
Rkns.Renderers.Paper__Controllers.EdgeEditor.prototype.destroy = function() { |
|
|
367 |
this.editor_block.remove(); |
|
|
368 |
this.editor_$.detach(); |
|
4
|
369 |
} |
|
2
|
370 |
|
|
|
371 |
/* */ |
|
1
|
372 |
|
|
|
373 |
Rkns.Renderers.Paper.prototype._init = function() { |
|
4
|
374 |
this._MARGIN_X = 80; |
|
|
375 |
this._MARGIN_Y = 50; |
|
|
376 |
var _canvas_id = this._project._opts.canvas_id; |
|
|
377 |
this.$ = Rkns.$("#"+_canvas_id) |
|
|
378 |
paper.setup(document.getElementById(_canvas_id)); |
|
2
|
379 |
this.scale = 1; |
|
|
380 |
this.offset = paper.view.center; |
|
|
381 |
this.totalScroll = 0; |
|
5
|
382 |
this.click_target = null; |
|
4
|
383 |
this.selected_target = null; |
|
2
|
384 |
this.edge_layer = new paper.Layer(); |
|
|
385 |
this.node_layer = new paper.Layer(); |
|
5
|
386 |
this.overlay_layer = new paper.Layer(); |
|
2
|
387 |
var _tool = new paper.Tool(), |
|
|
388 |
_this = this; |
|
5
|
389 |
_tool.minDistance = Rkns._MIN_DRAG_DISTANCE; |
|
4
|
390 |
_tool.onMouseMove = function(_event) { |
|
|
391 |
_this.onMouseMove(_event); |
|
|
392 |
} |
|
2
|
393 |
_tool.onMouseDown = function(_event) { |
|
|
394 |
_this.onMouseDown(_event); |
|
|
395 |
} |
|
|
396 |
_tool.onMouseDrag = function(_event) { |
|
|
397 |
_this.onMouseDrag(_event); |
|
|
398 |
} |
|
4
|
399 |
_tool.onMouseUp = function(_event) { |
|
|
400 |
_this.onMouseUp(_event); |
|
|
401 |
} |
|
|
402 |
this.$.mousewheel(function(_event, _delta) { |
|
2
|
403 |
_this.onScroll(_event, _delta); |
|
|
404 |
}) |
|
4
|
405 |
this.$.dblclick(function(_event) { |
|
|
406 |
_this.onDoubleClick(_event); |
|
|
407 |
}) |
|
2
|
408 |
paper.view.onResize = function(_event) { |
|
|
409 |
_this.offset = _this.offset.add(_event.delta.divide(2)); |
|
|
410 |
_this.redraw(); |
|
|
411 |
} |
|
|
412 |
} |
|
|
413 |
|
|
|
414 |
Rkns.Renderers.Paper.prototype.toPaperCoords = function(_point) { |
|
|
415 |
return _point.multiply(this.scale).add(this.offset); |
|
|
416 |
} |
|
|
417 |
|
|
|
418 |
|
|
|
419 |
Rkns.Renderers.Paper.prototype.toModelCoords = function(_point) { |
|
|
420 |
return _point.subtract(this.offset).divide(this.scale); |
|
1
|
421 |
} |
|
|
422 |
|
|
|
423 |
Rkns.Renderers.Paper.prototype.draw = function() { |
|
2
|
424 |
var _this = this, |
|
|
425 |
_xx = this._project.nodes.map(function(_node) { return _node.position.x }), |
|
|
426 |
_yy = this._project.nodes.map(function(_node) { return _node.position.y }), |
|
|
427 |
_minx = Math.min.apply(Math, _xx), |
|
|
428 |
_miny = Math.min.apply(Math, _yy), |
|
|
429 |
_maxx = Math.max.apply(Math, _xx), |
|
|
430 |
_maxy = Math.max.apply(Math, _yy); |
|
4
|
431 |
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
|
432 |
this.offset = paper.view.center.subtract(new paper.Point([(_maxx + _minx) / 2, (_maxy + _miny) / 2]).multiply(this.scale)); |
|
4
|
433 |
this.controllers = new Rkns.Model.List(); |
|
|
434 |
this._project.nodes.forEach(function(_node) { |
|
5
|
435 |
_this.addController("Node", _node); |
|
1
|
436 |
}); |
|
4
|
437 |
this._project.edges.forEach(function(_edge) { |
|
5
|
438 |
_this.addController("Edge", _edge); |
|
2
|
439 |
}); |
|
|
440 |
|
|
|
441 |
this.redraw(); |
|
1
|
442 |
} |
|
|
443 |
|
|
5
|
444 |
Rkns.Renderers.Paper.prototype.addController = function(_type, _controller) { |
|
|
445 |
var _el = new Rkns.Renderers.Paper__Controllers[_type](this, _controller); |
|
4
|
446 |
this.controllers.push(_el); |
|
|
447 |
return _el; |
|
|
448 |
} |
|
|
449 |
|
|
5
|
450 |
Rkns.Renderers.Paper.prototype.removeController = function(_controller) { |
|
|
451 |
_controller.destroy(); |
|
|
452 |
this.controllers.removeId(_controller); |
|
|
453 |
} |
|
|
454 |
|
|
|
455 |
Rkns.Renderers.Paper.prototype.removeControllersOfType = function(_type) { |
|
|
456 |
var _controllers = this.controllers.filter(function(_ctrl) { |
|
|
457 |
return _ctrl.type == _type; |
|
|
458 |
}), |
|
|
459 |
_this = this; |
|
|
460 |
_controllers.forEach(function(_ctrl) { |
|
|
461 |
_this.removeController(_ctrl); |
|
|
462 |
}); |
|
|
463 |
} |
|
|
464 |
|
|
1
|
465 |
Rkns.Renderers.Paper.prototype.redraw = function() { |
|
4
|
466 |
this.controllers.forEach(function(_controller) { |
|
|
467 |
_controller.redraw(); |
|
2
|
468 |
}); |
|
|
469 |
paper.view.draw(); |
|
|
470 |
} |
|
|
471 |
|
|
4
|
472 |
Rkns.Renderers.Paper.prototype.onMouseMove = function(_event) { |
|
|
473 |
var _hitResult = paper.project.hitTest(_event.point); |
|
|
474 |
if (_hitResult && typeof _hitResult.item.__controller !== "undefined") { |
|
|
475 |
if (this.selected_target !== _hitResult.item.__controller) { |
|
|
476 |
if (this.selected_target) { |
|
|
477 |
this.selected_target.unselect(); |
|
|
478 |
} |
|
|
479 |
this.selected_target = _hitResult.item.__controller; |
|
|
480 |
this.selected_target.select(); |
|
|
481 |
} |
|
|
482 |
} else { |
|
|
483 |
if (this.selected_target) { |
|
|
484 |
this.selected_target.unselect(); |
|
|
485 |
} |
|
|
486 |
this.selected_target = null; |
|
|
487 |
} |
|
|
488 |
} |
|
|
489 |
|
|
2
|
490 |
Rkns.Renderers.Paper.prototype.onMouseDown = function(_event) { |
|
5
|
491 |
this.is_dragging = false; |
|
2
|
492 |
var _hitResult = paper.project.hitTest(_event.point); |
|
|
493 |
if (_hitResult && typeof _hitResult.item.__controller !== "undefined") { |
|
5
|
494 |
this.click_target = _hitResult.item.__controller; |
|
|
495 |
if (this.click_target.type === "node" && _hitResult.type === "stroke") { |
|
|
496 |
var _tmpEdge = this.addController("TempEdge",{}); |
|
4
|
497 |
_tmpEdge.end_pos = _event.point; |
|
5
|
498 |
_tmpEdge.from_node_controller = this.click_target; |
|
4
|
499 |
_tmpEdge.redraw(); |
|
5
|
500 |
this.click_target = _tmpEdge; |
|
4
|
501 |
} |
|
2
|
502 |
} else { |
|
5
|
503 |
this.click_target = null; |
|
2
|
504 |
} |
|
1
|
505 |
} |
|
2
|
506 |
|
|
|
507 |
Rkns.Renderers.Paper.prototype.onMouseDrag = function(_event) { |
|
5
|
508 |
this.is_dragging = true; |
|
|
509 |
if (this.click_target && typeof this.click_target.paperShift === "function") { |
|
|
510 |
this.click_target.paperShift(_event.delta); |
|
2
|
511 |
} else { |
|
|
512 |
this.offset = this.offset.add(_event.delta); |
|
|
513 |
this.redraw(); |
|
|
514 |
} |
|
|
515 |
} |
|
|
516 |
|
|
4
|
517 |
Rkns.Renderers.Paper.prototype.onMouseUp = function(_event) { |
|
5
|
518 |
if (this.click_target) { |
|
|
519 |
switch (this.click_target.type) { |
|
|
520 |
case "node": |
|
|
521 |
if (!this.is_dragging) { |
|
|
522 |
this.removeControllersOfType("editor"); |
|
|
523 |
var _editor = this.addController("NodeEditor",{}); |
|
|
524 |
_editor.node_controller = this.click_target; |
|
|
525 |
_editor.redraw(); |
|
|
526 |
} |
|
|
527 |
break; |
|
|
528 |
case "edge": |
|
|
529 |
if (!this.is_dragging) { |
|
|
530 |
this.removeControllersOfType("editor"); |
|
|
531 |
var _editor = this.addController("EdgeEditor",{}); |
|
|
532 |
_editor.edge_controller = this.click_target; |
|
|
533 |
_editor.redraw(); |
|
|
534 |
} |
|
|
535 |
break; |
|
|
536 |
case "temp-edge": |
|
|
537 |
this.click_target.finishEdge(_event); |
|
|
538 |
break; |
|
|
539 |
} |
|
4
|
540 |
} |
|
5
|
541 |
this.is_dragging = false; |
|
|
542 |
this.click_target = null; |
|
4
|
543 |
} |
|
|
544 |
|
|
3
|
545 |
Rkns.Renderers.Paper.prototype.onScroll = function(_event, _scrolldelta) { |
|
|
546 |
this.totalScroll += _scrolldelta; |
|
2
|
547 |
if (Math.abs(this.totalScroll) >= 1) { |
|
4
|
548 |
var _off = this.$.offset(), |
|
3
|
549 |
_delta = new paper.Point([ |
|
|
550 |
_event.pageX - _off.left, |
|
|
551 |
_event.pageY - _off.top |
|
|
552 |
]).subtract(this.offset).multiply( Math.SQRT2 - 1 ); |
|
2
|
553 |
if (this.totalScroll > 0) { |
|
3
|
554 |
this.offset = this.offset.subtract(_delta); |
|
2
|
555 |
this.scale *= Math.SQRT2; |
|
|
556 |
} else { |
|
3
|
557 |
this.offset = this.offset.add(_delta.divide( Math.SQRT2 )); |
|
2
|
558 |
this.scale *= Math.SQRT1_2; |
|
|
559 |
} |
|
|
560 |
this.totalScroll = 0; |
|
|
561 |
this.redraw(); |
|
|
562 |
} |
|
|
563 |
} |
|
4
|
564 |
|
|
|
565 |
Rkns.Renderers.Paper.prototype.onDoubleClick = function(_event) { |
|
|
566 |
var _off = this.$.offset(), |
|
|
567 |
_point = new paper.Point([ |
|
|
568 |
_event.pageX - _off.left, |
|
|
569 |
_event.pageY - _off.top |
|
|
570 |
]); |
|
|
571 |
var _hitResult = paper.project.hitTest(_point); |
|
|
572 |
if (!_hitResult || typeof _hitResult.item.__controller === "undefined") { |
|
|
573 |
var _coords = this.toModelCoords(_point); |
|
|
574 |
this._project.addNode({ |
|
|
575 |
position: { |
|
|
576 |
x: _coords.x, |
|
|
577 |
y: _coords.y |
|
|
578 |
} |
|
|
579 |
}, Rkns._RENDER_AND_SAVE); |
|
|
580 |
} |
|
|
581 |
paper.view.draw(); |
|
|
582 |
} |