From dbbd40b58aac67195478ae0dc2a039968d05c65a Mon Sep 17 00:00:00 2001 From: Kevin Baumgartner Date: Thu, 7 May 2026 20:55:22 +0200 Subject: [PATCH 1/3] feat(text_input): add text input class with initialization --- src/text_input.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/text_input.py diff --git a/src/text_input.py b/src/text_input.py new file mode 100644 index 0000000..e215b96 --- /dev/null +++ b/src/text_input.py @@ -0,0 +1,20 @@ +import pygame + +class TextInput: + def __init__(self, width, height, pos_x, pos_y): + self.rect = pygame.Rect(pos_x, pos_y, width, height) + + self.font = pygame.font.SysFont("Arial", 22) + + # input text + self.text = "" + + self.active = True + + # colors + self.text_color = (255, 255, 255) + self.border_color = (255, 255, 255) + self.active_border_color = (0, 255, 0) + self.background_color = (30, 30, 30) + + From b625acdd60e0919216e7aae24a002b38d675235d Mon Sep 17 00:00:00 2001 From: Kevin Baumgartner Date: Thu, 7 May 2026 21:12:02 +0200 Subject: [PATCH 2/3] feat(text_input): add draw function --- src/text_input.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/text_input.py b/src/text_input.py index e215b96..6272f3b 100644 --- a/src/text_input.py +++ b/src/text_input.py @@ -16,5 +16,29 @@ class TextInput: self.border_color = (255, 255, 255) self.active_border_color = (0, 255, 0) self.background_color = (30, 30, 30) - + def handle_event(self, event): + # todo check keyboard input events + pass + + def draw(self, screen): + # draw background of input box + pygame.draw.rect(screen, self.background_color, self.rect) + + # change border color depending if input box is active or not + if self.active: + border_color = self.active_border_color + else: + border_color = self.border_color + + # draw border + pygame.draw.rect(screen, border_color, self.rect, 2) + + # draw text + text_surface = self.font.render(self.text, True, self.text_color) + + # show text with indentation + screen.blit(text_surface, (self.rect.x + 10, self.rect.y + 10)) + + def get_text(self): + return self.text From 387a328b1ce59e1df645caaee4ce15fb8ae80634 Mon Sep 17 00:00:00 2001 From: Kevin Baumgartner Date: Thu, 7 May 2026 21:19:42 +0200 Subject: [PATCH 3/3] feat(text_input): add event handler function (mouse and keyboard input) --- src/text_input.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/text_input.py b/src/text_input.py index 6272f3b..64acc25 100644 --- a/src/text_input.py +++ b/src/text_input.py @@ -9,7 +9,7 @@ class TextInput: # input text self.text = "" - self.active = True + self.active = False # colors self.text_color = (255, 255, 255) @@ -18,8 +18,25 @@ class TextInput: self.background_color = (30, 30, 30) def handle_event(self, event): - # todo check keyboard input events - pass + # check if mouse click event on text input box + if event.type == pygame.MOUSEBUTTONDOWN: + if self.rect.collidepoint(event.pos): + self.active = True + else: + self.active = False + + # check for keyboard input + if event.type == pygame.KEYDOWN and self.active: + # enter will submit and exit input box + if event.key == pygame.K_RETURN: + self.active = False + + # backspace will delete last character + elif event.key == pygame.K_BACKSPACE: + self.text = self.text[:-1] + # other characters will be added to the string + else: + self.text += event.unicode def draw(self, screen): # draw background of input box