diff --git a/SDK/include/NDK/BaseWidget.inl b/SDK/include/NDK/BaseWidget.inl index aa2b222b6..1833a2224 100644 --- a/SDK/include/NDK/BaseWidget.inl +++ b/SDK/include/NDK/BaseWidget.inl @@ -5,10 +5,12 @@ #include #include #include +#include namespace Ndk { inline BaseWidget::BaseWidget() : + m_canvasIndex(std::numeric_limits::max()), m_backgroundColor(Nz::Color(230, 230, 230, 255)), m_canvas(nullptr), m_contentSize(50.f, 50.f), diff --git a/SDK/src/NDK/BaseWidget.cpp b/SDK/src/NDK/BaseWidget.cpp index dcaccd756..b7476dd30 100644 --- a/SDK/src/NDK/BaseWidget.cpp +++ b/SDK/src/NDK/BaseWidget.cpp @@ -25,7 +25,8 @@ namespace Ndk BaseWidget::~BaseWidget() { - m_canvas->UnregisterWidget(m_canvasIndex); + if (m_canvasIndex != std::numeric_limits::max()) + m_canvas->UnregisterWidget(m_canvasIndex); } inline void BaseWidget::EnableBackground(bool enable) diff --git a/SDK/src/NDK/Canvas.cpp b/SDK/src/NDK/Canvas.cpp index c06d320c1..b13b7ef6a 100644 --- a/SDK/src/NDK/Canvas.cpp +++ b/SDK/src/NDK/Canvas.cpp @@ -49,21 +49,23 @@ namespace Ndk void Canvas::UnregisterWidget(std::size_t index) { + WidgetBox& entry = m_widgetBoxes[index]; + + if (m_hoveredWidget == &entry) + m_hoveredWidget = nullptr; + + if (m_keyboardOwner == entry.widget) + m_keyboardOwner = nullptr; + if (m_widgetBoxes.size() > 1U) { - WidgetBox& entry = m_widgetBoxes[index]; WidgetBox& lastEntry = m_widgetBoxes.back(); - if (m_hoveredWidget == &entry) - m_hoveredWidget = nullptr; - - if (m_keyboardOwner == entry.widget) - m_keyboardOwner = nullptr; - entry = std::move(lastEntry); entry.widget->UpdateCanvasIndex(index); - m_widgetBoxes.pop_back(); } + + m_widgetBoxes.pop_back(); } void Canvas::OnMouseButtonPressed(const Nz::EventHandler* /*eventHandler*/, const Nz::WindowEvent::MouseButtonEvent& event)