-
Notifications
You must be signed in to change notification settings - Fork 19
/
gridgets.py
123 lines (99 loc) · 3.39 KB
/
gridgets.py
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
import logging
from palette import palette
##############################################################################
# And first, a few constants
ARROWS = "UP DOWN LEFT RIGHT".split()
MENU = "BUTTON_1 BUTTON_2 BUTTON_3 BUTTON_4".split()
##############################################################################
class Surface(object):
def __init__(self, parent):
# Initialize our "framebuffer"
self.leds = {}
for led in parent:
self.leds[led] = palette.BLACK
# Setup the masked surface
# (By default, it filters out all display)
self.parent = MaskedSurface(parent)
def __iter__(self):
return self.leds.__iter__()
def __getitem__(self, led):
return self.leds[led]
def __setitem__(self, led, color):
if led not in self.leds:
logging.error("LED {} does not exist!".format(led))
else:
current_color = self.leds[led]
if color != current_color:
self.leds[led] = color
if self.parent:
self.parent[led] = color
##############################################################################
class MaskedSurface(object):
def __init__(self, parent):
self.parent = parent
self.mask = set() # leds that are ALLOWED
def __iter__(self):
return self.mask.__iter__()
def __setitem__(self, led, color):
if led in self.mask:
self.parent[led] = color
##############################################################################
class Gridget(object):
def pad_pressed(self, row, column, velocity):
pass
def button_pressed(self, button):
pass
##############################################################################
class Menu(Gridget):
def __init__(self, grid):
self.grid = grid
self.surface = Surface(grid.surface)
self.menu = dict(
BUTTON_1 = [
self.grid.loopcontroller,
self.grid.scalepicker,
],
BUTTON_2 = [
self.grid.notepickers,
],
BUTTON_3 = [
self.grid.instrumentpickers,
self.grid.arpconfigs,
self.grid.latchconfigs,
],
BUTTON_4 = [
self.grid.colorpicker,
self.grid.faders,
self.grid.bpmsetter,
],
)
self.current = "BUTTON_2"
self.draw()
def draw(self):
for button in self.menu:
if button == self.current:
self.surface[button] = palette.MENU[1]
else:
self.surface[button] = palette.MENU[0]
def button_pressed(self, button):
entries = self.menu[button]
if button == self.current:
# Cycle through the entries of one menu
entries.append(entries.pop(0))
cycle = True
else:
# Switch to another menu
self.current = button
cycle = False
entry = entries[0]
# Resolve the exact gridget
if isinstance(entry, list):
gridget = entry[self.grid.channel]
else:
gridget = entry
# Special case for the notepicker
if len(entries) == 1 and cycle:
gridget.cycle()
else:
self.grid.focus(gridget)
self.draw()