IronForce is a decentralized network, Degeon is a messenger built on it
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

104 lines
4.1 KiB

from __future__ import annotations
import typing
from dataclasses import dataclass, field
import pygame
from button import Button
from config import HEIGHT, WIDTH, CHAT_SELECTOR_WIDTH, DARKER_BLUE, CHAT_PREVIEW_HEIGHT, WHITE, font, DARK_BLUE, \
MESSAGE_HEIGHT
import degeon_core as dc
from input_field import TextField
from message import Message
@dataclass
class ActiveChat:
"""
The widget with the current chat
Attributes:
:param chat (dc.Chat): the chat (Rust Chat type)
:param height (int): the height of the active chat widget
:param width (int): its width
:param delta_x (int): distance from the left edge of the application to the left edge of the screen
:param header_height (int): height of the header (the title)
"""
chat: dc.Chat
input_field: TextField = field(default_factory=lambda: TextField(placeholder='New message'))
send_button: Button = field(default_factory=lambda: Button(height=CHAT_PREVIEW_HEIGHT, width=100, text='Send',
top_left=(WIDTH - 112, HEIGHT - 85)))
height: int = HEIGHT
width: int = WIDTH - CHAT_SELECTOR_WIDTH - 10
delta_x: int = CHAT_SELECTOR_WIDTH + 10
header_height: int = int(CHAT_PREVIEW_HEIGHT * 1.5)
@classmethod
def new(cls, chat: dc.Chat, **kwargs) -> ActiveChat:
"""
Create a new `Chat` from a rust Chat object
:param chat: rusty chat
:param kwargs: optional other paraeters
:return: the `Chat`
"""
return cls(chat=chat, **kwargs)
def get_messages(self, core: dc.Degeon) -> typing.Iterable[Message]:
"""
Get an iterator over all messages in this chat in the backwards order
This function creates a python `message.Message` object from rust instances
:return: an iterator of `message.Message` objects
"""
for msg in reversed(self.chat.messages):
yield Message(text=msg.get_content_py().text, is_from_me=core.check_message_ownership(msg))
def get_header(self) -> pygame.Surface:
"""
Render a pygame surface with the header.
The header is (for now) just a name of the user written on a background
:return: the header
"""
surface: pygame.Surface = pygame.Surface((self.width, self.header_height))
surface.fill(DARK_BLUE)
name_surface: pygame.Surface = font.render(self.chat.profile.name, True, WHITE)
surface.blit(name_surface, (20, 20))
return surface
def render(self, core: dc.Degeon) -> 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))
surface.fill(DARKER_BLUE)
# Render messages
# This is the y0 for the last message
last_message_y = self.height - MESSAGE_HEIGHT * 2
for i, message in zip(range(30), self.get_messages(core)):
msg_surface = message.render()
last_message_y -= msg_surface.get_height() + 30
surface.blit(msg_surface, (0, last_message_y - 30))
# Render header
header = self.get_header()
surface.blit(header, (0, 10))
# Render message input
input_field_surface: pygame.Surface = self.input_field.render()
surface.blit(input_field_surface, (0, self.height - input_field_surface.get_height()))
# Render sending button
sending_button_surface: pygame.Surface = self.send_button.render()
surface.blit(sending_button_surface,
(self.send_button.top_left[0] - self.delta_x, self.send_button.top_left[1]))
return surface
def process_event(self, event: pygame.event.Event) -> typing.Optional[str]:
"""
Process clicks in the active chat widget. Return a message to send if needed
:param event: a pygame event
:return: A message to send if there is one
"""
self.input_field.process_event(event)
if self.send_button.process_event(event):
return self.input_field.collect()