|
|
|
@ -4,12 +4,15 @@ from dataclasses import dataclass, field
|
|
|
|
|
import typing |
|
|
|
|
import pygame |
|
|
|
|
|
|
|
|
|
from button import Button |
|
|
|
|
from chat_selector import ChatSelector |
|
|
|
|
from active_chat import ActiveChat |
|
|
|
|
from config import FPS, DARKER_BLUE, font, WHITE, WIDTH, CHAT_SELECTOR_WIDTH, HEIGHT |
|
|
|
|
|
|
|
|
|
import degeon_core as dc |
|
|
|
|
|
|
|
|
|
from input_field import TextField |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass |
|
|
|
|
class Degeon: |
|
|
|
@ -22,9 +25,13 @@ class Degeon:
|
|
|
|
|
:param active_chat (typing.Optional[ActiveChat]): current chat widget |
|
|
|
|
:param fps (int): FPS rate |
|
|
|
|
""" |
|
|
|
|
core: 'dc.Degeon' |
|
|
|
|
core: typing.Optional[dc.Degeon] |
|
|
|
|
chat_selector: ChatSelector |
|
|
|
|
active_chat: typing.Optional[ActiveChat] = None |
|
|
|
|
name_input_field: TextField = field( |
|
|
|
|
default_factory=lambda: TextField(placeholder='Name', top_left_corner=(WIDTH // 5, HEIGHT // 3))) |
|
|
|
|
name_input_button: Button = field(default_factory=lambda: Button(text='Done', width=200, height=100, |
|
|
|
|
top_left=(round(2 * WIDTH / 5), 2 * HEIGHT // 3))) |
|
|
|
|
has_profile_popup_opened: bool = False |
|
|
|
|
has_no_peers_popup: bool = False |
|
|
|
|
clock: pygame.time.Clock = field(default_factory=pygame.time.Clock) |
|
|
|
@ -36,15 +43,34 @@ class Degeon:
|
|
|
|
|
Create a new default instance with settings from file |
|
|
|
|
:return: a Degeon instance |
|
|
|
|
""" |
|
|
|
|
core: dc.Degeon = dc.new_degeon() |
|
|
|
|
if dc.is_data_available(): |
|
|
|
|
core: dc.Degeon = dc.new_degeon() |
|
|
|
|
else: |
|
|
|
|
# core: dc.Degeon = dc.new_degeon_with_name(input('Enter name: ')) |
|
|
|
|
core = None |
|
|
|
|
chat_selector = ChatSelector() |
|
|
|
|
return cls(core=core, chat_selector=chat_selector) |
|
|
|
|
|
|
|
|
|
def register(self, name: str): |
|
|
|
|
""" |
|
|
|
|
Create new rust-Degeon instance with name |
|
|
|
|
:param name: user's name |
|
|
|
|
""" |
|
|
|
|
self.core = dc.new_degeon_with_name(name) |
|
|
|
|
self.chat_selector.chats = self.core.chats |
|
|
|
|
|
|
|
|
|
def render(self, screen: pygame.Surface): |
|
|
|
|
""" |
|
|
|
|
Render everything on the screen |
|
|
|
|
:param screen: the main screen |
|
|
|
|
""" |
|
|
|
|
if self.core is None: |
|
|
|
|
screen.fill(DARKER_BLUE) |
|
|
|
|
button_surface: pygame.Surface = self.name_input_button.render() |
|
|
|
|
screen.blit(button_surface, self.name_input_button.top_left) |
|
|
|
|
name_field_surface: pygame.Surface = self.name_input_field.render() |
|
|
|
|
screen.blit(name_field_surface, self.name_input_field.top_left_corner) |
|
|
|
|
return |
|
|
|
|
chats_surface = self.chat_selector.render() |
|
|
|
|
screen.blit(chats_surface, (0, 0)) |
|
|
|
|
if self.active_chat is not None: |
|
|
|
@ -59,6 +85,8 @@ class Degeon:
|
|
|
|
|
""" |
|
|
|
|
Do all the necessary Rust work |
|
|
|
|
""" |
|
|
|
|
if self.core is None: |
|
|
|
|
return |
|
|
|
|
while self.core.message_queue_len(): |
|
|
|
|
self.core.handling_loop_iteration() |
|
|
|
|
|
|
|
|
@ -66,12 +94,15 @@ class Degeon:
|
|
|
|
|
""" |
|
|
|
|
Handle incoming messages, update chats, create no_peers popup if necessary |
|
|
|
|
""" |
|
|
|
|
if self.core is None: |
|
|
|
|
return |
|
|
|
|
# process events in core |
|
|
|
|
self.process_core_messages() |
|
|
|
|
self.chat_selector.chats = self.core.chats |
|
|
|
|
if self.core is not None: |
|
|
|
|
self.chat_selector.chats = self.core.chats |
|
|
|
|
if 0 <= self.chat_selector.active_chat < len(self.chat_selector.chats) and self.active_chat is None: |
|
|
|
|
self.active_chat = ActiveChat.new(self.chat_selector.chats[self.chat_selector.active_chat]) |
|
|
|
|
if self.active_chat is not None: |
|
|
|
|
if self.active_chat is not None and self.core is not None: |
|
|
|
|
self.active_chat.chat = self.core.chats[self.chat_selector.active_chat] |
|
|
|
|
|
|
|
|
|
def process_event(self, event: pygame.event.Event): |
|
|
|
@ -79,6 +110,11 @@ class Degeon:
|
|
|
|
|
Process an event |
|
|
|
|
:param event: pygame event |
|
|
|
|
""" |
|
|
|
|
if self.core is None: |
|
|
|
|
self.name_input_field.process_event(event) |
|
|
|
|
if self.name_input_button.process_event(event): |
|
|
|
|
self.register(self.name_input_field.value) |
|
|
|
|
return |
|
|
|
|
if self.chat_selector.process_event(event): |
|
|
|
|
if 0 <= self.chat_selector.active_chat < len(self.chat_selector.chats): |
|
|
|
|
self.active_chat = ActiveChat.new(self.chat_selector.chats[self.chat_selector.active_chat]) |
|
|
|
|