Sdk/BaseWidget: Cleanup canvas index

This commit is contained in:
Lynix 2017-01-19 23:09:23 +01:00
parent e4b6f8e126
commit 6ba35700bf
4 changed files with 27 additions and 11 deletions

View File

@ -15,6 +15,7 @@
#include <Nazara/Utility/Event.hpp>
#include <Nazara/Utility/Mouse.hpp>
#include <Nazara/Utility/Node.hpp>
#include <limits>
namespace Ndk
{
@ -95,11 +96,14 @@ namespace Ndk
inline void DestroyChild(BaseWidget* widget);
void DestroyChildren();
void RegisterToCanvas();
inline bool IsRegisteredToCanvas() const;
inline void NotifyParentResized(const Nz::Vector2f& newSize);
void RegisterToCanvas();
inline void UpdateCanvasIndex(std::size_t index);
void UnregisterFromCanvas();
static constexpr std::size_t InvalidCanvasIndex = std::numeric_limits<std::size_t>::max();
std::size_t m_canvasIndex;
std::vector<EntityOwner> m_entities;
std::vector<std::unique_ptr<BaseWidget>> m_children;

View File

@ -5,12 +5,11 @@
#include <NDK/BaseWidget.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Math/Algorithm.hpp>
#include <limits>
namespace Ndk
{
inline BaseWidget::BaseWidget() :
m_canvasIndex(std::numeric_limits<std::size_t>::max()),
m_canvasIndex(InvalidCanvasIndex),
m_backgroundColor(Nz::Color(230, 230, 230, 255)),
m_canvas(nullptr),
m_contentSize(50.f, 50.f),
@ -98,6 +97,11 @@ namespace Ndk
Layout();
}
inline bool BaseWidget::IsRegisteredToCanvas() const
{
return m_canvas && m_canvasIndex != InvalidCanvasIndex;
}
inline void BaseWidget::NotifyParentResized(const Nz::Vector2f& newSize)
{
for (const auto& widgetPtr : m_children)

View File

@ -36,7 +36,7 @@ namespace Ndk
DestroyChildren();
// Prevent our parent from trying to call us
m_canvasIndex = std::numeric_limits<std::size_t>::max();
m_canvasIndex = InvalidCanvasIndex;
}
inline const WorldHandle& Canvas::GetWorld() const

View File

@ -76,6 +76,14 @@ namespace Ndk
}
}
void BaseWidget::SetCursor(Nz::SystemCursor systemCursor)
{
m_cursor = systemCursor;
if (IsRegisteredToCanvas())
m_canvas->NotifyWidgetCursorUpdate(m_canvasIndex);
}
void BaseWidget::SetSize(const Nz::Vector2f& size)
{
SetContentSize({std::max(size.x - m_padding.left - m_padding.right, 0.f), std::max(size.y - m_padding.top - m_padding.bottom, 0.f)});
@ -119,8 +127,8 @@ namespace Ndk
void BaseWidget::Layout()
{
if (m_canvas)
m_canvas->NotifyWidgetUpdate(m_canvasIndex);
if (IsRegisteredToCanvas())
m_canvas->NotifyWidgetBoxUpdate(m_canvasIndex);
if (m_backgroundEntity)
m_backgroundSprite->SetSize(m_contentSize.x + m_padding.left + m_padding.right, m_contentSize.y + m_padding.top + m_padding.bottom);
@ -130,8 +138,8 @@ namespace Ndk
{
Node::InvalidateNode();
if (m_canvas)
m_canvas->NotifyWidgetUpdate(m_canvasIndex);
if (IsRegisteredToCanvas())
m_canvas->NotifyWidgetBoxUpdate(m_canvasIndex);
}
void BaseWidget::OnKeyPressed(const Nz::WindowEvent::KeyEvent& key)
@ -189,17 +197,17 @@ namespace Ndk
void BaseWidget::RegisterToCanvas()
{
NazaraAssert(m_canvasIndex == std::numeric_limits<std::size_t>::max(), "Widget is already registered to canvas");
NazaraAssert(!IsRegisteredToCanvas(), "Widget is already registered to canvas");
m_canvasIndex = m_canvas->RegisterWidget(this);
}
void BaseWidget::UnregisterFromCanvas()
{
if (m_canvasIndex != std::numeric_limits<std::size_t>::max())
if (IsRegisteredToCanvas())
{
m_canvas->UnregisterWidget(m_canvasIndex);
m_canvasIndex = std::numeric_limits<std::size_t>::max();
m_canvasIndex = InvalidCanvasIndex;
}
}
}