dolphin/Source/Core/DolphinQt/Config/Mapping/MappingButton.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

160 lines
4.3 KiB
C++
Raw Normal View History

2017-05-20 15:53:17 +00:00
// Copyright 2017 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#include "DolphinQt/Config/Mapping/MappingButton.h"
#include <QApplication>
#include <QFontMetrics>
2017-05-20 15:53:17 +00:00
#include <QMouseEvent>
#include <QRegExp>
#include <QString>
2017-05-20 15:53:17 +00:00
#include "Common/Thread.h"
#include "Core/Core.h"
2018-07-06 22:40:15 +00:00
#include "DolphinQt/Config/Mapping/IOWindow.h"
#include "DolphinQt/Config/Mapping/MappingCommon.h"
#include "DolphinQt/Config/Mapping/MappingWidget.h"
#include "DolphinQt/Config/Mapping/MappingWindow.h"
#include "DolphinQt/QtUtils/BlockUserInputFilter.h"
#include "DolphinQt/QtUtils/QueueOnObject.h"
2018-07-06 22:40:15 +00:00
#include "DolphinQt/Settings.h"
2017-05-20 15:53:17 +00:00
#include "InputCommon/ControlReference/ControlReference.h"
#include "InputCommon/ControllerEmu/ControlGroup/Buttons.h"
2017-05-20 15:53:17 +00:00
#include "InputCommon/ControllerEmu/ControllerEmu.h"
#include "InputCommon/ControllerInterface/ControllerInterface.h"
#include "InputCommon/ControllerInterface/Device.h"
2017-05-20 15:53:17 +00:00
constexpr int SLIDER_TICK_COUNT = 100;
2019-03-03 14:43:18 +00:00
// Escape ampersands and remove ticks
static QString ToDisplayString(QString&& string)
{
return string.replace(QLatin1Char{'&'}, QStringLiteral("&&"))
.replace(QLatin1Char{'`'}, QString{});
}
2018-04-01 14:25:34 +00:00
bool MappingButton::IsInput() const
{
return m_reference->IsInput();
}
2019-03-15 01:27:49 +00:00
MappingButton::MappingButton(MappingWidget* parent, ControlReference* ref, bool indicator)
: ElidedButton(ToDisplayString(QString::fromStdString(ref->GetExpression()))), m_parent(parent),
m_reference(ref)
2017-05-20 15:53:17 +00:00
{
// Force all mapping buttons to stay at a minimal height.
setFixedHeight(minimumSizeHint().height());
// Make sure that long entries don't throw our layout out of whack.
2019-08-01 13:28:04 +00:00
setFixedWidth(WIDGET_MAX_WIDTH);
setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
2018-02-07 17:16:15 +00:00
setToolTip(
tr("Left-click to detect input.\nMiddle-click to clear.\nRight-click for more options."));
connect(this, &MappingButton::clicked, this, &MappingButton::Detect);
2019-03-15 01:27:49 +00:00
if (indicator)
connect(parent, &MappingWidget::Update, this, &MappingButton::UpdateIndicator);
2019-03-15 01:27:49 +00:00
connect(parent, &MappingWidget::ConfigChanged, this, &MappingButton::ConfigChanged);
2017-05-20 15:53:17 +00:00
}
2019-03-15 01:27:49 +00:00
void MappingButton::AdvancedPressed()
2017-05-20 15:53:17 +00:00
{
2019-03-15 01:27:49 +00:00
IOWindow io(this, m_parent->GetController(), m_reference,
m_reference->IsInput() ? IOWindow::Type::Input : IOWindow::Type::Output);
io.exec();
ConfigChanged();
m_parent->SaveSettings();
2017-05-20 15:53:17 +00:00
}
2018-04-01 14:25:34 +00:00
void MappingButton::Detect()
2017-05-20 15:53:17 +00:00
{
2019-03-15 01:27:49 +00:00
if (!m_reference->IsInput())
2017-05-20 15:53:17 +00:00
return;
const auto default_device_qualifier = m_parent->GetController()->GetDefaultDevice();
QString expression;
2017-05-20 15:53:17 +00:00
if (m_parent->GetParent()->IsMappingAllDevices())
{
expression = MappingCommon::DetectExpression(this, g_controller_interface,
g_controller_interface.GetAllDeviceStrings(),
default_device_qualifier);
}
else
{
expression = MappingCommon::DetectExpression(this, g_controller_interface,
{default_device_qualifier.ToString()},
default_device_qualifier);
}
if (expression.isEmpty())
return;
2018-04-01 14:25:34 +00:00
m_reference->SetExpression(expression.toStdString());
m_parent->GetController()->UpdateSingleControlReference(g_controller_interface, m_reference);
2017-05-20 15:53:17 +00:00
2019-03-15 01:27:49 +00:00
ConfigChanged();
m_parent->SaveSettings();
2017-05-20 15:53:17 +00:00
}
void MappingButton::Clear()
{
m_reference->range = 100.0 / SLIDER_TICK_COUNT;
2019-03-15 01:27:49 +00:00
m_reference->SetExpression("");
m_parent->GetController()->UpdateSingleControlReference(g_controller_interface, m_reference);
2019-03-15 01:27:49 +00:00
m_parent->SaveSettings();
2019-03-15 01:27:49 +00:00
ConfigChanged();
}
void MappingButton::UpdateIndicator()
{
if (!isActiveWindow())
return;
const auto state = m_reference->State();
QFont f = m_parent->font();
if (state > ControllerEmu::Buttons::ACTIVATION_THRESHOLD)
f.setBold(true);
setFont(f);
2017-05-20 15:53:17 +00:00
}
2019-03-15 01:27:49 +00:00
void MappingButton::ConfigChanged()
2017-05-20 15:53:17 +00:00
{
2019-03-03 14:43:18 +00:00
setText(ToDisplayString(QString::fromStdString(m_reference->GetExpression())));
2017-05-20 15:53:17 +00:00
}
void MappingButton::mouseReleaseEvent(QMouseEvent* event)
{
switch (event->button())
2017-05-20 15:53:17 +00:00
{
case Qt::MouseButton::LeftButton:
if (m_reference->IsInput())
2017-05-20 15:53:17 +00:00
QPushButton::mouseReleaseEvent(event);
else
2019-03-15 01:27:49 +00:00
AdvancedPressed();
return;
2018-02-07 17:16:15 +00:00
case Qt::MouseButton::MidButton:
Clear();
return;
case Qt::MouseButton::RightButton:
2019-03-15 01:27:49 +00:00
AdvancedPressed();
return;
default:
return;
2017-05-20 15:53:17 +00:00
}
}