@@ -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}
106108JsonValue 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}
117126void 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}
325334JsonValue 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}
333345void 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