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.
42 lines
1.6 KiB
42 lines
1.6 KiB
from __future__ import annotations |
|
|
|
from dataclasses import dataclass |
|
|
|
import pygame |
|
|
|
from config import WIDTH, CHAT_SELECTOR_WIDTH, MESSAGE_HEIGHT, font, BLUE, DARK_BLUE, WHITE, DARKER_BLUE |
|
|
|
|
|
@dataclass |
|
class Message: |
|
""" |
|
The message (for now, it consists only of text) |
|
|
|
Attributes: |
|
:param text (str): the message text |
|
:param is_from_me (bool): False if the message is not from the current user |
|
:param chat_width (int): the width of the active chat widget |
|
""" |
|
text: str |
|
is_from_me: bool |
|
chat_width: int = WIDTH - CHAT_SELECTOR_WIDTH - 10 |
|
height: int = MESSAGE_HEIGHT |
|
|
|
def render(self) -> pygame.Surface: |
|
""" |
|
Creates a surface with a rectangle and the message text written on it |
|
:return: the surface with rendered message |
|
""" |
|
surface = pygame.Surface((self.chat_width, self.height)) |
|
surface.fill(DARKER_BLUE) |
|
bg_color = BLUE * self.is_from_me + DARK_BLUE * (not self.is_from_me) |
|
text_surface: pygame.Surface = font.render(self.text, True, WHITE) |
|
padding = 5 |
|
# Size of the scaled text surface |
|
blit_height: int = self.height - padding * 2 |
|
blit_width: int = round(text_surface.get_width() * blit_height / text_surface.get_height()) |
|
x: int = 0 if not self.is_from_me else self.chat_width - blit_width - padding * 3.5 |
|
pygame.draw.rect(surface, bg_color, (x, 0, blit_width + padding * 2, self.height)) |
|
text_surface: pygame.Surface = pygame.transform.smoothscale(text_surface, (blit_width, blit_height)) |
|
surface.blit(text_surface, (x + padding, padding)) |
|
return surface
|
|
|