Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WidgetContainer memory management refacto #287

Draft
wants to merge 2 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/Overload/OvCore/include/OvCore/ECS/Actor.h
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ namespace OvCore::ECS
static OvTools::Eventing::Event<Actor&> DestroyedEvent;
static OvTools::Eventing::Event<Actor&> CreatedEvent;
static OvTools::Eventing::Event<Actor&, Actor&> AttachEvent;
static OvTools::Eventing::Event<Actor&> DettachEvent;
static OvTools::Eventing::Event<Actor&, Actor*> DettachEvent;

private:
/* Settings */
Expand Down
4 changes: 2 additions & 2 deletions Sources/Overload/OvCore/src/OvCore/ECS/Actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
OvTools::Eventing::Event<OvCore::ECS::Actor&> OvCore::ECS::Actor::DestroyedEvent;
OvTools::Eventing::Event<OvCore::ECS::Actor&> OvCore::ECS::Actor::CreatedEvent;
OvTools::Eventing::Event<OvCore::ECS::Actor&, OvCore::ECS::Actor&> OvCore::ECS::Actor::AttachEvent;
OvTools::Eventing::Event<OvCore::ECS::Actor&> OvCore::ECS::Actor::DettachEvent;
OvTools::Eventing::Event<OvCore::ECS::Actor&, OvCore::ECS::Actor*> OvCore::ECS::Actor::DettachEvent;

OvCore::ECS::Actor::Actor(int64_t p_actorID, const std::string & p_name, const std::string & p_tag, bool& p_playing) :
m_actorID(p_actorID),
Expand Down Expand Up @@ -131,7 +131,7 @@ void OvCore::ECS::Actor::SetParent(Actor& p_parent)

