Skip to content

Commit

Permalink
Add more docstrings and some formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
bennett-nguyen committed Sep 9, 2024
1 parent d8fd64a commit 21dc56a
Show file tree
Hide file tree
Showing 12 changed files with 204 additions and 23 deletions.
22 changes: 16 additions & 6 deletions src/cmd_ui/command_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@
from src.core.utils import const

class CommandBox:
"""
Represents a command input box for user interactions within the
application. This class initializes the command box UI element and manages
its dimensions based on window size changes.
"""

def __init__(self, manager: pygame_gui.core.interfaces.IUIManagerInterface):
self.relative_rect = pg.Rect(0, 0, pygame_window.window_width - const.COMMAND_BOX_ANCHOR_OFFSET * 2, 50)
self.relative_rect.bottomleft = (const.COMMAND_BOX_ANCHOR_OFFSET, -const.COMMAND_BOX_ANCHOR_OFFSET)
relative_rect = pg.Rect(0, 0, pygame_window.window_width - const.COMMAND_BOX_ANCHOR_OFFSET * 2, 50)
relative_rect.bottomleft = (const.COMMAND_BOX_ANCHOR_OFFSET, -const.COMMAND_BOX_ANCHOR_OFFSET)

self.command_box = pygame_gui.elements.UITextEntryLine(
relative_rect=self.relative_rect,
self.UI = pygame_gui.elements.UITextEntryLine(
relative_rect=relative_rect,
manager=manager,
anchors={
"left": "left",
Expand All @@ -21,7 +27,11 @@ def __init__(self, manager: pygame_gui.core.interfaces.IUIManagerInterface):
object_id=const.COMMAND_BOX_OBJECT_ID
)

self.command_box.show()
self.UI.show()

def on_window_size_changed(self):
self.command_box.set_dimensions((pygame_window.window_width - const.COMMAND_BOX_ANCHOR_OFFSET * 2, 50))
"""
Updates the dimensions of the command box when the window size changes.
"""

self.UI.set_dimensions((pygame_window.window_width - const.COMMAND_BOX_ANCHOR_OFFSET * 2, const.COMMAND_BOX_HEIGHT))
33 changes: 32 additions & 1 deletion src/cmd_ui/ui_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,48 @@
from src.core import pygame_window

class UIManager:
"""
Manages the user interface elements of the application using pygame_gui.
This class handles the initialization, updating, event processing, and drawing
of UI components on the screen.
"""

def __init__(self):
self.manager = pygame_gui.UIManager(pygame_window.size, const.CMD_THEME_FILE)

def update(self, dt_time: float):
"""
Updates the UI manager with the elapsed time since the last frame.
This method ensures that all UI elements are refreshed and responsive to changes.
Args:
dt_time (float): The time in seconds since the last update call.
"""

self.manager.update(dt_time)

def process_event(self, event: pg.event.Event):
"""
Processes input events for the UI manager.
This method allows the user interface to respond to various user interactions,
such as mouse clicks and keyboard inputs.
"""

self.manager.process_events(event)

def draw(self, screen: pg.Surface):
"""
Draws the UI elements onto the specified surface.
This method renders all visible components of the user interface.
"""

self.manager.draw_ui(screen)

def on_window_size_changed(self):
"""
Adjusts the UI manager's resolution when the window size changes.
This method ensures that all UI elements are correctly scaled and positioned
according to the new dimensions of the window.
"""

self.manager.set_window_resolution(pygame_window.size)
6 changes: 6 additions & 0 deletions src/core/dataclasses/theme.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@

@dataclass(slots=True, frozen=True)
class Theme:
"""
Represents a theme configuration for the user interface.
This class encapsulates various color settings used throughout the application,
allowing for consistent styling and easy theme management.
"""

CMD_UI_FILE_PATH: Optional[str]

NAME: str
Expand Down
10 changes: 10 additions & 0 deletions src/core/tree_utils/segment_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def __init__(self, array: list[int], function_obj: Function):
function_obj (Function): An object containing the function used for
combining values and the value to return for invalid queries.
"""

self.array = array
self.root = Node()

Expand All @@ -38,6 +39,7 @@ def array_length(self) -> int:
Returns:
int: The length of the array.
"""

return len(self.array)

def switch_function(self, function_obj: Function):
Expand Down Expand Up @@ -69,6 +71,7 @@ def query(self, q_low: int, q_high: int) -> int:
Returns:
int: The result of the query for the specified range.
"""

return self._query(q_low, q_high, self.root, 0, self.array_length-1)

def update(self, pos: int, val: int) -> None:
Expand All @@ -85,6 +88,7 @@ def update(self, pos: int, val: int) -> None:
Returns:
None
"""

self.array[pos] = val
self._update(pos, val, self.root, 0, self.array_length-1)

Expand All @@ -95,6 +99,7 @@ def rebuild(self):
the tree structure based on the current array. It ensures that the segment
tree is updated to reflect any changes in the underlying data.
"""

self.root = Node()
self._build(self.root, 0, self.array_length-1)

Expand All @@ -112,6 +117,7 @@ def _build(self, node: Node, low: int, high: int, ID: int = 1) -> None:
high (int): The upper index of the range for the current node.
ID (int, optional): The identifier for the current node. Defaults to 1.
"""

if self.array_length == 0:
return

Expand Down Expand Up @@ -144,6 +150,7 @@ def _update(self, pos: int, val: int, node: Node, low: int, high: int) -> None:
low (int): The lower index of the range for the current node.
high (int): The upper index of the range for the current node.
"""

if low == high:
node.data = val
return
Expand Down Expand Up @@ -172,6 +179,7 @@ def _query(self, q_low: int, q_high: int, node: Node, low: int, high: int) -> in
low (int): The lower index of the range for the current node.
high (int): The upper index of the range for the current node.
"""

if self._is_query_invalid(q_low, q_high, low, high):
return self._INVALID_QUERY
if self._is_query_within_range(q_low, q_high, low, high):
Expand Down Expand Up @@ -200,6 +208,7 @@ def _is_query_invalid(self, q_low: int, q_high: int, low: int, high: int) -> boo
Returns:
bool: True if the query range is invalid, otherwise False.
"""

return low > high or low > q_high or high < q_low

def _is_query_within_range(self, q_low: int, q_high: int, low: int, high: int) -> bool:
Expand All @@ -219,4 +228,5 @@ def _is_query_within_range(self, q_low: int, q_high: int, low: int, high: int) -
Returns:
bool: True if the current range is within the query range, otherwise False.
"""

return q_low <= low and high <= q_high
8 changes: 8 additions & 0 deletions src/core/utils/app_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class ContourEnum(Enum):
LEFT (str): Represents the left side contour.
RIGHT (str): Represents the right side contour.
"""

LEFT = "left"
RIGHT = "right"

Expand All @@ -25,12 +26,19 @@ class VisibilityEnum(Enum):
NODE_DATA_FIELD: Represents the visibility of the node data field.
NODE_INFO_FIELD: Represents the visibility of the node information field.
"""

ARRAY_FIELD = auto()
NODE_DATA_FIELD = auto()
NODE_INFO_FIELD = auto()

@unique
class JSONThemeFieldsEnum(Enum):
"""
Enumerates the fields used in JSON theme files that this app uses.
This class provides a set of constants that represent the various attributes
listed in a JSON theme file, facilitating safer data access.
"""

NAME = "Name"
PALETTE = "Palette"
USE_DEFAULT_CMD_UI = "use_default_cmd_ui"
Expand Down
1 change: 1 addition & 0 deletions src/core/utils/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
# -- Command box
COMMAND_BOX_OBJECT_ID = "#command-box"
COMMAND_BOX_ANCHOR_OFFSET = 20
COMMAND_BOX_HEIGHT = 50

# Links
GITHUB_LINK: WebLink = WebLink("https://github.com/bennett-nguyen/KAY")
Expand Down
59 changes: 59 additions & 0 deletions src/core/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
)

class PygameWindow:
"""
Represents a window for the Pygame application, managing the display and
rendering settings. This class initializes the window, sets the icon and caption,
and provides methods to manipulate the window's appearance and frame rate.
"""

__slots__ = ("screen", "clock")
def __init__(self):
info_obj = pg.display.Info()
Expand All @@ -34,29 +40,82 @@ def __init__(self):
pg.display.set_icon(icon)

def fill_background(self, color: pg.Color):
"""
Fills the window's background with the specified color.
This method is used to clear the screen before drawing new graphics, ensuring
that the previous frame does not interfere with the current one.
Args:
color (pg.Color): The color to fill the background with.
"""

self.screen.fill(color)

def set_framerate(self, FPS: float) -> float:
"""
Sets the frame rate for the application and regulates the speed of the game loop.
Args:
FPS (float): The desired frames per second for the application.
"""

return self.clock.tick(FPS)

@property
def size(self) -> tuple[int, int]:
"""
Retrieves the current size of the window.
This property returns the width and height of the window as a tuple, allowing
other components of the application to adapt to the window's dimensions.
Returns:
tuple[int, int]: A tuple containing the width and height of the window.
"""

return self.screen.get_size()

@property
def window_width(self) -> int:
"""
Retrieves the current width of the window.
Returns:
int: The width of the window in pixels.
"""

return self.screen.get_width()

@property
def window_height(self) -> int:
"""
Retrieves the current height of the window.
Returns:
int: The height of the window in pixels.
"""

return self.screen.get_height()

@property
def half_window_width(self) -> int:
"""
Retrieves half the current width of the window.
Returns:
int: Half the width of the window in pixels.
"""

return int(self.window_width / 2)

@property
def half_window_height(self) -> int:
"""
Retrieves half the current height of the window.
Returns:
int: Half the height of the window in pixels.
"""

return int(self.window_height / 2)

pygame_window = PygameWindow()
7 changes: 4 additions & 3 deletions src/mvc/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,19 @@ def process_input(self, events: list[pg.event.Event]):
Args:
events (list[pg.event.Event]): A list of Pygame event objects to be processed.
"""

cmdline_interface = self.model.cmdline_interface

for event in events:
if event.type == pg.MOUSEWHEEL and not cmdline_interface.command_box.command_box.is_focused:
if event.type == pg.MOUSEWHEEL and not cmdline_interface.command_box.UI.is_focused:
self.model.zoom(event.y)

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

cmdline_interface.process_event(event)

if not cmdline_interface.command_box.command_box.is_focused:
if not cmdline_interface.command_box.UI.is_focused:
self.model.pan()


Expand Down Expand Up @@ -67,4 +68,4 @@ def update_view(self, dt_time: float):
self.view.view_array(tree_manager.segment_tree.array, hovered_node)
self.view.view_hovered_node_info(hovered_node)

cmdline_interface.draw_ui(pygame_window.screen)
cmdline_interface.draw_ui()
14 changes: 7 additions & 7 deletions src/mvc/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@


class Model:
"""
Manages the application's data and interactions, including theme management and
tree structure handling. This class initializes the necessary components for
rendering and user interaction, providing methods for zooming and panning the view.
"""

def __init__(self):
"""
Initializes the model with the necessary components for managing themes and
Expand Down Expand Up @@ -89,6 +83,12 @@ def pan(self):
self.tree_manager.move_tree_by_delta_pos(delta_x, delta_y)
self.previous_mouse_pos = self.current_mouse_pos
self.tree_manager.compute_transformed_coordinates(self.zoom_level)

def on_window_size_changed(self):
"""
Updates the command line interface when the window size changes.
This method ensures that the command line interface adapts to the new dimensions
of the window, maintaining a consistent user experience.
"""

self.cmdline_interface.on_window_size_changed()
Loading

0 comments on commit 21dc56a

Please sign in to comment.