diff --git a/SDK/include/NDK/Widgets.hpp b/SDK/include/NDK/Widgets.hpp index f2cce1559..26917a4c9 100644 --- a/SDK/include/NDK/Widgets.hpp +++ b/SDK/include/NDK/Widgets.hpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include diff --git a/SDK/include/NDK/Widgets/ImageWidget.hpp b/SDK/include/NDK/Widgets/ImageWidget.hpp new file mode 100644 index 000000000..7fe59c9f2 --- /dev/null +++ b/SDK/include/NDK/Widgets/ImageWidget.hpp @@ -0,0 +1,53 @@ +// Copyright (C) 2017 Samy Bensaid +// 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_IMAGEWIDGET_HPP +#define NDK_WIDGETS_IMAGEWIDGET_HPP + +#include +#include +#include +#include +#include +#include + +namespace Ndk +{ + class NDK_API ImageWidget : public BaseWidget + { + public: + ImageWidget(BaseWidget* parent = nullptr); + ImageWidget(const ImageWidget&) = delete; + ImageWidget(ImageWidget&&) = default; + ~ImageWidget() = default; + + //virtual ImageWidget* Clone() const = 0; + + void ResizeToContent() override; + + inline const Nz::Color& GetColor() const; + inline const Nz::TextureRef& GetTexture() const; + inline const Nz::Rectf& GetTextureCoords() const; + + inline void SetColor(const Nz::Color& color); + inline void SetTexture(const Nz::TextureRef& texture, bool resizeToContent = true); + inline void SetTextureCoords(const Nz::Rectf& coords); + inline void SetTextureRect(const Nz::Rectui& rect); + + ImageWidget& operator=(const ImageWidget&) = delete; + ImageWidget& operator=(ImageWidget&&) = default; + + private: + void Layout() override; + + Ndk::EntityHandle m_entity; + Nz::SpriteRef m_sprite; + }; +} + +#include + +#endif // NDK_WIDGETS_IMAGEWIDGET_HPP diff --git a/SDK/include/NDK/Widgets/ImageWidget.inl b/SDK/include/NDK/Widgets/ImageWidget.inl new file mode 100644 index 000000000..05c530f83 --- /dev/null +++ b/SDK/include/NDK/Widgets/ImageWidget.inl @@ -0,0 +1,46 @@ +// Copyright (C) 2017 Samy Bensaid +// 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 const Nz::Color& ImageWidget::GetColor() const + { + return m_sprite->GetColor(); + } + + inline const Nz::TextureRef& ImageWidget::GetTexture() const + { + return m_sprite->GetMaterial()->GetDiffuseMap(); + } + + inline const Nz::Rectf& ImageWidget::GetTextureCoords() const + { + return m_sprite->GetTextureCoords(); + } + + inline void ImageWidget::SetColor(const Nz::Color& color) + { + m_sprite->SetColor(color); + } + + inline void ImageWidget::SetTexture(const Nz::TextureRef& texture, bool resizeToContent) + { + m_sprite->SetTexture(texture, false); + + if (resizeToContent) + ResizeToContent(); + } + + inline void ImageWidget::SetTextureCoords(const Nz::Rectf& coords) + { + m_sprite->SetTextureCoords(coords); + } + + inline void ImageWidget::SetTextureRect(const Nz::Rectui& rect) + { + m_sprite->SetTextureRect(rect); + } +} diff --git a/SDK/src/NDK/Widgets/ImageWidget.cpp b/SDK/src/NDK/Widgets/ImageWidget.cpp new file mode 100644 index 000000000..a845979f7 --- /dev/null +++ b/SDK/src/NDK/Widgets/ImageWidget.cpp @@ -0,0 +1,37 @@ +// Copyright (C) 2017 Samy Bensaid +// 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 +{ + ImageWidget::ImageWidget(BaseWidget* parent) : + BaseWidget(parent) + { + m_entity = CreateEntity(); + m_entity->AddComponent(); + auto& gfx = m_entity->AddComponent(); + + m_sprite = Nz::Sprite::New(); + gfx.Attach(m_sprite); + } + + void ImageWidget::ResizeToContent() + { + Nz::Vector3ui textureSize = m_sprite->GetMaterial()->GetDiffuseMap()->GetSize(); + SetSize({ static_cast(textureSize.x), static_cast(textureSize.y) }); + } + + void ImageWidget::Layout() + { + BaseWidget::Layout(); + Nz::Vector2f origin = GetContentOrigin(); + Nz::Vector2f contentSize = GetContentSize(); + + m_entity->GetComponent().SetPosition(origin); + m_sprite->SetSize(contentSize); + } +}