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 # "hit" anything inside
242 #if in_x and in_y and prox > PROX_MAX
243 # prox = PROX_MAX - 1
249 @el.setAttribute 'width', @width - 2
251 @el.setAttribute 'height', @height - 2
252 @reposition_controls()
253 reposition_controls: ->
254 if @controls.length > 1
255 positions = @control_positions()
256 for i in [0...positions.length]
257 @controls[i].move x: positions[i].x, y: positions[i].y
258 control_positions: ->
261 w2p = Math.floor(@width / 2) + 0.5
262 h2p = Math.floor(@height / 2) + 0.5
264 { x: @x - gap, y: @y - gap }
265 { x: @x + w2p, y: @y - mgap }
266 { x: @x + @width + gap, y: @y - gap }
267 { x: @x + @width + mgap, y: @y + h2p }
268 { x: @x + @width + gap, y: @y + @height + gap }
269 { x: @x + w2p, y: @y + @height + mgap }
270 { x: @x - gap, y: @y + @height + gap }
271 { x: @x - mgap, y: @y + h2p }
273 make_controls: (args) -> # create controls, return them
274 if @controls.length > 0
276 console.log "warning: re-adding controls"
278 positions = @control_positions()
279 for i in [0...positions.length]
280 @controls.push new ControlPath {
286 shape: resizer_shapes[i % resizer_shapes.length]
290 class PolylineWidget extends Widget
291 constructor: (args) ->
293 @css_class = 'polyline'
296 @nodes.push x: n.x, y: n.y
297 @el = json_to_svg path:
299 class: 'polyline normal'
306 return new PolylineWidget @
318 set_state: (state) ->
323 @el.setAttribute 'd', @my_path_d()
324 @reposition_controls()
325 proximity: (xy) -> # return the square of the distance to your visible bits
330 p = dx * dx + dy * dy
334 l1x = @x + @nodes[i-1].x
335 l1y = @y + @nodes[i-1].y
336 l2x = @x + @nodes[i].x
337 l2y = @y + @nodes[i].y
340 if ldx is 0 # vertical line
341 if (xy.y < l1y) is (xy.y < l2y)
345 else if ldy is 0 # horizontal line
346 if (xy.x < l1x) is (xy.x < l2x)
351 # https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line
354 c = l2x * l1y - l2y * l1x
355 y_on_line = (a * (a * xy.y - b * xy.x) - b * c) / (a * a + b * b)
356 if (y_on_line < l1y) is (y_on_line < l2y)
358 p = (a * xy.x + b * xy.y + c) / Math.sqrt(a * a + b * b)
365 # FIXME (apply to more than just 2nd node)
370 @el.setAttribute 'd', @my_path_d()
371 @reposition_controls()
376 for i in [1...@nodes.length]
379 @move x: @x + dxy.x, y: @y + dxy.y
383 @el.setAttribute 'd', @my_path_d()
384 @reposition_controls()
385 reposition_controls: ->
386 if @controls.length > 1
387 positions = @control_positions()
388 for i in [0...positions.length]
389 @controls[i].move x: positions[i].x, y: positions[i].y
391 control_positions: ->
394 ret.push x: @x + n.x, y: @y + n.y
396 make_controls: (args) -> # create controls, return them
397 console.log 'make line controls'
398 if @controls.length > 0
400 console.log "warning: re-adding controls"
402 positions = @control_positions()
403 for i in [0...positions.length]
404 @controls.push new ControlPath {
409 drag: @node_dragger i
410 shape: shape_node_move
414 # called automatically on domcontentloaded
417 $container = $ '.crayon_mockup'
418 svg = json_to_svg svg: width: width, height: height, viewBox: "0 0 #{width} #{height}"
420 $container.append $svg
421 svg.appendChild json_to_svg filter:
422 id: 'crayon', filterUnits: 'userSpaceOnUse'
423 x: '-5%', y: '-5%', height: '110%', width: '110%'
425 { feTurbulence: baseFrequency: '.3', numOctaves: '2', type: 'fractalNoise' }
426 { feDisplacementMap: scale: '6', xChannelSelector: 'R', in: 'SourceGraphic' }
428 svg.appendChild json_to_svg style:
430 contents: '.box.normal,.polyline.normal{filter: url(#crayon)}'
432 # create canvas border
433 svg.appendChild json_to_svg rect:
437 height: height - 2 - supply_height
438 class: 'canvas_border'
442 supply_add = (type, args) ->
445 args.x += 30 + supply_count * 90
446 args.y += (supply_height - 50) / 2
451 supply_add RectWidget, width: 50, height: 50
452 supply_add PolylineWidget, y: 25, nodes: [{x: 0, y: 0}, {x: 50, y: 0}]
453 supply_add PolylineWidget, x: 25, nodes: [{x: 0, y: 0}, {x: 0, y: 50}]
454 supply_add PolylineWidget, x: 10, nodes: [{x: 0, y: 0}, {x: 15, y: 50}, {x: 30, y: 0}]
455 supply_add PolylineWidget, nodes: [{x: 0, y: 50}, {x: 17, y: 0}, {x: 33, y: 50}, {x: 50, y: 0}]
458 controls_layer = { all: {}, selected: {} }
459 widget_layer = { all: {}, selected: {}, editing: null }
460 layers = [controls_layer, widget_layer]
461 hovered = null # can be in any layer
462 dragging = false # mouse state
464 drag_from = x: 0, y: 0 # mouse was here at last frame of drag
465 shift_key_down = false
468 if widget_layer.editing
469 widget_layer.editing.kill_controls()
470 widget_layer.editing = null
471 deselect = (layer, s) ->
472 return unless layer.selected[s.id]?
473 s.set_state STATES.NORMAL
474 delete layer.selected[s.id]
475 if widget_layer.editing is s
476 widget_layer.editing = null
478 deselect_all = (layer, except = null) ->
479 for id, s of layer.selected
482 _select = (layer, s) -> # don't call this directly, use select_only() or select_also()
483 s.set_state STATES.SELECTED
484 layer.selected[s.id] = s
486 select_only = (layer, s) ->
487 deselect_all layer, s
488 return if layer.selected[s.id]?
491 select_also = (layer, s) ->
492 return if layer.selected[s.id]?
493 if layer is widget_layer
497 find_closest = (widgets, xy) ->
501 new_prox = w.proximity xy
508 svg_event_to_xy = (e) ->
510 svg_offset = $svg.offset()
512 x: Math.round(e.pageX - svg_offset.left)
513 y: Math.round(e.pageY - svg_offset.top)
515 closest_in_layers = (xy) ->
517 s = find_closest layer.selected, xy
518 return layer: layer, s: s if s?
519 s = find_closest layer.all, xy
520 return layer: layer, s: s if s?
527 if dragging # two mousedowns in a row?! it happens
529 xy = svg_event_to_xy e
530 if xy.y < supply_height
531 s = find_closest supply, xy
537 widget_layer.all[hit.s.id] = hit.s
539 hit = closest_in_layers xy
541 if hit.layer.selected[hit.s.id]
543 # TODO start detection of a click that doesn't drag (to shrink selection)
544 else if xy.y < supply_height
545 # dragging a new thing in
546 select_only hit.layer, hit.s
547 else if shift_key_down
548 select_also hit.layer, hit.s
550 select_only hit.layer, hit.s
551 for id, s of hit.layer.selected
552 s.set_state STATES.DRAGGING
554 drag_layer = hit.layer
557 deselect_all widget_layer
563 for id, s of drag_layer.selected
564 if s.y < supply_height and drag_layer is widget_layer
565 deselect drag_layer, s
567 delete drag_layer.all[id]
570 s.set_state STATES.SELECTED
571 if drag_layer is widget_layer and selected_count is 1
572 for id, s of drag_layer.selected
573 s.set_state STATES.EDITING
574 cs = s.make_controls done: (c) ->
575 deselect controls_layer, c
576 delete controls_layer.all[c.id]
578 controls_layer.all[c.id] = c
579 widget_layer.editing = s
583 xy = svg_event_to_xy e
585 return if drag_from.x is xy.x and drag_from.y is xy.y
586 rel_x = xy.x - drag_from.x
587 rel_y = xy.y - drag_from.y
589 for id, w of drag_layer.selected
590 w.drag x: rel_x, y: rel_y
592 hit = closest_in_layers xy
593 if hovered and hovered isnt hit?.s
594 hovered.set_hover false
597 hovered.set_hover true
599 $svg.mousedown (e) ->
605 $svg.mousemove (e) ->
608 $(document).on 'keyup keydown', (e) ->
609 shift_key_down = e.shiftKey
611 #($ document).keydown (e) ->