1
0
Fork 0

Ability to configure background color

This commit is contained in:
Michał Rudowicz 2024-09-08 18:02:13 +02:00
parent cd287083a0
commit a16e689ec5
2 changed files with 24 additions and 18 deletions

View File

@ -20,5 +20,6 @@
"label": "example 5", "label": "example 5",
"url": "http://example.com" "url": "http://example.com"
} }
] ],
"bg_color": "lime"
} }

39
menu.py
View File

@ -63,35 +63,35 @@ class Button:
class MessageBoard(IBoard): class MessageBoard(IBoard):
def __init__(self, screen_rect: pygame.Rect, text: str): def __init__(self, screen_rect: pygame.Rect, text: str, bg_color: str):
self.text = text self.text = text
self.font = pygame.freetype.SysFont(name="Sans", size=30) self.font = pygame.freetype.SysFont(name="Sans", size=30)
text_rect = self.font.get_rect(self.text) text_rect = self.font.get_rect(self.text)
self.text_pos = pygame.Rect((screen_rect.left + (screen_rect.width - text_rect.width)/2), 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), (screen_rect.top + (screen_rect.height - text_rect.height)/2),
text_rect.width, text_rect.height) text_rect.width, text_rect.height)
self.bg_color = bg_color
def handle_event(self, _): def handle_event(self, _):
pass pass
def draw(self, screen: pygame.Surface): def draw(self, screen: pygame.Surface):
screen.fill("purple") screen.fill(self.bg_color)
self.font.render_to(screen, self.text_pos, self.text, fgcolor="black") self.font.render_to(screen, self.text_pos, self.text, fgcolor="black")
class MenuBoard(IBoard): class MenuBoard(IBoard):
BUTTON_MARGINS = 10 BUTTON_MARGINS = 10
def __init__(self, screen_rect: pygame.Rect, buttons: List[ButtonDef]): def __init__(self, screen_rect: pygame.Rect, buttons: List[ButtonDef], bg_color: str):
self.rows = math.floor(math.sqrt(len(buttons))) self.rows = math.floor(math.sqrt(len(buttons)))
self.cols = math.ceil(math.sqrt(len(buttons))) self.cols = math.ceil(math.sqrt(len(buttons)))
if (self.rows * self.cols) < len(buttons): if (self.rows * self.cols) < len(buttons):
# extra row if buttons don't fit # extra row if buttons don't fit
self.rows += 1 self.rows += 1
button_positions = self.generate_button_positions(screen_rect) button_positions = self.generate_button_positions(screen_rect)
self.buttons = list(map(lambda d: Button(next(button_positions), self.buttons = list(map(lambda d: Button(next(button_positions), d['text'], d['cbk']), buttons))
f"Button {d['text']}", self.bg_color = bg_color
d['cbk']), buttons))
def generate_button_positions(self, screen_rect: pygame.Rect): def generate_button_positions(self, screen_rect: pygame.Rect):
current_button_row = 0 current_button_row = 0
@ -112,21 +112,22 @@ class MenuBoard(IBoard):
b.handle_event(event) b.handle_event(event)
def draw(self, screen: pygame.Surface): def draw(self, screen: pygame.Surface):
screen.fill("purple") screen.fill(self.bg_color)
for b in self.buttons: for b in self.buttons:
b.draw(screen) b.draw(screen)
class App: class App:
def __init__(self, urls: List[UrlDef]): def __init__(self, urls: List[UrlDef], bg_color: str):
pygame.init() pygame.init()
info = pygame.display.Info() info = pygame.display.Info()
self.screen = pygame.display.set_mode((info.current_w, info.current_h), flags=pygame.FULLSCREEN) self.screen = pygame.display.set_mode((info.current_w, info.current_h), flags=pygame.FULLSCREEN)
self.clock = pygame.time.Clock() self.clock = pygame.time.Clock()
self.running = False self.running = False
self.bg_color = bg_color
buttons = list(map(lambda u: ButtonDef(text=u['label'], cbk=self.button_press_handler(u['url'])), urls)) buttons = list(map(lambda u: ButtonDef(text=u['label'], cbk=self.button_press_handler(u['url'])), urls))
buttons.append(ButtonDef(text="Exit", cbk=self.quit)) buttons.append(ButtonDef(text="Exit", cbk=self.quit))
self.board: IBoard = MenuBoard(self.get_screen_rect(), buttons) self.board: IBoard = MenuBoard(self.get_screen_rect(), buttons, bg_color)
self.task_q = Queue() self.task_q = Queue()
def get_screen_rect(self) -> pygame.Rect: def get_screen_rect(self) -> pygame.Rect:
@ -145,7 +146,7 @@ class App:
process_thr = Thread(target=thr_fun) process_thr = Thread(target=thr_fun)
process_thr.start() process_thr.start()
self.board = MessageBoard(self.get_screen_rect(), f"Fetching: {url}") self.board = MessageBoard(self.get_screen_rect(), f"Fetching: {url}", bg_color=self.bg_color)
return impl return impl
def loop(self): def loop(self):
@ -168,16 +169,14 @@ class App:
pygame.quit() pygame.quit()
def quit(self): def quit(self):
self.board = MessageBoard(self.get_screen_rect(), "Exiting...") self.board = MessageBoard(self.get_screen_rect(), "Exiting...", bg_color=self.bg_color)
self.running = False self.running = False
def get_config(path: str) -> List[UrlDef]: def get_url_defs(config_data: dict) -> List[UrlDef]:
url_defs = [] url_defs = []
with open(path, "rb") as f: for d in data['urls']:
data = json.load(f) url_defs.append(UrlDef(label=d['label'], url=d['url']))
for d in data['urls']:
url_defs.append(UrlDef(label=d['label'], url=d['url']))
return url_defs return url_defs
@ -185,5 +184,11 @@ if __name__ == '__main__':
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument("config_path", help="Path to the config file.", type=str) parser.add_argument("config_path", help="Path to the config file.", type=str)
args = parser.parse_args() args = parser.parse_args()
app = App(get_config(args.config_path))
url_defs: List[UrlDef] = []
with open(args.config_path, "rb") as f:
data = json.load(f)
url_defs = get_url_defs(data)
app = App(url_defs, bg_color=data["bg_color"])
app.loop() app.loop()