Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #8417 from jordan-woyak/setting-expressions
InputCommon: Allow controller settings specified with input expresions.
  • Loading branch information
Tilka committed Feb 9, 2020
2 parents 7fe11c9 + 0a1634b commit 2e25403
Show file tree
Hide file tree
Showing 20 changed files with 315 additions and 63 deletions.
3 changes: 1 addition & 2 deletions Source/Core/Core/HW/WiimoteEmu/Dynamics.cpp
Expand Up @@ -330,8 +330,7 @@ void EmulateIMUCursor(IMUCursorState* state, ControllerEmu::IMUCursor* imu_ir_gr
auto target_yaw = std::clamp(yaw, -max_yaw, max_yaw);

// Handle the "Recenter" button being pressed.
if (imu_ir_group->controls[0]->control_ref->State() >
ControllerEmu::Buttons::ACTIVATION_THRESHOLD)
if (imu_ir_group->controls[0]->control_ref->GetState<bool>())
{
state->recentered_pitch = std::asin((inv_rotation * Common::Vec3{0, 1, 0}).z);
target_yaw = 0;
Expand Down
4 changes: 1 addition & 3 deletions Source/Core/DolphinQt/Config/Mapping/MappingButton.cpp
Expand Up @@ -132,11 +132,9 @@ void MappingButton::UpdateIndicator()
if (!isActiveWindow())
return;

const auto state = m_reference->State();

QFont f = m_parent->font();

if (state > ControllerEmu::Buttons::ACTIVATION_THRESHOLD)
if (m_reference->GetState<bool>())
f.setBold(true);

setFont(f);
Expand Down
59 changes: 53 additions & 6 deletions Source/Core/DolphinQt/Config/Mapping/MappingNumeric.cpp
Expand Up @@ -4,6 +4,8 @@

#include "DolphinQt/Config/Mapping/MappingNumeric.h"

#include <limits>

#include "DolphinQt/Config/Mapping/MappingWidget.h"

#include "InputCommon/ControllerEmu/ControllerEmu.h"
Expand All @@ -12,24 +14,20 @@
MappingDouble::MappingDouble(MappingWidget* parent, ControllerEmu::NumericSetting<double>* setting)
: QDoubleSpinBox(parent), m_setting(*setting)
{
setRange(m_setting.GetMinValue(), m_setting.GetMaxValue());
setDecimals(2);

setFixedWidth(WIDGET_MAX_WIDTH);

if (const auto ui_suffix = m_setting.GetUISuffix())
setSuffix(QLatin1Char{' '} + tr(ui_suffix));

if (const auto ui_description = m_setting.GetUIDescription())
setToolTip(tr(ui_description));

connect(this, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this,
[this, parent](double value) {
m_setting.SetValue(value);
ConfigChanged();
parent->SaveSettings();
});

connect(parent, &MappingWidget::ConfigChanged, this, &MappingDouble::ConfigChanged);
connect(parent, &MappingWidget::Update, this, &MappingDouble::Update);
}

// Overriding QDoubleSpinBox's fixup to set the default value when input is cleared.
Expand All @@ -41,6 +39,36 @@ void MappingDouble::fixup(QString& input) const
void MappingDouble::ConfigChanged()
{
const QSignalBlocker blocker(this);

QString suffix;

if (const auto ui_suffix = m_setting.GetUISuffix())
suffix += QLatin1Char{' '} + tr(ui_suffix);

if (m_setting.IsSimpleValue())
{
setRange(m_setting.GetMinValue(), m_setting.GetMaxValue());
setButtonSymbols(ButtonSymbols::UpDownArrows);
}
else
{
constexpr auto inf = std::numeric_limits<double>::infinity();
setRange(-inf, inf);
setButtonSymbols(ButtonSymbols::NoButtons);
suffix += QString::fromUtf8(" 🎮");
}

setSuffix(suffix);

setValue(m_setting.GetValue());
}

void MappingDouble::Update()
{
if (m_setting.IsSimpleValue() || hasFocus())
return;

const QSignalBlocker blocker(this);
setValue(m_setting.GetValue());
}

Expand All @@ -49,14 +77,33 @@ MappingBool::MappingBool(MappingWidget* parent, ControllerEmu::NumericSetting<bo
{
connect(this, &QCheckBox::stateChanged, this, [this, parent](int value) {
m_setting.SetValue(value != 0);
ConfigChanged();
parent->SaveSettings();
});

connect(parent, &MappingWidget::ConfigChanged, this, &MappingBool::ConfigChanged);
connect(parent, &MappingWidget::Update, this, &MappingBool::Update);

setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Ignored);
}

void MappingBool::ConfigChanged()
{
const QSignalBlocker blocker(this);

if (m_setting.IsSimpleValue())
setText({});
else
setText(QString::fromUtf8("🎮"));

setChecked(m_setting.GetValue());
}

void MappingBool::Update()
{
if (m_setting.IsSimpleValue())
return;

const QSignalBlocker blocker(this);
setChecked(m_setting.GetValue());
}
2 changes: 2 additions & 0 deletions Source/Core/DolphinQt/Config/Mapping/MappingNumeric.h
Expand Up @@ -21,6 +21,7 @@ class MappingDouble : public QDoubleSpinBox
void fixup(QString& input) const override;

void ConfigChanged();
void Update();

ControllerEmu::NumericSetting<double>& m_setting;
};
Expand All @@ -32,6 +33,7 @@ class MappingBool : public QCheckBox

private:
void ConfigChanged();
void Update();

ControllerEmu::NumericSetting<bool>& m_setting;
};
45 changes: 35 additions & 10 deletions Source/Core/DolphinQt/Config/Mapping/MappingWidget.cpp
Expand Up @@ -9,8 +9,8 @@
#include <QGroupBox>
#include <QLabel>
#include <QPushButton>
#include <QTimer>

#include "DolphinQt/Config/Mapping/IOWindow.h"
#include "DolphinQt/Config/Mapping/MappingButton.h"
#include "DolphinQt/Config/Mapping/MappingIndicator.h"
#include "DolphinQt/Config/Mapping/MappingNumeric.h"
Expand All @@ -28,14 +28,6 @@ MappingWidget::MappingWidget(MappingWindow* parent) : m_parent(parent)
connect(parent, &MappingWindow::Update, this, &MappingWidget::Update);
connect(parent, &MappingWindow::Save, this, &MappingWidget::SaveSettings);
connect(parent, &MappingWindow::ConfigChanged, this, &MappingWidget::ConfigChanged);

const auto timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, [this] {
const auto lock = m_parent->GetController()->GetStateLock();
emit Update();
});

timer->start(1000 / INDICATOR_UPDATE_FREQ);
}

MappingWindow* MappingWidget::GetParent() const
Expand Down Expand Up @@ -138,10 +130,21 @@ QGroupBox* MappingWidget::CreateGroupBox(const QString& name, ControllerEmu::Con
setting_widget =
new MappingBool(this, static_cast<ControllerEmu::NumericSetting<bool>*>(setting.get()));
break;

default:
// FYI: Widgets for additional types can be implemented as needed.
break;
}

if (setting_widget)
form_layout->addRow(tr(setting->GetUIName()), setting_widget);
{
const auto hbox = new QHBoxLayout;

hbox->addWidget(setting_widget);
hbox->addWidget(CreateSettingAdvancedMappingButton(*setting));

form_layout->addRow(tr(setting->GetUIName()), hbox);
}
}

if (group->can_be_disabled)
Expand Down Expand Up @@ -173,3 +176,25 @@ ControllerEmu::EmulatedController* MappingWidget::GetController() const
{
return m_parent->GetController();
}

QPushButton*
MappingWidget::CreateSettingAdvancedMappingButton(ControllerEmu::NumericSettingBase& setting)
{
const auto button = new QPushButton(tr("..."));
button->setFixedWidth(QFontMetrics(font()).boundingRect(button->text()).width() * 2);

button->connect(button, &QPushButton::clicked, [this, &setting]() {
if (setting.IsSimpleValue())
setting.SetExpressionFromValue();

IOWindow io(this, GetController(), &setting.GetInputReference(), IOWindow::Type::Input);
io.exec();

setting.SimplifyIfPossible();

ConfigChanged();
SaveSettings();
});

return button;
}
9 changes: 3 additions & 6 deletions Source/Core/DolphinQt/Config/Mapping/MappingWidget.h
Expand Up @@ -14,24 +14,20 @@ constexpr int WIDGET_MAX_WIDTH = 112;

class ControlGroupBox;
class InputConfig;
class IOWindow;
class MappingButton;
class MappingNumeric;
class MappingWindow;
class QPushButton;
class QGroupBox;

namespace ControllerEmu
{
class Control;
class ControlGroup;
class EmulatedController;
class NumericSettingBase;
} // namespace ControllerEmu

namespace ciface::Core
{
class Device;
} // namespace ciface::Core

constexpr int INDICATOR_UPDATE_FREQ = 30;

class MappingWidget : public QWidget
Expand All @@ -57,6 +53,7 @@ class MappingWidget : public QWidget

QGroupBox* CreateGroupBox(ControllerEmu::ControlGroup* group);
QGroupBox* CreateGroupBox(const QString& name, ControllerEmu::ControlGroup* group);
QPushButton* CreateSettingAdvancedMappingButton(ControllerEmu::NumericSettingBase& setting);

private:
MappingWindow* m_parent;
Expand Down
15 changes: 15 additions & 0 deletions Source/Core/DolphinQt/Config/Mapping/MappingWindow.cpp
Expand Up @@ -11,6 +11,7 @@
#include <QHBoxLayout>
#include <QPushButton>
#include <QTabWidget>
#include <QTimer>
#include <QVBoxLayout>

#include "Core/Core.h"
Expand Down Expand Up @@ -62,6 +63,15 @@ MappingWindow::MappingWindow(QWidget* parent, Type type, int port_num)
ConnectWidgets();
SetMappingType(type);

const auto timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, [this] {
const auto lock = GetController()->GetStateLock();
emit Update();
});

timer->start(1000 / INDICATOR_UPDATE_FREQ);

GetController()->GetStateLock();
emit ConfigChanged();
}

Expand Down Expand Up @@ -235,6 +245,7 @@ void MappingWindow::OnLoadProfilePressed()
m_controller->LoadConfig(ini.GetOrCreateSection("Profile"));
m_controller->UpdateReferences(g_controller_interface);

GetController()->GetStateLock();
emit ConfigChanged();
}

Expand Down Expand Up @@ -426,6 +437,8 @@ void MappingWindow::OnDefaultFieldsPressed()
{
m_controller->LoadDefaults(g_controller_interface);
m_controller->UpdateReferences(g_controller_interface);

GetController()->GetStateLock();
emit ConfigChanged();
emit Save();
}
Expand All @@ -441,6 +454,8 @@ void MappingWindow::OnClearFieldsPressed()
m_controller->SetDefaultDevice(default_device);

m_controller->UpdateReferences(g_controller_interface);

GetController()->GetStateLock();
emit ConfigChanged();
emit Save();
}
Expand Down
41 changes: 34 additions & 7 deletions Source/Core/DolphinQt/Config/Mapping/WiimoteEmuGeneral.cpp
Expand Up @@ -8,6 +8,8 @@
#include <QFormLayout>
#include <QGridLayout>
#include <QGroupBox>
#include <QLabel>
#include <QPushButton>

