Refactored to use selection grid during round (#348)

* Added wrapping to selection grid

* Added return value to on_selection_changed to allow aborting on wrap

* Extracted button code to button.c/.h

* Bounds checking changes for selection_grid_move_selection_horz()

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Rickey <ric@rf3.xyz>
This commit is contained in:
MeirGavish
2026-01-15 20:32:09 +02:00
committed by GitHub
co-authored by Copilot Rickey
parent b7cc3ec5d6
commit 5300846f73
5 changed files with 553 additions and 322 deletions
+76
View File
@@ -0,0 +1,76 @@
/**
* @file button.h
*
* @brief A button structure containing the common button functionalities
*/
#ifndef BUTTON_H
#define BUTTON_H
#include <tonc_types.h>
#define BTN_HIGHLIGHT_COLOR 0xFFFF
#define BUTTON_SFX_VOLUME 154 // 60% of MM_FULL_VOLUME
/**
* @brief The function to be called when the button is pressed
*/
typedef void (*ButtonOnPressedFunc)(void);
/**
* @brief Returns true if the button should be activated when pressed.
*/
typedef bool (*ButtonCanBePressedFunc)(void);
/**
* @brief A button representation
*/
typedef struct
{
// Using u8 for the palette indices to make sure they don't overflow pal_bg_mem
/**
* @brief Palette index of the button border whose color is updated depending on highlight.
*/
u8 border_pal_idx;
/**
* @brief Palette index of the color of the button itself, used for the border when not
* highlighted.
*/
u8 button_pal_idx;
/**
* @brief Called when the button is pressed if @ref can_be_pressed, don't call this directly,
* call @ref button_press() instead.
* Should not be set to NULL.
*/
ButtonOnPressedFunc on_pressed;
/**
* @brief Returns true if the button should be activated when pressed.
* Can be NULL if button can always be pressed.
*/
ButtonCanBePressedFunc can_be_pressed;
} Button;
/**
* @brief Set the button highlight on or off.
* @param button The button to modify. No-op on NULL.
* @param highlight true to highlight, false to unhighlight.
*/
void button_set_highlight(Button* button, bool highlight);
/**
* @brief Execute a button press, by calling the button's @ref on_pressed function.
* This checks the button's @ref can_be_pressed function and will do nothing if
* returns false.
*
* Button presses should go through this function rather than directly call @ref on_pressed.
*
* @param button The button being pressed. No-op on NULL.
*/
void button_press(Button* button);
#endif // BUTTON_H
+15 -1
View File
@@ -37,9 +37,10 @@ typedef struct SelectionGrid SelectionGrid;
* Contains the previous cursor position
* @param new_selection Pointer to the Selection state after the change occurred.
* Contains the new cursor position
* @return false if the selection change needs to be aborted, true if can proceed.
*
*/
typedef void (*RowOnSelectionChangedFunc)(
typedef bool (*RowOnSelectionChangedFunc)(
SelectionGrid* selection_grid,
int row_idx,
const Selection* prev_selection,
@@ -71,6 +72,18 @@ typedef int (*RowGetSizeFunc)();
*/
typedef void (*RowOnKeyTransitFunc)(SelectionGrid* selection_grid, Selection* selection);
/**
* @brief A set of attributes to the selection grid row affecting selection grid behavior.
*/
typedef struct
{
/**
* @brief Whether to wrap selection when it passes the end of the row.
*/
bool wrap;
} SelGridRowAttributes;
/**
* @brief A single row in the selection grid, defined by its callback functions.
*
@@ -82,6 +95,7 @@ struct SelectionGridRow
RowGetSizeFunc get_size;
RowOnSelectionChangedFunc on_selection_changed;
RowOnKeyTransitFunc on_key_transit;
SelGridRowAttributes attributes;
};
/**