-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenulayout.py
146 lines (127 loc) · 4.84 KB
/
menulayout.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import displayio
import terminalio
from adafruit_display_text.label import Label
from adafruit_display_shapes.rect import Rect
from adafruit_displayio_layout.layouts.grid_layout import GridLayout
class BasicEntry():
def __init__(self):
raise OSError("BasicEntry is not supposed to be instantiated")
label_defaults = {
"font": terminalio.FONT,
"padding_left": 2,
"padding_right": 2,
"padding_top": -2,
"padding_bottom": -1,
"scale": 1,
"x": 0,
"y": 0,
}
@staticmethod
def complementary_color(bg_color: int):
fg_color = 0xFFFFFF
if bg_color > 0xFFFFFF or bg_color < 0:
raise OSError("invalid color")
if ((bg_color>>8)&(0xFF)) > 0xD0 or (((bg_color)&(0xFF)) > 0x7F and ((bg_color>>16)&(0xFF)) > 0x7F):
fg_color = 0
return {"color": fg_color, "background_color": bg_color}
@staticmethod
def highlight(label, yes):
if yes:
label._background_palette.make_opaque(0)
label.color = BasicEntry.complementary_color(label.background_color)["color"]
else:
label._background_palette.make_transparent(0)
label.color = 0xFFFFFF
class MenuEntry(BasicEntry):
def highlight(self, yes):
BasicEntry.highlight(self.label, yes)
def __init__(self, name, highlight_color=0xFFFFFF, **kwargs) -> None:
self.name = name
self.highlight_color = highlight_color
label_properties = BasicEntry.label_defaults.copy()
label_properties.update(kwargs)
label_properties.update(BasicEntry.complementary_color(self.highlight_color))
self.label = Label(text=self.name, **label_properties)
self.highlight(False)
class MenuMultiEntry(BasicEntry):
pass
class MenuLayout:
class Orientation:
VERTICAL = 0
HORIZONTAL = 1
@property
def _scrolling(self):
return len(self.menu_entries) > self.display_entries
def _set_index(self, no):
if self._scrolling:
if no >= len(self.menu_entries):
no = len(self.menu_entries)-1
elif no < 0:
no = 0
offset = int(self.display_entries / 2 - 0.5)
pos = int(self.cell_space*(-no+offset))
print("no:{} len:{} pos:{} off:{}".format(no, len(self.menu_entries), pos, offset))
if self.orientation == self.Orientation.VERTICAL:
self.layout.y = pos
elif self.orientation == self.Orientation.HORIZONTAL:
self.layout.x = pos
@property
def index(self):
return self._index
@index.setter
def index(self, index):
self.menu_entries[self._index].highlight(False)
self._index = index
self.menu_entries[self._index].highlight(True)
self._set_index(self._index)
def __init__(self, display, entries, display_entries=3, orientation=Orientation.VERTICAL, **kwargs):
self.display = display
self.orientation = orientation
self.group = displayio.Group()
self.menu_entries = []
self.display_entries = display_entries
self._index = 0
self._i_offset = 0
if len(entries) < display_entries:
self._i_offset = display_entries - len(entries)
for me in entries:
self.add_label(me)
space = w = h = x_off = y_off = 0
c = l = 1
if self.orientation == self.Orientation.VERTICAL:
w = display.width
l = len(self.menu_entries)
space = display.height / self.display_entries
h = space * l
y_off = int(space*self._i_offset/2 - 0.5)
elif self.orientation == self.Orientation.HORIZONTAL:
h = self.display.height
c = len(self.menu_entries)
space = display.width / self.display_entries
w = space * c
x_off = int(space*self._i_offset/2 - 0.5)
self.cell_space = space
#print("cs:{} w:{} h:{} c:{}, l:{}".format(self.cell_space,w,h,c,l))
self.layout = GridLayout(
x=x_off,
y=y_off,
width=w,
height=h,
grid_size=(c, l),
cell_padding=0,
divider_lines=False,
cell_anchor_point=(0.5, 0.5)
)
self.labels = []
for i in range (0,len(self.menu_entries)):
x = y = 0
if self.orientation == self.Orientation.VERTICAL:
y = i
elif self.orientation == self.Orientation.HORIZONTAL:
x = i
self.layout.add_content(self.menu_entries[i].label, grid_position=(x, y), cell_size=(1, 1))
self.group.append(self.layout)
def add_label(self, menu_entry, **kwargs):
self.menu_entries.append(menu_entry)
def show(self):
self.display.show(self.group)