Lev
3 years ago
3 changed files with 68 additions and 1 deletions
@ -0,0 +1,42 @@
|
||||
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 = self.height - padding * 2 |
||||
blit_width = round(text_surface.get_width() * blit_height / text_surface.get_height()) |
||||
x = 0 if not self.is_from_me else self.chat_width - blit_width - padding * 2 |
||||
pygame.draw.rect(surface, bg_color, (x, 0, blit_width + padding * 2, self.height)) |
||||
text_surface = pygame.transform.smoothscale(text_surface, (blit_width, blit_height)) |
||||
surface.blit(text_surface, (x + padding, padding)) |
||||
return surface |
Loading…
Reference in new issue