void OvCore::ECS::Actor::DetachFromParent()
{
DettachEvent.Invoke(*this);
DettachEvent.Invoke(*this, m_parent);

/* Remove the actor from the parent children list */
if (m_parent)
Expand Down
8 changes: 4 additions & 4 deletions Sources/Overload/OvCore/src/OvCore/ECS/Components/CCamera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,13 @@ void OvCore::ECS::Components::CCamera::OnInspector(OvUI::Internal::WidgetContain
auto currentProjectionMode = GetProjectionMode();

OvCore::Helpers::GUIDrawer::DrawScalar<float>(p_root, "Field of view", std::bind(&CCamera::GetFov, this), std::bind(&CCamera::SetFov, this, std::placeholders::_1));
auto& fovWidget = *p_root.GetWidgets()[p_root.GetWidgets().size() - 1].first;
auto& fovWidgetLabel = *p_root.GetWidgets()[p_root.GetWidgets().size() - 2].first;
auto& fovWidget = *p_root.GetWidgets()[p_root.GetWidgets().size() - 1];
auto& fovWidgetLabel = *p_root.GetWidgets()[p_root.GetWidgets().size() - 2];
fovWidget.enabled = fovWidgetLabel.enabled = currentProjectionMode == OvRendering::Settings::EProjectionMode::PERSPECTIVE;

OvCore::Helpers::GUIDrawer::DrawScalar<float>(p_root, "Size", std::bind(&CCamera::GetSize, this), std::bind(&CCamera::SetSize, this, std::placeholders::_1));
auto& sizeWidget = *p_root.GetWidgets()[p_root.GetWidgets().size() - 1].first;
auto& sizeWidgetLabel = *p_root.GetWidgets()[p_root.GetWidgets().size() - 2].first;
auto& sizeWidget = *p_root.GetWidgets()[p_root.GetWidgets().size() - 1];
auto& sizeWidgetLabel = *p_root.GetWidgets()[p_root.GetWidgets().size() - 2];
sizeWidget.enabled = sizeWidgetLabel.enabled = currentProjectionMode == OvRendering::Settings::EProjectionMode::ORTHOGRAPHIC;

OvCore::Helpers::GUIDrawer::DrawScalar<float>(p_root, "Near", std::bind(&CCamera::GetNear, this), std::bind(&CCamera::SetNear, this, std::placeholders::_1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ namespace OvEditor::Panels
* Detach the given actor linked widget from its parent widget
* @param p_actor
*/
void DetachFromParent(OvCore::ECS::Actor& p_actor);
void DetachFromParent(OvCore::ECS::Actor& p_actor, OvCore::ECS::Actor* p_parentActor);

/**
* Delete the widget referencing the given actor
Expand Down
59 changes: 34 additions & 25 deletions Sources/Overload/OvEditor/src/OvEditor/Panels/Hierarchy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,21 +185,37 @@ OvEditor::Panels::Hierarchy::Hierarchy
m_sceneRoot->AddPlugin<OvUI::Plugins::DDTarget<std::pair<OvCore::ECS::Actor*, OvUI::Widgets::Layout::TreeNode*>>>("Actor").DataReceivedEvent += [this](std::pair<OvCore::ECS::Actor*, OvUI::Widgets::Layout::TreeNode*> p_element)
{
if (p_element.second->HasParent())
p_element.second->GetParent()->UnconsiderWidget(*p_element.second);

m_sceneRoot->ConsiderWidget(*p_element.second);
p_element.second->GetParent()->TransferOwnership(*p_element.second, *m_sceneRoot);

p_element.first->DetachFromParent();
};
m_sceneRoot->AddPlugin<ActorContextualMenu>(nullptr, *m_sceneRoot);

EDITOR_EVENT(ActorSelectedEvent) += std::bind(&Hierarchy::SelectActorByInstance, this, std::placeholders::_1);
EDITOR_EVENT(ActorUnselectedEvent) += std::bind(&Hierarchy::UnselectActorsWidgets, this);

EDITOR_CONTEXT(sceneManager).SceneUnloadEvent += std::bind(&Hierarchy::Clear, this);
OvCore::ECS::Actor::CreatedEvent += std::bind(&Hierarchy::AddActorByInstance, this, std::placeholders::_1);
OvCore::ECS::Actor::DestroyedEvent += std::bind(&Hierarchy::DeleteActorByInstance, this, std::placeholders::_1);
EDITOR_EVENT(ActorSelectedEvent) += std::bind(&Hierarchy::SelectActorByInstance, this, std::placeholders::_1);
OvCore::ECS::Actor::AttachEvent += std::bind(&Hierarchy::AttachActorToParent, this, std::placeholders::_1);
OvCore::ECS::Actor::DettachEvent += std::bind(&Hierarchy::DetachFromParent, this, std::placeholders::_1);

OvCore::ECS::Actor::CreatedEvent += [this](OvCore::ECS::Actor& p_actor)
{
m_callbackQueue.push([this, &p_actor] { this->AddActorByInstance(p_actor); });
};

OvCore::ECS::Actor::DestroyedEvent += [this](OvCore::ECS::Actor& p_actor)
{
m_callbackQueue.push([this, &p_actor] { this->DeleteActorByInstance(p_actor); });
};

OvCore::ECS::Actor::AttachEvent += [this](OvCore::ECS::Actor& p_actor, OvCore::ECS::Actor& p_parentActor)
{
m_callbackQueue.push([this, &p_actor] { this->AttachActorToParent(p_actor); });
};

OvCore::ECS::Actor::DettachEvent += [this](OvCore::ECS::Actor& p_actor, OvCore::ECS::Actor* p_parentActor)
{
m_callbackQueue.push([this, &p_actor, p_parentActor] { this->DetachFromParent(p_actor, p_parentActor); });
};

}

void OvEditor::Panels::Hierarchy::Clear()
Expand All @@ -223,7 +239,7 @@ void OvEditor::Panels::Hierarchy::SelectActorByInstance(OvCore::ECS::Actor& p_ac
SelectActorByWidget(*result->second);
}

void OvEditor::Panels::Hierarchy::SelectActorByWidget(OvUI::Widgets::Layout::TreeNode & p_widget)
void OvEditor::Panels::Hierarchy::SelectActorByWidget(OvUI::Widgets::Layout::TreeNode& p_widget)
{
UnselectActorsWidgets();

Expand All @@ -235,44 +251,37 @@ void OvEditor::Panels::Hierarchy::SelectActorByWidget(OvUI::Widgets::Layout::Tre
}
}

void OvEditor::Panels::Hierarchy::AttachActorToParent(OvCore::ECS::Actor & p_actor)
void OvEditor::Panels::Hierarchy::AttachActorToParent(OvCore::ECS::Actor& p_actor)
{
auto actorWidget = m_widgetActorLink.find(&p_actor);

if (actorWidget != m_widgetActorLink.end())
{
auto widget = actorWidget->second;

if (widget->HasParent())
widget->GetParent()->UnconsiderWidget(*widget);
const auto parentWidget = m_widgetActorLink.at(p_actor.GetParent());
parentWidget->leaf = false;

if (p_actor.HasParent())
{
auto parentWidget = m_widgetActorLink.at(p_actor.GetParent());
parentWidget->leaf = false;
parentWidget->ConsiderWidget(*widget);
}
widget->GetParent()->TransferOwnership(*widget, *parentWidget);
}
}

void OvEditor::Panels::Hierarchy::DetachFromParent(OvCore::ECS::Actor & p_actor)
void OvEditor::Panels::Hierarchy::DetachFromParent(OvCore::ECS::Actor & p_actor, OvCore::ECS::Actor* p_parentActor)
{
if (auto actorWidget = m_widgetActorLink.find(&p_actor); actorWidget != m_widgetActorLink.end())
{
if (p_actor.HasParent() && p_actor.GetParent()->GetChildren().size() == 1)
if (p_parentActor && p_parentActor->GetChildren().size() <= 1)
{
if (auto parentWidget = m_widgetActorLink.find(p_actor.GetParent()); parentWidget != m_widgetActorLink.end())
if (const auto parentWidget = m_widgetActorLink.find(p_parentActor); parentWidget != m_widgetActorLink.end())
{
parentWidget->second->leaf = true;
}
}

auto widget = actorWidget->second;

if (widget->HasParent())
widget->GetParent()->UnconsiderWidget(*widget);

m_sceneRoot->ConsiderWidget(*widget);
if(widget->GetParent() != m_sceneRoot)
widget->GetParent()->TransferOwnership(*widget, *m_sceneRoot);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Overload/OvGame/include/OvGame/Utils/FPSCounter.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace OvGame::Utils
void Update(float p_deltaTime);

private:
OvUI::Widgets::Texts::TextColored m_text;
OvUI::Widgets::Texts::TextColored& m_text;

OvWindowing::Window& m_window;
float m_elapsed = 0.0f;
Expand Down
3 changes: 1 addition & 2 deletions Sources/Overload/OvGame/src/OvGame/Utils/FPSCounter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@

#include "OvGame/Utils/FPSCounter.h"

OvGame::Utils::FPSCounter::FPSCounter(OvWindowing::Window& p_window) : m_window(p_window)
OvGame::Utils::FPSCounter::FPSCounter(OvWindowing::Window& p_window) : m_text(CreateWidget<OvUI::Widgets::Texts::TextColored>()), m_window(p_window)
{
m_text.color = OvUI::Types::Color::Yellow;
m_defaultHorizontalAlignment = OvUI::Settings::EHorizontalAlignment::RIGHT;
m_defaultPosition = { static_cast<float>(m_window.GetSize().first) - 10.0f , 10.0f };
m_text.content = "999 FPS";
ConsiderWidget(m_text, false);
}

void OvGame::Utils::FPSCounter::Update(float p_deltaTime)
Expand Down
21 changes: 21 additions & 0 deletions Sources/Overload/OvTools/CallbackQueue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include <queue>
#include <functional>

namespace OvTools::Utils
{
class CallbackQueue : public std::queue<std::function<void()>>
{
public:
void Process()
{
while (!empty())
{
auto& callback = front();
callback();
pop();
}
}
};
}
21 changes: 6 additions & 15 deletions Sources/Overload/OvUI/include/OvUI/Internal/WidgetContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#pragma once

#include <memory>
#include <vector>

#include "OvUI/Widgets/AWidget.h"
Expand Down Expand Up @@ -40,17 +41,7 @@ namespace OvUI::Internal
*/
void RemoveAllWidgets();

/**
* Consider a widget
* @param p_manageMemory
*/
void ConsiderWidget(Widgets::AWidget& p_widget, bool p_manageMemory = true);

/**
* Unconsider a widget
* @param p_widget
*/
void UnconsiderWidget(Widgets::AWidget& p_widget);
void TransferOwnership(Widgets::AWidget& p_widget, WidgetContainer& p_widgetCoontainer);

/**
* Collect garbages by removing widgets marked as "Destroyed"
Expand All @@ -74,19 +65,19 @@ namespace OvUI::Internal
template <typename T, typename ... Args>
T& CreateWidget(Args&&... p_args)
{
m_widgets.emplace_back(new T(p_args...), Internal::EMemoryMode::INTERNAL_MANAGMENT);
T& instance = *reinterpret_cast<T*>(m_widgets.back().first);
m_widgets.emplace_back(std::make_unique<T>(p_args...));
T& instance = *static_cast<T*>(m_widgets.back().get());
instance.SetParent(this);
return instance;
}

/**
* Returns the widgets and their memory management mode
*/
std::vector<std::pair<OvUI::Widgets::AWidget*, Internal::EMemoryMode>>& GetWidgets();
std::vector<std::unique_ptr<Widgets::AWidget>>& GetWidgets();

protected:
std::vector<std::pair<OvUI::Widgets::AWidget*, Internal::EMemoryMode>> m_widgets;
std::vector<std::unique_ptr<Widgets::AWidget>> m_widgets;
bool m_reversedDrawOrder = false;
};
}
3 changes: 3 additions & 0 deletions Sources/Overload/OvUI/include/OvUI/Panels/APanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <vector>
#include <unordered_map>

#include "../../../../OvTools/CallbackQueue.h"
#include "OvUI/Internal/WidgetContainer.h"

namespace OvUI::Panels
Expand Down Expand Up @@ -42,8 +43,10 @@ namespace OvUI::Panels

protected:
std::string m_panelID;
OvTools::Utils::CallbackQueue m_callbackQueue;

private:
static uint64_t __PANEL_ID_INCREMENT;

};
}
3 changes: 3 additions & 0 deletions Sources/Overload/OvUI/include/OvUI/Widgets/Buttons/AButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ namespace OvUI::Widgets::Buttons
*/
class AButton : public AWidget
{
public:
AButton() = default;
virtual ~AButton() override = default;
protected:
void _Draw_Impl() override = 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace OvUI::Widgets::Buttons
* @param p_disabled
*/
Button(const std::string& p_label = "", const OvMaths::FVector2& p_size = OvMaths::FVector2(0.f, 0.f), bool p_disabled = false);

virtual ~Button() override = default;
protected:
void _Draw_Impl() override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace OvUI::Widgets::Buttons
* @param p_direction
*/
ButtonArrow(ImGuiDir p_direction = ImGuiDir_None);

virtual ~ButtonArrow() override = default;
protected:
void _Draw_Impl() override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace OvUI::Widgets::Buttons
* @param p_enableAlpha
*/
ButtonColored(const std::string& p_label = "", const Types::Color& p_color = {}, const OvMaths::FVector2& p_size =OvMaths::FVector2(0.f, 0.f), bool p_enableAlpha = true);

virtual ~ButtonColored() override = default;
protected:
void _Draw_Impl() override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace OvUI::Widgets::Buttons
* @param p_size
*/
ButtonImage(uint32_t p_textureID, const OvMaths::FVector2& p_size);

virtual ~ButtonImage() override = default;
protected:
void _Draw_Impl() override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace OvUI::Widgets::Buttons
* @param p_label
*/
ButtonSmall(const std::string& p_label = "");

virtual ~ButtonSmall() override = default;
protected:
void _Draw_Impl() override;

Expand Down
2 changes: 2 additions & 0 deletions Sources/Overload/OvUI/include/OvUI/Widgets/Drags/DragDouble.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,7 @@ namespace OvUI::Widgets::Drags
const std::string& p_label = "",
const std::string& p_format = "%.6f"
);

