diff --git a/CHANGES b/CHANGES index 707aa9367..f5c40cbfc 100644 --- a/CHANGES +++ b/CHANGES @@ -2,6 +2,7 @@ Features: - ELF support - Game Boy Camera support + - Qt: Set default Game Boy colors Bugfixes: - GB Audio: Make audio unsigned with bias (fixes mgba.io/i/749) - Python: Fix importing .gb or .gba before .core diff --git a/src/platform/qt/CMakeLists.txt b/src/platform/qt/CMakeLists.txt index 7bea1cb71..c34df5909 100644 --- a/src/platform/qt/CMakeLists.txt +++ b/src/platform/qt/CMakeLists.txt @@ -66,6 +66,7 @@ set(SOURCE_FILES CheatsModel.cpp CheatsView.cpp ConfigController.cpp + ColorPicker.cpp CoreManager.cpp CoreController.cpp Display.cpp diff --git a/src/platform/qt/ColorPicker.cpp b/src/platform/qt/ColorPicker.cpp new file mode 100644 index 000000000..7351286e4 --- /dev/null +++ b/src/platform/qt/ColorPicker.cpp @@ -0,0 +1,56 @@ +/* Copyright (c) 2013-2017 Jeffrey Pfau + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#include "ColorPicker.h" + +#include +#include + +using namespace QGBA; + +ColorPicker::ColorPicker() { +} + +ColorPicker::ColorPicker(QWidget* parent, const QColor& defaultColor) + : m_parent(parent) +{ + QPalette palette = parent->palette(); + palette.setColor(parent->backgroundRole(), defaultColor); + parent->setPalette(palette); + parent->installEventFilter(this); +} + +ColorPicker& ColorPicker::operator=(const ColorPicker& other) { + if (m_parent) { + m_parent->removeEventFilter(this); + } + m_parent = other.m_parent; + m_parent->installEventFilter(this); + + return *this; +} + +bool ColorPicker::eventFilter(QObject* obj, QEvent* event) { + if (event->type() != QEvent::MouseButtonRelease) { + return false; + } + int colorId; + if (obj != m_parent) { + return false; + } + + QWidget* swatch = static_cast(obj); + + QColorDialog* colorPicker = new QColorDialog; + colorPicker->setAttribute(Qt::WA_DeleteOnClose); + colorPicker->open(); + connect(colorPicker, &QColorDialog::colorSelected, [this, swatch](const QColor& color) { + QPalette palette = swatch->palette(); + palette.setColor(swatch->backgroundRole(), color); + swatch->setPalette(palette); + emit colorChanged(color); + }); + return true; +} diff --git a/src/platform/qt/ColorPicker.h b/src/platform/qt/ColorPicker.h new file mode 100644 index 000000000..720e55922 --- /dev/null +++ b/src/platform/qt/ColorPicker.h @@ -0,0 +1,37 @@ +/* Copyright (c) 2013-2015 Jeffrey Pfau + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#ifndef QGBA_COLOR_PICKER +#define QGBA_COLOR_PICKER + +#include + +class QColor; +class QWidget; + +namespace QGBA { + +class ColorPicker : public QObject { +Q_OBJECT + +public: + ColorPicker(); + ColorPicker(QWidget* parent, const QColor& defaultColor); + + ColorPicker& operator=(const ColorPicker&); + +signals: + void colorChanged(const QColor&); + +protected: + bool eventFilter(QObject* obj, QEvent* event) override; + +private: + QWidget* m_parent = nullptr; +}; + +} + +#endif diff --git a/src/platform/qt/OverrideView.cpp b/src/platform/qt/OverrideView.cpp index 640f5bcf4..a1fd24334 100644 --- a/src/platform/qt/OverrideView.cpp +++ b/src/platform/qt/OverrideView.cpp @@ -5,7 +5,6 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "OverrideView.h" -#include #include #include "ConfigController.h" @@ -76,20 +75,16 @@ OverrideView::OverrideView(ConfigController* config, QWidget* parent) connect(m_ui.gbModel, &QComboBox::currentTextChanged, this, &OverrideView::updateOverrides); connect(m_ui.mbc, &QComboBox::currentTextChanged, this, &OverrideView::updateOverrides); - QPalette palette = m_ui.color0->palette(); - palette.setColor(backgroundRole(), QColor(0xF8, 0xF8, 0xF8)); - m_ui.color0->setPalette(palette); - palette.setColor(backgroundRole(), QColor(0xA8, 0xA8, 0xA8)); - m_ui.color1->setPalette(palette); - palette.setColor(backgroundRole(), QColor(0x50, 0x50, 0x50)); - m_ui.color2->setPalette(palette); - palette.setColor(backgroundRole(), QColor(0x00, 0x00, 0x00)); - m_ui.color3->setPalette(palette); - - m_ui.color0->installEventFilter(this); - m_ui.color1->installEventFilter(this); - m_ui.color2->installEventFilter(this); - m_ui.color3->installEventFilter(this); + m_colorPickers[0] = ColorPicker(m_ui.color0, QColor(0xF8, 0xF8, 0xF8)); + m_colorPickers[1] = ColorPicker(m_ui.color1, QColor(0xA8, 0xA8, 0xA8)); + m_colorPickers[2] = ColorPicker(m_ui.color2, QColor(0x50, 0x50, 0x50)); + m_colorPickers[3] = ColorPicker(m_ui.color3, QColor(0x00, 0x00, 0x00)); + for (int colorId = 0; colorId < 4; ++colorId) { + connect(&m_colorPickers[colorId], &ColorPicker::colorChanged, this, [this, colorId](const QColor& color) { + m_gbColors[colorId] = color.rgb(); + updateOverrides(); + }); + } connect(m_ui.tabWidget, &QTabWidget::currentChanged, this, &OverrideView::updateOverrides); #ifndef M_CORE_GBA @@ -115,42 +110,6 @@ void OverrideView::setController(std::shared_ptr controller) { } } -bool OverrideView::eventFilter(QObject* obj, QEvent* event) { -#ifdef M_CORE_GB - if (event->type() != QEvent::MouseButtonRelease) { - return false; - } - int colorId; - if (obj == m_ui.color0) { - colorId = 0; - } else if (obj == m_ui.color1) { - colorId = 1; - } else if (obj == m_ui.color2) { - colorId = 2; - } else if (obj == m_ui.color3) { - colorId = 3; - } else { - return false; - } - - QWidget* swatch = static_cast(obj); - - QColorDialog* colorPicker = new QColorDialog; - colorPicker->setAttribute(Qt::WA_DeleteOnClose); - colorPicker->open(); - connect(colorPicker, &QColorDialog::colorSelected, [this, swatch, colorId](const QColor& color) { - QPalette palette = swatch->palette(); - palette.setColor(backgroundRole(), color); - swatch->setPalette(palette); - m_gbColors[colorId] = color.rgb(); - updateOverrides(); - }); - return true; -#else - return false; -#endif -} - void OverrideView::saveOverride() { if (!m_config || !m_controller) { return; diff --git a/src/platform/qt/OverrideView.h b/src/platform/qt/OverrideView.h index 2b971aaae..e9d6b80c3 100644 --- a/src/platform/qt/OverrideView.h +++ b/src/platform/qt/OverrideView.h @@ -14,6 +14,7 @@ #include #endif +#include "ColorPicker.h" #include "Override.h" #include "ui_OverrideView.h" @@ -42,9 +43,6 @@ private slots: void gameStarted(); void gameStopped(); -protected: - bool eventFilter(QObject* obj, QEvent* event) override; - private: Ui::OverrideView m_ui; @@ -54,6 +52,7 @@ private: #ifdef M_CORE_GB uint32_t m_gbColors[4]{}; + ColorPicker m_colorPickers[4]; static QList s_gbModelList; static QList s_mbcList; diff --git a/src/platform/qt/SettingsView.cpp b/src/platform/qt/SettingsView.cpp index b13ebaf2a..4dd9bc143 100644 --- a/src/platform/qt/SettingsView.cpp +++ b/src/platform/qt/SettingsView.cpp @@ -159,6 +159,34 @@ SettingsView::SettingsView(ConfigController* controller, InputController* inputC connect(m_ui.gbcBiosBrowse, &QPushButton::clicked, [this]() { selectBios(m_ui.gbcBios); }); + + QList defaultColors; + defaultColors.append(QColor(0xF8, 0xF8, 0xF8)); + defaultColors.append(QColor(0xA8, 0xA8, 0xA8)); + defaultColors.append(QColor(0x50, 0x50, 0x50)); + defaultColors.append(QColor(0x00, 0x00, 0x00)); + bool ok; + if (m_controller->getOption("gb.pal[0]").toUInt(&ok) || ok) { + defaultColors[0] = QColor::fromRgb(m_controller->getOption("gb.pal[0]").toUInt()); + } + if (m_controller->getOption("gb.pal[1]").toUInt(&ok) || ok) { + defaultColors[1] = QColor::fromRgb(m_controller->getOption("gb.pal[1]").toUInt()); + } + if (m_controller->getOption("gb.pal[2]").toUInt(&ok) || ok) { + defaultColors[2] = QColor::fromRgb(m_controller->getOption("gb.pal[2]").toUInt()); + } + if (m_controller->getOption("gb.pal[3]").toUInt(&ok) || ok) { + defaultColors[3] = QColor::fromRgb(m_controller->getOption("gb.pal[3]").toUInt()); + } + m_colorPickers[0] = ColorPicker(m_ui.color0, defaultColors[0]); + m_colorPickers[1] = ColorPicker(m_ui.color1, defaultColors[1]); + m_colorPickers[2] = ColorPicker(m_ui.color2, defaultColors[2]); + m_colorPickers[3] = ColorPicker(m_ui.color3, defaultColors[3]); + for (int colorId = 0; colorId < 4; ++colorId) { + connect(&m_colorPickers[colorId], &ColorPicker::colorChanged, this, [this, colorId](const QColor& color) { + m_gbColors[colorId] = color.rgb(); + }); + } #else m_ui.gbBiosBrowse->hide(); m_ui.gbcBiosBrowse->hide(); @@ -331,6 +359,13 @@ void SettingsView::updateConfig() { emit languageChanged(); } + if (m_gbColors[0] | m_gbColors[1] | m_gbColors[2] | m_gbColors[3]) { + m_controller->setOption("gb.pal[0]", m_gbColors[0]); + m_controller->setOption("gb.pal[1]", m_gbColors[1]); + m_controller->setOption("gb.pal[2]", m_gbColors[2]); + m_controller->setOption("gb.pal[3]", m_gbColors[3]); + } + m_controller->write(); emit pathsChanged(); diff --git a/src/platform/qt/SettingsView.h b/src/platform/qt/SettingsView.h index cc628e10d..0713171f9 100644 --- a/src/platform/qt/SettingsView.h +++ b/src/platform/qt/SettingsView.h @@ -8,6 +8,8 @@ #include +#include "ColorPicker.h" + #include #include "ui_SettingsView.h" @@ -48,6 +50,8 @@ private: ConfigController* m_controller; InputController* m_input; ShaderSelector* m_shader = nullptr; + uint32_t m_gbColors[4]{}; + ColorPicker m_colorPickers[4]; void saveSetting(const char* key, const QAbstractButton*); void saveSetting(const char* key, const QComboBox*); diff --git a/src/platform/qt/SettingsView.ui b/src/platform/qt/SettingsView.ui index e9a029f23..6a2379302 100644 --- a/src/platform/qt/SettingsView.ui +++ b/src/platform/qt/SettingsView.ui @@ -1056,9 +1056,6 @@ - - false - Default colors