Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -62,7 +63,7 @@



UnitTestDatabase make_UNIT_TESTS_ALL(){

Check warning on line 66 in SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::ComputerPrograms::make_UNIT_TESTS_ALL(): function_lines=47 (advisory above 25, limit 50)
UnitTestDatabase ret;

add_tests_QtEventThreadPool(ret, GlobalThreadPools::computation_normal());
Expand Down Expand Up @@ -96,6 +97,7 @@
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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<DpadPosition>& moves, DpadPosition direction, size_t count){

Check warning on line 18 in SerialPrograms/Source/NintendoSwitch/Programs/MenuCursor/NintendoSwitch_MenuCursor.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

style: add_presses: output parameter 'std::vector<DpadPosition>& moves' comes before an input; put inputs (by value, const&) first and non-const references last (Google "Function Parameter Ordering")
moves.insert(moves.end(), count, direction);
}
void add_sideways(std::vector<DpadPosition>& moves, size_t from_column, size_t to_column){

Check warning on line 21 in SerialPrograms/Source/NintendoSwitch/Programs/MenuCursor/NintendoSwitch_MenuCursor.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

style: add_sideways: output parameter 'std::vector<DpadPosition>& moves' comes before an input; put inputs (by value, const&) first and non-const references last (Google "Function Parameter Ordering")
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<std::vector<DpadPosition>> 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<DpadPosition> 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<DpadPosition>& moves){
for (DpadPosition move : moves){
pbf_press_dpad(context, move, 80ms, 320ms);
}
}



} // namespace NintendoSwitch
} // namespace PokemonAutomation
Original file line number Diff line number Diff line change
@@ -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 <cstddef>
#include <optional>
#include <vector>
#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<std::vector<DpadPosition>> 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<DpadPosition>& moves);



} // namespace NintendoSwitch
} // namespace PokemonAutomation
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/* Menu Cursor - Tests
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <string>
#include <vector>
#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<DpadPosition>& 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<DpadPosition> expectation;
};

UnitTestResult test_cursor_moves(){

Check warning on line 40 in SerialPrograms/Source/NintendoSwitch/Programs/MenuCursor/NintendoSwitch_MenuCursor_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::test_cursor_moves(): function_lines=32 (advisory above 25, limit 50)
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<DpadPosition>(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<std::vector<DpadPosition>> moves = menu_cursor_moves(c.input.grid, c.input.from, c.input.to);
if (!moves){
return std::string(c.name) + ": no moves.";

Check warning on line 61 in SerialPrograms/Source/NintendoSwitch/Programs/MenuCursor/NintendoSwitch_MenuCursor_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

style: build strings with std::format, not + on a literal / std::to_string / std::string (fork AGENTS.md "Strings")
}
if (*moves != c.expectation){
return std::string(c.name) + ": " + describe(*moves) + ", expected " + describe(c.expectation) + ".";

Check warning on line 64 in SerialPrograms/Source/NintendoSwitch/Programs/MenuCursor/NintendoSwitch_MenuCursor_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

style: build strings with std::format, not + on a literal / std::to_string / std::string (fork AGENTS.md "Strings")
}
}
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) +

Check warning on line 75 in SerialPrograms/Source/NintendoSwitch/Programs/MenuCursor/NintendoSwitch_MenuCursor_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

style: build strings with std::format, not + on a literal / std::to_string / std::string (fork AGENTS.md "Strings")
" from " + std::to_string(from) + " to " + std::to_string(to);

Check warning on line 76 in SerialPrograms/Source/NintendoSwitch/Programs/MenuCursor/NintendoSwitch_MenuCursor_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

style: build strings with std::format, not + on a literal / std::to_string / std::string (fork AGENTS.md "Strings")
const std::optional<std::vector<DpadPosition>> moves = menu_cursor_moves(grid, from, to);
if (!moves){
return pair + ": no moves.";

Check warning on line 79 in SerialPrograms/Source/NintendoSwitch/Programs/MenuCursor/NintendoSwitch_MenuCursor_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

style: build strings with std::format, not + on a literal / std::to_string / std::string (fork AGENTS.md "Strings")
}
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.";

Check warning on line 88 in SerialPrograms/Source/NintendoSwitch/Programs/MenuCursor/NintendoSwitch_MenuCursor_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

style: build strings with std::format, not + on a literal / std::to_string / std::string (fork AGENTS.md "Strings")
}
}
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<Test_MenuCursor>(item);
}
}



} // namespace NintendoSwitch
} // namespace PokemonAutomation
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions SerialPrograms/cmake/SourceFiles.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading