SDK/Widgets: Add buttons (not clickable atm)

Former-commit-id: b90e4ddc0fd7dd18efd480243639d4e7c76c719b [formerly 2ff8f6dc8bb21873734f6d7a3b4bba6fb6edf24d] [formerly 0268cc083e0a8c6285cf05eead7b1a375ae5466d [formerly a4bdefb8421a31c7c62c293f128253de0fa5c6da]]
Former-commit-id: 8788b5116384b23544d620c28105516d8c2eb6a1 [formerly 785d6986573cd5483a37697b3f13651fa9ffa5b2]
Former-commit-id: 2d424bb16eb5f01381b3a6b315bce258092542c1
This commit is contained in:
Lynix 2016-09-13 18:22:40 +02:00
parent b8955b3a58
commit 81e819567b
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);
}
}