Added TextField option type

This commit is contained in:
thecozies
2024-08-07 09:20:43 -05:00
committed by Mr-Wiseguy
parent a5f795fafe
commit a1cea99c54
11 changed files with 136 additions and 13 deletions
+4
View File
@@ -90,6 +90,10 @@ void ElementConfigOption::AddOptionTypeElement() {
add_option_el<ElementOptionTypeDropdown>(doc, wrapper, "recomp-option-type-dropdown", config_key);
break;
}
case ConfigOptionType::TextField: {
add_option_el<ElementOptionTypeTextField>(doc, wrapper, "recomp-option-type-textfield", config_key);
break;
}
case ConfigOptionType::RadioTabs: {
add_option_el<ElementOptionTypeRadioTabs>(doc, wrapper, "recomp-option-type-radio-tabs", config_key);
break;
+1 -1
View File
@@ -13,7 +13,7 @@ public:
ElementOptionType(const Rml::String& tag, const std::string& base_class);
virtual ~ElementOptionType();
std::string config_key;
std::string config_key;
};
} // namespace recompui
@@ -0,0 +1,67 @@
#include "ElementOptionTypeTextField.h"
#include <string>
using json = nlohmann::json;
namespace recompui {
static const std::string input_id = "recomp-textfield";
static const std::string cls_base = "config-option-textfield";
static const std::string cls_wrapper = cls_base + "__wrapper";
static const std::string cls_input = cls_base + "__input";
ElementOptionTypeTextField::ElementOptionTypeTextField(const Rml::String& tag) : ElementOptionType(tag, cls_base)
{
Rml::Element *wrapper = AppendChild(GetOwnerDocument()->CreateElement("label"));
wrapper->SetClass(cls_wrapper, true);
Rml::ElementFormControlSelect *input_el = (Rml::ElementFormControlSelect *)wrapper->AppendChild(GetOwnerDocument()->CreateElement("input"));
input_el->SetId(input_id);
input_el->SetValue("");
input_el->AddEventListener(Rml::EventId::Change, this, false);
input_el->SetClass(cls_input, true);
}
Rml::ElementFormControlInput *ElementOptionTypeTextField::get_input()
{
return (Rml::ElementFormControlInput *)GetElementById(input_id);
}
ElementOptionTypeTextField::~ElementOptionTypeTextField()
{
auto input_el = get_input();
RemoveEventListener(Rml::EventId::Change, this, false);
}
void ElementOptionTypeTextField::init_option(std::string& _config_key) {
config_key = _config_key;
const json& option_json = recomp::config::get_json_from_key(config_key);;
auto val = recomp::config::get_config_store_value<std::string>(config_key);
auto input_el = get_input();
input_el->SetValue(val);
const auto maxlength = recomp::config::get_value_in_json<int>(option_json, "maxlength");
input_el->SetAttribute("maxlength", std::to_string(maxlength));
// RMLUI sadly doesn't support placeholders yet, so this will be a _placeholder_ until thats ready.
input_el->SetAttribute("placeholder", recomp::config::get_config_store_value_with_default<std::string>("translations/" + config_key + ":placeholder", ""));
}
void ElementOptionTypeTextField::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");
recomp::config::set_config_store_value(config_key, val_variant->Get<std::string>());
}
}
}
} // namespace Rml
@@ -0,0 +1,22 @@
#ifndef RECOMPUI_ELEMENT_OPTION_TYPE_TEXTFIELD_H
#define RECOMPUI_ELEMENT_OPTION_TYPE_TEXTFIELD_H
#include "common.h"
#include "ElementOptionType.h"
namespace recompui {
class ElementOptionTypeTextField : public ElementOptionType {
public:
ElementOptionTypeTextField(const Rml::String& tag);
virtual ~ElementOptionTypeTextField();
void init_option(std::string& _config_key);
protected:
void ProcessEvent(Rml::Event& event) override;
private:
Rml::ElementFormControlInput* get_input();
};
} // namespace recompui
#endif
+1
View File
@@ -15,6 +15,7 @@ static RecompElementConfig custom_elements[] = {
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-textfield", recompui::ElementOptionTypeTextField),
CUSTOM_ELEMENT("recomp-option-type-radio-tabs", recompui::ElementOptionTypeRadioTabs),
CUSTOM_ELEMENT("recomp-option-type-range", recompui::ElementOptionTypeRange),
};
+1
View File
@@ -11,6 +11,7 @@
#include "elements/ElementOptionTypeDropdown.h"
#include "elements/ElementOptionTypeRadioTabs.h"
#include "elements/ElementOptionTypeRange.h"
#include "elements/ElementOptionTypeTextField.h"
#include "elements/ElementDescription.h"
namespace recompui {