From f76c25614ce0bc690be422eda0af7f6615dcc89c Mon Sep 17 00:00:00 2001 From: MeirGavish Date: Mon, 22 Dec 2025 08:00:08 +0200 Subject: [PATCH] Change selection grid to maintain relative horizontal position when switching rows (#329) * Change selection grid to maintain relative horizontal position when switching rows * clang-format * Fixed joker selling index error * Protect against division by 0 and removed redundant fixed point conversions. --- source/game.c | 6 +++++- source/selection_grid.c | 15 +++++++++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/source/game.c b/source/game.c index d54374a..882b995 100644 --- a/source/game.c +++ b/source/game.c @@ -3994,9 +3994,13 @@ static void jokers_sel_row_on_key_transit(SelectionGrid* selection_grid, Selecti if (key_hit(SELL_KEY)) { - game_sell_joker(selection->x); + int sold_joker_idx = selection->x; + // Move the selection away from the jokers so it doesn't point to an invalid place + // Do this before selling the joker so valid row sizes are used selection_grid_move_selection_vert(selection_grid, SCREEN_DOWN); + + game_sell_joker(sold_joker_idx); } } diff --git a/source/selection_grid.c b/source/selection_grid.c index f42dd7b..d21f63b 100644 --- a/source/selection_grid.c +++ b/source/selection_grid.c @@ -57,12 +57,15 @@ void selection_grid_move_selection_vert(SelectionGrid* selection_grid, int direc if (new_row_size <= 0) return; - if (selection.x >= new_row_size) - { - // TODO: Maintain relative horizontal position - // Clip selection to row size - new_selection.x = new_row_size - 1; - } + 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))); + if (selection.y >= 0 && selection.y < selection_grid->num_rows) { selection_grid->rows[selection.y]