SDK/Widgets: Add buttons (not clickable atm)

Former-commit-id: 8c37250acd7bf5123674f1d83d4d55d7125f080e [formerly 6705a2a453bb8c1e441a5008beb9a4ac60847fa2] [formerly a391c49c98f003cfcb8409d67052ce7998329f9b [formerly a8440703ccade130b116dc51246cd3ec0e16e6ae]]
Former-commit-id: 7f0f4bd7353768e7e0440ef41ce24c8035d7ee83 [formerly 6d3310e1488bbea850e17257da5588f9f0d0ec03]
Former-commit-id: 1ef3f72963ba3df19e789aae2a2e263e104a2e94
This commit is contained in:
Lynix 2016-09-13 18:22:40 +02:00
parent 6e520f5ed6
commit 136fb65a6d
6 changed files with 122 additions and 7 deletions

View File

@ -33,6 +33,7 @@ namespace Ndk
//virtual BaseWidget* Clone() const = 0;
inline const Padding& GetPadding() const;
inline const Nz::Vector2f& GetContentSize() const;
inline Nz::Vector2f GetSize() const;
virtual void ResizeToContent() = 0;
@ -55,10 +56,9 @@ namespace Ndk
protected:
EntityHandle CreateEntity();
void DestroyEntity(Entity* entity);
virtual void Layout();
private:
void UpdateBackground();
std::vector<EntityOwner> m_entities;
std::vector<BaseWidget*> m_children;
EntityOwner m_backgroundEntity;

View File

@ -27,6 +27,11 @@ namespace Ndk
return m_padding;
}
inline const Nz::Vector2f& BaseWidget::GetContentSize() const
{
return m_contentSize;
}
inline Nz::Vector2f BaseWidget::GetSize() const
{
return Nz::Vector2f(m_contentSize.x + m_padding.left + m_padding.right, m_contentSize.y + m_padding.top + m_padding.bottom);
@ -35,7 +40,8 @@ namespace Ndk
inline void BaseWidget::SetContentSize(const Nz::Vector2f& size)
{
m_contentSize = size;
UpdateBackground();
Layout();
}
inline void BaseWidget::SetPadding(float left, float top, float right, float bottom)
@ -45,6 +51,6 @@ namespace Ndk
m_padding.bottom = bottom;
m_padding.right = right;
UpdateBackground();
Layout();
}
}

View File

@ -0,0 +1,49 @@
// 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
#pragma once
#ifndef NDK_WIDGETS_BUTTONWIDGET_HPP
#define NDK_WIDGETS_BUTTONWIDGET_HPP
#include <NDK/Prerequesites.hpp>
#include <NDK/BaseWidget.hpp>
#include <Nazara/Utility/AbstractTextDrawer.hpp>
#include <Nazara/Graphics/Sprite.hpp>
#include <Nazara/Graphics/TextSprite.hpp>
namespace Ndk
{
class World;
class NDK_API ButtonWidget : public BaseWidget
{
public:
ButtonWidget(const WorldHandle& world, BaseWidget* parent = nullptr);
ButtonWidget(const ButtonWidget&) = delete;
ButtonWidget(ButtonWidget&&) = default;
~ButtonWidget() = default;
//virtual ButtonWidget* Clone() const = 0;
void ResizeToContent();
inline void UpdateText(const Nz::AbstractTextDrawer& drawer);
ButtonWidget& operator=(const ButtonWidget&) = delete;
ButtonWidget& operator=(ButtonWidget&&) = default;
private:
void Layout() override;
EntityHandle m_textEntity;
EntityHandle m_gradientEntity;
Nz::SpriteRef m_gradientSprite;
Nz::TextSpriteRef m_textSprite;
};
}
#include <NDK/Widgets/ButtonWidget.inl>
#endif // NDK_WIDGETS_BUTTONWIDGET_HPP

View File

@ -0,0 +1,13 @@
// 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/ButtonWidget.hpp>
namespace Ndk
{
inline void ButtonWidget::UpdateText(const Nz::AbstractTextDrawer& drawer)
{
m_textSprite->Update(drawer);
}
}

View File

@ -15,7 +15,7 @@ namespace Ndk
delete child;
}
inline void Ndk::BaseWidget::EnableBackground(bool enable)
inline void BaseWidget::EnableBackground(bool enable)
{
if (m_backgroundEntity.IsValid() == enable)
return;
@ -30,7 +30,7 @@ namespace Ndk
m_backgroundEntity->AddComponent<GraphicsComponent>().Attach(m_backgroundSprite, -1);
m_backgroundEntity->AddComponent<NodeComponent>().SetParent(this);
UpdateBackground();
BaseWidget::Layout(); // Only layout background
}
else
{
@ -58,7 +58,7 @@ namespace Ndk
m_entities.erase(it);
}
void BaseWidget::UpdateBackground()
void BaseWidget::Layout()
{
if (m_backgroundEntity)
{

View File

@ -0,0 +1,47 @@
// 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/ButtonWidget.hpp>
#include <NDK/Components/GraphicsComponent.hpp>
#include <NDK/Components/NodeComponent.hpp>
#include <NDK/World.hpp>
namespace Ndk
{
ButtonWidget::ButtonWidget(const WorldHandle& world, BaseWidget* parent) :
BaseWidget(world, parent)
{
m_gradientSprite = Nz::Sprite::New();
m_gradientSprite->SetColor(Nz::Color(74, 74, 74));
m_gradientSprite->SetCornerColor(Nz::RectCorner_LeftBottom, Nz::Color(180, 180, 180));
m_gradientSprite->SetCornerColor(Nz::RectCorner_RightBottom, Nz::Color(180, 180, 180));
m_gradientSprite->SetMaterial(Nz::Material::New("Basic2D"));
m_gradientEntity = CreateEntity();
m_gradientEntity->AddComponent<NodeComponent>().SetParent(this);
m_gradientEntity->AddComponent<GraphicsComponent>().Attach(m_gradientSprite);
m_gradientEntity->GetComponent<GraphicsComponent>().Attach(m_borderSprite, Nz::Matrix4f::Translate(Nz::Vector2f(-1.f, -1.f)), -1);
m_textSprite = Nz::TextSprite::New();
m_textEntity = CreateEntity();
m_textEntity->AddComponent<NodeComponent>().SetParent(this);
m_textEntity->AddComponent<GraphicsComponent>().Attach(m_textSprite, 1);
}
void ButtonWidget::ResizeToContent()
{
SetContentSize(Nz::Vector2f(m_textSprite->GetBoundingVolume().obb.localBox.GetLengths()));
}
void ButtonWidget::Layout()
{
const Nz::Vector2f& contentSize = GetContentSize();
m_gradientSprite->SetSize(contentSize);
Nz::Boxf textBox = m_textEntity->GetComponent<GraphicsComponent>().GetBoundingVolume().aabb;
m_textEntity->GetComponent<NodeComponent>().SetPosition(contentSize.x / 2 - textBox.width / 2, contentSize.y / 2 - textBox.height / 2);
}
}