STYLE_EDITING = 3
STYLE_DRAGGING = 4
+STYLE_TO_CLASS = [
+ "normal"
+ "selected"
+ "hover"
+ "editing"
+ "dragging"
+]
+
+set_style_class = (args) ->
+ args.el.setAttribute 'class', "#{args.class} #{STYLE_TO_CLASS[args.style]}"
+
# json (compiled to javascript and minified) is ~8% smaller than the resulting xml
json_to_svg = (json) ->
for tag, attrs of json
if k is 'children'
for child in v
el.appendChild json_to_svg child
+ else if k is 'contents'
+ el.appendChild document.createTextNode v
else
el.setAttribute k, v
return el
next_widget_id = 0
# public vars: x, y, width, height, el
-class Widget
+class Visible
# required args: svg
constructor: (args) ->
@id = next_widget_id
next_widget_id += 1
@svg = args.svg
- @style = args.style ? STYLE_NORMAL
@x = args.x ? 1
@y = args.y ? 1
@width = args.width ? 50
@height = args.height ? 34
+ @style = args.style ? STYLE_NORMAL
destruct: ->
- clone: ->
- return new Widget @
move: (args) ->
@x = args.x
@y = args.y
proximity: (xy) -> # return the square of the distance to your visible bits
return PROX_MAX + 1
set_style: (style) ->
- return if @style is style
- if style is STYLE_NORMAL
- @el.setAttribute 'style', 'filter: url(#crayon)'
- else
- if @style is STYLE_NORMAL
- @el.setAttribute 'style', ''
- switch style
- when STYLE_NORMAL
- @el.setAttribute 'class', "#{@css_class}"
- when STYLE_SELECTED
- @el.setAttribute 'class', "#{@css_class} selected"
- when STYLE_HOVER
- @el.setAttribute 'class', "#{@css_class} hover"
- when STYLE_DRAGGING
- @el.setAttribute 'class', "#{@css_class} dragging"
- # FIXME when STYLE_EDITING
@style = style
+
+class Control extends Visible
+
+class Widget extends Visible
+ #sub-classes are expected to implement all of these:
+ clone: ->
+ return new Widget @
controls: -> # create controls, return them
return []
hide_controls: ->
y: @y + 1
width: @width - 2
height: @height - 2
- class: 'box'
- style: if @style is STYLE_NORMAL then 'filter: url(#crayon)' else ''
+ class: 'box normal'
@svg.appendChild @el
destruct: ->
super()
@svg.removeChild @el
clone: ->
return new RectWidget @
+ set_style: (style) ->
+ super style
+ set_style_class el: @el, class: 'box', style: style
move: (args) ->
super args
@el.setAttribute 'x', @x + 1
{ feTurbulence: baseFrequency: '.3', numOctaves: '2', type: 'fractalNoise' }
{ feDisplacementMap: scale: '6', xChannelSelector: 'R', in: 'SourceGraphic' }
]
+ svg.appendChild json_to_svg style:
+ type: 'text/css'
+ contents: '.box.normal,.box.hover,.box.selected{filter: url(#crayon)}'
# create canvas border
svg.appendChild json_to_svg rect: