Skip to content

Commit

Permalink
OptionsUiModel cleanups.
Browse files Browse the repository at this point in the history
  • Loading branch information
afritz1 committed Jan 1, 2025
1 parent 3c032e2 commit 27f9d82
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 75 deletions.
4 changes: 2 additions & 2 deletions OpenTESArena/src/Interface/OptionsPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ bool OptionsPanel::init()
if (hoveredIndex.has_value() && (*hoveredIndex < static_cast<int>(visibleOptions.size())))
{
const auto &option = visibleOptions[*hoveredIndex];
const std::string &descText = option->getTooltip();
const std::string &descText = option->tooltip;
this->descriptionTextBox.setText(descText);
}
else
Expand Down Expand Up @@ -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];
Expand Down
51 changes: 14 additions & 37 deletions OpenTESArena/src/Interface/OptionsUiModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<std::string> &&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<std::string> &&displayOverrides, Callback &&callback)
: Option(name, std::move(tooltip), OptionType::Int), displayOverrides(std::move(displayOverrides)), callback(std::move(callback))
{
this->value = value;
this->delta = delta;
Expand All @@ -73,12 +56,10 @@ OptionsUiModel::IntOption::IntOption(const std::string &name, int value, int del
std::vector<std::string> &&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::string>(), 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
Expand All @@ -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()
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
59 changes: 23 additions & 36 deletions OpenTESArena/src/Interface/OptionsUiModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -84,92 +80,83 @@ namespace OptionsUiModel

using OptionGroup = std::vector<std::unique_ptr<OptionsUiModel::Option>>;

class BoolOption : public Option
struct BoolOption : public Option
{
public:
using Callback = std::function<void(bool)>;
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;

void toggle();
};

class IntOption : public Option
struct IntOption : public Option
{
public:
using Callback = std::function<void(int)>;
private:

int value, delta, min, max;
std::vector<std::string> 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<std::string> &&displayOverrides, Callback &&callback);
IntOption(const std::string &name, int value, int delta, int min, int max,
std::vector<std::string> &&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<std::string> &&displayOverrides, Callback &&callback);
IntOption(const std::string &name, int value, int delta, int min, int max, std::vector<std::string> &&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;

void set(int value);
};

class DoubleOption : public Option
struct DoubleOption : public Option
{
public:
using Callback = std::function<void(double)>;
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;

void set(double value);
};

class StringOption : public Option
struct StringOption : public Option
{
public:
using Callback = std::function<void(const std::string&)>;
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;
Expand Down

0 comments on commit 27f9d82

Please sign in to comment.