Files
balatro-gba-chinese-jocker-…/source/selection_grid.c
T
Geralt e7f550ce22 [Feature] Implement options menu and reading/writing values from/to the SRAM (#449)
* background

* Implement Options button

* Update main menu artwork

* add options menu tiles

* prepare options menu handling

* incorporate main menu refactor

* toto

* add all menu interactions

* do not reset selection_x in main_menu

* options struct

* fix rebase from ricfehr3's repo

* Implement high contrast cards

* Save/Load options values to SRAM

* load options on game_init and validate data within the load_options function

* Get changes from ricfehr3's PR

* recover fix for glitched ace

* clang format

* clang format

* clang format

* Save 500B of image data by using only minimal set of tiles instead of keeping final window in source file

* clang format

* clang format

* Implement Options button

* Update main menu artwork

* progress, leaving it alone for now until discussion

* make game_vars an extern

* Move options vars into their own struct

* rollback previous commit

* Implement save validation. Version check to come

* implement data validity + version check for game saves

* clang format

* Save position in rng sequence for consistent run after load

* fix rebase issues

* fix rebase issue

* remove unused/duplicate define

* Oops, made a mistake when testing PR #424

* encode high contrast palette into an image, and load it at game_start

* document save.c/h and correct typos

* correct alignment

* game speed and high contrast buttons working

* small cleanup

* sliders also work again now

* clang format

* Moved balatro_version extern declaration to version.h file

* removed unsused variable

* make showing ROM version a toggle

* move ante, round and money to the GameVariables struct

* Rename SaveCheckInfo struct to SaveHeader for clarity

* Add SaveHeader struct explanation

* clang format

* Revert unnecessary tileset optimization

* SFX now follows the sound volume slider

* clang format

* Music now follows volume slider

* Changed volume slider increment from 5 to 20 to avoid having the music sound too rough under 20% volume

* refactor main_menu.c to use SelectionGrid and Button structs

* clang format

* make a "new" save button so we don't save during the state transition

* clang format

* Reduced state transition flicker by only changing affine background to a different one

* Fix loading game too late in game init sequence

* First batch of suggestions from @MeirGavish

* Remove unnecessary intermediary buffers for high contrast palette swapping

* comment row warp hack for button input

* Add some of Copilot's suggestions

* rename balatro_version into gbalatro_version for rebranding reasons + version display erase screen fix

* remove unnecessary NULL check on `on_selection_changed` method

* implement small game speed arrow buttons animation

* factorize some code

* remove unnecessary data validation in load_game(), since we already checked the header

* Implement @MeirGavish 's suggestion regarding splitting the huge `on_update` function

* clang format

* implement @ricfehr3 's suggestion about keeping last selected button instead of using an obscure `on_boot` boolean

* Apply @ricfehr3's suggestions

* fix clang-format flags

---------

Co-authored-by: MathisMartin31 <mathis.martin31@gmail.com>
2026-05-02 14:28:52 -07:00

134 lines
4.2 KiB
C

#include "selection_grid.h"
static void selection_grid_process_directional_input(SelectionGrid* selection_grid)
{
int horz_tri_input = bit_tribool(key_hit(KEY_ANY), KI_RIGHT, KI_LEFT);
if (horz_tri_input != 0)
{
selection_grid_move_selection_horz(selection_grid, horz_tri_input);
/* Avoid handling both vertical and horizontal input at the same time,
* it creates all sorts of difficult edge cases.
*/
return;
}
int vert_tri_input = bit_tribool(key_hit(KEY_ANY), KI_DOWN, KI_UP);
if (vert_tri_input != 0)
{
selection_grid_move_selection_vert(selection_grid, vert_tri_input);
}
}
void selection_grid_move_selection_horz(SelectionGrid* selection_grid, int direction_tribool)
{
if (selection_grid == NULL || selection_grid->selection.y < 0 ||
selection_grid->selection.y >= selection_grid->num_rows)
{
return;
}
SelectionGridRow current_row = selection_grid->rows[selection_grid->selection.y];
// Choose the horizontal exit index if it exists
Selection new_selection =
current_row.attributes.has_h_exit_idx
? (Selection){selection_grid->selection.x, current_row.attributes.h_exit_idx}
: selection_grid->selection;
new_selection.x += direction_tribool;
int row_size = selection_grid->rows[new_selection.y].get_size();
bool wrap_enabled = selection_grid->rows[new_selection.y].attributes.wrap;
if (wrap_enabled)
{
new_selection.x = wrap(new_selection.x, 0, row_size);
}
if (wrap_enabled || (new_selection.x >= 0 && new_selection.x < row_size))
{
bool proceed_selection =
selection_grid->rows[selection_grid->selection.y].on_selection_changed(
selection_grid,
current_row.row_idx,
&selection_grid->selection,
&new_selection
);
if (proceed_selection)
{
selection_grid->selection = new_selection;
}
}
}
void selection_grid_move_selection_vert(SelectionGrid* selection_grid, int direction_tribool)
{
if (selection_grid == NULL)
return;
Selection selection = selection_grid->selection;
Selection new_selection = selection;
new_selection.y += direction_tribool;
if (new_selection.y >= 0 && new_selection.y < selection_grid->num_rows)
{
int new_row_size = selection_grid->rows[new_selection.y].get_size();
if (new_row_size <= 0)
return;
int old_row_size = selection_grid->rows[selection.y].get_size();
// Branchless set to 1 if 0 to avoid division by 0
old_row_size += (old_row_size == 0);
// Maintain relative horizontal position
// The operations are equivalent to fixed point if all the numbers were converted
new_selection.x = fx2int(selection.x * ((int2fx(new_row_size) / old_row_size)));
bool proceed_selection = true;
if (selection.y >= 0 && selection.y < selection_grid->num_rows)
{
proceed_selection =
selection_grid->rows[selection.y]
.on_selection_changed(selection_grid, selection.y, &selection, &new_selection);
}
if (proceed_selection)
{
proceed_selection = selection_grid->rows[new_selection.y].on_selection_changed(
selection_grid,
new_selection.y,
&selection,
&new_selection
);
}
if (proceed_selection)
{
selection_grid->selection = new_selection;
}
}
}
void selection_grid_process_input(SelectionGrid* selection_grid)
{
if (selection_grid == NULL || selection_grid->rows == NULL)
return;
selection_grid_process_directional_input(selection_grid);
u32 non_directional_key = KEY_ANY & ~KEY_DIR;
if (key_transit(non_directional_key))
{
// To make the next line shorter and more readable
Selection* selection = &selection_grid->selection;
if (selection_grid->rows[selection->y].on_key_transit != NULL)
{
selection_grid->rows[selection->y].on_key_transit(selection_grid, selection);
}
}
}