Verified Commit b5dfc265 authored by Rahix's avatar Rahix

refactor(personal-state): Use simple_menu

Signed-off-by: Rahix's avatarRahix <rahix@rahix.de>
parent ba4a314d
"""
Personal State Script
===========
With this script you can
=====================
"""
import buttons
import color
import display
import os
import personal_state
import simple_menu
states = [
("No State", personal_state.NO_STATE),
......@@ -18,75 +16,35 @@ states = [
]
def button_events():
"""Iterate over button presses (event-loop)."""
yield 0
button_pressed = False
while True:
v = buttons.read(buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT | buttons.TOP_RIGHT)
class StateMenu(simple_menu.Menu):
color_sel = color.WHITE
if v == 0:
button_pressed = False
def on_scroll(self, item, index):
personal_state.set(item[1], False)
if not button_pressed and v & buttons.BOTTOM_LEFT != 0:
button_pressed = True
yield buttons.BOTTOM_LEFT
def on_select(self, item, index):
personal_state.set(item[1], True)
os.exit()
if not button_pressed and v & buttons.BOTTOM_RIGHT != 0:
button_pressed = True
yield buttons.BOTTOM_RIGHT
def draw_entry(self, item, index, offset):
if item[1] == personal_state.NO_CONTACT:
bg = color.RED
fg = color.WHITE
elif item[1] == personal_state.CHAOS:
bg = color.CHAOSBLUE
fg = color.CHAOSBLUE_DARK
elif item[1] == personal_state.COMMUNICATION:
bg = color.COMMYELLOW
fg = color.COMMYELLOW_DARK
elif item[1] == personal_state.CAMP:
bg = color.CAMPGREEN
fg = color.CAMPGREEN_DARK
else:
bg = color.Color(100, 100, 100)
fg = color.Color(200, 200, 200)
if not button_pressed and v & buttons.TOP_RIGHT != 0:
button_pressed = True
yield buttons.TOP_RIGHT
COLOR1, COLOR2 = (color.CHAOSBLUE_DARK, color.CHAOSBLUE)
def draw_menu(disp, idx, offset):
disp.clear()
for y, i in enumerate(range(len(states) + idx - 3, len(states) + idx + 4)):
selected = states[i % len(states)]
disp.print(
" " + selected[0] + " " * (11 - len(selected[0])),
posy=offset + y * 20 - 40,
bg=COLOR1 if i % 2 == 0 else COLOR2,
)
disp.print(">", posy=20, fg=color.COMMYELLOW, bg=COLOR2 if idx % 2 == 0 else COLOR1)
disp.update()
def main():
disp = display.open()
numstates = len(states)
current, _ = personal_state.get()
for ev in button_events():
if ev == buttons.BOTTOM_RIGHT:
# Scroll down
draw_menu(disp, current, -8)
current = (current + 1) % numstates
state = states[current]
personal_state.set(state[1], False)
elif ev == buttons.BOTTOM_LEFT:
# Scroll up
draw_menu(disp, current, 8)
current = (current + numstates - 1) % numstates
state = states[current]
personal_state.set(state[1], False)
elif ev == buttons.TOP_RIGHT:
state = states[current]
personal_state.set(state[1], True)
# Select & start
disp.clear().update()
disp.close()
os.exit(0)
draw_menu(disp, current, 0)
self.disp.print(" " + str(item[0]) + " " * 9, posy=offset, fg=fg, bg=bg)
if __name__ == "__main__":
main()
StateMenu(states).run()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment