optimized and commented code

Signed-off-by: Peter Siegmund <developer@mars3142.org>
This commit is contained in:
2025-06-14 19:02:51 +02:00
parent 4aa3e2cbeb
commit 52a49363eb
10 changed files with 494 additions and 134 deletions

View File

@@ -1,20 +1,33 @@
// Prevents multiple inclusions of this header file
#pragma once
// Standard libraries for function objects and smart pointers
#include <functional>
#include <memory>
// Project-specific headers
#include "common/Widget.h"
#include "u8g2.h"
class Widget;
// Structure for menu options and callback functions
typedef struct
{
// Pointer to u8g2 display object for graphics output
u8g2_t *u8g2;
// Callback function to set the current screen
// Parameter: Smart pointer to a Widget object
std::function<void(std::shared_ptr<Widget>)> setScreen;
// Callback function to add a new screen to the stack
// Parameter: Smart pointer to a Widget object
std::function<void(std::shared_ptr<Widget>)> pushScreen;
// Callback function to remove the top screen from the stack
// No parameters required
std::function<void()> popScreen;
// Callback function to handle button presses
// Parameter: 8-bit button ID/status
std::function<void(uint8_t button)> onButtonClicked;
} menu_options_t;
} menu_options_t;

View File

@@ -2,6 +2,20 @@
#include <functional>
enum class ButtonType { NONE, UP, DOWN, LEFT, RIGHT, SELECT, BACK };
// Enumeration defining the different types of buttons available in the system
// NONE represents no button pressed or an invalid button state
enum class ButtonType {
NONE, // No button or invalid state
UP, // Up directional button
DOWN, // Down directional button
LEFT, // Left directional button
RIGHT, // Right directional button
SELECT, // Select/confirm button
BACK // Back/cancel button
};
typedef std::function<void(uint8_t, ButtonType)> ButtonCallback;
// Type alias for button event callback function
// Parameters:
// - uint8_t: Button identifier or additional data
// - ButtonType: The type of button that was pressed
typedef std::function<void(uint8_t, ButtonType)> ButtonCallback;

View File

@@ -7,35 +7,131 @@
#include "Widget.h"
#include "data/MenuItem.h"
/**
* PSMenu - A menu widget class
*
* This class extends the Widget base class to provide a customizable menu system
* with various types of interactive menu items including text buttons, selections,
* number inputs, and toggles.
*/
class PSMenu : public Widget
{
public:
/**
* Constructor - Creates a new PSMenu instance
* @param options Pointer to menu configuration options
*/
explicit PSMenu(menu_options_t *options);
/**
* Destructor - Cleans up resources when menu is destroyed
*/
~PSMenu() override;
/**
* Adds a text-based menu item (button) to the menu
* @param id Unique identifier for this menu item
* @param text Display text shown on the menu
* @param callback Function to call when this item is selected
*/
void addText(uint8_t id, const std::string &text, const ButtonCallback &callback);
/**
* Adds a selection menu item (dropdown/list selection)
* @param id Unique identifier for this menu item
* @param text Display text/label for the selection
* @param value Reference to current selected value (will be modified)
* @param values Vector of all available options to choose from
* @param callback Function to call when selection changes
*/
void addSelection(uint8_t id, const std::string &text, std::string &value, const std::vector<std::string>& values,
const ButtonCallback &callback);
/**
* Adds a numeric input menu item
* @param id Unique identifier for this menu item
* @param text Display text/label for the number input
* @param value Reference to current numeric value as string (will be modified)
* @param callback Function to call when value changes
*/
void addNumber(uint8_t id, const std::string &text, std::string &value, const ButtonCallback &callback);
/**
* Adds a toggle/checkbox menu item
* @param id Unique identifier for this menu item
* @param text Display text/label for the toggle
* @param selected Current state of the toggle (true = on, false = off)
* @param callback Function to call when toggle state changes
*/
void addToggle(uint8_t id, const std::string &text, bool selected, const ButtonCallback &callback);
private:
/**
* Renders the entire menu on screen
* Override from Widget base class
*/
void render() override;
/**
* Handles button press events from the controller/input system
* @param button The button that was pressed
* Override from Widget base class
*/
void onButtonClicked(uint8_t button) override;
// Navigation event handlers
/**
* Handles down arrow/stick input - moves selection down
*/
void onPressedDown();
/**
* Handles up arrow/stick input - moves selection up
*/
void onPressedUp();
/**
* Handles left arrow/stick input - decreases value for current item
*/
void onPressedLeft() const;
/**
* Handles right arrow/stick input - increases value for current item
*/
void onPressedRight() const;
/**
* Handles select/confirm button (X on PlayStation controller)
*/
void onPressedSelect() const;
/**
* Handles back/cancel button (Circle on PlayStation controller)
*/
void onPressedBack() const;
// Rendering helper methods
/**
* Draws the scroll bar indicating position in long menus
*/
void drawScrollBar() const;
/**
* Draws the selection highlight box around current menu item
*/
void drawSelectionBox() const;
/**
* Renders an individual menu item widget
* @param item Pointer to the menu item to render
* @param font Font to use for rendering text
* @param x X coordinate for rendering position
* @param y Y coordinate for rendering position
*/
void renderWidget(const MenuItem *item, const uint8_t *font, int x, int y) const;
size_t m_selected_item = 0;
std::vector<MenuItem> m_items;
menu_options_t *m_options;
// Member variables
size_t m_selected_item = 0; ///< Index of currently selected menu item
std::vector<MenuItem> m_items; ///< Collection of all menu items
menu_options_t *m_options; ///< Pointer to menu configuration options
};

View File

@@ -3,22 +3,50 @@
#include "MenuOptions.h"
#include "Widget.h"
/**
* ScrollBar class that represents a vertical scrollbar widget
* Inherits from Widget base class and provides scrolling functionality
*/
class ScrollBar final : public Widget
{
public:
public:
/**
* Constructor for ScrollBar
* @param options Pointer to menu options configuration
* @param x X coordinate position of the scrollbar
* @param y Y coordinate position of the scrollbar
* @param width Width of the scrollbar
* @param height Height of the scrollbar
*/
ScrollBar(const menu_options_t *options, size_t x, size_t y, size_t width, size_t height);
/**
* Renders the scrollbar to the screen
* Overrides the base Widget render method
*/
void render() override;
/**
* Updates the scrollbar state with new values
* @param value Current scroll position value
* @param max Maximum scroll value
* @param min Minimum scroll value (default: 0)
*/
void refresh(size_t value, size_t max, size_t min = 0);
private:
size_t m_x;
size_t m_y;
size_t m_width;
size_t m_height;
size_t m_value;
size_t m_max;
size_t m_min;
private:
// Position and dimensions
size_t m_x; // X coordinate of the scrollbar
size_t m_y; // Y coordinate of the scrollbar
size_t m_width; // Width of the scrollbar
size_t m_height; // Height of the scrollbar
size_t m_thumbHeight;
size_t m_thumbY;
};
// Scroll state values
size_t m_value; // Current scroll position
size_t m_max; // Maximum scroll value
size_t m_min; // Minimum scroll value
// Calculated thumb properties
size_t m_thumbHeight; // Height of the scroll thumb
size_t m_thumbY; // Y position of the scroll thumb
};

View File

@@ -1,22 +1,52 @@
#pragma once
#include <cstdint>
#include "u8g2.h"
/**
* Base class for UI widgets that can be rendered and interact with user input.
* This class provides a common interface for all widgets in the system.
*/
class Widget
{
public:
/**
* Constructs a widget with the given u8g2 display context.
* @param u8g2 Pointer to the u8g2 display context used for rendering
*/
explicit Widget(u8g2_t *u8g2);
/**
* Virtual destructor to ensure proper cleanup of derived classes.
*/
virtual ~Widget() = default;
/**
* Updates the widget's internal state based on elapsed time.
* This method is called once per frame to handle animations,
* timers, or other time-dependent behavior.
* @param dt Delta time in milliseconds since last update
*/
virtual void update(uint64_t dt);
/**
* Renders the widget to the display.
* This method should be overridden by derived classes to implement
* their specific rendering logic using the u8g2 display context.
*/
virtual void render();
/**
* Handles button click events.
* This method is called when a button is pressed and allows
* the widget to respond to user input.
* @param button The identifier of the button that was clicked
*/
virtual void onButtonClicked(uint8_t button);
protected:
/**
* Pointer to the u8g2 display context used for rendering operations.
* This provides access to drawing functions for text, graphics, and other UI elements.
*/
u8g2_t *u8g2;
};

View File

@@ -5,27 +5,103 @@
#include "common/Common.h"
/**
* Represents a menu item that can be displayed in a user interface.
* Supports different types of menu items with various interaction capabilities.
*/
class MenuItem
{
public:
/**
* Constructor for a simple menu item with text and callback.
* @param id Unique identifier for this menu item
* @param type Type of the menu item (defines behavior and appearance)
* @param text Display text for the menu item
* @param callback Function to call when the item is activated
*/
MenuItem(uint8_t id, uint8_t type, std::string text, ButtonCallback callback);
/**
* Constructor for a menu item with a value field.
* @param id Unique identifier for this menu item
* @param type Type of the menu item
* @param text Display text for the menu item
* @param value Current value associated with this item
* @param callback Function to call when the item is activated
*/
MenuItem(uint8_t id, uint8_t type, std::string text, std::string value, ButtonCallback callback);
/**
* Constructor for a menu item with multiple selectable values.
* @param id Unique identifier for this menu item
* @param type Type of the menu item
* @param text Display text for the menu item
* @param value Currently selected value
* @param values List of all available values for selection
* @param callback Function to call when the item is activated
*/
MenuItem(uint8_t id, uint8_t type, std::string text, std::string value, std::vector<std::string> values,
ButtonCallback callback);
/**
* Constructor for a boolean/toggle menu item.
* @param id Unique identifier for this menu item
* @param type Type of the menu item
* @param text Display text for the menu item
* @param selected Whether this item is currently selected/checked
* @param callback Function to call when the item is activated
*/
MenuItem(uint8_t id, uint8_t type, std::string text, bool selected, ButtonCallback callback);
/**
* Gets the unique identifier of this menu item.
* @return The menu item's ID
*/
[[nodiscard]] uint8_t getId() const;
/**
* Gets the type of this menu item.
* @return The menu item's type
*/
[[nodiscard]] uint8_t getType() const;
/**
* Gets the display text of this menu item.
* @return Reference to the menu item's text
*/
[[nodiscard]] const std::string &getText() const;
/**
* Gets the current value of this menu item.
* @return Reference to the menu item's current value
*/
[[nodiscard]] const std::string &getValue() const;
/**
* Sets a new value for this menu item.
* @param value The new value to set
*/
void setValue(const std::string &value);
/**
* Handles button press events for this menu item.
* Calls the associated callback function if one exists.
* @param id The ID of the item that was pressed
* @param button The type of button that was pressed
*/
void onButtonPressed(uint8_t id, ButtonType button) const;
/**
* Checks if this menu item has an associated callback function.
* @return true if a callback is set, false otherwise
*/
[[nodiscard]] bool hasCallback() const;
private:
uint8_t m_id;
uint8_t m_type;
std::string m_text;
std::string m_value;
std::vector<std::string> m_values;
ButtonCallback m_callback;
uint8_t m_id; // Unique identifier for this menu item
uint8_t m_type; // Type defining the item's behavior
std::string m_text; // Display text shown to the user
std::string m_value; // Current value (for value-based items)
std::vector<std::string> m_values; // Available values (for selection items)
ButtonCallback m_callback; // Function to call when activated
};

View File

@@ -2,13 +2,30 @@
#include "common/PSMenu.h"
/**
* MainMenu class - represents the main menu interface of the application
* Inherits from PSMenu to provide menu functionality
*/
class MainMenu final : public PSMenu
{
public:
/**
* Constructor - initializes the main menu with given options
* @param options Pointer to menu options configuration
*/
explicit MainMenu(menu_options_t *options);
private:
/**
* Handles button press events from the menu interface
* @param id Button identifier that was pressed
* @param button Type of button that was pressed
*/
void onButtonPressed(uint8_t id, ButtonType button) const;
/**
* Pointer to menu options configuration
* Stores the menu configuration passed during construction
*/
menu_options_t *m_options;
};

View File

@@ -3,13 +3,48 @@
#include "MenuOptions.h"
#include "common/Widget.h"
/**
* @brief SplashScreen class represents the initial screen shown when the application starts
*
* This class inherits from Widget and is responsible for displaying the splash screen
* that typically shows application branding, loading status, or initialization messages.
* It's marked as final to prevent further inheritance.
*/
class SplashScreen final : public Widget
{
public:
/**
* @brief Constructs a new SplashScreen object
*
* @param options Pointer to menu options configuration that controls
* the behavior and appearance of the splash screen
*/
explicit SplashScreen(menu_options_t *options);
/**
* @brief Updates the splash screen state and animations
*
* This method is called every frame to update the splash screen's
* internal state, handle timing, animations, or transitions.
*
* @param dt Delta time in milliseconds since the last update
*/
void update(uint64_t dt) override;
/**
* @brief Renders the splash screen to the display
*
* This method handles all the drawing operations for the splash screen,
* including background, logos, text, and any visual effects.
*/
void render() override;
private:
/**
* @brief Pointer to menu options configuration
*
* Stores the configuration options that control various aspects
* of the splash screen's behavior and appearance.
*/
menu_options_t *m_options;
};
};