44 *
55 */
66
7+ #include " Common/Cpp/PrettyPrint.h"
78#include " Common/Cpp/Json/JsonObject.h"
9+ #include " Controllers/JoystickTools.h"
810#include " NintendoSwitch_Joycon.h"
911#include " NintendoSwitch_JoyconState.h"
1012
@@ -17,8 +19,7 @@ namespace NintendoSwitch{
1719
1820void JoyconState::clear (){
1921 buttons = BUTTON_NONE;
20- joystick_x = 128 ;
21- joystick_y = 128 ;
22+ joystick = JoystickPosition ();
2223}
2324bool JoyconState::operator ==(const ControllerState& x) const {
2425 if (typeid (*this ) != typeid (x)){
@@ -30,18 +31,13 @@ bool JoyconState::operator==(const ControllerState& x) const{
3031 if (buttons != r.buttons ){
3132 return false ;
3233 }
33- if (joystick_x != r.joystick_x ){
34- return false ;
35- }
36- if (joystick_y != r.joystick_y ){
34+ if (joystick != r.joystick ){
3735 return false ;
3836 }
3937 return true ;
4038}
4139bool JoyconState::is_neutral () const {
42- return buttons == 0
43- && joystick_x == 128
44- && joystick_y == 128 ;
40+ return buttons == 0 && joystick.is_neutral ();
4541}
4642
4743void JoyconState::load_json (const JsonObject& json){
@@ -58,35 +54,48 @@ void JoyconState::load_json(const JsonObject& json){
5854 buttons = string_to_button (buttons_string);
5955 }
6056
57+
6158 // Backwards compatibility.
62- json.read_integer (joystick_x, " joystick_x" , 0 , 255 );
63- json.read_integer (joystick_y, " joystick_y" , 0 , 255 );
59+ {
60+ uint8_t joystick_x = 128 ;
61+ uint8_t joystick_y = 128 ;
62+
63+ json.read_integer (joystick_x, " joystick_x" , 0 , 255 );
64+ json.read_integer (joystick_y, " joystick_y" , 0 , 255 );
65+
66+ json.read_integer (joystick_x, " jx" , 0 , 255 );
67+ json.read_integer (joystick_y, " jy" , 0 , 255 );
68+
69+ joystick.x = JoystickTools::linear_u8_to_float (joystick_x);
70+ joystick.y = -JoystickTools::linear_u8_to_float (joystick_y);
71+ }
6472
65- json.read_integer (joystick_x , " jx " , 0 , 255 );
66- json.read_integer (joystick_y , " jy " , 0 , 255 );
73+ json.read_float (joystick. x , " jxf " );
74+ json.read_float (joystick. y , " jyf " );
6775}
6876JsonObject JoyconState::to_json () const {
6977 JsonObject obj;
7078 if (buttons != BUTTON_NONE){
7179 obj[" buttons" ] = button_to_string (buttons);
7280 }
73- if (joystick_x != STICK_CENTER || joystick_y != STICK_CENTER ){
74- obj[" jx " ] = joystick_x ;
75- obj[" jy " ] = joystick_y ;
81+ if (!joystick. is_neutral () ){
82+ obj[" jxf " ] = joystick. x ;
83+ obj[" jyf " ] = joystick. y ;
7684 }
7785 return obj;
7886}
7987void JoyconState::execute (
8088 Cancellable* scope,
89+ bool enable_logging,
8190 AbstractController& controller,
8291 Milliseconds duration
8392) const {
8493 controller.cast_with_exception <JoyconController>().issue_full_controller_state (
8594 scope,
86- true ,
95+ enable_logging ,
8796 duration,
8897 buttons,
89- joystick_x, joystick_y
98+ joystick
9099 );
91100}
92101std::string JoyconState::to_cpp (Milliseconds hold, Milliseconds release) const {
@@ -97,7 +106,7 @@ std::string JoyconState::to_cpp(Milliseconds hold, Milliseconds release) const{
97106 non_neutral_field = 0 ;
98107 non_neutral_fields++;
99108 }
100- if (joystick_x != STICK_CENTER || joystick_y != STICK_CENTER ){
109+ if (!joystick. is_neutral () ){
101110 non_neutral_field = 1 ;
102111 non_neutral_fields++;
103112 }
@@ -114,7 +123,7 @@ std::string JoyconState::to_cpp(Milliseconds hold, Milliseconds release) const{
114123 std::string ret;
115124 ret += " pbf_controller_state(context, "
116125 + button_to_code_string (buttons) + " , "
117- + std::to_string (joystick_x ) + " , " + std::to_string (joystick_y ) + " , "
126+ + tostr_fixed (joystick. x , 3 ) + " , " + tostr_fixed (joystick. y , 3 ) + " , "
118127 + hold_str +" );\n " ;
119128 if (release != Milliseconds (0 )){
120129 ret += " pbf_wait(context, " + release_str + " );\n " ;
@@ -128,7 +137,7 @@ std::string JoyconState::to_cpp(Milliseconds hold, Milliseconds release) const{
128137 + hold_str + " , " + release_str + " );\n " ;
129138 case 1 :
130139 return " pbf_move_joystick(context, "
131- + std::to_string (joystick_x ) + " , " + std::to_string (joystick_y ) + " , "
140+ + tostr_fixed (joystick. x , 3 ) + " , " + tostr_fixed (joystick. y , 3 ) + " , "
132141 + hold_str + " , " + release_str + " );\n " ;
133142 }
134143 throw InternalProgramError (nullptr , PA_CURRENT_FUNCTION, " Impossible state." );
0 commit comments