}
TYPE_WIDGET = 1
TYPE_CONTROL = 2
-
+CSS_CLASS_TO_PICKLE_TYPE = {
+ box: '0'
+ polyline: '1'
+}
# json (compiled to javascript and minified) is ~8% smaller than the resulting xml
json_to_svg = (json) ->
for tag, attrs of json
@svg.removeChild @el
clone: ->
return new RectWidget @
+ as_array: ->
+ return [@x, @y, @width, @height]
+ from_array: (a) ->
+ return new RectWidget x: a[0], y: a[1], width: a[2], height: a[3]
set_state: (state) ->
super state
@update_class()
new_prox *= new_prox
if new_prox < prox
prox = new_prox
- if in_x and in_y and prox > PROX_MAX
- prox = PROX_MAX - 1
+ # "hit" anything inside
+ #if in_x and in_y and prox > PROX_MAX
+ # prox = PROX_MAX - 1
return prox
resize: (wh) ->
dw = wh.w - @width
super()
if @el?
@svg.removeChild @el
+ as_array: ->
+ ret = []
+ for n in @nodes
+ ret.push @x + n.x
+ ret.push @y + n.y
+ return ret
+ from_array: (a) ->
+ args = {
+ x: a[0]
+ y: a[1]
+ nodes: []
+ }
+ while a.length
+ args.nodes.push x: a.shift() - args.x, y: a.shift() - args.x
+ return new PolylineWidget args
clone: ->
return new PolylineWidget @
my_path_d: ->
ret.push x: @x + n.x, y: @y + n.y
return ret
make_controls: (args) -> # create controls, return them
- console.log 'make line controls'
if @controls.length > 0
if console?.log?
- console.log "warning: re-adding controls"
+ console.log "warning: re-adding line controls"
@kill_controls()
positions = @control_positions()
for i in [0...positions.length]
supply_add PolylineWidget, y: 25, nodes: [{x: 0, y: 0}, {x: 50, y: 0}]
supply_add PolylineWidget, x: 25, nodes: [{x: 0, y: 0}, {x: 0, y: 50}]
supply_add PolylineWidget, x: 10, nodes: [{x: 0, y: 0}, {x: 15, y: 50}, {x: 30, y: 0}]
- supply_add PolylineWidget, x: 0, nodes: [{x: 0, y: 50}, {x: 17, y: 0}, {x: 33, y: 50}, {x: 50, y: 0}]
+ supply_add PolylineWidget, nodes: [{x: 0, y: 50}, {x: 17, y: 0}, {x: 33, y: 50}, {x: 50, y: 0}]
# editor state
controls_layer = { all: {}, selected: {} }
drag_from = x: 0, y: 0 # mouse was here at last frame of drag
shift_key_down = false
+ lc = "abcdefghijklmnopqrstuvwxyz"
+ uc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ # takes an array of positive integers, and encodes it as a string
+ pickle = (a) ->
+ ret = ''
+ for i in a
+ cs = lc
+ r = ''
+ while i > 0 or r is ''
+ digit = i % cs.length
+ i -= digit
+ i /= cs.length
+ r = cs.charAt(digit) + r
+ cs = uc
+ ret += r
+ return ret
+ pickle_widgets = ->
+ ret = '0' # version of this encoding scheme
+ for id, w of widget_layer.all
+ if CSS_CLASS_TO_PICKLE_TYPE[w.css_class]?
+ ret += CSS_CLASS_TO_PICKLE_TYPE[w.css_class]
+ ret += pickle w.as_array()
+ return ret
+ save = ->
+ window.location.hash = pickle_widgets()
stop_editing = ->
if widget_layer.editing
widget_layer.editing.kill_controls()
deselect_all widget_layer
return
mouseup = (e) ->
+ save()
mousemove e
if dragging
selected_count = 0