Take ownership of created pointers on Element class.

This commit is contained in:
Dario 2025-01-12 22:48:32 -03:00 committed by Mr-Wiseguy
parent e037e1a8c6
commit 319788b5ba
9 changed files with 83 additions and 65 deletions

View file

@ -12,7 +12,6 @@ namespace recompui {
class Button : public Element { class Button : public Element {
protected: protected:
ButtonStyle style = ButtonStyle::Primary; ButtonStyle style = ButtonStyle::Primary;
std::unique_ptr<Element> floater;
std::list<std::function<void()>> pressed_callbacks; std::list<std::function<void()>> pressed_callbacks;
bool hovered = false; bool hovered = false;

View file

@ -81,15 +81,27 @@ Element::Element(Element *parent, uint32_t events_enabled, Rml::String base_clas
base = parent->base->AppendChild(parent->base->GetOwnerDocument()->CreateElement(base_class)); base = parent->base->AppendChild(parent->base->GetOwnerDocument()->CreateElement(base_class));
if (parent->owner) {
parent->add_child(this);
}
register_event_listeners(events_enabled); register_event_listeners(events_enabled);
} }
Element::~Element() { Element::~Element() {
children.clear();
if (owner) { if (owner) {
base->GetParentNode()->RemoveChild(base); base->GetParentNode()->RemoveChild(base);
} }
} }
void Element::add_child(Element *child) {
assert(child != nullptr);
children.emplace_back(child);
}
void Element::set_property(Rml::PropertyId property_id, const Rml::Property &property, Animation animation) { void Element::set_property(Rml::PropertyId property_id, const Rml::Property &property, Animation animation) {
assert(base != nullptr); assert(base != nullptr);

View file

@ -154,7 +154,9 @@ struct Animation {
}; };
class Element : public Rml::EventListener { class Element : public Rml::EventListener {
friend class Element;
private: private:
void add_child(Element *child);
void set_property(Rml::PropertyId property_id, const Rml::Property &property, Animation animation = Animation()); void set_property(Rml::PropertyId property_id, const Rml::Property &property, Animation animation = Animation());
void register_event_listeners(uint32_t events_enabled); void register_event_listeners(uint32_t events_enabled);
@ -162,6 +164,7 @@ private:
virtual void ProcessEvent(Rml::Event &event) override; virtual void ProcessEvent(Rml::Event &event) override;
protected: protected:
Rml::Element *base; Rml::Element *base;
std::vector<std::unique_ptr<Element>> children;
bool owner; bool owner;
virtual void process_event(const Event &e); virtual void process_event(const Event &e);

View file

@ -12,7 +12,7 @@ namespace recompui {
set_cursor(Cursor::Pointer); set_cursor(Cursor::Pointer);
set_border_width(2.0f); set_border_width(2.0f);
floater = std::make_unique<Element>(this); floater = new Element(this);
floater->set_position(Position::Relative); floater->set_position(Position::Relative);
floater->set_top(2.0f); floater->set_top(2.0f);
floater->set_width(80.0f); floater->set_width(80.0f);

View file

@ -6,7 +6,7 @@ namespace recompui {
class Toggle : public Element { class Toggle : public Element {
protected: protected:
std::unique_ptr<Element> floater; Element *floater;
std::list<std::function<void(bool)>> checked_callbacks; std::list<std::function<void(bool)>> checked_callbacks;
bool checked = false; bool checked = false;
bool hovered = false; bool hovered = false;

View file

@ -11,40 +11,40 @@ ModDetailsPanel::ModDetailsPanel(Element *parent) : Element(parent) {
set_border_bottom_right_radius(16.0f); set_border_bottom_right_radius(16.0f);
set_background_color(Color{ 190, 184, 219, 25 }); set_background_color(Color{ 190, 184, 219, 25 });
header_container = std::make_unique<recompui::Container>(FlexDirection::Row, JustifyContent::FlexStart, this); header_container = new Container(FlexDirection::Row, JustifyContent::FlexStart, this);
header_container->set_padding(16.0f); header_container->set_padding(16.0f);
header_container->set_background_color(Color{ 0, 0, 0, 89 }); header_container->set_background_color(Color{ 0, 0, 0, 89 });
{ {
thumbnail_container = std::make_unique<recompui::Container>(FlexDirection::Column, JustifyContent::SpaceEvenly, header_container.get()); thumbnail_container = new Container(FlexDirection::Column, JustifyContent::SpaceEvenly, header_container);
{ {
thumbnail_image = std::make_unique<recompui::Image>(thumbnail_container.get()); thumbnail_image = new Image(thumbnail_container);
thumbnail_image->set_width(100.0f); thumbnail_image->set_width(100.0f);
thumbnail_image->set_height(100.0f); thumbnail_image->set_height(100.0f);
thumbnail_image->set_background_color(Color{ 190, 184, 219, 25 }); thumbnail_image->set_background_color(Color{ 190, 184, 219, 25 });
} }
header_details_container = std::make_unique<recompui::Container>(FlexDirection::Column, JustifyContent::SpaceEvenly, header_container.get()); header_details_container = new Container(FlexDirection::Column, JustifyContent::SpaceEvenly, header_container);
header_details_container->set_width(100.0f, Unit::Percent); header_details_container->set_width(100.0f, Unit::Percent);
header_details_container->set_margin_left(32.0f); header_details_container->set_margin_left(32.0f);
header_details_container->set_overflow(Overflow::Hidden); header_details_container->set_overflow(Overflow::Hidden);
{ {
title_label = std::make_unique<recompui::Label>(LabelStyle::Large, header_details_container.get()); title_label = new Label(LabelStyle::Large, header_details_container);
version_label = std::make_unique<recompui::Label>(LabelStyle::Normal, header_details_container.get()); version_label = new Label(LabelStyle::Normal, header_details_container);
} }
} }
body_container = std::make_unique<recompui::Container>(FlexDirection::Column, JustifyContent::FlexStart, this); body_container = new recompui::Container(FlexDirection::Column, JustifyContent::FlexStart, this);
body_container->set_padding_left(16.0f); body_container->set_padding_left(16.0f);
{ {
description_label = std::make_unique<recompui::Label>(LabelStyle::Normal, body_container.get()); description_label = new Label(LabelStyle::Normal, body_container);
authors_label = std::make_unique<recompui::Label>(LabelStyle::Normal, body_container.get()); authors_label = new Label(LabelStyle::Normal, body_container);
buttons_container = std::make_unique<recompui::Container>(FlexDirection::Row, JustifyContent::SpaceAround, body_container.get()); buttons_container = new Container(FlexDirection::Row, JustifyContent::SpaceAround, body_container);
buttons_container->set_padding_left(16.0f); buttons_container->set_padding_left(16.0f);
{ {
enable_toggle = std::make_unique<recompui::Toggle>(buttons_container.get()); enable_toggle = new Toggle(buttons_container);
configure_button = std::make_unique<recompui::Button>("Configure", recompui::ButtonStyle::Secondary, buttons_container.get()); configure_button = new Button("Configure", recompui::ButtonStyle::Secondary, buttons_container);
erase_button = std::make_unique<recompui::Button>("Erase", recompui::ButtonStyle::Secondary, buttons_container.get()); erase_button = new Button("Erase", recompui::ButtonStyle::Secondary, buttons_container);
} }
} }
} }

View file

@ -17,19 +17,19 @@ public:
void set_mod_details(const recomp::mods::ModDetails& details); void set_mod_details(const recomp::mods::ModDetails& details);
private: private:
recomp::mods::ModDetails cur_details; recomp::mods::ModDetails cur_details;
std::unique_ptr<recompui::Container> thumbnail_container; Container *thumbnail_container = nullptr;
std::unique_ptr<recompui::Image> thumbnail_image; Image *thumbnail_image = nullptr;
std::unique_ptr<recompui::Container> header_container; Container *header_container = nullptr;
std::unique_ptr<recompui::Container> header_details_container; Container *header_details_container = nullptr;
std::unique_ptr<recompui::Label> title_label; Label *title_label = nullptr;
std::unique_ptr<recompui::Label> version_label; Label *version_label = nullptr;
std::unique_ptr<recompui::Container> body_container; Container *body_container = nullptr;
std::unique_ptr<recompui::Label> description_label; Label *description_label = nullptr;
std::unique_ptr<recompui::Label> authors_label; Label *authors_label = nullptr;
std::unique_ptr<recompui::Container> buttons_container; Container *buttons_container = nullptr;
std::unique_ptr<recompui::Toggle> enable_toggle; Toggle *enable_toggle = nullptr;
std::unique_ptr<recompui::Button> configure_button; Button *configure_button = nullptr;
std::unique_ptr<recompui::Button> erase_button; Button *erase_button = nullptr;
}; };
} // namespace recompui } // namespace recompui

View file

@ -31,22 +31,22 @@ ModEntry::ModEntry(Element *parent, const recomp::mods::ModDetails &details, uin
set_cursor(Cursor::Pointer); set_cursor(Cursor::Pointer);
{ {
thumbnail_image = std::make_unique<Image>(this); thumbnail_image = new Image(this);
thumbnail_image->set_width(100.0f); thumbnail_image->set_width(100.0f);
thumbnail_image->set_height(100.0f); thumbnail_image->set_height(100.0f);
thumbnail_image->set_min_width(100.0f); thumbnail_image->set_min_width(100.0f);
thumbnail_image->set_min_height(100.0f); thumbnail_image->set_min_height(100.0f);
thumbnail_image->set_background_color(Color{ 190, 184, 219, 25 }); thumbnail_image->set_background_color(Color{ 190, 184, 219, 25 });
body_container = std::make_unique<Container>(FlexDirection::Column, JustifyContent::FlexStart, this); body_container = new Container(FlexDirection::Column, JustifyContent::FlexStart, this);
body_container->set_width_auto(); body_container->set_width_auto();
body_container->set_height(100.0f); body_container->set_height(100.0f);
body_container->set_margin_left(16.0f); body_container->set_margin_left(16.0f);
body_container->set_overflow(Overflow::Hidden); body_container->set_overflow(Overflow::Hidden);
{ {
name_label = std::make_unique<Label>(details.mod_id, LabelStyle::Normal, body_container.get()); name_label = new Label(details.mod_id, LabelStyle::Normal, body_container);
description_label = std::make_unique<Label>("Short description of mod here.", LabelStyle::Small, body_container.get()); description_label = new Label("Short description of mod here.", LabelStyle::Small, body_container);
} // body_container } // body_container
} // this } // this
} }
@ -81,11 +81,15 @@ void ModMenu::refresh_mods() {
void ModMenu::create_mod_list() { void ModMenu::create_mod_list() {
// Clear the contents of the list scroll. // Clear the contents of the list scroll.
for (ModEntry *entry : mod_entries) {
delete entry;
}
mod_entries.clear(); mod_entries.clear();
// Create the child elements for the list scroll. // Create the child elements for the list scroll.
for (size_t mod_index = 0; mod_index < mod_details.size(); mod_index++) { for (size_t mod_index = 0; mod_index < mod_details.size(); mod_index++) {
mod_entries.emplace_back(std::make_unique<ModEntry>(list_scroll_container.get(), mod_details[mod_index], mod_index, this)); mod_entries.emplace_back(new ModEntry(list_scroll_container, mod_details[mod_index], mod_index, this));
} }
set_active_mod(0); set_active_mod(0);
@ -103,12 +107,12 @@ ModMenu::ModMenu(Element *parent) : Element(parent) {
set_height(100.0f, Unit::Percent); set_height(100.0f, Unit::Percent);
{ {
body_container = std::make_unique<Container>(FlexDirection::Row, JustifyContent::FlexStart, this); body_container = new Container(FlexDirection::Row, JustifyContent::FlexStart, this);
body_container->set_flex(1.0f, 1.0f, 100.0f); body_container->set_flex(1.0f, 1.0f, 100.0f);
body_container->set_width(100.0f, Unit::Percent); body_container->set_width(100.0f, Unit::Percent);
body_container->set_height(100.0f, Unit::Percent); body_container->set_height(100.0f, Unit::Percent);
{ {
list_container = std::make_unique<Container>(FlexDirection::Column, JustifyContent::Center, body_container.get()); list_container = new Container(FlexDirection::Column, JustifyContent::Center, body_container);
list_container->set_display(Display::Block); list_container->set_display(Display::Block);
list_container->set_flex_basis_percentage(100.0f); list_container->set_flex_basis_percentage(100.0f);
list_container->set_align_items(AlignItems::Center); list_container->set_align_items(AlignItems::Center);
@ -116,14 +120,14 @@ ModMenu::ModMenu(Element *parent) : Element(parent) {
list_container->set_background_color(Color{ 0, 0, 0, 89 }); list_container->set_background_color(Color{ 0, 0, 0, 89 });
list_container->set_border_bottom_left_radius(16.0f); list_container->set_border_bottom_left_radius(16.0f);
{ {
list_scroll_container = std::make_unique<ScrollContainer>(ScrollDirection::Vertical, list_container.get()); list_scroll_container = new ScrollContainer(ScrollDirection::Vertical, list_container);
} // list_container } // list_container
mod_details_panel = std::make_unique<ModDetailsPanel>(body_container.get()); mod_details_panel = new ModDetailsPanel(body_container);
} // body_container } // body_container
footer_container = std::make_unique<recompui::Container>(FlexDirection::Row, JustifyContent::SpaceBetween, this); footer_container = new Container(FlexDirection::Row, JustifyContent::SpaceBetween, this);
footer_container->set_width(100.0f, recompui::Unit::Percent); footer_container->set_width(100.0f, recompui::Unit::Percent);
footer_container->set_align_items(recompui::AlignItems::Center); footer_container->set_align_items(recompui::AlignItems::Center);
footer_container->set_background_color(Color{ 0, 0, 0, 89 }); footer_container->set_background_color(Color{ 0, 0, 0, 89 });
@ -133,7 +137,7 @@ ModMenu::ModMenu(Element *parent) : Element(parent) {
footer_container->set_border_bottom_left_radius(16.0f); footer_container->set_border_bottom_left_radius(16.0f);
footer_container->set_border_bottom_right_radius(16.0f); footer_container->set_border_bottom_right_radius(16.0f);
{ {
refresh_button = std::make_unique<recompui::Button>("Refresh", recompui::ButtonStyle::Primary, footer_container.get()); refresh_button = new Button("Refresh", recompui::ButtonStyle::Primary, footer_container);
refresh_button->add_pressed_callback(std::bind(&ModMenu::refresh_mods, this)); refresh_button->add_pressed_callback(std::bind(&ModMenu::refresh_mods, this));
} // footer_container } // footer_container
} // this } // this

View file

@ -16,12 +16,12 @@ public:
protected: protected:
virtual void process_event(const Event &e); virtual void process_event(const Event &e);
private: private:
uint32_t mod_index; uint32_t mod_index = 0;
ModMenu *mod_menu; ModMenu *mod_menu = nullptr;
std::unique_ptr<Image> thumbnail_image; Image *thumbnail_image = nullptr;
std::unique_ptr<Container> body_container; Container *body_container = nullptr;
std::unique_ptr<Label> name_label; Label *name_label = nullptr;
std::unique_ptr<Label> description_label; Label *description_label = nullptr;
}; };
class ModMenu : public Element { class ModMenu : public Element {
@ -33,13 +33,13 @@ private:
void refresh_mods(); void refresh_mods();
void create_mod_list(); void create_mod_list();
std::unique_ptr<Container> body_container; Container *body_container = nullptr;
std::unique_ptr<Container> list_container; Container *list_container = nullptr;
std::unique_ptr<ScrollContainer> list_scroll_container; ScrollContainer *list_scroll_container = nullptr;
std::unique_ptr<ModDetailsPanel> mod_details_panel; ModDetailsPanel *mod_details_panel = nullptr;
std::unique_ptr<Container> footer_container; Container *footer_container = nullptr;
std::unique_ptr<Button> refresh_button; Button *refresh_button = nullptr;
std::vector<std::unique_ptr<ModEntry>> mod_entries; std::vector<ModEntry *> mod_entries;
std::vector<recomp::mods::ModDetails> mod_details{}; std::vector<recomp::mods::ModDetails> mod_details{};
std::string game_mod_id; std::string game_mod_id;
}; };