Skip to content

Commit

Permalink
Merge pull request #8396 from jordan-woyak/mapping-input-list-states
Browse files Browse the repository at this point in the history
Qt/Mapping: Show current input states in advanced dialog.
  • Loading branch information
leoetlino committed Nov 8, 2019
2 parents 0ff99b3 + 40d1598 commit d8c62b5
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 30 deletions.
139 changes: 118 additions & 21 deletions Source/Core/DolphinQt/Config/Mapping/IOWindow.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -11,21 +11,26 @@
#include <QDialogButtonBox> #include <QDialogButtonBox>
#include <QGroupBox> #include <QGroupBox>
#include <QHBoxLayout> #include <QHBoxLayout>
#include <QHeaderView>
#include <QItemDelegate>
#include <QLabel> #include <QLabel>
#include <QLineEdit> #include <QLineEdit>
#include <QListWidget> #include <QPainter>
#include <QMessageBox>
#include <QPlainTextEdit> #include <QPlainTextEdit>
#include <QPushButton> #include <QPushButton>
#include <QSlider> #include <QSlider>
#include <QSpinBox> #include <QSpinBox>
#include <QTableWidget>
#include <QVBoxLayout> #include <QVBoxLayout>


#include "Core/Core.h" #include "Core/Core.h"


#include "DolphinQt/Config/Mapping/MappingCommon.h" #include "DolphinQt/Config/Mapping/MappingCommon.h"
#include "DolphinQt/Config/Mapping/MappingIndicator.h"
#include "DolphinQt/Config/Mapping/MappingWidget.h"
#include "DolphinQt/Config/Mapping/MappingWindow.h" #include "DolphinQt/Config/Mapping/MappingWindow.h"
#include "DolphinQt/QtUtils/BlockUserInputFilter.h" #include "DolphinQt/QtUtils/BlockUserInputFilter.h"
#include "DolphinQt/QtUtils/ModalMessageBox.h"


#include "InputCommon/ControlReference/ControlReference.h" #include "InputCommon/ControlReference/ControlReference.h"
#include "InputCommon/ControlReference/ExpressionParser.h" #include "InputCommon/ControlReference/ExpressionParser.h"
Expand Down Expand Up @@ -184,12 +189,26 @@ void ControlExpressionSyntaxHighlighter::highlightBlock(const QString&)
} }
} }


IOWindow::IOWindow(QWidget* parent, ControllerEmu::EmulatedController* controller, class InputStateDelegate : public QItemDelegate
{
public:
explicit InputStateDelegate(IOWindow* parent);

void paint(QPainter* painter, const QStyleOptionViewItem& option,
const QModelIndex& index) const override;

private:
IOWindow* m_parent;
};

IOWindow::IOWindow(MappingWidget* parent, ControllerEmu::EmulatedController* controller,
ControlReference* ref, IOWindow::Type type) ControlReference* ref, IOWindow::Type type)
: QDialog(parent), m_reference(ref), m_controller(controller), m_type(type) : QDialog(parent), m_reference(ref), m_controller(controller), m_type(type)
{ {
CreateMainLayout(); CreateMainLayout();


connect(parent, &MappingWidget::Update, this, &IOWindow::Update);

setWindowTitle(type == IOWindow::Type::Input ? tr("Configure Input") : tr("Configure Output")); setWindowTitle(type == IOWindow::Type::Input ? tr("Configure Input") : tr("Configure Output"));
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint); setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);


Expand All @@ -198,15 +217,20 @@ IOWindow::IOWindow(QWidget* parent, ControllerEmu::EmulatedController* controlle
ConnectWidgets(); ConnectWidgets();
} }


std::shared_ptr<ciface::Core::Device> IOWindow::GetSelectedDevice()
{
return m_selected_device;
}

void IOWindow::CreateMainLayout() void IOWindow::CreateMainLayout()
{ {
m_main_layout = new QVBoxLayout(); m_main_layout = new QVBoxLayout();


m_devices_combo = new QComboBox(); m_devices_combo = new QComboBox();
m_option_list = new QListWidget(); m_option_list = new QTableWidget();
m_select_button = new QPushButton(tr("Select")); m_select_button = new QPushButton(tr("Select"));
m_detect_button = new QPushButton(tr("Detect")); m_detect_button = new QPushButton(tr("Detect"), this);
m_test_button = new QPushButton(tr("Test")); m_test_button = new QPushButton(tr("Test"), this);
m_button_box = new QDialogButtonBox(); m_button_box = new QDialogButtonBox();
m_clear_button = new QPushButton(tr("Clear")); m_clear_button = new QPushButton(tr("Clear"));
m_apply_button = new QPushButton(tr("Apply")); m_apply_button = new QPushButton(tr("Apply"));
Expand Down Expand Up @@ -242,7 +266,7 @@ void IOWindow::CreateMainLayout()
m_operators_combo->addItem(tr(", Comma")); m_operators_combo->addItem(tr(", Comma"));
} }


m_functions_combo = new QComboBox(); m_functions_combo = new QComboBox(this);
m_functions_combo->addItem(tr("Functions")); m_functions_combo->addItem(tr("Functions"));
m_functions_combo->insertSeparator(1); m_functions_combo->insertSeparator(1);
m_functions_combo->addItem(QStringLiteral("if")); m_functions_combo->addItem(QStringLiteral("if"));
Expand All @@ -269,6 +293,30 @@ void IOWindow::CreateMainLayout()
m_range_spinbox->setMaximum(500); m_range_spinbox->setMaximum(500);
m_main_layout->addLayout(range_hbox); m_main_layout->addLayout(range_hbox);


// Options (Buttons, Outputs) and action buttons

if (m_type == Type::Input)
{
m_option_list->setColumnCount(2);
m_option_list->setColumnWidth(1, 64);
m_option_list->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Fixed);

m_option_list->setItemDelegate(new InputStateDelegate(this));
}
else
{
m_option_list->setColumnCount(1);
}

m_option_list->horizontalHeader()->hide();
m_option_list->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
m_option_list->verticalHeader()->hide();
m_option_list->verticalHeader()->setDefaultSectionSize(
m_option_list->verticalHeader()->minimumSectionSize());
m_option_list->setEditTriggers(QAbstractItemView::NoEditTriggers);
m_option_list->setSelectionBehavior(QAbstractItemView::SelectRows);
m_option_list->setSelectionMode(QAbstractItemView::SingleSelection);

auto* hbox = new QHBoxLayout(); auto* hbox = new QHBoxLayout();
auto* button_vbox = new QVBoxLayout(); auto* button_vbox = new QVBoxLayout();
hbox->addWidget(m_option_list, 8); hbox->addWidget(m_option_list, 8);
Expand Down Expand Up @@ -310,6 +358,11 @@ void IOWindow::ConfigChanged()
UpdateOptionList(); UpdateOptionList();
} }


void IOWindow::Update()
{
m_option_list->viewport()->update();
}

