Skip to content

Commit 0215f32

Browse files
committed
Refactor for macro API.
1 parent eb53c37 commit 0215f32

File tree

11 files changed

+146
-54
lines changed

11 files changed

+146
-54
lines changed

Common/Cpp/Options/EnumDropdownDatabase.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,7 @@ IntegerEnumDropdownDatabase::IntegerEnumDropdownDatabase()
100100
IntegerEnumDropdownDatabase::IntegerEnumDropdownDatabase(std::initializer_list<EnumEntry> list)
101101
: m_core(CONSTRUCT_TOKEN)
102102
{
103-
size_t index = 0;
104-
for (auto iter = list.begin(); iter != list.end(); ++iter, index++){
103+
for (auto iter = list.begin(); iter != list.end(); ++iter){
105104
add(iter->enum_value, std::move(iter->slug), std::move(iter->display), iter->enabled);
106105
}
107106
}

Common/Cpp/Options/EnumDropdownDatabase.h

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ class IntegerEnumDropdownDatabase{
4747
IntegerEnumDropdownDatabase(); // Constructs empty database.
4848
IntegerEnumDropdownDatabase(std::initializer_list<EnumEntry> list);
4949

50+
// Warning, these functions do not have strong exception safety!
51+
// If these throw, this class will be in a bad state.
5052
void add(EnumEntry entry);
5153
void add(size_t value, std::string slug, std::string display, bool enabled = true){
5254
add(EnumEntry{value, std::move(slug), std::move(display), enabled});
@@ -92,12 +94,31 @@ class EnumDropdownDatabase : public IntegerEnumDropdownDatabase{
9294
EnumDropdownDatabase(std::initializer_list<Entry> list){
9395
size_t index = 0;
9496
for (auto iter = list.begin(); iter != list.end(); ++iter, index++){
95-
add(iter->value, std::move(iter->slug), std::move(iter->display), iter->enabled);
97+
add(
98+
iter->value,
99+
std::move(iter->slug),
100+
std::move(iter->display),
101+
iter->enabled
102+
);
96103
}
97104
}
98105

99-
void add(EnumType value, std::string slug, std::string display, bool enabled){
100-
IntegerEnumDropdownDatabase::add(EnumEntry{(size_t)value, std::move(slug), std::move(display), enabled});
106+
// Warning, these functions do not have strong exception safety!
107+
// If these throw, this class will be in a bad state.
108+
void add(
109+
EnumType value,
110+
std::string slug,
111+
std::string display,
112+
bool enabled
113+
){
114+
IntegerEnumDropdownDatabase::add(
115+
EnumEntry{
116+
(size_t)value,
117+
std::move(slug),
118+
std::move(display),
119+
enabled
120+
}
121+
);
101122
}
102123

103124
// Find an enum. Returns null if not in the database.

SerialPrograms/Source/CommonFramework/Globals.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace PokemonAutomation{
2626
const bool IS_BETA_VERSION = true;
2727
const int PROGRAM_VERSION_MAJOR = 0;
2828
const int PROGRAM_VERSION_MINOR = 60;
29-
const int PROGRAM_VERSION_PATCH = 2;
29+
const int PROGRAM_VERSION_PATCH = 3;
3030

3131
const std::string PROGRAM_VERSION_BASE =
3232
"v" + std::to_string(PROGRAM_VERSION_MAJOR) +

SerialPrograms/Source/Controllers/ControllerState.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*
55
*/
66

7-
#include "Common/Cpp/Json/JsonValue.h"
7+
#include "Common/Cpp/Json/JsonObject.h"
88
#include "CommonFramework/Exceptions/OperationFailedException.h"
99
#include "ControllerState.h"
1010

@@ -13,13 +13,13 @@ namespace PokemonAutomation{
1313

1414

1515

16-
void ControllerState::load_json(const JsonValue& json){
16+
void ControllerState::load_json(const JsonObject& json){
1717
throw OperationFailedException(
1818
ErrorReport::NO_ERROR_REPORT,
1919
"This controller does not support serialization."
2020
);
2121
}
22-
JsonValue ControllerState::to_json() const{
22+
JsonObject ControllerState::to_json() const{
2323
throw OperationFailedException(
2424
ErrorReport::NO_ERROR_REPORT,
2525
"This controller does not support serialization."

SerialPrograms/Source/Controllers/ControllerState.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace PokemonAutomation{
1313

14-
class JsonValue;
14+
class JsonObject;
1515
class CancellableScope;
1616
class AbstractController;
1717

@@ -33,8 +33,8 @@ class ControllerState{
3333

3434
public:
3535
// Serialization
36-
virtual void load_json(const JsonValue& json);
37-
virtual JsonValue to_json() const;
36+
virtual void load_json(const JsonObject& json);
37+
virtual JsonObject to_json() const;
3838

3939
public:
4040
// Execution

SerialPrograms/Source/NintendoSwitch/Controllers/NintendoSwitch_ControllerButtons.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ enum Button : ButtonFlagType{
5050
BUTTON_RIGHT_SR = ((uint32_t)1 << 23),
5151
BUTTON_C = ((uint32_t)1 << 24),
5252
};
53+
54+
inline constexpr Button empty_value(const Button*){
55+
return BUTTON_NONE;
56+
}
57+
inline constexpr bool is_empty(Button x){
58+
return x == BUTTON_NONE;
59+
}
5360
inline constexpr Button operator|(Button x, Button y){
5461
return (Button)((ButtonFlagType)x | (ButtonFlagType)y);
5562
}
@@ -62,6 +69,12 @@ inline constexpr Button operator&(Button x, Button y){
6269
inline constexpr void operator&=(Button& x, Button y){
6370
x = (Button)((ButtonFlagType)x & (ButtonFlagType)y);
6471
}
72+
inline constexpr Button operator^(Button x, Button y){
73+
return (Button)((ButtonFlagType)x ^ (ButtonFlagType)y);
74+
}
75+
inline constexpr void operator^=(Button& x, Button y){
76+
x = (Button)((ButtonFlagType)x ^ (ButtonFlagType)y);
77+
}
6578

6679
std::string button_to_string(Button button);
6780
std::string button_to_code_string(Button button);

SerialPrograms/Source/NintendoSwitch/Controllers/NintendoSwitch_JoyconState.cpp

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
*/
66

77
#include "Common/Cpp/Json/JsonObject.h"
8-
#include "NintendoSwitch_JoyconState.h"
98
#include "NintendoSwitch_Joycon.h"
9+
#include "NintendoSwitch_JoyconState.h"
1010

1111
namespace PokemonAutomation{
1212
namespace NintendoSwitch{
@@ -43,34 +43,28 @@ bool JoyconState::is_neutral() const{
4343
&& joystick_y == 128;
4444
}
4545

46-
void JoyconState::load_json(const JsonValue& json){
46+
void JoyconState::load_json(const JsonObject& json){
4747
clear();
4848

49-
if (json.is_null()){
50-
return;
51-
}
52-
53-
const JsonObject& obj = json.to_object_throw();
54-
5549
// Backwards compatibility.
56-
if (obj.get_boolean_default("is_neutral", false)){
50+
if (json.get_boolean_default("is_neutral", false)){
5751
return;
5852
}
5953

6054
{
6155
std::string buttons_string;
62-
obj.read_string(buttons_string, "buttons");
56+
json.read_string(buttons_string, "buttons");
6357
buttons = string_to_button(buttons_string);
6458
}
6559

6660
// Backwards compatibility.
67-
obj.read_integer(joystick_x, "joystick_x", 0, 255);
68-
obj.read_integer(joystick_y, "joystick_y", 0, 255);
61+
json.read_integer(joystick_x, "joystick_x", 0, 255);
62+
json.read_integer(joystick_y, "joystick_y", 0, 255);
6963

70-
obj.read_integer(joystick_x, "jx", 0, 255);
71-
obj.read_integer(joystick_y, "jy", 0, 255);
64+
json.read_integer(joystick_x, "jx", 0, 255);
65+
json.read_integer(joystick_y, "jy", 0, 255);
7266
}
73-
JsonValue JoyconState::to_json() const{
67+
JsonObject JoyconState::to_json() const{
7468
JsonObject obj;
7569
if (buttons != BUTTON_NONE){
7670
obj["buttons"] = button_to_string(buttons);

SerialPrograms/Source/NintendoSwitch/Controllers/NintendoSwitch_JoyconState.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ class JoyconState : public ControllerState{
2727
virtual bool operator==(const ControllerState& x) const override;
2828
virtual bool is_neutral() const override;
2929

30-
virtual void load_json(const JsonValue& json) override;
31-
virtual JsonValue to_json() const override;
30+
virtual void load_json(const JsonObject& json) override;
31+
virtual JsonObject to_json() const override;
3232

3333
virtual void execute(
3434
CancellableScope& scope,

SerialPrograms/Source/NintendoSwitch/Controllers/NintendoSwitch_ProControllerState.cpp

Lines changed: 86 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
*/
66

77
#include "Common/Cpp/Json/JsonObject.h"
8-
#include "NintendoSwitch_ProControllerState.h"
8+
#include "Common/Cpp/Options/SimpleIntegerOption.h"
9+
#include "Common/Cpp/Options/EnumDropdownOption.h"
10+
#include "Controllers/ControllerStateTable.h"
911
#include "NintendoSwitch_ProController.h"
12+
#include "NintendoSwitch_ProControllerState.h"
1013

1114
namespace PokemonAutomation{
1215
namespace NintendoSwitch{
@@ -58,45 +61,39 @@ bool ProControllerState::is_neutral() const{
5861
}
5962

6063

61-
void ProControllerState::load_json(const JsonValue& json){
64+
void ProControllerState::load_json(const JsonObject& json){
6265
clear();
6366

64-
if (json.is_null()){
65-
return;
66-
}
67-
68-
const JsonObject& obj = json.to_object_throw();
69-
7067
// Backwards compatibility.
71-
if (obj.get_boolean_default("is_neutral", false)){
68+
if (json.get_boolean_default("is_neutral", false)){
7269
return;
7370
}
7471

7572
{
7673
std::string buttons_string;
77-
if (obj.read_string(buttons_string, "buttons")){
74+
if (json.read_string(buttons_string, "buttons")){
7875
buttons = string_to_button(buttons_string);
7976
}
8077
}
8178
{
8279
std::string dpad_string;
83-
if (obj.read_string(dpad_string, "dpad")){
80+
if (json.read_string(dpad_string, "dpad")){
8481
dpad = string_to_dpad(dpad_string);
8582
}
8683
}
8784

8885
// Backwards compatibility.
89-
obj.read_integer(left_x, "left_x", 0, 255);
90-
obj.read_integer(left_y, "left_y", 0, 255);
91-
obj.read_integer(right_x, "right_x", 0, 255);
92-
obj.read_integer(right_y, "right_y", 0, 255);
93-
94-
obj.read_integer(left_x, "lx", 0, 255);
95-
obj.read_integer(left_y, "ly", 0, 255);
96-
obj.read_integer(right_x, "rx", 0, 255);
97-
obj.read_integer(right_y, "ry", 0, 255);
86+
json.read_integer(left_x, "left_x", 0, 255);
87+
json.read_integer(left_y, "left_y", 0, 255);
88+
json.read_integer(right_x, "right_x", 0, 255);
89+
json.read_integer(right_y, "right_y", 0, 255);
90+
91+
json.read_integer(left_x, "lx", 0, 255);
92+
json.read_integer(left_y, "ly", 0, 255);
93+
json.read_integer(right_x, "rx", 0, 255);
94+
json.read_integer(right_y, "ry", 0, 255);
9895
}
99-
JsonValue ProControllerState::to_json() const{
96+
JsonObject ProControllerState::to_json() const{
10097
JsonObject obj;
10198
if (buttons != BUTTON_NONE){
10299
obj["buttons"] = button_to_string(buttons);
@@ -194,6 +191,74 @@ std::string ProControllerState::to_cpp(Milliseconds hold, Milliseconds release)
194191

195192

196193

194+
#if 0
195+
196+
197+
const EnumDropdownDatabase<DpadPosition>& DPAD_DATABASE(){
198+
static EnumDropdownDatabase<DpadPosition> database{
199+
{DpadPosition::DPAD_NONE, "none", "---"},
200+
{DpadPosition::DPAD_UP, "up", "Up"},
201+
{DpadPosition::DPAD_UP_RIGHT, "up-right", "Up+Right"},
202+
{DpadPosition::DPAD_RIGHT, "right", "Right"},
203+
{DpadPosition::DPAD_DOWN_RIGHT, "down-right", "Down+Right"},
204+
{DpadPosition::DPAD_DOWN, "down", "Down"},
205+
{DpadPosition::DPAD_DOWN_LEFT, "down-left", "Down+Left"},
206+
{DpadPosition::DPAD_LEFT, "left", "Left"},
207+
{DpadPosition::DPAD_UP_LEFT, "up-left", "Up+Left"},
208+
};
209+
return database;
210+
}
211+
212+
213+
214+
class ProControllerStateRow : public ControllerStateRow{
215+
public:
216+
ProControllerStateRow(EditableTableOption& parent_table)
217+
: ControllerStateRow(parent_table)
218+
, DPAD(
219+
DPAD_DATABASE(),
220+
LockMode::UNLOCK_WHILE_RUNNING,
221+
DpadPosition::DPAD_NONE
222+
)
223+
, LEFT_JOYSTICK(LockMode::UNLOCK_WHILE_RUNNING, 128, 0, 255)
224+
{
225+
PA_ADD_OPTION(DPAD);
226+
PA_ADD_OPTION(LEFT_JOYSTICK);
227+
}
228+
229+
virtual std::unique_ptr<EditableTableRow> clone() const override{
230+
std::unique_ptr<ProControllerStateRow> ret(new ProControllerStateRow(parent()));
231+
ret->m_state = m_state;
232+
return ret;
233+
}
234+
235+
virtual void load_json(const JsonValue& json) override{
236+
const JsonObject& obj = json.to_object_throw();
237+
m_milliseconds = Milliseconds(obj.get_integer_throw("duration_in_ms"));
238+
m_state.load_json(obj);
239+
}
240+
virtual JsonValue to_json() const override{
241+
JsonObject json = m_state.to_json();
242+
json["duration_in_ms"] = m_milliseconds.count();
243+
return json;
244+
}
245+
246+
virtual const ControllerState& get_state() const override{
247+
return m_state;
248+
}
249+
250+
private:
251+
ProControllerState m_state;
252+
253+
EnumDropdownCell<DpadPosition> DPAD;
254+
SimpleIntegerCell<uint8_t> LEFT_JOYSTICK;
255+
};
256+
257+
258+
259+
#endif
260+
261+
197262

198263

199264

SerialPrograms/Source/NintendoSwitch/Controllers/NintendoSwitch_ProControllerState.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ class ProControllerState : public ControllerState{
2828
virtual bool operator==(const ControllerState& x) const override;
2929
virtual bool is_neutral() const override;
3030

31-
virtual void load_json(const JsonValue& json) override;
32-
virtual JsonValue to_json() const override;
31+
virtual void load_json(const JsonObject& json) override;
32+
virtual JsonObject to_json() const override;
3333

3434
virtual void execute(
3535
CancellableScope& scope,

0 commit comments

Comments
 (0)