Skip to content

Commit

Permalink
Complete basic input support
Browse files Browse the repository at this point in the history
  • Loading branch information
bennett-nguyen committed Sep 9, 2024
1 parent bba0bfe commit d8fd64a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/cmd_ui/command_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ def __init__(self, manager: pygame_gui.core.interfaces.IUIManagerInterface):
"left": "left",
"bottom": "bottom",
},
placeholder_text="real",
object_id="#command-box"
placeholder_text="Press '/' to start typing commands!",
object_id=const.COMMAND_BOX_OBJECT_ID
)

self.command_box.show()

def on_window_size_changed(self):
self.command_box.set_dimensions((pygame_window.window_width - const.COMMAND_BOX_ANCHOR_OFFSET * 2, 50))
1 change: 1 addition & 0 deletions src/core/utils/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
CMD_THEME_FILE: str = "theme/cmdline_ui-DO-NOT-EDIT.json"

# -- Command box
COMMAND_BOX_OBJECT_ID = "#command-box"
COMMAND_BOX_ANCHOR_OFFSET = 20

# Links
Expand Down
3 changes: 2 additions & 1 deletion src/mvc/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ def process_input(self, events: list[pg.event.Event]):
for event in events:
if event.type == pg.MOUSEWHEEL and not cmdline_interface.command_box.command_box.is_focused:
self.model.zoom(event.y)
elif event.type == pg.VIDEORESIZE:

if event.type == pg.VIDEORESIZE:
self.model.on_window_size_changed()

cmdline_interface.process_event(event)
Expand Down
20 changes: 19 additions & 1 deletion src/mvc/model_helpers/command_line_interface.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json

import pygame as pg
import pygame_gui

from src.core.utils import const
from src.core.dataclasses import Theme
Expand All @@ -10,10 +11,27 @@ class CMDLineInterface:
def __init__(self):
self.ui_manager = UIManager()
self.command_box = CommandBox(self.ui_manager.manager)

self._focused_textbox = False

def process_event(self, event: pg.event.Event):
self.ui_manager.process_event(event)

if self._focused_textbox:
self.command_box.command_box.focus()
self._focused_textbox = False

if event.type == pg.KEYDOWN:
if event.key == pg.K_SLASH:
self._focused_textbox = True

elif event.key == pg.K_ESCAPE:
self.command_box.command_box.unfocus()

elif event.type == pygame_gui.UI_TEXT_ENTRY_FINISHED \
and event.ui_object_id == const.COMMAND_BOX_OBJECT_ID and event.text:
self.command_box.command_box.clear()
self.command_box.command_box.focus()

def update(self, dt_time: float):
self.ui_manager.update(dt_time)

Expand Down

0 comments on commit d8fd64a

Please sign in to comment.