From ae81a5dc8c575f60d11594b676495339f82933b1 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 13 Nov 2026 17:02:51 -0800 Subject: [PATCH] Nintendo Switch: menu cursor moves on a grid of items menu_cursor_moves() gives the D-pad presses from one item to another on a list or a grid with a short last row, as bdsp/rebuild-base carries it (09acde1e3, BDSP's new-game intro picks). Its tests register with the shared test runner; each case is a name, an input and an expectation. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../ComputerPrograms_UnitTestRunner.cpp | 2 + .../MenuCursor/NintendoSwitch_MenuCursor.cpp | 63 +++++++ .../MenuCursor/NintendoSwitch_MenuCursor.h | 44 +++++ .../NintendoSwitch_MenuCursor_Tests.cpp | 166 ++++++++++++++++++ .../NintendoSwitch_MenuCursor_Tests.h | 23 +++ SerialPrograms/cmake/SourceFiles.cmake | 4 + 6 files changed, 302 insertions(+) create mode 100644 SerialPrograms/Source/NintendoSwitch/Programs/MenuCursor/NintendoSwitch_MenuCursor.cpp create mode 100644 SerialPrograms/Source/NintendoSwitch/Programs/MenuCursor/NintendoSwitch_MenuCursor.h create mode 100644 SerialPrograms/Source/NintendoSwitch/Programs/MenuCursor/NintendoSwitch_MenuCursor_Tests.cpp create mode 100644 SerialPrograms/Source/NintendoSwitch/Programs/MenuCursor/NintendoSwitch_MenuCursor_Tests.h diff --git a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp index 57691f3c19..cea6d90964 100644 --- a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp +++ b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp @@ -31,6 +31,7 @@ #include "NintendoSwitch/Inference/NintendoSwitch_FailedToConnectDetector.h" #include "NintendoSwitch/Inference/NintendoSwitch_UpdatePopupDetector.h" #include "NintendoSwitch/Programs/Typewriter/NintendoSwitch_Typewriter_Tests.h" +#include "NintendoSwitch/Programs/MenuCursor/NintendoSwitch_MenuCursor_Tests.h" #include "NintendoSwitch/Controllers/SysbotBase/SysbotBase_Connection.h" #include "PokemonBDSP/Inference/PokemonBDSP_DialogDetector.h" #include "PokemonBDSP/Inference/PokemonBDSP_MenuDetector.h" @@ -90,6 +91,7 @@ UnitTestDatabase make_UNIT_TESTS_ALL(){ NintendoSwitch::add_tests_FailedToConnectDetector(ret); NintendoSwitch::add_tests_UpdatePopupDetector(ret); NintendoSwitch::Typewriter::add_tests_TypewriterTextMapping(ret); + NintendoSwitch::add_tests_MenuCursor(ret); NintendoSwitch::PokemonBDSP::add_tests_DialogDetector(ret); NintendoSwitch::PokemonBDSP::add_tests_MenuDetector(ret); NintendoSwitch::PokemonFRLG::add_tests(ret); diff --git a/SerialPrograms/Source/NintendoSwitch/Programs/MenuCursor/NintendoSwitch_MenuCursor.cpp b/SerialPrograms/Source/NintendoSwitch/Programs/MenuCursor/NintendoSwitch_MenuCursor.cpp new file mode 100644 index 0000000000..d90e5fc5d1 --- /dev/null +++ b/SerialPrograms/Source/NintendoSwitch/Programs/MenuCursor/NintendoSwitch_MenuCursor.cpp @@ -0,0 +1,63 @@ +/* Menu Cursor + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "NintendoSwitch_MenuCursor.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ + +using namespace std::chrono_literals; + + +namespace{ + +void add_presses(std::vector& moves, DpadPosition direction, size_t count){ + moves.insert(moves.end(), count, direction); +} +void add_sideways(std::vector& moves, size_t from_column, size_t to_column){ + if (to_column > from_column){ + add_presses(moves, DPAD_RIGHT, to_column - from_column); + }else{ + add_presses(moves, DPAD_LEFT, from_column - to_column); + } +} + +} // namespace + + +std::optional> menu_cursor_moves(const MenuGrid& grid, size_t from, size_t to){ + if (grid.columns == 0 || from >= grid.count || to >= grid.count){ + return std::nullopt; + } + const size_t from_row = from / grid.columns; + const size_t from_column = from % grid.columns; + const size_t to_row = to / grid.columns; + const size_t to_column = to % grid.columns; + std::vector moves; + if (to_row > from_row){ + // The start's row is above the target's, so it is full: sideways first. + add_sideways(moves, from_column, to_column); + add_presses(moves, DPAD_DOWN, to_row - from_row); + }else{ + // Up to the target's row first, which is full unless it is the start's. + add_presses(moves, DPAD_UP, from_row - to_row); + add_sideways(moves, from_column, to_column); + } + return moves; +} + + +void press_cursor_moves(ProControllerContext& context, const std::vector& moves){ + for (DpadPosition move : moves){ + pbf_press_dpad(context, move, 80ms, 320ms); + } +} + + + +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/NintendoSwitch/Programs/MenuCursor/NintendoSwitch_MenuCursor.h b/SerialPrograms/Source/NintendoSwitch/Programs/MenuCursor/NintendoSwitch_MenuCursor.h new file mode 100644 index 0000000000..077dfd0175 --- /dev/null +++ b/SerialPrograms/Source/NintendoSwitch/Programs/MenuCursor/NintendoSwitch_MenuCursor.h @@ -0,0 +1,44 @@ +/* Menu Cursor + * + * From: https://github.com/PokemonAutomation/ + * + * Moves the cursor of a fixed list or grid (a game's language select, the + * player's look) to an item by its index. The menu's shape is data; the + * presses follow from it; a detector for the menu reads where the cursor is. + * + */ + +#ifndef PokemonAutomation_NintendoSwitch_MenuCursor_H +#define PokemonAutomation_NintendoSwitch_MenuCursor_H + +#include +#include +#include +#include "NintendoSwitch/Controllers/NintendoSwitch_ControllerButtons.h" +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ + + +// A menu of `count` items in rows of `columns`, numbered left to right, then +// top to bottom; a list has one column. Only the last row may be short. +struct MenuGrid{ + size_t columns = 1; + size_t count = 0; +}; + +// The D-pad presses that take the cursor from item `from` to item `to`. +// None crosses an edge, so they hold whether or not the menu wraps, and +// sideways presses happen on a full row. nullopt when either item is outside +// the grid. +std::optional> menu_cursor_moves(const MenuGrid& grid, size_t from, size_t to); + +// Presses `moves` in order, one D-pad tap each. +void press_cursor_moves(ProControllerContext& context, const std::vector& moves); + + + +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/NintendoSwitch/Programs/MenuCursor/NintendoSwitch_MenuCursor_Tests.cpp b/SerialPrograms/Source/NintendoSwitch/Programs/MenuCursor/NintendoSwitch_MenuCursor_Tests.cpp new file mode 100644 index 0000000000..ca591e816d --- /dev/null +++ b/SerialPrograms/Source/NintendoSwitch/Programs/MenuCursor/NintendoSwitch_MenuCursor_Tests.cpp @@ -0,0 +1,166 @@ +/* Menu Cursor - Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "NintendoSwitch_MenuCursor.h" +#include "NintendoSwitch_MenuCursor_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ + +namespace{ + + +std::string describe(const std::vector& moves){ + std::string out; + for (DpadPosition move : moves){ + out += (out.empty() ? "" : " ") + dpad_to_string(move); + } + return out.empty() ? "(none)" : out; +} + +struct MovesInput{ + MenuGrid grid; + size_t from; + size_t to; +}; + +struct MovesCase{ + std::string_view name; + MovesInput input; + std::vector expectation; +}; + +UnitTestResult test_cursor_moves(){ + const MenuGrid list{1, 9}; + const MenuGrid looks{4, 8}; + // Seven items in rows of three: the last row holds one. + const MenuGrid ragged{3, 7}; + const MovesCase cases[] = { + {"list: stay", {list, 1, 1}, {}}, + {"list: down", {list, 1, 2}, {DPAD_DOWN}}, + {"list: up to the top", {list, 1, 0}, {DPAD_UP}}, + {"list: top to bottom, no wrap", {list, 0, 8}, std::vector(8, DPAD_DOWN)}, + {"grid: right on the top row", {looks, 0, 2}, {DPAD_RIGHT, DPAD_RIGHT}}, + {"grid: down a row", {looks, 0, 4}, {DPAD_DOWN}}, + {"grid: sideways, then down", {looks, 1, 7}, {DPAD_RIGHT, DPAD_RIGHT, DPAD_DOWN}}, + {"grid: up, then sideways", {looks, 7, 0}, {DPAD_UP, DPAD_LEFT, DPAD_LEFT, DPAD_LEFT}}, + // The short row has only column 0: sideways happens on the full row above. + {"ragged: into the short row", {ragged, 2, 6}, {DPAD_LEFT, DPAD_LEFT, DPAD_DOWN, DPAD_DOWN}}, + {"ragged: out of the short row", {ragged, 6, 5}, {DPAD_UP, DPAD_RIGHT, DPAD_RIGHT}}, + }; + for (const MovesCase& c : cases){ + const std::optional> moves = menu_cursor_moves(c.input.grid, c.input.from, c.input.to); + if (!moves){ + return std::string(c.name) + ": no moves."; + } + if (*moves != c.expectation){ + return std::string(c.name) + ": " + describe(*moves) + ", expected " + describe(c.expectation) + "."; + } + } + if (menu_cursor_moves(looks, 0, 8) || menu_cursor_moves(looks, 8, 0) || menu_cursor_moves(MenuGrid{0, 4}, 0, 1)){ + return "An item outside the grid, or a grid with no columns, must give no moves."; + } + return true; +} + +// Walks the moves from `from` to `to` on `grid`; what went wrong, or "". +std::string check_walk(const MenuGrid& grid, size_t from, size_t to){ + const std::string pair = std::to_string(grid.columns) + "x" + std::to_string(grid.count) + + " from " + std::to_string(from) + " to " + std::to_string(to); + const std::optional> moves = menu_cursor_moves(grid, from, to); + if (!moves){ + return pair + ": no moves."; + } + long row = (long)(from / grid.columns); + long column = (long)(from % grid.columns); + for (DpadPosition move : *moves){ + row += move == DPAD_DOWN ? 1 : move == DPAD_UP ? -1 : 0; + column += move == DPAD_RIGHT ? 1 : move == DPAD_LEFT ? -1 : 0; + const long index = row * (long)grid.columns + column; + if (row < 0 || column < 0 || column >= (long)grid.columns || index >= (long)grid.count){ + return pair + ": " + describe(*moves) + " leaves the grid."; + } + } + if (row * (long)grid.columns + column != (long)to){ + return pair + ": " + describe(*moves) + " ends elsewhere."; + } + return ""; +} + +// Every pair of items in a few shapes: the moves land on the target and never +// leave the grid or step on a missing item. +UnitTestResult test_cursor_moves_stay_inside(){ + for (const MenuGrid& grid : {MenuGrid{1, 9}, MenuGrid{4, 8}, MenuGrid{3, 7}, MenuGrid{5, 1}}){ + for (size_t from = 0; from < grid.count; from++){ + for (size_t to = 0; to < grid.count; to++){ + const std::string error = check_walk(grid, from, to); + if (!error.empty()){ + return error; + } + } + } + } + return true; +} + +struct MenuCursorInput{ + UnitTestResult (*body)(); +}; + +struct MenuCursorExpectation{ +}; + +struct MenuCursorCase{ + std::string name; + MenuCursorInput input; + MenuCursorExpectation expectation; +}; + +class Test_MenuCursor : public UnitTest{ +public: + explicit Test_MenuCursor(MenuCursorCase test_case) + : UnitTest("NintendoSwitch::MenuCursor - " + test_case.name) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + return m_case.input.body(); + } + +private: + MenuCursorCase m_case; +}; + +const MenuCursorCase MENU_CURSOR_CASES[] = { + { + .name = "presses from item to item", + .input = {test_cursor_moves}, + .expectation = {}, + }, + { + .name = "presses stay inside the grid", + .input = {test_cursor_moves_stay_inside}, + .expectation = {}, + }, +}; + + +} // namespace + + +void add_tests_MenuCursor(UnitTestDatabase& database){ + for (const MenuCursorCase& item : MENU_CURSOR_CASES){ + database.add(item); + } +} + + + +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/NintendoSwitch/Programs/MenuCursor/NintendoSwitch_MenuCursor_Tests.h b/SerialPrograms/Source/NintendoSwitch/Programs/MenuCursor/NintendoSwitch_MenuCursor_Tests.h new file mode 100644 index 0000000000..10b6131cb3 --- /dev/null +++ b/SerialPrograms/Source/NintendoSwitch/Programs/MenuCursor/NintendoSwitch_MenuCursor_Tests.h @@ -0,0 +1,23 @@ +/* Menu Cursor - Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_NintendoSwitch_MenuCursor_Tests_H +#define PokemonAutomation_NintendoSwitch_MenuCursor_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ + + +// The cursor presses for list and grid items. No console. +void add_tests_MenuCursor(UnitTestDatabase& database); + + + +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index bc315ba159..84d08f6474 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1438,6 +1438,10 @@ file(GLOB LIBRARY_SOURCES Source/NintendoSwitch/Programs/NintendoSwitch_TurboButton.h Source/NintendoSwitch/Programs/NintendoSwitch_TurboMacro.cpp Source/NintendoSwitch/Programs/NintendoSwitch_TurboMacro.h + Source/NintendoSwitch/Programs/MenuCursor/NintendoSwitch_MenuCursor.cpp + Source/NintendoSwitch/Programs/MenuCursor/NintendoSwitch_MenuCursor.h + Source/NintendoSwitch/Programs/MenuCursor/NintendoSwitch_MenuCursor_Tests.cpp + Source/NintendoSwitch/Programs/MenuCursor/NintendoSwitch_MenuCursor_Tests.h Source/NintendoSwitch/Programs/Typewriter/NintendoSwitch_Typewriter.cpp Source/NintendoSwitch/Programs/Typewriter/NintendoSwitch_Typewriter.h Source/NintendoSwitch/Programs/Typewriter/NintendoSwitch_Typewriter_Tests.cpp