mirror of https://git.sr.ht/~michalr/menu
color_scheme -> theme
This commit is contained in:
parent
0c21a3e34a
commit
b002dfcf4f
|
@ -35,7 +35,7 @@
|
|||
"action_type": "QUIT"
|
||||
}
|
||||
],
|
||||
"color_scheme": {
|
||||
"theme": {
|
||||
"menu_bg_color": "#4c566a",
|
||||
"message_bg_color": "#eceff4",
|
||||
"message_text_color": "#2e3440",
|
||||
|
|
42
menu.py
42
menu.py
|
@ -39,19 +39,19 @@ class Button:
|
|||
PRESS_OFFSET = 2
|
||||
HOVER_ANIMATION_STEPS = 5
|
||||
|
||||
def __init__(self, position: pygame.Rect, text: str, color_scheme: Dict, cbk: Callable[[], None]):
|
||||
def __init__(self, position: pygame.Rect, text: str, theme: Dict, cbk: Callable[[], None]):
|
||||
self.position: pygame.Rect
|
||||
self.set_position(position)
|
||||
self.text = text
|
||||
self.color_scheme = color_scheme
|
||||
self.theme = theme
|
||||
self.cbk = cbk
|
||||
self.font = pygame.freetype.SysFont(name="Sans", size=20)
|
||||
text_rect = self.font.get_rect(self.text)
|
||||
self.text_pos = pygame.Rect((self.position.left + (self.position.width - text_rect.width)/2),
|
||||
(self.position.top + (self.position.height - text_rect.height)/2),
|
||||
text_rect.width, text_rect.height)
|
||||
self.bg_color = pygame.Color(self.color_scheme["btn_bg_color"])
|
||||
self.hover_bg_color = pygame.Color(self.color_scheme["btn_hover_color"])
|
||||
self.bg_color = pygame.Color(self.theme["btn_bg_color"])
|
||||
self.hover_bg_color = pygame.Color(self.theme["btn_hover_color"])
|
||||
self.hover_animation_step = 0
|
||||
|
||||
def get_bg_color(self):
|
||||
|
@ -89,7 +89,7 @@ class Button:
|
|||
|
||||
def draw(self, screen: pygame.Surface):
|
||||
pygame.draw.rect(screen, self.get_bg_color(), self.get_position(), 0)
|
||||
self.font.render_to(screen, self.get_text_pos(), self.text, fgcolor=self.color_scheme["btn_text_color"])
|
||||
self.font.render_to(screen, self.get_text_pos(), self.text, fgcolor=self.theme["btn_text_color"])
|
||||
|
||||
def pos_is_inside(self, pos: Tuple) -> bool:
|
||||
return (self.position.left < pos[0] and (self.position.left + self.position.width) > pos[0]) and \
|
||||
|
@ -102,14 +102,14 @@ class Button:
|
|||
|
||||
|
||||
class MessageBoard(IBoard):
|
||||
def __init__(self, screen_rect: pygame.Rect, text: str, color_scheme: Dict):
|
||||
def __init__(self, screen_rect: pygame.Rect, text: str, theme: Dict):
|
||||
self.text = text
|
||||
self.font = pygame.freetype.SysFont(name="Sans", size=30)
|
||||
text_rect = self.font.get_rect(self.text)
|
||||
self.text_pos = pygame.Rect((screen_rect.left + (screen_rect.width - text_rect.width)/2),
|
||||
(screen_rect.top + (screen_rect.height - text_rect.height)/2),
|
||||
text_rect.width, text_rect.height)
|
||||
self.color_scheme = color_scheme
|
||||
self.theme = theme
|
||||
self.was_drawn_already = False
|
||||
|
||||
def handle_event(self, _):
|
||||
|
@ -117,23 +117,23 @@ class MessageBoard(IBoard):
|
|||
|
||||
def draw(self, screen: pygame.Surface, force_redraw: bool):
|
||||
if not self.was_drawn_already or force_redraw:
|
||||
screen.fill(self.color_scheme["message_bg_color"])
|
||||
self.font.render_to(screen, self.text_pos, self.text, fgcolor=self.color_scheme["message_text_color"])
|
||||
screen.fill(self.theme["message_bg_color"])
|
||||
self.font.render_to(screen, self.text_pos, self.text, fgcolor=self.theme["message_text_color"])
|
||||
self.was_drawn_already = True
|
||||
|
||||
|
||||
class MenuBoard(IBoard):
|
||||
BUTTON_MARGINS = 10
|
||||
|
||||
def __init__(self, screen_rect: pygame.Rect, buttons: List[ButtonDef], color_scheme: Dict):
|
||||
def __init__(self, screen_rect: pygame.Rect, buttons: List[ButtonDef], theme: Dict):
|
||||
self.rows = math.floor(math.sqrt(len(buttons)))
|
||||
self.cols = math.ceil(math.sqrt(len(buttons)))
|
||||
if (self.rows * self.cols) < len(buttons):
|
||||
# extra row if buttons don't fit
|
||||
self.rows += 1
|
||||
button_positions = self.generate_button_positions(screen_rect)
|
||||
self.buttons = list(map(lambda d: Button(next(button_positions), d['text'], color_scheme, d['cbk']), buttons))
|
||||
self.color_scheme = color_scheme
|
||||
self.buttons = list(map(lambda d: Button(next(button_positions), d['text'], theme, d['cbk']), buttons))
|
||||
self.theme = theme
|
||||
|
||||
def generate_button_positions(self, screen_rect: pygame.Rect):
|
||||
current_button_row = 0
|
||||
|
@ -155,7 +155,7 @@ class MenuBoard(IBoard):
|
|||
|
||||
def draw(self, screen: pygame.Surface, force_redraw: bool):
|
||||
if force_redraw or any(map(Button.needs_redrawing, self.buttons)):
|
||||
screen.fill(self.color_scheme["menu_bg_color"])
|
||||
screen.fill(self.theme["menu_bg_color"])
|
||||
for b in self.buttons:
|
||||
b.draw(screen)
|
||||
|
||||
|
@ -163,7 +163,7 @@ class MenuBoard(IBoard):
|
|||
class App:
|
||||
FPS = 30
|
||||
|
||||
def __init__(self, actions: List[Action], color_scheme: Dict, fullscreen: bool, screensaver_delay: int):
|
||||
def __init__(self, actions: List[Action], theme: Dict, fullscreen: bool, screensaver_delay: int):
|
||||
pygame.init()
|
||||
info = pygame.display.Info()
|
||||
flags = 0
|
||||
|
@ -172,9 +172,9 @@ class App:
|
|||
self.screen = pygame.display.set_mode((info.current_w, info.current_h), flags=flags)
|
||||
self.clock = pygame.time.Clock()
|
||||
self.running = False
|
||||
self.color_scheme = color_scheme
|
||||
self.theme = theme
|
||||
buttons = list(map(lambda u: ButtonDef(text=u['label'], cbk=self.button_press_handler(u)), actions))
|
||||
self.board: IBoard = MenuBoard(self.get_screen_rect(), buttons, color_scheme)
|
||||
self.board: IBoard = MenuBoard(self.get_screen_rect(), buttons, theme)
|
||||
self.task_q = Queue()
|
||||
self.screensaver = ClockScreensaver()
|
||||
self.TICKS_UNTIL_SCREENSAVER = screensaver_delay * self.FPS
|
||||
|
@ -198,7 +198,7 @@ class App:
|
|||
def f(ex):
|
||||
def x():
|
||||
self.board = MessageBoard(self.get_screen_rect(), f"Exception caught: {ex}",
|
||||
color_scheme=self.color_scheme)
|
||||
theme=self.theme)
|
||||
return x
|
||||
self.task_q.put(f(e))
|
||||
sleep(5)
|
||||
|
@ -207,7 +207,7 @@ class App:
|
|||
|
||||
process_thr = Thread(target=thr_fun)
|
||||
process_thr.start()
|
||||
self.board = MessageBoard(self.get_screen_rect(), action['action_message'], color_scheme=self.color_scheme)
|
||||
self.board = MessageBoard(self.get_screen_rect(), action['action_message'], theme=self.theme)
|
||||
return impl
|
||||
|
||||
def get_handler(self, action: Action) -> Callable[[], None]:
|
||||
|
@ -224,7 +224,7 @@ class App:
|
|||
|
||||
process_thr = Thread(target=thr_fun)
|
||||
process_thr.start()
|
||||
self.board = MessageBoard(self.get_screen_rect(), action['action_message'], color_scheme=self.color_scheme)
|
||||
self.board = MessageBoard(self.get_screen_rect(), action['action_message'], theme=self.theme)
|
||||
return impl
|
||||
|
||||
def button_press_handler(self, action: Action) -> Callable[[], None]:
|
||||
|
@ -265,7 +265,7 @@ class App:
|
|||
pygame.quit()
|
||||
|
||||
def quit(self):
|
||||
self.board = MessageBoard(self.get_screen_rect(), "Exiting...", color_scheme=self.color_scheme)
|
||||
self.board = MessageBoard(self.get_screen_rect(), "Exiting...", theme=self.theme)
|
||||
self.running = False
|
||||
|
||||
|
||||
|
@ -292,6 +292,6 @@ if __name__ == '__main__':
|
|||
data = json.load(f)
|
||||
url_defs = get_url_defs(data)
|
||||
|
||||
app = App(url_defs, color_scheme=data["color_scheme"], fullscreen=not args.no_fullscreen,
|
||||
app = App(url_defs, theme=data["theme"], fullscreen=not args.no_fullscreen,
|
||||
screensaver_delay=args.screensaver_delay)
|
||||
app.loop()
|
||||
|
|
Loading…
Reference in New Issue