#include "Core/HW/Wiimote.h"
#include "Core/HW/WiimoteEmu/WiimoteEmu.h"
Expand All @@ -22,7 +24,7 @@ WiimoteEmuGeneral::WiimoteEmuGeneral(MappingWindow* window, WiimoteEmuExtension*
: MappingWidget(window), m_extension_widget(extension)
{
CreateMainLayout();
Connect(window);
Connect();
}

void WiimoteEmuGeneral::CreateMainLayout()
Expand All @@ -45,14 +47,20 @@ void WiimoteEmuGeneral::CreateMainLayout()
Wiimote::GetWiimoteGroup(GetPort(), WiimoteEmu::WiimoteGroup::Attachments);
auto* extension = CreateGroupBox(tr("Extension"), extension_group);
auto* ce_extension = static_cast<ControllerEmu::Attachments*>(extension_group);
m_extension_combo = new QComboBox();

const auto combo_hbox = new QHBoxLayout;
combo_hbox->addWidget(m_extension_combo = new QComboBox());
combo_hbox->addWidget(m_extension_combo_dynamic_indicator = new QLabel(QString::fromUtf8("🎮")));
combo_hbox->addWidget(CreateSettingAdvancedMappingButton(ce_extension->GetSelectionSetting()));

m_extension_combo_dynamic_indicator->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Ignored);

for (const auto& attachment : ce_extension->GetAttachmentList())
m_extension_combo->addItem(tr(attachment->GetDisplayName().c_str()));

extension->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

static_cast<QFormLayout*>(extension->layout())->insertRow(0, m_extension_combo);
static_cast<QFormLayout*>(extension->layout())->insertRow(0, combo_hbox);

layout->addWidget(extension, 0, 3);
layout->addWidget(CreateGroupBox(tr("Rumble"), Wiimote::GetWiimoteGroup(
Expand All @@ -67,27 +75,46 @@ void WiimoteEmuGeneral::CreateMainLayout()
setLayout(layout);
}

void WiimoteEmuGeneral::Connect(MappingWindow* window)
void WiimoteEmuGeneral::Connect()
{
connect(m_extension_combo, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
this, &WiimoteEmuGeneral::OnAttachmentChanged);
connect(window, &MappingWindow::ConfigChanged, this, &WiimoteEmuGeneral::ConfigChanged);
connect(m_extension_combo, QOverload<int>::of(&QComboBox::currentIndexChanged), this,
&WiimoteEmuGeneral::OnAttachmentChanged);
connect(m_extension_combo, QOverload<int>::of(&QComboBox::activated), this,
&WiimoteEmuGeneral::OnAttachmentSelected);
connect(this, &MappingWidget::ConfigChanged, this, &WiimoteEmuGeneral::ConfigChanged);
connect(this, &MappingWidget::Update, this, &WiimoteEmuGeneral::Update);
}

void WiimoteEmuGeneral::OnAttachmentChanged(int extension)
{
GetParent()->ShowExtensionMotionTabs(extension == WiimoteEmu::ExtensionNumber::NUNCHUK);

m_extension_widget->ChangeExtensionType(extension);
}

void WiimoteEmuGeneral::OnAttachmentSelected(int extension)
{
auto* ce_extension = static_cast<ControllerEmu::Attachments*>(
Wiimote::GetWiimoteGroup(GetPort(), WiimoteEmu::WiimoteGroup::Attachments));

ce_extension->SetSelectedAttachment(extension);

ConfigChanged();
SaveSettings();
}

void WiimoteEmuGeneral::ConfigChanged()
{
auto* ce_extension = static_cast<ControllerEmu::Attachments*>(
Wiimote::GetWiimoteGroup(GetPort(), WiimoteEmu::WiimoteGroup::Attachments));

m_extension_combo->setCurrentIndex(ce_extension->GetSelectedAttachment());

m_extension_combo_dynamic_indicator->setVisible(
!ce_extension->GetSelectionSetting().IsSimpleValue());
}

void WiimoteEmuGeneral::Update()
{
auto* ce_extension = static_cast<ControllerEmu::Attachments*>(
Wiimote::GetWiimoteGroup(GetPort(), WiimoteEmu::WiimoteGroup::Attachments));
Expand Down

0 comments on commit 2e25403

Please sign in to comment.