Widgets: Add ImageWidget

This commit is contained in:
Jérôme Leclercq 2021-11-24 22:24:57 +01:00
parent 6c97f538a1
commit 643b1a2b15
4 changed files with 138 additions and 0 deletions

View File

@ -33,6 +33,7 @@
#include <Nazara/Widgets/ButtonWidget.hpp> #include <Nazara/Widgets/ButtonWidget.hpp>
#include <Nazara/Widgets/Canvas.hpp> #include <Nazara/Widgets/Canvas.hpp>
#include <Nazara/Widgets/Config.hpp> #include <Nazara/Widgets/Config.hpp>
#include <Nazara/Widgets/ImageWidget.hpp>
#include <Nazara/Widgets/LabelWidget.hpp> #include <Nazara/Widgets/LabelWidget.hpp>
#include <Nazara/Widgets/Widgets.hpp> #include <Nazara/Widgets/Widgets.hpp>

View File

@ -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 <Nazara/Graphics/Sprite.hpp>
#include <Nazara/Math/Vector2.hpp>
#include <Nazara/Renderer/Texture.hpp>
#include <Nazara/Widgets/BaseWidget.hpp>
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<Material>& GetMaterial() const;
inline const Rectf& GetTextureCoords() const;
inline void SetColor(const Color& color);
inline void SetMaterial(const std::shared_ptr<Material>& 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<Sprite> m_sprite;
};
}
#include <Nazara/Widgets/ImageWidget.inl>
#endif // NAZARA_WIDGETS_IMAGEWIDGET_HPP

View File

@ -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 <Nazara/Widgets/ImageWidget.hpp>
#include <Nazara/Widgets/Debug.hpp>
namespace Nz
{
inline const Color& ImageWidget::GetColor() const
{
return m_sprite->GetColor();
}
inline const std::shared_ptr<Material>& 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<Material>& 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 <Nazara/Widgets/DebugOff.hpp>

View File

@ -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 <Nazara/Widgets/ImageWidget.hpp>
#include <Nazara/Graphics/Components/GraphicsComponent.hpp>
#include <Nazara/Utility/Components/NodeComponent.hpp>
#include <Nazara/Widgets/Canvas.hpp>
#include <Nazara/Widgets/Widgets.hpp>
#include <Nazara/Widgets/Debug.hpp>
namespace Nz
{
ImageWidget::ImageWidget(BaseWidget* parent) :
BaseWidget(parent)
{
m_sprite = std::make_shared<Sprite>(Widgets::Instance()->GetTransparentMaterial());
auto& registry = GetRegistry();
m_entity = CreateEntity();
auto& gfxComponent = registry.emplace<GraphicsComponent>(m_entity, IsVisible());
gfxComponent.AttachRenderable(m_sprite, GetCanvas()->GetRenderMask());
auto& nodeComponent = registry.emplace<NodeComponent>(m_entity);
nodeComponent.SetParent(this);
}
void ImageWidget::Layout()
{
BaseWidget::Layout();
m_sprite->SetSize(GetSize());
}
}