Skip to content

Commit ae03749

Browse files
committed
More keyboard recording refactor.
1 parent 34a7c5c commit ae03749

File tree

4 files changed

+78
-389
lines changed

4 files changed

+78
-389
lines changed

SerialPrograms/Source/NintendoSwitch/Controllers/NintendoSwitch_ControllerButtons.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
#include "Common/Cpp/EnumStringMap.h"
99
#include "Common/Cpp/StringTools.h"
1010

11+
//#include <iostream>
12+
//using std::cout;
13+
//using std::endl;
14+
1115
namespace PokemonAutomation{
1216
namespace NintendoSwitch{
1317

@@ -91,6 +95,9 @@ Button string_to_button(std::string multi_button_string){
9195
std::vector<std::string> string_vector = StringTools::split(multi_button_string, " ");
9296
Button button_result = BUTTON_NONE;
9397
for (const std::string& button_string : string_vector){
98+
if (button_string.empty()){
99+
continue;
100+
}
94101
Button one_button = BUTTON_STRINGS.get_enum(button_string);
95102
button_result |= one_button;
96103
}

SerialPrograms/Source/NintendoSwitch/Controllers/NintendoSwitch_VirtualControllerState.cpp

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,15 @@ void ProControllerState::load_json(const JsonValue& json){
8383

8484
{
8585
std::string buttons_string;
86-
obj.read_string(buttons_string, "buttons");
87-
buttons = string_to_button(buttons_string);
86+
if (obj.read_string(buttons_string, "buttons")){
87+
buttons = string_to_button(buttons_string);
88+
}
8889
}
8990
{
9091
std::string dpad_string;
91-
obj.read_string(dpad_string, "dpad");
92-
dpad = string_to_dpad(dpad_string);
92+
if (obj.read_string(dpad_string, "dpad")){
93+
dpad = string_to_dpad(dpad_string);
94+
}
9395
}
9496

9597
// Backwards compatibility.
@@ -105,13 +107,20 @@ void ProControllerState::load_json(const JsonValue& json){
105107
}
106108
JsonValue ProControllerState::to_json() const{
107109
JsonObject obj;
108-
obj["is_neutral"] = is_neutral();
109-
obj["buttons"] = button_to_string(buttons);
110-
obj["dpad"] = dpad_to_string(dpad);
111-
obj["left_x"] = left_x;
112-
obj["left_y"] = left_y;
113-
obj["right_x"] = right_x;
114-
obj["right_y"] = right_y;
110+
if (buttons != BUTTON_NONE){
111+
obj["buttons"] = button_to_string(buttons);
112+
}
113+
if (dpad != DPAD_NONE){
114+
obj["dpad"] = dpad_to_string(dpad);
115+
}
116+
if (left_x != STICK_CENTER || left_y != STICK_CENTER){
117+
obj["lx"] = left_x;
118+
obj["ly"] = left_y;
119+
}
120+
if (right_x != STICK_CENTER || right_y != STICK_CENTER){
121+
obj["rx"] = right_x;
122+
obj["ry"] = right_y;
123+
}
115124
return obj;
116125
}
117126
void ProControllerState::execute(AbstractControllerContext& context, Milliseconds duration) const{
@@ -149,7 +158,7 @@ std::string ProControllerState::to_cpp(Milliseconds hold, Milliseconds release)
149158
}while (false);
150159

151160
if (non_neutral_fields == 0){
152-
return "pbf_wait(context, " + std::to_string((hold + release).count()) + "ms);";
161+
return "pbf_wait(context, " + std::to_string((hold + release).count()) + "ms);\n";
153162
}
154163

155164
std::string hold_str = std::to_string(hold.count()) + "ms";
@@ -324,10 +333,13 @@ void JoyconState::load_json(const JsonValue& json){
324333
}
325334
JsonValue JoyconState::to_json() const{
326335
JsonObject obj;
327-
obj["is_neutral"] = is_neutral();
328-
obj["buttons"] = button_to_string(buttons);
329-
obj["joystick_x"] = joystick_x;
330-
obj["joystick_y"] = joystick_y;
336+
if (buttons != BUTTON_NONE){
337+
obj["buttons"] = button_to_string(buttons);
338+
}
339+
if (joystick_x != STICK_CENTER || joystick_y != STICK_CENTER){
340+
obj["jx"] = joystick_x;
341+
obj["jy"] = joystick_y;
342+
}
331343
return obj;
332344
}
333345
void JoyconState::execute(AbstractControllerContext& context, Milliseconds duration) const{
@@ -355,7 +367,7 @@ std::string JoyconState::to_cpp(Milliseconds hold, Milliseconds release) const{
355367
}while (false);
356368

357369
if (non_neutral_fields == 0){
358-
return "pbf_wait(context, " + std::to_string((hold + release).count()) + ");";
370+
return "pbf_wait(context, " + std::to_string((hold + release).count()) + ");\n";
359371
}
360372

361373
std::string hold_str = std::to_string(hold.count()) + "ms";

0 commit comments

Comments
 (0)