JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
experimental pickling to url hash
[crayon_mockup.git] / main.coffee
index 8f20313..d8f75b5 100644 (file)
@@ -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()
@@ -302,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: ->
@@ -394,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]
@@ -464,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()
@@ -557,6 +603,7 @@ init = ->
                        deselect_all widget_layer
                return
        mouseup = (e) ->
+               save()
                mousemove e
                if dragging
                        selected_count = 0