Sdk/BaseWidget: Add Destroy method

This commit is contained in:
Lynix 2017-01-11 19:14:32 +01:00
parent 6acf101d77
commit fd923ed58b
2 changed files with 24 additions and 1 deletions

View File

@ -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);

View File

@ -7,6 +7,7 @@
#include <NDK/Components/GraphicsComponent.hpp>
#include <NDK/Components/NodeComponent.hpp>
#include <NDK/World.hpp>
#include <algorithm>
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<BaseWidget>& widgetPtr) -> bool
{
return widgetPtr.get() == widget;
});
NazaraAssert(it != m_children.end(), "Child widget not found in parent");
m_children.erase(it);
}
}