void IOWindow::ConnectWidgets() void IOWindow::ConnectWidgets()
{ {
connect(m_select_button, &QPushButton::clicked, [this] { AppendSelectedOption(); }); connect(m_select_button, &QPushButton::clicked, [this] { AppendSelectedOption(); });
Expand Down Expand Up @@ -378,12 +431,7 @@ void IOWindow::OnDialogButtonPressed(QAbstractButton* button)


if (ciface::ExpressionParser::ParseStatus::SyntaxError == m_reference->GetParseStatus()) if (ciface::ExpressionParser::ParseStatus::SyntaxError == m_reference->GetParseStatus())
{ {
QMessageBox error(this); ModalMessageBox::warning(this, tr("Error"), tr("The expression contains a syntax error."));
error.setIcon(QMessageBox::Critical);
error.setWindowTitle(tr("Error"));
error.setText(tr("The expression contains a syntax error."));
error.setWindowModality(Qt::WindowModal);
error.exec();
} }


if (button != m_apply_button) if (button != m_apply_button)
Expand Down Expand Up @@ -419,25 +467,32 @@ void IOWindow::OnRangeChanged(int value)


void IOWindow::UpdateOptionList() void IOWindow::UpdateOptionList()
{ {
m_option_list->clear(); m_selected_device = g_controller_interface.FindDevice(m_devq);

m_option_list->setRowCount(0);
const auto device = g_controller_interface.FindDevice(m_devq);


if (device == nullptr) if (m_selected_device == nullptr)
return; return;


if (m_reference->IsInput()) if (m_reference->IsInput())
{ {
for (const auto* input : device->Inputs()) int row = 0;
for (const auto* input : m_selected_device->Inputs())
{ {
m_option_list->addItem(QString::fromStdString(input->GetName())); m_option_list->insertRow(row);
m_option_list->setItem(row, 0,
new QTableWidgetItem(QString::fromStdString(input->GetName())));
++row;
} }
} }
else else
{ {
for (const auto* output : device->Outputs()) int row = 0;
for (const auto* output : m_selected_device->Outputs())
{ {
m_option_list->addItem(QString::fromStdString(output->GetName())); m_option_list->insertRow(row);
m_option_list->setItem(row, 0,
new QTableWidgetItem(QString::fromStdString(output->GetName())));
++row;
} }
} }
} }
Expand All @@ -452,3 +507,45 @@ void IOWindow::UpdateDeviceList()
m_devices_combo->setCurrentText( m_devices_combo->setCurrentText(
QString::fromStdString(m_controller->GetDefaultDevice().ToString())); QString::fromStdString(m_controller->GetDefaultDevice().ToString()));
} }

InputStateDelegate::InputStateDelegate(IOWindow* parent) : QItemDelegate(parent), m_parent(parent)
{
}

void InputStateDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option,
const QModelIndex& index) const
{
QItemDelegate::paint(painter, option, index);

// Don't do anything special for the first column.
if (index.column() == 0)
return;

// Clamp off negative values but allow greater than one in the text display.
const auto state =
std::max(m_parent->GetSelectedDevice()->Inputs()[index.row()]->GetState(), 0.0);
const auto state_str = QString::number(state, 'g', 4);

QRect rect = option.rect;
rect.setWidth(rect.width() * std::clamp(state, 0.0, 1.0));

// Create a temporary indicator object to retreive color constants.
MappingIndicator indicator(nullptr);

painter->save();

// Normal text.
painter->setPen(indicator.GetTextColor());
painter->drawText(option.rect, Qt::AlignCenter, state_str);

// Input state meter.
painter->fillRect(rect, indicator.GetAdjustedInputColor());

// Text on top of meter.
painter->setPen(indicator.GetAltTextColor());
painter->setClipping(true);
painter->setClipRect(rect);
painter->drawText(option.rect, Qt::AlignCenter, state_str);

painter->restore();
}
11 changes: 8 additions & 3 deletions Source/Core/DolphinQt/Config/Mapping/IOWindow.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
#include "InputCommon/ControllerInterface/Device.h" #include "InputCommon/ControllerInterface/Device.h"


class ControlReference; class ControlReference;
class MappingWidget;
class QAbstractButton; class QAbstractButton;
class QComboBox; class QComboBox;
class QDialogButtonBox; class QDialogButtonBox;
class QLineEdit; class QLineEdit;
class QListWidget; class QTableWidget;
class QVBoxLayout; class QVBoxLayout;
class QWidget; class QWidget;
class QPlainTextEdit; class QPlainTextEdit;
Expand Down Expand Up @@ -52,13 +53,16 @@ class IOWindow final : public QDialog
Output Output
}; };


explicit IOWindow(QWidget* parent, ControllerEmu::EmulatedController* m_controller, explicit IOWindow(MappingWidget* parent, ControllerEmu::EmulatedController* m_controller,
ControlReference* ref, Type type); ControlReference* ref, Type type);


std::shared_ptr<ciface::Core::Device> GetSelectedDevice();

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


