Added ImageWidget (#139)

* Added ImageWidget

* Remove useless ResizeToContent function

* Add ResizeToContent function

* Forgot operators

* Added textures & colors functions

* oops
This commit is contained in:
S6066 2017-11-06 16:50:52 +01:00 committed by Jérôme Leclercq
parent 078641aa23
commit 2ed9691c80
4 changed files with 137 additions and 0 deletions

View File

@ -8,6 +8,7 @@
#include <NDK/Widgets/ButtonWidget.hpp>
#include <NDK/Widgets/CheckboxWidget.hpp>
#include <NDK/Widgets/Enums.hpp>
#include <NDK/Widgets/ImageWidget.hpp>
#include <NDK/Widgets/LabelWidget.hpp>
#include <NDK/Widgets/ProgressBarWidget.hpp>
#include <NDK/Widgets/TextAreaWidget.hpp>

View File

@ -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 <NDK/Prerequesites.hpp>
#include <NDK/BaseWidget.hpp>
#include <NDK/Entity.hpp>
#include <Nazara/Graphics/Sprite.hpp>
#include <Nazara/Renderer/Texture.hpp>
#include <Nazara/Math/Vector2.hpp>
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 <NDK/Widgets/ImageWidget.inl>
#endif // NDK_WIDGETS_IMAGEWIDGET_HPP

View File

@ -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 <NDK/Widgets/ImageWidget.hpp>
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);
}
}

View File

@ -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 <NDK/Widgets/ImageWidget.hpp>
#include <NDK/Components/NodeComponent.hpp>
#include <NDK/Components/GraphicsComponent.hpp>
namespace Ndk
{
ImageWidget::ImageWidget(BaseWidget* parent) :
BaseWidget(parent)
{
m_entity = CreateEntity();
m_entity->AddComponent<NodeComponent>();
auto& gfx = m_entity->AddComponent<GraphicsComponent>();
m_sprite = Nz::Sprite::New();
gfx.Attach(m_sprite);
}
void ImageWidget::ResizeToContent()
{
Nz::Vector3ui textureSize = m_sprite->GetMaterial()->GetDiffuseMap()->GetSize();
SetSize({ static_cast<float>(textureSize.x), static_cast<float>(textureSize.y) });
}
void ImageWidget::Layout()
{
BaseWidget::Layout();
Nz::Vector2f origin = GetContentOrigin();
Nz::Vector2f contentSize = GetContentSize();
m_entity->GetComponent<NodeComponent>().SetPosition(origin);
m_sprite->SetSize(contentSize);
}
}