Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
41 changes: 41 additions & 0 deletions SerialPrograms/Source/Controllers/ControllerStateTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "ControllerTypeStrings.h"
#include "ControllerStateTable.h"

#include "NintendoSwitch/Controllers/NintendoSwitch_ControllerButtons.h"
#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProControllerTable.h"
#include "NintendoSwitch/Controllers/Joycon/NintendoSwitch_JoyconTable.h"

Expand Down Expand Up @@ -367,6 +368,46 @@ EnumDropdownDatabase<ControllerClass> ControllerCommandTables::make_database(



std::string get_joystick_direction(uint8_t x, uint8_t y){
std::string direction = "";
if (x > NintendoSwitch::STICK_CENTER){
if (y > NintendoSwitch::STICK_CENTER){
// right-down
direction = "\u2198";
}else if (y == NintendoSwitch::STICK_CENTER){
// right
direction = "\u2192";
}else{ // y < STICK_CENTER
// right-up
direction = "\u2197";
}
}else if (x == NintendoSwitch::STICK_CENTER){
if (y > NintendoSwitch::STICK_CENTER){
// down
direction = "\u2193";
}else if (y == NintendoSwitch::STICK_CENTER){
// neutral
direction = "neutral";
}else{ // y < STICK_CENTER
// up
direction = "\u2191";
}

}else { // x < STICK_CENTER
if (y > NintendoSwitch::STICK_CENTER){
// left-down
direction = "\u2199";
}else if (y == NintendoSwitch::STICK_CENTER){
// left
direction = "\u2190";
}else{ // y < STICK_CENTER
// left-up
direction = "\u2196";
}
}

return direction;
}



Expand Down
2 changes: 1 addition & 1 deletion SerialPrograms/Source/Controllers/ControllerStateTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class ControllerCommandTables : public GroupOption, public ConfigOption::Listene
};



std::string get_joystick_direction(uint8_t x, uint8_t y);



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ void register_joycon_tables(){
"Buttons",
"Joystick (X)",
"Joystick (Y)",
"Action",
}
);
ControllerCommandTable::register_controller_type(
Expand All @@ -66,11 +67,18 @@ void register_joycon_tables(){
"Buttons",
"Joystick (X)",
"Joystick (Y)",
"Action",
}
);
}


JoyconStateRow::~JoyconStateRow(){
DURATION.remove_listener(*this);
BUTTONS.remove_listener(*this);
JOYSTICK_X.remove_listener(*this);
JOYSTICK_Y.remove_listener(*this);
}



Expand All @@ -87,11 +95,19 @@ JoyconStateRow::JoyconStateRow(EditableTableOption& parent_table)
)
, JOYSTICK_X(LockMode::UNLOCK_WHILE_RUNNING, 128, 0, 255)
, JOYSTICK_Y(LockMode::UNLOCK_WHILE_RUNNING, 128, 0, 255)
, ACTION(false, LockMode::UNLOCK_WHILE_RUNNING, "", "")
{
PA_ADD_OPTION(DURATION);
PA_ADD_OPTION(BUTTONS);
PA_ADD_OPTION(JOYSTICK_X);
PA_ADD_OPTION(JOYSTICK_Y);
PA_ADD_OPTION(ACTION);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would actually put this between DURATION and BUTTONS. IOW, put it near the beginning.


JoyconStateRow::on_config_value_changed(this);
DURATION.add_listener(*this);
BUTTONS.add_listener(*this);
JOYSTICK_X.add_listener(*this);
JOYSTICK_Y.add_listener(*this);
}

