X-Git-Url: https://jasonwoof.com/gitweb/?p=crayon_mockup.git;a=blobdiff_plain;f=main.coffee;h=d8f75b5e8cbe13710f2ad91d4c0e2de03996808b;hp=0a60f329096c943477f89a689b7f10887781e6a8;hb=6c36ab228f79fe01714b1cdf7cf08316fc65f3d7;hpb=ecf9cebe3e8d5274c7247b5e2baf3527bd2c026a diff --git a/main.coffee b/main.coffee index 0a60f32..d8f75b5 100644 --- a/main.coffee +++ b/main.coffee @@ -31,7 +31,10 @@ STATES = { } 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 @@ -206,6 +209,10 @@ class RectWidget extends Widget @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() @@ -238,8 +245,9 @@ class RectWidget extends Widget 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 @@ -301,6 +309,21 @@ class PolylineWidget extends Widget 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: -> @@ -393,10 +416,9 @@ class PolylineWidget extends Widget 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] @@ -451,7 +473,7 @@ init = -> 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: {} } @@ -463,6 +485,31 @@ init = -> 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() @@ -556,6 +603,7 @@ init = -> deselect_all widget_layer return mouseup = (e) -> + save() mousemove e if dragging selected_count = 0