Skip to content

Commit

Permalink
InputCommon: Allow controller settings specified with input expresions.
Browse files Browse the repository at this point in the history
  • Loading branch information
jordan-woyak committed Oct 19, 2019
1 parent 4a613da commit 3ebb3b6
Show file tree
Hide file tree
Showing 12 changed files with 163 additions and 25 deletions.
4 changes: 1 addition & 3 deletions Source/Core/DolphinQt/Config/Mapping/MappingButton.cpp
Expand Up @@ -122,11 +122,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
45 changes: 42 additions & 3 deletions Source/Core/DolphinQt/Config/Mapping/MappingNumeric.cpp
Expand Up @@ -12,11 +12,8 @@
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));

Expand All @@ -26,10 +23,12 @@ MappingDouble::MappingDouble(MappingWidget* parent, ControllerEmu::NumericSettin
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,22 +40,62 @@ void MappingDouble::fixup(QString& input) const
void MappingDouble::ConfigChanged()
{
const QSignalBlocker blocker(this);

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

setValue(m_setting.GetValue());
}

void MappingDouble::Update()
{
if (!m_setting.IsSimpleValue() && !hasFocus())
{
const QSignalBlocker blocker(this);
setValue(m_setting.GetValue());
}
}

MappingBool::MappingBool(MappingWidget* parent, ControllerEmu::NumericSetting<bool>* setting)
: QCheckBox(parent), m_setting(*setting)
{
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);
}

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())
{
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;
};
28 changes: 25 additions & 3 deletions Source/Core/DolphinQt/Config/Mapping/MappingWidget.cpp
Expand Up @@ -113,8 +113,6 @@ QGroupBox* MappingWidget::CreateGroupBox(const QString& name, ControllerEmu::Con
const QString translated_name =
translate ? tr(control->ui_name.c_str()) : QString::fromStdString(control->ui_name);
form_layout->addRow(translated_name, button);

m_buttons.push_back(button);
}

for (auto& setting : group->numeric_settings)
Expand All @@ -135,7 +133,31 @@ QGroupBox* MappingWidget::CreateGroupBox(const QString& name, ControllerEmu::Con
}

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

const auto advanced_button = new QPushButton(tr("..."));
advanced_button->setFixedWidth(
QFontMetrics(font()).boundingRect(advanced_button->text()).width() * 2);

hbox->addWidget(advanced_button);

advanced_button->connect(
advanced_button, &QPushButton::clicked, [this, &setting = *setting.get()]() {
setting.SetExpressionFromValue();

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

setting.SimplifyIfPossible();

ConfigChanged();
SaveSettings();
});

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

return group_box;
Expand Down
1 change: 0 additions & 1 deletion Source/Core/DolphinQt/Config/Mapping/MappingWidget.h
Expand Up @@ -61,5 +61,4 @@ class MappingWidget : public QWidget
private:
MappingWindow* m_parent;
bool m_first = true;
std::vector<MappingButton*> m_buttons;
};
16 changes: 16 additions & 0 deletions Source/Core/InputCommon/ControlReference/ControlReference.h
Expand Up @@ -7,6 +7,7 @@
#include <memory>

#include "InputCommon/ControlReference/ExpressionParser.h"
#include "InputCommon/ControlReference/FunctionExpression.h"
#include "InputCommon/ControllerInterface/Device.h"

// ControlReference
Expand All @@ -28,6 +29,9 @@ class ControlReference
virtual ControlState State(const ControlState state = 0) = 0;
virtual bool IsInput() const = 0;

template <typename T>
T GetState();

int BoundCount() const;
ciface::ExpressionParser::ParseStatus GetParseStatus() const;
void UpdateReference(ciface::ExpressionParser::ControlEnvironment& env);
Expand All @@ -43,6 +47,18 @@ class ControlReference
ciface::ExpressionParser::ParseStatus m_parse_status;
};

template <>
inline bool ControlReference::GetState<bool>()
{
return State() > ciface::ExpressionParser::CONDITION_THRESHOLD;
}

template <typename T>
T ControlReference::GetState()
{
return State();
}

//
// InputReference
//
Expand Down
Expand Up @@ -12,7 +12,6 @@ namespace ciface
namespace ExpressionParser
{
constexpr int LOOP_MAX_REPS = 10000;
constexpr ControlState CONDITION_THRESHOLD = 0.5;

using Clock = std::chrono::steady_clock;
using FSec = std::chrono::duration<ControlState>;
Expand Down
Expand Up @@ -10,12 +10,13 @@
#include <vector>

#include "InputCommon/ControlReference/ExpressionParser.h"
#include "InputCommon/ControlReference/FunctionExpression.h"

namespace ciface
{
namespace ExpressionParser
{
constexpr ControlState CONDITION_THRESHOLD = 0.5;

class FunctionExpression : public Expression
{
public:
Expand Down
4 changes: 1 addition & 3 deletions Source/Core/InputCommon/ControllerEmu/ControlGroup/Buttons.h
Expand Up @@ -24,13 +24,11 @@ class Buttons : public ControlGroup
{
for (auto& control : controls)
{
if (control->control_ref->State() > ACTIVATION_THRESHOLD)
if (control->control_ref->GetState<bool>())
*buttons |= *bitmasks;

bitmasks++;
}
}

static constexpr ControlState ACTIVATION_THRESHOLD = 0.5;
};
} // namespace ControllerEmu
Expand Up @@ -35,19 +35,19 @@ void ModifySettingsButton::GetState()
{
for (size_t i = 0; i < controls.size(); ++i)
{
ControlState state = controls[i]->control_ref->State();
const bool state = controls[i]->control_ref->GetState<bool>();

if (!associated_settings_toggle[i])
{
// not toggled
associated_settings[i] = state > ACTIVATION_THRESHOLD;
associated_settings[i] = state;
}
else
{
// toggle (loading savestates does not en-/disable toggle)
// after we passed the threshold, we en-/disable. but after that, we don't change it
// anymore
if (!threshold_exceeded[i] && state > ACTIVATION_THRESHOLD)
if (!threshold_exceeded[i] && state)
{
associated_settings[i] = !associated_settings[i];

Expand All @@ -59,7 +59,7 @@ void ModifySettingsButton::GetState()
threshold_exceeded[i] = true;
}

if (state < ACTIVATION_THRESHOLD)
if (!state)
threshold_exceeded[i] = false;
}
}
Expand Down
4 changes: 4 additions & 0 deletions Source/Core/InputCommon/ControllerEmu/ControllerEmu.cpp
Expand Up @@ -14,6 +14,7 @@
#include "InputCommon/ControllerEmu/Control/Control.h"
#include "InputCommon/ControllerEmu/ControlGroup/Attachments.h"
#include "InputCommon/ControllerEmu/ControlGroup/ControlGroup.h"
#include "InputCommon/ControllerEmu/Setting/NumericSetting.h"
#include "InputCommon/ControllerInterface/ControllerInterface.h"

namespace ControllerEmu
Expand Down Expand Up @@ -54,6 +55,9 @@ void EmulatedController::UpdateReferences(ciface::ExpressionParser::ControlEnvir
for (auto& control : ctrlGroup->controls)
control->control_ref->UpdateReference(env);

for (auto& setting : ctrlGroup->numeric_settings)
setting->UpdateReference(env);

// Attachments:
if (ctrlGroup->type == GroupType::Attachments)
{
Expand Down

0 comments on commit 3ebb3b6

Please sign in to comment.