void OnDialogButtonPressed(QAbstractButton* button); void OnDialogButtonPressed(QAbstractButton* button);
void OnDeviceChanged(const QString& device); void OnDeviceChanged(const QString& device);
Expand All @@ -77,7 +81,7 @@ class IOWindow final : public QDialog
QComboBox* m_devices_combo; QComboBox* m_devices_combo;


// Options // Options
QListWidget* m_option_list; QTableWidget* m_option_list;


// Range // Range
QSlider* m_range_slider; QSlider* m_range_slider;
Expand Down Expand Up @@ -108,4 +112,5 @@ class IOWindow final : public QDialog


ciface::Core::DeviceQualifier m_devq; ciface::Core::DeviceQualifier m_devq;
Type m_type; Type m_type;
std::shared_ptr<ciface::Core::Device> m_selected_device;
}; };
2 changes: 1 addition & 1 deletion Source/Core/DolphinQt/Config/Mapping/MappingButton.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ MappingButton::MappingButton(MappingWidget* parent, ControlReference* ref, bool


void MappingButton::AdvancedPressed() void MappingButton::AdvancedPressed()
{ {
IOWindow io(this, m_parent->GetController(), m_reference, IOWindow io(m_parent, m_parent->GetController(), m_reference,
m_reference->IsInput() ? IOWindow::Type::Input : IOWindow::Type::Output); m_reference->IsInput() ? IOWindow::Type::Input : IOWindow::Type::Output);
io.exec(); io.exec();


Expand Down
2 changes: 1 addition & 1 deletion Source/Core/DolphinQt/Config/Mapping/MappingIndicator.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ MappingIndicator::MappingIndicator(ControllerEmu::ControlGroup* group) : m_group
// TODO: Make these magic numbers less ugly. // TODO: Make these magic numbers less ugly.
int required_height = 106; int required_height = 106;


if (ControllerEmu::GroupType::MixedTriggers == group->type) if (group && ControllerEmu::GroupType::MixedTriggers == group->type)
required_height = 64 + 1; required_height = 64 + 1;


setFixedHeight(required_height); setFixedHeight(required_height);
Expand Down
6 changes: 3 additions & 3 deletions Source/Core/DolphinQt/Config/Mapping/MappingIndicator.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ class MappingIndicator : public QWidget


void SetCalibrationWidget(CalibrationWidget* widget); void SetCalibrationWidget(CalibrationWidget* widget);


protected:
WiimoteEmu::MotionState m_motion_state{};

QPen GetBBoxPen() const; QPen GetBBoxPen() const;
QBrush GetBBoxBrush() const; QBrush GetBBoxBrush() const;
QColor GetRawInputColor() const; QColor GetRawInputColor() const;
Expand All @@ -49,8 +46,11 @@ class MappingIndicator : public QWidget
QColor GetAltTextColor() const; QColor GetAltTextColor() const;
QColor GetGateColor() const; QColor GetGateColor() const;


protected:
double GetScale() const; double GetScale() const;


WiimoteEmu::MotionState m_motion_state{};

private: private:
void DrawCursor(ControllerEmu::Cursor& cursor); void DrawCursor(ControllerEmu::Cursor& cursor);
void DrawReshapableInput(ControllerEmu::ReshapableInput& stick); void DrawReshapableInput(ControllerEmu::ReshapableInput& stick);
Expand Down
1 change: 0 additions & 1 deletion Source/Core/DolphinQt/Config/Mapping/MappingWidget.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include <QPushButton> #include <QPushButton>
#include <QTimer> #include <QTimer>


#include "DolphinQt/Config/Mapping/IOWindow.h"
#include "DolphinQt/Config/Mapping/MappingButton.h" #include "DolphinQt/Config/Mapping/MappingButton.h"
#include "DolphinQt/Config/Mapping/MappingIndicator.h" #include "DolphinQt/Config/Mapping/MappingIndicator.h"
#include "DolphinQt/Config/Mapping/MappingNumeric.h" #include "DolphinQt/Config/Mapping/MappingNumeric.h"
Expand Down

0 comments on commit d8c62b5

Please sign in to comment.