forked from Hack-a-Day/Vectorscope
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu.py
285 lines (227 loc) · 8.65 KB
/
menu.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
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
import screennorm
import keyboardcb
import keyleds
import joystick
import asyncio
import gc9a01
import vectoros
import vos_debug
from vos_state import vos_state
screen=vectoros.get_screen()
BACK=-1
EXIT=1
CONT=0
SUBMENU=[]
def m_back(arg):
return BACK
def m_exit(arg):
return EXIT
class Menu:
"""
A class that manages a menu.
Attributes:
fg (int): The foreground color.
bg (int): The background color.
clear_after (bool): A flag to indicate if the menu should be cleared after exit.
joycontroller (Joystick): The joystick controller.
scanrate (int): The scan rate.
cursor (int): The position of the screen cursor.
dispmenu (int): The top line of the display.
current (list): The current menu string.
level (int): The current level in the submenus.
stack (list): The stack of how we got here.
clear_after (bool): A flag to indicate if the menu should be cleared after exit.
fg (int): The foreground color.
bg (int): The background color.
update_callback (function): A function to call to update the menu in real time.
"""
def __init__(self, fg_color=gc9a01.color565(45, 217, 80), bg_color=gc9a01.color565(0, 0, 0),cursor_fg=gc9a01.color565(120,247,180),
cursor_bg=None,clear_after=False, joy_controller=None, scan_rate=0):
"""
The constructor for Menu class.
Args:
fg_color (int): The foreground color. Default is gc9a01.color565(243,191,16).
bg_color (int): The background color. Default is gc9a01.color565(26,26,26).
clear_after (bool): A flag to indicate if the menu should be cleared after exit. Default is False.
joycontroller (Joystick): The joystick controller. Default is None.
scanrate (int): The scan rate. Default is 0.
"""
if joy_controller != None:
self.joy=joy_controller
self._extjoy=True
else:
self.joy=None
self._extjoy=False
if scan_rate!=0:
joystick.Joystick.run(scan_rate)
self.cursor=0
self.dispmenu=0
self.current=[]
self.level=0
self.stack=[]
self.clear_after=clear_after
self.fg=fg_color
self.bg=bg_color
if cursor_fg==None:
self.cfg=self.bg
else:
self.cfg=cursor_fg
if cursor_bg==None:
self.cbg=self.fg
else:
self.cbg=cursor_bg
self.update_callback=None
self.font=None
self.scale=1.0
def __enter__(self):
return self
def __exit__(self,exc_type,exc_val,exc_tb):
if self.joy!=None:
self.joy.detach()
def detach(self):
"""
Method to detach the joystick from the menu.
Returns:
Task: The created task.
"""
self.joy.detach()
def set_font(self,font,scale=1.0):
"""
Method to set a font and scale factor.
Default: use default font (set to None)
"*": use default vector font (romans)
Or you can load a vector font and pass it
Args:
font (font or string or None): See above
scale (float): Text scale (defaults to 1.0)
"""
if font=="*":
self.font=screen.get_vfont()
else:
self.font=font
self.scale=scale
async def menu_custom(self):
"""
Method you can override to customize the menu in real time
Or, if you don't want to subclass, you can use set_callback
and the default will call that
"""
if self.update_callback!=None:
try:
await self.update_callback(self)
except TypeError:
pass
def set_callback(self,func):
"""
Method to set a callback function.
Args:
func (function): The function to set as the callback.
"""
self.update_callback=func
async def menu_update(self):
"""
Method to update the menu with cursor, scrolling, etc.
This method updates the menu based on the current state of the cursor and calls the custom update function if it is set.
"""
screen.clear(self.bg)
await self.menu_custom()
for i in range(0,min(4,len(self.current))):
if i==self.cursor:
xfg=self.cfg
xbg=self.cbg
else:
xfg=self.fg
xbg=self.bg
# screen.tft.fill_rect(24,40*(i+1),195,40,xbg)
if self.font==None:
screen.text(24,40*(i+1),self.current[self.dispmenu+i][0],xfg,xbg)
else:
screen.text_font(self.font,24,40*(i+1)+20,self.current[self.dispmenu+i][0],xfg,self.scale)
# the controller callback for the keyboard
# all the real work happens here
async def _menu_control(self,key):
"""
Method to handle joystick events.
This method updates the menu based on the joystick event. It can move the cursor, select a menu item,
go to a submenu, or exit the menu.
Args:
key (int): The keycode of the joystick event.
"""
rv=0
if self.level<=0:
return # no menu
if key==keyleds.JOY_UP:
if self.cursor>0:
self.cursor=self.cursor-1
else:
if self.dispmenu>0:
self.dispmenu=self.dispmenu-1
await self.menu_update()
if key==keyleds.JOY_DN:
if self.cursor<min(3,len(self.current)-1):
self.cursor=self.cursor+1
else:
if self.dispmenu<len(self.current)-4:
self.dispmenu=self.dispmenu+1
await self.menu_update()
if key==keyleds.JOY_PRESS or key==keyleds.JOY_RT:
cmd=self.current[self.cursor+self.dispmenu][1]
arg=self.current[self.cursor+self.dispmenu][2]
if cmd==None: # replace with ret val from built-in callback
rv=-1
elif cmd==[]:
self.stack.append(self.current)
self.current=arg
self.cursor=0
self.dispmenu=0
self.level=self.level+1
else:
if len(self.current[self.cursor+self.dispmenu])<4:
rv=self.current[self.cursor+self.dispmenu][1](self.current[self.cursor+self.dispmenu][2])
else:
rv=await self.current[self.cursor+self.dispmenu][1](self.current[self.cursor+self.dispmenu][2])
if rv==1:
self.stack=[]
rv=-1 # make sure we exit
await self.menu_update()
if key==keyleds.JOY_LT or rv==-1:
self.level=self.level-1
if self.stack==[]:
self.level=-1
self.current=None
else:
self.current=self.stack.pop()
self.cursor=0
self.dispmenu=0
await self.menu_update()
# This is how you kick off the menu. The menu list is a list
# with a sublist for each entry. The sublists have three items:
# A text string, a function, and an argument
# Pro tip: pass a single list, tuple, etc as an argument and you can pass as much as you want
async def do_menu(self,menulist):
"""
Method to start the menu.
This method starts the menu and runs a loop until the level is less than 0. It also updates the menu after each event.
Args:
menulist (list): The list of menu items. Each item is a sublist with three items: a text string, a function, and an argument.
"""
if self._extjoy==False:
self.joy=joystick.Joystick(self._menu_control,True)
self.current=menulist
self.stack=[]
self.dispmenu=0
self.cursor=0
self.level=1
screen.clear()
await self.menu_update()
while self.level>=0:
await asyncio.sleep(0)
if self.clear_after:
screen.clear()
if (self._extjoy):
self.joy.detach()
# launch a tag (global, use in menus)
def launch(tag):
vos_state.show_menu=False # get the menu of the way
vectoros.launch_task(tag)
return EXIT