SDK/Widgets: Fix Show overriding entity enabled state

This commit is contained in:
Lynix 2019-07-05 22:23:48 +02:00
parent 3bed2fb7fe
commit 2d4dd7a8ab
3 changed files with 28 additions and 4 deletions

View File

@ -271,6 +271,7 @@ Nazara Development Kit:
- Added BaseWidget::OnMouseWheelMoved - Added BaseWidget::OnMouseWheelMoved
- Added Entity::OnEntity[Disabled|Enabled] signals - Added Entity::OnEntity[Disabled|Enabled] signals
- Added BaseWidget::SetParent - Added BaseWidget::SetParent
- BaseWidget::Show will no longer show entities disabled by the widget
# 0.4: # 0.4:

View File

@ -26,8 +26,6 @@ namespace Ndk
friend Canvas; friend Canvas;
public: public:
struct Padding;
BaseWidget(BaseWidget* parent); BaseWidget(BaseWidget* parent);
BaseWidget(const BaseWidget&) = delete; BaseWidget(const BaseWidget&) = delete;
BaseWidget(BaseWidget&&) = delete; BaseWidget(BaseWidget&&) = delete;
@ -138,6 +136,10 @@ namespace Ndk
struct WidgetEntity struct WidgetEntity
{ {
EntityOwner handle; EntityOwner handle;
bool isEnabled = true;
NazaraSlot(Ndk::Entity, OnEntityDisabled, onDisabledSlot);
NazaraSlot(Ndk::Entity, OnEntityEnabled, onEnabledSlot);
}; };
static constexpr std::size_t InvalidCanvasIndex = std::numeric_limits<std::size_t>::max(); static constexpr std::size_t InvalidCanvasIndex = std::numeric_limits<std::size_t>::max();

View File

@ -178,14 +178,20 @@ namespace Ndk
UnregisterFromCanvas(); UnregisterFromCanvas();
for (WidgetEntity& entity : m_entities) for (WidgetEntity& entity : m_entities)
entity.handle->Enable(show); {
if (entity.isEnabled)
{
entity.handle->Enable(show); //< This will override isEnabled
entity.isEnabled = true;
}
}
for (const auto& widgetPtr : m_children) for (const auto& widgetPtr : m_children)
widgetPtr->Show(show); widgetPtr->Show(show);
} }
} }
const Ndk::EntityHandle& BaseWidget::CreateEntity() const EntityHandle& BaseWidget::CreateEntity()
{ {
const EntityHandle& newEntity = m_world->CreateEntity(); const EntityHandle& newEntity = m_world->CreateEntity();
newEntity->Enable(m_visible); newEntity->Enable(m_visible);
@ -193,6 +199,21 @@ namespace Ndk
m_entities.emplace_back(); m_entities.emplace_back();
WidgetEntity& widgetEntity = m_entities.back(); WidgetEntity& widgetEntity = m_entities.back();
widgetEntity.handle = newEntity; widgetEntity.handle = newEntity;
widgetEntity.onDisabledSlot.Connect(newEntity->OnEntityDisabled, [this](Entity* entity)
{
auto it = std::find_if(m_entities.begin(), m_entities.end(), [&](const WidgetEntity& widgetEntity) { return widgetEntity.handle == entity; });
NazaraAssert(it != m_entities.end(), "Entity does not belong to this widget");
it->isEnabled = false;
});
widgetEntity.onEnabledSlot.Connect(newEntity->OnEntityEnabled, [this](Entity* entity)
{
auto it = std::find_if(m_entities.begin(), m_entities.end(), [&](const WidgetEntity& widgetEntity) { return widgetEntity.handle == entity; });
NazaraAssert(it != m_entities.end(), "Entity does not belong to this widget");
it->isEnabled = true;
});
return newEntity; return newEntity;
} }