Commit current work
Former-commit-id: 79be9d4e937a73626cd5ec02c1d2e2fcbd440d18
This commit is contained in:
71
SDK/src/NDK/BaseWidget.cpp
Normal file
71
SDK/src/NDK/BaseWidget.cpp
Normal file
@@ -0,0 +1,71 @@
|
||||
// Copyright (C) 2015 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Development Kit"
|
||||
// For conditions of distribution and use, see copyright notice in Prerequesites.hpp
|
||||
|
||||
#include <NDK/BaseWidget.hpp>
|
||||
#include <NDK/Components/GraphicsComponent.hpp>
|
||||
#include <NDK/Components/NodeComponent.hpp>
|
||||
#include <NDK/World.hpp>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
BaseWidget::~BaseWidget()
|
||||
{
|
||||
for (BaseWidget* child : m_children)
|
||||
delete child;
|
||||
}
|
||||
|
||||
inline void Ndk::BaseWidget::EnableBackground(bool enable)
|
||||
{
|
||||
if (m_backgroundEntity.IsValid() == enable)
|
||||
return;
|
||||
|
||||
if (enable)
|
||||
{
|
||||
m_backgroundSprite = Nz::Sprite::New();
|
||||
m_backgroundSprite->SetColor(m_backgroundColor);
|
||||
m_backgroundSprite->SetMaterial(Nz::MaterialLibrary::Get((m_backgroundColor.IsOpaque()) ? "Basic2D" : "Translucent2D"));
|
||||
|
||||
m_backgroundEntity = m_world->CreateEntity();
|
||||
m_backgroundEntity->AddComponent<GraphicsComponent>().Attach(m_backgroundSprite, -1);
|
||||
m_backgroundEntity->AddComponent<NodeComponent>().SetParent(this);
|
||||
|
||||
UpdateBackground();
|
||||
}
|
||||
else
|
||||
{
|
||||
m_backgroundEntity->Kill();
|
||||
m_backgroundSprite.Reset();
|
||||
}
|
||||
}
|
||||
|
||||
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)});
|
||||
}
|
||||
|
||||
EntityHandle BaseWidget::CreateEntity()
|
||||
{
|
||||
m_entities.emplace_back(m_world->CreateEntity());
|
||||
return m_entities.back();
|
||||
}
|
||||
|
||||
void BaseWidget::DestroyEntity(Entity* entity)
|
||||
{
|
||||
auto it = std::find(m_entities.begin(), m_entities.end(), entity);
|
||||
NazaraAssert(it != m_entities.end(), "Entity does not belong to this widget");
|
||||
|
||||
m_entities.erase(it);
|
||||
}
|
||||
|
||||
void BaseWidget::UpdateBackground()
|
||||
{
|
||||
if (m_backgroundEntity)
|
||||
{
|
||||
NodeComponent& node = m_backgroundEntity->GetComponent<NodeComponent>();
|
||||
node.SetPosition(-m_padding.left, -m_padding.top);
|
||||
|
||||
m_backgroundSprite->SetSize(m_contentSize.x + m_padding.left + m_padding.right, m_contentSize.y + m_padding.top + m_padding.bottom);
|
||||
}
|
||||
}
|
||||
}
|
||||
26
SDK/src/NDK/Widgets/LabelWidget.cpp
Normal file
26
SDK/src/NDK/Widgets/LabelWidget.cpp
Normal file
@@ -0,0 +1,26 @@
|
||||
// Copyright (C) 2015 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Development Kit"
|
||||
// For conditions of distribution and use, see copyright notice in Prerequesites.hpp
|
||||
|
||||
#include <NDK/Widgets/LabelWidget.hpp>
|
||||
#include <NDK/Components/GraphicsComponent.hpp>
|
||||
#include <NDK/Components/NodeComponent.hpp>
|
||||
#include <NDK/World.hpp>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
LabelWidget::LabelWidget(const WorldHandle& world, BaseWidget* parent) :
|
||||
BaseWidget(world, parent)
|
||||
{
|
||||
m_textSprite = Nz::TextSprite::New();
|
||||
|
||||
m_textEntity = CreateEntity();
|
||||
m_textEntity->AddComponent<GraphicsComponent>().Attach(m_textSprite);
|
||||
m_textEntity->AddComponent<NodeComponent>().SetParent(this);
|
||||
}
|
||||
|
||||
void LabelWidget::ResizeToContent()
|
||||
{
|
||||
SetContentSize(Nz::Vector2f(m_textSprite->GetBoundingVolume().obb.localBox.GetLengths()));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user