Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DolphinQt: Replace MappingButton bold text with graphical indicators. #13283

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 1 addition & 19 deletions Source/Core/DolphinQt/Config/Mapping/MappingButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ bool MappingButton::IsInput() const
return m_reference->IsInput();
}

MappingButton::MappingButton(MappingWidget* parent, ControlReference* ref, bool indicator)
MappingButton::MappingButton(MappingWidget* parent, ControlReference* ref)
: ElidedButton(RefToDisplayString(ref)), m_mapping_window(parent->GetParent()), m_reference(ref)
{
if (IsInput())
Expand All @@ -87,9 +87,6 @@ MappingButton::MappingButton(MappingWidget* parent, ControlReference* ref, bool

connect(this, &MappingButton::clicked, this, &MappingButton::Clicked);

if (indicator)
connect(parent, &MappingWidget::Update, this, &MappingButton::UpdateIndicator);

connect(parent, &MappingWidget::ConfigChanged, this, &MappingButton::ConfigChanged);
connect(this, &MappingButton::ConfigChanged, [this] {
setText(RefToDisplayString(m_reference));
Expand Down Expand Up @@ -134,21 +131,6 @@ void MappingButton::Clear()
m_mapping_window->UnQueueInputDetection(this);
}

void MappingButton::UpdateIndicator()
{
QFont f = m_mapping_window->font();

if (isActiveWindow() && m_reference->IsInput() && m_reference->GetState<bool>() && !m_is_mapping)
f.setBold(true);

// If the expression has failed to parse, show it in italic.
// Some expressions still work even the failed to parse so don't prevent the GetState() above.
if (m_reference->GetParseStatus() == ciface::ExpressionParser::ParseStatus::SyntaxError)
f.setItalic(true);

setFont(f);
}

void MappingButton::StartMapping()
{
// Focus just makes it more clear which button is currently being mapped.
Expand Down
3 changes: 1 addition & 2 deletions Source/Core/DolphinQt/Config/Mapping/MappingButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class MappingButton : public ElidedButton
{
Q_OBJECT
public:
MappingButton(MappingWidget* widget, ControlReference* ref, bool indicator);
MappingButton(MappingWidget* widget, ControlReference* ref);

bool IsInput() const;
ControlReference* GetControlReference();
Expand All @@ -26,7 +26,6 @@ class MappingButton : public ElidedButton

private:
void Clear();
void UpdateIndicator();
void AdvancedPressed();

void Clicked();
Expand Down
33 changes: 33 additions & 0 deletions Source/Core/DolphinQt/Config/Mapping/MappingIndicator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include <QAction>
#include <QDateTime>
#include <QLabel>
#include <QPainter>
#include <QPainterPath>
#include <QTimer>
Expand Down Expand Up @@ -128,6 +129,38 @@ void MappingIndicator::AdjustGateColor(QColor* color)
color->setHsvF(color->hueF(), color->saturationF(), 1 - color->valueF());
}

ButtonIndicator::ButtonIndicator(ControlReference* control_ref) : m_control_ref{control_ref}
{
setSizePolicy(QSizePolicy::Policy::Fixed, QSizePolicy::Policy::Fixed);
}

QSize ButtonIndicator::sizeHint() const
{
auto result = QLabel{}.sizeHint();
result.setWidth(result.width() / 2);
return result;
}

void ButtonIndicator::Draw()
{
QPainter p(this);
p.setBrush(GetBBoxBrush());
p.setPen(GetBBoxPen());
p.drawRect(QRect{{0, 0}, size() - QSize{1, 1}});

const auto input_value = std::min(m_control_ref->GetState<ControlState>(), 1.0);
const auto is_pressed = std::lround(input_value);
QSizeF value_size = size() - QSizeF{2, 2};
value_size.setHeight(value_size.height() * input_value);

p.translate(0, height());
p.scale(1, -1);

p.setPen(Qt::NoPen);
p.setBrush(is_pressed ? GetAdjustedInputColor() : GetRawInputColor());
p.drawRect(QRectF{{1, 1}, value_size});
}

SquareIndicator::SquareIndicator()
{
// Additional pixel for border.
Expand Down
11 changes: 11 additions & 0 deletions Source/Core/DolphinQt/Config/Mapping/MappingIndicator.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ class MappingIndicator : public QWidget
Clock::time_point m_last_update = Clock::now();
};

class ButtonIndicator final : public MappingIndicator
{
public:
ButtonIndicator(ControlReference* control_ref);

private:
ControlReference* const m_control_ref;
QSize sizeHint() const override;
void Draw() override;
};

class SquareIndicator : public MappingIndicator
{
protected:
Expand Down
33 changes: 24 additions & 9 deletions Source/Core/DolphinQt/Config/Mapping/MappingWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,19 +173,19 @@ QGroupBox* MappingWidget::CreateGroupBox(const QString& name, ControllerEmu::Con
form_layout->insertRow(2, mouse_button);

using ControllerEmu::Cursor;
connect(mouse_button, &QCheckBox::clicked, [this, group = static_cast<Cursor*>(group)] {
connect(mouse_button, &QCheckBox::clicked, [this, grp = static_cast<Cursor*>(group)] {
std::string default_device = g_controller_interface.GetDefaultDeviceString() + ":";
const std::string controller_device = GetController()->GetDefaultDevice().ToString() + ":";
if (default_device == controller_device)
{
default_device.clear();
}
group->SetControlExpression(0, fmt::format("`{}Cursor Y-`", default_device));
group->SetControlExpression(1, fmt::format("`{}Cursor Y+`", default_device));
group->SetControlExpression(2, fmt::format("`{}Cursor X-`", default_device));
group->SetControlExpression(3, fmt::format("`{}Cursor X+`", default_device));
grp->SetControlExpression(0, fmt::format("`{}Cursor Y-`", default_device));
grp->SetControlExpression(1, fmt::format("`{}Cursor Y+`", default_device));
grp->SetControlExpression(2, fmt::format("`{}Cursor X-`", default_device));
grp->SetControlExpression(3, fmt::format("`{}Cursor X+`", default_device));

group->SetRelativeInput(false);
grp->SetRelativeInput(false);

emit ConfigChanged();
GetController()->UpdateReferences(g_controller_interface);
Expand Down Expand Up @@ -313,14 +313,29 @@ QGroupBox* MappingWidget::CreateControlsBox(const QString& name, ControllerEmu::
void MappingWidget::CreateControl(const ControllerEmu::Control* control, QFormLayout* layout,
bool indicator)
{
auto* button = new MappingButton(this, control->control_ref.get(), indicator);

auto* const button = new MappingButton(this, control->control_ref.get());
button->setMinimumWidth(100);
button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

const bool translate = control->translate == ControllerEmu::Translatability::Translate;
const QString translated_name =
translate ? tr(control->ui_name.c_str()) : QString::fromStdString(control->ui_name);
layout->addRow(translated_name, button);

if (indicator && control->control_ref->IsInput())
{
auto* const button_indicator = new ButtonIndicator{control->control_ref.get()};
connect(this, &MappingWidget::Update, button_indicator, qOverload<>(&MappingIndicator::update));

auto* const hbox = new QHBoxLayout;
hbox->setSpacing(0);
hbox->addWidget(button_indicator);
hbox->addWidget(button);
layout->addRow(translated_name, hbox);
}
else
{
layout->addRow(translated_name, button);
}
}

ControllerEmu::EmulatedController* MappingWidget::GetController() const
Expand Down