From 5588a573a6ee576deb650cf4bfe2574d1ff63ba3 Mon Sep 17 00:00:00 2001 From: Lynix Date: Fri, 13 May 2016 13:00:52 +0200 Subject: [PATCH] Commit current work Former-commit-id: 79be9d4e937a73626cd5ec02c1d2e2fcbd440d18 --- SDK/include/NDK/BaseWidget.hpp | 76 +++++++++++++++++++++++++ SDK/include/NDK/BaseWidget.inl | 50 ++++++++++++++++ SDK/include/NDK/Widgets/LabelWidget.hpp | 44 ++++++++++++++ SDK/include/NDK/Widgets/LabelWidget.inl | 13 +++++ SDK/src/NDK/BaseWidget.cpp | 71 +++++++++++++++++++++++ SDK/src/NDK/Widgets/LabelWidget.cpp | 26 +++++++++ 6 files changed, 280 insertions(+) create mode 100644 SDK/include/NDK/BaseWidget.hpp create mode 100644 SDK/include/NDK/BaseWidget.inl create mode 100644 SDK/include/NDK/Widgets/LabelWidget.hpp create mode 100644 SDK/include/NDK/Widgets/LabelWidget.inl create mode 100644 SDK/src/NDK/BaseWidget.cpp create mode 100644 SDK/src/NDK/Widgets/LabelWidget.cpp diff --git a/SDK/include/NDK/BaseWidget.hpp b/SDK/include/NDK/BaseWidget.hpp new file mode 100644 index 000000000..58c00349e --- /dev/null +++ b/SDK/include/NDK/BaseWidget.hpp @@ -0,0 +1,76 @@ +// 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_BASEWIDGET_HPP +#define NDK_BASEWIDGET_HPP + +#include +#include +#include +#include +#include +#include + +namespace Ndk +{ + class NDK_API BaseWidget : public Nz::Node + { + public: + struct Padding; + + inline BaseWidget(WorldHandle world, BaseWidget* parent = nullptr); + BaseWidget(const BaseWidget&) = delete; + BaseWidget(BaseWidget&&) = default; + virtual ~BaseWidget(); + + inline void AddChild(std::unique_ptr&& widget); + + void EnableBackground(bool enable); + + //virtual BaseWidget* Clone() const = 0; + + inline const Padding& GetPadding() const; + inline Nz::Vector2f GetSize() const; + + virtual void ResizeToContent() = 0; + + inline void SetContentSize(const Nz::Vector2f& size); + inline void SetPadding(float left, float top, float right, float bottom); + void SetSize(const Nz::Vector2f& size); + + BaseWidget& operator=(const BaseWidget&) = delete; + BaseWidget& operator=(BaseWidget&&) = default; + + struct Padding + { + float left; + float top; + float right; + float bottom; + }; + + protected: + EntityHandle CreateEntity(); + void DestroyEntity(Entity* entity); + + private: + void UpdateBackground(); + + std::vector m_entities; + std::vector m_children; + EntityOwner m_backgroundEntity; + Padding m_padding; + WorldHandle m_world; + Nz::Color m_backgroundColor; + Nz::SpriteRef m_backgroundSprite; + Nz::Vector2f m_contentSize; + BaseWidget* m_widgetParent; + }; +} + +#include + +#endif // NDK_BASEWIDGET_HPP diff --git a/SDK/include/NDK/BaseWidget.inl b/SDK/include/NDK/BaseWidget.inl new file mode 100644 index 000000000..560d2b16e --- /dev/null +++ b/SDK/include/NDK/BaseWidget.inl @@ -0,0 +1,50 @@ +// 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 + +namespace Ndk +{ + inline BaseWidget::BaseWidget(WorldHandle world, BaseWidget* parent) : + m_world(std::move(world)), + m_backgroundColor(Nz::Color(230, 230, 230, 255)), + m_contentSize(50.f, 50.f), + m_widgetParent(parent) + { + SetPadding(5.f, 5.f, 5.f, 5.f); + } + + inline void BaseWidget::AddChild(std::unique_ptr&& widget) + { + m_children.push_back(widget.release()); + } + + inline const BaseWidget::Padding& BaseWidget::GetPadding() const + { + return m_padding; + } + + 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); + } + + inline void BaseWidget::SetContentSize(const Nz::Vector2f& size) + { + m_contentSize = size; + UpdateBackground(); + } + + inline void BaseWidget::SetPadding(float left, float top, float right, float bottom) + { + m_padding.left = left; + m_padding.top = top; + m_padding.bottom = bottom; + m_padding.right = right; + + UpdateBackground(); + } +} diff --git a/SDK/include/NDK/Widgets/LabelWidget.hpp b/SDK/include/NDK/Widgets/LabelWidget.hpp new file mode 100644 index 000000000..6b9fe32fb --- /dev/null +++ b/SDK/include/NDK/Widgets/LabelWidget.hpp @@ -0,0 +1,44 @@ +// 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_LABELWIDGET_HPP +#define NDK_WIDGETS_LABELWIDGET_HPP + +#include +#include +#include +#include + +namespace Ndk +{ + class World; + + class NDK_API LabelWidget : public BaseWidget + { + public: + LabelWidget(const WorldHandle& world, BaseWidget* parent = nullptr); + LabelWidget(const LabelWidget&) = delete; + LabelWidget(LabelWidget&&) = default; + ~LabelWidget() = default; + + //virtual LabelWidget* Clone() const = 0; + + void ResizeToContent(); + + inline void UpdateText(const Nz::AbstractTextDrawer& drawer); + + LabelWidget& operator=(const LabelWidget&) = delete; + LabelWidget& operator=(LabelWidget&&) = default; + + private: + EntityHandle m_textEntity; + Nz::TextSpriteRef m_textSprite; + }; +} + +#include + +#endif // NDK_WIDGETS_LABELWIDGET_HPP diff --git a/SDK/include/NDK/Widgets/LabelWidget.inl b/SDK/include/NDK/Widgets/LabelWidget.inl new file mode 100644 index 000000000..4d4e100ce --- /dev/null +++ b/SDK/include/NDK/Widgets/LabelWidget.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 LabelWidget::UpdateText(const Nz::AbstractTextDrawer& drawer) + { + m_textSprite->Update(drawer); + } +} diff --git a/SDK/src/NDK/BaseWidget.cpp b/SDK/src/NDK/BaseWidget.cpp new file mode 100644 index 000000000..acb1270c7 --- /dev/null +++ b/SDK/src/NDK/BaseWidget.cpp @@ -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 +#include +#include +#include + +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().Attach(m_backgroundSprite, -1); + m_backgroundEntity->AddComponent().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(); + 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); + } + } +} \ No newline at end of file diff --git a/SDK/src/NDK/Widgets/LabelWidget.cpp b/SDK/src/NDK/Widgets/LabelWidget.cpp new file mode 100644 index 000000000..ac2a87bf5 --- /dev/null +++ b/SDK/src/NDK/Widgets/LabelWidget.cpp @@ -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 +#include +#include +#include + +namespace Ndk +{ + LabelWidget::LabelWidget(const WorldHandle& world, BaseWidget* parent) : + BaseWidget(world, parent) + { + m_textSprite = Nz::TextSprite::New(); + + m_textEntity = CreateEntity(); + m_textEntity->AddComponent().Attach(m_textSprite); + m_textEntity->AddComponent().SetParent(this); + } + + void LabelWidget::ResizeToContent() + { + SetContentSize(Nz::Vector2f(m_textSprite->GetBoundingVolume().obb.localBox.GetLengths())); + } +}