diff --git a/Source/Core/DolphinQt/Config/ConfigControls/ConfigSlider.cpp b/Source/Core/DolphinQt/Config/ConfigControls/ConfigSlider.cpp index a296d57e86..349e73eeda 100644 --- a/Source/Core/DolphinQt/Config/ConfigControls/ConfigSlider.cpp +++ b/Source/Core/DolphinQt/Config/ConfigControls/ConfigSlider.cpp @@ -1,6 +1,8 @@ // Copyright 2017 Dolphin Emulator Project // SPDX-License-Identifier: GPL-2.0-or-later +#include + #include #include "DolphinQt/Config/ConfigControls/ConfigSlider.h" @@ -24,14 +26,58 @@ ConfigSlider::ConfigSlider(int minimum, int maximum, const Config::Info& se connect(this, &ConfigSlider::valueChanged, this, &ConfigSlider::Update); } +ConfigSlider::ConfigSlider(std::vector tick_values, const Config::Info& setting, + Config::Layer* layer) + : ConfigControl(Qt::Horizontal, setting.GetLocation(), layer), m_setting(setting), + m_tick_values(std::move(tick_values)) +{ + assert(!m_tick_values.empty()); + setMinimum(0); + setMaximum(static_cast(m_tick_values.size() - 1)); + setPageStep(1); + setTickPosition(QSlider::TicksBelow); + OnConfigChanged(); + + connect(this, &ConfigSlider::valueChanged, this, &ConfigSlider::Update); +} + void ConfigSlider::Update(int value) { - SaveValue(m_setting, value); + if (!m_tick_values.empty()) + { + if (value >= 0 && static_cast(value) < m_tick_values.size()) + SaveValue(m_setting, m_tick_values[static_cast(value)]); + } + else + { + SaveValue(m_setting, value); + } } void ConfigSlider::OnConfigChanged() { - setValue(ReadValue(m_setting)); + if (!m_tick_values.empty()) + { + // re-enable in case it was disabled + setEnabled(true); + + const int config_value = ReadValue(m_setting); + for (size_t i = 0; i < m_tick_values.size(); ++i) + { + if (m_tick_values[i] == config_value) + { + setValue(static_cast(i)); + return; + } + } + + // if we reach here than none of the options matched, disable the slider + setEnabled(false); + } + else + { + setValue(ReadValue(m_setting)); + } } ConfigSliderLabel::ConfigSliderLabel(const QString& text, ConfigSlider* slider) diff --git a/Source/Core/DolphinQt/Config/ConfigControls/ConfigSlider.h b/Source/Core/DolphinQt/Config/ConfigControls/ConfigSlider.h index 3df98d53b7..ec1ba827ad 100644 --- a/Source/Core/DolphinQt/Config/ConfigControls/ConfigSlider.h +++ b/Source/Core/DolphinQt/Config/ConfigControls/ConfigSlider.h @@ -3,6 +3,8 @@ #pragma once +#include + #include #include @@ -19,6 +21,11 @@ public: ConfigSlider(int minimum, int maximum, const Config::Info& setting, Config::Layer* layer, int tick = 0); + // Generates a slider with tick_values.size() ticks. Each tick corresponds to the integer at that + // index in the vector. + ConfigSlider(std::vector tick_values, const Config::Info& setting, + Config::Layer* layer); + void Update(int value); protected: @@ -26,6 +33,9 @@ protected: private: const Config::Info m_setting; + + // Mappings for slider ticks to config values. Identity mapping is assumed if this is empty. + std::vector m_tick_values; }; class ConfigSliderLabel final : public QLabel