diff --git a/SDK/include/NDK/BaseWidget.hpp b/SDK/include/NDK/BaseWidget.hpp index 6cfea3a8c..9d13499d9 100644 --- a/SDK/include/NDK/BaseWidget.hpp +++ b/SDK/include/NDK/BaseWidget.hpp @@ -95,8 +95,10 @@ namespace Ndk inline void DestroyChild(BaseWidget* widget); void DestroyChildren(); + void RegisterToCanvas(); inline void NotifyParentResized(const Nz::Vector2f& newSize); inline void UpdateCanvasIndex(std::size_t index); + void UnregisterFromCanvas(); std::size_t m_canvasIndex; std::vector m_entities; diff --git a/SDK/include/NDK/Canvas.hpp b/SDK/include/NDK/Canvas.hpp index db271051f..914287fa2 100644 --- a/SDK/include/NDK/Canvas.hpp +++ b/SDK/include/NDK/Canvas.hpp @@ -33,8 +33,6 @@ namespace Ndk Canvas& operator=(Canvas&&) = delete; protected: - void Layout() override; - void NotifyWidgetUpdate(std::size_t index); std::size_t RegisterWidget(BaseWidget* widget); diff --git a/SDK/include/NDK/Canvas.inl b/SDK/include/NDK/Canvas.inl index 173474bdf..83de51b97 100644 --- a/SDK/include/NDK/Canvas.inl +++ b/SDK/include/NDK/Canvas.inl @@ -12,6 +12,10 @@ namespace Ndk m_world(std::move(world)) { m_canvas = this; + m_widgetParent = nullptr; + + // Widgetception + m_canvasIndex = m_canvas->RegisterWidget(this); m_keyPressedSlot.Connect(eventHandler.OnKeyPressed, this, &Canvas::OnKeyPressed); m_keyReleasedSlot.Connect(eventHandler.OnKeyReleased, this, &Canvas::OnKeyReleased); @@ -20,12 +24,17 @@ namespace Ndk m_mouseMovedSlot.Connect(eventHandler.OnMouseMoved, this, &Canvas::OnMouseMoved); m_mouseLeftSlot.Connect(eventHandler.OnMouseLeft, this, &Canvas::OnMouseLeft); m_textEnteredSlot.Connect(eventHandler.OnTextEntered, this, &Canvas::OnTextEntered); + + SetPadding(0.f, 0.f, 0.f, 0.f); } inline Canvas::~Canvas() { - // Destroy children explicitly because they signal us when getting destroyed, and that can't happend after our own destruction + // Destroy children explicitly because they signal us when getting destroyed, and that can't happen after our own destruction DestroyChildren(); + + // Prevent our parent from trying to call us + m_canvasIndex = std::numeric_limits::max(); } inline const WorldHandle& Canvas::GetWorld() const diff --git a/SDK/src/NDK/BaseWidget.cpp b/SDK/src/NDK/BaseWidget.cpp index bdd3b93bb..a971139eb 100644 --- a/SDK/src/NDK/BaseWidget.cpp +++ b/SDK/src/NDK/BaseWidget.cpp @@ -21,13 +21,12 @@ namespace Ndk m_widgetParent = parent; m_world = m_canvas->GetWorld(); - m_canvasIndex = m_canvas->RegisterWidget(this); + RegisterToCanvas(); } BaseWidget::~BaseWidget() { - if (m_canvasIndex != std::numeric_limits::max()) - m_canvas->UnregisterWidget(m_canvasIndex); + UnregisterFromCanvas(); } void BaseWidget::Destroy() @@ -88,6 +87,11 @@ namespace Ndk { m_visible = show; + if (m_visible) + RegisterToCanvas(); + else + UnregisterFromCanvas(); + for (const EntityHandle& entity : m_entities) entity->Enable(show); @@ -182,4 +186,20 @@ namespace Ndk { m_children.clear(); } + + void BaseWidget::RegisterToCanvas() + { + NazaraAssert(m_canvasIndex == std::numeric_limits::max(), "Widget is already registered to canvas"); + + m_canvasIndex = m_canvas->RegisterWidget(this); + } + + void BaseWidget::UnregisterFromCanvas() + { + if (m_canvasIndex != std::numeric_limits::max()) + { + m_canvas->UnregisterWidget(m_canvasIndex); + m_canvasIndex = std::numeric_limits::max(); + } + } } diff --git a/SDK/src/NDK/Canvas.cpp b/SDK/src/NDK/Canvas.cpp index 2aac8846c..74178d49a 100644 --- a/SDK/src/NDK/Canvas.cpp +++ b/SDK/src/NDK/Canvas.cpp @@ -14,14 +14,6 @@ namespace Ndk { } - void Canvas::Layout() - { - if (m_backgroundEntity) - { - m_backgroundSprite->SetSize(m_contentSize.x + m_padding.left + m_padding.right, m_contentSize.y + m_padding.top + m_padding.bottom); - } - } - void Canvas::NotifyWidgetUpdate(std::size_t index) { WidgetBox& entry = m_widgetBoxes[index];