Add widgets (WIP)
This commit is contained in:
@@ -65,7 +65,7 @@ namespace Nz
|
||||
builder.BeginRenderPass(*passData.framebuffer, *passData.renderPass, passData.renderRect);
|
||||
|
||||
if (!passData.name.empty())
|
||||
builder.BeginDebugRegion(passData.name, Nz::Color::Green);
|
||||
builder.BeginDebugRegion(passData.name, Color::Green);
|
||||
|
||||
bool first = true;
|
||||
for (auto& subpass : passData.subpasses)
|
||||
|
||||
@@ -12,12 +12,14 @@ namespace Nz
|
||||
{
|
||||
Sprite::Sprite(std::shared_ptr<Material> material) :
|
||||
InstancedRenderable(Nz::Boxf(-1000.f, -1000.f, -1000.f, 2000.f, 2000.f, 2000.f)),
|
||||
m_material(std::move(material))
|
||||
m_material(std::move(material)),
|
||||
m_color(Color::White),
|
||||
m_textureCoords(0.f, 0.f, 1.f, 1.f),
|
||||
m_size(64.f, 64.f)
|
||||
{
|
||||
m_vertices[0] = VertexStruct_XYZ_Color_UV{ Vector3f(0.f, 0.f, 0.f), Nz::Color::White, Vector2f(0.f, 0.f) };
|
||||
m_vertices[1] = VertexStruct_XYZ_Color_UV{ Vector3f(1.f, 0.f, 0.f), Nz::Color::White, Vector2f(1.f, 0.f) };
|
||||
m_vertices[2] = VertexStruct_XYZ_Color_UV{ Vector3f(0.f, 1.f, 0.f), Nz::Color::White, Vector2f(0.f, 1.f) };
|
||||
m_vertices[3] = VertexStruct_XYZ_Color_UV{ Vector3f(1.f, 1.f, 0.f), Nz::Color::White, Vector2f(1.f, 1.f) };
|
||||
m_cornerColor.fill(Color::White);
|
||||
|
||||
UpdateVertices();
|
||||
}
|
||||
|
||||
void Sprite::BuildElement(std::size_t passIndex, const WorldInstance& worldInstance, std::vector<std::unique_ptr<RenderElement>>& elements) const
|
||||
@@ -43,6 +45,16 @@ namespace Nz
|
||||
elements.emplace_back(std::make_unique<RenderSpriteChain>(0, renderPipeline, vertexDeclaration, whiteTexture, 1, m_vertices.data(), materialPass->GetShaderBinding(), worldInstance, materialPass->GetFlags()));
|
||||
}
|
||||
|
||||
inline const Color& Sprite::GetColor() const
|
||||
{
|
||||
return m_color;
|
||||
}
|
||||
|
||||
inline const Color& Sprite::GetCornerColor(RectCorner corner) const
|
||||
{
|
||||
return m_cornerColor[UnderlyingCast(corner)];
|
||||
}
|
||||
|
||||
const std::shared_ptr<Material>& Sprite::GetMaterial(std::size_t i) const
|
||||
{
|
||||
assert(i == 0);
|
||||
|
||||
401
src/Nazara/Widgets/BaseWidget.cpp
Normal file
401
src/Nazara/Widgets/BaseWidget.cpp
Normal file
@@ -0,0 +1,401 @@
|
||||
// Copyright (C) 2017 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Development Kit"
|
||||
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
|
||||
|
||||
#include <Nazara/Widgets/BaseWidget.hpp>
|
||||
#include <Nazara/Widgets/Canvas.hpp>
|
||||
#include <Nazara/Graphics/BasicMaterial.hpp>
|
||||
#include <Nazara/Graphics/Material.hpp>
|
||||
#include <Nazara/Graphics/Components/GraphicsComponent.hpp>
|
||||
#include <Nazara/Utility/Components/NodeComponent.hpp>
|
||||
#include <algorithm>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
/*!
|
||||
* \ingroup NDK
|
||||
* \class Ndk::BaseWidget
|
||||
* \brief Abstract class serving as a base class for all widgets
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \brief Constructs a BaseWidget object using another widget as its parent
|
||||
*
|
||||
* \param parent Parent widget, must be valid and attached to a canvas
|
||||
*
|
||||
* Constructs a BaseWidget object using another widget as a base.
|
||||
* This will also register the widget to the canvas owning the top-most widget.
|
||||
*/
|
||||
BaseWidget::BaseWidget(BaseWidget* parent) :
|
||||
BaseWidget()
|
||||
{
|
||||
NazaraAssert(parent, "Invalid parent");
|
||||
NazaraAssert(parent->GetCanvas(), "Parent has no canvas");
|
||||
|
||||
m_canvas = parent->GetCanvas();
|
||||
m_widgetParent = parent;
|
||||
m_registry = &m_canvas->GetRegistry();
|
||||
|
||||
RegisterToCanvas();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Frees the widget, unregistering it from its canvas
|
||||
*/
|
||||
BaseWidget::~BaseWidget()
|
||||
{
|
||||
UnregisterFromCanvas();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Clears keyboard focus if and only if this widget owns it.
|
||||
*/
|
||||
void BaseWidget::ClearFocus()
|
||||
{
|
||||
if (IsRegisteredToCanvas())
|
||||
m_canvas->ClearKeyboardOwner(m_canvasIndex);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Destroy the widget, deleting it in the process.
|
||||
*
|
||||
* Calling this function immediately destroys the widget, freeing its memory.
|
||||
*/
|
||||
void BaseWidget::Destroy()
|
||||
{
|
||||
NazaraAssert(this != m_canvas, "Canvas cannot be destroyed by calling Destroy()");
|
||||
|
||||
m_widgetParent->DestroyChild(this); //< This does delete us
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Enable or disables the widget background.
|
||||
*/
|
||||
void BaseWidget::EnableBackground(bool enable)
|
||||
{
|
||||
if (m_backgroundEntity.has_value() == enable)
|
||||
return;
|
||||
|
||||
if (enable)
|
||||
{
|
||||
auto material = std::make_shared<Material>();
|
||||
material->AddPass("ForwardPass", std::make_shared<MaterialPass>(BasicMaterial::GetSettings()));
|
||||
|
||||
m_backgroundSprite = std::make_shared<Nz::Sprite>(std::move(material));
|
||||
m_backgroundSprite->SetColor(m_backgroundColor);
|
||||
//m_backgroundSprite->SetMaterial(Nz::Material::New((m_backgroundColor.IsOpaque()) ? "Basic2D" : "Translucent2D")); //< TODO: Use a shared material instead of creating one everytime
|
||||
|
||||
entt::entity backgroundEntity = CreateEntity();
|
||||
m_registry->emplace<GraphicsComponent>(backgroundEntity).AttachRenderable(m_backgroundSprite);
|
||||
m_registry->emplace<NodeComponent>(backgroundEntity).SetParent(this);
|
||||
|
||||
m_backgroundEntity = backgroundEntity;
|
||||
|
||||
BaseWidget::Layout(); // Only layout background
|
||||
}
|
||||
else
|
||||
{
|
||||
assert(m_backgroundEntity);
|
||||
|
||||
DestroyEntity(*m_backgroundEntity);
|
||||
m_backgroundSprite.reset();
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Checks if this widget has keyboard focus
|
||||
* \return true if widget has keyboard focus, false otherwise
|
||||
*/
|
||||
bool BaseWidget::HasFocus() const
|
||||
{
|
||||
if (!IsRegisteredToCanvas())
|
||||
return false;
|
||||
|
||||
return m_canvas->IsKeyboardOwner(m_canvasIndex);
|
||||
}
|
||||
|
||||
void BaseWidget::Resize(const Nz::Vector2f& size)
|
||||
{
|
||||
// Adjust new size
|
||||
Nz::Vector2f newSize = size;
|
||||
newSize.Maximize(m_minimumSize);
|
||||
newSize.Minimize(m_maximumSize);
|
||||
|
||||
NotifyParentResized(newSize);
|
||||
m_size = newSize;
|
||||
|
||||
Layout();
|
||||
}
|
||||
|
||||
void BaseWidget::SetBackgroundColor(const Color& color)
|
||||
{
|
||||
m_backgroundColor = color;
|
||||
|
||||
if (m_backgroundSprite)
|
||||
{
|
||||
m_backgroundSprite->SetColor(color);
|
||||
//m_backgroundSprite->GetMaterial()->Configure((color.IsOpaque()) ? "Basic2D" : "Translucent2D"); //< Our sprite has its own material (see EnableBackground)
|
||||
}
|
||||
}
|
||||
|
||||
void BaseWidget::SetCursor(Nz::SystemCursor systemCursor)
|
||||
{
|
||||
m_cursor = systemCursor;
|
||||
|
||||
if (IsRegisteredToCanvas())
|
||||
m_canvas->NotifyWidgetCursorUpdate(m_canvasIndex);
|
||||
}
|
||||
|
||||
void BaseWidget::SetFocus()
|
||||
{
|
||||
if (IsRegisteredToCanvas())
|
||||
m_canvas->SetKeyboardOwner(m_canvasIndex);
|
||||
}
|
||||
|
||||
void BaseWidget::SetParent(BaseWidget* widget)
|
||||
{
|
||||
Canvas* oldCanvas = m_canvas;
|
||||
Canvas* newCanvas = widget->GetCanvas();
|
||||
|
||||
// Changing a widget canvas is a problem because of the canvas entities
|
||||
NazaraAssert(oldCanvas == newCanvas, "Transferring a widget between canvas is not yet supported");
|
||||
|
||||
Node::SetParent(widget);
|
||||
m_widgetParent = widget;
|
||||
|
||||
Layout();
|
||||
}
|
||||
|
||||
void BaseWidget::SetRenderingRect(const Nz::Rectf& renderingRect)
|
||||
{
|
||||
m_renderingRect = renderingRect;
|
||||
|
||||
UpdatePositionAndSize();
|
||||
for (const auto& widgetPtr : m_children)
|
||||
widgetPtr->UpdatePositionAndSize();
|
||||
}
|
||||
|
||||
void BaseWidget::Show(bool show)
|
||||
{
|
||||
if (m_visible != show)
|
||||
{
|
||||
m_visible = show;
|
||||
|
||||
if (m_visible)
|
||||
RegisterToCanvas();
|
||||
else
|
||||
UnregisterFromCanvas();
|
||||
|
||||
for (WidgetEntity& entity : m_entities)
|
||||
{
|
||||
if (entity.isEnabled)
|
||||
{
|
||||
//entity.handle->Enable(show); //< This will override isEnabled, so reset it next line
|
||||
entity.isEnabled = true;
|
||||
}
|
||||
}
|
||||
|
||||
ShowChildren(show);
|
||||
}
|
||||
}
|
||||
|
||||
entt::entity BaseWidget::CreateEntity()
|
||||
{
|
||||
entt::entity newEntity = m_registry->create();
|
||||
//newEntity->Enable(m_visible);
|
||||
|
||||
m_entities.emplace_back();
|
||||
WidgetEntity& newWidgetEntity = m_entities.back();
|
||||
newWidgetEntity.handle = newEntity;
|
||||
/*newWidgetEntity.onDisabledSlot.Connect(newEntity->OnEntityDisabled, [this](Entity* entity)
|
||||
{
|
||||
auto it = std::find_if(m_entities.begin(), m_entities.end(), [&](const WidgetEntity& widgetEntity) { return widgetEntity.handle == entity; });
|
||||
NazaraAssert(it != m_entities.end(), "Entity does not belong to this widget");
|
||||
|
||||
it->isEnabled = false;
|
||||
});
|
||||
|
||||
newWidgetEntity.onEnabledSlot.Connect(newEntity->OnEntityEnabled, [this](Entity* entity)
|
||||
{
|
||||
auto it = std::find_if(m_entities.begin(), m_entities.end(), [&](const WidgetEntity& widgetEntity) { return widgetEntity.handle == entity; });
|
||||
NazaraAssert(it != m_entities.end(), "Entity does not belong to this widget");
|
||||
|
||||
if (!IsVisible())
|
||||
entity->Disable(); // Next line will override isEnabled status
|
||||
|
||||
it->isEnabled = true;
|
||||
});*/
|
||||
|
||||
return newEntity;
|
||||
}
|
||||
|
||||
void BaseWidget::DestroyEntity(entt::entity entity)
|
||||
{
|
||||
auto it = std::find_if(m_entities.begin(), m_entities.end(), [&](const WidgetEntity& widgetEntity) { return widgetEntity.handle == entity; });
|
||||
NazaraAssert(it != m_entities.end(), "Entity does not belong to this widget");
|
||||
|
||||
m_entities.erase(it);
|
||||
m_registry->destroy(entity);
|
||||
}
|
||||
|
||||
void BaseWidget::Layout()
|
||||
{
|
||||
if (m_backgroundSprite)
|
||||
m_backgroundSprite->SetSize({ m_size.x, m_size.y });
|
||||
|
||||
UpdatePositionAndSize();
|
||||
}
|
||||
|
||||
void BaseWidget::InvalidateNode()
|
||||
{
|
||||
Node::InvalidateNode();
|
||||
|
||||
UpdatePositionAndSize();
|
||||
}
|
||||
|
||||
Nz::Rectf BaseWidget::GetScissorRect() const
|
||||
{
|
||||
Nz::Vector2f widgetPos = Nz::Vector2f(GetPosition(Nz::CoordSys::Global));
|
||||
Nz::Vector2f widgetSize = GetSize();
|
||||
|
||||
Nz::Rectf widgetRect(widgetPos.x, widgetPos.y, widgetSize.x, widgetSize.y);
|
||||
Nz::Rectf widgetRenderingRect(widgetPos.x + m_renderingRect.x, widgetPos.y + m_renderingRect.y, m_renderingRect.width, m_renderingRect.height);
|
||||
|
||||
widgetRect.Intersect(widgetRenderingRect, &widgetRect);
|
||||
|
||||
return widgetRect;
|
||||
}
|
||||
|
||||
bool BaseWidget::IsFocusable() const
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
void BaseWidget::OnFocusLost()
|
||||
{
|
||||
}
|
||||
|
||||
void BaseWidget::OnFocusReceived()
|
||||
{
|
||||
}
|
||||
|
||||
bool BaseWidget::OnKeyPressed(const Nz::WindowEvent::KeyEvent& key)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
void BaseWidget::OnKeyReleased(const Nz::WindowEvent::KeyEvent& /*key*/)
|
||||
{
|
||||
}
|
||||
|
||||
void BaseWidget::OnMouseEnter()
|
||||
{
|
||||
}
|
||||
|
||||
void BaseWidget::OnMouseMoved(int /*x*/, int /*y*/, int /*deltaX*/, int /*deltaY*/)
|
||||
{
|
||||
}
|
||||
|
||||
void BaseWidget::OnMouseButtonPress(int /*x*/, int /*y*/, Nz::Mouse::Button /*button*/)
|
||||
{
|
||||
}
|
||||
|
||||
void BaseWidget::OnMouseButtonRelease(int /*x*/, int /*y*/, Nz::Mouse::Button /*button*/)
|
||||
{
|
||||
}
|
||||
|
||||
void BaseWidget::OnMouseWheelMoved(int /*x*/, int /*y*/, float /*delta*/)
|
||||
{
|
||||
}
|
||||
|
||||
void BaseWidget::OnMouseExit()
|
||||
{
|
||||
}
|
||||
|
||||
void BaseWidget::OnParentResized(const Nz::Vector2f& /*newSize*/)
|
||||
{
|
||||
}
|
||||
|
||||
void BaseWidget::OnTextEntered(char32_t /*character*/, bool /*repeated*/)
|
||||
{
|
||||
}
|
||||
|
||||
void BaseWidget::OnTextEdited(const std::array<char, 32>& /*characters*/, int /*length*/)
|
||||
{
|
||||
}
|
||||
|
||||
inline entt::registry& BaseWidget::GetRegistry()
|
||||
{
|
||||
assert(m_registry);
|
||||
return *m_registry;
|
||||
}
|
||||
|
||||
inline const entt::registry& BaseWidget::GetRegistry() const
|
||||
{
|
||||
assert(m_registry);
|
||||
return *m_registry;
|
||||
}
|
||||
|
||||
void BaseWidget::ShowChildren(bool show)
|
||||
{
|
||||
for (const auto& widgetPtr : m_children)
|
||||
widgetPtr->Show(show);
|
||||
}
|
||||
|
||||
void BaseWidget::DestroyChild(BaseWidget* widget)
|
||||
{
|
||||
auto it = std::find_if(m_children.begin(), m_children.end(), [widget] (const std::unique_ptr<BaseWidget>& widgetPtr) -> bool
|
||||
{
|
||||
return widgetPtr.get() == widget;
|
||||
});
|
||||
|
||||
NazaraAssert(it != m_children.end(), "Child widget not found in parent");
|
||||
|
||||
m_children.erase(it);
|
||||
}
|
||||
|
||||
void BaseWidget::DestroyChildren()
|
||||
{
|
||||
m_children.clear();
|
||||
}
|
||||
|
||||
void BaseWidget::RegisterToCanvas()
|
||||
{
|
||||
NazaraAssert(!IsRegisteredToCanvas(), "Widget is already registered to canvas");
|
||||
|
||||
m_canvasIndex = m_canvas->RegisterWidget(this);
|
||||
}
|
||||
|
||||
void BaseWidget::UnregisterFromCanvas()
|
||||
{
|
||||
if (IsRegisteredToCanvas())
|
||||
{
|
||||
m_canvas->UnregisterWidget(m_canvasIndex);
|
||||
m_canvasIndex = InvalidCanvasIndex;
|
||||
}
|
||||
}
|
||||
|
||||
void BaseWidget::UpdatePositionAndSize()
|
||||
{
|
||||
if (IsRegisteredToCanvas())
|
||||
m_canvas->NotifyWidgetBoxUpdate(m_canvasIndex);
|
||||
|
||||
Nz::Rectf scissorRect = GetScissorRect();
|
||||
|
||||
if (m_widgetParent)
|
||||
{
|
||||
Nz::Rectf parentScissorRect = m_widgetParent->GetScissorRect();
|
||||
|
||||
if (!scissorRect.Intersect(parentScissorRect, &scissorRect))
|
||||
scissorRect = parentScissorRect;
|
||||
}
|
||||
|
||||
/*Nz::Recti fullBounds(scissorRect);
|
||||
for (WidgetEntity& widgetEntity : m_entities)
|
||||
{
|
||||
const Ndk::EntityHandle& entity = widgetEntity.handle;
|
||||
if (entity->HasComponent<GraphicsComponent>())
|
||||
entity->GetComponent<GraphicsComponent>().SetScissorRect(fullBounds);
|
||||
}*/
|
||||
}
|
||||
}
|
||||
97
src/Nazara/Widgets/ButtonWidget.cpp
Normal file
97
src/Nazara/Widgets/ButtonWidget.cpp
Normal file
@@ -0,0 +1,97 @@
|
||||
// Copyright (C) 2017 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Development Kit"
|
||||
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
|
||||
|
||||
#if 0
|
||||
|
||||
#include <Nazara/Widgets/ButtonWidget.hpp>
|
||||
#include <Nazara/Graphics/Components/GraphicsComponent.hpp>
|
||||
#include <Nazara/Utility/Components/NodeComponent.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
ButtonWidget::ButtonWidget(BaseWidget* parent) :
|
||||
BaseWidget(parent),
|
||||
m_color { 74, 74, 74 },
|
||||
m_cornerColor { 180, 180, 180 },
|
||||
m_hoverColor { 128, 128, 128 },
|
||||
m_hoverCornerColor { 180, 180, 180 },
|
||||
m_pressColor { 180, 180, 180 },
|
||||
m_pressCornerColor { 74, 74, 74 }
|
||||
{
|
||||
entt::registry& registry = GetRegistry();
|
||||
|
||||
m_gradientSprite = std::make_shared<Sprite>();
|
||||
m_gradientSprite->SetColor(m_color);
|
||||
m_gradientSprite->SetCornerColor(Nz::RectCorner::LeftBottom, m_cornerColor);
|
||||
m_gradientSprite->SetCornerColor(Nz::RectCorner::RightBottom, m_cornerColor);
|
||||
m_gradientSprite->SetMaterial(Nz::Material::New("Basic2D"));
|
||||
|
||||
m_gradientEntity = CreateEntity();
|
||||
registry.emplace<NodeComponent>(m_gradientEntity).SetParent(this);
|
||||
registry.emplace<GraphicsComponent>(m_gradientEntity).AttachRenderable(m_gradientSprite);
|
||||
|
||||
m_textSprite = std::make_shared<TextSprite>();
|
||||
|
||||
m_textEntity = CreateEntity();
|
||||
registry.emplace<NodeComponent>(m_textEntity).SetParent(this);
|
||||
registry.emplace<GraphicsComponent>(m_textEntity).AttachRenderable(m_textSprite);
|
||||
|
||||
Layout();
|
||||
}
|
||||
|
||||
void ButtonWidget::Layout()
|
||||
{
|
||||
BaseWidget::Layout();
|
||||
|
||||
Nz::Vector2f size = GetSize();
|
||||
m_gradientSprite->SetSize(size);
|
||||
|
||||
entt::registry& registry = GetRegistry();
|
||||
|
||||
Nz::Boxf textBox = m_textSprite->GetAABB();
|
||||
registry.get<NodeComponent>(m_textEntity).SetPosition(size.x / 2.f - textBox.width / 2.f, size.y / 2.f - textBox.height / 2.f);
|
||||
}
|
||||
|
||||
void ButtonWidget::OnMouseButtonPress(int /*x*/, int /*y*/, Nz::Mouse::Button button)
|
||||
{
|
||||
if (button == Nz::Mouse::Left)
|
||||
{
|
||||
m_gradientSprite->SetColor(m_pressColor);
|
||||
m_gradientSprite->SetCornerColor(Nz::RectCorner::LeftBottom, m_pressCornerColor);
|
||||
m_gradientSprite->SetCornerColor(Nz::RectCorner::RightBottom, m_pressCornerColor);
|
||||
m_gradientSprite->SetTexture(m_pressTexture, false);
|
||||
}
|
||||
}
|
||||
|
||||
void ButtonWidget::OnMouseButtonRelease(int /*x*/, int /*y*/, Nz::Mouse::Button button)
|
||||
{
|
||||
if (button == Nz::Mouse::Left)
|
||||
{
|
||||
m_gradientSprite->SetColor(m_hoverColor);
|
||||
m_gradientSprite->SetCornerColor(Nz::RectCorner::LeftBottom, m_hoverCornerColor);
|
||||
m_gradientSprite->SetCornerColor(Nz::RectCorner::RightBottom, m_hoverCornerColor);
|
||||
m_gradientSprite->SetTexture(m_hoverTexture, false);
|
||||
|
||||
OnButtonTrigger(this);
|
||||
}
|
||||
}
|
||||
|
||||
void ButtonWidget::OnMouseEnter()
|
||||
{
|
||||
m_gradientSprite->SetColor(m_hoverColor);
|
||||
m_gradientSprite->SetCornerColor(Nz::RectCorner::LeftBottom, m_hoverCornerColor);
|
||||
m_gradientSprite->SetCornerColor(Nz::RectCorner::RightBottom, m_hoverCornerColor);
|
||||
m_gradientSprite->SetTexture(m_hoverTexture, false);
|
||||
}
|
||||
|
||||
void ButtonWidget::OnMouseExit()
|
||||
{
|
||||
m_gradientSprite->SetColor(m_color);
|
||||
m_gradientSprite->SetCornerColor(Nz::RectCorner::LeftBottom, m_cornerColor);
|
||||
m_gradientSprite->SetCornerColor(Nz::RectCorner::RightBottom, m_cornerColor);
|
||||
m_gradientSprite->SetTexture(m_texture, false);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
230
src/Nazara/Widgets/Canvas.cpp
Normal file
230
src/Nazara/Widgets/Canvas.cpp
Normal file
@@ -0,0 +1,230 @@
|
||||
// Copyright (C) 2020 Jérôme Leclercq
|
||||
// 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/Canvas.hpp>
|
||||
#include <limits>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
std::size_t Canvas::RegisterWidget(BaseWidget* widget)
|
||||
{
|
||||
WidgetEntry box;
|
||||
box.cursor = widget->GetCursor();
|
||||
box.widget = widget;
|
||||
|
||||
std::size_t index = m_widgetEntries.size();
|
||||
m_widgetEntries.emplace_back(box);
|
||||
|
||||
NotifyWidgetBoxUpdate(index);
|
||||
return index;
|
||||
}
|
||||
|
||||
void Canvas::UnregisterWidget(std::size_t index)
|
||||
{
|
||||
WidgetEntry& entry = m_widgetEntries[index];
|
||||
|
||||
if (m_hoveredWidget == index)
|
||||
m_hoveredWidget = InvalidCanvasIndex;
|
||||
|
||||
if (m_keyboardOwner == index)
|
||||
m_keyboardOwner = InvalidCanvasIndex;
|
||||
|
||||
if (m_widgetEntries.size() > 1U)
|
||||
{
|
||||
WidgetEntry& lastEntry = m_widgetEntries.back();
|
||||
std::size_t lastEntryIndex = m_widgetEntries.size() - 1;
|
||||
|
||||
entry = std::move(lastEntry);
|
||||
entry.widget->UpdateCanvasIndex(index);
|
||||
|
||||
if (m_hoveredWidget == lastEntryIndex)
|
||||
m_hoveredWidget = index;
|
||||
|
||||
if (m_keyboardOwner == lastEntryIndex)
|
||||
m_keyboardOwner = index;
|
||||
}
|
||||
|
||||
m_widgetEntries.pop_back();
|
||||
}
|
||||
|
||||
void Canvas::OnEventMouseButtonPressed(const Nz::EventHandler* /*eventHandler*/, const Nz::WindowEvent::MouseButtonEvent& event)
|
||||
{
|
||||
if (m_hoveredWidget != InvalidCanvasIndex)
|
||||
{
|
||||
WidgetEntry& hoveredWidget = m_widgetEntries[m_hoveredWidget];
|
||||
|
||||
int x = static_cast<int>(std::round(event.x - hoveredWidget.box.x));
|
||||
int y = static_cast<int>(std::round(event.y - hoveredWidget.box.y));
|
||||
|
||||
hoveredWidget.widget->OnMouseButtonPress(x, y, event.button);
|
||||
}
|
||||
}
|
||||
|
||||
void Canvas::OnEventMouseButtonRelease(const Nz::EventHandler* /*eventHandler*/, const Nz::WindowEvent::MouseButtonEvent& event)
|
||||
{
|
||||
if (m_hoveredWidget != InvalidCanvasIndex)
|
||||
{
|
||||
WidgetEntry& hoveredWidget = m_widgetEntries[m_hoveredWidget];
|
||||
|
||||
int x = static_cast<int>(std::round(event.x - hoveredWidget.box.x));
|
||||
int y = static_cast<int>(std::round(event.y - hoveredWidget.box.y));
|
||||
|
||||
hoveredWidget.widget->OnMouseButtonRelease(x, y, event.button);
|
||||
}
|
||||
}
|
||||
|
||||
void Canvas::OnEventMouseMoved(const Nz::EventHandler* /*eventHandler*/, const Nz::WindowEvent::MouseMoveEvent& event)
|
||||
{
|
||||
std::size_t bestEntry = InvalidCanvasIndex;
|
||||
float bestEntryArea = std::numeric_limits<float>::infinity();
|
||||
|
||||
Nz::Vector3f mousePos(float(event.x), float(event.y), 0.f);
|
||||
for (std::size_t i = 0; i < m_widgetEntries.size(); ++i)
|
||||
{
|
||||
const Nz::Boxf& box = m_widgetEntries[i].box;
|
||||
|
||||
if (box.Contains(mousePos))
|
||||
{
|
||||
float area = box.width * box.height;
|
||||
if (area < bestEntryArea)
|
||||
{
|
||||
bestEntry = i;
|
||||
bestEntryArea = area;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (bestEntry != InvalidCanvasIndex)
|
||||
{
|
||||
if (m_hoveredWidget != bestEntry)
|
||||
{
|
||||
if (m_hoveredWidget != InvalidCanvasIndex)
|
||||
{
|
||||
WidgetEntry& previouslyHovered = m_widgetEntries[m_hoveredWidget];
|
||||
previouslyHovered.widget->OnMouseExit();
|
||||
}
|
||||
|
||||
m_hoveredWidget = bestEntry;
|
||||
m_widgetEntries[m_hoveredWidget].widget->OnMouseEnter();
|
||||
|
||||
if (m_cursorController)
|
||||
m_cursorController->UpdateCursor(Nz::Cursor::Get(m_widgetEntries[m_hoveredWidget].cursor));
|
||||
}
|
||||
|
||||
WidgetEntry& hoveredWidget = m_widgetEntries[m_hoveredWidget];
|
||||
|
||||
int x = static_cast<int>(std::round(event.x - hoveredWidget.box.x));
|
||||
int y = static_cast<int>(std::round(event.y - hoveredWidget.box.y));
|
||||
hoveredWidget.widget->OnMouseMoved(x, y, event.deltaX, event.deltaY);
|
||||
}
|
||||
else if (m_hoveredWidget != InvalidCanvasIndex)
|
||||
{
|
||||
m_widgetEntries[m_hoveredWidget].widget->OnMouseExit();
|
||||
m_hoveredWidget = InvalidCanvasIndex;
|
||||
|
||||
if (m_cursorController)
|
||||
m_cursorController->UpdateCursor(Nz::Cursor::Get(Nz::SystemCursor::Default));
|
||||
}
|
||||
}
|
||||
|
||||
void Canvas::OnEventMouseWheelMoved(const Nz::EventHandler* /*eventHandler*/, const Nz::WindowEvent::MouseWheelEvent& event)
|
||||
{
|
||||
if (m_hoveredWidget != InvalidCanvasIndex)
|
||||
{
|
||||
WidgetEntry& hoveredWidget = m_widgetEntries[m_hoveredWidget];
|
||||
|
||||
int x = static_cast<int>(std::round(event.x - hoveredWidget.box.x));
|
||||
int y = static_cast<int>(std::round(event.y - hoveredWidget.box.y));
|
||||
|
||||
hoveredWidget.widget->OnMouseWheelMoved(x, y, event.delta);
|
||||
}
|
||||
}
|
||||
|
||||
void Canvas::OnEventMouseLeft(const Nz::EventHandler* /*eventHandler*/)
|
||||
{
|
||||
if (m_hoveredWidget != InvalidCanvasIndex)
|
||||
{
|
||||
m_widgetEntries[m_hoveredWidget].widget->OnMouseExit();
|
||||
m_hoveredWidget = InvalidCanvasIndex;
|
||||
}
|
||||
}
|
||||
|
||||
void Canvas::OnEventKeyPressed(const Nz::EventHandler* eventHandler, const Nz::WindowEvent::KeyEvent& event)
|
||||
{
|
||||
if (m_keyboardOwner != InvalidCanvasIndex)
|
||||
{
|
||||
if (m_widgetEntries[m_keyboardOwner].widget->OnKeyPressed(event))
|
||||
return;
|
||||
|
||||
if (event.virtualKey == Nz::Keyboard::VKey::Tab)
|
||||
{
|
||||
if (!event.shift)
|
||||
{
|
||||
// Forward
|
||||
for (std::size_t i = m_keyboardOwner + 1; i < m_widgetEntries.size(); ++i)
|
||||
{
|
||||
if (m_widgetEntries[i].widget->IsFocusable())
|
||||
{
|
||||
SetKeyboardOwner(i);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
for (std::size_t i = 0; i < m_keyboardOwner; ++i)
|
||||
{
|
||||
if (m_widgetEntries[i].widget->IsFocusable())
|
||||
{
|
||||
SetKeyboardOwner(i);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Backward
|
||||
for (decltype(m_widgetEntries)::reverse_iterator rit{ m_widgetEntries.begin() + m_keyboardOwner }; rit != m_widgetEntries.rend(); ++rit)
|
||||
{
|
||||
if (rit->widget->IsFocusable())
|
||||
{
|
||||
SetKeyboardOwner(std::distance(m_widgetEntries.begin(), rit.base()) - 1);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
decltype(m_widgetEntries)::reverse_iterator rend { m_widgetEntries.begin() + m_keyboardOwner };
|
||||
for (auto rit = m_widgetEntries.rbegin(); rit != rend; ++rit)
|
||||
{
|
||||
if (rit->widget->IsFocusable())
|
||||
{
|
||||
SetKeyboardOwner(std::distance(m_widgetEntries.begin(), rit.base()) - 1);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
OnUnhandledKeyPressed(eventHandler, event);
|
||||
}
|
||||
|
||||
void Canvas::OnEventKeyReleased(const Nz::EventHandler* eventHandler, const Nz::WindowEvent::KeyEvent& event)
|
||||
{
|
||||
if (m_keyboardOwner != InvalidCanvasIndex)
|
||||
m_widgetEntries[m_keyboardOwner].widget->OnKeyReleased(event);
|
||||
|
||||
OnUnhandledKeyReleased(eventHandler, event);
|
||||
}
|
||||
|
||||
void Canvas::OnEventTextEntered(const Nz::EventHandler* /*eventHandler*/, const Nz::WindowEvent::TextEvent& event)
|
||||
{
|
||||
if (m_keyboardOwner != InvalidCanvasIndex)
|
||||
m_widgetEntries[m_keyboardOwner].widget->OnTextEntered(event.character, event.repeated);
|
||||
}
|
||||
|
||||
void Canvas::OnEventTextEdited(const Nz::EventHandler* /*eventHandler*/, const Nz::WindowEvent::EditEvent& event)
|
||||
{
|
||||
if (m_keyboardOwner != InvalidCanvasIndex)
|
||||
m_widgetEntries[m_keyboardOwner].widget->OnTextEdited(event.text, event.length);
|
||||
}
|
||||
}
|
||||
31
src/Nazara/Widgets/Debug/NewOverload.cpp
Normal file
31
src/Nazara/Widgets/Debug/NewOverload.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
// Copyright (C) 2020 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Graphics module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Widgets/Config.hpp>
|
||||
#if NAZARA_WIDGETS_MANAGE_MEMORY
|
||||
|
||||
#include <Nazara/Core/MemoryManager.hpp>
|
||||
#include <new> // Nécessaire ?
|
||||
|
||||
void* operator new(std::size_t size)
|
||||
{
|
||||
return Nz::MemoryManager::Allocate(size, false);
|
||||
}
|
||||
|
||||
void* operator new[](std::size_t size)
|
||||
{
|
||||
return Nz::MemoryManager::Allocate(size, true);
|
||||
}
|
||||
|
||||
void operator delete(void* pointer) noexcept
|
||||
{
|
||||
Nz::MemoryManager::Free(pointer, false);
|
||||
}
|
||||
|
||||
void operator delete[](void* pointer) noexcept
|
||||
{
|
||||
Nz::MemoryManager::Free(pointer, true);
|
||||
}
|
||||
|
||||
#endif
|
||||
36
src/Nazara/Widgets/LabelWidget.cpp
Normal file
36
src/Nazara/Widgets/LabelWidget.cpp
Normal file
@@ -0,0 +1,36 @@
|
||||
// Copyright (C) 2017 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Development Kit"
|
||||
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
|
||||
|
||||
#include <Nazara/Widgets/LabelWidget.hpp>
|
||||
#include <Nazara/Graphics/BasicMaterial.hpp>
|
||||
#include <Nazara/Graphics/Material.hpp>
|
||||
#include <Nazara/Graphics/MaterialPass.hpp>
|
||||
#include <Nazara/Graphics/Components/GraphicsComponent.hpp>
|
||||
#include <Nazara/Utility/Components/NodeComponent.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
LabelWidget::LabelWidget(BaseWidget* parent) :
|
||||
BaseWidget(parent)
|
||||
{
|
||||
auto materialPass = std::make_shared<MaterialPass>(BasicMaterial::GetSettings());
|
||||
materialPass->EnableFlag(Nz::MaterialPassFlag::Transparent);
|
||||
materialPass->EnableDepthBuffer(true);
|
||||
materialPass->EnableDepthWrite(false);
|
||||
materialPass->EnableBlending(true);
|
||||
materialPass->SetBlendEquation(Nz::BlendEquation::Add, Nz::BlendEquation::Add);
|
||||
materialPass->SetBlendFunc(Nz::BlendFunc::SrcAlpha, Nz::BlendFunc::InvSrcAlpha, Nz::BlendFunc::One, Nz::BlendFunc::Zero);
|
||||
|
||||
auto material = std::make_shared<Material>();
|
||||
material->AddPass("ForwardPass", std::move(materialPass));
|
||||
|
||||
m_textSprite = std::make_shared<TextSprite>(std::move(material));
|
||||
|
||||
m_textEntity = CreateEntity();
|
||||
GetRegistry().emplace<GraphicsComponent>(m_textEntity).AttachRenderable(m_textSprite);
|
||||
GetRegistry().emplace<NodeComponent>(m_textEntity).SetParent(this);
|
||||
|
||||
Layout();
|
||||
}
|
||||
}
|
||||
22
src/Nazara/Widgets/Widgets.cpp
Normal file
22
src/Nazara/Widgets/Widgets.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
// Copyright (C) 2020 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Engine - Graphics module"
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Widgets/Widgets.hpp>
|
||||
#include <Nazara/Widgets/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
/*!
|
||||
* \ingroup widgets
|
||||
* \class Nz::Widgets
|
||||
* \brief Widgets class that represents the module initializer of Widgets
|
||||
*/
|
||||
Widgets::Widgets(Config config) :
|
||||
ModuleBase("Widgets", this)
|
||||
{
|
||||
ECS::RegisterComponents();
|
||||
}
|
||||
|
||||
Widgets* Widgets::s_instance = nullptr;
|
||||
}
|
||||
Reference in New Issue
Block a user