2017-06-13 15:16:41 +00:00
|
|
|
// Copyright 2017 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2+
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2018-07-06 22:40:15 +00:00
|
|
|
#include "DolphinQt/Config/Mapping/IOWindow.h"
|
2017-06-13 15:16:41 +00:00
|
|
|
|
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
#include <QComboBox>
|
|
|
|
#include <QDialogButtonBox>
|
|
|
|
#include <QGroupBox>
|
|
|
|
#include <QHBoxLayout>
|
|
|
|
#include <QLabel>
|
|
|
|
#include <QListWidget>
|
2019-01-27 16:35:29 +00:00
|
|
|
#include <QMessageBox>
|
2017-06-13 15:16:41 +00:00
|
|
|
#include <QPlainTextEdit>
|
|
|
|
#include <QPushButton>
|
|
|
|
#include <QSlider>
|
|
|
|
#include <QSpinBox>
|
2019-03-02 16:10:26 +00:00
|
|
|
#include <QSyntaxHighlighter>
|
2017-06-13 15:16:41 +00:00
|
|
|
#include <QVBoxLayout>
|
|
|
|
|
|
|
|
#include "Core/Core.h"
|
2018-05-28 01:48:04 +00:00
|
|
|
|
2018-07-06 22:40:15 +00:00
|
|
|
#include "DolphinQt/Config/Mapping/MappingCommon.h"
|
|
|
|
#include "DolphinQt/Config/Mapping/MappingWindow.h"
|
|
|
|
#include "DolphinQt/QtUtils/BlockUserInputFilter.h"
|
2018-05-28 01:48:04 +00:00
|
|
|
|
2017-06-13 15:16:41 +00:00
|
|
|
#include "InputCommon/ControlReference/ControlReference.h"
|
2019-03-02 16:10:26 +00:00
|
|
|
#include "InputCommon/ControlReference/ExpressionParser.h"
|
2017-06-13 15:16:41 +00:00
|
|
|
#include "InputCommon/ControllerEmu/ControllerEmu.h"
|
|
|
|
#include "InputCommon/ControllerInterface/ControllerInterface.h"
|
|
|
|
|
|
|
|
constexpr int SLIDER_TICK_COUNT = 100;
|
|
|
|
|
2019-03-02 16:10:26 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
QTextCharFormat GetSpecialCharFormat()
|
|
|
|
{
|
|
|
|
QTextCharFormat format;
|
|
|
|
format.setFontWeight(QFont::Weight::Bold);
|
|
|
|
return format;
|
|
|
|
}
|
|
|
|
|
|
|
|
QTextCharFormat GetOperatorCharFormat()
|
|
|
|
{
|
|
|
|
QTextCharFormat format;
|
|
|
|
format.setFontWeight(QFont::Weight::Bold);
|
|
|
|
format.setForeground(QBrush{Qt::darkBlue});
|
|
|
|
return format;
|
|
|
|
}
|
|
|
|
|
|
|
|
QTextCharFormat GetLiteralCharFormat()
|
|
|
|
{
|
|
|
|
QTextCharFormat format;
|
|
|
|
format.setForeground(QBrush{Qt::darkMagenta});
|
|
|
|
return format;
|
|
|
|
}
|
|
|
|
|
|
|
|
QTextCharFormat GetInvalidCharFormat()
|
|
|
|
{
|
|
|
|
QTextCharFormat format;
|
|
|
|
format.setUnderlineStyle(QTextCharFormat::WaveUnderline);
|
|
|
|
format.setUnderlineColor(Qt::darkRed);
|
|
|
|
return format;
|
|
|
|
}
|
|
|
|
|
|
|
|
QTextCharFormat GetControlCharFormat()
|
|
|
|
{
|
|
|
|
QTextCharFormat format;
|
|
|
|
format.setForeground(QBrush{Qt::darkGreen});
|
|
|
|
return format;
|
|
|
|
}
|
|
|
|
|
|
|
|
QTextCharFormat GetVariableCharFormat()
|
|
|
|
{
|
|
|
|
QTextCharFormat format;
|
|
|
|
format.setForeground(QBrush{Qt::magenta});
|
|
|
|
return format;
|
|
|
|
}
|
|
|
|
|
|
|
|
QTextCharFormat GetFunctionCharFormat()
|
|
|
|
{
|
|
|
|
QTextCharFormat format;
|
|
|
|
format.setForeground(QBrush{Qt::darkCyan});
|
|
|
|
return format;
|
|
|
|
}
|
|
|
|
|
|
|
|
class SyntaxHighlighter : public QSyntaxHighlighter
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
SyntaxHighlighter(QTextDocument* parent) : QSyntaxHighlighter(parent) {}
|
|
|
|
|
|
|
|
void highlightBlock(const QString& text) final override
|
|
|
|
{
|
|
|
|
// TODO: This is going to result in improper highlighting with non-ascii characters:
|
|
|
|
ciface::ExpressionParser::Lexer lexer(text.toStdString());
|
|
|
|
|
|
|
|
std::vector<ciface::ExpressionParser::Token> tokens;
|
|
|
|
lexer.Tokenize(tokens);
|
|
|
|
|
|
|
|
using ciface::ExpressionParser::TokenType;
|
|
|
|
|
|
|
|
for (auto& token : tokens)
|
|
|
|
{
|
|
|
|
switch (token.type)
|
|
|
|
{
|
|
|
|
case TokenType::TOK_INVALID:
|
|
|
|
setFormat(token.string_position, token.string_length, GetInvalidCharFormat());
|
|
|
|
break;
|
|
|
|
case TokenType::TOK_LPAREN:
|
|
|
|
case TokenType::TOK_RPAREN:
|
|
|
|
case TokenType::TOK_COMMA:
|
|
|
|
setFormat(token.string_position, token.string_length, GetSpecialCharFormat());
|
|
|
|
break;
|
|
|
|
case TokenType::TOK_LITERAL:
|
|
|
|
setFormat(token.string_position, token.string_length, GetLiteralCharFormat());
|
|
|
|
break;
|
|
|
|
case TokenType::TOK_CONTROL:
|
|
|
|
setFormat(token.string_position, token.string_length, GetControlCharFormat());
|
|
|
|
break;
|
|
|
|
case TokenType::TOK_FUNCTION:
|
|
|
|
setFormat(token.string_position, token.string_length, GetFunctionCharFormat());
|
|
|
|
break;
|
|
|
|
case TokenType::TOK_VARIABLE:
|
|
|
|
setFormat(token.string_position, token.string_length, GetVariableCharFormat());
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (token.IsBinaryOperator())
|
|
|
|
setFormat(token.string_position, token.string_length, GetOperatorCharFormat());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2017-06-13 15:16:41 +00:00
|
|
|
IOWindow::IOWindow(QWidget* parent, ControllerEmu::EmulatedController* controller,
|
|
|
|
ControlReference* ref, IOWindow::Type type)
|
|
|
|
: QDialog(parent), m_reference(ref), m_controller(controller), m_type(type)
|
|
|
|
{
|
|
|
|
CreateMainLayout();
|
2018-05-05 00:29:16 +00:00
|
|
|
|
2017-06-13 15:16:41 +00:00
|
|
|
setWindowTitle(type == IOWindow::Type::Input ? tr("Configure Input") : tr("Configure Output"));
|
2018-05-05 00:29:16 +00:00
|
|
|
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
|
2017-06-13 15:16:41 +00:00
|
|
|
|
2019-03-15 01:27:49 +00:00
|
|
|
ConfigChanged();
|
2019-01-27 16:35:29 +00:00
|
|
|
|
|
|
|
ConnectWidgets();
|
2017-06-13 15:16:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void IOWindow::CreateMainLayout()
|
|
|
|
{
|
|
|
|
m_main_layout = new QVBoxLayout();
|
|
|
|
|
|
|
|
m_devices_combo = new QComboBox();
|
|
|
|
m_option_list = new QListWidget();
|
|
|
|
m_select_button = new QPushButton(tr("Select"));
|
|
|
|
m_detect_button = new QPushButton(tr("Detect"));
|
|
|
|
m_test_button = new QPushButton(tr("Test"));
|
|
|
|
m_button_box = new QDialogButtonBox();
|
|
|
|
m_clear_button = new QPushButton(tr("Clear"));
|
|
|
|
m_apply_button = new QPushButton(tr("Apply"));
|
|
|
|
m_range_slider = new QSlider(Qt::Horizontal);
|
|
|
|
m_range_spinbox = new QSpinBox();
|
|
|
|
|
2019-03-02 16:10:26 +00:00
|
|
|
m_expression_text = new QPlainTextEdit();
|
|
|
|
m_expression_text->setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont));
|
|
|
|
new SyntaxHighlighter(m_expression_text->document());
|
|
|
|
|
2019-01-27 16:35:29 +00:00
|
|
|
m_operators_combo = new QComboBox();
|
|
|
|
m_operators_combo->addItem(tr("Operators"));
|
|
|
|
m_operators_combo->insertSeparator(1);
|
|
|
|
if (m_type == Type::Input)
|
|
|
|
{
|
|
|
|
m_operators_combo->addItem(tr("! Not"));
|
|
|
|
m_operators_combo->addItem(tr("* Multiply"));
|
|
|
|
m_operators_combo->addItem(tr("/ Divide"));
|
|
|
|
m_operators_combo->addItem(tr("% Modulo"));
|
|
|
|
m_operators_combo->addItem(tr("+ Add"));
|
|
|
|
m_operators_combo->addItem(tr("- Subtract"));
|
|
|
|
m_operators_combo->addItem(tr("> Greater-than"));
|
|
|
|
m_operators_combo->addItem(tr("< Less-than"));
|
|
|
|
m_operators_combo->addItem(tr("& And"));
|
|
|
|
}
|
|
|
|
m_operators_combo->addItem(tr("| Or"));
|
|
|
|
if (m_type == Type::Input)
|
|
|
|
{
|
|
|
|
m_operators_combo->addItem(tr(", Comma"));
|
|
|
|
}
|
|
|
|
|
|
|
|
m_functions_combo = new QComboBox();
|
|
|
|
m_functions_combo->addItem(tr("Functions"));
|
|
|
|
m_functions_combo->insertSeparator(1);
|
|
|
|
m_functions_combo->addItem(QStringLiteral("!if"));
|
|
|
|
m_functions_combo->addItem(QStringLiteral("!sin"));
|
|
|
|
m_functions_combo->addItem(QStringLiteral("!timer"));
|
|
|
|
m_functions_combo->addItem(QStringLiteral("!toggle"));
|
|
|
|
m_functions_combo->addItem(QStringLiteral("!while"));
|
|
|
|
m_functions_combo->addItem(QStringLiteral("!deadzone"));
|
|
|
|
m_functions_combo->addItem(QStringLiteral("!smooth"));
|
|
|
|
m_functions_combo->addItem(QStringLiteral("!hold"));
|
|
|
|
m_functions_combo->addItem(QStringLiteral("!tap"));
|
|
|
|
m_functions_combo->addItem(QStringLiteral("!relative"));
|
|
|
|
m_functions_combo->addItem(QStringLiteral("!pulse"));
|
|
|
|
|
2017-06-13 15:16:41 +00:00
|
|
|
// Devices
|
|
|
|
m_main_layout->addWidget(m_devices_combo);
|
|
|
|
|
|
|
|
// Range
|
|
|
|
auto* range_hbox = new QHBoxLayout();
|
|
|
|
range_hbox->addWidget(new QLabel(tr("Range")));
|
|
|
|
range_hbox->addWidget(m_range_slider);
|
|
|
|
range_hbox->addWidget(m_range_spinbox);
|
|
|
|
m_range_slider->setMinimum(-500);
|
|
|
|
m_range_slider->setMaximum(500);
|
|
|
|
m_range_spinbox->setMinimum(-500);
|
|
|
|
m_range_spinbox->setMaximum(500);
|
2018-05-08 23:54:47 +00:00
|
|
|
m_main_layout->addLayout(range_hbox);
|
2017-06-13 15:16:41 +00:00
|
|
|
|
|
|
|
auto* hbox = new QHBoxLayout();
|
|
|
|
auto* button_vbox = new QVBoxLayout();
|
|
|
|
hbox->addWidget(m_option_list, 8);
|
|
|
|
hbox->addLayout(button_vbox, 1);
|
|
|
|
|
|
|
|
button_vbox->addWidget(m_select_button);
|
|
|
|
button_vbox->addWidget(m_type == Type::Input ? m_detect_button : m_test_button);
|
2019-01-27 16:35:29 +00:00
|
|
|
button_vbox->addWidget(m_operators_combo);
|
2017-06-13 15:16:41 +00:00
|
|
|
if (m_type == Type::Input)
|
|
|
|
{
|
2019-01-27 16:35:29 +00:00
|
|
|
button_vbox->addWidget(m_functions_combo);
|
2017-06-13 15:16:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_main_layout->addLayout(hbox, 2);
|
|
|
|
m_main_layout->addWidget(m_expression_text, 1);
|
|
|
|
|
|
|
|
// Button Box
|
|
|
|
m_main_layout->addWidget(m_button_box);
|
|
|
|
m_button_box->addButton(m_clear_button, QDialogButtonBox::ActionRole);
|
|
|
|
m_button_box->addButton(m_apply_button, QDialogButtonBox::ActionRole);
|
|
|
|
m_button_box->addButton(QDialogButtonBox::Ok);
|
|
|
|
|
|
|
|
setLayout(m_main_layout);
|
|
|
|
}
|
|
|
|
|
2019-03-15 01:27:49 +00:00
|
|
|
void IOWindow::ConfigChanged()
|
2017-06-13 15:16:41 +00:00
|
|
|
{
|
2019-04-27 15:51:57 +00:00
|
|
|
const QSignalBlocker blocker(this);
|
2019-03-15 01:27:49 +00:00
|
|
|
|
2017-06-08 02:02:16 +00:00
|
|
|
m_expression_text->setPlainText(QString::fromStdString(m_reference->GetExpression()));
|
2018-09-03 16:03:26 +00:00
|
|
|
m_expression_text->moveCursor(QTextCursor::End, QTextCursor::MoveAnchor);
|
2017-06-13 15:16:41 +00:00
|
|
|
m_range_spinbox->setValue(m_reference->range * SLIDER_TICK_COUNT);
|
|
|
|
m_range_slider->setValue(m_reference->range * SLIDER_TICK_COUNT);
|
|
|
|
|
2017-11-04 22:29:15 +00:00
|
|
|
m_devq = m_controller->GetDefaultDevice();
|
2017-06-13 15:16:41 +00:00
|
|
|
|
|
|
|
UpdateDeviceList();
|
|
|
|
UpdateOptionList();
|
|
|
|
}
|
|
|
|
|
|
|
|
void IOWindow::ConnectWidgets()
|
|
|
|
{
|
2019-01-27 16:35:29 +00:00
|
|
|
connect(m_select_button, &QPushButton::clicked, [this] { AppendSelectedOption(); });
|
2017-06-13 15:16:41 +00:00
|
|
|
|
2019-02-28 00:10:18 +00:00
|
|
|
connect(m_detect_button, &QPushButton::clicked, this, &IOWindow::OnDetectButtonPressed);
|
|
|
|
connect(m_test_button, &QPushButton::clicked, this, &IOWindow::OnTestButtonPressed);
|
2017-06-13 15:16:41 +00:00
|
|
|
|
|
|
|
connect(m_button_box, &QDialogButtonBox::clicked, this, &IOWindow::OnDialogButtonPressed);
|
|
|
|
connect(m_devices_combo, &QComboBox::currentTextChanged, this, &IOWindow::OnDeviceChanged);
|
|
|
|
connect(m_range_spinbox, static_cast<void (QSpinBox::*)(int value)>(&QSpinBox::valueChanged),
|
|
|
|
this, &IOWindow::OnRangeChanged);
|
|
|
|
connect(m_range_slider, static_cast<void (QSlider::*)(int value)>(&QSlider::valueChanged), this,
|
|
|
|
&IOWindow::OnRangeChanged);
|
2019-01-27 16:35:29 +00:00
|
|
|
|
|
|
|
connect(m_expression_text, &QPlainTextEdit::textChanged, [this] {
|
|
|
|
m_apply_button->setText(m_apply_button->text().remove(QStringLiteral("*")));
|
|
|
|
m_apply_button->setText(m_apply_button->text() + QStringLiteral("*"));
|
|
|
|
});
|
|
|
|
|
|
|
|
connect(m_operators_combo, QOverload<int>::of(&QComboBox::activated), [this](int index) {
|
|
|
|
if (0 == index)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_expression_text->insertPlainText(m_operators_combo->currentText().left(1));
|
|
|
|
|
|
|
|
m_operators_combo->setCurrentIndex(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
connect(m_functions_combo, QOverload<int>::of(&QComboBox::activated), [this](int index) {
|
|
|
|
if (0 == index)
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_expression_text->insertPlainText(m_functions_combo->currentText() + QStringLiteral("()"));
|
|
|
|
|
|
|
|
m_functions_combo->setCurrentIndex(0);
|
|
|
|
});
|
2017-06-13 15:16:41 +00:00
|
|
|
}
|
|
|
|
|
2019-01-27 16:35:29 +00:00
|
|
|
void IOWindow::AppendSelectedOption()
|
2017-06-13 15:16:41 +00:00
|
|
|
{
|
|
|
|
if (m_option_list->currentItem() == nullptr)
|
|
|
|
return;
|
|
|
|
|
2019-01-27 16:35:29 +00:00
|
|
|
m_expression_text->insertPlainText(MappingCommon::GetExpressionForControl(
|
|
|
|
m_option_list->currentItem()->text(), m_devq, m_controller->GetDefaultDevice()));
|
2017-06-13 15:16:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void IOWindow::OnDeviceChanged(const QString& device)
|
|
|
|
{
|
|
|
|
m_devq.FromString(device.toStdString());
|
|
|
|
UpdateOptionList();
|
|
|
|
}
|
|
|
|
|
|
|
|
void IOWindow::OnDialogButtonPressed(QAbstractButton* button)
|
|
|
|
{
|
|
|
|
if (button == m_clear_button)
|
|
|
|
{
|
|
|
|
m_expression_text->clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-08 02:02:16 +00:00
|
|
|
m_reference->SetExpression(m_expression_text->toPlainText().toStdString());
|
2019-01-27 16:35:29 +00:00
|
|
|
m_controller->UpdateSingleControlReference(g_controller_interface, m_reference);
|
|
|
|
|
|
|
|
m_apply_button->setText(m_apply_button->text().remove(QStringLiteral("*")));
|
|
|
|
|
|
|
|
if (ciface::ExpressionParser::ParseStatus::SyntaxError == m_reference->GetParseStatus())
|
|
|
|
{
|
|
|
|
QMessageBox error(this);
|
|
|
|
error.setIcon(QMessageBox::Critical);
|
|
|
|
error.setWindowTitle(tr("Error"));
|
|
|
|
error.setText(tr("The expression contains a syntax error."));
|
|
|
|
error.setWindowModality(Qt::WindowModal);
|
|
|
|
error.exec();
|
|
|
|
}
|
2017-06-13 15:16:41 +00:00
|
|
|
|
|
|
|
if (button != m_apply_button)
|
|
|
|
accept();
|
|
|
|
}
|
|
|
|
|
|
|
|
void IOWindow::OnDetectButtonPressed()
|
|
|
|
{
|
2019-02-28 00:10:18 +00:00
|
|
|
const auto expression =
|
|
|
|
MappingCommon::DetectExpression(m_detect_button, g_controller_interface, {m_devq.ToString()},
|
|
|
|
m_devq, MappingCommon::Quote::Off);
|
2017-06-13 15:16:41 +00:00
|
|
|
|
2019-02-28 00:10:18 +00:00
|
|
|
if (expression.isEmpty())
|
|
|
|
return;
|
2017-06-13 15:16:41 +00:00
|
|
|
|
2019-02-28 00:10:18 +00:00
|
|
|
const auto list = m_option_list->findItems(expression, Qt::MatchFixedString);
|
2017-06-13 15:16:41 +00:00
|
|
|
|
2019-02-28 00:10:18 +00:00
|
|
|
if (!list.empty())
|
|
|
|
m_option_list->setCurrentItem(list[0]);
|
|
|
|
}
|
2017-06-27 06:23:22 +00:00
|
|
|
|
2019-02-28 00:10:18 +00:00
|
|
|
void IOWindow::OnTestButtonPressed()
|
|
|
|
{
|
|
|
|
MappingCommon::TestOutput(m_test_button, static_cast<OutputReference*>(m_reference));
|
2017-06-13 15:16:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void IOWindow::OnRangeChanged(int value)
|
|
|
|
{
|
|
|
|
m_reference->range = static_cast<double>(value) / SLIDER_TICK_COUNT;
|
|
|
|
m_range_spinbox->setValue(m_reference->range * SLIDER_TICK_COUNT);
|
|
|
|
m_range_slider->setValue(m_reference->range * SLIDER_TICK_COUNT);
|
|
|
|
}
|
|
|
|
|
|
|
|
void IOWindow::UpdateOptionList()
|
|
|
|
{
|
|
|
|
m_option_list->clear();
|
|
|
|
|
|
|
|
const auto device = g_controller_interface.FindDevice(m_devq);
|
|
|
|
|
2018-05-10 19:12:19 +00:00
|
|
|
if (device == nullptr)
|
|
|
|
return;
|
|
|
|
|
2017-06-13 15:16:41 +00:00
|
|
|
if (m_reference->IsInput())
|
|
|
|
{
|
|
|
|
for (const auto* input : device->Inputs())
|
|
|
|
{
|
|
|
|
m_option_list->addItem(QString::fromStdString(input->GetName()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (const auto* output : device->Outputs())
|
|
|
|
{
|
|
|
|
m_option_list->addItem(QString::fromStdString(output->GetName()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void IOWindow::UpdateDeviceList()
|
|
|
|
{
|
|
|
|
m_devices_combo->clear();
|
|
|
|
|
2019-03-15 01:27:49 +00:00
|
|
|
for (const auto& name : g_controller_interface.GetAllDeviceStrings())
|
|
|
|
m_devices_combo->addItem(QString::fromStdString(name));
|
2017-06-13 15:16:41 +00:00
|
|
|
|
2019-03-15 01:27:49 +00:00
|
|
|
m_devices_combo->setCurrentText(
|
|
|
|
QString::fromStdString(m_controller->GetDefaultDevice().ToString()));
|
2017-06-13 15:16:41 +00:00
|
|
|
}
|