From fd923ed58b252e9de8ddd55749eec2451c5b1125 Mon Sep 17 00:00:00 2001 From: Lynix Date: Wed, 11 Jan 2017 19:14:32 +0100 Subject: [PATCH] Sdk/BaseWidget: Add Destroy method --- SDK/include/NDK/BaseWidget.hpp | 3 +++ SDK/src/NDK/BaseWidget.cpp | 22 +++++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/SDK/include/NDK/BaseWidget.hpp b/SDK/include/NDK/BaseWidget.hpp index e2fe6d438..76def4b1b 100644 --- a/SDK/include/NDK/BaseWidget.hpp +++ b/SDK/include/NDK/BaseWidget.hpp @@ -37,6 +37,8 @@ namespace Ndk inline void Center(); + inline void Destroy(); + void EnableBackground(bool enable); //virtual BaseWidget* Clone() const = 0; @@ -84,6 +86,7 @@ namespace Ndk private: inline BaseWidget(); + inline void DestroyChild(BaseWidget* widget); inline void NotifyParentResized(const Nz::Vector2f& newSize); inline void UpdateCanvasIndex(std::size_t index); diff --git a/SDK/src/NDK/BaseWidget.cpp b/SDK/src/NDK/BaseWidget.cpp index b7476dd30..c627019ea 100644 --- a/SDK/src/NDK/BaseWidget.cpp +++ b/SDK/src/NDK/BaseWidget.cpp @@ -7,6 +7,7 @@ #include #include #include +#include namespace Ndk { @@ -29,7 +30,14 @@ namespace Ndk m_canvas->UnregisterWidget(m_canvasIndex); } - inline void BaseWidget::EnableBackground(bool enable) + void BaseWidget::Destroy() + { + NazaraAssert(this != m_canvas, "Canvas cannot be destroyed by calling Destroy()"); + + m_widgetParent->DestroyChild(this); //< This does delete us + } + + void BaseWidget::EnableBackground(bool enable) { if (m_backgroundEntity.IsValid() == enable) return; @@ -134,4 +142,16 @@ namespace Ndk void BaseWidget::OnTextEntered(char32_t character, bool repeated) { } + + void BaseWidget::DestroyChild(BaseWidget* widget) + { + auto it = std::find_if(m_children.begin(), m_children.end(), [widget] (const std::unique_ptr& widgetPtr) -> bool + { + return widgetPtr.get() == widget; + }); + + NazaraAssert(it != m_children.end(), "Child widget not found in parent"); + + m_children.erase(it); + } }