Refactored to account for context changes.

This commit is contained in:
Dario 2025-01-18 22:58:43 -03:00 committed by Mr-Wiseguy
parent 921eb5aee3
commit e72085404b
7 changed files with 25 additions and 21 deletions

View file

@ -227,7 +227,7 @@ bool Element::is_enabled() const {
return enabled && !disabled_from_parent; return enabled && !disabled_from_parent;
} }
void Element::set_text(const std::string &text) { void Element::set_text(std::string_view text) {
base->SetInnerRML(std::string(text)); base->SetInnerRML(std::string(text));
} }

View file

@ -47,7 +47,7 @@ public:
void add_style(Style *style, const std::initializer_list<std::string_view> &style_names); void add_style(Style *style, const std::initializer_list<std::string_view> &style_names);
void set_enabled(bool enabled); void set_enabled(bool enabled);
bool is_enabled() const; bool is_enabled() const;
void set_text(const std::string &text); void set_text(std::string_view text);
void set_style_enabled(std::string_view style_name, bool enabled); void set_style_enabled(std::string_view style_name, bool enabled);
bool is_element() override { return true; } bool is_element() override { return true; }
float get_absolute_left(); float get_absolute_left();

View file

@ -67,16 +67,18 @@ namespace recompui {
set_flex(1.0f, 1.0f, 100.0f, Unit::Percent); set_flex(1.0f, 1.0f, 100.0f, Unit::Percent);
set_flex_direction(FlexDirection::Row); set_flex_direction(FlexDirection::Row);
value_label = new Label("0", LabelStyle::Small, this); ContextId context = get_current_context();
value_label = context.create_element<Label>("0", LabelStyle::Small, this);
value_label->set_margin_right(20.0f); value_label->set_margin_right(20.0f);
value_label->set_min_width(60.0f); value_label->set_min_width(60.0f);
value_label->set_max_width(60.0f); value_label->set_max_width(60.0f);
slider_element = new Element(this); slider_element = context.create_element<Element>(this);
slider_element->set_width(slider_width_dp); slider_element->set_width(slider_width_dp);
{ {
bar_element = new Clickable(slider_element, true); bar_element = context.create_element<Clickable>(slider_element, true);
bar_element->set_width(100.0f, Unit::Percent); bar_element->set_width(100.0f, Unit::Percent);
bar_element->set_height(2.0f); bar_element->set_height(2.0f);
bar_element->set_margin_top(8.0f); bar_element->set_margin_top(8.0f);
@ -84,7 +86,7 @@ namespace recompui {
bar_element->add_pressed_callback(std::bind(&Slider::bar_clicked, this, std::placeholders::_1, std::placeholders::_2)); bar_element->add_pressed_callback(std::bind(&Slider::bar_clicked, this, std::placeholders::_1, std::placeholders::_2));
bar_element->add_dragged_callback(std::bind(&Slider::bar_dragged, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)); bar_element->add_dragged_callback(std::bind(&Slider::bar_dragged, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
circle_element = new Clickable(slider_element, true); circle_element = context.create_element<Clickable>(slider_element, true);
circle_element->set_position(Position::Relative); circle_element->set_position(Position::Relative);
circle_element->set_width(16.0f); circle_element->set_width(16.0f);
circle_element->set_height(16.0f); circle_element->set_height(16.0f);

View file

@ -21,7 +21,7 @@ void ConfigOptionElement::process_event(const Event &e) {
ConfigOptionElement::ConfigOptionElement(Element *parent) : Element(parent, Events(EventType::Hover)) { ConfigOptionElement::ConfigOptionElement(Element *parent) : Element(parent, Events(EventType::Hover)) {
set_min_height(100.0f); set_min_height(100.0f);
name_label = new Label(LabelStyle::Normal, this); name_label = get_current_context().create_element<Label>(LabelStyle::Normal, this);
} }
ConfigOptionElement::~ConfigOptionElement() { ConfigOptionElement::~ConfigOptionElement() {
@ -53,7 +53,7 @@ void ConfigOptionSlider::slider_value_changed(double v) {
} }
ConfigOptionSlider::ConfigOptionSlider(Element *parent) : ConfigOptionElement(parent) { ConfigOptionSlider::ConfigOptionSlider(Element *parent) : ConfigOptionElement(parent) {
slider = new Slider(SliderType::Percent, this); slider = get_current_context().create_element<Slider>(SliderType::Percent, this);
slider->add_value_changed_callback(std::bind(&ConfigOptionSlider::slider_value_changed, this, std::placeholders::_1)); slider->add_value_changed_callback(std::bind(&ConfigOptionSlider::slider_value_changed, this, std::placeholders::_1));
} }
@ -103,25 +103,26 @@ ConfigSubMenu::ConfigSubMenu(Element *parent) : Element(parent) {
set_flex_direction(FlexDirection::Column); set_flex_direction(FlexDirection::Column);
set_height(100.0f, Unit::Percent); set_height(100.0f, Unit::Percent);
header_container = new Container(FlexDirection::Row, JustifyContent::FlexStart, this); recompui::ContextId context = get_current_context();
header_container = context.create_element<Container>(FlexDirection::Row, JustifyContent::FlexStart, this);
{ {
back_button = new Button("Back", ButtonStyle::Secondary, header_container); back_button = context.create_element<Button>("Back", ButtonStyle::Secondary, header_container);
back_button->add_pressed_callback(std::bind(&ConfigSubMenu::back_button_pressed, this)); back_button->add_pressed_callback(std::bind(&ConfigSubMenu::back_button_pressed, this));
title_label = new Label("Title", LabelStyle::Large, header_container); title_label = context.create_element<Label>("Title", LabelStyle::Large, header_container);
} }
body_container = new Container(FlexDirection::Row, JustifyContent::SpaceEvenly, this); body_container = context.create_element<Container>(FlexDirection::Row, JustifyContent::SpaceEvenly, this);
{ {
config_container = new Container(FlexDirection::Column, JustifyContent::Center, body_container); config_container = context.create_element<Container>(FlexDirection::Column, JustifyContent::Center, body_container);
config_container->set_display(Display::Block); config_container->set_display(Display::Block);
config_container->set_flex_basis(100.0f); config_container->set_flex_basis(100.0f);
config_container->set_align_items(AlignItems::Center); config_container->set_align_items(AlignItems::Center);
{ {
config_scroll_container = new ScrollContainer(ScrollDirection::Vertical, config_container); config_scroll_container = context.create_element<ScrollContainer>(ScrollDirection::Vertical, config_container);
} }
description_label = new Label("Description", LabelStyle::Small, body_container); description_label = context.create_element<Label>("Description", LabelStyle::Small, body_container);
description_label->set_min_width(800.0f); description_label->set_min_width(800.0f);
} }
} }
@ -152,7 +153,7 @@ void ConfigSubMenu::add_option(ConfigOptionElement *option, std::string_view nam
} }
void ConfigSubMenu::add_slider_option(std::string_view name, std::string_view description, double min, double max) { void ConfigSubMenu::add_slider_option(std::string_view name, std::string_view description, double min, double max) {
ConfigOptionSlider *option_slider = new ConfigOptionSlider(config_scroll_container); ConfigOptionSlider *option_slider = get_current_context().create_element<ConfigOptionSlider>(config_scroll_container);
option_slider->set_min_value(min); option_slider->set_min_value(min);
option_slider->set_max_value(max); option_slider->set_max_value(max);
add_option(option_slider, name, description); add_option(option_slider, name, description);
@ -174,7 +175,8 @@ ElementConfigSubMenu::ElementConfigSubMenu(const Rml::String &tag) : Rml::Elemen
SetProperty("height", "100%"); SetProperty("height", "100%");
recompui::Element this_compat(this); recompui::Element this_compat(this);
config_sub_menu = std::make_unique<ConfigSubMenu>(&this_compat); recompui::ContextId context = get_current_context();
config_sub_menu = context.create_element<ConfigSubMenu>(&this_compat);
} }
ElementConfigSubMenu::~ElementConfigSubMenu() { ElementConfigSubMenu::~ElementConfigSubMenu() {
@ -194,7 +196,7 @@ void ElementConfigSubMenu::set_quit_sub_menu_callback(std::function<void()> call
} }
ConfigSubMenu *ElementConfigSubMenu::get_config_sub_menu_element() const { ConfigSubMenu *ElementConfigSubMenu::get_config_sub_menu_element() const {
return config_sub_menu.get(); return config_sub_menu;
} }
} }

View file

@ -78,7 +78,7 @@ public:
void set_quit_sub_menu_callback(std::function<void()> callback); void set_quit_sub_menu_callback(std::function<void()> callback);
ConfigSubMenu *get_config_sub_menu_element() const; ConfigSubMenu *get_config_sub_menu_element() const;
private: private:
std::unique_ptr<ConfigSubMenu> config_sub_menu; ConfigSubMenu *config_sub_menu;
}; };
} }

View file

@ -195,7 +195,7 @@ ElementModMenu::ElementModMenu(const Rml::String &tag) : Rml::Element(tag) {
recompui::Element this_compat(this); recompui::Element this_compat(this);
recompui::ContextId context = get_current_context(); recompui::ContextId context = get_current_context();
context.create_element<ModMenu>(&this_compat); mod_menu = context.create_element<ModMenu>(&this_compat);
} }
ElementModMenu::~ElementModMenu() { ElementModMenu::~ElementModMenu() {

View file

@ -56,7 +56,7 @@ public:
virtual ~ElementModMenu(); virtual ~ElementModMenu();
void set_config_sub_menu(ConfigSubMenu *config_sub_menu); void set_config_sub_menu(ConfigSubMenu *config_sub_menu);
private: private:
std::unique_ptr<ModMenu> mod_menu; ModMenu *mod_menu;
}; };
} // namespace recompui } // namespace recompui