Skip to content

Commit 8165f57

Browse files
committed
Refactor VirtualController and keyboard mappings. At UI for editing keyboard mapping table.
1 parent 51f18f0 commit 8165f57

15 files changed

+538
-104
lines changed

Common/Qt/Options/EditableTableWidget.cpp

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <QScrollBar>
1111
#include <QLabel>
1212
#include <QPushButton>
13+
#include <QMessageBox>
1314
#include "Common/Cpp/Json/JsonValue.h"
1415
#include "Common/Qt/AutoHeightTable.h"
1516
#include "EditableTableWidget.h"
@@ -94,10 +95,10 @@ EditableTableWidget::EditableTableWidget(QWidget& parent, EditableTableOption& v
9495
QHBoxLayout* buttons = new QHBoxLayout();
9596
layout->addLayout(buttons);
9697
{
97-
QPushButton* load_button = new QPushButton("Load Table", this);
98-
buttons->addWidget(load_button, 1);
98+
QPushButton* button = new QPushButton("Load Table", this);
99+
buttons->addWidget(button, 1);
99100
connect(
100-
load_button, &QPushButton::clicked,
101+
button, &QPushButton::clicked,
101102
this, [this, &value](bool){
102103
std::string path = QFileDialog::getOpenFileName(
103104
this,
@@ -111,10 +112,10 @@ EditableTableWidget::EditableTableWidget(QWidget& parent, EditableTableOption& v
111112
);
112113
}
113114
{
114-
QPushButton* save_button = new QPushButton("Save Table", this);
115-
buttons->addWidget(save_button, 1);
115+
QPushButton* button = new QPushButton("Save Table", this);
116+
buttons->addWidget(button, 1);
116117
connect(
117-
save_button, &QPushButton::clicked,
118+
button, &QPushButton::clicked,
118119
this, [this, &value](bool){
119120
std::string path = QFileDialog::getSaveFileName(
120121
this,
@@ -128,6 +129,24 @@ EditableTableWidget::EditableTableWidget(QWidget& parent, EditableTableOption& v
128129
}
129130
);
130131
}
132+
{
133+
QPushButton* button = new QPushButton("Restore Defaults", this);
134+
buttons->addWidget(button, 1);
135+
connect(
136+
button, &QPushButton::clicked,
137+
this, [&value](bool){
138+
QMessageBox::StandardButton button = QMessageBox::question(
139+
nullptr,
140+
"Restore Defaults",
141+
"Are you sure you wish to this table back to defaults? This will wipe the current table.",
142+
QMessageBox::Ok | QMessageBox::Cancel
143+
);
144+
if (button == QMessageBox::Ok){
145+
value.restore_defaults();
146+
}
147+
}
148+
);
149+
}
131150
buttons->addStretch(2);
132151
}
133152

SerialPrograms/CMakeLists.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ file(GLOB MAIN_SOURCES
138138
../Common/Cpp/Options/GroupOption.h
139139
../Common/Cpp/Options/IntegerRangeOption.cpp
140140
../Common/Cpp/Options/IntegerRangeOption.h
141+
../Common/Cpp/Options/KeyBindingOption.cpp
142+
../Common/Cpp/Options/KeyBindingOption.h
141143
../Common/Cpp/Options/RandomCodeOption.cpp
142144
../Common/Cpp/Options/RandomCodeOption.h
143145
../Common/Cpp/Options/SimpleIntegerOption.cpp
@@ -214,6 +216,8 @@ file(GLOB MAIN_SOURCES
214216
../Common/Qt/Options/GroupWidget.h
215217
../Common/Qt/Options/IntegerRangeWidget.cpp
216218
../Common/Qt/Options/IntegerRangeWidget.h
219+
../Common/Qt/Options/KeyBindingWidget.cpp
220+
../Common/Qt/Options/KeyBindingWidget.h
217221
../Common/Qt/Options/RandomCodeWidget.cpp
218222
../Common/Qt/Options/RandomCodeWidget.h
219223
../Common/Qt/Options/SimpleIntegerWidget.cpp
@@ -832,8 +836,6 @@ file(GLOB MAIN_SOURCES
832836
Source/NintendoSwitch/Framework/NintendoSwitch_SwitchSystemSession.h
833837
Source/NintendoSwitch/Framework/NintendoSwitch_VirtualController.cpp
834838
Source/NintendoSwitch/Framework/NintendoSwitch_VirtualController.h
835-
Source/NintendoSwitch/Framework/NintendoSwitch_VirtualControllerMapping.cpp
836-
Source/NintendoSwitch/Framework/NintendoSwitch_VirtualControllerMapping.h
837839
Source/NintendoSwitch/Framework/NintendoSwitch_VirtualControllerState.cpp
838840
Source/NintendoSwitch/Framework/NintendoSwitch_VirtualControllerState.h
839841
Source/NintendoSwitch/Framework/UI/NintendoSwitch_CommandRow.cpp
@@ -852,6 +854,8 @@ file(GLOB MAIN_SOURCES
852854
Source/NintendoSwitch/Inference/NintendoSwitch_DetectHome.h
853855
Source/NintendoSwitch/NintendoSwitch_ConsoleHandle.cpp
854856
Source/NintendoSwitch/NintendoSwitch_ConsoleHandle.h
857+
Source/NintendoSwitch/NintendoSwitch_KeyboardMapping.cpp
858+
Source/NintendoSwitch/NintendoSwitch_KeyboardMapping.h
855859
Source/NintendoSwitch/NintendoSwitch_MultiSwitchProgram.cpp
856860
Source/NintendoSwitch/NintendoSwitch_MultiSwitchProgram.h
857861
Source/NintendoSwitch/NintendoSwitch_Panels.cpp

SerialPrograms/SerialPrograms.pro

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ SOURCES += \
110110
../Common/Cpp/Options/FloatingPointOption.cpp \
111111
../Common/Cpp/Options/GroupOption.cpp \
112112
../Common/Cpp/Options/IntegerRangeOption.cpp \
113+
../Common/Cpp/Options/KeyBindingOption.cpp \
113114
../Common/Cpp/Options/RandomCodeOption.cpp \
114115
../Common/Cpp/Options/SimpleIntegerOption.cpp \
115116
../Common/Cpp/Options/StaticTableOption.cpp \
@@ -141,6 +142,7 @@ SOURCES += \
141142
../Common/Qt/Options/FloatingPointWidget.cpp \
142143
../Common/Qt/Options/GroupWidget.cpp \
143144
../Common/Qt/Options/IntegerRangeWidget.cpp \
145+
../Common/Qt/Options/KeyBindingWidget.cpp \
144146
../Common/Qt/Options/RandomCodeWidget.cpp \
145147
../Common/Qt/Options/SimpleIntegerWidget.cpp \
146148
../Common/Qt/Options/StaticTableWidget.cpp \
@@ -413,7 +415,6 @@ SOURCES += \
413415
Source/NintendoSwitch/Framework/NintendoSwitch_SwitchSystemOption.cpp \
414416
Source/NintendoSwitch/Framework/NintendoSwitch_SwitchSystemSession.cpp \
415417
Source/NintendoSwitch/Framework/NintendoSwitch_VirtualController.cpp \
416-
Source/NintendoSwitch/Framework/NintendoSwitch_VirtualControllerMapping.cpp \
417418
Source/NintendoSwitch/Framework/NintendoSwitch_VirtualControllerState.cpp \
418419
Source/NintendoSwitch/Framework/UI/NintendoSwitch_CommandRow.cpp \
419420
Source/NintendoSwitch/Framework/UI/NintendoSwitch_MultiSwitchProgramWidget.cpp \
@@ -423,6 +424,7 @@ SOURCES += \
423424
Source/NintendoSwitch/Inference/NintendoSwitch_DateReader.cpp \
424425
Source/NintendoSwitch/Inference/NintendoSwitch_DetectHome.cpp \
425426
Source/NintendoSwitch/NintendoSwitch_ConsoleHandle.cpp \
427+
Source/NintendoSwitch/NintendoSwitch_KeyboardMapping.cpp \
426428
Source/NintendoSwitch/NintendoSwitch_MultiSwitchProgram.cpp \
427429
Source/NintendoSwitch/NintendoSwitch_Panels.cpp \
428430
Source/NintendoSwitch/NintendoSwitch_Settings.cpp \
@@ -646,6 +648,8 @@ SOURCES += \
646648
Source/PokemonLA/Resources/PokemonLA_NameDatabase.cpp \
647649
Source/PokemonLA/Resources/PokemonLA_PokemonSprites.cpp \
648650
Source/PokemonLA/Resources/PokemonLA_WeatherAndTimeIcons.cpp \
651+
Source/PokemonRSE/PokemonRSE_Panels.cpp \
652+
Source/PokemonRSE/PokemonRSE_Settings.cpp \
649653
Source/PokemonSV/Inference/Battles/PokemonSV_BattleBallReader.cpp \
650654
Source/PokemonSV/Inference/Battles/PokemonSV_EncounterWatcher.cpp \
651655
Source/PokemonSV/Inference/Battles/PokemonSV_NormalBattleMenus.cpp \
@@ -1166,6 +1170,7 @@ HEADERS += \
11661170
../Common/Cpp/Options/FloatingPointOption.h \
11671171
../Common/Cpp/Options/GroupOption.h \
11681172
../Common/Cpp/Options/IntegerRangeOption.h \
1173+
../Common/Cpp/Options/KeyBindingOption.h \
11691174
../Common/Cpp/Options/RandomCodeOption.h \
11701175
../Common/Cpp/Options/SimpleIntegerOption.h \
11711176
../Common/Cpp/Options/StaticTableOption.h \
@@ -1210,6 +1215,7 @@ HEADERS += \
12101215
../Common/Qt/Options/FloatingPointWidget.h \
12111216
../Common/Qt/Options/GroupWidget.h \
12121217
../Common/Qt/Options/IntegerRangeWidget.h \
1218+
../Common/Qt/Options/KeyBindingWidget.h \
12131219
../Common/Qt/Options/RandomCodeWidget.h \
12141220
../Common/Qt/Options/SimpleIntegerWidget.h \
12151221
../Common/Qt/Options/StaticTableWidget.h \
@@ -1547,7 +1553,6 @@ HEADERS += \
15471553
Source/NintendoSwitch/Framework/NintendoSwitch_SwitchSystemOption.h \
15481554
Source/NintendoSwitch/Framework/NintendoSwitch_SwitchSystemSession.h \
15491555
Source/NintendoSwitch/Framework/NintendoSwitch_VirtualController.h \
1550-
Source/NintendoSwitch/Framework/NintendoSwitch_VirtualControllerMapping.h \
15511556
Source/NintendoSwitch/Framework/NintendoSwitch_VirtualControllerState.h \
15521557
Source/NintendoSwitch/Framework/UI/NintendoSwitch_CommandRow.h \
15531558
Source/NintendoSwitch/Framework/UI/NintendoSwitch_MultiSwitchProgramWidget.h \
@@ -1556,6 +1561,7 @@ HEADERS += \
15561561
Source/NintendoSwitch/Framework/UI/NintendoSwitch_SwitchSystemWidget.h \
15571562
Source/NintendoSwitch/Inference/NintendoSwitch_DateReader.h \
15581563
Source/NintendoSwitch/Inference/NintendoSwitch_DetectHome.h \
1564+
Source/NintendoSwitch/NintendoSwitch_KeyboardMapping.h \
15591565
Source/NintendoSwitch/NintendoSwitch_MultiSwitchProgram.h \
15601566
Source/NintendoSwitch/NintendoSwitch_Panels.h \
15611567
Source/NintendoSwitch/NintendoSwitch_Settings.h \
@@ -1784,6 +1790,8 @@ HEADERS += \
17841790
Source/PokemonLA/Resources/PokemonLA_NameDatabase.h \
17851791
Source/PokemonLA/Resources/PokemonLA_PokemonSprites.h \
17861792
Source/PokemonLA/Resources/PokemonLA_WeatherAndTimeIcons.h \
1793+
Source/PokemonRSE/PokemonRSE_Panels.h \
1794+
Source/PokemonRSE/PokemonRSE_Settings.h \
17871795
Source/PokemonSV/Inference/Battles/PokemonSV_BattleBallReader.h \
17881796
Source/PokemonSV/Inference/Battles/PokemonSV_EncounterWatcher.h \
17891797
Source/PokemonSV/Inference/Battles/PokemonSV_NormalBattleMenus.h \

SerialPrograms/Source/CommonFramework/PersistentSettings.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include "CommonFramework/Globals.h"
1313
#include "CommonFramework/GlobalSettingsPanel.h"
1414
#include "CommonFramework/Options/Environment/PerformanceOptions.h"
15-
#include "NintendoSwitch/Framework/NintendoSwitch_VirtualControllerMapping.h"
1615
#include "PersistentSettings.h"
1716

1817
// #include <iostream>
@@ -37,7 +36,7 @@ void PersistentSettings::write() const{
3736
JsonObject root;
3837

3938
root["20-GlobalSettings"] = GlobalSettings::instance().to_json();
40-
root["50-SwitchKeyboardMapping"] = NintendoSwitch::read_keyboard_mapping();
39+
// root["50-SwitchKeyboardMapping"] = NintendoSwitch::read_keyboard_mapping();
4140

4241
root["99-Panels"] = panels.clone();
4342

@@ -67,12 +66,14 @@ void PersistentSettings::read(){
6766
// GlobalSettings::instance().PROCESS_PRIORITY0.update_priority_to_option();
6867
GlobalSettings::instance().PERFORMANCE->REALTIME_THREAD_PRIORITY.set_on_this_thread();
6968

69+
#if 0
7070
{
7171
const JsonArray* array = obj->get_array("50-SwitchKeyboardMapping");
7272
if (array){
7373
NintendoSwitch::set_keyboard_mapping(*array);
7474
}
7575
}
76+
#endif
7677
{
7778
JsonObject* value = obj->get_object("99-Panels");
7879
if (value){

SerialPrograms/Source/NintendoSwitch/Framework/NintendoSwitch_VirtualController.cpp

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
#include "CommonFramework/Exceptions/ProgramFinishedException.h"
1212
#include "CommonFramework/Options/Environment/PerformanceOptions.h"
1313
#include "Controllers/GlobalQtKeyMap.h"
14+
#include "NintendoSwitch/NintendoSwitch_Settings.h"
1415
#include "NintendoSwitch/Controllers/NintendoSwitch_Controller.h"
15-
#include "NintendoSwitch_VirtualControllerMapping.h"
1616
#include "NintendoSwitch_VirtualController.h"
1717

1818
//#include <iostream>
@@ -38,8 +38,15 @@ VirtualController::VirtualController(
3838
, m_allow_commands_while_running(allow_commands_while_running)
3939
, m_last_known_state(ProgramState::STOPPED)
4040
, m_stop(false)
41-
, m_thread(run_with_catch, "VirtualController::thread_loop()", [this]{ thread_loop(); })
42-
{}
41+
{
42+
std::vector<std::shared_ptr<EditableTableRow>> mapping = ConsoleSettings::instance().KEYBOARD_MAPPINGS.current_refs();
43+
for (const auto& deltas : mapping){
44+
const KeyMapTableRow& row = static_cast<const KeyMapTableRow&>(*deltas);
45+
m_mapping[(Qt::Key)(uint32_t)row.key] += row.snapshot();
46+
}
47+
48+
m_thread = std::thread(run_with_catch, "VirtualController::thread_loop()", [this]{ thread_loop(); });
49+
}
4350
VirtualController::~VirtualController(){
4451
m_stop.store(true, std::memory_order_release);
4552
{
@@ -60,6 +67,10 @@ void VirtualController::clear_state(){
6067

6168
bool VirtualController::on_key_press(const QKeyEvent& key){
6269
// cout << "press: " << key.key() << ", native = " << key.nativeVirtualKey() << endl;
70+
71+
// QKeySequence seq((Qt::Key)key.key());
72+
// cout << "button: " << seq.toString().toStdString() << endl;
73+
6374
QtKeyMap::instance().record(key);
6475
{
6576
WriteSpinLock lg(m_state_lock);
@@ -101,7 +112,7 @@ bool VirtualController::try_next_interrupt(){
101112
void VirtualController::thread_loop(){
102113
GlobalSettings::instance().PERFORMANCE->REALTIME_THREAD_PRIORITY.set_on_this_thread();
103114

104-
VirtualControllerState last;
115+
ControllerState last;
105116
bool last_neutral = true;
106117
WallClock last_press = current_time();
107118
while (true){
@@ -113,7 +124,7 @@ void VirtualController::thread_loop(){
113124
if (!m_allow_commands_while_running &&
114125
m_last_known_state.load(std::memory_order_acquire) != ProgramState::STOPPED
115126
){
116-
last = VirtualControllerState();
127+
last = ControllerState();
117128
std::unique_lock<std::mutex> lg(m_sleep_lock);
118129
if (m_stop.load(std::memory_order_acquire)){
119130
return;
@@ -131,33 +142,24 @@ void VirtualController::thread_loop(){
131142
next_wake = m_state_tracker.next_state_change();
132143
}
133144

134-
#if 0
135-
// REMOVE
136-
std::string str = "state: ";
137-
for (uint32_t native_key : pressed_native_keys){
138-
str += std::to_string(native_key);
139-
str += " ";
140-
}
141-
cout << str << endl; // REMOVE
142-
#endif
143-
144145
// Convert the raw keyboard state to controller state.
145-
VirtualControllerState current;
146+
bool neutral;
147+
ControllerState current;
146148
{
149+
ControllerDeltas deltas;
147150
const QtKeyMap& qkey_map = QtKeyMap::instance();
148151
for (uint32_t native_key : pressed_native_keys){
149152
const std::set<Qt::Key>& qkeys = qkey_map.get_QtKeys(native_key);
150153
for (Qt::Key qkey : qkeys){
151-
const ControllerButton* button = button_lookup(qkey);
152-
if (button != nullptr){
153-
button->press(current);
154+
auto iter = m_mapping.find(qkey);
155+
if (iter != m_mapping.end()){
156+
deltas += iter->second;
157+
break;
154158
}
155159
}
156160
}
161+
neutral = deltas.to_state(current);
157162
}
158-
ControllerState state;
159-
bool neutral = current.to_state(state);
160-
// cout << "neutral = " << neutral << endl;
161163

162164

163165
// Send the command.
@@ -175,7 +177,7 @@ void VirtualController::thread_loop(){
175177
// If state is neutral, just issue a stop.
176178
if (neutral){
177179
if (try_stop_commands()){
178-
last = VirtualControllerState();
180+
last = ControllerState();
179181
last_neutral = true;
180182
last_press = now;
181183
}else{
@@ -193,22 +195,22 @@ void VirtualController::thread_loop(){
193195

194196
// Send the command.
195197
m_logger.log(
196-
"VirtualController: (" + button_to_string(state.buttons) +
197-
"), dpad(" + dpad_to_string(state.dpad) +
198-
"), LJ(" + std::to_string(state.left_x) + "," + std::to_string(state.left_y) +
199-
"), RJ(" + std::to_string(state.right_x) + "," + std::to_string(state.right_y) +
198+
"VirtualController: (" + button_to_string(current.buttons) +
199+
"), dpad(" + dpad_to_string(current.dpad) +
200+
"), LJ(" + std::to_string(current.left_x) + "," + std::to_string(current.left_y) +
201+
"), RJ(" + std::to_string(current.right_x) + "," + std::to_string(current.right_y) +
200202
")",
201203
COLOR_DARKGREEN
202204
);
203205
std::string error = m_session.try_run<SwitchController>([=](SwitchController& controller){
204206
controller.issue_controller_state(
205207
nullptr,
206-
state.buttons,
207-
state.dpad,
208-
state.left_x,
209-
state.left_y,
210-
state.right_x,
211-
state.right_y,
208+
current.buttons,
209+
current.dpad,
210+
current.left_x,
211+
current.left_y,
212+
current.right_x,
213+
current.right_y,
212214
255*8ms
213215
);
214216
});

SerialPrograms/Source/NintendoSwitch/Framework/NintendoSwitch_VirtualController.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "CommonFramework/Globals.h"
1414
#include "Controllers/ControllerSession.h"
1515
#include "Controllers/KeyboardStateTracker.h"
16+
#include "NintendoSwitch_VirtualControllerState.h"
1617

1718
namespace PokemonAutomation{
1819
namespace NintendoSwitch{
@@ -54,6 +55,8 @@ class VirtualController{
5455
ControllerSession& m_session;
5556
const bool m_allow_commands_while_running;
5657

58+
std::map<Qt::Key, ControllerDeltas> m_mapping;
59+
5760
// Controller State
5861
SpinLock m_state_lock;
5962
KeyboardStateTracker m_state_tracker;

0 commit comments

Comments
 (0)