Skip to content

Commit

Permalink
Qt: finalize migration to the Joystick API
Browse files Browse the repository at this point in the history
  • Loading branch information
fleroviux committed Jul 29, 2023
1 parent 402cda4 commit 555659e
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 98 deletions.
22 changes: 11 additions & 11 deletions src/platform/qt/rc/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,20 @@ mp2k_hle_enable = false
mp2k_hle_cubic = false

[input]
fast_forward = [32, -1, 4]
fast_forward = [32, -1, -1]
hold_fast_forward = true
controller_guid = ""
[input.gba]
b = [83, 1, -1]
a = [65, 0, -1]
select = [16777219, 4, -1]
right = [16777236, 14, 0]
r = [70, 10, -1]
l = [68, 9, -1]
left = [16777234, 13, 128]
start = [16777220, 6, -1]
down = [16777237, 12, 1]
up = [16777235, 11, 129]
b = [83, -1, -1]
a = [65, -1, -1]
select = [16777219, -1, -1]
right = [16777236, -1, -1]
r = [70, -1, -1]
l = [68, -1, -1]
left = [16777234, -1, -1]
start = [16777220, -1, -1]
down = [16777237, -1, -1]
up = [16777235, -1, -1]

[window]
fullscreen = false
Expand Down
3 changes: 1 addition & 2 deletions src/platform/qt/src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ void QtConfig::LoadCustomData(toml::value const& data) {
input.hold_fast_forward = toml::find_or<bool>(input_, "hold_fast_forward", true);

const auto get_map = [&](toml::value const& value, std::string key) {
return Map::FromArray(toml::find_or<std::array<int, 3>>(value, key, {
0, SDL_CONTROLLER_BUTTON_INVALID, SDL_CONTROLLER_AXIS_INVALID}));
return Map::FromArray(toml::find_or<std::array<int, 3>>(value, key, {0, -1, -1}));
};

input.fast_forward = get_map(input_, "fast_forward");
Expand Down
29 changes: 14 additions & 15 deletions src/platform/qt/src/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include <filesystem>
#include <platform/config.hpp>
#include <Qt>
#include <SDL.h>
#include <vector>

#ifdef MACOS_BUILD_APP_BUNDLE
Expand All @@ -32,9 +31,9 @@ struct QtConfig final : nba::PlatformConfig {
int keyboard = 0;

struct {
int button = SDL_CONTROLLER_BUTTON_INVALID;
int axis = SDL_CONTROLLER_AXIS_INVALID;
} controller;
int button = -1;
int axis = -1;
} controller{};

static auto FromArray(std::array<int, 3> const& array) -> Map {
return {array[0], {array[1], array[2]}};
Expand All @@ -46,19 +45,19 @@ struct QtConfig final : nba::PlatformConfig {
};

Map gba[nba::InputDevice::kKeyCount] {
{Qt::Key_Up, {SDL_CONTROLLER_BUTTON_DPAD_UP, SDL_CONTROLLER_AXIS_LEFTY | 0x80}},
{Qt::Key_Down, {SDL_CONTROLLER_BUTTON_DPAD_DOWN, SDL_CONTROLLER_AXIS_LEFTY}},
{Qt::Key_Left, {SDL_CONTROLLER_BUTTON_DPAD_LEFT, SDL_CONTROLLER_AXIS_LEFTX | 0x80}},
{Qt::Key_Right, {SDL_CONTROLLER_BUTTON_DPAD_RIGHT, SDL_CONTROLLER_AXIS_LEFTX}},
{Qt::Key_Return, {SDL_CONTROLLER_BUTTON_START}},
{Qt::Key_Backspace, {SDL_CONTROLLER_BUTTON_BACK}},
{Qt::Key_A, {SDL_CONTROLLER_BUTTON_A}},
{Qt::Key_S, {SDL_CONTROLLER_BUTTON_B}},
{Qt::Key_D, {SDL_CONTROLLER_BUTTON_LEFTSHOULDER}},
{Qt::Key_F, {SDL_CONTROLLER_BUTTON_RIGHTSHOULDER}}
{Qt::Key_Up},
{Qt::Key_Down},
{Qt::Key_Left},
{Qt::Key_Right},
{Qt::Key_Return},
{Qt::Key_Backspace},
{Qt::Key_A},
{Qt::Key_S},
{Qt::Key_D},
{Qt::Key_F}
};

Map fast_forward = {Qt::Key_Space, {SDL_CONTROLLER_BUTTON_X}};
Map fast_forward = {Qt::Key_Space};

std::string controller_guid;
bool hold_fast_forward = true;
Expand Down
36 changes: 18 additions & 18 deletions src/platform/qt/src/widget/controller_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,45 +48,45 @@ void ControllerManager::Initialize() {
#endif

connect(
this, &ControllerManager::OnControllerListChanged,
this, &ControllerManager::UpdateGameControllerList,
this, &ControllerManager::OnJoystickListChanged,
this, &ControllerManager::UpdateJoystickList,
Qt::QueuedConnection
);

connect(
this, &ControllerManager::OnControllerButtonReleased,
this, &ControllerManager::BindCurrentKeyToControllerButton,
this, &ControllerManager::OnJoystickButtonReleased,
this, &ControllerManager::BindCurrentKeyToJoystickButton,
Qt::QueuedConnection
);

connect(
this, &ControllerManager::OnControllerAxisMoved,
this, &ControllerManager::BindCurrentKeyToControllerAxis,
this, &ControllerManager::OnJoystickAxisMoved,
this, &ControllerManager::BindCurrentKeyToJoystickAxis,
Qt::QueuedConnection
);
}

void ControllerManager::UpdateGameControllerList() {
void ControllerManager::UpdateJoystickList() {
auto input_window = main_window->input_window;

if(input_window) {
input_window->UpdateGameControllerList();
input_window->UpdateJoystickList();
}
}

void ControllerManager::BindCurrentKeyToControllerButton(int button) {
void ControllerManager::BindCurrentKeyToJoystickButton(int button) {
auto input_window = main_window->input_window;

if(input_window) {
input_window->BindCurrentKeyToControllerButton(button);
input_window->BindCurrentKeyToJoystickButton(button);
}
}

void ControllerManager::BindCurrentKeyToControllerAxis(int axis, bool negative) {
void ControllerManager::BindCurrentKeyToJoystickAxis(int axis, bool negative) {
auto input_window = main_window->input_window;

if(input_window) {
input_window->BindCurrentKeyToControllerAxis(axis, negative);
input_window->BindCurrentKeyToJoystickAxis(axis, negative);
}
}

Expand All @@ -96,7 +96,7 @@ void ControllerManager::Open(std::string const& guid) {
Close();

for(int device_id = 0; device_id < joystick_count; device_id++) {
if(GetControllerGUIDStringFromIndex(device_id) == guid) {
if(GetJoystickGUIDStringFromIndex(device_id) == guid) {
joystick = SDL_JoystickOpen(device_id);
instance_id = SDL_JoystickInstanceID(joystick);
break;
Expand Down Expand Up @@ -136,18 +136,18 @@ void ControllerManager::ProcessEvents() {
while(SDL_PollEvent(&event)) {
switch(event.type) {
case SDL_JOYDEVICEADDED: {
emit OnControllerListChanged();
emit OnJoystickListChanged();

auto device_id = ((SDL_JoyDeviceEvent*)&event)->which;
auto guid = GetControllerGUIDStringFromIndex(device_id);
auto guid = GetJoystickGUIDStringFromIndex(device_id);

if(guid == config->input.controller_guid) {
Open(guid);
}
break;
}
case SDL_JOYDEVICEREMOVED: {
emit OnControllerListChanged();
emit OnJoystickListChanged();

if(instance_id == ((SDL_JoyDeviceEvent*)&event)->which) {
Close();
Expand All @@ -157,7 +157,7 @@ void ControllerManager::ProcessEvents() {
case SDL_JOYBUTTONUP: {
auto button_event = (SDL_JoyButtonEvent*)&event;

emit OnControllerButtonReleased(button_event->button);
emit OnJoystickButtonReleased(button_event->button);
break;
}
case SDL_JOYAXISMOTION: {
Expand All @@ -167,7 +167,7 @@ void ControllerManager::ProcessEvents() {
auto value = axis_event->value;

if(std::abs(value) > threshold) {
emit OnControllerAxisMoved(axis_event->axis, value < 0);
emit OnJoystickAxisMoved(axis_event->axis, value < 0);
}
break;
}
Expand Down
12 changes: 6 additions & 6 deletions src/platform/qt/src/widget/controller_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ struct ControllerManager : QWidget {
void Initialize();

signals:
void OnControllerListChanged();
void OnControllerButtonReleased(int button);
void OnControllerAxisMoved(int axis, bool negative);
void OnJoystickListChanged();
void OnJoystickButtonReleased(int button);
void OnJoystickAxisMoved(int axis, bool negative);

private slots:
void UpdateGameControllerList();
void BindCurrentKeyToControllerButton(int button);
void BindCurrentKeyToControllerAxis(int axis, bool negative);
void UpdateJoystickList();
void BindCurrentKeyToJoystickButton(int button);
void BindCurrentKeyToJoystickAxis(int axis, bool negative);

private:
void Open(std::string const& guid);
Expand Down
72 changes: 34 additions & 38 deletions src/platform/qt/src/widget/input_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ InputWindow::InputWindow(
auto vbox = new QVBoxLayout{this};
vbox->setSizeConstraint(QLayout::SetFixedSize);
vbox->addLayout(CreateKeyMapTable());
vbox->addLayout(CreateGameControllerList());
vbox->addLayout(CreateJoystickList());

app->installEventFilter(this);

Expand Down Expand Up @@ -50,59 +50,59 @@ bool InputWindow::eventFilter(QObject* obj, QEvent* event) {
return QObject::eventFilter(obj, event);
}

void InputWindow::BindCurrentKeyToControllerButton(int button) {
if(waiting_for_controller) {
void InputWindow::BindCurrentKeyToJoystickButton(int button) {
if(waiting_for_joystick) {
active_mapping->controller.button = button;
active_button->setText(GetControllerButtonName(active_mapping));
waiting_for_controller = false;
active_button->setText(GetJoystickButtonName(active_mapping));
waiting_for_joystick = false;
config->Save();
}
}

void InputWindow::BindCurrentKeyToControllerAxis(int axis, bool negative) {
if(waiting_for_controller) {
void InputWindow::BindCurrentKeyToJoystickAxis(int axis, bool negative) {
if(waiting_for_joystick) {
active_mapping->controller.axis = axis | (negative ? 0x80 : 0);
active_button->setText(GetControllerButtonName(active_mapping));
waiting_for_controller = false;
active_button->setText(GetJoystickButtonName(active_mapping));
waiting_for_joystick = false;
config->Save();
}
}

auto InputWindow::CreateGameControllerList() -> QLayout* {
auto InputWindow::CreateJoystickList() -> QLayout* {
auto hbox = new QHBoxLayout{};

controller_combo_box = new QComboBox();
joystick_combo_box = new QComboBox();

auto label = new QLabel{"Game Controller:"};
auto label = new QLabel{"Joystick:"};
hbox->addWidget(label);
hbox->addWidget(controller_combo_box);
hbox->addWidget(joystick_combo_box);

connect(controller_combo_box, QOverload<int>::of(&QComboBox::activated), [this](int index) {
config->input.controller_guid = controller_combo_box->itemData(index).toString().toStdString();
connect(joystick_combo_box, QOverload<int>::of(&QComboBox::activated), [this](int index) {
config->input.controller_guid = joystick_combo_box->itemData(index).toString().toStdString();
config->Save();

has_game_controller_choice_changed = true;
});

UpdateGameControllerList();
UpdateJoystickList();
return hbox;
}

void InputWindow::UpdateGameControllerList() {
void InputWindow::UpdateJoystickList() {
auto joystick_count = SDL_NumJoysticks();

controller_combo_box->clear();
joystick_combo_box->clear();

controller_combo_box->addItem("(none)", "");
controller_combo_box->setCurrentIndex(0);
joystick_combo_box->addItem("(none)", "");
joystick_combo_box->setCurrentIndex(0);

for(int i = 0; i < joystick_count; i++) {
auto guid = GetControllerGUIDStringFromIndex(i);
auto guid = GetJoystickGUIDStringFromIndex(i);

controller_combo_box->addItem(SDL_JoystickNameForIndex(i), QString::fromStdString(guid));
joystick_combo_box->addItem(SDL_JoystickNameForIndex(i), QString::fromStdString(guid));

if(guid == config->input.controller_guid) {
controller_combo_box->setCurrentIndex(controller_combo_box->count() - 1);
joystick_combo_box->setCurrentIndex(joystick_combo_box->count() - 1);
}
}
}
Expand Down Expand Up @@ -154,14 +154,14 @@ void InputWindow::CreateKeyMapEntry(
}

{
button_controller = new QPushButton{GetControllerButtonName(mapping)};
button_controller = new QPushButton{GetJoystickButtonName(mapping)};

connect(button_controller, &QPushButton::clicked, [=]() {
RestoreActiveButtonLabel();
button_controller->setText("[press button]");
active_mapping = mapping;
active_button = button_controller;
waiting_for_controller = true;
waiting_for_joystick = true;
});

layout->addWidget(button_controller, row, 2);
Expand All @@ -173,13 +173,13 @@ void InputWindow::CreateKeyMapEntry(
connect(button, &QPushButton::clicked, [=]() {
if(active_mapping == mapping) {
waiting_for_keyboard = false;
waiting_for_controller = false;
waiting_for_joystick = false;
}

*mapping = {};
config->Save();
button_keyboard->setText(GetKeyboardButtonName(mapping->keyboard));
button_controller->setText(GetControllerButtonName(mapping));
button_controller->setText(GetJoystickButtonName(mapping));
});

layout->addWidget(button, row, 3);
Expand All @@ -192,9 +192,9 @@ void InputWindow::RestoreActiveButtonLabel() {
waiting_for_keyboard = false;
}

if(waiting_for_controller) {
active_button->setText(GetControllerButtonName(active_mapping));
waiting_for_controller = false;
if(waiting_for_joystick) {
active_button->setText(GetJoystickButtonName(active_mapping));
waiting_for_joystick = false;
}
}

Expand All @@ -205,22 +205,18 @@ auto InputWindow::GetKeyboardButtonName(int key) -> QString {
return QKeySequence{key}.toString();
}

auto InputWindow::GetControllerButtonName(QtConfig::Input::Map* mapping) -> QString {
auto InputWindow::GetJoystickButtonName(QtConfig::Input::Map* mapping) -> QString {
const auto button = mapping->controller.button;
const auto axis = mapping->controller.axis;

// const QString button_name = QString::asprintf("Button %d", button);
// const QString axis_name = QString::asprintf("Axis%c %d", (axis & 0x80) ? '-' : '+', axis & ~0x80);

const QString button_name = QStringLiteral("Button %1").arg(button);
const QString axis_name = QStringLiteral("Axis%1 %2").arg(axis & 0x80 ? '-' : '+').arg(axis & ~0x80);

// @todo: fix the conditions up
if(button != SDL_CONTROLLER_BUTTON_INVALID && axis != SDL_CONTROLLER_AXIS_INVALID) {
if(button != -1 && axis != -1) {
return QStringLiteral("%1 - %2").arg(button_name).arg(axis_name);
} else if(button != SDL_CONTROLLER_BUTTON_INVALID) {
} else if(button != -1) {
return button_name;
} else if(axis != SDL_CONTROLLER_AXIS_INVALID) {
} else if(axis != -1) {
return axis_name;
}

Expand Down
Loading

0 comments on commit 555659e

Please sign in to comment.