From 27f9d8219bf659a3396976ce52df83ca6c00f42e Mon Sep 17 00:00:00 2001 From: Aaron Date: Wed, 1 Jan 2025 13:51:45 -0800 Subject: [PATCH] OptionsUiModel cleanups. --- OpenTESArena/src/Interface/OptionsPanel.cpp | 4 +- OpenTESArena/src/Interface/OptionsUiModel.cpp | 51 +++++----------- OpenTESArena/src/Interface/OptionsUiModel.h | 59 ++++++++----------- 3 files changed, 39 insertions(+), 75 deletions(-) diff --git a/OpenTESArena/src/Interface/OptionsPanel.cpp b/OpenTESArena/src/Interface/OptionsPanel.cpp index aa130cc10..7217fbc85 100644 --- a/OpenTESArena/src/Interface/OptionsPanel.cpp +++ b/OpenTESArena/src/Interface/OptionsPanel.cpp @@ -87,7 +87,7 @@ bool OptionsPanel::init() if (hoveredIndex.has_value() && (*hoveredIndex < static_cast(visibleOptions.size()))) { const auto &option = visibleOptions[*hoveredIndex]; - const std::string &descText = option->getTooltip(); + const std::string &descText = option->tooltip; this->descriptionTextBox.setText(descText); } else @@ -268,7 +268,7 @@ void OptionsPanel::updateOptionText(int index) const auto &visibleOptions = this->getVisibleOptions(); DebugAssertIndex(visibleOptions, index); const auto &visibleOption = visibleOptions[index]; - const std::string text = visibleOption->getName() + ": " + visibleOption->getDisplayedValue(); + const std::string text = visibleOption->name + ": " + visibleOption->getDisplayedValue(); DebugAssertIndex(this->optionTextBoxes, index); TextBox &textBox = this->optionTextBoxes[index]; diff --git a/OpenTESArena/src/Interface/OptionsUiModel.cpp b/OpenTESArena/src/Interface/OptionsUiModel.cpp index cda401c55..f7e9bb754 100644 --- a/OpenTESArena/src/Interface/OptionsUiModel.cpp +++ b/OpenTESArena/src/Interface/OptionsUiModel.cpp @@ -12,23 +12,7 @@ OptionsUiModel::Option::Option(const std::string &name, std::string &&tooltip, O OptionsUiModel::Option::Option(const std::string &name, OptionType type) : Option(name, std::string(), type) { } -const std::string &OptionsUiModel::Option::getName() const -{ - return this->name; -} - -const std::string &OptionsUiModel::Option::getTooltip() const -{ - return this->tooltip; -} - -OptionsUiModel::OptionType OptionsUiModel::Option::getType() const -{ - return this->type; -} - -OptionsUiModel::BoolOption::BoolOption(const std::string &name, std::string &&tooltip, - bool value, Callback &&callback) +OptionsUiModel::BoolOption::BoolOption(const std::string &name, std::string &&tooltip, bool value, Callback &&callback) : Option(name, std::move(tooltip), OptionType::Bool), callback(std::move(callback)) { this->value = value; @@ -58,10 +42,9 @@ void OptionsUiModel::BoolOption::toggle() this->callback(this->value); } -OptionsUiModel::IntOption::IntOption(const std::string &name, std::string &&tooltip, int value, int delta, int min, - int max, std::vector &&displayOverrides, Callback &&callback) - : Option(name, std::move(tooltip), OptionType::Int), displayOverrides(std::move(displayOverrides)), - callback(std::move(callback)) +OptionsUiModel::IntOption::IntOption(const std::string &name, std::string &&tooltip, int value, int delta, int min, int max, + std::vector &&displayOverrides, Callback &&callback) + : Option(name, std::move(tooltip), OptionType::Int), displayOverrides(std::move(displayOverrides)), callback(std::move(callback)) { this->value = value; this->delta = delta; @@ -73,12 +56,10 @@ OptionsUiModel::IntOption::IntOption(const std::string &name, int value, int del std::vector &&displayOverrides, Callback &&callback) : IntOption(name, std::string(), value, delta, min, max, std::move(displayOverrides), std::move(callback)) { } -OptionsUiModel::IntOption::IntOption(const std::string &name, std::string &&tooltip, int value, int delta, - int min, int max, Callback &&callback) +OptionsUiModel::IntOption::IntOption(const std::string &name, std::string &&tooltip, int value, int delta, int min, int max, Callback &&callback) : IntOption(name, std::move(tooltip), value, delta, min, max, std::vector(), std::move(callback)) { } -OptionsUiModel::IntOption::IntOption(const std::string &name, int value, int delta, int min, int max, - Callback &&callback) +OptionsUiModel::IntOption::IntOption(const std::string &name, int value, int delta, int min, int max, Callback &&callback) : IntOption(name, std::string(), value, delta, min, max, std::move(callback)) { } int OptionsUiModel::IntOption::getNext() const @@ -93,8 +74,7 @@ int OptionsUiModel::IntOption::getPrev() const std::string OptionsUiModel::IntOption::getDisplayedValue() const { - return (this->displayOverrides.size() > 0) ? - this->displayOverrides.at(this->value) : std::to_string(this->value); + return (this->displayOverrides.size() > 0) ? this->displayOverrides.at(this->value) : std::to_string(this->value); } void OptionsUiModel::IntOption::tryIncrement() @@ -113,8 +93,8 @@ void OptionsUiModel::IntOption::set(int value) this->callback(this->value); } -OptionsUiModel::DoubleOption::DoubleOption(const std::string &name, std::string &&tooltip, - double value, double delta, double min, double max, int precision, Callback &&callback) +OptionsUiModel::DoubleOption::DoubleOption(const std::string &name, std::string &&tooltip, double value, double delta, + double min, double max, int precision, Callback &&callback) : Option(name, std::move(tooltip), OptionType::Double), callback(std::move(callback)) { this->value = value; @@ -124,8 +104,8 @@ OptionsUiModel::DoubleOption::DoubleOption(const std::string &name, std::string this->precision = precision; } -OptionsUiModel::DoubleOption::DoubleOption(const std::string &name, double value, double delta, - double min, double max, int precision, Callback &&callback) +OptionsUiModel::DoubleOption::DoubleOption(const std::string &name, double value, double delta, double min, double max, + int precision, Callback &&callback) : DoubleOption(name, std::string(), value, delta, min, max, precision, std::move(callback)) { } double OptionsUiModel::DoubleOption::getNext() const @@ -159,13 +139,10 @@ void OptionsUiModel::DoubleOption::set(double value) this->callback(this->value); } -OptionsUiModel::StringOption::StringOption(const std::string &name, std::string &&tooltip, - std::string &&value, Callback &&callback) - : Option(name, std::move(tooltip), OptionType::String), value(std::move(value)), - callback(std::move(callback)) { } +OptionsUiModel::StringOption::StringOption(const std::string &name, std::string &&tooltip, std::string &&value, Callback &&callback) + : Option(name, std::move(tooltip), OptionType::String), value(std::move(value)), callback(std::move(callback)) { } -OptionsUiModel::StringOption::StringOption(const std::string &name, std::string &&value, - Callback &&callback) +OptionsUiModel::StringOption::StringOption(const std::string &name, std::string &&value, Callback &&callback) : StringOption(name, std::string(), std::move(value), std::move(callback)) { } std::string OptionsUiModel::StringOption::getDisplayedValue() const diff --git a/OpenTESArena/src/Interface/OptionsUiModel.h b/OpenTESArena/src/Interface/OptionsUiModel.h index 48d755746..185fd5eac 100644 --- a/OpenTESArena/src/Interface/OptionsUiModel.h +++ b/OpenTESArena/src/Interface/OptionsUiModel.h @@ -62,20 +62,16 @@ namespace OptionsUiModel // This is the base class for all interactive options. Each option has a write-only interface // since the options panel shouldn't really store all the values itself; it's intended to be // a ferry between the UI and wherever in the program the options are actually used. - class Option + struct Option { - private: const std::string &name; // Reference to global constant. std::string tooltip; OptionType type; - public: + Option(const std::string &name, std::string &&tooltip, OptionType type); Option(const std::string &name, OptionType type); virtual ~Option() = default; - const std::string &getName() const; - const std::string &getTooltip() const; - OptionType getType() const; virtual std::string getDisplayedValue() const = 0; virtual void tryIncrement() = 0; @@ -84,19 +80,18 @@ namespace OptionsUiModel using OptionGroup = std::vector>; - class BoolOption : public Option + struct BoolOption : public Option { - public: using Callback = std::function; - private: + bool value; Callback callback; - public: + BoolOption(const std::string &name, std::string &&tooltip, bool value, Callback &&callback); BoolOption(const std::string &name, bool value, Callback &&callback); ~BoolOption() override = default; - virtual std::string getDisplayedValue() const override; + std::string getDisplayedValue() const override; void tryIncrement() override; void tryDecrement() override; @@ -104,27 +99,23 @@ namespace OptionsUiModel void toggle(); }; - class IntOption : public Option + struct IntOption : public Option { - public: using Callback = std::function; - private: + int value, delta, min, max; std::vector displayOverrides; // For displaying names instead of integers. Callback callback; - public: - IntOption(const std::string &name, std::string &&tooltip, int value, int delta, int min, int max, - std::vector &&displayOverrides, Callback &&callback); - IntOption(const std::string &name, int value, int delta, int min, int max, - std::vector &&displayOverrides, Callback &&callback); - IntOption(const std::string &name, std::string &&tooltip, int value, int delta, int min, int max, - Callback &&callback); + + IntOption(const std::string &name, std::string &&tooltip, int value, int delta, int min, int max, std::vector &&displayOverrides, Callback &&callback); + IntOption(const std::string &name, int value, int delta, int min, int max, std::vector &&displayOverrides, Callback &&callback); + IntOption(const std::string &name, std::string &&tooltip, int value, int delta, int min, int max, Callback &&callback); IntOption(const std::string &name, int value, int delta, int min, int max, Callback &&callback); ~IntOption() override = default; int getNext() const; // Adds delta to current value, clamped between [min, max]. int getPrev() const; // Subtracts delta from current value, clamped between [min, max]. - virtual std::string getDisplayedValue() const override; + std::string getDisplayedValue() const override; void tryIncrement() override; void tryDecrement() override; @@ -132,24 +123,21 @@ namespace OptionsUiModel void set(int value); }; - class DoubleOption : public Option + struct DoubleOption : public Option { - public: using Callback = std::function; - private: + double value, delta, min, max; int precision; Callback callback; - public: - DoubleOption(const std::string &name, std::string &&tooltip, double value, double delta, - double min, double max, int precision, Callback &&callback); - DoubleOption(const std::string &name, double value, double delta, double min, double max, - int precision, Callback &&callback); + + DoubleOption(const std::string &name, std::string &&tooltip, double value, double delta, double min, double max, int precision, Callback &&callback); + DoubleOption(const std::string &name, double value, double delta, double min, double max, int precision, Callback &&callback); ~DoubleOption() override = default; double getNext() const; // Adds delta to current value, clamped between [min, max]. double getPrev() const; // Subtracts delta from current value, clamped between [min, max]. - virtual std::string getDisplayedValue() const override; + std::string getDisplayedValue() const override; void tryIncrement() override; void tryDecrement() override; @@ -157,19 +145,18 @@ namespace OptionsUiModel void set(double value); }; - class StringOption : public Option + struct StringOption : public Option { - public: using Callback = std::function; - private: + std::string value; Callback callback; - public: + StringOption(const std::string &name, std::string &&tooltip, std::string &&value, Callback &&callback); StringOption(const std::string &name, std::string &&value, Callback &&callback); ~StringOption() override = default; - virtual std::string getDisplayedValue() const override; + std::string getDisplayedValue() const override; void tryIncrement() override; void tryDecrement() override;