diff --git a/SDK/include/NDK/BaseWidget.hpp b/SDK/include/NDK/BaseWidget.hpp index 22bfa9dcf..2bdf0dd81 100644 --- a/SDK/include/NDK/BaseWidget.hpp +++ b/SDK/include/NDK/BaseWidget.hpp @@ -49,6 +49,8 @@ namespace Ndk inline const Nz::Vector2f& GetContentSize() const; inline Nz::Vector2f GetSize() const; + inline bool IsVisible() const; + void GrabKeyboard(); virtual void ResizeToContent() = 0; @@ -58,6 +60,8 @@ namespace Ndk inline void SetPadding(float left, float top, float right, float bottom); void SetSize(const Nz::Vector2f& size); + void Show(bool show = true); + BaseWidget& operator=(const BaseWidget&) = delete; BaseWidget& operator=(BaseWidget&&) = default; @@ -104,6 +108,7 @@ namespace Ndk Nz::SpriteRef m_backgroundSprite; Nz::Vector2f m_contentSize; BaseWidget* m_widgetParent; + bool m_visible; }; } diff --git a/SDK/include/NDK/BaseWidget.inl b/SDK/include/NDK/BaseWidget.inl index d5a91a2ed..041abe9bd 100644 --- a/SDK/include/NDK/BaseWidget.inl +++ b/SDK/include/NDK/BaseWidget.inl @@ -14,7 +14,8 @@ namespace Ndk m_backgroundColor(Nz::Color(230, 230, 230, 255)), m_canvas(nullptr), m_contentSize(50.f, 50.f), - m_widgetParent(nullptr) + m_widgetParent(nullptr), + m_visible(true) { SetPadding(5.f, 5.f, 5.f, 5.f); } @@ -31,6 +32,7 @@ namespace Ndk inline void BaseWidget::AddChild(std::unique_ptr&& widget) { + widget->Show(m_visible); m_children.emplace_back(std::move(widget)); } @@ -68,6 +70,11 @@ namespace Ndk return Nz::Vector2f(m_contentSize.x + m_padding.left + m_padding.right, m_contentSize.y + m_padding.top + m_padding.bottom); } + inline bool BaseWidget::IsVisible() const + { + return m_visible; + } + inline void BaseWidget::SetContentSize(const Nz::Vector2f& size) { NotifyParentResized(size); diff --git a/SDK/src/NDK/BaseWidget.cpp b/SDK/src/NDK/BaseWidget.cpp index a65a8a889..69d60965e 100644 --- a/SDK/src/NDK/BaseWidget.cpp +++ b/SDK/src/NDK/BaseWidget.cpp @@ -48,7 +48,7 @@ namespace Ndk m_backgroundSprite->SetColor(m_backgroundColor); m_backgroundSprite->SetMaterial(Nz::Material::New((m_backgroundColor.IsOpaque()) ? "Basic2D" : "Translucent2D")); - m_backgroundEntity = m_world->CreateEntity(); + m_backgroundEntity = CreateEntity(); m_backgroundEntity->AddComponent().Attach(m_backgroundSprite, -1); m_backgroundEntity->AddComponent().SetParent(this); @@ -61,6 +61,11 @@ namespace Ndk } } + void BaseWidget::GrabKeyboard() + { + m_canvas->SetKeyboardOwner(this); + } + void BaseWidget::SetBackgroundColor(const Nz::Color& color) { m_backgroundColor = color; @@ -77,10 +82,27 @@ namespace Ndk 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)}); } + void BaseWidget::Show(bool show) + { + if (m_visible != show) + { + m_visible = show; + + for (const EntityHandle& entity : m_entities) + entity->Enable(show); + + for (const auto& widgetPtr : m_children) + widgetPtr->Show(show); + } + } + EntityHandle BaseWidget::CreateEntity() { - m_entities.emplace_back(m_world->CreateEntity()); - return m_entities.back(); + EntityHandle newEntity = m_world->CreateEntity(); + newEntity->Enable(m_visible); + + m_entities.emplace_back(newEntity); + return newEntity; } void BaseWidget::DestroyEntity(Entity* entity) @@ -91,11 +113,6 @@ namespace Ndk m_entities.erase(it); } - void BaseWidget::GrabKeyboard() - { - m_canvas->SetKeyboardOwner(this); - } - void BaseWidget::Layout() { if (m_canvas)