Added dropdown option type

This commit is contained in:
thecozies
2024-07-22 09:46:04 -05:00
committed by Mr-Wiseguy
parent af432a60d5
commit a5f795fafe
12 changed files with 246 additions and 3 deletions
+4
View File
@@ -86,6 +86,10 @@ void ElementConfigOption::AddOptionTypeElement() {
add_option_el<ElementOptionTypeColor>(doc, wrapper, "recomp-option-type-color", config_key);
break;
}
case ConfigOptionType::Dropdown: {
add_option_el<ElementOptionTypeDropdown>(doc, wrapper, "recomp-option-type-dropdown", config_key);
break;
}
case ConfigOptionType::RadioTabs: {
add_option_el<ElementOptionTypeRadioTabs>(doc, wrapper, "recomp-option-type-radio-tabs", config_key);
break;
+16
View File
@@ -0,0 +1,16 @@
#include "ElementOptionType.h"
namespace recompui {
ElementOptionType::ElementOptionType(const Rml::String& tag, const std::string& base_class) : Rml::Element(tag)
{
SetAttribute("recomp-store-element", true);
SetClass(base_class, true);
}
ElementOptionType::~ElementOptionType()
{
}
} // namespace Rml
+20
View File
@@ -0,0 +1,20 @@
#ifndef RECOMPUI_ELEMENT_OPTION_TYPE_H
#define RECOMPUI_ELEMENT_OPTION_TYPE_H
#include "common.h"
// base class for ElementOptionTypes. Already set up as an event listener,
// and contains required data. Initialization sets an important flag.
namespace recompui {
class ElementOptionType : public Rml::Element, public Rml::EventListener {
public:
ElementOptionType(const Rml::String& tag, const std::string& base_class);
virtual ~ElementOptionType();
std::string config_key;
};
} // namespace recompui
#endif
@@ -0,0 +1,79 @@
#include "ElementOptionTypeDropdown.h"
#include <string>
using json = nlohmann::json;
namespace recompui {
static const std::string select_id = "recomp-select";
static const std::string select_option_id = "recomp-select-option__";
static const std::string cls_base = "config-option-dropdown";
static const std::string cls_select = cls_base + "__select";
ElementOptionTypeDropdown::ElementOptionTypeDropdown(const Rml::String& tag) : ElementOptionType(tag, cls_base)
{
Rml::ElementFormControlSelect *select_el = (Rml::ElementFormControlSelect *)AppendChild(GetOwnerDocument()->CreateElement("select"));
select_el->SetId(select_id);
select_el->SetValue("0");
select_el->AddEventListener(Rml::EventId::Change, this, false);
select_el->SetClass(cls_select, true);
}
ElementOptionTypeDropdown::~ElementOptionTypeDropdown()
{
auto select_el = get_select();
RemoveEventListener(Rml::EventId::Change, this, false);
}
Rml::ElementFormControlSelect *ElementOptionTypeDropdown::get_select()
{
return (Rml::ElementFormControlSelect *)GetElementById(select_id);
}
void ElementOptionTypeDropdown::set_cur_option(int opt) {
auto select_el = get_select();
select_el->SetValue(std::to_string(opt));
DirtyLayout();
}
void ElementOptionTypeDropdown::init_option(std::string& _config_key) {
config_key = _config_key;
const json& option_json = recomp::config::get_json_from_key(config_key);;
int opt = recomp::config::get_config_store_value<int>(config_key);
const json& opt_array = option_json["values"];
auto select_el = get_select();
for (int i = 0; i < opt_array.size(); i++) {
const auto &j_opt = opt_array[i];
const std::string opt_val = j_opt.get<std::string>();
const std::string opt_id = select_option_id + config_key + "--" + opt_val;
const std::string translation_key = "translations/" + config_key + "/values/" + opt_val;
const std::string& opt_text = recomp::config::get_config_store_value<std::string>(translation_key);
select_el->Add(opt_text, std::to_string(i));
}
set_cur_option(opt);
}
void ElementOptionTypeDropdown::ProcessEvent(Rml::Event& event)
{
if (event == Rml::EventId::Change)
{
if (event.GetPhase() == Rml::EventPhase::Bubble || event.GetPhase() == Rml::EventPhase::Target)
{
Rml::Element *target = event.GetTargetElement();
auto val_variant = target->GetAttribute("value");
int new_value = val_variant->Get<int>();
recomp::config::set_config_store_value(config_key, new_value);
}
}
}
} // namespace Rml
@@ -0,0 +1,24 @@
#ifndef RECOMPUI_ELEMENT_OPTION_TYPE_DROPDOWN_H
#define RECOMPUI_ELEMENT_OPTION_TYPE_DROPDOWN_H
#include "common.h"
#include "ElementOptionType.h"
namespace recompui {
class ElementOptionTypeDropdown : public ElementOptionType {
public:
ElementOptionTypeDropdown(const Rml::String& tag);
virtual ~ElementOptionTypeDropdown();
void init_option(std::string& _config_key);
protected:
void ProcessEvent(Rml::Event& event) override;
private:
void set_cur_option(int opt);
Rml::ElementFormControlSelect *get_select();
};
} // namespace recompui
#endif
+1
View File
@@ -14,6 +14,7 @@ static RecompElementConfig custom_elements[] = {
CUSTOM_ELEMENT("recomp-config-option", recompui::ElementConfigOption),
CUSTOM_ELEMENT("recomp-option-type-checkbox", recompui::ElementOptionTypeCheckbox),
CUSTOM_ELEMENT("recomp-option-type-color", recompui::ElementOptionTypeColor),
CUSTOM_ELEMENT("recomp-option-type-dropdown", recompui::ElementOptionTypeDropdown),
CUSTOM_ELEMENT("recomp-option-type-radio-tabs", recompui::ElementOptionTypeRadioTabs),
CUSTOM_ELEMENT("recomp-option-type-range", recompui::ElementOptionTypeRange),
};
+1
View File
@@ -8,6 +8,7 @@
#include "elements/ElementConfigGroup.h"
#include "elements/ElementOptionTypeCheckbox.h"
#include "elements/ElementOptionTypeColor.h"
#include "elements/ElementOptionTypeDropdown.h"
#include "elements/ElementOptionTypeRadioTabs.h"
#include "elements/ElementOptionTypeRange.h"
#include "elements/ElementDescription.h"