-
Notifications
You must be signed in to change notification settings - Fork 22
/
__old_streamdeck.lua
273 lines (247 loc) · 7.24 KB
/
__old_streamdeck.lua
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
-- Base Button Class
Button = {}
function Button:__tostring()
if rawget(self, "type") then -- only classes have field "type"
return "Class: "..tostring(self.type)
else -- instances of classes do not have field "type"
return
"Type: "..tostring(self.type)
end
end
function Button:newChildClass(type)
-- Inspired by https://stackoverflow.com/questions/40004898/child-class-constructor-method-in-lua
self.__index = self
return setmetatable({
type = type or "none",
parentClass = self,
__tostring = self.__tostring
}, self)
end
function Button:new()
self.__index = self
local o = {
titles = {},
images = {},
pressFns = {},
stateIdx = 0
}
return setmetatable(o, self)
end
function Button:del()
-- noop in the base class
print("ERROR: Button:del() called on base class")
end
function Button:update(deck, buttonNum)
-- noop in the base class
print("ERROR: Button:update() called on base class")
end
function Button:pressEvent(deck, buttonMgr, buttonNum, pressed)
-- noop in the base class
print("ERROR: Button:pressEvent() called on base class")
end
function Button:setStateIdx(idx)
self.stateIdx = idx
self:update()
end
-- Class for buttons with solid colors
ButtonColor = Button:newChildClass("color")
function ButtonColor:new(color)
local o = ButtonColor.parentClass.new(self)
o.color = color or hs.drawing.color.black
return o
end
function ButtonColor:setColor(color)
self.color = color
end
function ButtonColor:update(deck, buttonNum)
print("ButtonColor:update for:" .. tostring(buttonNum))
deck:setButtonColor(buttonNum, self.color)
end
function ButtonColor:pressEvent(deck, buttonMgr, buttonNum, pressed)
-- FIXME: Something something
end
-- Class for buttons with random colors
ButtonRandomColor = Button:newChildClass("randomColor")
function ButtonRandomColor:new(color)
local o = ButtonRandomColor.parentClass.new(self)
o.color = color or {
alpha = 1,
red = math.random(),
green = math.random(),
blue = math.random()
}
return o
end
function ButtonRandomColor:update(deck, buttonNum)
print("ButtonColor:update for:" .. tostring(buttonNum))
deck:setButtonColor(buttonNum, self.color)
end
function ButtonRandomColor:pressEvent(deck, buttonMgr, buttonNum, pressed)
if ~pressed then
self.color = {
alpha = 1,
red = math.random(),
green = math.random(),
blue = math.random()
}
end
end
-- Class for buttons with images
ButtonImage = Button:newChildClass("image")
function ButtonImage:new(image)
local o = ButtonImage.parentClass.new(self)
o.image = image
return o
end
function ButtonImage:setImage(image)
self.image = image
end
function ButtonImage:update(deck, buttonNum)
print("ButtonImage:update for:" .. tostring(buttonNum))
deck:setButtonImage(buttonNum, self.image)
end
-- Base class for a page of buttons
Page = {}
function Page:__tostring()
if rawget(self, "type") then -- only classes have field "type"
return "Class: "..tostring(self.type)
else -- instances of classes do not have field "type"
return
"Type: "..tostring(self.type)
end
end
function Page:newChildClass(type)
-- Inspired by https://stackoverflow.com/questions/40004898/child-class-constructor-method-in-lua
self.__index = self
return setmetatable({
type = type or "none",
parentClass = self,
__tostring = self.__tostring
}, self)
end
function Page:new(deck, rows, cols)
self.__index = self
local o = {
rows = rows,
cols = cols,
buttons = {}
}
for row = 1, rows do
o.buttons[row] = {}
for col = 1, cols do
o.buttons[row][col] = ButtonRandomColor:new() -- FIXME: Put this back at some point: hs.drawing.color.black)
end
end
return setmetatable(o, self)
end
function Page:createButton(row, col, button)
if (self.buttons[row][col] ~= nil) then
self.buttons[row][col]:del()
end
self.buttons[row][col] = button
end
function Page:getButton(row, col)
return self.buttons[row][col]
end
function Page:update(deck)
local i = 1
for row = 1, self.rows do
for col = 1, self.cols do
local button = self:getButton(row, col)
print(button)
button:update(deck, i)
i = i + 1
end
end
end
function Page:pressEvent(deck, buttonMgr, buttonNum, pressed)
local button = self.buttons[buttonNum]
if button == nil then
print("ERROR: Button " .. tostring(buttonNum) .. " is nil")
return
end
print(hs.inspect(button))
button:pressEvent(deck, buttonMgr, buttonNum, pressed)
end
function Page:updateButton(deck, buttonNum)
self.buttons[buttonNum]:update(deck, buttonNum)
end
-- Class for a Page drawer
PageDrawer = Page:newChildClass("drawer")
function PageDrawer:new(deck, rows, cols)
local o = PageDrawer.parentClass.new(self, deck, rows, cols)
o.parentPageIdx = 1
end
function PageDrawer:setParentIdx(idx)
self.parentPageIdx = idx
end
function PageDrawer:pressEvent(deck, buttonMgr, buttonNum, pressed)
if pressed and buttonNum == 1 then
buttonMgr:setPage(self.parentPageIdx)
else
self.buttons[buttonNum]:pressEvent(deck, buttonMgr, buttonNum, pressed)
end
end
-- Class for a button manager for a given deck
-- FIXME: This should stop storing buttons directly, we should have a Page class that stores buttons
ButtonManager = {}
function ButtonManager:new(deck, firstPage)
local rows, cols = deck:buttonLayout()
local o = {
deck = deck,
buttonSize = deck:imageSize(),
rows = rows,
cols = cols,
pages = {},
curPageIdx = 1
}
table.insert(o.pages, firstPage or Page:new(deck, rows, cols))
setmetatable(o, self)
self.__index = self
return o
end
function ButtonManager:update()
self.pages[self.curPageIdx]:update(self.deck)
end
function ButtonManager:setPage(idx)
self.curPageIdx = idx
self:update()
end
function ButtonManager:getPage(idx)
return self.pages[idx]
end
function ButtonManager:indexForPage(page)
for i, p in ipairs(self.pages) do
if p == page then
return i
end
end
return nil
end
function ButtonManager:showPage(page)
local idx = self:indexForPage(page)
if idx ~= nil then
self:setPage(idx)
end
end
function ButtonManager:enableButtonCallback()
self.deck:buttonCallback(function(deck, buttonNum, pressed)
self.pages[self.curPageIdx]:pressEvent(deck, self, buttonNum, pressed)
self.pages[self.curPageIdx]:updateButton(deck, buttonNum)
end)
end
function ButtonManager:disableButtonCallback()
self.deck:buttonCallback(nil)
end
--[=====[
require("streamdeck")
deck = hs.streamdeck.getDevice(1)
bm = ButtonManager:new(deck)
page = bm:getPage(1)
page:createButton(1, 3, ButtonImage:new(hs.image.imageFromAppBundle("com.apple.Safari")))
bm:update()
bm:enableButtonCallback()
--]=====]
-- foo = Button:new()
-- foo.titles = {"On", "Off"}
-- foo.pressFns = {function() print("On") foo:setStateIdx(1) end, function() print("Off") foo:setStateIdx(0) end}