From c712b5a7becb0a336f97392619b8bbf34ab3789c Mon Sep 17 00:00:00 2001 From: Alexey Date: Mon, 13 Dec 2021 12:18:36 +0300 Subject: [PATCH] The send button in interface --- degeon_py/active_chat.py | 9 +++++++++ degeon_py/button.py | 6 +++--- degeon_py/config.py | 4 ++-- degeon_py/input_field.py | 6 +++--- 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/degeon_py/active_chat.py b/degeon_py/active_chat.py index b4475e3..38bef75 100644 --- a/degeon_py/active_chat.py +++ b/degeon_py/active_chat.py @@ -4,6 +4,7 @@ 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 @@ -26,6 +27,8 @@ class ActiveChat: """ chat: dc.Chat input_field: TextField = field(default_factory=lambda: TextField()) + 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 @@ -83,6 +86,10 @@ class ActiveChat: # 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]: @@ -92,3 +99,5 @@ class ActiveChat: :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() diff --git a/degeon_py/button.py b/degeon_py/button.py index 95af420..59f525c 100644 --- a/degeon_py/button.py +++ b/degeon_py/button.py @@ -1,7 +1,7 @@ import pygame import typing -from config import font, WHITE, BLUE, BLACK +from config import font_large, WHITE, BLUE, BLACK from dataclasses import dataclass @@ -26,7 +26,7 @@ class Button: top_left: typing.Tuple[int, int] width: int height: int - padding: int = 5 + padding: int = 13 bg_color: pygame.Color = BLUE text_color: pygame.Color = WHITE hovered_color: pygame.Color = (BLUE * 3 + WHITE) // 4 @@ -71,7 +71,7 @@ class Button: surface: pygame.Surface = pygame.Surface((self.width, self.height)) bg_color, text_color = self.get_colors() surface.fill(bg_color) - text_surface: pygame.Surface = font.render(self.text, True, text_color) + text_surface: pygame.Surface = font_large.render(self.text, True, text_color) text_height: int = self.height - 2 * self.padding text_width: int = round(text_surface.get_width() * text_height / text_surface.get_height()) text_surface: pygame.Surface = pygame.transform.scale(text_surface, (text_width, text_height)) diff --git a/degeon_py/config.py b/degeon_py/config.py index 48e871e..ff63594 100644 --- a/degeon_py/config.py +++ b/degeon_py/config.py @@ -4,8 +4,8 @@ import pygame pygame.init() # Fontss -font = pygame.font.Font('CRC35.otf', 25) -font_large = pygame.font.Font('CRC35.otf', 35) +font = pygame.font.Font('CRC35.otf', 32) +font_large = pygame.font.Font('CRC35.otf', 50) # Colors used in the app RED = 0xFF0000 diff --git a/degeon_py/input_field.py b/degeon_py/input_field.py index 5f4584d..f6e7f3f 100644 --- a/degeon_py/input_field.py +++ b/degeon_py/input_field.py @@ -13,8 +13,8 @@ class TextField: Field for message input """ value: str = '' - width: int = WIDTH - CHAT_SELECTOR_WIDTH - 80 - height: int = MESSAGE_HEIGHT * 2 + width: int = WIDTH - CHAT_SELECTOR_WIDTH - 140 + height: int = MESSAGE_HEIGHT * 1.5 top_left_corner: typing.Tuple[int, int] = (CHAT_SELECTOR_WIDTH, HEIGHT - MESSAGE_HEIGHT * 2) is_focused: bool = False placeholder: str = '' @@ -43,7 +43,7 @@ class TextField: surface.blit( message_text, ( - 0, # or, if we want to center, `(self.width - message_text.get_width()) // 2,` + 40, # or, if we want to center, `(self.width - message_text.get_width()) // 2,` (self.height - message_text.get_height()) // 2 ) )