1 # Copyright 2015 Jason Woofenden
3 # This program is free software: you can redistribute it and/or modify it under
4 # the terms of the GNU General Public License as published by the Free Software
5 # Foundation, either version 3 of the License, or (at your option) any later
8 # This program is distributed in the hope that it will be useful, but WITHOUT
9 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 # You should have received a copy of the GNU General Public License along with
14 # this program. If not, see <http://www.gnu.org/licenses/>.
21 CLICK_FUZ = 10 # this far away from things is close enough to be "clicked on"
22 PROX_MAX = CLICK_FUZ * CLICK_FUZ
23 PROX_TOO_FAR = PROX_MAX + 1 # no need to be precice when it's too far
27 NORMAL: { txt: 'normal' }
28 SELECTED: { txt: 'selected' }
29 DRAGGING: { txt: 'dragging' }
30 EDITING: { txt: 'editing' }
35 # json (compiled to javascript and minified) is ~8% smaller than the resulting xml
36 json_to_svg = (json) ->
37 for tag, attrs of json
38 el = document.createElementNS 'http://www.w3.org/2000/svg', tag
42 el.appendChild json_to_svg child
43 else if k is 'contents'
44 el.appendChild document.createTextNode v
49 resizer_nw = (widget) ->
51 widget.resize w: widget.width - dxy.x, h: widget.height - dxy.y
52 widget.move x: widget.x + dxy.x, y: widget.y + dxy.y
53 resizer_n = (widget) ->
55 widget.resize w: widget.width, h: widget.height - dxy.y
56 widget.move x: widget.x, y: widget.y + dxy.y
57 resizer_ne = (widget) ->
59 widget.resize w: widget.width + dxy.x, h: widget.height - dxy.y
60 widget.move x: widget.x, y: widget.y + dxy.y
61 resizer_e = (widget) ->
63 widget.resize w: widget.width + dxy.x, h: widget.height
64 resizer_se = (widget) ->
66 widget.resize w: widget.width + dxy.x, h: widget.height + dxy.y
67 resizer_s = (widget) ->
69 widget.resize w: widget.width, h: widget.height + dxy.y
70 resizer_sw = (widget) ->
72 widget.resize w: widget.width - dxy.x, h: widget.height + dxy.y
73 widget.move x: widget.x + dxy.x, y: widget.y
74 resizer_w = (widget) ->
76 widget.resize w: widget.width - dxy.x, h: widget.height
77 widget.move x: widget.x + dxy.x, y: widget.y
89 -> return "M#{@x - 5} #{@y - 5}h6l-2 2 4 4 2 -2v6h-6l2-2-4-4-2 2z"
90 -> return "M #{@x},#{@y - 7} l 4,4 -2.5,0 0,5 2.5,0 -4,4 -4,-4 2.5,0 0,-5 -2.5,0 z"
91 -> return "M#{@x + 5} #{@y - 5}v6l-2-2-4 4 2 2h-6v-6l2 2 4-4-2-2z"
92 -> return "M #{@x - 7},#{@y} l 4,-4 0,2.5 5,0 0,-2.5 4,4 -4,4 0,-2.5 -5,0 0,2.5 z"
94 shape_node_move = -> "M#{@x} #{@y - 9}l-2.5 4.5h2v2.404a2.156 2.156 0 0 0-1.596 1.596h-2.404v-2l-4.5 2.5 4.5 2.5v-2h2.404a2.156 2.156 0 0 0 1.596 1.596v2.404h-2l2.5 4.5 2.5-4.5h-2v-2.404a2.156 2.156 0 0 0 1.596-1.596h2.404v2l4.5-2.5-4.5-2.5v2h-2.404a2.156 2.156 0 0 0-1.596-1.596v-2.404h2l-2.5-4.5z"
97 # public vars: x, y, width, height, el
100 constructor: (args) ->
106 @width = args.width ? 50
107 @height = args.height ? 34
108 @state = args.state ? STATES.NORMAL
112 css_class = "#{@css_class} #{@state.txt}"
114 css_class += " hover"
115 @el.setAttribute 'class', css_class
120 move: (xy) -> # just move
123 drag: (dxy) -> # react to mouse drag (obey constraints, etc.)
124 @move x: @x + dxy.x, y: @y + dxy.y
125 proximity: (xy) -> # return the square of the distance to your visible bits
127 set_state: (state) ->
130 class Control extends Visible
131 constructor: (args) ->
135 @on_destruct = args.done ? null
140 drag: (args) -> # call this when control point is being manipulated directly
142 proximity: (xy) -> # return the square of the distance to your visible bits
145 return dx * dx + dy * dy
146 class ControlPath extends Control
147 constructor: (args) ->
149 @css_class = 'control_point'
150 @make_path = args.shape
151 @el = json_to_svg path:
153 class: 'control_point normal'
161 @el.setAttribute 'd', @make_path()
163 class Widget extends Visible
164 #sub-classes are expected to implement all of these:
165 constructor: (args) ->
173 make_controls: -> # create controls, return them
180 move: (xy) -> # just move
185 c.move x: c.x + dx, y: c.y + dy
186 set_state: (state) ->
187 return if @state is state
188 if @state is STATES.EDITING
192 class RectWidget extends Widget
193 constructor: (args) ->
196 @el = json_to_svg rect:
208 return new RectWidget @
209 set_state: (state) ->
214 @el.setAttribute 'x', @x + 1
215 @el.setAttribute 'y', @y + 1
216 @reposition_controls()
217 proximity: (xy) -> # return the square of the distance to your visible bits
223 if x > @x - CLICK_FUZ and x < @x + @width + CLICK_FUZ
225 if y < @y + @height / 2
228 new_prox = @y + @height - y
232 if y > @y - CLICK_FUZ and y < @y + @height + CLICK_FUZ
234 if x < @x + @width / 2
237 new_prox = @x + @width - x
241 if in_x and in_y and prox > PROX_MAX
248 @el.setAttribute 'width', @width - 2
250 @el.setAttribute 'height', @height - 2
251 @reposition_controls()
252 reposition_controls: ->
253 if @controls.length > 1
254 positions = @control_positions()
255 for i in [0...positions.length]
256 @controls[i].move x: positions[i].x, y: positions[i].y
257 control_positions: ->
260 w2p = Math.floor(@width / 2) + 0.5
261 h2p = Math.floor(@height / 2) + 0.5
263 { x: @x - gap, y: @y - gap }
264 { x: @x + w2p, y: @y - mgap }
265 { x: @x + @width + gap, y: @y - gap }
266 { x: @x + @width + mgap, y: @y + h2p }
267 { x: @x + @width + gap, y: @y + @height + gap }
268 { x: @x + w2p, y: @y + @height + mgap }
269 { x: @x - gap, y: @y + @height + gap }
270 { x: @x - mgap, y: @y + h2p }
272 make_controls: (args) -> # create controls, return them
273 if @controls.length > 0
275 console.log "warning: re-adding controls"
277 positions = @control_positions()
278 for i in [0...positions.length]
279 @controls.push new ControlPath {
285 shape: resizer_shapes[i % resizer_shapes.length]
289 class PolylineWidget extends Widget
290 constructor: (args) ->
292 @css_class = 'polyline'
295 @nodes.push x: n.x, y: n.y
296 @el = json_to_svg path:
298 class: 'polyline normal'
305 return new PolylineWidget @
317 set_state: (state) ->
322 @el.setAttribute 'd', @my_path_d()
323 @reposition_controls()
324 proximity: (xy) -> # return the square of the distance to your visible bits
329 p = dx * dx + dy * dy
333 l1x = @x + @nodes[i-1].x
334 l1y = @y + @nodes[i-1].y
335 l2x = @x + @nodes[i].x
336 l2y = @y + @nodes[i].y
339 if ldx is 0 # vertical line
340 if (xy.y < l1y) is (xy.y < l2y)
344 else if ldy is 0 # horizontal line
345 if (xy.x < l1x) is (xy.x < l2x)
350 # https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line
353 c = l2x * l1y - l2y * l1x
354 y_on_line = (a * (a * xy.y - b * xy.x) - b * c) / (a * a + b * b)
355 if (y_on_line < l1y) is (y_on_line < l2y)
357 p = (a * xy.x + b * xy.y + c) / Math.sqrt(a * a + b * b)
364 # FIXME (apply to more than just 2nd node)
369 @el.setAttribute 'd', @my_path_d()
370 @reposition_controls()
375 for i in [1...@nodes.length]
378 @move x: @x + dxy.x, y: @y + dxy.y
382 @el.setAttribute 'd', @my_path_d()
383 @reposition_controls()
384 reposition_controls: ->
385 if @controls.length > 1
386 positions = @control_positions()
387 for i in [0...positions.length]
388 @controls[i].move x: positions[i].x, y: positions[i].y
390 control_positions: ->
393 ret.push x: @x + n.x, y: @y + n.y
395 make_controls: (args) -> # create controls, return them
396 console.log 'make line controls'
397 if @controls.length > 0
399 console.log "warning: re-adding controls"
401 positions = @control_positions()
402 for i in [0...positions.length]
403 @controls.push new ControlPath {
408 drag: @node_dragger i
409 shape: shape_node_move
413 # called automatically on domcontentloaded
416 $container = $ '.crayon_mockup'
417 svg = json_to_svg svg: width: width, height: height, viewBox: "0 0 #{width} #{height}"
419 $container.append $svg
420 svg.appendChild json_to_svg filter:
421 id: 'crayon', filterUnits: 'userSpaceOnUse'
422 x: '-5%', y: '-5%', height: '110%', width: '110%'
424 { feTurbulence: baseFrequency: '.3', numOctaves: '2', type: 'fractalNoise' }
425 { feDisplacementMap: scale: '6', xChannelSelector: 'R', in: 'SourceGraphic' }
427 svg.appendChild json_to_svg style:
429 contents: '.box.normal,.polyline.normal{filter: url(#crayon)}'
431 # create canvas border
432 svg.appendChild json_to_svg rect:
436 height: height - 2 - supply_height
437 class: 'canvas_border'
441 supply_add = (type, args) ->
444 args.x += 30 + supply_count * 90
445 args.y += (supply_height - 50) / 2
450 supply_add RectWidget, width: 50, height: 50
451 supply_add PolylineWidget, y: 25, nodes: [{x: 0, y: 0}, {x: 50, y: 0}]
452 supply_add PolylineWidget, x: 25, nodes: [{x: 0, y: 0}, {x: 0, y: 50}]
453 supply_add PolylineWidget, x: 10, nodes: [{x: 0, y: 0}, {x: 15, y: 50}, {x: 30, y: 0}]
454 supply_add PolylineWidget, x: 0, nodes: [{x: 0, y: 50}, {x: 17, y: 0}, {x: 33, y: 50}, {x: 50, y: 0}]
457 controls_layer = { all: {}, selected: {} }
458 widget_layer = { all: {}, selected: {}, editing: null }
459 layers = [controls_layer, widget_layer]
460 hovered = null # can be in any layer
461 dragging = false # mouse state
463 drag_from = x: 0, y: 0 # mouse was here at last frame of drag
464 shift_key_down = false
467 if widget_layer.editing
468 widget_layer.editing.kill_controls()
469 widget_layer.editing = null
470 deselect = (layer, s) ->
471 return unless layer.selected[s.id]?
472 s.set_state STATES.NORMAL
473 delete layer.selected[s.id]
474 if widget_layer.editing is s
475 widget_layer.editing = null
477 deselect_all = (layer, except = null) ->
478 for id, s of layer.selected
481 _select = (layer, s) -> # don't call this directly, use select_only() or select_also()
482 s.set_state STATES.SELECTED
483 layer.selected[s.id] = s
485 select_only = (layer, s) ->
486 deselect_all layer, s
487 return if layer.selected[s.id]?
490 select_also = (layer, s) ->
491 return if layer.selected[s.id]?
492 if layer is widget_layer
496 find_closest = (widgets, xy) ->
500 new_prox = w.proximity xy
507 svg_event_to_xy = (e) ->
509 svg_offset = $svg.offset()
511 x: Math.round(e.pageX - svg_offset.left)
512 y: Math.round(e.pageY - svg_offset.top)
514 closest_in_layers = (xy) ->
516 s = find_closest layer.selected, xy
517 return layer: layer, s: s if s?
518 s = find_closest layer.all, xy
519 return layer: layer, s: s if s?
526 if dragging # two mousedowns in a row?! it happens
528 xy = svg_event_to_xy e
529 if xy.y < supply_height
530 s = find_closest supply, xy
536 widget_layer.all[hit.s.id] = hit.s
538 hit = closest_in_layers xy
540 if hit.layer.selected[hit.s.id]
542 # TODO start detection of a click that doesn't drag (to shrink selection)
543 else if xy.y < supply_height
544 # dragging a new thing in
545 select_only hit.layer, hit.s
546 else if shift_key_down
547 select_also hit.layer, hit.s
549 select_only hit.layer, hit.s
550 for id, s of hit.layer.selected
551 s.set_state STATES.DRAGGING
553 drag_layer = hit.layer
556 deselect_all widget_layer
562 for id, s of drag_layer.selected
563 if s.y < supply_height and drag_layer is widget_layer
564 deselect drag_layer, s
566 delete drag_layer.all[id]
569 s.set_state STATES.SELECTED
570 if drag_layer is widget_layer and selected_count is 1
571 for id, s of drag_layer.selected
572 s.set_state STATES.EDITING
573 cs = s.make_controls done: (c) ->
574 deselect controls_layer, c
575 delete controls_layer.all[c.id]
577 controls_layer.all[c.id] = c
578 widget_layer.editing = s
582 xy = svg_event_to_xy e
584 return if drag_from.x is xy.x and drag_from.y is xy.y
585 rel_x = xy.x - drag_from.x
586 rel_y = xy.y - drag_from.y
588 for id, w of drag_layer.selected
589 w.drag x: rel_x, y: rel_y
591 hit = closest_in_layers xy
592 if hovered and hovered isnt hit?.s
593 hovered.set_hover false
596 hovered.set_hover true
598 $svg.mousedown (e) ->
604 $svg.mousemove (e) ->
607 $(document).on 'keyup keydown', (e) ->
608 shift_key_down = e.shiftKey
610 #($ document).keydown (e) ->