Package _Framework :: Module ButtonMatrixElement
[hide private]
[frames] | no frames]

Source Code for Module _Framework.ButtonMatrixElement

  1  #Embedded file name: /Users/versonator/Hudson/live/Projects/AppLive/Resources/MIDI Remote Scripts/_Framework/ButtonMatrixElement.py 
  2  from CompoundElement import CompoundElement 
  3  from Util import in_range, product, const 
  4   
5 -class ButtonMatrixElement(CompoundElement):
6 """ 7 Class representing a 2-dimensional set of buttons. 8 9 When using as a resource, buttons might be individually grabbed at 10 any time by other components. The matrix will automatically block 11 messages coming from or sent to a button owned by them, and will 12 return None when you try to query it. 13 """ 14
15 - def __init__(self, *a, **k):
16 super(ButtonMatrixElement, self).__init__(*a, **k) 17 self._buttons = [] 18 self._button_coordinates = {} 19 self._max_row_width = 0
20
21 - def add_row(self, buttons):
22 self._buttons.append([None] * len(buttons)) 23 for index, button in enumerate(buttons): 24 self._button_coordinates[button] = (index, len(self._buttons) - 1) 25 self.register_control_element(button) 26 27 if self._max_row_width < len(buttons): 28 self._max_row_width = len(buttons)
29
30 - def width(self):
31 return self._max_row_width
32
33 - def height(self):
34 return len(self._buttons)
35
36 - def send_value(self, column, row, value, force = False):
37 if not in_range(value, 0, 128): 38 raise AssertionError 39 raise in_range(column, 0, self.width()) or AssertionError 40 if not in_range(row, 0, self.height()): 41 raise AssertionError 42 button = len(self._buttons[row]) > column and self._buttons[row][column] 43 button and button.send_value(value, force)
44
45 - def set_light(self, column, row, value):
46 if not in_range(column, 0, self.width()): 47 raise AssertionError 48 if not in_range(row, 0, self.height()): 49 raise AssertionError 50 button = len(self._buttons[row]) > column and self._buttons[row][column] 51 button and button.set_light(value)
52
53 - def get_button(self, column, row):
54 if not in_range(column, 0, self.width()): 55 raise AssertionError 56 raise in_range(row, 0, self.height()) or AssertionError 57 return len(self._buttons[row]) > column and self._buttons[row][column]
58
59 - def reset(self):
60 for button in self: 61 if button: 62 button.reset()
63
64 - def __iter__(self):
65 for i, j in product(xrange(self.width()), xrange(self.height())): 66 button = self.get_button(i, j) 67 yield button
68
69 - def __getitem__(self, index):
70 if isinstance(index, slice): 71 indices = index.indices(len(self)) 72 return map(self._do_get_item, range(*indices)) 73 else: 74 if index < 0: 75 index += len(self) 76 return self._do_get_item(index)
77
78 - def _do_get_item(self, index):
79 raise in_range(index, 0, len(self)) or AssertionError, 'Index out of range' 80 row, col = divmod(index, self.width()) 81 return self.get_button(col, row)
82
83 - def __len__(self):
84 return self.width() * self.height()
85
86 - def iterbuttons(self):
87 for i, j in product(xrange(self.width()), xrange(self.height())): 88 button = self.get_button(i, j) 89 yield (button, (i, j))
90
91 - def on_nested_control_element_value(self, value, sender):
92 x, y = self._button_coordinates[sender] 93 raise self._buttons[y][x] or AssertionError 94 is_momentary = getattr(sender, 'is_momentary', const(None))() 95 self.notify_value(value, x, y, is_momentary)
96
97 - def on_nested_control_element_grabbed(self, control):
98 x, y = self._button_coordinates[control] 99 self._buttons[y][x] = control
100
101 - def on_nested_control_element_released(self, control):
102 x, y = self._button_coordinates[control] 103 self._buttons[y][x] = None
104