Browse Source

The send button in interface

interface-2
Lev 3 years ago
parent
commit
888f77c6bb
  1. 9
      degeon_py/active_chat.py
  2. 6
      degeon_py/button.py
  3. 4
      degeon_py/config.py
  4. 6
      degeon_py/input_field.py

9
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()

6
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))

4
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

6
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
)
)

Loading…
Cancel
Save