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' }
33 # json (compiled to javascript and minified) is ~8% smaller than the resulting xml
34 json_to_svg = (json) ->
35 for tag, attrs of json
36 el = document.createElementNS 'http://www.w3.org/2000/svg', tag
40 el.appendChild json_to_svg child
41 else if k is 'contents'
42 el.appendChild document.createTextNode v
47 resizer_nw = (widget) ->
49 widget.resize w: widget.width - dxy.x, h: widget.height - dxy.y
50 widget.move x: widget.x + dxy.x, y: widget.y + dxy.y
51 resizer_n = (widget) ->
53 widget.resize w: widget.width, h: widget.height - dxy.y
54 widget.move x: widget.x, y: widget.y + dxy.y
55 resizer_ne = (widget) ->
57 widget.resize w: widget.width + dxy.x, h: widget.height - dxy.y
58 widget.move x: widget.x, y: widget.y + dxy.y
59 resizer_e = (widget) ->
61 widget.resize w: widget.width + dxy.x, h: widget.height
62 resizer_se = (widget) ->
64 widget.resize w: widget.width + dxy.x, h: widget.height + dxy.y
65 resizer_s = (widget) ->
67 widget.resize w: widget.width, h: widget.height + dxy.y
68 resizer_sw = (widget) ->
70 widget.resize w: widget.width - dxy.x, h: widget.height + dxy.y
71 widget.move x: widget.x + dxy.x, y: widget.y
72 resizer_w = (widget) ->
74 widget.resize w: widget.width - dxy.x, h: widget.height
75 widget.move x: widget.x + dxy.x, y: widget.y
87 -> return "M#{@x - 5} #{@y - 5}h6l-2 2 4 4 2 -2v6h-6l2-2-4-4-2 2z"
88 -> 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"
89 -> return "M#{@x + 5} #{@y - 5}v6l-2-2-4 4 2 2h-6v-6l2 2 4-4-2-2z"
90 -> 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"
92 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"
95 # public vars: x, y, width, height, el
98 constructor: (args) ->
104 @width = args.width ? 50
105 @height = args.height ? 34
106 @state = args.state ? STATES.NORMAL
110 css_class = "#{@css_class} #{@state.txt}"
112 css_class += " hover"
113 @el.setAttribute 'class', css_class
118 move: (xy) -> # just move
121 drag: (dxy) -> # react to mouse drag (obey constraints, etc.)
122 @move x: @x + dxy.x, y: @y + dxy.y
123 proximity: (xy) -> # return the square of the distance to your visible bits
125 set_state: (state) ->
128 class Control extends Visible
129 constructor: (args) ->
132 @on_destruct = args.done ? null
137 drag: (args) -> # call this when control point is being manipulated directly
139 proximity: (xy) -> # return the square of the distance to your visible bits
142 return dx * dx + dy * dy
143 class ControlPath extends Control
144 constructor: (args) ->
146 @css_class = 'control_point'
147 @make_path = args.shape
148 @el = json_to_svg path:
150 class: 'control_point normal'
158 @el.setAttribute 'd', @make_path()
160 class Widget extends Visible
161 #sub-classes are expected to implement all of these:
162 constructor: (args) ->
169 make_controls: -> # create controls, return them
176 move: (xy) -> # just move
181 c.move x: c.x + dx, y: c.y + dy
182 set_state: (state) ->
183 return if @state is state
184 if @state is STATES.EDITING
188 class RectWidget extends Widget
189 constructor: (args) ->
192 @el = json_to_svg rect:
204 return new RectWidget @
206 return [@x, @y, @width, @height]
207 from_array: (args, a) ->
212 return new RectWidget args
213 set_state: (state) ->
218 @el.setAttribute 'x', @x + 1
219 @el.setAttribute 'y', @y + 1
220 @reposition_controls()
221 proximity: (xy) -> # return the square of the distance to your visible bits
227 if x > @x - CLICK_FUZ and x < @x + @width + CLICK_FUZ
229 if y < @y + @height / 2
232 new_prox = @y + @height - y
236 if y > @y - CLICK_FUZ and y < @y + @height + CLICK_FUZ
238 if x < @x + @width / 2
241 new_prox = @x + @width - x
245 # "hit" anything inside
246 #if in_x and in_y and prox > PROX_MAX
247 # prox = PROX_MAX - 1
253 @el.setAttribute 'width', @width - 2
255 @el.setAttribute 'height', @height - 2
256 @reposition_controls()
257 reposition_controls: ->
258 if @controls.length > 1
259 positions = @control_positions()
260 for i in [0...positions.length]
261 @controls[i].move x: positions[i].x, y: positions[i].y
262 control_positions: ->
265 w2p = Math.floor(@width / 2) + 0.5
266 h2p = Math.floor(@height / 2) + 0.5
268 { x: @x - gap, y: @y - gap }
269 { x: @x + w2p, y: @y - mgap }
270 { x: @x + @width + gap, y: @y - gap }
271 { x: @x + @width + mgap, y: @y + h2p }
272 { x: @x + @width + gap, y: @y + @height + gap }
273 { x: @x + w2p, y: @y + @height + mgap }
274 { x: @x - gap, y: @y + @height + gap }
275 { x: @x - mgap, y: @y + h2p }
277 make_controls: (args) -> # create controls, return them
278 if @controls.length > 0
280 console.log "warning: re-adding controls"
282 positions = @control_positions()
283 for i in [0...positions.length]
284 @controls.push new ControlPath {
290 shape: resizer_shapes[i % resizer_shapes.length]
294 class PolylineWidget extends Widget
295 constructor: (args) ->
297 @css_class = 'polyline'
300 @nodes.push x: n.x, y: n.y
301 @el = json_to_svg path:
303 class: 'polyline normal'
315 from_array: (args, a) ->
321 xy.x = a.shift() - args.x
322 xy.y = a.shift() - args.y
324 return new PolylineWidget args
326 return new PolylineWidget @
338 set_state: (state) ->
343 @el.setAttribute 'd', @my_path_d()
344 @reposition_controls()
345 proximity: (xy) -> # return the square of the distance to your visible bits
350 p = dx * dx + dy * dy
354 l1x = @x + @nodes[i-1].x
355 l1y = @y + @nodes[i-1].y
356 l2x = @x + @nodes[i].x
357 l2y = @y + @nodes[i].y
360 if ldx is 0 # vertical line
361 if (xy.y < l1y) is (xy.y < l2y)
365 else if ldy is 0 # horizontal line
366 if (xy.x < l1x) is (xy.x < l2x)
371 # https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line
374 c = l2x * l1y - l2y * l1x
375 y_on_line = (a * (a * xy.y - b * xy.x) - b * c) / (a * a + b * b)
376 if (y_on_line < l1y) is (y_on_line < l2y)
378 p = (a * xy.x + b * xy.y + c) / Math.sqrt(a * a + b * b)
385 # FIXME (apply to more than just 2nd node)
390 @el.setAttribute 'd', @my_path_d()
391 @reposition_controls()
396 for j in [1...@nodes.length]
399 @move x: @x + dxy.x, y: @y + dxy.y
403 @el.setAttribute 'd', @my_path_d()
404 @reposition_controls()
405 reposition_controls: ->
406 if @controls.length > 1
407 positions = @control_positions()
408 for i in [0...positions.length]
409 @controls[i].move x: positions[i].x, y: positions[i].y
411 control_positions: ->
414 ret.push x: @x + n.x, y: @y + n.y
416 make_controls: (args) -> # create controls, return them
417 if @controls.length > 0
419 console.log "warning: re-adding line controls"
421 positions = @control_positions()
422 for i in [0...positions.length]
423 @controls.push new ControlPath {
428 drag: @node_dragger i
429 shape: shape_node_move
433 CSS_CLASS_TO_PICKLE_TYPE = {
437 PICKLE_TYPE_TO_WIDGET_CLASS = [
441 # called automatically on domcontentloaded
444 $container = $ '.crayon_mockup'
445 svg = json_to_svg svg: width: width, height: height, viewBox: "0 0 #{width} #{height}"
447 $container.append $svg
448 svg.appendChild json_to_svg filter:
449 id: 'crayon', filterUnits: 'userSpaceOnUse'
450 x: '-5%', y: '-5%', height: '110%', width: '110%'
452 { feTurbulence: baseFrequency: '.3', numOctaves: '2', type: 'fractalNoise' }
453 { feDisplacementMap: scale: '6', xChannelSelector: 'R', in: 'SourceGraphic' }
455 svg.appendChild json_to_svg style:
457 contents: '.box.normal,.polyline.normal{filter: url(#crayon)}'
459 # create canvas border
460 svg.appendChild json_to_svg rect:
464 height: height - 2 - supply_height
465 class: 'canvas_border'
469 supply_add = (type, args) ->
472 args.x += 30 + supply_count * 90
473 args.y += (supply_height - 50) / 2
478 supply_add RectWidget, width: 50, height: 50
479 supply_add PolylineWidget, y: 25, nodes: [{x: 0, y: 0}, {x: 50, y: 0}]
480 supply_add PolylineWidget, x: 25, nodes: [{x: 0, y: 0}, {x: 0, y: 50}]
481 supply_add PolylineWidget, x: 10, nodes: [{x: 0, y: 0}, {x: 15, y: 50}, {x: 30, y: 0}]
482 supply_add PolylineWidget, y: 50, nodes: [{x: 0, y: 0}, {x: 17, y: -50}, {x: 33, y: 0}, {x: 50, y: -50}]
485 controls_layer = { all: {}, selected: {} }
486 widget_layer = { all: {}, selected: {}, editing: null }
487 layers = [controls_layer, widget_layer]
488 hovered = null # can be in any layer
489 dragging = false # mouse state
491 drag_from = x: 0, y: 0 # mouse was here at last frame of drag
492 shift_key_down = false
494 lc = "abcdefghijklmnopqrstuvwxyz"
495 uc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
496 cc_lca = lc.charCodeAt 0
497 cc_lcz = lc.charCodeAt 25
498 cc_uca = uc.charCodeAt 0
499 cc_ucz = uc.charCodeAt 25
500 cc_0 = '0'.charCodeAt 0
501 cc_9 = '9'.charCodeAt 0
502 # takes an array of positive integers, and encodes it as a string
508 while i > 0 or r is ''
509 digit = i % cs.length
512 r = cs.charAt(digit) + r
517 ret = '0' # version of this encoding scheme
518 for id, w of widget_layer.all
519 if CSS_CLASS_TO_PICKLE_TYPE[w.css_class]?
520 ret += CSS_CLASS_TO_PICKLE_TYPE[w.css_class]
521 ret += pickle w.as_array()
524 window.location.hash = pickle_widgets()
526 return if str.charAt(1) isnt '0'
530 load_1 = (next_type) ->
535 w = PICKLE_TYPE_TO_WIDGET_CLASS[wtype]::from_array svg: svg, args
536 widget_layer.all[w.id] = w
540 for i in [2...str.length]
541 c = str.charCodeAt(i)
544 else if cc_lca <= c <= cc_lcz
549 else if cc_uca <= c <= cc_ucz
554 if widget_layer.editing
555 widget_layer.editing.kill_controls()
556 widget_layer.editing = null
557 deselect = (layer, s) ->
558 return unless layer.selected[s.id]?
559 s.set_state STATES.NORMAL
560 delete layer.selected[s.id]
561 if widget_layer.editing is s
562 widget_layer.editing = null
564 deselect_all = (layer, except = null) ->
565 for id, s of layer.selected
568 _select = (layer, s) -> # don't call this directly, use select_only() or select_also()
569 s.set_state STATES.SELECTED
570 layer.selected[s.id] = s
572 select_only = (layer, s) ->
573 deselect_all layer, s
574 return if layer.selected[s.id]?
577 select_also = (layer, s) ->
578 return if layer.selected[s.id]?
579 if layer is widget_layer
583 find_closest = (widgets, xy) ->
587 new_prox = w.proximity xy
594 svg_event_to_xy = (e) ->
596 svg_offset = $svg.offset()
598 x: Math.round(e.pageX - svg_offset.left)
599 y: Math.round(e.pageY - svg_offset.top)
601 closest_in_layers = (xy) ->
603 s = find_closest layer.selected, xy
604 return layer: layer, s: s if s?
605 s = find_closest layer.all, xy
606 return layer: layer, s: s if s?
613 if dragging # two mousedowns in a row?! it happens
615 xy = svg_event_to_xy e
616 if xy.y < supply_height
617 s = find_closest supply, xy
623 widget_layer.all[hit.s.id] = hit.s
625 hit = closest_in_layers xy
627 if hit.layer.selected[hit.s.id]
629 # TODO start detection of a click that doesn't drag (to shrink selection)
630 else if xy.y < supply_height
631 # dragging a new thing in
632 select_only hit.layer, hit.s
633 else if shift_key_down
634 select_also hit.layer, hit.s
636 select_only hit.layer, hit.s
637 for id, s of hit.layer.selected
638 s.set_state STATES.DRAGGING
640 drag_layer = hit.layer
643 deselect_all widget_layer
650 for id, s of drag_layer.selected
651 if s.y < supply_height and drag_layer is widget_layer
652 deselect drag_layer, s
654 delete drag_layer.all[id]
657 s.set_state STATES.SELECTED
658 if drag_layer is widget_layer and selected_count is 1
659 for id, s of drag_layer.selected
660 s.set_state STATES.EDITING
661 cs = s.make_controls done: (c) ->
662 deselect controls_layer, c
663 delete controls_layer.all[c.id]
665 controls_layer.all[c.id] = c
666 widget_layer.editing = s
670 xy = svg_event_to_xy e
672 return if drag_from.x is xy.x and drag_from.y is xy.y
673 rel_x = xy.x - drag_from.x
674 rel_y = xy.y - drag_from.y
676 for id, w of drag_layer.selected
677 w.drag x: rel_x, y: rel_y
679 hit = closest_in_layers xy
680 if hovered and hovered isnt hit?.s
681 hovered.set_hover false
684 hovered.set_hover true
686 $svg.mousedown (e) ->
692 $svg.mousemove (e) ->
695 $(document).on 'keyup keydown', (e) ->
696 shift_key_down = e.shiftKey
698 #($ document).keydown (e) ->
700 if window.location.hash
701 load window.location.hash