Sdk/BaseWidget: Add Destroy method
This commit is contained in:
parent
6acf101d77
commit
fd923ed58b
|
|
@ -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);
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue