From 110e7acbb99314d66e530254be73e8f87a074dea Mon Sep 17 00:00:00 2001 From: thecozies <79979276+thecozies@users.noreply.github.com> Date: Mon, 2 Sep 2024 10:52:20 -0500 Subject: [PATCH] Button config type + callback wip --- CMakeLists.txt | 1 + config_example.cheats.en_us.json | 4 ++ config_example.cheats.json | 14 ++++- src/ui/elements/ElementConfigOption.cpp | 7 +++ src/ui/elements/ElementOptionTypeButton.cpp | 61 +++++++++++++++++++ src/ui/elements/ElementOptionTypeButton.h | 22 +++++++ .../elements/ElementOptionTypeTextField.cpp | 2 +- src/ui/ui_elements.cpp | 1 + src/ui/ui_elements.h | 1 + 9 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 src/ui/elements/ElementOptionTypeButton.cpp create mode 100644 src/ui/elements/ElementOptionTypeButton.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 03b7f2b..1152660 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/config_example.cheats.en_us.json b/config_example.cheats.en_us.json index 70dca26..44f9c1b 100644 --- a/config_example.cheats.en_us.json +++ b/config_example.cheats.en_us.json @@ -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", diff --git a/config_example.cheats.json b/config_example.cheats.json index 7b25d94..be48ff5 100644 --- a/config_example.cheats.json +++ b/config_example.cheats.json @@ -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" } ] }, diff --git a/src/ui/elements/ElementConfigOption.cpp b/src/ui/elements/ElementConfigOption.cpp index 6297269..990f4d2 100644 --- a/src/ui/elements/ElementConfigOption.cpp +++ b/src/ui/elements/ElementConfigOption.cpp @@ -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(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(doc, wrapper, "recomp-option-type-checkbox", config_key); break; diff --git a/src/ui/elements/ElementOptionTypeButton.cpp b/src/ui/elements/ElementOptionTypeButton.cpp new file mode 100644 index 0000000..ffdfbcc --- /dev/null +++ b/src/ui/elements/ElementOptionTypeButton.cpp @@ -0,0 +1,61 @@ + +#include "ElementOptionTypeButton.h" + +#include + +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("translations/" + config_key) + ); + + std::string variantClass = recomp::config::get_value_in_json_with_default(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 diff --git a/src/ui/elements/ElementOptionTypeButton.h b/src/ui/elements/ElementOptionTypeButton.h new file mode 100644 index 0000000..2152b6f --- /dev/null +++ b/src/ui/elements/ElementOptionTypeButton.h @@ -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 diff --git a/src/ui/elements/ElementOptionTypeTextField.cpp b/src/ui/elements/ElementOptionTypeTextField.cpp index 2cae204..7be155e 100644 --- a/src/ui/elements/ElementOptionTypeTextField.cpp +++ b/src/ui/elements/ElementOptionTypeTextField.cpp @@ -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) { diff --git a/src/ui/ui_elements.cpp b/src/ui/ui_elements.cpp index 3510d1c..8286102 100644 --- a/src/ui/ui_elements.cpp +++ b/src/ui/ui_elements.cpp @@ -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), diff --git a/src/ui/ui_elements.h b/src/ui/ui_elements.h index 6bdb734..6069c54 100644 --- a/src/ui/ui_elements.h +++ b/src/ui/ui_elements.h @@ -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"