Sdk/BaseWidget: Add visibility control

This commit is contained in:
Lynix 2017-01-15 12:13:29 +01:00
parent e1e290808a
commit c9458eeb17
3 changed files with 38 additions and 9 deletions

View File

@ -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;
};
}

View File

@ -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<BaseWidget>&& 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);

View File

@ -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<GraphicsComponent>().Attach(m_backgroundSprite, -1);
m_backgroundEntity->AddComponent<NodeComponent>().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)