diff --git a/include/Nazara/Widgets.hpp b/include/Nazara/Widgets.hpp index 2df183ce7..22ddb0396 100644 --- a/include/Nazara/Widgets.hpp +++ b/include/Nazara/Widgets.hpp @@ -33,6 +33,7 @@ #include #include #include +#include #include #include diff --git a/include/Nazara/Widgets/ImageWidget.hpp b/include/Nazara/Widgets/ImageWidget.hpp new file mode 100644 index 000000000..346262b8c --- /dev/null +++ b/include/Nazara/Widgets/ImageWidget.hpp @@ -0,0 +1,47 @@ +// Copyright (C) 2021 Samy Bensaid +// This file is part of the "Nazara Engine - Widgets module" +// For conditions of distribution and use, see copyright notice in Config.hpp + +#pragma once + +#ifndef NAZARA_WIDGETS_IMAGEWIDGET_HPP +#define NAZARA_WIDGETS_IMAGEWIDGET_HPP + +#include +#include +#include +#include + +namespace Nz +{ + class NAZARA_WIDGETS_API ImageWidget : public BaseWidget + { + public: + ImageWidget(BaseWidget* parent); + ImageWidget(const ImageWidget&) = delete; + ImageWidget(ImageWidget&&) = default; + ~ImageWidget() = default; + + inline const Color& GetColor() const; + inline const std::shared_ptr& GetMaterial() const; + inline const Rectf& GetTextureCoords() const; + + inline void SetColor(const Color& color); + inline void SetMaterial(const std::shared_ptr& texture); + inline void SetTextureCoords(const Rectf& coords); + inline void SetTextureRect(const Rectf& rect); + + ImageWidget& operator=(const ImageWidget&) = delete; + ImageWidget& operator=(ImageWidget&&) = default; + + private: + void Layout() override; + + entt::entity m_entity; + std::shared_ptr m_sprite; + }; +} + +#include + +#endif // NAZARA_WIDGETS_IMAGEWIDGET_HPP diff --git a/include/Nazara/Widgets/ImageWidget.inl b/include/Nazara/Widgets/ImageWidget.inl new file mode 100644 index 000000000..39ac77f2c --- /dev/null +++ b/include/Nazara/Widgets/ImageWidget.inl @@ -0,0 +1,54 @@ +// Copyright (C) 2021 Samy Bensaid +// This file is part of the "Nazara Engine - Widgets module" +// For conditions of distribution and use, see copyright notice in Config.hpp + +#include +#include + +namespace Nz +{ + inline const Color& ImageWidget::GetColor() const + { + return m_sprite->GetColor(); + } + + inline const std::shared_ptr& ImageWidget::GetMaterial() const + { + return m_sprite->GetMaterial(); + } + + inline const Rectf& ImageWidget::GetTextureCoords() const + { + return m_sprite->GetTextureCoords(); + } + + inline void ImageWidget::SetColor(const Color& color) + { + m_sprite->SetColor(color); + } + + inline void ImageWidget::SetMaterial(const std::shared_ptr& texture) + { + m_sprite->SetMaterial(texture); + + const Rectf& textureCoords = GetTextureCoords(); + + Vector2f textureSize = Vector2f(Vector2ui(m_sprite->GetTextureSize())); + textureSize.x *= textureCoords.width; + textureSize.y *= textureCoords.height; + + SetPreferredSize(textureSize); + } + + inline void ImageWidget::SetTextureCoords(const Rectf& coords) + { + m_sprite->SetTextureCoords(coords); + } + + inline void ImageWidget::SetTextureRect(const Rectf& rect) + { + m_sprite->SetTextureRect(rect); + } +} + +#include diff --git a/src/Nazara/Widgets/ImageWidget.cpp b/src/Nazara/Widgets/ImageWidget.cpp new file mode 100644 index 000000000..d48775edc --- /dev/null +++ b/src/Nazara/Widgets/ImageWidget.cpp @@ -0,0 +1,36 @@ +// Copyright (C) 2021 Samy Bensaid +// This file is part of the "Nazara Engine - Widgets module" +// For conditions of distribution and use, see copyright notice in Config.hpp + +#include +#include +#include +#include +#include +#include + +namespace Nz +{ + ImageWidget::ImageWidget(BaseWidget* parent) : + BaseWidget(parent) + { + m_sprite = std::make_shared(Widgets::Instance()->GetTransparentMaterial()); + + auto& registry = GetRegistry(); + + m_entity = CreateEntity(); + + auto& gfxComponent = registry.emplace(m_entity, IsVisible()); + gfxComponent.AttachRenderable(m_sprite, GetCanvas()->GetRenderMask()); + + auto& nodeComponent = registry.emplace(m_entity); + nodeComponent.SetParent(this); + } + + void ImageWidget::Layout() + { + BaseWidget::Layout(); + + m_sprite->SetSize(GetSize()); + } +}