From f16bafee8fdc53991a23e372e9b629c05be543cb Mon Sep 17 00:00:00 2001 From: Felix Pankratz Date: Sun, 19 May 2024 15:31:43 +0200 Subject: [PATCH] refactor BingoDisplay to own file --- BingoDisplay.py | 54 +++++++++++++++++++++++++++++++++++++++++++++++++ bingo.py | 53 ++---------------------------------------------- 2 files changed, 56 insertions(+), 51 deletions(-) create mode 100644 BingoDisplay.py diff --git a/BingoDisplay.py b/BingoDisplay.py new file mode 100644 index 0000000..3ffba27 --- /dev/null +++ b/BingoDisplay.py @@ -0,0 +1,54 @@ +from textual.widgets import Input, Static, Button +from textual.containers import Horizontal +from textual.validation import Number +from textual.app import ComposeResult + +from datetime import datetime + +from BingoBoard import BingoBoard + +class BingoDisplay(Static): + ''' + A Widget to represent the bingo UI. + + Contains the board, input field and re-roll button. + + Attributes + ---------- + board : BingoBoard + The BingoBoard object + input_field : Input + User input for game seed + ''' + + def compose(self) -> ComposeResult: + '''Create child widgets for the app.''' + self.board = BingoBoard() + yield self.board + self.input_field = Input( + str(self.board.seed), + type='integer', + placeholder='UNIX timestamp', + max_length=10, + classes='seed_input', + validators=[ + Number(minimum=1000000000, maximum = 2000000000) + ] + ) + self.input_field.border_title = 'Seed' + yield Horizontal( + self.input_field, + Button.error(':game_die: re-roll', classes='roll_btn'), + classes='bottom_line' + ) + + def on_button_pressed(self, event: Button.Pressed) -> None: + '''Re-roll the board state with current time as seed''' + self.board.roll_board(int(datetime.now().timestamp())) + self.input_field.value = str(self.board.seed) + + def on_input_submitted(self, event: Input.Submitted) -> None: + '''Re-roll the board state with the seed from the input''' + if event.validation_result.is_valid: + self.board.roll_board(int(event.value)) + diff --git a/bingo.py b/bingo.py index e1e538f..228295b 100644 --- a/bingo.py +++ b/bingo.py @@ -1,14 +1,10 @@ #!/usr/bin/env python3 from textual.app import App, ComposeResult -from textual.widgets import Header, Input, Static, Button -from textual.containers import Horizontal -from textual.validation import Number +from textual.widgets import Header from textual.reactive import reactive -from datetime import datetime - -from BingoBoard import BingoBoard +from BingoDisplay import BingoDisplay from AboutCommand import AboutCommand from Sidebar import Sidebar @@ -49,51 +45,6 @@ class BingoApp(App): self.title = 'CCC Bingo' self.sub_title = 'GPN22 Edition' -class BingoDisplay(Static): - ''' - A Widget to represent the bingo UI. - - Contains the board, input field and re-roll button. - - Attributes - ---------- - board : BingoBoard - The BingoBoard object - input_field : Input - User input for game seed - ''' - - def compose(self) -> ComposeResult: - '''Create child widgets for the app.''' - self.board = BingoBoard() - yield self.board - self.input_field = Input( - str(self.board.seed), - type='integer', - placeholder='UNIX timestamp', - max_length=10, - classes='seed_input', - validators=[ - Number(minimum=1000000000, maximum = 2000000000) - ] - ) - self.input_field.border_title = 'Seed' - yield Horizontal( - self.input_field, - Button.error(':game_die: re-roll', classes='roll_btn'), - classes='bottom_line' - ) - - def on_button_pressed(self, event: Button.Pressed) -> None: - '''Re-roll the board state with current time as seed''' - self.board.roll_board(int(datetime.now().timestamp())) - self.input_field.value = str(self.board.seed) - - def on_input_submitted(self, event: Input.Submitted) -> None: - '''Re-roll the board state with the seed from the input''' - if event.validation_result.is_valid: - self.board.roll_board(int(event.value)) - if __name__ == "__main__": app = BingoApp() app.run()