2018-07-07 05:51:34 +00:00
|
|
|
// Copyright 2018 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2018-07-07 05:51:34 +00:00
|
|
|
|
2020-10-18 11:47:00 +00:00
|
|
|
#include "DolphinQt/TAS/TASInputWindow.h"
|
|
|
|
|
2018-07-07 05:51:34 +00:00
|
|
|
#include <cmath>
|
2021-03-21 21:42:33 +00:00
|
|
|
#include <utility>
|
2018-07-07 05:51:34 +00:00
|
|
|
|
|
|
|
#include <QCheckBox>
|
|
|
|
#include <QGroupBox>
|
|
|
|
#include <QHBoxLayout>
|
|
|
|
#include <QLabel>
|
|
|
|
#include <QShortcut>
|
|
|
|
#include <QSlider>
|
|
|
|
#include <QSpinBox>
|
|
|
|
#include <QVBoxLayout>
|
|
|
|
|
|
|
|
#include "Common/CommonTypes.h"
|
|
|
|
|
|
|
|
#include "DolphinQt/QtUtils/AspectRatioWidget.h"
|
|
|
|
#include "DolphinQt/QtUtils/QueueOnObject.h"
|
2020-02-17 14:07:18 +00:00
|
|
|
#include "DolphinQt/Resources.h"
|
2018-07-07 05:51:34 +00:00
|
|
|
#include "DolphinQt/TAS/StickWidget.h"
|
2019-03-31 02:49:57 +00:00
|
|
|
#include "DolphinQt/TAS/TASCheckBox.h"
|
2020-10-18 11:47:00 +00:00
|
|
|
#include "DolphinQt/TAS/TASSlider.h"
|
2023-03-07 18:22:09 +00:00
|
|
|
#include "DolphinQt/TAS/TASSpinBox.h"
|
2018-07-07 05:51:34 +00:00
|
|
|
|
2021-03-21 21:42:33 +00:00
|
|
|
#include "InputCommon/ControllerEmu/ControllerEmu.h"
|
|
|
|
#include "InputCommon/ControllerEmu/StickGate.h"
|
|
|
|
|
|
|
|
void InputOverrider::AddFunction(std::string_view group_name, std::string_view control_name,
|
|
|
|
OverrideFunction function)
|
|
|
|
{
|
|
|
|
m_functions.emplace(std::make_pair(group_name, control_name), std::move(function));
|
|
|
|
}
|
|
|
|
|
|
|
|
ControllerEmu::InputOverrideFunction InputOverrider::GetInputOverrideFunction() const
|
|
|
|
{
|
|
|
|
return [this](std::string_view group_name, std::string_view control_name,
|
|
|
|
ControlState controller_state) {
|
|
|
|
const auto it = m_functions.find(std::make_pair(group_name, control_name));
|
|
|
|
return it != m_functions.end() ? it->second(controller_state) : std::nullopt;
|
|
|
|
};
|
|
|
|
}
|
2018-07-07 05:51:34 +00:00
|
|
|
|
|
|
|
TASInputWindow::TASInputWindow(QWidget* parent) : QDialog(parent)
|
|
|
|
{
|
|
|
|
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
|
2020-02-17 14:07:18 +00:00
|
|
|
setWindowIcon(Resources::GetAppIcon());
|
|
|
|
|
2020-06-30 19:07:25 +00:00
|
|
|
QGridLayout* settings_layout = new QGridLayout;
|
|
|
|
|
2023-07-28 03:36:18 +00:00
|
|
|
m_use_controller = new QCheckBox(tr("Enable Controller Inpu&t"));
|
2018-07-07 05:51:34 +00:00
|
|
|
m_use_controller->setToolTip(tr("Warning: Analog inputs may reset to controller values at "
|
|
|
|
"random. In some cases this can be fixed by adding a deadzone."));
|
2020-06-30 19:07:25 +00:00
|
|
|
settings_layout->addWidget(m_use_controller, 0, 0, 1, 2);
|
|
|
|
|
|
|
|
QLabel* turbo_press_label = new QLabel(tr("Duration of Turbo Button Press (frames):"));
|
|
|
|
m_turbo_press_frames = new QSpinBox();
|
|
|
|
m_turbo_press_frames->setMinimum(1);
|
|
|
|
settings_layout->addWidget(turbo_press_label, 1, 0);
|
|
|
|
settings_layout->addWidget(m_turbo_press_frames, 1, 1);
|
|
|
|
|
|
|
|
QLabel* turbo_release_label = new QLabel(tr("Duration of Turbo Button Release (frames):"));
|
|
|
|
m_turbo_release_frames = new QSpinBox();
|
|
|
|
m_turbo_release_frames->setMinimum(1);
|
|
|
|
settings_layout->addWidget(turbo_release_label, 2, 0);
|
|
|
|
settings_layout->addWidget(m_turbo_release_frames, 2, 1);
|
|
|
|
|
|
|
|
m_settings_box = new QGroupBox(tr("Settings"));
|
|
|
|
m_settings_box->setLayout(settings_layout);
|
|
|
|
}
|
|
|
|
|
|
|
|
int TASInputWindow::GetTurboPressFrames() const
|
|
|
|
{
|
|
|
|
return m_turbo_press_frames->value();
|
|
|
|
}
|
|
|
|
|
|
|
|
int TASInputWindow::GetTurboReleaseFrames() const
|
|
|
|
{
|
|
|
|
return m_turbo_release_frames->value();
|
|
|
|
}
|
|
|
|
|
2021-03-21 21:42:33 +00:00
|
|
|
TASCheckBox* TASInputWindow::CreateButton(const QString& text, std::string_view group_name,
|
|
|
|
std::string_view control_name, InputOverrider* overrider)
|
2020-06-30 19:07:25 +00:00
|
|
|
{
|
2021-03-21 21:42:33 +00:00
|
|
|
TASCheckBox* checkbox = new TASCheckBox(text, this);
|
|
|
|
|
|
|
|
overrider->AddFunction(group_name, control_name, [this, checkbox](ControlState controller_state) {
|
|
|
|
return GetButton(checkbox, controller_state);
|
|
|
|
});
|
|
|
|
|
|
|
|
return checkbox;
|
2018-07-07 05:51:34 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 21:42:33 +00:00
|
|
|
QGroupBox* TASInputWindow::CreateStickInputs(const QString& text, std::string_view group_name,
|
2022-10-04 19:24:33 +00:00
|
|
|
InputOverrider* overrider, int min_x, int min_y,
|
|
|
|
int max_x, int max_y, Qt::Key x_shortcut_key,
|
2018-07-07 05:51:34 +00:00
|
|
|
Qt::Key y_shortcut_key)
|
|
|
|
{
|
2021-04-25 00:18:28 +00:00
|
|
|
const QKeySequence x_shortcut_key_sequence = QKeySequence(Qt::ALT | x_shortcut_key);
|
|
|
|
const QKeySequence y_shortcut_key_sequence = QKeySequence(Qt::ALT | y_shortcut_key);
|
2018-07-07 05:51:34 +00:00
|
|
|
|
|
|
|
auto* box =
|
|
|
|
new QGroupBox(QStringLiteral("%1 (%2/%3)")
|
2021-03-21 21:42:33 +00:00
|
|
|
.arg(text, x_shortcut_key_sequence.toString(QKeySequence::NativeText),
|
2018-07-07 05:51:34 +00:00
|
|
|
y_shortcut_key_sequence.toString(QKeySequence::NativeText)));
|
|
|
|
|
2020-10-18 11:47:00 +00:00
|
|
|
const int x_default = static_cast<int>(std::round(max_x / 2.));
|
|
|
|
const int y_default = static_cast<int>(std::round(max_y / 2.));
|
|
|
|
|
2018-07-07 05:51:34 +00:00
|
|
|
auto* x_layout = new QHBoxLayout;
|
2023-03-07 18:22:09 +00:00
|
|
|
TASSpinBox* x_value = CreateSliderValuePair(x_layout, x_default, max_x, x_shortcut_key_sequence,
|
|
|
|
Qt::Horizontal, box);
|
2018-07-07 05:51:34 +00:00
|
|
|
|
|
|
|
auto* y_layout = new QVBoxLayout;
|
2023-03-07 18:22:09 +00:00
|
|
|
TASSpinBox* y_value =
|
2020-10-18 11:47:00 +00:00
|
|
|
CreateSliderValuePair(y_layout, y_default, max_y, y_shortcut_key_sequence, Qt::Vertical, box);
|
2018-07-07 05:51:34 +00:00
|
|
|
y_value->setMaximumWidth(60);
|
|
|
|
|
|
|
|
auto* visual = new StickWidget(this, max_x, max_y);
|
2020-10-18 11:47:00 +00:00
|
|
|
visual->SetX(x_default);
|
|
|
|
visual->SetY(y_default);
|
|
|
|
|
2023-11-04 21:01:39 +00:00
|
|
|
connect(x_value, &QSpinBox::valueChanged, visual, &StickWidget::SetX);
|
|
|
|
connect(y_value, &QSpinBox::valueChanged, visual, &StickWidget::SetY);
|
2018-07-07 05:51:34 +00:00
|
|
|
connect(visual, &StickWidget::ChangedX, x_value, &QSpinBox::setValue);
|
|
|
|
connect(visual, &StickWidget::ChangedY, y_value, &QSpinBox::setValue);
|
|
|
|
|
|
|
|
auto* visual_ar = new AspectRatioWidget(visual, max_x, max_y);
|
|
|
|
|
|
|
|
auto* visual_layout = new QHBoxLayout;
|
|
|
|
visual_layout->addWidget(visual_ar);
|
|
|
|
visual_layout->addLayout(y_layout);
|
|
|
|
|
|
|
|
auto* layout = new QVBoxLayout;
|
|
|
|
layout->addLayout(x_layout);
|
|
|
|
layout->addLayout(visual_layout);
|
|
|
|
box->setLayout(layout);
|
|
|
|
|
2021-03-21 21:42:33 +00:00
|
|
|
overrider->AddFunction(group_name, ControllerEmu::ReshapableInput::X_INPUT_OVERRIDE,
|
|
|
|
[this, x_value, x_default, min_x, max_x](ControlState controller_state) {
|
|
|
|
return GetSpinBox(x_value, x_default, min_x, max_x, controller_state);
|
|
|
|
});
|
|
|
|
|
|
|
|
overrider->AddFunction(group_name, ControllerEmu::ReshapableInput::Y_INPUT_OVERRIDE,
|
|
|
|
[this, y_value, y_default, min_y, max_y](ControlState controller_state) {
|
|
|
|
return GetSpinBox(y_value, y_default, min_y, max_y, controller_state);
|
|
|
|
});
|
|
|
|
|
2018-07-07 05:51:34 +00:00
|
|
|
return box;
|
|
|
|
}
|
|
|
|
|
2021-03-21 21:42:33 +00:00
|
|
|
QBoxLayout* TASInputWindow::CreateSliderValuePairLayout(
|
|
|
|
const QString& text, std::string_view group_name, std::string_view control_name,
|
2022-10-04 19:24:33 +00:00
|
|
|
InputOverrider* overrider, int zero, int default_, int min, int max, Qt::Key shortcut_key,
|
2023-03-07 18:30:21 +00:00
|
|
|
QWidget* shortcut_widget, std::optional<ControlState> scale)
|
2018-07-07 05:51:34 +00:00
|
|
|
{
|
2021-04-25 00:18:28 +00:00
|
|
|
const QKeySequence shortcut_key_sequence = QKeySequence(Qt::ALT | shortcut_key);
|
2018-07-07 05:51:34 +00:00
|
|
|
|
|
|
|
auto* label = new QLabel(QStringLiteral("%1 (%2)").arg(
|
2021-03-21 21:42:33 +00:00
|
|
|
text, shortcut_key_sequence.toString(QKeySequence::NativeText)));
|
2018-07-07 05:51:34 +00:00
|
|
|
|
|
|
|
QBoxLayout* layout = new QHBoxLayout;
|
|
|
|
layout->addWidget(label);
|
|
|
|
|
2023-03-07 18:30:21 +00:00
|
|
|
CreateSliderValuePair(group_name, control_name, overrider, layout, zero, default_, min, max,
|
|
|
|
shortcut_key_sequence, Qt::Horizontal, shortcut_widget, scale);
|
2018-07-07 05:51:34 +00:00
|
|
|
|
|
|
|
return layout;
|
|
|
|
}
|
|
|
|
|
2023-03-07 18:22:09 +00:00
|
|
|
TASSpinBox* TASInputWindow::CreateSliderValuePair(
|
2021-03-21 21:42:33 +00:00
|
|
|
std::string_view group_name, std::string_view control_name, InputOverrider* overrider,
|
2022-10-04 19:24:33 +00:00
|
|
|
QBoxLayout* layout, int zero, int default_, int min, int max,
|
2021-03-21 21:42:33 +00:00
|
|
|
QKeySequence shortcut_key_sequence, Qt::Orientation orientation, QWidget* shortcut_widget,
|
|
|
|
std::optional<ControlState> scale)
|
|
|
|
{
|
2023-03-07 18:22:09 +00:00
|
|
|
TASSpinBox* value = CreateSliderValuePair(layout, default_, max, shortcut_key_sequence,
|
|
|
|
orientation, shortcut_widget);
|
2021-03-21 21:42:33 +00:00
|
|
|
|
|
|
|
InputOverrider::OverrideFunction func;
|
|
|
|
if (scale)
|
|
|
|
{
|
|
|
|
func = [this, value, zero, scale](ControlState controller_state) {
|
|
|
|
return GetSpinBox(value, zero, controller_state, *scale);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
func = [this, value, zero, min, max](ControlState controller_state) {
|
|
|
|
return GetSpinBox(value, zero, min, max, controller_state);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
overrider->AddFunction(group_name, control_name, std::move(func));
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2018-07-07 05:51:34 +00:00
|
|
|
// The shortcut_widget argument needs to specify the container widget that will be hidden/shown.
|
|
|
|
// This is done to avoid ambigous shortcuts
|
2022-10-04 19:24:33 +00:00
|
|
|
TASSpinBox* TASInputWindow::CreateSliderValuePair(QBoxLayout* layout, int default_, int max,
|
2023-03-07 18:22:09 +00:00
|
|
|
QKeySequence shortcut_key_sequence,
|
|
|
|
Qt::Orientation orientation,
|
|
|
|
QWidget* shortcut_widget)
|
2018-07-07 05:51:34 +00:00
|
|
|
{
|
2023-03-07 18:22:09 +00:00
|
|
|
auto* value = new TASSpinBox();
|
2018-07-07 05:51:34 +00:00
|
|
|
value->setRange(0, 99999);
|
2020-10-18 11:47:00 +00:00
|
|
|
value->setValue(default_);
|
2023-11-04 21:01:39 +00:00
|
|
|
connect(value, &QSpinBox::valueChanged, [value, max](int i) {
|
2019-07-30 13:35:46 +00:00
|
|
|
if (i > max)
|
|
|
|
value->setValue(max);
|
|
|
|
});
|
2020-10-18 11:47:00 +00:00
|
|
|
auto* slider = new TASSlider(default_, orientation);
|
2018-07-07 05:51:34 +00:00
|
|
|
slider->setRange(0, max);
|
2020-10-18 11:47:00 +00:00
|
|
|
slider->setValue(default_);
|
2018-07-07 05:51:34 +00:00
|
|
|
slider->setFocusPolicy(Qt::ClickFocus);
|
|
|
|
|
|
|
|
connect(slider, &QSlider::valueChanged, value, &QSpinBox::setValue);
|
2023-11-04 21:01:39 +00:00
|
|
|
connect(value, &QSpinBox::valueChanged, slider, &QSlider::setValue);
|
2018-07-07 05:51:34 +00:00
|
|
|
|
|
|
|
auto* shortcut = new QShortcut(shortcut_key_sequence, shortcut_widget);
|
|
|
|
connect(shortcut, &QShortcut::activated, [value] {
|
|
|
|
value->setFocus();
|
|
|
|
value->selectAll();
|
|
|
|
});
|
|
|
|
|
|
|
|
layout->addWidget(slider);
|
|
|
|
layout->addWidget(value);
|
|
|
|
if (orientation == Qt::Vertical)
|
|
|
|
layout->setAlignment(slider, Qt::AlignRight);
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2021-03-21 21:42:33 +00:00
|
|
|
std::optional<ControlState> TASInputWindow::GetButton(TASCheckBox* checkbox,
|
|
|
|
ControlState controller_state)
|
2018-07-07 05:51:34 +00:00
|
|
|
{
|
2021-03-21 21:42:33 +00:00
|
|
|
const bool pressed = std::llround(controller_state) > 0;
|
2018-07-07 05:51:34 +00:00
|
|
|
if (m_use_controller->isChecked())
|
2023-03-04 11:24:31 +00:00
|
|
|
checkbox->OnControllerValueChanged(pressed);
|
2018-07-07 05:51:34 +00:00
|
|
|
|
2021-03-21 21:42:33 +00:00
|
|
|
return checkbox->GetValue() ? 1.0 : 0.0;
|
2018-07-07 05:51:34 +00:00
|
|
|
}
|
|
|
|
|
2022-10-04 19:24:33 +00:00
|
|
|
std::optional<ControlState> TASInputWindow::GetSpinBox(TASSpinBox* spin, int zero, int min, int max,
|
2021-03-21 21:42:33 +00:00
|
|
|
ControlState controller_state)
|
2018-07-07 05:51:34 +00:00
|
|
|
{
|
2022-10-04 19:24:33 +00:00
|
|
|
const int controller_value =
|
|
|
|
ControllerEmu::EmulatedController::MapFloat<int>(controller_state, zero, 0, max);
|
2021-03-21 21:42:33 +00:00
|
|
|
|
2018-07-07 05:51:34 +00:00
|
|
|
if (m_use_controller->isChecked())
|
2023-03-07 18:22:09 +00:00
|
|
|
spin->OnControllerValueChanged(controller_value);
|
2018-07-07 05:51:34 +00:00
|
|
|
|
2022-10-04 19:24:33 +00:00
|
|
|
return ControllerEmu::EmulatedController::MapToFloat<ControlState, int>(spin->GetValue(), zero,
|
2023-03-07 18:22:09 +00:00
|
|
|
min, max);
|
2018-07-07 05:51:34 +00:00
|
|
|
}
|
|
|
|
|
2022-10-04 19:24:33 +00:00
|
|
|
std::optional<ControlState> TASInputWindow::GetSpinBox(TASSpinBox* spin, int zero,
|
2021-03-21 21:42:33 +00:00
|
|
|
ControlState controller_state,
|
|
|
|
ControlState scale)
|
2018-07-07 05:51:34 +00:00
|
|
|
{
|
2022-10-04 19:24:33 +00:00
|
|
|
const int controller_value = static_cast<int>(std::llround(controller_state * scale + zero));
|
2021-03-21 21:42:33 +00:00
|
|
|
|
2018-07-07 05:51:34 +00:00
|
|
|
if (m_use_controller->isChecked())
|
2023-03-07 18:22:09 +00:00
|
|
|
spin->OnControllerValueChanged(controller_value);
|
2018-07-07 05:51:34 +00:00
|
|
|
|
2023-03-07 18:22:09 +00:00
|
|
|
return (spin->GetValue() - zero) / scale;
|
2018-07-07 05:51:34 +00:00
|
|
|
}
|