From 81e819567bf5e37384639b7d3fe895f21a434a24 Mon Sep 17 00:00:00 2001 From: Lynix Date: Tue, 13 Sep 2016 18:22:40 +0200 Subject: [PATCH] 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 --- SDK/include/NDK/BaseWidget.hpp | 4 +- SDK/include/NDK/BaseWidget.inl | 10 ++++- SDK/include/NDK/Widgets/ButtonWidget.hpp | 49 ++++++++++++++++++++++++ SDK/include/NDK/Widgets/ButtonWidget.inl | 13 +++++++ SDK/src/NDK/BaseWidget.cpp | 6 +-- SDK/src/NDK/Widgets/ButtonWidget.cpp | 47 +++++++++++++++++++++++ 6 files changed, 122 insertions(+), 7 deletions(-) create mode 100644 SDK/include/NDK/Widgets/ButtonWidget.hpp create mode 100644 SDK/include/NDK/Widgets/ButtonWidget.inl create mode 100644 SDK/src/NDK/Widgets/ButtonWidget.cpp diff --git a/SDK/include/NDK/BaseWidget.hpp b/SDK/include/NDK/BaseWidget.hpp index 58c00349e..46690ba64 100644 --- a/SDK/include/NDK/BaseWidget.hpp +++ b/SDK/include/NDK/BaseWidget.hpp @@ -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 m_entities; std::vector m_children; EntityOwner m_backgroundEntity; diff --git a/SDK/include/NDK/BaseWidget.inl b/SDK/include/NDK/BaseWidget.inl index 560d2b16e..a3cfc9763 100644 --- a/SDK/include/NDK/BaseWidget.inl +++ b/SDK/include/NDK/BaseWidget.inl @@ -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(); } } diff --git a/SDK/include/NDK/Widgets/ButtonWidget.hpp b/SDK/include/NDK/Widgets/ButtonWidget.hpp new file mode 100644 index 000000000..b70e9ca3d --- /dev/null +++ b/SDK/include/NDK/Widgets/ButtonWidget.hpp @@ -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 +#include +#include +#include +#include + +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 + +#endif // NDK_WIDGETS_BUTTONWIDGET_HPP diff --git a/SDK/include/NDK/Widgets/ButtonWidget.inl b/SDK/include/NDK/Widgets/ButtonWidget.inl new file mode 100644 index 000000000..92d40719a --- /dev/null +++ b/SDK/include/NDK/Widgets/ButtonWidget.inl @@ -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 + +namespace Ndk +{ + inline void ButtonWidget::UpdateText(const Nz::AbstractTextDrawer& drawer) + { + m_textSprite->Update(drawer); + } +} diff --git a/SDK/src/NDK/BaseWidget.cpp b/SDK/src/NDK/BaseWidget.cpp index 774a36b25..368c18190 100644 --- a/SDK/src/NDK/BaseWidget.cpp +++ b/SDK/src/NDK/BaseWidget.cpp @@ -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().Attach(m_backgroundSprite, -1); m_backgroundEntity->AddComponent().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) { diff --git a/SDK/src/NDK/Widgets/ButtonWidget.cpp b/SDK/src/NDK/Widgets/ButtonWidget.cpp new file mode 100644 index 000000000..5afcb8ad8 --- /dev/null +++ b/SDK/src/NDK/Widgets/ButtonWidget.cpp @@ -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 +#include +#include +#include + +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().SetParent(this); + m_gradientEntity->AddComponent().Attach(m_gradientSprite); + m_gradientEntity->GetComponent().Attach(m_borderSprite, Nz::Matrix4f::Translate(Nz::Vector2f(-1.f, -1.f)), -1); + + m_textSprite = Nz::TextSprite::New(); + + m_textEntity = CreateEntity(); + m_textEntity->AddComponent().SetParent(this); + m_textEntity->AddComponent().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().GetBoundingVolume().aabb; + m_textEntity->GetComponent().SetPosition(contentSize.x / 2 - textBox.width / 2, contentSize.y / 2 - textBox.height / 2); + } +}