Fix options and sliders to prevent index out of range issues. (#4421)

This commit is contained in:
Malkierian 2024-10-15 17:59:20 -07:00 committed by GitHub
parent 960c9c4fd1
commit 45af1028af
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 18 deletions

View File

@ -90,10 +90,10 @@ void Option::RestoreDelayedOption() {
SetVariable(); SetVariable();
} }
void Option::SetSelectedIndex(const size_t idx) { void Option::SetSelectedIndex(size_t idx) {
selectedOption = idx; selectedOption = idx;
if (selectedOption >= options.size()) { if (selectedOption > options.size() - 1) {
selectedOption = 0; selectedOption = options.size() - 1;
} }
SetVariable(); SetVariable();
} }
@ -133,7 +133,7 @@ bool Option::IsCategory(const OptionCategory category) const {
return category == this->category; return category == this->category;
} }
bool Option::RenderImGui() const { bool Option::RenderImGui() {
bool changed = false; bool changed = false;
ImGui::BeginGroup(); ImGui::BeginGroup();
switch (widgetType) { switch (widgetType) {
@ -179,7 +179,7 @@ Option::Option(uint8_t var_, std::string name_, std::vector<std::string> options
defaultOption(defaultOption_), defaultHidden(defaultHidden_), imFlags(imFlags_) { defaultOption(defaultOption_), defaultHidden(defaultHidden_), imFlags(imFlags_) {
selectedOption = defaultOption; selectedOption = defaultOption;
hidden = defaultHidden; hidden = defaultHidden;
SetVariable(); SetFromCVar();
} }
Option::Option(bool var_, std::string name_, std::vector<std::string> options_, const OptionCategory category_, Option::Option(bool var_, std::string name_, std::vector<std::string> options_, const OptionCategory category_,
std::string cvarName_, std::string description_, WidgetType widgetType_, const uint8_t defaultOption_, std::string cvarName_, std::string description_, WidgetType widgetType_, const uint8_t defaultOption_,
@ -189,10 +189,10 @@ Option::Option(bool var_, std::string name_, std::vector<std::string> options_,
defaultOption(defaultOption_), defaultHidden(defaultHidden_), imFlags(imFlags_) { defaultOption(defaultOption_), defaultHidden(defaultHidden_), imFlags(imFlags_) {
selectedOption = defaultOption; selectedOption = defaultOption;
hidden = defaultHidden; hidden = defaultHidden;
SetVariable(); SetFromCVar();
} }
bool Option::RenderCheckbox() const { bool Option::RenderCheckbox() {
bool changed = false; bool changed = false;
if (disabled) { if (disabled) {
UIWidgets::DisableComponent(ImGui::GetStyle().Alpha * 0.5f); UIWidgets::DisableComponent(ImGui::GetStyle().Alpha * 0.5f);
@ -212,7 +212,7 @@ bool Option::RenderCheckbox() const {
return changed; return changed;
} }
bool Option::RenderTristateCheckbox() const { bool Option::RenderTristateCheckbox() {
bool changed = false; bool changed = false;
if (disabled) { if (disabled) {
UIWidgets::DisableComponent(ImGui::GetStyle().Alpha * 0.5f); UIWidgets::DisableComponent(ImGui::GetStyle().Alpha * 0.5f);
@ -232,7 +232,7 @@ bool Option::RenderTristateCheckbox() const {
return changed; return changed;
} }
bool Option::RenderCombobox() const { bool Option::RenderCombobox() {
bool changed = false; bool changed = false;
if (disabled) { if (disabled) {
UIWidgets::DisableComponent(ImGui::GetStyle().Alpha * 0.5f); UIWidgets::DisableComponent(ImGui::GetStyle().Alpha * 0.5f);
@ -268,11 +268,11 @@ bool Option::RenderCombobox() const {
return changed; return changed;
} }
bool Option::RenderSlider() const { bool Option::RenderSlider() {
bool changed = false; bool changed = false;
int val = CVarGetInteger(cvarName.c_str(), defaultOption); int val = GetSelectedOptionIndex();
if (val >= options.size()) { if (val > options.size() - 1) {
val = options.size(); val = options.size() - 1;
CVarSetInteger(cvarName.c_str(), val); CVarSetInteger(cvarName.c_str(), val);
changed = true; changed = true;
} }
@ -320,6 +320,7 @@ bool Option::RenderSlider() const {
} }
if (changed) { if (changed) {
CVarSetInteger(cvarName.c_str(), val); CVarSetInteger(cvarName.c_str(), val);
SetFromCVar();
Ship::Context::GetInstance()->GetWindow()->GetGui()->SaveConsoleVariablesOnNextTick(); Ship::Context::GetInstance()->GetWindow()->GetGui()->SaveConsoleVariablesOnNextTick();
} }
return changed; return changed;

View File

@ -301,7 +301,7 @@ class Option {
* U8 options are rendered as Comboboxes, but this can be overriden during construction with * U8 options are rendered as Comboboxes, but this can be overriden during construction with
* the `widgetType` property. * the `widgetType` property.
*/ */
bool RenderImGui() const; bool RenderImGui();
bool HasFlag(int imFlag_) const; bool HasFlag(int imFlag_) const;
void AddFlag(int imFlag_); void AddFlag(int imFlag_);
@ -317,10 +317,10 @@ protected:
bool defaultHidden_, int imFlags_); bool defaultHidden_, int imFlags_);
private: private:
bool RenderCheckbox() const; bool RenderCheckbox();
bool RenderTristateCheckbox() const; bool RenderTristateCheckbox();
bool RenderCombobox() const; bool RenderCombobox();
bool RenderSlider() const; bool RenderSlider();
std::variant<bool, uint8_t> var; std::variant<bool, uint8_t> var;
std::string name; std::string name;
std::vector<std::string> options; std::vector<std::string> options;