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 {
protected:
ButtonStyle style = ButtonStyle::Primary;
std::unique_ptr<Element> floater;
std::list<std::function<void()>> pressed_callbacks;
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));
if (parent->owner) {
parent->add_child(this);
}
register_event_listeners(events_enabled);
}
Element::~Element() {
children.clear();
if (owner) {
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) {
assert(base != nullptr);

View File

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

View File

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

View File

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

View File

@ -11,40 +11,40 @@ ModDetailsPanel::ModDetailsPanel(Element *parent) : Element(parent) {
set_border_bottom_right_radius(16.0f);
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_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_height(100.0f);
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_margin_left(32.0f);
header_details_container->set_overflow(Overflow::Hidden);
{
title_label = std::make_unique<recompui::Label>(LabelStyle::Large, header_details_container.get());
version_label = std::make_unique<recompui::Label>(LabelStyle::Normal, header_details_container.get());
title_label = new Label(LabelStyle::Large, header_details_container);
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);
{
description_label = std::make_unique<recompui::Label>(LabelStyle::Normal, body_container.get());
authors_label = std::make_unique<recompui::Label>(LabelStyle::Normal, body_container.get());
buttons_container = std::make_unique<recompui::Container>(FlexDirection::Row, JustifyContent::SpaceAround, body_container.get());
description_label = new Label(LabelStyle::Normal, body_container);
authors_label = new Label(LabelStyle::Normal, body_container);
buttons_container = new Container(FlexDirection::Row, JustifyContent::SpaceAround, body_container);
buttons_container->set_padding_left(16.0f);
{
enable_toggle = std::make_unique<recompui::Toggle>(buttons_container.get());
configure_button = std::make_unique<recompui::Button>("Configure", recompui::ButtonStyle::Secondary, buttons_container.get());
erase_button = std::make_unique<recompui::Button>("Erase", recompui::ButtonStyle::Secondary, buttons_container.get());
enable_toggle = new Toggle(buttons_container);
configure_button = new Button("Configure", recompui::ButtonStyle::Secondary, buttons_container);
erase_button = new Button("Erase", recompui::ButtonStyle::Secondary, buttons_container);
}
}
}

View File

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

View File

@ -31,22 +31,22 @@ ModEntry::ModEntry(Element *parent, const recomp::mods::ModDetails &details, uin
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_height(100.0f);
thumbnail_image->set_min_width(100.0f);
thumbnail_image->set_min_height(100.0f);
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_height(100.0f);
body_container->set_margin_left(16.0f);
body_container->set_overflow(Overflow::Hidden);
{
name_label = std::make_unique<Label>(details.mod_id, LabelStyle::Normal, body_container.get());
description_label = std::make_unique<Label>("Short description of mod here.", LabelStyle::Small, body_container.get());
name_label = new Label(details.mod_id, LabelStyle::Normal, body_container);
description_label = new Label("Short description of mod here.", LabelStyle::Small, body_container);
} // body_container
} // this
}
@ -81,11 +81,15 @@ void ModMenu::refresh_mods() {
void ModMenu::create_mod_list() {
// Clear the contents of the list scroll.
for (ModEntry *entry : mod_entries) {
delete entry;
}
mod_entries.clear();
// Create the child elements for the list scroll.
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);
@ -103,12 +107,12 @@ ModMenu::ModMenu(Element *parent) : Element(parent) {
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_width(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_flex_basis_percentage(100.0f);
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_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
mod_details_panel = std::make_unique<ModDetailsPanel>(body_container.get());
mod_details_panel = new ModDetailsPanel(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_align_items(recompui::AlignItems::Center);
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_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));
} // footer_container
} // this

View File

@ -11,45 +11,45 @@ class ModMenu;
class ModEntry : public Element {
public:
ModEntry(Element *parent, const recomp::mods::ModDetails &details, uint32_t mod_index, ModMenu *mod_menu);
virtual ~ModEntry();
ModEntry(Element *parent, const recomp::mods::ModDetails &details, uint32_t mod_index, ModMenu *mod_menu);
virtual ~ModEntry();
protected:
virtual void process_event(const Event &e);
virtual void process_event(const Event &e);
private:
uint32_t mod_index;
ModMenu *mod_menu;
std::unique_ptr<Image> thumbnail_image;
std::unique_ptr<Container> body_container;
std::unique_ptr<Label> name_label;
std::unique_ptr<Label> description_label;
uint32_t mod_index = 0;
ModMenu *mod_menu = nullptr;
Image *thumbnail_image = nullptr;
Container *body_container = nullptr;
Label *name_label = nullptr;
Label *description_label = nullptr;
};
class ModMenu : public Element {
public:
ModMenu(Element *parent);
ModMenu(Element *parent);
virtual ~ModMenu();
void set_active_mod(uint32_t mod_index);
void set_active_mod(uint32_t mod_index);
private:
void refresh_mods();
void create_mod_list();
std::unique_ptr<Container> body_container;
std::unique_ptr<Container> list_container;
std::unique_ptr<ScrollContainer> list_scroll_container;
std::unique_ptr<ModDetailsPanel> mod_details_panel;
std::unique_ptr<Container> footer_container;
std::unique_ptr<Button> refresh_button;
std::vector<std::unique_ptr<ModEntry>> mod_entries;
Container *body_container = nullptr;
Container *list_container = nullptr;
ScrollContainer *list_scroll_container = nullptr;
ModDetailsPanel *mod_details_panel = nullptr;
Container *footer_container = nullptr;
Button *refresh_button = nullptr;
std::vector<ModEntry *> mod_entries;
std::vector<recomp::mods::ModDetails> mod_details{};
std::string game_mod_id;
};
class ElementModMenu : public Rml::Element {
public:
ElementModMenu(const Rml::String& tag);
virtual ~ElementModMenu();
ElementModMenu(const Rml::String& tag);
virtual ~ElementModMenu();
private:
std::unique_ptr<ModMenu> mod_menu;
std::unique_ptr<ModMenu> mod_menu;
};
} // namespace recompui