Button config type + callback wip

This commit is contained in:
thecozies 2024-09-02 10:52:20 -05:00
parent 446912d449
commit 110e7acbb9
9 changed files with 111 additions and 2 deletions

View File

@ -164,6 +164,7 @@ set (SOURCES
${CMAKE_SOURCE_DIR}/src/ui/elements/ElementConfigOption.cpp
${CMAKE_SOURCE_DIR}/src/ui/elements/ElementDescription.cpp
${CMAKE_SOURCE_DIR}/src/ui/elements/ElementOptionType.cpp
${CMAKE_SOURCE_DIR}/src/ui/elements/ElementOptionTypeButton.cpp
${CMAKE_SOURCE_DIR}/src/ui/elements/ElementOptionTypeCheckbox.cpp
${CMAKE_SOURCE_DIR}/src/ui/elements/ElementOptionTypeColor.cpp
${CMAKE_SOURCE_DIR}/src/ui/elements/ElementOptionTypeDropdown.cpp

View File

@ -7,6 +7,10 @@
"consumables/infinite_bombs": "Infinite Bombs",
"consumables/infinite_health": "Infinite Health",
"consumable_actions": "Consumable Actions",
"consumable_actions/refill_all": "primary",
"consumable_actions/refill_all:description": "Refills anything that can be refilled, like magic, rupees, arrows, bombs, health, etc.",
"gameplay": "Gameplay",
"gameplay/movement": "Movement",

View File

@ -29,7 +29,19 @@
"type": "Checkbox",
"key": "infinite_health",
"default": false,
"onChange": "on_update_health"
"callback": "on_update_health"
}
]
},
{
"type": "Group",
"key": "consumable_actions",
"options": [
{
"type": "Button",
"key": "refill_all",
"variant": "primary",
"callback": "on_refill_all"
}
]
},

View File

@ -78,6 +78,13 @@ void ElementConfigOption::AddOptionTypeElement() {
default:
printf("No option type element exists for type '%d'\n", el_option_type);
return;
case ConfigOptionType::Button: {
add_option_el<ElementOptionTypeButton>(doc, wrapper, "recomp-option-type-button", config_key);
// Button contains label text, so hide the label
auto label = GetLabel();
label->SetProperty("display", "none");
break;
}
case ConfigOptionType::Checkbox: {
add_option_el<ElementOptionTypeCheckbox>(doc, wrapper, "recomp-option-type-checkbox", config_key);
break;

View File

@ -0,0 +1,61 @@
#include "ElementOptionTypeButton.h"
#include <string>
using json = nlohmann::json;
namespace recompui {
static const std::string button_id = "recomp-button";
static const std::string cls_base = "config-option-button";
static const std::string cls_button = "button";
ElementOptionTypeButton::ElementOptionTypeButton(const Rml::String& tag) : ElementOptionType(tag, cls_base)
{
Rml::Element *button = AppendChild(GetOwnerDocument()->CreateElement("button"));
button->SetClass(cls_button, true);
button->SetId(button_id);
button->AddEventListener(Rml::EventId::Click, this, false);
}
ElementOptionTypeButton::~ElementOptionTypeButton()
{
auto button_el = get_button();
button_el->RemoveEventListener(Rml::EventId::Click, this, false);
}
Rml::Element *ElementOptionTypeButton::get_button()
{
return GetElementById(button_id);
}
void ElementOptionTypeButton::init_option(std::string& _config_key) {
config_key = _config_key;
const json& option_json = recomp::config::get_json_from_key(config_key);
auto button_el = get_button();
button_el->SetInnerRML(
recomp::config::get_config_store_value<std::string>("translations/" + config_key)
);
std::string variantClass = recomp::config::get_value_in_json_with_default<std::string>(option_json, "variant", "primary");
button_el->SetClass(cls_button + "--" + variantClass, true);
}
void ElementOptionTypeButton::ProcessEvent(Rml::Event& event)
{
if (event == Rml::EventId::Click)
{
if (event.GetPhase() == Rml::EventPhase::Bubble || event.GetPhase() == Rml::EventPhase::Target)
{
printf("Button clicked\n");
}
}
}
} // namespace Rml

View File

@ -0,0 +1,22 @@
#ifndef RECOMPUI_ELEMENT_OPTION_TYPE_BUTTON_H
#define RECOMPUI_ELEMENT_OPTION_TYPE_BUTTON_H
#include "common.h"
#include "ElementOptionType.h"
namespace recompui {
class ElementOptionTypeButton : public ElementOptionType {
public:
ElementOptionTypeButton(const Rml::String& tag);
virtual ~ElementOptionTypeButton();
void init_option(std::string& _config_key);
protected:
void ProcessEvent(Rml::Event& event) override;
private:
Rml::Element* get_button();
};
} // namespace recompui
#endif

View File

@ -32,7 +32,7 @@ Rml::ElementFormControlInput *ElementOptionTypeTextField::get_input()
ElementOptionTypeTextField::~ElementOptionTypeTextField()
{
auto input_el = get_input();
RemoveEventListener(Rml::EventId::Change, this, false);
input_el->RemoveEventListener(Rml::EventId::Change, this, false);
}
void ElementOptionTypeTextField::init_option(std::string& _config_key) {

View File

@ -12,6 +12,7 @@ static RecompElementConfig custom_elements[] = {
CUSTOM_ELEMENT("recomp-description", recompui::ElementDescription),
CUSTOM_ELEMENT("recomp-config-group", recompui::ElementConfigGroup),
CUSTOM_ELEMENT("recomp-config-option", recompui::ElementConfigOption),
CUSTOM_ELEMENT("recomp-option-type-button", recompui::ElementOptionTypeButton),
CUSTOM_ELEMENT("recomp-option-type-checkbox", recompui::ElementOptionTypeCheckbox),
CUSTOM_ELEMENT("recomp-option-type-color", recompui::ElementOptionTypeColor),
CUSTOM_ELEMENT("recomp-option-type-dropdown", recompui::ElementOptionTypeDropdown),

View File

@ -6,6 +6,7 @@
#include "elements/ElementConfigOption.h"
#include "elements/ElementConfigGroup.h"
#include "elements/ElementOptionTypeButton.h"
#include "elements/ElementOptionTypeCheckbox.h"
#include "elements/ElementOptionTypeColor.h"
#include "elements/ElementOptionTypeDropdown.h"