Sdk/Widget: Add Canvas system, mouse movement event for widgets (buttons react to hovering)
Former-commit-id: 685295853d1f0edf28c36b2f698eca881da25ea2 [formerly f65f951ed3d2edfd9c12d390837cd13e5691eaa3] [formerly 5e239f37c28bf5e93a71cba29a94f0de680a79a2 [formerly 38346fa5d68c7bf79ddebc1990959e7c56617640]] Former-commit-id: 92da4ed40fb63fe0b02c7543ea64cebda0575623 [formerly 2db1fe5fca87a1461ceb8314709d764af079a7be] Former-commit-id: 77d61a99fd5a2bb21701fbe4a4b4451c3655663a
This commit is contained in:
@@ -16,12 +16,16 @@
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
class Canvas;
|
||||
|
||||
class NDK_API BaseWidget : public Nz::Node
|
||||
{
|
||||
friend Canvas;
|
||||
|
||||
public:
|
||||
struct Padding;
|
||||
|
||||
inline BaseWidget(WorldHandle world, BaseWidget* parent = nullptr);
|
||||
BaseWidget(BaseWidget* parent);
|
||||
BaseWidget(const BaseWidget&) = delete;
|
||||
BaseWidget(BaseWidget&&) = default;
|
||||
virtual ~BaseWidget();
|
||||
@@ -32,6 +36,7 @@ namespace Ndk
|
||||
|
||||
//virtual BaseWidget* Clone() const = 0;
|
||||
|
||||
inline Canvas* GetCanvas();
|
||||
inline const Padding& GetPadding() const;
|
||||
inline const Nz::Vector2f& GetContentSize() const;
|
||||
inline Nz::Vector2f GetSize() const;
|
||||
@@ -57,10 +62,21 @@ namespace Ndk
|
||||
EntityHandle CreateEntity();
|
||||
void DestroyEntity(Entity* entity);
|
||||
virtual void Layout();
|
||||
void InvalidateNode() override;
|
||||
|
||||
virtual void OnMouseEnter();
|
||||
virtual void OnMouseMoved(int x, int y, int deltaX, int deltaY);
|
||||
virtual void OnMouseExit();
|
||||
|
||||
private:
|
||||
inline BaseWidget();
|
||||
|
||||
inline void UpdateCanvasIndex(std::size_t index);
|
||||
|
||||
std::size_t m_canvasIndex;
|
||||
std::vector<EntityOwner> m_entities;
|
||||
std::vector<BaseWidget*> m_children;
|
||||
std::vector<std::unique_ptr<BaseWidget>> m_children;
|
||||
Canvas* m_canvas;
|
||||
EntityOwner m_backgroundEntity;
|
||||
Padding m_padding;
|
||||
WorldHandle m_world;
|
||||
|
||||
@@ -8,18 +8,23 @@
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
inline BaseWidget::BaseWidget(WorldHandle world, BaseWidget* parent) :
|
||||
m_world(std::move(world)),
|
||||
inline BaseWidget::BaseWidget() :
|
||||
m_backgroundColor(Nz::Color(230, 230, 230, 255)),
|
||||
m_canvas(nullptr),
|
||||
m_contentSize(50.f, 50.f),
|
||||
m_widgetParent(parent)
|
||||
m_widgetParent(nullptr)
|
||||
{
|
||||
SetPadding(5.f, 5.f, 5.f, 5.f);
|
||||
}
|
||||
|
||||
inline void BaseWidget::AddChild(std::unique_ptr<BaseWidget>&& widget)
|
||||
{
|
||||
m_children.push_back(widget.release());
|
||||
m_children.emplace_back(std::move(widget));
|
||||
}
|
||||
|
||||
inline Canvas* BaseWidget::GetCanvas()
|
||||
{
|
||||
return m_canvas;
|
||||
}
|
||||
|
||||
inline const BaseWidget::Padding& BaseWidget::GetPadding() const
|
||||
@@ -53,4 +58,9 @@ namespace Ndk
|
||||
|
||||
Layout();
|
||||
}
|
||||
|
||||
inline void BaseWidget::UpdateCanvasIndex(std::size_t index)
|
||||
{
|
||||
m_canvasIndex = index;
|
||||
}
|
||||
}
|
||||
|
||||
61
SDK/include/NDK/Canvas.hpp
Normal file
61
SDK/include/NDK/Canvas.hpp
Normal file
@@ -0,0 +1,61 @@
|
||||
// Copyright (C) 2015 Jérôme Leclercq
|
||||
// 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_CANVAS_HPP
|
||||
#define NDK_CANVAS_HPP
|
||||
|
||||
#include <NDK/Prerequesites.hpp>
|
||||
#include <NDK/BaseWidget.hpp>
|
||||
#include <Nazara/Utility/EventHandler.hpp>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
class NDK_API Canvas : public BaseWidget
|
||||
{
|
||||
friend BaseWidget;
|
||||
|
||||
public:
|
||||
struct Padding;
|
||||
|
||||
inline Canvas(WorldHandle world, Nz::EventHandler& eventHandler);
|
||||
Canvas(const Canvas&) = delete;
|
||||
Canvas(Canvas&&) = delete;
|
||||
~Canvas() = default;
|
||||
|
||||
inline const WorldHandle& GetWorld() const;
|
||||
|
||||
void ResizeToContent() override;
|
||||
|
||||
Canvas& operator=(const Canvas&) = delete;
|
||||
Canvas& operator=(Canvas&&) = delete;
|
||||
|
||||
protected:
|
||||
void Layout() override;
|
||||
|
||||
void NotifyWidgetUpdate(std::size_t index);
|
||||
std::size_t RegisterWidget(BaseWidget* widget);
|
||||
void UnregisterWidget(std::size_t index);
|
||||
|
||||
private:
|
||||
void OnMouseMoved(const Nz::EventHandler* eventHandler, const Nz::WindowEvent::MouseMoveEvent& event);
|
||||
|
||||
struct WidgetBox
|
||||
{
|
||||
BaseWidget* widget;
|
||||
Nz::Boxf box;
|
||||
};
|
||||
|
||||
NazaraSlot(Nz::EventHandler, OnMouseMoved, m_mouseMovedSlot);
|
||||
|
||||
std::vector<WidgetBox> m_widgetBoxes;
|
||||
BaseWidget* m_hoveredWidget;
|
||||
WorldHandle m_world;
|
||||
};
|
||||
}
|
||||
|
||||
#include <NDK/Canvas.inl>
|
||||
|
||||
#endif // NDK_CANVAS_HPP
|
||||
22
SDK/include/NDK/Canvas.inl
Normal file
22
SDK/include/NDK/Canvas.inl
Normal file
@@ -0,0 +1,22 @@
|
||||
// Copyright (C) 2015 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Development Kit"
|
||||
// For conditions of distribution and use, see copyright notice in Prerequesites.hpp
|
||||
|
||||
#include <NDK/Canvas.hpp>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
inline Canvas::Canvas(WorldHandle world, Nz::EventHandler& eventHandler) :
|
||||
m_hoveredWidget(nullptr),
|
||||
m_world(std::move(world))
|
||||
{
|
||||
m_canvas = this;
|
||||
|
||||
m_mouseMovedSlot.Connect(eventHandler.OnMouseMoved, this, &Canvas::OnMouseMoved);
|
||||
}
|
||||
|
||||
inline const WorldHandle& Canvas::GetWorld() const
|
||||
{
|
||||
return m_world;
|
||||
}
|
||||
}
|
||||
@@ -20,7 +20,7 @@ namespace Ndk
|
||||
class NDK_API ButtonWidget : public BaseWidget
|
||||
{
|
||||
public:
|
||||
ButtonWidget(const WorldHandle& world, BaseWidget* parent = nullptr);
|
||||
ButtonWidget(BaseWidget* parent = nullptr);
|
||||
ButtonWidget(const ButtonWidget&) = delete;
|
||||
ButtonWidget(ButtonWidget&&) = default;
|
||||
~ButtonWidget() = default;
|
||||
@@ -37,6 +37,10 @@ namespace Ndk
|
||||
private:
|
||||
void Layout() override;
|
||||
|
||||
void OnMouseEnter() override;
|
||||
void OnMouseMoved(int x, int y, int deltaX, int deltaY) override;
|
||||
void OnMouseExit() override;
|
||||
|
||||
EntityHandle m_textEntity;
|
||||
EntityHandle m_gradientEntity;
|
||||
Nz::SpriteRef m_gradientSprite;
|
||||
|
||||
@@ -19,7 +19,7 @@ namespace Ndk
|
||||
class NDK_API LabelWidget : public BaseWidget
|
||||
{
|
||||
public:
|
||||
LabelWidget(const WorldHandle& world, BaseWidget* parent = nullptr);
|
||||
LabelWidget(BaseWidget* parent = nullptr);
|
||||
LabelWidget(const LabelWidget&) = delete;
|
||||
LabelWidget(LabelWidget&&) = default;
|
||||
~LabelWidget() = default;
|
||||
|
||||
Reference in New Issue
Block a user