-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathinit.lua
More file actions
340 lines (310 loc) · 8.94 KB
/
Copy pathinit.lua
File metadata and controls
340 lines (310 loc) · 8.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
local Class = require "Base.Class"
local Base = require "SpottedStrip.Control"
local Encoder = require "Encoder"
local ViewControl = Class {}
ViewControl:include(Base)
function ViewControl:init(name)
Base.init(self)
self:setClassName("Unit.ViewControl")
self:setInstanceName(name)
self.encoderState = Encoder.Neutral
self.pinCount = 0
end
function ViewControl:getRootChain()
local parent = self.parent
return parent and parent.getRootChain and parent:getRootChain()
end
function ViewControl:configureBorderShape()
local view = self:callUp("getView")
local controls = view.controls
local n = #controls
-- Usually, the first visible control is after the insert control.
if n == 1 and self == controls[1] then
-- added this case for the control editor
self.controlGraphic:setCornerRadius(5, 5, 5, 5)
elseif self == controls[2] then
if self == controls[n] then
-- the one and only visible control
self.controlGraphic:setCornerRadius(5, 5, 5, 5)
else
-- the first visible control
self.controlGraphic:setCornerRadius(5, 5, 0, 0)
end
elseif self == controls[n] then
-- the last visible control
self.controlGraphic:setCornerRadius(0, 0, 5, 5)
else
-- in the middle
self.controlGraphic:setCornerRadius(0, 0, 0, 0)
end
end
function ViewControl:getBranch()
return self.branch
end
function ViewControl:serialize()
return {}
end
function ViewControl:deserialize(t)
end
function ViewControl:onFocused()
end
function ViewControl:onUnfocused()
end
function ViewControl:rename(name)
self:setInstanceName(name)
end
function ViewControl:getDisplayName()
return self:getInstanceName()
end
function ViewControl:enableHighlight()
self:configureBorderShape()
self.controlGraphic:setBorder(1)
if self:callUp("hasView", self.id) then
if self.moreLeft then
self.moreLeft:show()
else
local graphic = self:getControlGraphic()
self.moreLeft = app.MoreThisWay(app.left)
graphic:addChild(self.moreLeft)
end
end
self.focused = true
end
function ViewControl:disableHighlight()
self.controlGraphic:setBorder(0)
Encoder.set(Encoder.Neutral)
if self.moreLeft then
self.moreLeft:hide()
end
self.focused = false
end
function ViewControl:focus(notify)
if notify == nil then
notify = true
end
self:grabFocus("encoder", "upReleased", "cancelReleased")
self:enableHighlight()
if notify then
self:onFocused()
end
end
function ViewControl:unfocus(notify)
if notify == nil then
notify = true
end
self:releaseFocus("encoder", "upReleased", "cancelReleased")
self:disableHighlight()
if notify then
self:onUnfocused()
end
end
function ViewControl:dialPressed(shifted)
if shifted then
return false
end
self.encoderState = Encoder.Neutral
Encoder.set(self.encoderState)
return true
end
function ViewControl:onCursorMove(spot, spot0)
-- app.logInfo("%s.onCursorMove(%s,%s)",self,spot,spot0)
end
function ViewControl:onCursorEnter(spot)
-- app.logInfo("%s.onCursorEnter(%s)",self,spot)
self:addSubGraphic(self.subGraphic)
self:grabFocus("dialPressed", "dialReleased", "subPressed", "subReleased",
"homeReleased", "homePressed", "zeroPressed", "enterReleased",
"selectReleased")
Encoder.set(self.encoderState)
end
function ViewControl:onCursorLeave(spot)
-- app.logInfo("%s.onCursorLeave(%s)",self,spot)
self:removeSubGraphic(self.subGraphic)
self:releaseFocus("dialPressed", "dialReleased", "subPressed", "subReleased",
"homeReleased", "homePressed", "zeroPressed",
"enterReleased", "cancelReleased", "upReleased",
"selectReleased", "encoder")
self:disableHighlight()
end
function ViewControl:toggleContext()
local hadFocus = self:hasFocus("encoder")
local viewChanged = false
-- call up context view
if self:callUp("hasView", self.id) then
-- a context view for this control exists
if self:queryUp("currentViewName") == self.id then
self:callUp("switchView", "expanded", self)
viewChanged = true
else
self:callUp("leftJustify")
self:callUp("switchView", self.id, self)
viewChanged = true
end
elseif self:queryUp("currentViewName") ~= "expanded" then
self:callUp("switchView", "expanded")
viewChanged = true
end
-- Hack
if viewChanged then
-- switchView will fail call CursorEnter
self:callUp("disableSelection")
self:callUp("enableSelection")
end
if hadFocus and viewChanged then
-- need to grab focus again, since switchView will lose it
self:focus(false)
end
end
function ViewControl:createPinMark()
local Drawings = require "Drawings"
local graphic = app.Drawing(0, 0, app.SECTION_PLY, 64)
graphic:add(Drawings.Control.Pin)
self.controlGraphic:addChildOnce(graphic)
self.pinMark = graphic
end
function ViewControl:onPinned()
if self.pinCount == 0 then
if self.pinMark == nil then
self:createPinMark()
end
self.pinMark:show()
end
self.pinCount = self.pinCount + 1
end
function ViewControl:onUnpinned()
if self.pinCount == 1 then
self.pinMark:hide()
end
self.pinCount = self.pinCount - 1
end
function ViewControl:spotReleased(spot, shifted)
if shifted then
return false
end
if self.focused then
self:unfocus()
else
self:focus()
end
return true
end
function ViewControl:getFloatingMenuItems()
local t = {}
if self:callUp("hasView", self.id) then
if self:queryUp("currentViewName") == self.id then
t[1] = "collapse"
else
t[1] = "expand"
end
end
if self.canEdit then
t[#t + 1] = "edit"
end
if self.getPinControl then
local chain = self:getRootChain()
if chain and chain.isRoot then
local pinSetNames = chain:getPinSetNames()
local pinned, pinCount = chain:getPinSetMembership(self)
if #pinSetNames > 1 and #pinSetNames > pinCount then
t[#t + 1] = "pin to all"
end
if pinCount > 1 then
t[#t + 1] = "unpin from all"
end
for _, name in ipairs(pinSetNames) do
if pinned[name] then
t[#t + 1] = "unpin from " .. name
else
t[#t + 1] = "pin to " .. name
end
end
t[#t + 1] = "pin to <new>"
end
end
return t
end
function ViewControl:onFloatingMenuSelection(choice)
local Utils = require "Utils"
if choice == "expand" or choice == "collapse" then
self:toggleContext()
return true
elseif choice == "edit" then
self:callUp("doEditControl", self)
return true
elseif choice == "pin to all" then
local chain = self:getRootChain()
if chain and chain.isRoot then
chain:pinControlToAllPinSets(self)
end
elseif choice == "unpin from all" then
local chain = self:getRootChain()
if chain and chain.isRoot then
chain:unpinControlFromAllPinSets(self)
end
elseif choice == "pin to <new>" then
local chain = self:getRootChain()
if chain and chain.isRoot then
local Keyboard = require "Keyboard"
local kb = Keyboard("New PinSet", chain:suggestPinSetName(), true,
"NamingStuff")
local task = function(text)
if text then
local pinSet = chain:addPinSet{
name = text
}
pinSet:startViewModifications()
pinSet:pinControl(self)
pinSet:endViewModifications()
end
end
kb:subscribe("done", task)
kb:show()
end
return true
elseif Utils.startsWith(choice, "pin to ") then
local name = choice:sub(8)
local chain = self:getRootChain()
if chain and chain.isRoot then
local pinSet = chain:getPinSetByName(name)
pinSet:startViewModifications()
pinSet:pinControl(self)
pinSet:endViewModifications()
end
return true
elseif Utils.startsWith(choice, "unpin from ") then
local name = choice:sub(12)
local chain = self:getRootChain()
if chain and chain.isRoot then
local pinSet = chain:getPinSetByName(name)
pinSet:startViewModifications()
pinSet:unpinControl(self)
pinSet:endViewModifications()
end
return true
else
return Base.onFloatingMenuSelection(self, choice)
end
end
function ViewControl:enterReleased()
self:toggleContext()
return true
end
function ViewControl:upReleased(shifted)
if not shifted then
self:unfocus()
end
return true
end
function ViewControl:onRemove()
-- app.logInfo("%s:onRemove()",self)
if self.pinCount > 0 then
local chain = self:getRootChain()
if chain and chain.isRoot then
chain:unpinControlFromAllPinSets(self)
else
app.logInfo("%s:onRemove(): failed to get root chain.", self)
end
end
Base.onRemove(self)
end
return ViewControl