annotate web/paint/paint-elements.coffee @ 2376:4556eebe5d73

topdir reorgs; let pdm have its src/ dir; separate vite area from light9/
author drewp@bigasterisk.com
date Sun, 12 May 2024 19:02:10 -0700
parents light9/web/paint/paint-elements.coffee@7fe81130b735
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1901
7fe81130b735 move web logging to https://github.com/visionmedia/debug/ so it can have channels that can be turned off
Drew Perttula <drewp@bigasterisk.com>
parents: 1626
diff changeset
1 log = debug('paint')
7fe81130b735 move web logging to https://github.com/visionmedia/debug/ so it can have channels that can be turned off
Drew Perttula <drewp@bigasterisk.com>
parents: 1626
diff changeset
2 debug.enable('paint')
1626
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1587
diff changeset
3
1522
69088fe2865e more progress on paint
Drew Perttula <drewp@bigasterisk.com>
parents: 1519
diff changeset
4 class Painting
1576
ef7ddef3acb5 clean up Painting & Stroke. hover now shows up. add Clear command
Drew Perttula <drewp@bigasterisk.com>
parents: 1573
diff changeset
5 constructor: (@svg) ->
1522
69088fe2865e more progress on paint
Drew Perttula <drewp@bigasterisk.com>
parents: 1519
diff changeset
6 @strokes = []
69088fe2865e more progress on paint
Drew Perttula <drewp@bigasterisk.com>
parents: 1519
diff changeset
7
1576
ef7ddef3acb5 clean up Painting & Stroke. hover now shows up. add Clear command
Drew Perttula <drewp@bigasterisk.com>
parents: 1573
diff changeset
8 setSize: (@size) ->
ef7ddef3acb5 clean up Painting & Stroke. hover now shows up. add Clear command
Drew Perttula <drewp@bigasterisk.com>
parents: 1573
diff changeset
9
ef7ddef3acb5 clean up Painting & Stroke. hover now shows up. add Clear command
Drew Perttula <drewp@bigasterisk.com>
parents: 1573
diff changeset
10 startStroke: (pos, color) ->
ef7ddef3acb5 clean up Painting & Stroke. hover now shows up. add Clear command
Drew Perttula <drewp@bigasterisk.com>
parents: 1573
diff changeset
11 stroke = new Stroke(pos, color, @size)
ef7ddef3acb5 clean up Painting & Stroke. hover now shows up. add Clear command
Drew Perttula <drewp@bigasterisk.com>
parents: 1573
diff changeset
12 stroke.appendElem(@svg)
ef7ddef3acb5 clean up Painting & Stroke. hover now shows up. add Clear command
Drew Perttula <drewp@bigasterisk.com>
parents: 1573
diff changeset
13 @strokes.push(stroke)
ef7ddef3acb5 clean up Painting & Stroke. hover now shows up. add Clear command
Drew Perttula <drewp@bigasterisk.com>
parents: 1573
diff changeset
14 return stroke
1522
69088fe2865e more progress on paint
Drew Perttula <drewp@bigasterisk.com>
parents: 1519
diff changeset
15
1573
20f48a0e7135 hover mode shows best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
16 hover: (pos) ->
1576
ef7ddef3acb5 clean up Painting & Stroke. hover now shows up. add Clear command
Drew Perttula <drewp@bigasterisk.com>
parents: 1573
diff changeset
17 @clear()
ef7ddef3acb5 clean up Painting & Stroke. hover now shows up. add Clear command
Drew Perttula <drewp@bigasterisk.com>
parents: 1573
diff changeset
18 s = @startStroke(pos, '#ffffff', @size)
ef7ddef3acb5 clean up Painting & Stroke. hover now shows up. add Clear command
Drew Perttula <drewp@bigasterisk.com>
parents: 1573
diff changeset
19 r = .02
ef7ddef3acb5 clean up Painting & Stroke. hover now shows up. add Clear command
Drew Perttula <drewp@bigasterisk.com>
parents: 1573
diff changeset
20 steps = 5
ef7ddef3acb5 clean up Painting & Stroke. hover now shows up. add Clear command
Drew Perttula <drewp@bigasterisk.com>
parents: 1573
diff changeset
21 for ang in [0..steps]
ef7ddef3acb5 clean up Painting & Stroke. hover now shows up. add Clear command
Drew Perttula <drewp@bigasterisk.com>
parents: 1573
diff changeset
22 ang = 6.28 * ang / steps
ef7ddef3acb5 clean up Painting & Stroke. hover now shows up. add Clear command
Drew Perttula <drewp@bigasterisk.com>
parents: 1573
diff changeset
23 s.move([pos[0] + r * Math.sin(ang), pos[1] + 1.5 * r * Math.cos(ang)])
1573
20f48a0e7135 hover mode shows best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
24
1522
69088fe2865e more progress on paint
Drew Perttula <drewp@bigasterisk.com>
parents: 1519
diff changeset
25 getDoc: ->
69088fe2865e more progress on paint
Drew Perttula <drewp@bigasterisk.com>
parents: 1519
diff changeset
26 {strokes: @strokes}
69088fe2865e more progress on paint
Drew Perttula <drewp@bigasterisk.com>
parents: 1519
diff changeset
27
1576
ef7ddef3acb5 clean up Painting & Stroke. hover now shows up. add Clear command
Drew Perttula <drewp@bigasterisk.com>
parents: 1573
diff changeset
28 clear: ->
ef7ddef3acb5 clean up Painting & Stroke. hover now shows up. add Clear command
Drew Perttula <drewp@bigasterisk.com>
parents: 1573
diff changeset
29 s.removeElem() for s in @strokes
ef7ddef3acb5 clean up Painting & Stroke. hover now shows up. add Clear command
Drew Perttula <drewp@bigasterisk.com>
parents: 1573
diff changeset
30 @strokes = []
ef7ddef3acb5 clean up Painting & Stroke. hover now shows up. add Clear command
Drew Perttula <drewp@bigasterisk.com>
parents: 1573
diff changeset
31
1519
a225c32bd9c8 new svg paint UI
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
32 class Stroke
1570
0480fc790527 paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1525
diff changeset
33 constructor: (pos, @color, @size) ->
1519
a225c32bd9c8 new svg paint UI
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
34 @path = document.createElementNS('http://www.w3.org/2000/svg', 'path')
1570
0480fc790527 paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1525
diff changeset
35 @path.setAttributeNS(null, 'd', "M #{pos[0]*@size[0]} #{pos[1]*@size[1]}")
1522
69088fe2865e more progress on paint
Drew Perttula <drewp@bigasterisk.com>
parents: 1519
diff changeset
36 @pts = [pos]
1519
a225c32bd9c8 new svg paint UI
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
37 @lastPos = pos
a225c32bd9c8 new svg paint UI
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
38
a225c32bd9c8 new svg paint UI
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
39 appendElem: (parent) ->
a225c32bd9c8 new svg paint UI
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
40 parent.appendChild(@path)
1576
ef7ddef3acb5 clean up Painting & Stroke. hover now shows up. add Clear command
Drew Perttula <drewp@bigasterisk.com>
parents: 1573
diff changeset
41
ef7ddef3acb5 clean up Painting & Stroke. hover now shows up. add Clear command
Drew Perttula <drewp@bigasterisk.com>
parents: 1573
diff changeset
42 removeElem: ->
ef7ddef3acb5 clean up Painting & Stroke. hover now shows up. add Clear command
Drew Perttula <drewp@bigasterisk.com>
parents: 1573
diff changeset
43 @path.remove()
1519
a225c32bd9c8 new svg paint UI
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
44
a225c32bd9c8 new svg paint UI
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
45 move: (pos) ->
1570
0480fc790527 paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1525
diff changeset
46 if Math.hypot(pos[0] - @lastPos[0], pos[1] - @lastPos[1]) < .02
1519
a225c32bd9c8 new svg paint UI
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
47 return
1570
0480fc790527 paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1525
diff changeset
48 @path.attributes.d.value += " L #{pos[0]*@size[0]} #{pos[1]*@size[1]}"
1522
69088fe2865e more progress on paint
Drew Perttula <drewp@bigasterisk.com>
parents: 1519
diff changeset
49 @pts.push(pos)
1519
a225c32bd9c8 new svg paint UI
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
50 @lastPos = pos
1570
0480fc790527 paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1525
diff changeset
51
1576
ef7ddef3acb5 clean up Painting & Stroke. hover now shows up. add Clear command
Drew Perttula <drewp@bigasterisk.com>
parents: 1573
diff changeset
52 finish: () ->
ef7ddef3acb5 clean up Painting & Stroke. hover now shows up. add Clear command
Drew Perttula <drewp@bigasterisk.com>
parents: 1573
diff changeset
53
1519
a225c32bd9c8 new svg paint UI
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
54 Polymer
1570
0480fc790527 paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1525
diff changeset
55 is: "light9-paint-canvas"
1519
a225c32bd9c8 new svg paint UI
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
56 behaviors: [ Polymer.IronResizableBehavior ]
a225c32bd9c8 new svg paint UI
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
57 listeners: 'iron-resize': 'onResize'
a225c32bd9c8 new svg paint UI
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
58 properties: {
1570
0480fc790527 paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1525
diff changeset
59 bg: { type: String },
1576
ef7ddef3acb5 clean up Painting & Stroke. hover now shows up. add Clear command
Drew Perttula <drewp@bigasterisk.com>
parents: 1573
diff changeset
60 tool: { type: String, value: 'hover' },
1570
0480fc790527 paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1525
diff changeset
61 painting: { type: Object } # output
1519
a225c32bd9c8 new svg paint UI
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
62 }
a225c32bd9c8 new svg paint UI
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
63 ready: ->
1576
ef7ddef3acb5 clean up Painting & Stroke. hover now shows up. add Clear command
Drew Perttula <drewp@bigasterisk.com>
parents: 1573
diff changeset
64 @painting = new Painting(@$.paint)
1570
0480fc790527 paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1525
diff changeset
65 @onResize()
1519
a225c32bd9c8 new svg paint UI
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
66 @$.paint.addEventListener('mousedown', @onDown.bind(@))
a225c32bd9c8 new svg paint UI
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
67 @$.paint.addEventListener('mousemove', @onMove.bind(@))
a225c32bd9c8 new svg paint UI
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
68 @$.paint.addEventListener('mouseup', @onUp.bind(@))
a225c32bd9c8 new svg paint UI
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
69 @$.paint.addEventListener('touchstart', @onDown.bind(@))
a225c32bd9c8 new svg paint UI
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
70 @$.paint.addEventListener('touchmove', @onMove.bind(@))
a225c32bd9c8 new svg paint UI
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
71 @$.paint.addEventListener('touchend', @onUp.bind(@))
a225c32bd9c8 new svg paint UI
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
72
1573
20f48a0e7135 hover mode shows best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
73 @hover = _.throttle((ev) =>
20f48a0e7135 hover mode shows best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
74 @painting.hover(@evPos(ev))
1576
ef7ddef3acb5 clean up Painting & Stroke. hover now shows up. add Clear command
Drew Perttula <drewp@bigasterisk.com>
parents: 1573
diff changeset
75 @scopeSubtree(@$.paint)
1573
20f48a0e7135 hover mode shows best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
76 @fire('paintingChanged', @painting)
20f48a0e7135 hover mode shows best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
77 , 100)
20f48a0e7135 hover mode shows best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
78
1519
a225c32bd9c8 new svg paint UI
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
79 evPos: (ev) ->
1570
0480fc790527 paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1525
diff changeset
80 px = (if ev.touches?.length? then [Math.round(ev.touches[0].clientX),
0480fc790527 paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1525
diff changeset
81 Math.round(ev.touches[0].clientY)] else [ev.x, ev.y])
0480fc790527 paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1525
diff changeset
82 return [px[0] / @size[0], px[1] / @size[1]]
1519
a225c32bd9c8 new svg paint UI
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
83
1576
ef7ddef3acb5 clean up Painting & Stroke. hover now shows up. add Clear command
Drew Perttula <drewp@bigasterisk.com>
parents: 1573
diff changeset
84 onClear: () ->
ef7ddef3acb5 clean up Painting & Stroke. hover now shows up. add Clear command
Drew Perttula <drewp@bigasterisk.com>
parents: 1573
diff changeset
85 @painting.clear()
ef7ddef3acb5 clean up Painting & Stroke. hover now shows up. add Clear command
Drew Perttula <drewp@bigasterisk.com>
parents: 1573
diff changeset
86 @fire('paintingChanged', @painting)
ef7ddef3acb5 clean up Painting & Stroke. hover now shows up. add Clear command
Drew Perttula <drewp@bigasterisk.com>
parents: 1573
diff changeset
87
1519
a225c32bd9c8 new svg paint UI
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
88 onDown: (ev) ->
1573
20f48a0e7135 hover mode shows best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
89 switch @tool
20f48a0e7135 hover mode shows best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
90 when "hover"
20f48a0e7135 hover mode shows best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
91 @onMove(ev)
20f48a0e7135 hover mode shows best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
92 when "paint"
20f48a0e7135 hover mode shows best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
93 # if it's on an existing one, do selection
1576
ef7ddef3acb5 clean up Painting & Stroke. hover now shows up. add Clear command
Drew Perttula <drewp@bigasterisk.com>
parents: 1573
diff changeset
94 @currentStroke = @painting.startStroke(@evPos(ev), '#aaaaaa')
1519
a225c32bd9c8 new svg paint UI
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
95 @scopeSubtree(@$.paint)
a225c32bd9c8 new svg paint UI
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
96
a225c32bd9c8 new svg paint UI
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
97 onMove: (ev) ->
1573
20f48a0e7135 hover mode shows best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
98 switch @tool
20f48a0e7135 hover mode shows best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
99 when "hover"
20f48a0e7135 hover mode shows best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
100 @hover(ev)
20f48a0e7135 hover mode shows best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
101
20f48a0e7135 hover mode shows best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
102 when "paint"
20f48a0e7135 hover mode shows best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1570
diff changeset
103 # ..or move selection
1576
ef7ddef3acb5 clean up Painting & Stroke. hover now shows up. add Clear command
Drew Perttula <drewp@bigasterisk.com>
parents: 1573
diff changeset
104 return unless @currentStroke
ef7ddef3acb5 clean up Painting & Stroke. hover now shows up. add Clear command
Drew Perttula <drewp@bigasterisk.com>
parents: 1573
diff changeset
105 @currentStroke.move(@evPos(ev))
1519
a225c32bd9c8 new svg paint UI
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
106
a225c32bd9c8 new svg paint UI
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
107 onUp: (ev) ->
1576
ef7ddef3acb5 clean up Painting & Stroke. hover now shows up. add Clear command
Drew Perttula <drewp@bigasterisk.com>
parents: 1573
diff changeset
108 return unless @currentStroke
ef7ddef3acb5 clean up Painting & Stroke. hover now shows up. add Clear command
Drew Perttula <drewp@bigasterisk.com>
parents: 1573
diff changeset
109 @currentStroke.finish()
ef7ddef3acb5 clean up Painting & Stroke. hover now shows up. add Clear command
Drew Perttula <drewp@bigasterisk.com>
parents: 1573
diff changeset
110 @currentStroke = null
1570
0480fc790527 paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1525
diff changeset
111
0480fc790527 paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1525
diff changeset
112 @notifyPath('painting.strokes.length') # not working
0480fc790527 paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1525
diff changeset
113 @fire('paintingChanged', @painting)
1519
a225c32bd9c8 new svg paint UI
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
114
a225c32bd9c8 new svg paint UI
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
115 onResize: (ev) ->
1570
0480fc790527 paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1525
diff changeset
116 @size = [@$.parent.offsetWidth, @$.parent.offsetHeight]
0480fc790527 paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1525
diff changeset
117 @$.paint.attributes.viewBox.value = "0 0 #{@size[0]} #{@size[1]}"
1576
ef7ddef3acb5 clean up Painting & Stroke. hover now shows up. add Clear command
Drew Perttula <drewp@bigasterisk.com>
parents: 1573
diff changeset
118 @painting.setSize(@size)
1525
1849713b0d73 light9-simulation element work
Drew Perttula <drewp@bigasterisk.com>
parents: 1522
diff changeset
119
1849713b0d73 light9-simulation element work
Drew Perttula <drewp@bigasterisk.com>
parents: 1522
diff changeset
120
1849713b0d73 light9-simulation element work
Drew Perttula <drewp@bigasterisk.com>
parents: 1522
diff changeset
121 Polymer
1849713b0d73 light9-simulation element work
Drew Perttula <drewp@bigasterisk.com>
parents: 1522
diff changeset
122 is: "light9-simulation"
1849713b0d73 light9-simulation element work
Drew Perttula <drewp@bigasterisk.com>
parents: 1522
diff changeset
123 properties: {
1577
e8161fdda8ad paint shows deviceattrs from graph
Drew Perttula <drewp@bigasterisk.com>
parents: 1576
diff changeset
124 graph: { type: Object }
1525
1849713b0d73 light9-simulation element work
Drew Perttula <drewp@bigasterisk.com>
parents: 1522
diff changeset
125 layers: { type: Object }
1570
0480fc790527 paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1525
diff changeset
126 solution: { type: Object }
1525
1849713b0d73 light9-simulation element work
Drew Perttula <drewp@bigasterisk.com>
parents: 1522
diff changeset
127 }
1849713b0d73 light9-simulation element work
Drew Perttula <drewp@bigasterisk.com>
parents: 1522
diff changeset
128 listeners: [
1849713b0d73 light9-simulation element work
Drew Perttula <drewp@bigasterisk.com>
parents: 1522
diff changeset
129 "onLayers(layers)"
1849713b0d73 light9-simulation element work
Drew Perttula <drewp@bigasterisk.com>
parents: 1522
diff changeset
130 ]
1849713b0d73 light9-simulation element work
Drew Perttula <drewp@bigasterisk.com>
parents: 1522
diff changeset
131 ready: ->
1849713b0d73 light9-simulation element work
Drew Perttula <drewp@bigasterisk.com>
parents: 1522
diff changeset
132 null
1849713b0d73 light9-simulation element work
Drew Perttula <drewp@bigasterisk.com>
parents: 1522
diff changeset
133 onLayers: (layers) ->
1570
0480fc790527 paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1525
diff changeset
134 log('upd', layers)
0480fc790527 paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1525
diff changeset
135
1577
e8161fdda8ad paint shows deviceattrs from graph
Drew Perttula <drewp@bigasterisk.com>
parents: 1576
diff changeset
136
e8161fdda8ad paint shows deviceattrs from graph
Drew Perttula <drewp@bigasterisk.com>
parents: 1576
diff changeset
137 Polymer
e8161fdda8ad paint shows deviceattrs from graph
Drew Perttula <drewp@bigasterisk.com>
parents: 1576
diff changeset
138 is: "light9-device-settings",
e8161fdda8ad paint shows deviceattrs from graph
Drew Perttula <drewp@bigasterisk.com>
parents: 1576
diff changeset
139 properties: {
e8161fdda8ad paint shows deviceattrs from graph
Drew Perttula <drewp@bigasterisk.com>
parents: 1576
diff changeset
140 graph: { type: Object }
e8161fdda8ad paint shows deviceattrs from graph
Drew Perttula <drewp@bigasterisk.com>
parents: 1576
diff changeset
141 subj: {type: String, notify: true},
e8161fdda8ad paint shows deviceattrs from graph
Drew Perttula <drewp@bigasterisk.com>
parents: 1576
diff changeset
142 label: {type: String, notify: true},
e8161fdda8ad paint shows deviceattrs from graph
Drew Perttula <drewp@bigasterisk.com>
parents: 1576
diff changeset
143 attrs: {type: Array, notify: true},
e8161fdda8ad paint shows deviceattrs from graph
Drew Perttula <drewp@bigasterisk.com>
parents: 1576
diff changeset
144 },
e8161fdda8ad paint shows deviceattrs from graph
Drew Perttula <drewp@bigasterisk.com>
parents: 1576
diff changeset
145 observers: [
e8161fdda8ad paint shows deviceattrs from graph
Drew Perttula <drewp@bigasterisk.com>
parents: 1576
diff changeset
146 'onSubj(graph, subj)'
e8161fdda8ad paint shows deviceattrs from graph
Drew Perttula <drewp@bigasterisk.com>
parents: 1576
diff changeset
147 ]
e8161fdda8ad paint shows deviceattrs from graph
Drew Perttula <drewp@bigasterisk.com>
parents: 1576
diff changeset
148 ready: ->
e8161fdda8ad paint shows deviceattrs from graph
Drew Perttula <drewp@bigasterisk.com>
parents: 1576
diff changeset
149 @label = "aura2"
e8161fdda8ad paint shows deviceattrs from graph
Drew Perttula <drewp@bigasterisk.com>
parents: 1576
diff changeset
150 @attrs = [
e8161fdda8ad paint shows deviceattrs from graph
Drew Perttula <drewp@bigasterisk.com>
parents: 1576
diff changeset
151 {attr: 'rx', val: .03},
e8161fdda8ad paint shows deviceattrs from graph
Drew Perttula <drewp@bigasterisk.com>
parents: 1576
diff changeset
152 {attr: 'color', val: '#ffe897'},
e8161fdda8ad paint shows deviceattrs from graph
Drew Perttula <drewp@bigasterisk.com>
parents: 1576
diff changeset
153 ]
e8161fdda8ad paint shows deviceattrs from graph
Drew Perttula <drewp@bigasterisk.com>
parents: 1576
diff changeset
154 onSubj: (graph, @subj) ->
1579
3c8c9a1deece drag LightSample into timeline to make a new effect
Drew Perttula <drewp@bigasterisk.com>
parents: 1578
diff changeset
155 graph.runHandler(@loadAttrs.bind(@), "loadAttrs #{@subj}")
1577
e8161fdda8ad paint shows deviceattrs from graph
Drew Perttula <drewp@bigasterisk.com>
parents: 1576
diff changeset
156 loadAttrs: ->
1586
901767febb47 bind 'this' in U shorthand.
Drew Perttula <drewp@bigasterisk.com>
parents: 1579
diff changeset
157 U = (x) => @graph.Uri(x)
1577
e8161fdda8ad paint shows deviceattrs from graph
Drew Perttula <drewp@bigasterisk.com>
parents: 1576
diff changeset
158 @attrs = []
e8161fdda8ad paint shows deviceattrs from graph
Drew Perttula <drewp@bigasterisk.com>
parents: 1576
diff changeset
159 for s in @graph.objects(U(@subj), U(':setting'))
e8161fdda8ad paint shows deviceattrs from graph
Drew Perttula <drewp@bigasterisk.com>
parents: 1576
diff changeset
160 attr = @graph.uriValue(s, U(':deviceAttr'))
e8161fdda8ad paint shows deviceattrs from graph
Drew Perttula <drewp@bigasterisk.com>
parents: 1576
diff changeset
161 attrLabel = @graph.stringValue(attr, U('rdfs:label'))
1587
2c973af2b13e refactor light9-device-settings settings table
Drew Perttula <drewp@bigasterisk.com>
parents: 1586
diff changeset
162 @attrs.push({attr: attrLabel, val: @settingValue(s)})
2c973af2b13e refactor light9-device-settings settings table
Drew Perttula <drewp@bigasterisk.com>
parents: 1586
diff changeset
163 @attrs = _.sortBy(@attrs, 'attr')
1577
e8161fdda8ad paint shows deviceattrs from graph
Drew Perttula <drewp@bigasterisk.com>
parents: 1576
diff changeset
164
1587
2c973af2b13e refactor light9-device-settings settings table
Drew Perttula <drewp@bigasterisk.com>
parents: 1586
diff changeset
165 settingValue: (s) ->
2c973af2b13e refactor light9-device-settings settings table
Drew Perttula <drewp@bigasterisk.com>
parents: 1586
diff changeset
166 U = (x) => @graph.Uri(x)
2c973af2b13e refactor light9-device-settings settings table
Drew Perttula <drewp@bigasterisk.com>
parents: 1586
diff changeset
167 for pred in [U(':value'), U(':scaledValue')]
2c973af2b13e refactor light9-device-settings settings table
Drew Perttula <drewp@bigasterisk.com>
parents: 1586
diff changeset
168 try
2c973af2b13e refactor light9-device-settings settings table
Drew Perttula <drewp@bigasterisk.com>
parents: 1586
diff changeset
169 return @graph.stringValue(s, pred)
2c973af2b13e refactor light9-device-settings settings table
Drew Perttula <drewp@bigasterisk.com>
parents: 1586
diff changeset
170 catch
2c973af2b13e refactor light9-device-settings settings table
Drew Perttula <drewp@bigasterisk.com>
parents: 1586
diff changeset
171 null
2c973af2b13e refactor light9-device-settings settings table
Drew Perttula <drewp@bigasterisk.com>
parents: 1586
diff changeset
172 try
2c973af2b13e refactor light9-device-settings settings table
Drew Perttula <drewp@bigasterisk.com>
parents: 1586
diff changeset
173 return @graph.floatValue(s, pred)
2c973af2b13e refactor light9-device-settings settings table
Drew Perttula <drewp@bigasterisk.com>
parents: 1586
diff changeset
174 catch
2c973af2b13e refactor light9-device-settings settings table
Drew Perttula <drewp@bigasterisk.com>
parents: 1586
diff changeset
175 null
2c973af2b13e refactor light9-device-settings settings table
Drew Perttula <drewp@bigasterisk.com>
parents: 1586
diff changeset
176 throw new Error("no value for #{s}")
1570
0480fc790527 paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1525
diff changeset
177
0480fc790527 paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1525
diff changeset
178 Polymer
0480fc790527 paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1525
diff changeset
179 is: "light9-paint"
0480fc790527 paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1525
diff changeset
180 properties: {
0480fc790527 paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1525
diff changeset
181 painting: { type: Object }
1626
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1587
diff changeset
182 client: { type: Object }
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1587
diff changeset
183 graph: { type: Object }
1570
0480fc790527 paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1525
diff changeset
184 }
0480fc790527 paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1525
diff changeset
185
0480fc790527 paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1525
diff changeset
186 ready: () ->
0480fc790527 paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1525
diff changeset
187 # couldn't make it work to bind to painting's notifyPath events
0480fc790527 paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1525
diff changeset
188 @$.canvas.addEventListener('paintingChanged', @paintingChanged.bind(@))
0480fc790527 paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1525
diff changeset
189 @$.solve.addEventListener('response', @onSolve.bind(@))
1626
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1587
diff changeset
190
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1587
diff changeset
191 @clientSendThrottled = _.throttle(@client.send.bind(@client), 60)
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1587
diff changeset
192 @bestMatchPending = false
1570
0480fc790527 paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1525
diff changeset
193
0480fc790527 paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1525
diff changeset
194 paintingChanged: (ev) ->
1626
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1587
diff changeset
195 U = (x) => @graph.Uri(x)
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1587
diff changeset
196
1570
0480fc790527 paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1525
diff changeset
197 @painting = ev.detail
0480fc790527 paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1525
diff changeset
198 @$.solve.body = JSON.stringify(@painting.getDoc())
1626
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1587
diff changeset
199 #@$.solve.generateRequest()
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1587
diff changeset
200
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1587
diff changeset
201 @$.bestMatches.body = JSON.stringify({
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1587
diff changeset
202 painting: @painting.getDoc(),
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1587
diff changeset
203 devices: [
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1587
diff changeset
204 U('dev:aura1'), U('dev:aura2'), U('dev:aura3'), U('dev:aura4'), U('dev:aura5'),
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1587
diff changeset
205 U('dev:q1'), U('dev:q2'), U('dev:q3'),
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1587
diff changeset
206 ]})
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1587
diff changeset
207
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1587
diff changeset
208 send = =>
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1587
diff changeset
209 @$.bestMatches.generateRequest().completes.then (r) =>
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1587
diff changeset
210 @clientSendThrottled(r.response.settings)
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1587
diff changeset
211 if @bestMatchPending
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1587
diff changeset
212 @bestMatchPending = false
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1587
diff changeset
213 send()
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1587
diff changeset
214
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1587
diff changeset
215 if @$.bestMatches.loading
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1587
diff changeset
216 @bestMatchPending = true
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1587
diff changeset
217 else
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1587
diff changeset
218 send()
1570
0480fc790527 paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1525
diff changeset
219
0480fc790527 paint now looks for best match
Drew Perttula <drewp@bigasterisk.com>
parents: 1525
diff changeset
220 onSolve: (response) ->
1626
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1587
diff changeset
221 U = (x) => @graph.Uri(x)
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1587
diff changeset
222
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1587
diff changeset
223 sample = @$.solve.lastResponse.bestMatch.uri
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1587
diff changeset
224 settingsList = []
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1587
diff changeset
225 for s in @graph.objects(sample, U(':setting'))
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1587
diff changeset
226 try
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1587
diff changeset
227 v = @graph.floatValue(s, U(':value'))
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1587
diff changeset
228 catch
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1587
diff changeset
229 v = @graph.stringValue(s, U(':scaledValue'))
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1587
diff changeset
230 row = [@graph.uriValue(s, U(':device')), @graph.uriValue(s, U(':deviceAttr')), v]
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1587
diff changeset
231 settingsList.push(row)
5d2dcae1a7c6 paint can now do best matches on multiple lights at once
drewp@bigasterisk.com
parents: 1587
diff changeset
232 @client.send(settingsList)