std::unique_ptr<EditableTableRow> JoyconStateRow::clone() const{
Expand Down Expand Up @@ -145,7 +161,37 @@ std::unique_ptr<ControllerState> JoyconStateRow::get_state(Milliseconds& duratio



void JoyconStateRow::on_config_value_changed(void* object){
JoyconState state;
get_state(state);

ACTION.set(get_controller_action(state));
}



std::string get_controller_action(JoyconState& state){
std::string action = "";

if (state.buttons != BUTTON_NONE){
action += "Button";
}

if (state.joystick_x != STICK_CENTER || state.joystick_y != STICK_CENTER){
if (action != ""){
action += ", ";
}
action += "Joystick";
action += " " + get_joystick_direction(state.joystick_x, state.joystick_y);

}

if (action == ""){
return "Wait";
}

return action;
}



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "Common/Cpp/Options/SimpleIntegerOption.h"
#include "Common/Cpp/Options/TimeDurationOption.h"
#include "Common/Cpp/Options/CheckboxDropdownOption.h"
#include "Common/Cpp/Options/StringOption.h"
#include "Controllers/ControllerStateTable.h"
#include "NintendoSwitch_JoyconState.h"

Expand All @@ -19,8 +20,9 @@ namespace NintendoSwitch{



class JoyconStateRow : public ControllerStateRow{
class JoyconStateRow : public ControllerStateRow, public ConfigOption::Listener{
public:
~JoyconStateRow();
JoyconStateRow(EditableTableOption& parent_table);

virtual std::unique_ptr<EditableTableRow> clone() const override;
Expand All @@ -31,16 +33,23 @@ class JoyconStateRow : public ControllerStateRow{
void get_state(JoyconState& state) const;
virtual std::unique_ptr<ControllerState> get_state(Milliseconds& duration) const override;

private:
virtual void on_config_value_changed(void* object) override;


private:
MillisecondsCell DURATION;
CheckboxDropdownCell<Button> BUTTONS;
SimpleIntegerCell<uint8_t> JOYSTICK_X;
SimpleIntegerCell<uint8_t> JOYSTICK_Y;
StringCell ACTION;
};


void register_joycon_tables();

std::string get_controller_action(JoyconState& state);



}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,20 @@ void register_procon_tables(){
"Left JS (Y)",
"Right JS (X)",
"Right JS (Y)",
"Action",
}
);
}


ProControllerStateRow::~ProControllerStateRow(){
DURATION.remove_listener(*this);
BUTTONS.remove_listener(*this);
DPAD.remove_listener(*this);
LEFT_JOYSTICK_X.remove_listener(*this);
LEFT_JOYSTICK_Y.remove_listener(*this);
RIGHT_JOYSTICK_X.remove_listener(*this);
RIGHT_JOYSTICK_Y.remove_listener(*this);
}

ProControllerStateRow::ProControllerStateRow(EditableTableOption& parent_table)
: ControllerStateRow(parent_table)
Expand All @@ -90,6 +99,7 @@ ProControllerStateRow::ProControllerStateRow(EditableTableOption& parent_table)
, LEFT_JOYSTICK_Y(LockMode::UNLOCK_WHILE_RUNNING, 128, 0, 255)
, RIGHT_JOYSTICK_X(LockMode::UNLOCK_WHILE_RUNNING, 128, 0, 255)
, RIGHT_JOYSTICK_Y(LockMode::UNLOCK_WHILE_RUNNING, 128, 0, 255)
, ACTION(false, LockMode::UNLOCK_WHILE_RUNNING, "", "")
{
PA_ADD_OPTION(DURATION);
PA_ADD_OPTION(BUTTONS);
Expand All @@ -98,6 +108,18 @@ ProControllerStateRow::ProControllerStateRow(EditableTableOption& parent_table)
PA_ADD_OPTION(LEFT_JOYSTICK_Y);
PA_ADD_OPTION(RIGHT_JOYSTICK_X);
PA_ADD_OPTION(RIGHT_JOYSTICK_Y);
PA_ADD_OPTION(ACTION);

ProControllerStateRow::on_config_value_changed(this);
DURATION.add_listener(*this);
BUTTONS.add_listener(*this);
DPAD.add_listener(*this);
LEFT_JOYSTICK_X.add_listener(*this);
LEFT_JOYSTICK_Y.add_listener(*this);
RIGHT_JOYSTICK_X.add_listener(*this);
RIGHT_JOYSTICK_Y.add_listener(*this);

// ACTION.set_visibility(ConfigOptionState::DISABLED);
}

std::unique_ptr<EditableTableRow> ProControllerStateRow::clone() const{
Expand Down Expand Up @@ -158,6 +180,48 @@ std::unique_ptr<ControllerState> ProControllerStateRow::get_state(Milliseconds&
return ret;
}

void ProControllerStateRow::on_config_value_changed(void* object){
ProControllerState state;
get_state(state);

ACTION.set(get_controller_action(state));
}


std::string get_controller_action(ProControllerState& state){
std::string action = "";

if (state.buttons != BUTTON_NONE){
action += "Button";
}
if (state.dpad != DPAD_NONE){
if (action != ""){
action += ", ";
}
action += "Dpad";
}
if (state.left_x != STICK_CENTER || state.left_y != STICK_CENTER){
if (action != ""){
action += ", ";
}
action += "L-stick";
action += " " + get_joystick_direction(state.left_x, state.left_y);

}
if (state.right_x != STICK_CENTER || state.right_y != STICK_CENTER){
if (action != ""){
action += ", ";
}
action += "R-stick";
action += " " + get_joystick_direction(state.right_x, state.right_y);
}

if (action == ""){
return "Wait";
}

return action;
}



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "Common/Cpp/Options/EnumDropdownOption.h"
#include "Common/Cpp/Options/TimeDurationOption.h"
#include "Common/Cpp/Options/CheckboxDropdownOption.h"
#include "Common/Cpp/Options/StringOption.h"
#include "Controllers/ControllerStateTable.h"
#include "NintendoSwitch_ProControllerState.h"

Expand All @@ -19,8 +20,9 @@ namespace NintendoSwitch{



class ProControllerStateRow : public ControllerStateRow{
class ProControllerStateRow : public ControllerStateRow, public ConfigOption::Listener{
public:
~ProControllerStateRow();
ProControllerStateRow(EditableTableOption& parent_table);

virtual std::unique_ptr<EditableTableRow> clone() const override;
Expand All @@ -31,6 +33,9 @@ class ProControllerStateRow : public ControllerStateRow{
void get_state(ProControllerState& state) const;
virtual std::unique_ptr<ControllerState> get_state(Milliseconds& duration) const override;

private:
virtual void on_config_value_changed(void* object) override;


private:
MillisecondsCell DURATION;
Expand All @@ -40,11 +45,13 @@ class ProControllerStateRow : public ControllerStateRow{
SimpleIntegerCell<uint8_t> LEFT_JOYSTICK_Y;
SimpleIntegerCell<uint8_t> RIGHT_JOYSTICK_X;
SimpleIntegerCell<uint8_t> RIGHT_JOYSTICK_Y;
StringCell ACTION;
};


void register_procon_tables();

std::string get_controller_action(ProControllerState& state);


}
Expand Down