virtual ~DragDouble() override = default;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ namespace OvUI::Widgets::Drags
const std::string& p_label = "",
const std::string& p_format = "%.3f"
);
virtual ~DragFloat() override = default;
};
}
1 change: 1 addition & 0 deletions Sources/Overload/OvUI/include/OvUI/Widgets/Drags/DragInt.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ namespace OvUI::Widgets::Drags
const std::string& p_label = "",
const std::string& p_format = "%d"
);
virtual ~DragInt() override = default;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace OvUI::Widgets::Layout

for (auto it = m_widgets.begin(); it != m_widgets.end();)
{
it->first->Draw();
it->get()->Draw();

++it;

Expand Down
4 changes: 4 additions & 0 deletions Sources/Overload/OvUI/include/OvUI/Widgets/Layout/NewLine.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ namespace OvUI::Widgets::Layout
*/
class NewLine : public AWidget
{
public:
NewLine() = default;
virtual ~NewLine() override = default;

protected:
void _Draw_Impl() override;
};
Expand Down
2 changes: 2 additions & 0 deletions Sources/Overload/OvUI/include/OvUI/Widgets/Layout/Spacing.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ namespace OvUI::Widgets::Layout
* @param p_spaces
*/
Spacing(uint16_t p_spaces = 1);
virtual ~Spacing() override = default;


protected:
void _Draw_Impl() override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace OvUI::Widgets::Layout
* @param p_arrowClickToOpen
*/
TreeNode(const std::string& p_name = "", bool p_arrowClickToOpen = false);

virtual ~TreeNode() override = default;
/**
* Open the tree node
*/
Expand Down
Loading