From 204310e159dcf2f5f131c28c493e4a5048be6344 Mon Sep 17 00:00:00 2001 From: Alexey Date: Thu, 9 Dec 2021 18:25:16 +0300 Subject: [PATCH] ActiveChat and Degeon.main_loop in interface --- degeon_py/main.py | 73 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 68 insertions(+), 5 deletions(-) diff --git a/degeon_py/main.py b/degeon_py/main.py index f35dda3..dc17845 100644 --- a/degeon_py/main.py +++ b/degeon_py/main.py @@ -46,7 +46,7 @@ class ChatSelector: hovered_chat: typing.Optional[int] = None width: int = WIDTH // 3 height: int = HEIGHT - chat_height: int = HEIGHT // 10 + chat_height: int = HEIGHT // 4 def render(self) -> pygame.Surface: """ @@ -68,7 +68,7 @@ class ChatSelector: def process_event(self, event: pygame.event.Event): """ Process a click: select the necessary chat if this click is in the widget - :param event: pygame event + :param event: a pygame event """ if event.type == pygame.MOUSEBUTTONUP and event.pos[0] < self.width: self.active_chat = event.pos[1] // self.chat_height @@ -93,8 +93,29 @@ class ActiveChat: Attributes: :param chat (dc.Chat): the chat (Rust Chat type) """ - chat: 'dc.Chat' + height: int = HEIGHT + width: int = 3 * (WIDTH // 4) + + @classmethod + def new(cls, chat: 'dc.Chat', **kwargs) -> ActiveChat: + return cls(chat=chat, **kwargs) + + def render(self) -> pygame.Surface: + """ + Creates a pygame surface and draws the chat on it + :return: the surface with the chat on it + """ + surface: pygame.Surface = pygame.Surface((self.width, self.height)) + + return surface + + def process_event(self, event: pygame.event.Event): + """ + Process a click: select the necessary chat if this click is in the widget + :param event: a pygame event + """ + pass # todo @dataclass @@ -106,8 +127,11 @@ class Degeon: """ core: 'dc.Degeon' chat_selector: ChatSelector + active_chat: typing.Optional[ActiveChat] = None has_profile_popup_opened: bool = False has_no_peers_popup: bool = False + clock: pygame.time.Clock = field(default_factory=pygame.time.Clock) + fps: int = FPS @classmethod def new(cls) -> Degeon: @@ -115,7 +139,9 @@ class Degeon: Create a new default instance with settings from file :return: a Degeon instance """ - pass # todo + core: dc.Degeon = dc.new_degeon() + chat_selector = ChatSelector() + return cls(core=core, chat_selector=chat_selector) def render(self, screen: pygame.Surface): """ @@ -125,8 +151,45 @@ class Degeon: chats_surface = self.chat_selector.render() screen.blit(chats_surface, (0, 0)) + def process_core_messages(self): + """ + Do all the necessary Rust work + """ + pass # todo + def tick(self): """ Handle incoming messages, update chats, create no_peers popup if necessary """ - pass # todo + # process events in core + self.process_core_messages() + self.chat_selector.chats = self.core.chats + if self.chat_selector.active_chat < len(self.chat_selector.chats): + self.active_chat = ActiveChat.new(self.chat_selector.chats[self.chat_selector.active_chat]) + + def process_event(self, event: pygame.event.Event): + """ + Process an event + :param event: pygame event + """ + self.chat_selector.process_event(event) + + def main_loop(self, screen: pygame.Surface): + """ + Drawing everything and handling events + """ + while True: + self.clock.tick(self.fps) + for event in pygame.event.get(): + if event.type == pygame.QUIT: + return + self.process_event(event) + self.tick() + self.render(screen) + pygame.display.update() + + +deg = Degeon.new() +screen = pygame.display.set_mode((WIDTH, HEIGHT)) +deg.main_loop(screen) +pygame.quit()