Skip to content

Commit

Permalink
Qt: Add DS4/DS5 LED settings
Browse files Browse the repository at this point in the history
  • Loading branch information
stenzek authored and lightningterror committed Jan 16, 2023
1 parent a0000a8 commit 43ccb63
Show file tree
Hide file tree
Showing 14 changed files with 366 additions and 14 deletions.
3 changes: 3 additions & 0 deletions pcsx2-qt/CMakeLists.txt
Expand Up @@ -11,6 +11,8 @@ target_sources(pcsx2-qt PRIVATE
AutoUpdaterDialog.cpp
AutoUpdaterDialog.h
AutoUpdaterDialog.ui
ColorPickerButton.cpp
ColorPickerButton.h
CoverDownloadDialog.cpp
CoverDownloadDialog.h
CoverDownloadDialog.ui
Expand Down Expand Up @@ -50,6 +52,7 @@ target_sources(pcsx2-qt PRIVATE
Settings/ControllerBindingWidget_DualShock2.ui
Settings/ControllerBindingWidgets.cpp
Settings/ControllerBindingWidgets.h
Settings/ControllerLEDSettingsDialog.ui
Settings/ControllerGlobalSettingsWidget.cpp
Settings/ControllerGlobalSettingsWidget.h
Settings/ControllerGlobalSettingsWidget.ui
Expand Down
66 changes: 66 additions & 0 deletions pcsx2-qt/ColorPickerButton.cpp
@@ -0,0 +1,66 @@
/* PCSX2 - PS2 Emulator for PCs
* Copyright (C) 2002-2023 PCSX2 Dev Team
*
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with PCSX2.
* If not, see <http://www.gnu.org/licenses/>.
*/

#include "PrecompiledHeader.h"
#include "ColorPickerButton.h"
#include "QtUtils.h"

#include <QtWidgets/QColorDialog>

ColorPickerButton::ColorPickerButton(QWidget* parent)
: QPushButton(parent)
{
connect(this, &QPushButton::clicked, this, &ColorPickerButton::onClicked);
updateBackgroundColor();
}

u32 ColorPickerButton::color()
{
return m_color;
}

void ColorPickerButton::setColor(u32 rgb)
{
if (m_color == rgb)
return;

m_color = rgb;
updateBackgroundColor();
}

void ColorPickerButton::updateBackgroundColor()
{
setStyleSheet(QStringLiteral("background-color: #%1;").arg(static_cast<uint>(m_color), 8, 16, QChar('0')));
}

void ColorPickerButton::onClicked()
{
const u32 red = (m_color >> 16) & 0xff;
const u32 green = (m_color >> 8) & 0xff;
const u32 blue = m_color & 0xff;

const QColor initial(QColor::fromRgb(red, green, blue));
const QColor selected(QColorDialog::getColor(initial, QtUtils::GetRootWidget(this), tr("Select LED Color")));

// QColorDialog returns Invalid on cancel, and apparently initial == Invalid is true...
if (!selected.isValid() || initial == selected)
return;

const u32 new_rgb =
(static_cast<u32>(selected.red()) << 16) | (static_cast<u32>(selected.green()) << 8) | static_cast<u32>(selected.blue());
m_color = new_rgb;
updateBackgroundColor();
emit colorChanged(new_rgb);
}
42 changes: 42 additions & 0 deletions pcsx2-qt/ColorPickerButton.h
@@ -0,0 +1,42 @@
/* PCSX2 - PS2 Emulator for PCs
* Copyright (C) 2002-2023 PCSX2 Dev Team
*
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with PCSX2.
* If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include "common/Pcsx2Defs.h"
#include <QtWidgets/QPushButton>

class ColorPickerButton : public QPushButton
{
Q_OBJECT

public:
ColorPickerButton(QWidget* parent);

Q_SIGNALS:
void colorChanged(quint32 new_color);

public Q_SLOTS:
quint32 color();
void setColor(quint32 rgb);

private Q_SLOTS:
void onClicked();

private:
void updateBackgroundColor();

u32 m_color = 0;
};
55 changes: 52 additions & 3 deletions pcsx2-qt/Settings/ControllerGlobalSettingsWidget.cpp
Expand Up @@ -22,6 +22,10 @@
#include "QtUtils.h"
#include "SettingWidgetBinder.h"

#ifdef SDL_BUILD
#include "pcsx2/Frontend/SDLInputSource.h"
#endif

ControllerGlobalSettingsWidget::ControllerGlobalSettingsWidget(QWidget* parent, ControllerSettingsDialog* dialog)
: QWidget(parent)
, m_dialog(dialog)
Expand All @@ -30,8 +34,16 @@ ControllerGlobalSettingsWidget::ControllerGlobalSettingsWidget(QWidget* parent,

SettingsInterface* sif = dialog->getProfileSettingsInterface();

#ifdef SDL_BUILD
SettingWidgetBinder::BindWidgetToBoolSetting(sif, m_ui.enableSDLSource, "InputSources", "SDL", true);
SettingWidgetBinder::BindWidgetToBoolSetting(sif, m_ui.enableSDLEnhancedMode, "InputSources", "SDLControllerEnhancedMode", false);
connect(m_ui.enableSDLSource, &QCheckBox::stateChanged, this, &ControllerGlobalSettingsWidget::updateSDLOptionsEnabled);
connect(m_ui.ledSettings, &QToolButton::clicked, this, &ControllerGlobalSettingsWidget::ledSettingsClicked);
#else
m_ui.enableSDLSource->setEnabled(false);
m_ui.ledSettings->setEnabled(false);
#endif

SettingWidgetBinder::BindWidgetToBoolSetting(sif, m_ui.enableMouseMapping, "UI", "EnableMouseMapping", false);
ControllerSettingWidgetBinder::BindWidgetToInputProfileBool(sif, m_ui.multitapPort1, "Pad", "MultitapPort1", false);
ControllerSettingWidgetBinder::BindWidgetToInputProfileBool(sif, m_ui.multitapPort2, "Pad", "MultitapPort2", false);
Expand Down Expand Up @@ -66,12 +78,13 @@ ControllerGlobalSettingsWidget::ControllerGlobalSettingsWidget(QWidget* parent,
m_ui.profileSettings = nullptr;
}

connect(m_ui.enableSDLSource, &QCheckBox::stateChanged, this, &ControllerGlobalSettingsWidget::updateSDLOptionsEnabled);
for (QCheckBox* cb : {m_ui.multitapPort1, m_ui.multitapPort2})
connect(cb, &QCheckBox::stateChanged, this, [this]() { emit bindingSetupChanged(); });

connect(m_ui.pointerXScale, &QSlider::valueChanged, this, [this](int value) { m_ui.pointerXScaleLabel->setText(QStringLiteral("%1").arg(value)); });
connect(m_ui.pointerYScale, &QSlider::valueChanged, this, [this](int value) { m_ui.pointerYScaleLabel->setText(QStringLiteral("%1").arg(value)); });
connect(m_ui.pointerXScale, &QSlider::valueChanged, this,
[this](int value) { m_ui.pointerXScaleLabel->setText(QStringLiteral("%1").arg(value)); });
connect(m_ui.pointerYScale, &QSlider::valueChanged, this,
[this](int value) { m_ui.pointerYScaleLabel->setText(QStringLiteral("%1").arg(value)); });
m_ui.pointerXScaleLabel->setText(QStringLiteral("%1").arg(m_ui.pointerXScale->value()));
m_ui.pointerYScaleLabel->setText(QStringLiteral("%1").arg(m_ui.pointerYScale->value()));

Expand Down Expand Up @@ -106,4 +119,40 @@ void ControllerGlobalSettingsWidget::updateSDLOptionsEnabled()
{
const bool enabled = m_ui.enableSDLSource->isChecked();
m_ui.enableSDLEnhancedMode->setEnabled(enabled);
m_ui.ledSettings->setEnabled(enabled);
}

void ControllerGlobalSettingsWidget::ledSettingsClicked()
{
ControllerLEDSettingsDialog dialog(this, m_dialog);
dialog.exec();
}

ControllerLEDSettingsDialog::ControllerLEDSettingsDialog(QWidget* parent, ControllerSettingsDialog* dialog)
: QDialog(parent)
, m_dialog(dialog)
{
m_ui.setupUi(this);

linkButton(m_ui.SDL0LED, 0);
linkButton(m_ui.SDL1LED, 1);
linkButton(m_ui.SDL2LED, 2);
linkButton(m_ui.SDL3LED, 3);

connect(m_ui.buttonBox->button(QDialogButtonBox::Close), &QPushButton::clicked, this, &QDialog::accept);
}

ControllerLEDSettingsDialog::~ControllerLEDSettingsDialog() = default;

void ControllerLEDSettingsDialog::linkButton(ColorPickerButton* button, u32 player_id)
{
#ifdef SDL_BUILD
std::string key(fmt::format("Player{}LED", player_id));
const u32 current_value = SDLInputSource::ParseRGBForPlayerId(m_dialog->getStringValue("SDLExtra", key.c_str(), ""), player_id);
button->setColor(current_value);

connect(button, &ColorPickerButton::colorChanged, this, [this, player_id, key = std::move(key)](u32 new_rgb) {
m_dialog->setStringValue("SDLExtra", key.c_str(), fmt::format("{:06X}", new_rgb).c_str());
});
#endif
}
22 changes: 21 additions & 1 deletion pcsx2-qt/Settings/ControllerGlobalSettingsWidget.h
Expand Up @@ -20,7 +20,10 @@
#include <array>
#include <vector>

#include "ColorPickerButton.h"

#include "ui_ControllerGlobalSettingsWidget.h"
#include "ui_ControllerLEDSettingsDialog.h"

class ControllerSettingsDialog;

Expand All @@ -38,9 +41,26 @@ class ControllerGlobalSettingsWidget : public QWidget
Q_SIGNALS:
void bindingSetupChanged();

private:
private Q_SLOTS:
void updateSDLOptionsEnabled();
void ledSettingsClicked();

private:
Ui::ControllerGlobalSettingsWidget m_ui;
ControllerSettingsDialog* m_dialog;
};

class ControllerLEDSettingsDialog : public QDialog
{
Q_OBJECT

public:
ControllerLEDSettingsDialog(QWidget* parent, ControllerSettingsDialog* dialog);
~ControllerLEDSettingsDialog();

private:
void linkButton(ColorPickerButton* button, u32 player_id);

Ui::ControllerLEDSettingsDialog m_ui;
ControllerSettingsDialog* m_dialog;
};
26 changes: 21 additions & 5 deletions pcsx2-qt/Settings/ControllerGlobalSettingsWidget.ui
Expand Up @@ -50,11 +50,27 @@
</widget>
</item>
<item row="1" column="1">
<widget class="QCheckBox" name="enableSDLEnhancedMode">
<property name="text">
<string>DualShock 4 / DualSense Enhanced Mode</string>
</property>
</widget>
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<widget class="QCheckBox" name="enableSDLEnhancedMode">
<property name="text">
<string>DualShock 4 / DualSense Enhanced Mode</string>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="ledSettings">
<property name="toolTip">
<string>Controller LED Settings</string>
</property>
<property name="icon">
<iconset theme="lightbulb-line">
<normaloff>.</normaloff>.
</iconset>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
Expand Down
83 changes: 83 additions & 0 deletions pcsx2-qt/Settings/ControllerLEDSettingsDialog.ui
@@ -0,0 +1,83 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>ControllerLEDSettingsDialog</class>
<widget class="QDialog" name="ControllerLEDSettingsDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>501</width>
<height>108</height>
</rect>
</property>
<property name="windowTitle">
<string>Controller LED Settings</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>SDL-0 LED</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_4">
<item>
<widget class="ColorPickerButton" name="SDL0LED"/>
</item>
</layout>
</widget>
</item>
<item row="0" column="1">
<widget class="QGroupBox" name="groupBox_2">
<property name="title">
<string>SDL-1 LED</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="ColorPickerButton" name="SDL1LED"/>
</item>
</layout>
</widget>
</item>
<item row="0" column="2">
<widget class="QGroupBox" name="groupBox_3">
<property name="title">
<string>SDL-2 LED</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="ColorPickerButton" name="SDL2LED"/>
</item>
</layout>
</widget>
</item>
<item row="0" column="3">
<widget class="QGroupBox" name="groupBox_4">
<property name="title">
<string>SDL-3 LED</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="ColorPickerButton" name="SDL3LED"/>
</item>
</layout>
</widget>
</item>
<item row="1" column="0" colspan="4">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="standardButtons">
<set>QDialogButtonBox::Close</set>
</property>
</widget>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>ColorPickerButton</class>
<extends>QPushButton</extends>
<header>ColorPickerButton.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>

0 comments on commit 43ccb63

Please sign in to comment.