DRAGGING: { txt: 'dragging' }
EDITING: { txt: 'editing' }
}
-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
if @hover
css_class += " hover"
@el.setAttribute 'class', css_class
- set_hover: (tf) ->
- if tf != @hover
- @hover = tf
+ set_hover: (bool) ->
+ if bool != @hover
+ @hover = bool
@update_class()
move: (xy) -> # just move
@x = xy.x
class Control extends Visible
constructor: (args) ->
super args
- @type = TYPE_CONTROL
@on_drag = args.drag
@on_destruct = args.done ? null
destruct: ->
constructor: (args) ->
super args
@controls = []
- @type = TYPE_WIDGET
destruct: ->
@kill_controls()
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]
+ from_array: (args, a) ->
+ args.x = a[0]
+ args.y = a[1]
+ args.width = a[2]
+ args.height = a[3]
+ return new RectWidget args
set_state: (state) ->
super state
@update_class()
ret.push @x + n.x
ret.push @y + n.y
return ret
- from_array: (a) ->
- args = {
- x: a[0]
- y: a[1]
- nodes: []
- }
+ from_array: (args, a) ->
+ args.x = a[0]
+ args.y = a[1]
+ args.nodes = []
while a.length
- args.nodes.push x: a.shift() - args.x, y: a.shift() - args.x
+ xy = {}
+ xy.x = a.shift() - args.x
+ xy.y = a.shift() - args.y
+ args.nodes.push xy
return new PolylineWidget args
clone: ->
return new PolylineWidget @
node_dragger: (i) ->
if i is 0
return (dxy) =>
- for i in [1...@nodes.length]
- @nodes[i].x -= dxy.x
- @nodes[i].y -= dxy.y
+ for j in [1...@nodes.length]
+ @nodes[j].x -= dxy.x
+ @nodes[j].y -= dxy.y
@move x: @x + dxy.x, y: @y + dxy.y
return (dxy) =>
@nodes[i].x += dxy.x
}
return @controls
+CSS_CLASS_TO_PICKLE_TYPE = {
+ box: '0'
+ polyline: '1'
+}
+PICKLE_TYPE_TO_WIDGET_CLASS = [
+ RectWidget
+ PolylineWidget
+]
# called automatically on domcontentloaded
init = ->
svg_offset = null
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, nodes: [{x: 0, y: 50}, {x: 17, y: 0}, {x: 33, y: 50}, {x: 50, y: 0}]
+ supply_add PolylineWidget, y: 50, nodes: [{x: 0, y: 0}, {x: 17, y: -50}, {x: 33, y: 0}, {x: 50, y: -50}]
# editor state
controls_layer = { all: {}, selected: {} }
lc = "abcdefghijklmnopqrstuvwxyz"
uc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ cc_lca = lc.charCodeAt 0
+ cc_lcz = lc.charCodeAt 25
+ cc_uca = uc.charCodeAt 0
+ cc_ucz = uc.charCodeAt 25
+ cc_0 = '0'.charCodeAt 0
+ cc_9 = '9'.charCodeAt 0
# takes an array of positive integers, and encodes it as a string
pickle = (a) ->
ret = ''
return ret
save = ->
window.location.hash = pickle_widgets()
+ load = (str) ->
+ return if str.charAt(1) isnt '0'
+ wtype = null
+ args = []
+ ii = 0
+ load_1 = (next_type) ->
+ unless wtype?
+ if next_type?
+ wtype = next_type
+ return
+ w = PICKLE_TYPE_TO_WIDGET_CLASS[wtype]::from_array svg: svg, args
+ widget_layer.all[w.id] = w
+ wtype = next_type
+ args = []
+ ii = 0
+ for i in [2...str.length]
+ c = str.charCodeAt(i)
+ if cc_0 <= c <= cc_9
+ load_1 c - cc_0
+ else if cc_lca <= c <= cc_lcz
+ ii *= lc.length
+ ii += c - cc_lca
+ args.push ii
+ ii = 0
+ else if cc_uca <= c <= cc_ucz
+ ii *= lc.length
+ ii += c - cc_uca
+ load_1 null
stop_editing = ->
if widget_layer.editing
widget_layer.editing.kill_controls()
return true
#($ document).keydown (e) ->
+ if window.location.hash
+ load window.location.hash
+
$ init