Merge branch 'master' into console-widget
This commit is contained in:
@@ -23,7 +23,7 @@ namespace Ndk
|
||||
using Factory = std::function<BaseComponent*()>;
|
||||
|
||||
BaseComponent(ComponentIndex componentIndex);
|
||||
BaseComponent(BaseComponent&&) = default;
|
||||
BaseComponent(BaseComponent&&) noexcept = default;
|
||||
virtual ~BaseComponent();
|
||||
|
||||
virtual std::unique_ptr<BaseComponent> Clone() const = 0;
|
||||
@@ -34,7 +34,7 @@ namespace Ndk
|
||||
inline static ComponentIndex GetMaxComponentIndex();
|
||||
|
||||
BaseComponent& operator=(const BaseComponent&) = delete;
|
||||
BaseComponent& operator=(BaseComponent&&) = default;
|
||||
BaseComponent& operator=(BaseComponent&&) noexcept = default;
|
||||
|
||||
protected:
|
||||
BaseComponent(const BaseComponent&) = default;
|
||||
|
||||
@@ -30,7 +30,7 @@ namespace Ndk
|
||||
|
||||
BaseWidget(BaseWidget* parent);
|
||||
BaseWidget(const BaseWidget&) = delete;
|
||||
BaseWidget(BaseWidget&&) = default;
|
||||
BaseWidget(BaseWidget&&) = delete;
|
||||
virtual ~BaseWidget();
|
||||
|
||||
template<typename T, typename... Args> T* Add(Args&&... args);
|
||||
@@ -46,44 +46,61 @@ namespace Ndk
|
||||
|
||||
void EnableBackground(bool enable);
|
||||
|
||||
template<typename F> void ForEachWidgetChild(F iterator);
|
||||
template<typename F> void ForEachWidgetChild(F iterator) const;
|
||||
|
||||
//virtual BaseWidget* Clone() const = 0;
|
||||
|
||||
inline const Nz::Color& GetBackgroundColor() const;
|
||||
inline Canvas* GetCanvas();
|
||||
inline Nz::SystemCursor GetCursor() const;
|
||||
inline const Padding& GetPadding() const;
|
||||
inline Nz::Vector2f GetContentOrigin() const;
|
||||
inline const Nz::Vector2f& GetContentSize() const;
|
||||
inline float GetHeight() const;
|
||||
|
||||
inline float GetMaximumHeight() const;
|
||||
inline Nz::Vector2f GetMaximumSize() const;
|
||||
inline float GetMaximumWidth() const;
|
||||
|
||||
inline float GetMinimumHeight() const;
|
||||
inline Nz::Vector2f GetMinimumSize() const;
|
||||
inline float GetMinimumWidth() const;
|
||||
|
||||
inline float GetPreferredHeight() const;
|
||||
inline Nz::Vector2f GetPreferredSize() const;
|
||||
inline float GetPreferredWidth() const;
|
||||
|
||||
inline Nz::Vector2f GetSize() const;
|
||||
inline float GetWidth() const;
|
||||
inline std::size_t GetWidgetChildCount() const;
|
||||
|
||||
bool HasFocus() const;
|
||||
|
||||
inline bool IsVisible() const;
|
||||
|
||||
virtual void ResizeToContent() = 0;
|
||||
void Resize(const Nz::Vector2f& size);
|
||||
|
||||
void SetBackgroundColor(const Nz::Color& color);
|
||||
void SetCursor(Nz::SystemCursor systemCursor);
|
||||
inline void SetContentSize(const Nz::Vector2f& size);
|
||||
void SetFocus();
|
||||
inline void SetPadding(float left, float top, float right, float bottom);
|
||||
void SetSize(const Nz::Vector2f& size);
|
||||
|
||||
inline void SetFixedHeight(float fixedHeight);
|
||||
inline void SetFixedSize(const Nz::Vector2f& fixedSize);
|
||||
inline void SetFixedWidth(float fixedWidth);
|
||||
|
||||
inline void SetMaximumHeight(float maximumHeight);
|
||||
inline void SetMaximumSize(const Nz::Vector2f& maximumSize);
|
||||
inline void SetMaximumWidth(float maximumWidth);
|
||||
|
||||
inline void SetMinimumHeight(float minimumHeight);
|
||||
inline void SetMinimumSize(const Nz::Vector2f& minimumSize);
|
||||
inline void SetMinimumWidth(float minimumWidth);
|
||||
|
||||
void Show(bool show = true);
|
||||
|
||||
BaseWidget& operator=(const BaseWidget&) = delete;
|
||||
BaseWidget& operator=(BaseWidget&&) = default;
|
||||
|
||||
struct Padding
|
||||
{
|
||||
float left;
|
||||
float top;
|
||||
float right;
|
||||
float bottom;
|
||||
};
|
||||
BaseWidget& operator=(BaseWidget&&) = delete;
|
||||
|
||||
protected:
|
||||
const EntityHandle& CreateEntity(bool isContentEntity);
|
||||
const EntityHandle& CreateEntity();
|
||||
void DestroyEntity(Entity* entity);
|
||||
virtual void Layout();
|
||||
|
||||
@@ -102,10 +119,12 @@ namespace Ndk
|
||||
virtual void OnParentResized(const Nz::Vector2f& newSize);
|
||||
virtual void OnTextEntered(char32_t character, bool repeated);
|
||||
|
||||
inline void SetPreferredSize(const Nz::Vector2f& preferredSize);
|
||||
|
||||
private:
|
||||
inline BaseWidget();
|
||||
|
||||
inline void DestroyChild(BaseWidget* widget);
|
||||
void DestroyChild(BaseWidget* widget);
|
||||
void DestroyChildren();
|
||||
inline bool IsRegisteredToCanvas() const;
|
||||
inline void NotifyParentResized(const Nz::Vector2f& newSize);
|
||||
@@ -117,7 +136,6 @@ namespace Ndk
|
||||
struct WidgetEntity
|
||||
{
|
||||
EntityOwner handle;
|
||||
bool isContent;
|
||||
};
|
||||
|
||||
static constexpr std::size_t InvalidCanvasIndex = std::numeric_limits<std::size_t>::max();
|
||||
@@ -127,12 +145,14 @@ namespace Ndk
|
||||
std::vector<std::unique_ptr<BaseWidget>> m_children;
|
||||
Canvas* m_canvas;
|
||||
EntityOwner m_backgroundEntity;
|
||||
Padding m_padding;
|
||||
WorldHandle m_world;
|
||||
Nz::Color m_backgroundColor;
|
||||
Nz::SpriteRef m_backgroundSprite;
|
||||
Nz::SystemCursor m_cursor;
|
||||
Nz::Vector2f m_contentSize;
|
||||
Nz::Vector2f m_maximumSize;
|
||||
Nz::Vector2f m_minimumSize;
|
||||
Nz::Vector2f m_preferredSize;
|
||||
Nz::Vector2f m_size;
|
||||
BaseWidget* m_widgetParent;
|
||||
bool m_visible;
|
||||
};
|
||||
|
||||
@@ -13,11 +13,13 @@ namespace Ndk
|
||||
m_canvas(nullptr),
|
||||
m_backgroundColor(Nz::Color(230, 230, 230, 255)),
|
||||
m_cursor(Nz::SystemCursor_Default),
|
||||
m_contentSize(50.f, 50.f),
|
||||
m_size(50.f, 50.f),
|
||||
m_maximumSize(std::numeric_limits<float>::infinity()),
|
||||
m_minimumSize(0.f),
|
||||
m_preferredSize(-1),
|
||||
m_widgetParent(nullptr),
|
||||
m_visible(true)
|
||||
{
|
||||
SetPadding(5.f, 5.f, 5.f, 5.f);
|
||||
}
|
||||
|
||||
template<typename T, typename... Args>
|
||||
@@ -64,6 +66,20 @@ namespace Ndk
|
||||
SetPosition(GetPosition(Nz::CoordSys_Local).x, (parentSize.y - mySize.y) / 2.f);
|
||||
}
|
||||
|
||||
template<typename F>
|
||||
inline void BaseWidget::ForEachWidgetChild(F iterator)
|
||||
{
|
||||
for (const auto& child : m_children)
|
||||
iterator(child.get());
|
||||
}
|
||||
|
||||
template<typename F>
|
||||
inline void BaseWidget::ForEachWidgetChild(F iterator) const
|
||||
{
|
||||
for (const auto& child : m_children)
|
||||
iterator(static_cast<const BaseWidget*>(child.get()));
|
||||
}
|
||||
|
||||
inline const Nz::Color& BaseWidget::GetBackgroundColor() const
|
||||
{
|
||||
return m_backgroundColor;
|
||||
@@ -79,24 +95,69 @@ namespace Ndk
|
||||
return m_cursor;
|
||||
}
|
||||
|
||||
inline const BaseWidget::Padding& BaseWidget::GetPadding() const
|
||||
inline float BaseWidget::GetHeight() const
|
||||
{
|
||||
return m_padding;
|
||||
return m_size.y;
|
||||
}
|
||||
|
||||
inline Nz::Vector2f BaseWidget::GetContentOrigin() const
|
||||
inline float BaseWidget::GetMaximumHeight() const
|
||||
{
|
||||
return { m_padding.left, m_padding.top };
|
||||
return m_maximumSize.y;
|
||||
}
|
||||
|
||||
inline const Nz::Vector2f& BaseWidget::GetContentSize() const
|
||||
inline Nz::Vector2f BaseWidget::GetMaximumSize() const
|
||||
{
|
||||
return m_contentSize;
|
||||
return m_maximumSize;
|
||||
}
|
||||
|
||||
inline float BaseWidget::GetMaximumWidth() const
|
||||
{
|
||||
return m_maximumSize.x;
|
||||
}
|
||||
|
||||
inline float BaseWidget::GetMinimumHeight() const
|
||||
{
|
||||
return m_minimumSize.y;
|
||||
}
|
||||
|
||||
inline Nz::Vector2f BaseWidget::GetMinimumSize() const
|
||||
{
|
||||
return m_minimumSize;
|
||||
}
|
||||
|
||||
inline float BaseWidget::GetMinimumWidth() const
|
||||
{
|
||||
return m_minimumSize.x;
|
||||
}
|
||||
|
||||
inline float BaseWidget::GetPreferredHeight() const
|
||||
{
|
||||
return m_preferredSize.y;
|
||||
}
|
||||
|
||||
inline Nz::Vector2f BaseWidget::GetPreferredSize() const
|
||||
{
|
||||
return m_preferredSize;
|
||||
}
|
||||
|
||||
inline float BaseWidget::GetPreferredWidth() const
|
||||
{
|
||||
return m_preferredSize.x;
|
||||
}
|
||||
|
||||
inline Nz::Vector2f BaseWidget::GetSize() const
|
||||
{
|
||||
return Nz::Vector2f(m_contentSize.x + m_padding.left + m_padding.right, m_contentSize.y + m_padding.top + m_padding.bottom);
|
||||
return Nz::Vector2f(GetWidth(), GetHeight());
|
||||
}
|
||||
|
||||
inline float BaseWidget::GetWidth() const
|
||||
{
|
||||
return m_size.x;
|
||||
}
|
||||
|
||||
inline std::size_t BaseWidget::GetWidgetChildCount() const
|
||||
{
|
||||
return m_children.size();
|
||||
}
|
||||
|
||||
inline bool BaseWidget::IsVisible() const
|
||||
@@ -104,22 +165,79 @@ namespace Ndk
|
||||
return m_visible;
|
||||
}
|
||||
|
||||
inline void BaseWidget::SetContentSize(const Nz::Vector2f& size)
|
||||
inline void BaseWidget::SetFixedHeight(float fixedHeight)
|
||||
{
|
||||
NotifyParentResized(size);
|
||||
m_contentSize = size;
|
||||
|
||||
Layout();
|
||||
SetMaximumHeight(fixedHeight);
|
||||
SetMinimumHeight(fixedHeight);
|
||||
}
|
||||
|
||||
inline void BaseWidget::SetPadding(float left, float top, float right, float bottom)
|
||||
inline void BaseWidget::SetFixedSize(const Nz::Vector2f& fixedSize)
|
||||
{
|
||||
m_padding.left = left;
|
||||
m_padding.top = top;
|
||||
m_padding.bottom = bottom;
|
||||
m_padding.right = right;
|
||||
SetMaximumSize(fixedSize);
|
||||
SetMinimumSize(fixedSize);
|
||||
}
|
||||
|
||||
Layout();
|
||||
inline void BaseWidget::SetFixedWidth(float fixedWidth)
|
||||
{
|
||||
SetMaximumWidth(fixedWidth);
|
||||
SetMinimumWidth(fixedWidth);
|
||||
}
|
||||
|
||||
inline void BaseWidget::SetMaximumHeight(float maximumHeight)
|
||||
{
|
||||
Nz::Vector2f maximumSize = GetMaximumSize();
|
||||
maximumSize.y = maximumHeight;
|
||||
|
||||
SetMaximumSize(maximumSize);
|
||||
}
|
||||
|
||||
inline void BaseWidget::SetMaximumSize(const Nz::Vector2f& maximumSize)
|
||||
{
|
||||
m_maximumSize = maximumSize;
|
||||
|
||||
Nz::Vector2f size = GetSize();
|
||||
if (size.x > m_maximumSize.x || size.y > m_maximumSize.y)
|
||||
Resize(size); //< Will clamp automatically
|
||||
}
|
||||
|
||||
inline void BaseWidget::SetMaximumWidth(float maximumWidth)
|
||||
{
|
||||
Nz::Vector2f maximumSize = GetMaximumSize();
|
||||
maximumSize.x = maximumWidth;
|
||||
|
||||
SetMaximumSize(maximumSize);
|
||||
}
|
||||
|
||||
inline void BaseWidget::SetMinimumHeight(float minimumHeight)
|
||||
{
|
||||
Nz::Vector2f minimumSize = GetMinimumSize();
|
||||
minimumSize.y = minimumHeight;
|
||||
|
||||
SetMinimumSize(minimumSize);
|
||||
}
|
||||
|
||||
inline void BaseWidget::SetMinimumSize(const Nz::Vector2f& minimumSize)
|
||||
{
|
||||
m_minimumSize = minimumSize;
|
||||
|
||||
Nz::Vector2f size = GetSize();
|
||||
if (size.x < m_minimumSize.x || size.y < m_minimumSize.y)
|
||||
Resize(size); //< Will clamp automatically
|
||||
}
|
||||
|
||||
inline void BaseWidget::SetMinimumWidth(float minimumWidth)
|
||||
{
|
||||
Nz::Vector2f minimumSize = GetMinimumSize();
|
||||
minimumSize.x = minimumWidth;
|
||||
|
||||
SetMinimumSize(minimumSize);
|
||||
}
|
||||
|
||||
inline void BaseWidget::SetPreferredSize(const Nz::Vector2f& preferredSize)
|
||||
{
|
||||
m_preferredSize = preferredSize;
|
||||
|
||||
Resize(m_preferredSize);
|
||||
}
|
||||
|
||||
inline bool BaseWidget::IsRegisteredToCanvas() const
|
||||
|
||||
@@ -28,8 +28,6 @@ namespace Ndk
|
||||
|
||||
inline const WorldHandle& GetWorld() const;
|
||||
|
||||
void ResizeToContent() override;
|
||||
|
||||
Canvas& operator=(const Canvas&) = delete;
|
||||
Canvas& operator=(Canvas&&) = delete;
|
||||
|
||||
|
||||
@@ -27,9 +27,6 @@ namespace Ndk
|
||||
m_mouseMovedSlot.Connect(eventHandler.OnMouseMoved, this, &Canvas::OnEventMouseMoved);
|
||||
m_mouseLeftSlot.Connect(eventHandler.OnMouseLeft, this, &Canvas::OnEventMouseLeft);
|
||||
m_textEnteredSlot.Connect(eventHandler.OnTextEntered, this, &Canvas::OnEventTextEntered);
|
||||
|
||||
// Disable padding by default
|
||||
SetPadding(0.f, 0.f, 0.f, 0.f);
|
||||
}
|
||||
|
||||
inline Canvas::~Canvas()
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
namespace Ndk
|
||||
{
|
||||
template<typename ComponentType>
|
||||
class Component : public BaseComponent
|
||||
class Component : public BaseComponent, public Nz::HandledObject<ComponentType>
|
||||
{
|
||||
public:
|
||||
Component();
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
#include <NDK/Components/CameraComponent.hpp>
|
||||
#include <NDK/Components/CollisionComponent2D.hpp>
|
||||
#include <NDK/Components/CollisionComponent3D.hpp>
|
||||
#include <NDK/Components/ConstraintComponent2D.hpp>
|
||||
#include <NDK/Components/DebugComponent.hpp>
|
||||
#include <NDK/Components/GraphicsComponent.hpp>
|
||||
#include <NDK/Components/LightComponent.hpp>
|
||||
#include <NDK/Components/ListenerComponent.hpp>
|
||||
@@ -17,6 +19,5 @@
|
||||
#include <NDK/Components/PhysicsComponent2D.hpp>
|
||||
#include <NDK/Components/PhysicsComponent3D.hpp>
|
||||
#include <NDK/Components/VelocityComponent.hpp>
|
||||
#include <NDK/Components/ConstraintComponent2D.hpp>
|
||||
|
||||
#endif // NDK_COMPONENTS_GLOBAL_HPP
|
||||
|
||||
@@ -22,7 +22,7 @@ namespace Ndk
|
||||
|
||||
using CameraComponentHandle = Nz::ObjectHandle<CameraComponent>;
|
||||
|
||||
class NDK_API CameraComponent : public Component<CameraComponent>, public Nz::AbstractViewer, public Nz::HandledObject<CameraComponent>
|
||||
class NDK_API CameraComponent : public Component<CameraComponent>, public Nz::AbstractViewer
|
||||
{
|
||||
public:
|
||||
inline CameraComponent();
|
||||
|
||||
@@ -38,7 +38,6 @@ namespace Ndk
|
||||
inline CameraComponent::CameraComponent(const CameraComponent& camera) :
|
||||
Component(camera),
|
||||
AbstractViewer(camera),
|
||||
HandledObject(camera),
|
||||
m_visibilityHash(camera.m_visibilityHash),
|
||||
m_projectionType(camera.m_projectionType),
|
||||
m_targetRegion(camera.m_targetRegion),
|
||||
|
||||
@@ -14,6 +14,10 @@
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
class CollisionComponent2D;
|
||||
|
||||
using CollisionComponent2DHandle = Nz::ObjectHandle<CollisionComponent2D>;
|
||||
|
||||
class NDK_API CollisionComponent2D : public Component<CollisionComponent2D>
|
||||
{
|
||||
friend class PhysicsSystem2D;
|
||||
|
||||
@@ -14,6 +14,10 @@
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
class CollisionComponent3D;
|
||||
|
||||
using CollisionComponent3DHandle = Nz::ObjectHandle<CollisionComponent3D>;
|
||||
|
||||
class NDK_API CollisionComponent3D : public Component<CollisionComponent3D>
|
||||
{
|
||||
friend class PhysicsSystem3D;
|
||||
|
||||
@@ -11,6 +11,10 @@
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
class ConstraintComponent2D;
|
||||
|
||||
using ConstraintComponent2DHandle = Nz::ObjectHandle<ConstraintComponent2D>;
|
||||
|
||||
class NDK_API ConstraintComponent2D : public Component<ConstraintComponent2D>
|
||||
{
|
||||
public:
|
||||
|
||||
84
SDK/include/NDK/Components/DebugComponent.hpp
Normal file
84
SDK/include/NDK/Components/DebugComponent.hpp
Normal file
@@ -0,0 +1,84 @@
|
||||
// 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
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef NDK_SERVER
|
||||
#ifndef NDK_COMPONENTS_DEBUGCOMPONENT_HPP
|
||||
#define NDK_COMPONENTS_DEBUGCOMPONENT_HPP
|
||||
|
||||
#include <Nazara/Core/Flags.hpp>
|
||||
#include <Nazara/Graphics/InstancedRenderable.hpp>
|
||||
#include <NDK/Component.hpp>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
enum class DebugDraw
|
||||
{
|
||||
//TODO: Collider2D
|
||||
Collider3D,
|
||||
GraphicsAABB,
|
||||
GraphicsOBB,
|
||||
|
||||
Max = GraphicsOBB
|
||||
};
|
||||
}
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
template<>
|
||||
struct EnumAsFlags<Ndk::DebugDraw>
|
||||
{
|
||||
static constexpr Ndk::DebugDraw max = Ndk::DebugDraw::GraphicsOBB;
|
||||
};
|
||||
}
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
using DebugDrawFlags = Nz::Flags<DebugDraw>;
|
||||
|
||||
constexpr DebugDrawFlags DebugDraw_None = 0;
|
||||
|
||||
class DebugComponent;
|
||||
|
||||
using DebugComponentHandle = Nz::ObjectHandle<DebugComponent>;
|
||||
|
||||
class NDK_API DebugComponent : public Component<DebugComponent>
|
||||
{
|
||||
friend class DebugSystem;
|
||||
|
||||
public:
|
||||
inline DebugComponent(DebugDrawFlags flags = DebugDraw_None);
|
||||
inline DebugComponent(const DebugComponent& debug);
|
||||
~DebugComponent() = default;
|
||||
|
||||
inline void Disable(DebugDrawFlags flags);
|
||||
inline void Enable(DebugDrawFlags flags);
|
||||
|
||||
inline DebugDrawFlags GetFlags() const;
|
||||
|
||||
inline bool IsEnabled(DebugDrawFlags flags) const;
|
||||
|
||||
inline DebugComponent& operator=(const DebugComponent& debug);
|
||||
|
||||
static ComponentIndex componentIndex;
|
||||
|
||||
private:
|
||||
inline const Nz::InstancedRenderableRef& GetDebugRenderable(DebugDraw option) const;
|
||||
inline DebugDrawFlags GetEnabledFlags() const;
|
||||
inline void UpdateDebugRenderable(DebugDraw option, Nz::InstancedRenderableRef renderable);
|
||||
inline void UpdateEnabledFlags(DebugDrawFlags flags);
|
||||
|
||||
static constexpr std::size_t DebugModeCount = static_cast<std::size_t>(DebugDraw::Max) + 1;
|
||||
|
||||
std::array<Nz::InstancedRenderableRef, DebugModeCount> m_debugRenderables;
|
||||
DebugDrawFlags m_enabledFlags;
|
||||
DebugDrawFlags m_flags;
|
||||
};
|
||||
}
|
||||
|
||||
#include <NDK/Components/DebugComponent.inl>
|
||||
|
||||
#endif // NDK_COMPONENTS_DEBUGCOMPONENT_HPP
|
||||
#endif // NDK_SERVER
|
||||
74
SDK/include/NDK/Components/DebugComponent.inl
Normal file
74
SDK/include/NDK/Components/DebugComponent.inl
Normal file
@@ -0,0 +1,74 @@
|
||||
// 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 <NDK/Components/DebugComponent.hpp>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
inline DebugComponent::DebugComponent(DebugDrawFlags flags) :
|
||||
m_flags(flags)
|
||||
{
|
||||
}
|
||||
|
||||
inline DebugComponent::DebugComponent(const DebugComponent& debug) :
|
||||
m_flags(debug.m_flags)
|
||||
{
|
||||
}
|
||||
|
||||
inline void DebugComponent::Disable(DebugDrawFlags flags)
|
||||
{
|
||||
m_flags &= ~flags;
|
||||
|
||||
if (m_entity)
|
||||
m_entity->Invalidate();
|
||||
}
|
||||
|
||||
inline void DebugComponent::Enable(DebugDrawFlags flags)
|
||||
{
|
||||
m_flags |= flags;
|
||||
|
||||
if (m_entity)
|
||||
m_entity->Invalidate();
|
||||
}
|
||||
|
||||
inline DebugDrawFlags DebugComponent::GetFlags() const
|
||||
{
|
||||
return m_flags;
|
||||
}
|
||||
|
||||
inline bool DebugComponent::IsEnabled(DebugDrawFlags flags) const
|
||||
{
|
||||
return (m_flags & flags) == flags;
|
||||
}
|
||||
|
||||
inline DebugComponent& DebugComponent::operator=(const DebugComponent& debug)
|
||||
{
|
||||
m_flags = debug.m_flags;
|
||||
|
||||
if (m_entity)
|
||||
m_entity->Invalidate();
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline const Nz::InstancedRenderableRef& DebugComponent::GetDebugRenderable(DebugDraw option) const
|
||||
{
|
||||
return m_debugRenderables[static_cast<std::size_t>(option)];
|
||||
}
|
||||
|
||||
inline DebugDrawFlags DebugComponent::GetEnabledFlags() const
|
||||
{
|
||||
return m_enabledFlags;
|
||||
}
|
||||
|
||||
inline void DebugComponent::UpdateDebugRenderable(DebugDraw option, Nz::InstancedRenderableRef renderable)
|
||||
{
|
||||
m_debugRenderables[static_cast<std::size_t>(option)] = std::move(renderable);
|
||||
}
|
||||
|
||||
inline void DebugComponent::UpdateEnabledFlags(DebugDrawFlags flags)
|
||||
{
|
||||
m_enabledFlags = flags;
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@
|
||||
|
||||
#include <Nazara/Graphics/CullingList.hpp>
|
||||
#include <Nazara/Graphics/InstancedRenderable.hpp>
|
||||
#include <Nazara/Math/Frustum.hpp>
|
||||
#include <Nazara/Utility/Node.hpp>
|
||||
#include <NDK/Component.hpp>
|
||||
#include <unordered_map>
|
||||
@@ -21,19 +22,20 @@ namespace Ndk
|
||||
using GraphicsComponentCullingList = Nz::CullingList<GraphicsComponent>;
|
||||
using GraphicsComponentHandle = Nz::ObjectHandle<GraphicsComponent>;
|
||||
|
||||
class NDK_API GraphicsComponent : public Component<GraphicsComponent>, public Nz::HandledObject<GraphicsComponent>
|
||||
class NDK_API GraphicsComponent : public Component<GraphicsComponent>
|
||||
{
|
||||
friend class RenderSystem;
|
||||
|
||||
public:
|
||||
using RenderableList = std::vector<Nz::InstancedRenderableRef>;
|
||||
|
||||
GraphicsComponent();
|
||||
inline GraphicsComponent();
|
||||
inline GraphicsComponent(const GraphicsComponent& graphicsComponent);
|
||||
~GraphicsComponent() = default;
|
||||
|
||||
inline void AddToCullingList(GraphicsComponentCullingList* cullingList) const;
|
||||
void AddToRenderQueue(Nz::AbstractRenderQueue* renderQueue) const;
|
||||
void AddToRenderQueueByCulling(const Nz::Frustumf& frustum, Nz::AbstractRenderQueue* renderQueue) const;
|
||||
|
||||
inline void Attach(Nz::InstancedRenderableRef renderable, int renderOrder = 0);
|
||||
void Attach(Nz::InstancedRenderableRef renderable, const Nz::Matrix4f& localMatrix, int renderOrder = 0);
|
||||
@@ -44,13 +46,19 @@ namespace Ndk
|
||||
|
||||
inline bool DoesRequireRealTimeReflections() const;
|
||||
|
||||
inline void EnsureBoundingVolumeUpdate() const;
|
||||
inline void EnsureBoundingVolumesUpdate() const;
|
||||
inline void EnsureTransformMatrixUpdate() const;
|
||||
|
||||
template<typename Func> void ForEachRenderable(const Func& func) const;
|
||||
|
||||
inline const Nz::Boxf& GetAABB() const;
|
||||
|
||||
inline void GetAttachedRenderables(RenderableList* renderables) const;
|
||||
inline std::size_t GetAttachedRenderableCount() const;
|
||||
|
||||
inline const Nz::BoundingVolumef& GetBoundingVolume() const;
|
||||
inline const Nz::BoundingVolumef& GetBoundingVolume(std::size_t renderableIndex) const;
|
||||
inline const Nz::Matrix4f& GetLocalMatrix(std::size_t renderableIndex) const;
|
||||
inline const Nz::Matrix4f& GetTransformMatrix(std::size_t renderableIndex) const;
|
||||
|
||||
inline void RemoveFromCullingList(GraphicsComponentCullingList* cullingList) const;
|
||||
|
||||
@@ -66,7 +74,8 @@ namespace Ndk
|
||||
|
||||
void ConnectInstancedRenderableSignals(Renderable& renderable);
|
||||
|
||||
inline void InvalidateBoundingVolume() const;
|
||||
inline void ForceCullingInvalidation();
|
||||
inline void InvalidateAABB() const;
|
||||
void InvalidateRenderableData(const Nz::InstancedRenderable* renderable, Nz::UInt32 flags, std::size_t index);
|
||||
void InvalidateRenderableMaterial(const Nz::InstancedRenderable* renderable, std::size_t skinIndex, std::size_t matIndex, const Nz::MaterialRef& newMat);
|
||||
inline void InvalidateRenderables();
|
||||
@@ -87,11 +96,20 @@ namespace Ndk
|
||||
|
||||
void UnregisterMaterial(Nz::Material* material);
|
||||
|
||||
void UpdateBoundingVolume() const;
|
||||
void UpdateBoundingVolumes() const;
|
||||
void UpdateTransformMatrix() const;
|
||||
|
||||
NazaraSlot(Nz::Node, OnNodeInvalidation, m_nodeInvalidationSlot);
|
||||
|
||||
using CullingListBoxEntry = GraphicsComponentCullingList::BoxEntry;
|
||||
|
||||
struct CullingBoxEntry
|
||||
{
|
||||
CullingListBoxEntry listEntry;
|
||||
|
||||
NazaraSlot(GraphicsComponentCullingList, OnCullingListRelease, cullingListReleaseSlot);
|
||||
};
|
||||
|
||||
struct MaterialEntry
|
||||
{
|
||||
NazaraSlot(Nz::Material, OnMaterialReflectionModeChange, reflectionModelChangeSlot);
|
||||
@@ -126,29 +144,21 @@ namespace Ndk
|
||||
NazaraSlot(Nz::InstancedRenderable, OnInstancedRenderableResetMaterials, renderableResetMaterialsSlot);
|
||||
NazaraSlot(Nz::InstancedRenderable, OnInstancedRenderableSkinChange, renderableSkinChangeSlot);
|
||||
|
||||
mutable Nz::BoundingVolumef boundingVolume;
|
||||
mutable Nz::InstancedRenderable::InstanceData data;
|
||||
Nz::InstancedRenderableRef renderable;
|
||||
mutable bool dataUpdated;
|
||||
};
|
||||
|
||||
using VolumeCullingListEntry = GraphicsComponentCullingList::VolumeEntry;
|
||||
|
||||
struct VolumeCullingEntry
|
||||
{
|
||||
VolumeCullingListEntry listEntry;
|
||||
|
||||
NazaraSlot(GraphicsComponentCullingList, OnCullingListRelease, cullingListReleaseSlot);
|
||||
};
|
||||
|
||||
std::size_t m_reflectiveMaterialCount;
|
||||
mutable std::vector<VolumeCullingEntry> m_volumeCullingEntries;
|
||||
mutable std::vector<CullingBoxEntry> m_cullingBoxEntries;
|
||||
std::vector<Renderable> m_renderables;
|
||||
std::unordered_map<const Nz::Material*, MaterialEntry> m_materialEntries;
|
||||
mutable Nz::BoundingVolumef m_boundingVolume;
|
||||
mutable Nz::Boxf m_aabb;
|
||||
mutable Nz::Matrix4f m_transformMatrix;
|
||||
Nz::Recti m_scissorRect;
|
||||
Nz::TextureRef m_reflectionMap;
|
||||
mutable bool m_boundingVolumeUpdated;
|
||||
mutable bool m_boundingVolumesUpdated;
|
||||
mutable bool m_transformMatrixUpdated;
|
||||
unsigned int m_reflectionMapSize;
|
||||
};
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
namespace Ndk
|
||||
{
|
||||
inline GraphicsComponent::GraphicsComponent() :
|
||||
m_reflectiveMaterialCount(0),
|
||||
m_scissorRect(-1, -1)
|
||||
{
|
||||
}
|
||||
@@ -21,10 +22,11 @@ namespace Ndk
|
||||
*/
|
||||
inline GraphicsComponent::GraphicsComponent(const GraphicsComponent& graphicsComponent) :
|
||||
Component(graphicsComponent),
|
||||
HandledObject(graphicsComponent),
|
||||
m_boundingVolume(graphicsComponent.m_boundingVolume),
|
||||
m_reflectiveMaterialCount(0),
|
||||
m_aabb(graphicsComponent.m_aabb),
|
||||
m_transformMatrix(graphicsComponent.m_transformMatrix),
|
||||
m_boundingVolumeUpdated(graphicsComponent.m_boundingVolumeUpdated),
|
||||
m_scissorRect(graphicsComponent.m_scissorRect),
|
||||
m_boundingVolumesUpdated(graphicsComponent.m_boundingVolumesUpdated),
|
||||
m_transformMatrixUpdated(graphicsComponent.m_transformMatrixUpdated)
|
||||
{
|
||||
m_renderables.reserve(graphicsComponent.m_renderables.size());
|
||||
@@ -34,12 +36,12 @@ namespace Ndk
|
||||
|
||||
inline void GraphicsComponent::AddToCullingList(GraphicsComponentCullingList* cullingList) const
|
||||
{
|
||||
m_volumeCullingEntries.emplace_back(VolumeCullingEntry{});
|
||||
VolumeCullingEntry& entry = m_volumeCullingEntries.back();
|
||||
m_cullingBoxEntries.emplace_back();
|
||||
CullingBoxEntry& entry = m_cullingBoxEntries.back();
|
||||
entry.cullingListReleaseSlot.Connect(cullingList->OnCullingListRelease, this, &GraphicsComponent::RemoveFromCullingList);
|
||||
entry.listEntry = cullingList->RegisterVolumeTest(this);
|
||||
entry.listEntry = cullingList->RegisterBoxTest(this);
|
||||
|
||||
InvalidateBoundingVolume();
|
||||
InvalidateAABB();
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -68,7 +70,7 @@ namespace Ndk
|
||||
InvalidateReflectionMap();
|
||||
}
|
||||
|
||||
InvalidateBoundingVolume();
|
||||
InvalidateAABB();
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -83,13 +85,15 @@ namespace Ndk
|
||||
{
|
||||
if (it->renderable == renderable)
|
||||
{
|
||||
InvalidateBoundingVolume();
|
||||
InvalidateAABB();
|
||||
|
||||
std::size_t materialCount = renderable->GetMaterialCount();
|
||||
for (std::size_t i = 0; i < materialCount; ++i)
|
||||
UnregisterMaterial(renderable->GetMaterial(i));
|
||||
|
||||
m_renderables.erase(it);
|
||||
|
||||
ForceCullingInvalidation();
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -111,10 +115,10 @@ namespace Ndk
|
||||
* \brief Ensures the bounding volume is up to date
|
||||
*/
|
||||
|
||||
inline void GraphicsComponent::EnsureBoundingVolumeUpdate() const
|
||||
inline void GraphicsComponent::EnsureBoundingVolumesUpdate() const
|
||||
{
|
||||
if (!m_boundingVolumeUpdated)
|
||||
UpdateBoundingVolume();
|
||||
if (!m_boundingVolumesUpdated)
|
||||
UpdateBoundingVolumes();
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -127,6 +131,17 @@ namespace Ndk
|
||||
UpdateTransformMatrix();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Gets the axis-aligned bounding box of the entity
|
||||
* \return A constant reference to the AABB
|
||||
*/
|
||||
inline const Nz::Boxf& GraphicsComponent::GetAABB() const
|
||||
{
|
||||
EnsureBoundingVolumesUpdate();
|
||||
|
||||
return m_aabb;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Gets the set of renderable elements
|
||||
*
|
||||
@@ -154,28 +169,50 @@ namespace Ndk
|
||||
return m_renderables.size();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Gets the bouding volume of the entity
|
||||
* \return A constant reference to the bounding volume
|
||||
*/
|
||||
|
||||
inline const Nz::BoundingVolumef& GraphicsComponent::GetBoundingVolume() const
|
||||
inline const Nz::BoundingVolumef& GraphicsComponent::GetBoundingVolume(std::size_t renderableIndex) const
|
||||
{
|
||||
EnsureBoundingVolumeUpdate();
|
||||
EnsureBoundingVolumesUpdate();
|
||||
|
||||
return m_boundingVolume;
|
||||
assert(renderableIndex < m_renderables.size());
|
||||
return m_renderables[renderableIndex].boundingVolume;
|
||||
}
|
||||
|
||||
inline const Nz::Matrix4f& GraphicsComponent::GetLocalMatrix(std::size_t renderableIndex) const
|
||||
{
|
||||
assert(renderableIndex < m_renderables.size());
|
||||
return m_renderables[renderableIndex].data.localMatrix;
|
||||
}
|
||||
|
||||
inline const Nz::Matrix4f& GraphicsComponent::GetTransformMatrix(std::size_t renderableIndex) const
|
||||
{
|
||||
EnsureBoundingVolumesUpdate();
|
||||
|
||||
assert(renderableIndex < m_renderables.size());
|
||||
return m_renderables[renderableIndex].data.transformMatrix;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Calls a function for every renderable attached to this component
|
||||
*
|
||||
* \param func Callback function which will be called with renderable data
|
||||
*/
|
||||
template<typename Func>
|
||||
void GraphicsComponent::ForEachRenderable(const Func& func) const
|
||||
{
|
||||
for (const auto& renderableData : m_renderables)
|
||||
func(renderableData.renderable, renderableData.data.localMatrix, renderableData.data.renderOrder);
|
||||
}
|
||||
|
||||
inline void GraphicsComponent::RemoveFromCullingList(GraphicsComponentCullingList* cullingList) const
|
||||
{
|
||||
for (auto it = m_volumeCullingEntries.begin(); it != m_volumeCullingEntries.end(); ++it)
|
||||
for (auto it = m_cullingBoxEntries.begin(); it != m_cullingBoxEntries.end(); ++it)
|
||||
{
|
||||
if (it->listEntry.GetParent() == cullingList)
|
||||
{
|
||||
if (m_volumeCullingEntries.size() > 1)
|
||||
*it = std::move(m_volumeCullingEntries.back());
|
||||
if (m_cullingBoxEntries.size() > 1)
|
||||
*it = std::move(m_cullingBoxEntries.back());
|
||||
|
||||
m_volumeCullingEntries.pop_back();
|
||||
m_cullingBoxEntries.pop_back();
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -185,7 +222,7 @@ namespace Ndk
|
||||
{
|
||||
m_scissorRect = scissorRect;
|
||||
|
||||
for (VolumeCullingEntry& entry : m_volumeCullingEntries)
|
||||
for (CullingBoxEntry& entry : m_cullingBoxEntries)
|
||||
entry.listEntry.ForceInvalidation(); //< Invalidate render queues
|
||||
}
|
||||
|
||||
@@ -197,7 +234,7 @@ namespace Ndk
|
||||
{
|
||||
renderable.data.localMatrix = localMatrix;
|
||||
|
||||
InvalidateBoundingVolume();
|
||||
InvalidateAABB();
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -219,9 +256,15 @@ namespace Ndk
|
||||
* \brief Invalidates the bounding volume
|
||||
*/
|
||||
|
||||
inline void GraphicsComponent::InvalidateBoundingVolume() const
|
||||
inline void GraphicsComponent::ForceCullingInvalidation()
|
||||
{
|
||||
m_boundingVolumeUpdated = false;
|
||||
for (CullingBoxEntry& entry : m_cullingBoxEntries)
|
||||
entry.listEntry.ForceInvalidation(); //< Invalidate render queues
|
||||
}
|
||||
|
||||
inline void GraphicsComponent::InvalidateAABB() const
|
||||
{
|
||||
m_boundingVolumesUpdated = false;
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -242,7 +285,7 @@ namespace Ndk
|
||||
{
|
||||
m_transformMatrixUpdated = false;
|
||||
|
||||
InvalidateBoundingVolume();
|
||||
InvalidateAABB();
|
||||
InvalidateRenderables();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// Copyright (C) 2017 Jérôme Leclercq
|
||||
// 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
|
||||
|
||||
@@ -13,6 +13,10 @@
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
class LightComponent;
|
||||
|
||||
using LightComponentHandle = Nz::ObjectHandle<LightComponent>;
|
||||
|
||||
class NDK_API LightComponent : public Component<LightComponent>, public Nz::Light
|
||||
{
|
||||
public:
|
||||
|
||||
@@ -12,6 +12,10 @@
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
class ListenerComponent;
|
||||
|
||||
using ListenerComponentHandle = Nz::ObjectHandle<ListenerComponent>;
|
||||
|
||||
class NDK_API ListenerComponent : public Component<ListenerComponent>
|
||||
{
|
||||
public:
|
||||
|
||||
@@ -17,7 +17,7 @@ namespace Ndk
|
||||
|
||||
using NodeComponentHandle = Nz::ObjectHandle<NodeComponent>;
|
||||
|
||||
class NDK_API NodeComponent : public Component<NodeComponent>, public Nz::Node, public Nz::HandledObject<NodeComponent>
|
||||
class NDK_API NodeComponent : public Component<NodeComponent>, public Nz::Node
|
||||
{
|
||||
public:
|
||||
NodeComponent() = default;
|
||||
|
||||
@@ -13,6 +13,10 @@
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
class ParticleEmitterComponent;
|
||||
|
||||
using ParticleEmitterComponentHandle = Nz::ObjectHandle<ParticleEmitterComponent>;
|
||||
|
||||
class NDK_API ParticleEmitterComponent : public Component<ParticleEmitterComponent>, public Nz::ParticleEmitter
|
||||
{
|
||||
public:
|
||||
|
||||
@@ -17,7 +17,7 @@ namespace Ndk
|
||||
|
||||
using ParticleGroupComponentHandle = Nz::ObjectHandle<ParticleGroupComponent>;
|
||||
|
||||
class NDK_API ParticleGroupComponent : public Component<ParticleGroupComponent>, public Nz::ParticleGroup, public Nz::HandledObject<ParticleGroupComponent>
|
||||
class NDK_API ParticleGroupComponent : public Component<ParticleGroupComponent>, public Nz::ParticleGroup
|
||||
{
|
||||
public:
|
||||
inline ParticleGroupComponent(unsigned int maxParticleCount, Nz::ParticleLayout layout);
|
||||
@@ -38,4 +38,4 @@ namespace Ndk
|
||||
#include <NDK/Components/ParticleGroupComponent.inl>
|
||||
|
||||
#endif // NDK_COMPONENTS_PARTICLEGROUPCOMPONENT_HPP
|
||||
#endif // NDK_SERVER
|
||||
#endif // NDK_SERVER
|
||||
|
||||
@@ -13,6 +13,10 @@
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
class PhysicsComponent2D;
|
||||
|
||||
using PhysicsComponent2DHandle = Nz::ObjectHandle<PhysicsComponent2D>;
|
||||
|
||||
class NDK_API PhysicsComponent2D : public Component<PhysicsComponent2D>
|
||||
{
|
||||
friend class CollisionComponent2D;
|
||||
@@ -20,32 +24,53 @@ namespace Ndk
|
||||
friend class ConstraintComponent2D;
|
||||
|
||||
public:
|
||||
PhysicsComponent2D() = default;
|
||||
PhysicsComponent2D();
|
||||
PhysicsComponent2D(const PhysicsComponent2D& physics);
|
||||
~PhysicsComponent2D() = default;
|
||||
|
||||
void AddForce(const Nz::Vector2f& force, Nz::CoordSys coordSys = Nz::CoordSys_Global);
|
||||
void AddForce(const Nz::Vector2f& force, const Nz::Vector2f& point, Nz::CoordSys coordSys = Nz::CoordSys_Global);
|
||||
void AddImpulse(const Nz::Vector2f& impulse, Nz::CoordSys coordSys = Nz::CoordSys_Global);
|
||||
void AddImpulse(const Nz::Vector2f& impulse, const Nz::Vector2f& point, Nz::CoordSys coordSys = Nz::CoordSys_Global);
|
||||
void AddTorque(float torque);
|
||||
inline void AddForce(const Nz::Vector2f& force, Nz::CoordSys coordSys = Nz::CoordSys_Global);
|
||||
inline void AddForce(const Nz::Vector2f& force, const Nz::Vector2f& point, Nz::CoordSys coordSys = Nz::CoordSys_Global);
|
||||
inline void AddImpulse(const Nz::Vector2f& impulse, Nz::CoordSys coordSys = Nz::CoordSys_Global);
|
||||
inline void AddImpulse(const Nz::Vector2f& impulse, const Nz::Vector2f& point, Nz::CoordSys coordSys = Nz::CoordSys_Global);
|
||||
inline void AddTorque(const Nz::RadianAnglef& torque);
|
||||
|
||||
Nz::Rectf GetAABB() const;
|
||||
float GetAngularVelocity() const;
|
||||
Nz::Vector2f GetCenterOfGravity(Nz::CoordSys coordSys = Nz::CoordSys_Local) const;
|
||||
float GetMass() const;
|
||||
Nz::Vector2f GetPosition() const;
|
||||
float GetRotation() const;
|
||||
Nz::Vector2f GetVelocity() const;
|
||||
inline bool ClosestPointQuery(const Nz::Vector2f& position, Nz::Vector2f* closestPoint, float* closestDistance) const;
|
||||
|
||||
bool IsSleeping() const;
|
||||
inline void EnableNodeSynchronization(bool nodeSynchronization);
|
||||
|
||||
void SetAngularVelocity(float angularVelocity);
|
||||
void SetMass(float mass);
|
||||
void SetMassCenter(const Nz::Vector2f& center);
|
||||
void SetPosition(const Nz::Vector2f& position);
|
||||
void SetRotation(float rotation);
|
||||
void SetVelocity(const Nz::Vector2f& velocity);
|
||||
inline Nz::Rectf GetAABB() const;
|
||||
inline float GetAngularDamping() const;
|
||||
inline Nz::RadianAnglef GetAngularVelocity() const;
|
||||
NAZARA_DEPRECATED("Name error, please use GetMassCenter")
|
||||
inline Nz::Vector2f GetCenterOfGravity(Nz::CoordSys coordSys = Nz::CoordSys_Local) const;
|
||||
inline float GetElasticity(std::size_t shapeIndex = 0) const;
|
||||
inline float GetFriction(std::size_t shapeIndex = 0) const;
|
||||
inline float GetMass() const;
|
||||
inline Nz::Vector2f GetMassCenter(Nz::CoordSys coordSys = Nz::CoordSys_Local) const;
|
||||
inline float GetMomentOfInertia() const;
|
||||
inline Nz::Vector2f GetPosition() const;
|
||||
inline Nz::RadianAnglef GetRotation() const;
|
||||
inline Nz::Vector2f GetSurfaceVelocity(std::size_t shapeIndex = 0) const;
|
||||
inline std::size_t GetShapeCount() const;
|
||||
inline Nz::Vector2f GetVelocity() const;
|
||||
|
||||
inline bool IsNodeSynchronizationEnabled() const;
|
||||
inline bool IsSleeping() const;
|
||||
|
||||
inline void SetAngularDamping(float angularDamping);
|
||||
inline void SetAngularVelocity(const Nz::RadianAnglef& angularVelocity);
|
||||
inline void SetElasticity(float elasticity);
|
||||
inline void SetElasticity(std::size_t shapeIndex, float friction);
|
||||
inline void SetFriction(float friction);
|
||||
inline void SetFriction(std::size_t shapeIndex, float friction);
|
||||
inline void SetMass(float mass, bool recomputeMoment = true);
|
||||
inline void SetMassCenter(const Nz::Vector2f& center, Nz::CoordSys coordSys = Nz::CoordSys_Local);
|
||||
inline void SetMomentOfInertia(float moment);
|
||||
inline void SetPosition(const Nz::Vector2f& position);
|
||||
inline void SetRotation(const Nz::RadianAnglef& rotation);
|
||||
inline void SetSurfaceVelocity(const Nz::Vector2f& velocity);
|
||||
inline void SetSurfaceVelocity(std::size_t shapeIndex, const Nz::Vector2f& velocity);
|
||||
inline void SetVelocity(const Nz::Vector2f& velocity);
|
||||
|
||||
static ComponentIndex componentIndex;
|
||||
|
||||
@@ -59,6 +84,7 @@ namespace Ndk
|
||||
void OnEntityDestruction() override;
|
||||
|
||||
std::unique_ptr<Nz::RigidBody2D> m_object;
|
||||
bool m_nodeSynchronizationEnabled;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -2,16 +2,24 @@
|
||||
// This file is part of the "Nazara Development Kit"
|
||||
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
|
||||
|
||||
#include <NDK/Components/PhysicsComponent2D.hpp>
|
||||
#include <Nazara/Core/Error.hpp>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
/*!
|
||||
* \brief Constructs a PhysicsComponent2D object by default
|
||||
*/
|
||||
inline PhysicsComponent2D::PhysicsComponent2D() :
|
||||
m_nodeSynchronizationEnabled(true)
|
||||
{
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Constructs a PhysicsComponent2D object by copy semantic
|
||||
*
|
||||
* \param physics PhysicsComponent2D to copy
|
||||
*/
|
||||
|
||||
inline PhysicsComponent2D::PhysicsComponent2D(const PhysicsComponent2D& physics)
|
||||
{
|
||||
// No copy of physical object (because we only create it when attached to an entity)
|
||||
@@ -90,13 +98,46 @@ namespace Ndk
|
||||
* \remark Produces a NazaraAssert if the physics object is invalid
|
||||
*/
|
||||
|
||||
inline void PhysicsComponent2D::AddTorque(float torque)
|
||||
inline void PhysicsComponent2D::AddTorque(const Nz::RadianAnglef& torque)
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid physics object");
|
||||
|
||||
m_object->AddTorque(torque);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Finds the closest point on the entity relative to a position
|
||||
* \return True if such a point exists (will return false if no collider exists)
|
||||
*
|
||||
* \param position The starting point which will be used for the query
|
||||
* \param closestPoint The closest point on entity surface
|
||||
* \param closestDistance The distance between the closest point and the starting point, may be negative if starting point is inside the entity
|
||||
*
|
||||
* \remark Produces a NazaraAssert if the physics object is invalid
|
||||
*/
|
||||
inline bool PhysicsComponent2D::ClosestPointQuery(const Nz::Vector2f& position, Nz::Vector2f* closestPoint, float* closestDistance) const
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid physics object");
|
||||
|
||||
return m_object->ClosestPointQuery(position, closestPoint, closestDistance);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Enables position/rotation synchronization with the NodeComponent
|
||||
*
|
||||
* By default, at every update of the PhysicsSystem2D, the NodeComponent's position and rotation (if any) will be synchronized with
|
||||
* the values of the PhysicsComponent2D. This function allows to enable/disable this behavior on a per-entity basis.
|
||||
*
|
||||
* \param nodeSynchronization Should synchronization occur between NodeComponent and PhysicsComponent2D
|
||||
*/
|
||||
inline void PhysicsComponent2D::EnableNodeSynchronization(bool nodeSynchronization)
|
||||
{
|
||||
m_nodeSynchronizationEnabled = nodeSynchronization;
|
||||
|
||||
if (m_entity)
|
||||
m_entity->Invalidate();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Gets the AABB of the physics object
|
||||
* \return AABB of the object
|
||||
@@ -110,6 +151,22 @@ namespace Ndk
|
||||
return m_object->GetAABB();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Gets the angular damping or moment of inertia of the physics object
|
||||
* \return Angular damping of the object
|
||||
*
|
||||
* \remark Produces a NazaraAssert if the physics object is invalid
|
||||
*
|
||||
* \see GetMomentOfInertia
|
||||
*/
|
||||
|
||||
inline float PhysicsComponent2D::GetAngularDamping() const
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid physics object");
|
||||
|
||||
return m_object->GetAngularDamping();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Gets the angular velocity of the physics object
|
||||
* \return Angular velocity of the object
|
||||
@@ -117,7 +174,7 @@ namespace Ndk
|
||||
* \remark Produces a NazaraAssert if the physics object is invalid
|
||||
*/
|
||||
|
||||
inline float PhysicsComponent2D::GetAngularVelocity() const
|
||||
inline Nz::RadianAnglef PhysicsComponent2D::GetAngularVelocity() const
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid physics object");
|
||||
|
||||
@@ -137,7 +194,37 @@ namespace Ndk
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid physics object");
|
||||
|
||||
return m_object->GetCenterOfGravity(coordSys);
|
||||
return m_object->GetMassCenter(coordSys);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Gets the elasticity of a shape belonging to this physics object
|
||||
* \return Elasticity of the shape
|
||||
*
|
||||
* \param shapeIndex Shape index of the collider we're interested
|
||||
*
|
||||
* \remark Produces a NazaraAssert if the physics object is invalid
|
||||
*/
|
||||
inline float PhysicsComponent2D::GetElasticity(std::size_t shapeIndex) const
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid physics object");
|
||||
|
||||
return m_object->GetElasticity(shapeIndex);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Gets the friction of a shape belonging to this physics object
|
||||
* \return Friction of the shape
|
||||
*
|
||||
* \param shapeIndex Shape index of the collider we're interested
|
||||
*
|
||||
* \remark Produces a NazaraAssert if the physics object is invalid
|
||||
*/
|
||||
inline float PhysicsComponent2D::GetFriction(std::size_t shapeIndex) const
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid physics object");
|
||||
|
||||
return m_object->GetFriction(shapeIndex);
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -146,7 +233,6 @@ namespace Ndk
|
||||
*
|
||||
* \remark Produces a NazaraAssert if the physics object is invalid
|
||||
*/
|
||||
|
||||
inline float PhysicsComponent2D::GetMass() const
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid physics object");
|
||||
@@ -154,6 +240,38 @@ namespace Ndk
|
||||
return m_object->GetMass();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Gets the gravity center of the physics object
|
||||
* \return Gravity center of the object
|
||||
*
|
||||
* \param coordSys System coordinates to consider
|
||||
*
|
||||
* \remark Produces a NazaraAssert if the physics object is invalid
|
||||
*/
|
||||
|
||||
inline Nz::Vector2f PhysicsComponent2D::GetMassCenter(Nz::CoordSys coordSys) const
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid physics object");
|
||||
|
||||
return m_object->GetMassCenter(coordSys);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Gets the angular damping or moment of inertia of the physics object
|
||||
* \return Moment of inertia of the object
|
||||
*
|
||||
* \remark Produces a NazaraAssert if the physics object is invalid
|
||||
*
|
||||
* \see GetAngularDamping
|
||||
*/
|
||||
|
||||
inline float PhysicsComponent2D::GetMomentOfInertia() const
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid physics object");
|
||||
|
||||
return m_object->GetMomentOfInertia();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Gets the position of the physics object
|
||||
* \return Position of the object
|
||||
@@ -174,14 +292,37 @@ namespace Ndk
|
||||
*
|
||||
* \remark Produces a NazaraAssert if the physics object is invalid
|
||||
*/
|
||||
|
||||
inline float PhysicsComponent2D::GetRotation() const
|
||||
inline Nz::RadianAnglef PhysicsComponent2D::GetRotation() const
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid physics object");
|
||||
|
||||
return m_object->GetRotation();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Gets the surface velocity of a shape belonging to this physics object
|
||||
* \return Surface velocity of the shape
|
||||
*
|
||||
* \param shapeIndex Shape index of the collider we're interested
|
||||
*
|
||||
* \remark Produces a NazaraAssert if the physics object is invalid
|
||||
*/
|
||||
inline Nz::Vector2f PhysicsComponent2D::GetSurfaceVelocity(std::size_t shapeIndex) const
|
||||
{
|
||||
return m_object->GetSurfaceVelocity(shapeIndex);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Gets the rotation of the physics object
|
||||
* \return Shape count of the object
|
||||
*
|
||||
* \remark Produces a NazaraAssert if the physics object is invalid
|
||||
*/
|
||||
inline std::size_t PhysicsComponent2D::GetShapeCount() const
|
||||
{
|
||||
return m_object->GetShapeCount();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Gets the velocity of the physics object
|
||||
* \return Velocity of the object
|
||||
@@ -196,13 +337,23 @@ namespace Ndk
|
||||
return m_object->GetVelocity();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Checks if position & rotation are synchronized with NodeComponent
|
||||
* \return true If synchronization is enabled
|
||||
*
|
||||
* \see EnableNodeSynchronization
|
||||
*/
|
||||
inline bool PhysicsComponent2D::IsNodeSynchronizationEnabled() const
|
||||
{
|
||||
return m_nodeSynchronizationEnabled;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Checks whether the entity is currently sleeping
|
||||
* \return true If it is the case
|
||||
*
|
||||
* \remark Produces a NazaraAssert if the physics object is invalid
|
||||
*/
|
||||
|
||||
inline bool PhysicsComponent2D::IsSleeping() const
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid physics object");
|
||||
@@ -210,6 +361,23 @@ namespace Ndk
|
||||
return m_object->IsSleeping();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Sets the angular damping or moment of inertia of the physics object
|
||||
*
|
||||
* \param angularDamping Angular damping of the object
|
||||
*
|
||||
* \remark Produces a NazaraAssert if the physics object is invalid
|
||||
*
|
||||
* \see SetMomentOfInertia
|
||||
*/
|
||||
|
||||
inline void PhysicsComponent2D::SetAngularDamping(float angularDamping)
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid physics object");
|
||||
|
||||
m_object->SetAngularDamping(angularDamping);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Sets the angular velocity of the physics object
|
||||
*
|
||||
@@ -217,29 +385,93 @@ namespace Ndk
|
||||
*
|
||||
* \remark Produces a NazaraAssert if the physics object is invalid
|
||||
*/
|
||||
|
||||
inline void PhysicsComponent2D::SetAngularVelocity(float angularVelocity)
|
||||
inline void PhysicsComponent2D::SetAngularVelocity(const Nz::RadianAnglef& angularVelocity)
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid physics object");
|
||||
|
||||
m_object->SetAngularVelocity(angularVelocity);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Sets the elasticity of the whole physics object
|
||||
*
|
||||
* Overrides all shapes elasticity with a single value
|
||||
*
|
||||
* \param elasticity Elasticity to be applied
|
||||
*
|
||||
* \remark Elasticity must be positive or zero
|
||||
*/
|
||||
inline void PhysicsComponent2D::SetElasticity(float elasticity)
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid physics object");
|
||||
NazaraAssert(elasticity >= 0.f, "Friction must be positive");
|
||||
|
||||
m_object->SetElasticity(elasticity);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Sets the elasticity of a single shape of the physics object
|
||||
*
|
||||
* \param shapeIndex Target shape index
|
||||
* \param elasticity Elasticity to be applied
|
||||
*
|
||||
* \remark Elasticity must be positive or zero
|
||||
*/
|
||||
inline void PhysicsComponent2D::SetElasticity(std::size_t shapeIndex, float elasticity)
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid physics object");
|
||||
NazaraAssert(elasticity >= 0.f, "Friction must be positive");
|
||||
|
||||
m_object->SetElasticity(shapeIndex, elasticity);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Sets the friction of the whole physics object
|
||||
*
|
||||
* Overrides all shapes friction with a single value
|
||||
*
|
||||
* \param friction Friction to be applied
|
||||
*
|
||||
* \remark Friction must be positive or zero
|
||||
*/
|
||||
inline void PhysicsComponent2D::SetFriction(float friction)
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid physics object");
|
||||
NazaraAssert(friction >= 0.f, "Friction must be positive");
|
||||
|
||||
m_object->SetFriction(friction);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Sets the friction of a single shape of the physics object
|
||||
*
|
||||
* \param shapeIndex Target shape index
|
||||
* \param friction Friction to be applied
|
||||
*
|
||||
* \remark Friction must be positive or zero
|
||||
*/
|
||||
inline void PhysicsComponent2D::SetFriction(std::size_t shapeIndex, float friction)
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid physics object");
|
||||
NazaraAssert(friction >= 0.f, "Friction must be positive");
|
||||
|
||||
m_object->SetFriction(shapeIndex, friction);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Sets the mass of the physics object
|
||||
*
|
||||
* \param mass Mass of the object
|
||||
* \param recomputeMoment Should the moment of inertia be recomputed according to the new mass
|
||||
*
|
||||
* \remark Produces a NazaraAssert if the physics object is invalid
|
||||
* \remark Produces a NazaraAssert if the mass is negative
|
||||
* \remark Mass must be positive or zero
|
||||
*/
|
||||
|
||||
inline void PhysicsComponent2D::SetMass(float mass)
|
||||
inline void PhysicsComponent2D::SetMass(float mass, bool recomputeMoment)
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid physics object");
|
||||
NazaraAssert(mass > 0.f, "Mass should be positive");
|
||||
NazaraAssert(mass >= 0.f, "Mass should be positive");
|
||||
|
||||
m_object->SetMass(mass);
|
||||
m_object->SetMass(mass, recomputeMoment);
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -249,12 +481,27 @@ namespace Ndk
|
||||
*
|
||||
* \remark Produces a NazaraAssert if the physics object is invalid
|
||||
*/
|
||||
|
||||
inline void PhysicsComponent2D::SetMassCenter(const Nz::Vector2f& center)
|
||||
inline void PhysicsComponent2D::SetMassCenter(const Nz::Vector2f& center, Nz::CoordSys coordSys)
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid physics object");
|
||||
|
||||
m_object->SetMassCenter(center);
|
||||
m_object->SetMassCenter(center, coordSys);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Sets the angular damping or moment of inertia of the physics object
|
||||
*
|
||||
* \param moment Moment of inertia of the object
|
||||
*
|
||||
* \remark Produces a NazaraAssert if the physics object is invalid
|
||||
*
|
||||
* \see SetAngularDamping
|
||||
*/
|
||||
inline void PhysicsComponent2D::SetMomentOfInertia(float moment)
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid physics object");
|
||||
|
||||
m_object->SetMomentOfInertia(moment);
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -264,7 +511,6 @@ namespace Ndk
|
||||
*
|
||||
* \remark Produces a NazaraAssert if the physics object is invalid
|
||||
*/
|
||||
|
||||
inline void PhysicsComponent2D::SetPosition(const Nz::Vector2f& position)
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid physics object");
|
||||
@@ -279,22 +525,45 @@ namespace Ndk
|
||||
*
|
||||
* \remark Produces a NazaraAssert if the physics object is invalid
|
||||
*/
|
||||
|
||||
inline void PhysicsComponent2D::SetRotation(float rotation)
|
||||
inline void PhysicsComponent2D::SetRotation(const Nz::RadianAnglef& rotation)
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid physics object");
|
||||
|
||||
m_object->SetRotation(rotation);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Sets the surface velocity of the whole physics object
|
||||
*
|
||||
* Overrides all shapes surface velocity with a single value
|
||||
*
|
||||
* \param velocity Surface velocity to be applied
|
||||
*/
|
||||
inline void PhysicsComponent2D::SetSurfaceVelocity(const Nz::Vector2f& velocity)
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid physics object");
|
||||
|
||||
m_object->SetSurfaceVelocity(velocity);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Sets the surface velocity of a single shape of the physics object
|
||||
*
|
||||
* \param shapeIndex Target shape index
|
||||
* \param velocity Surface velocity to be applied
|
||||
*/
|
||||
inline void PhysicsComponent2D::SetSurfaceVelocity(std::size_t shapeIndex, const Nz::Vector2f& velocity)
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid physics object");
|
||||
|
||||
m_object->SetSurfaceVelocity(shapeIndex, velocity);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Sets the velocity of the physics object
|
||||
*
|
||||
* \param velocity Velocity of the object
|
||||
*
|
||||
* \remark Produces a NazaraAssert if the physics object is invalid
|
||||
*/
|
||||
|
||||
inline void PhysicsComponent2D::SetVelocity(const Nz::Vector2f& velocity)
|
||||
{
|
||||
NazaraAssert(m_object, "Invalid physics object");
|
||||
@@ -306,7 +575,6 @@ namespace Ndk
|
||||
* \brief Gets the underlying physics object
|
||||
* \return A reference to the physics object
|
||||
*/
|
||||
|
||||
inline Nz::RigidBody2D* PhysicsComponent2D::GetRigidBody()
|
||||
{
|
||||
return m_object.get();
|
||||
|
||||
@@ -13,6 +13,10 @@
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
class PhysicsComponent3D;
|
||||
|
||||
using PhysicsComponent3DHandle = Nz::ObjectHandle<PhysicsComponent3D>;
|
||||
|
||||
class NDK_API PhysicsComponent3D : public Component<PhysicsComponent3D>
|
||||
{
|
||||
friend class CollisionComponent3D;
|
||||
@@ -23,49 +27,49 @@ namespace Ndk
|
||||
PhysicsComponent3D(const PhysicsComponent3D& physics);
|
||||
~PhysicsComponent3D() = default;
|
||||
|
||||
void AddForce(const Nz::Vector3f& force, Nz::CoordSys coordSys = Nz::CoordSys_Global);
|
||||
void AddForce(const Nz::Vector3f& force, const Nz::Vector3f& point, Nz::CoordSys coordSys = Nz::CoordSys_Global);
|
||||
void AddTorque(const Nz::Vector3f& torque, Nz::CoordSys coordSys = Nz::CoordSys_Global);
|
||||
inline void AddForce(const Nz::Vector3f& force, Nz::CoordSys coordSys = Nz::CoordSys_Global);
|
||||
inline void AddForce(const Nz::Vector3f& force, const Nz::Vector3f& point, Nz::CoordSys coordSys = Nz::CoordSys_Global);
|
||||
inline void AddTorque(const Nz::Vector3f& torque, Nz::CoordSys coordSys = Nz::CoordSys_Global);
|
||||
|
||||
void EnableAutoSleep(bool autoSleep);
|
||||
void EnableNodeSynchronization(bool nodeSynchronization);
|
||||
inline void EnableAutoSleep(bool autoSleep);
|
||||
inline void EnableNodeSynchronization(bool nodeSynchronization);
|
||||
|
||||
Nz::Boxf GetAABB() const;
|
||||
Nz::Vector3f GetAngularDamping() const;
|
||||
Nz::Vector3f GetAngularVelocity() const;
|
||||
float GetGravityFactor() const;
|
||||
float GetLinearDamping() const;
|
||||
Nz::Vector3f GetLinearVelocity() const;
|
||||
float GetMass() const;
|
||||
Nz::Vector3f GetMassCenter(Nz::CoordSys coordSys = Nz::CoordSys_Local) const;
|
||||
const Nz::Matrix4f& GetMatrix() const;
|
||||
Nz::Vector3f GetPosition() const;
|
||||
Nz::Quaternionf GetRotation() const;
|
||||
inline Nz::Boxf GetAABB() const;
|
||||
inline Nz::Vector3f GetAngularDamping() const;
|
||||
inline Nz::Vector3f GetAngularVelocity() const;
|
||||
inline float GetGravityFactor() const;
|
||||
inline float GetLinearDamping() const;
|
||||
inline Nz::Vector3f GetLinearVelocity() const;
|
||||
inline float GetMass() const;
|
||||
inline Nz::Vector3f GetMassCenter(Nz::CoordSys coordSys = Nz::CoordSys_Local) const;
|
||||
inline const Nz::Matrix4f& GetMatrix() const;
|
||||
inline Nz::Vector3f GetPosition() const;
|
||||
inline Nz::Quaternionf GetRotation() const;
|
||||
|
||||
bool IsAutoSleepEnabled() const;
|
||||
bool IsMoveable() const;
|
||||
bool IsNodeSynchronizationEnabled() const;
|
||||
bool IsSleeping() const;
|
||||
inline bool IsAutoSleepEnabled() const;
|
||||
inline bool IsMoveable() const;
|
||||
inline bool IsNodeSynchronizationEnabled() const;
|
||||
inline bool IsSleeping() const;
|
||||
|
||||
void SetAngularDamping(const Nz::Vector3f& angularDamping);
|
||||
void SetAngularVelocity(const Nz::Vector3f& angularVelocity);
|
||||
void SetGravityFactor(float gravityFactor);
|
||||
void SetLinearDamping(float damping);
|
||||
void SetLinearVelocity(const Nz::Vector3f& velocity);
|
||||
void SetMass(float mass);
|
||||
void SetMassCenter(const Nz::Vector3f& center);
|
||||
void SetMaterial(const Nz::String& materialName);
|
||||
void SetMaterial(int materialIndex);
|
||||
void SetPosition(const Nz::Vector3f& position);
|
||||
void SetRotation(const Nz::Quaternionf& rotation);
|
||||
inline void SetAngularDamping(const Nz::Vector3f& angularDamping);
|
||||
inline void SetAngularVelocity(const Nz::Vector3f& angularVelocity);
|
||||
inline void SetGravityFactor(float gravityFactor);
|
||||
inline void SetLinearDamping(float damping);
|
||||
inline void SetLinearVelocity(const Nz::Vector3f& velocity);
|
||||
inline void SetMass(float mass);
|
||||
inline void SetMassCenter(const Nz::Vector3f& center);
|
||||
inline void SetMaterial(const Nz::String& materialName);
|
||||
inline void SetMaterial(int materialIndex);
|
||||
inline void SetPosition(const Nz::Vector3f& position);
|
||||
inline void SetRotation(const Nz::Quaternionf& rotation);
|
||||
|
||||
static ComponentIndex componentIndex;
|
||||
|
||||
private:
|
||||
void ApplyPhysicsState(Nz::RigidBody3D& rigidBody) const;
|
||||
void CopyPhysicsState(const Nz::RigidBody3D& rigidBody);
|
||||
Nz::RigidBody3D* GetRigidBody();
|
||||
const Nz::RigidBody3D& GetRigidBody() const;
|
||||
inline void ApplyPhysicsState(Nz::RigidBody3D& rigidBody) const;
|
||||
inline void CopyPhysicsState(const Nz::RigidBody3D& rigidBody);
|
||||
inline Nz::RigidBody3D* GetRigidBody();
|
||||
inline const Nz::RigidBody3D& GetRigidBody() const;
|
||||
|
||||
void OnAttached() override;
|
||||
void OnComponentAttached(BaseComponent& component) override;
|
||||
|
||||
@@ -16,7 +16,7 @@ namespace Ndk
|
||||
|
||||
using VelocityComponentHandle = Nz::ObjectHandle<VelocityComponent>;
|
||||
|
||||
class NDK_API VelocityComponent : public Component<VelocityComponent>, public Nz::HandledObject<VelocityComponent>
|
||||
class NDK_API VelocityComponent : public Component<VelocityComponent>
|
||||
{
|
||||
public:
|
||||
VelocityComponent(const Nz::Vector3f& velocity = Nz::Vector3f::Zero());
|
||||
|
||||
@@ -8,7 +8,8 @@
|
||||
#define NDK_ENTITY_HPP
|
||||
|
||||
#include <Nazara/Core/Bitset.hpp>
|
||||
#include <Nazara/Core/HandledObject.hpp>
|
||||
#include <Nazara/Core/MovablePtr.hpp>
|
||||
#include <Nazara/Core/ObjectHandle.hpp>
|
||||
#include <Nazara/Core/Signal.hpp>
|
||||
#include <NDK/Algorithm.hpp>
|
||||
#include <NDK/Prerequisites.hpp>
|
||||
@@ -33,7 +34,7 @@ namespace Ndk
|
||||
|
||||
public:
|
||||
Entity(const Entity&) = delete;
|
||||
Entity(Entity&& entity);
|
||||
Entity(Entity&& entity) noexcept;
|
||||
~Entity();
|
||||
|
||||
BaseComponent& AddComponent(std::unique_ptr<BaseComponent>&& component);
|
||||
@@ -60,6 +61,7 @@ namespace Ndk
|
||||
|
||||
void Invalidate();
|
||||
inline bool IsEnabled() const;
|
||||
bool IsDying() const;
|
||||
inline bool IsValid() const;
|
||||
|
||||
inline void RemoveAllComponents();
|
||||
@@ -96,8 +98,8 @@ namespace Ndk
|
||||
Nz::Bitset<> m_componentBits;
|
||||
Nz::Bitset<> m_removedComponentBits;
|
||||
Nz::Bitset<> m_systemBits;
|
||||
Nz::MovablePtr<World> m_world;
|
||||
EntityId m_id;
|
||||
World* m_world;
|
||||
bool m_enabled;
|
||||
bool m_valid;
|
||||
};
|
||||
|
||||
@@ -15,17 +15,18 @@ namespace Ndk
|
||||
{
|
||||
public:
|
||||
EntityOwner() = default;
|
||||
explicit EntityOwner(Entity* entity);
|
||||
EntityOwner(Entity* entity);
|
||||
EntityOwner(const EntityOwner& handle) = delete;
|
||||
EntityOwner(EntityOwner&& handle) noexcept = default;
|
||||
~EntityOwner();
|
||||
|
||||
void Release();
|
||||
void Reset(Entity* entity = nullptr);
|
||||
void Reset(EntityOwner&& handle);
|
||||
|
||||
EntityOwner& operator=(Entity* entity);
|
||||
EntityOwner& operator=(const EntityOwner& handle) = delete;
|
||||
EntityOwner& operator=(EntityOwner&& handle) noexcept = default;
|
||||
EntityOwner& operator=(EntityOwner&& handle) noexcept;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// This file is part of the "Nazara Development Kit"
|
||||
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
|
||||
|
||||
#include <NDK/EntityOwner.hpp>
|
||||
#include <Nazara/Core/StringStream.hpp>
|
||||
#include <functional>
|
||||
#include <limits>
|
||||
@@ -31,22 +32,28 @@ namespace Ndk
|
||||
*
|
||||
* \see Reset
|
||||
*/
|
||||
|
||||
inline EntityOwner::~EntityOwner()
|
||||
{
|
||||
Reset(nullptr);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Release the ownership of the entity without killing it
|
||||
*/
|
||||
inline void EntityOwner::Release()
|
||||
{
|
||||
EntityHandle::Reset(nullptr);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Resets the ownership of the entity, previous is killed
|
||||
*
|
||||
* \param entity Entity to own
|
||||
*/
|
||||
|
||||
inline void EntityOwner::Reset(Entity* entity)
|
||||
{
|
||||
if (m_object)
|
||||
m_object->Kill();
|
||||
if (IsValid())
|
||||
GetObject()->Kill();
|
||||
|
||||
EntityHandle::Reset(entity);
|
||||
}
|
||||
@@ -56,11 +63,10 @@ namespace Ndk
|
||||
*
|
||||
* \param handle EntityOwner to move into this
|
||||
*/
|
||||
|
||||
inline void EntityOwner::Reset(EntityOwner&& handle)
|
||||
{
|
||||
Reset(handle.GetObject());
|
||||
handle.m_object = nullptr;
|
||||
handle.Release();
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -68,13 +74,26 @@ namespace Ndk
|
||||
*
|
||||
* \param entity Entity to own
|
||||
*/
|
||||
|
||||
inline EntityOwner& EntityOwner::operator=(Entity* entity)
|
||||
{
|
||||
Reset(entity);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Steals ownership of a EntityOwner
|
||||
*
|
||||
* \param handle Handle to the new entity to own, or an invalid handle
|
||||
*/
|
||||
inline EntityOwner& EntityOwner::operator=(EntityOwner&& handle) noexcept
|
||||
{
|
||||
Reset(); //< Kill previously owned entity, if any
|
||||
|
||||
EntityHandle::operator=(std::move(handle));
|
||||
|
||||
return *this;
|
||||
}
|
||||
}
|
||||
|
||||
namespace std
|
||||
|
||||
@@ -269,6 +269,15 @@ namespace Nz
|
||||
return ret;
|
||||
}
|
||||
|
||||
inline unsigned int LuaImplQueryArg(const LuaState& state, int index, Vector2i* vec, TypeTag<Vector2i>)
|
||||
{
|
||||
Vector2d vecDouble;
|
||||
unsigned int ret = LuaImplQueryArg(state, index, &vecDouble, TypeTag<Vector2d>());
|
||||
|
||||
vec->Set(vecDouble);
|
||||
return ret;
|
||||
}
|
||||
|
||||
inline unsigned int LuaImplQueryArg(const LuaState& state, int index, Vector3d* vec, TypeTag<Vector3d>)
|
||||
{
|
||||
switch (state.GetType(index))
|
||||
@@ -308,6 +317,15 @@ namespace Nz
|
||||
return ret;
|
||||
}
|
||||
|
||||
inline unsigned int LuaImplQueryArg(const LuaState& state, int index, Vector3i* vec, TypeTag<Vector3i>)
|
||||
{
|
||||
Vector3d vecDouble;
|
||||
unsigned int ret = LuaImplQueryArg(state, index, &vecDouble, TypeTag<Vector3d>());
|
||||
|
||||
vec->Set(vecDouble);
|
||||
return ret;
|
||||
}
|
||||
|
||||
inline unsigned int LuaImplQueryArg(const LuaState& state, int index, Ndk::Entity** handle, TypeTag<Ndk::Entity*>)
|
||||
{
|
||||
if (!state.IsOfType(index, LuaType_Nil))
|
||||
@@ -384,7 +402,7 @@ namespace Nz
|
||||
return 1;
|
||||
}
|
||||
|
||||
inline unsigned int LuaImplQueryArg(const LuaState& state, int index, MusicParams* params, TypeTag<MusicParams>)
|
||||
inline unsigned int LuaImplQueryArg(const LuaState& state, int index, SoundBufferParams* params, TypeTag<SoundBufferParams>)
|
||||
{
|
||||
state.CheckType(index, Nz::LuaType_Table);
|
||||
|
||||
@@ -393,7 +411,7 @@ namespace Nz
|
||||
return 1;
|
||||
}
|
||||
|
||||
inline unsigned int LuaImplQueryArg(const LuaState& state, int index, SoundBufferParams* params, TypeTag<SoundBufferParams>)
|
||||
inline unsigned int LuaImplQueryArg(const LuaState& state, int index, SoundStreamParams* params, TypeTag<SoundStreamParams>)
|
||||
{
|
||||
state.CheckType(index, Nz::LuaType_Table);
|
||||
|
||||
@@ -538,6 +556,12 @@ namespace Nz
|
||||
return 1;
|
||||
}
|
||||
|
||||
inline int LuaImplReplyVal(const LuaState& state, Vector2i&& val, TypeTag<Vector2i>)
|
||||
{
|
||||
state.PushInstance<Vector2d>("Vector2", val);
|
||||
return 1;
|
||||
}
|
||||
|
||||
inline int LuaImplReplyVal(const LuaState& state, Vector3d&& val, TypeTag<Vector3d>)
|
||||
{
|
||||
state.PushInstance<Vector3d>("Vector3", val);
|
||||
@@ -556,6 +580,12 @@ namespace Nz
|
||||
return 1;
|
||||
}
|
||||
|
||||
inline int LuaImplReplyVal(const LuaState& state, Vector3i&& val, TypeTag<Vector3i>)
|
||||
{
|
||||
state.PushInstance<Vector3d>("Vector3", val);
|
||||
return 1;
|
||||
}
|
||||
|
||||
inline int LuaImplReplyVal(const LuaState& state, Ndk::Entity* ptr, TypeTag<Ndk::Entity*>)
|
||||
{
|
||||
state.PushInstance<Ndk::EntityHandle>("Entity", ptr);
|
||||
@@ -605,12 +635,24 @@ namespace Nz
|
||||
return 1;
|
||||
}
|
||||
|
||||
inline int LuaImplReplyVal(const LuaState& state, ModelRef&& handle, TypeTag<ModelRef>)
|
||||
{
|
||||
state.PushInstance<ModelRef>("Model", handle);
|
||||
return 1;
|
||||
}
|
||||
|
||||
inline int LuaImplReplyVal(const LuaState& state, const SoundBuffer* val, TypeTag<const SoundBuffer*>)
|
||||
{
|
||||
state.PushInstance<SoundBufferConstRef>("SoundBuffer", val);
|
||||
return 1;
|
||||
}
|
||||
|
||||
inline int LuaImplReplyVal(const LuaState& state, SoundBufferRef&& handle, TypeTag<SoundBufferRef>)
|
||||
{
|
||||
state.PushInstance<SoundBufferRef>("SoundBuffer", handle);
|
||||
return 1;
|
||||
}
|
||||
|
||||
inline int LuaImplReplyVal(const LuaState& state, SpriteRef&& handle, TypeTag<SpriteRef>)
|
||||
{
|
||||
state.PushInstance<SpriteRef>("Sprite", handle);
|
||||
|
||||
@@ -17,11 +17,11 @@ namespace Ndk
|
||||
public:
|
||||
System();
|
||||
System(const System&) = delete;
|
||||
System(System&&) = default;
|
||||
System(System&&) noexcept = default;
|
||||
virtual ~System();
|
||||
|
||||
System& operator=(const System&) = delete;
|
||||
System& operator=(System&&) = default;
|
||||
System& operator=(System&&) noexcept = default;
|
||||
|
||||
static SystemIndex RegisterSystem();
|
||||
};
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#ifndef NDK_SYSTEMS_GLOBAL_HPP
|
||||
#define NDK_SYSTEMS_GLOBAL_HPP
|
||||
|
||||
#include <NDK/Systems/DebugSystem.hpp>
|
||||
#include <NDK/Systems/ListenerSystem.hpp>
|
||||
#include <NDK/Systems/ParticleSystem.hpp>
|
||||
#include <NDK/Systems/PhysicsSystem2D.hpp>
|
||||
|
||||
53
SDK/include/NDK/Systems/DebugSystem.hpp
Normal file
53
SDK/include/NDK/Systems/DebugSystem.hpp
Normal file
@@ -0,0 +1,53 @@
|
||||
// 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
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef NDK_SERVER
|
||||
#ifndef NDK_SYSTEMS_DEBUGSYSTEM_HPP
|
||||
#define NDK_SYSTEMS_DEBUGSYSTEM_HPP
|
||||
|
||||
#include <Nazara/Graphics/InstancedRenderable.hpp>
|
||||
#include <Nazara/Utility/IndexBuffer.hpp>
|
||||
#include <Nazara/Utility/Mesh.hpp>
|
||||
#include <Nazara/Utility/VertexBuffer.hpp>
|
||||
#include <NDK/System.hpp>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
class NDK_API DebugSystem : public System<DebugSystem>
|
||||
{
|
||||
public:
|
||||
DebugSystem();
|
||||
~DebugSystem() = default;
|
||||
|
||||
static SystemIndex systemIndex;
|
||||
|
||||
private:
|
||||
Nz::InstancedRenderableRef GenerateBox(Nz::Boxf box);
|
||||
Nz::InstancedRenderableRef GenerateCollision3DMesh(Entity* entity);
|
||||
|
||||
Nz::MaterialRef GetCollisionMaterial();
|
||||
Nz::MaterialRef GetGlobalAABBMaterial();
|
||||
Nz::MaterialRef GetLocalAABBMaterial();
|
||||
Nz::MaterialRef GetOBBMaterial();
|
||||
std::pair<Nz::IndexBufferRef, Nz::VertexBufferRef> GetBoxMesh();
|
||||
|
||||
void OnEntityValidation(Entity* entity, bool justAdded) override;
|
||||
|
||||
void OnUpdate(float elapsedTime) override;
|
||||
|
||||
Nz::MaterialRef m_globalAabbMaterial;
|
||||
Nz::MaterialRef m_localAabbMaterial;
|
||||
Nz::MaterialRef m_collisionMaterial;
|
||||
Nz::MaterialRef m_obbMaterial;
|
||||
Nz::IndexBufferRef m_boxMeshIndexBuffer;
|
||||
Nz::VertexBufferRef m_boxMeshVertexBuffer;
|
||||
};
|
||||
}
|
||||
|
||||
#include <NDK/Systems/DebugSystem.inl>
|
||||
|
||||
#endif // NDK_SYSTEMS_DEBUGSYSTEM_HPP
|
||||
#endif // NDK_SERVER
|
||||
6
SDK/include/NDK/Systems/DebugSystem.inl
Normal file
6
SDK/include/NDK/Systems/DebugSystem.inl
Normal file
@@ -0,0 +1,6 @@
|
||||
// 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 <NDK/Systems/DebugSystem.hpp>
|
||||
|
||||
@@ -16,24 +16,111 @@ namespace Ndk
|
||||
{
|
||||
class NDK_API PhysicsSystem2D : public System<PhysicsSystem2D>
|
||||
{
|
||||
friend class CollisionComponent2D;
|
||||
friend class PhysicsComponent2D;
|
||||
|
||||
using ContactEndCallback = std::function<void(PhysicsSystem2D& world, Nz::Arbiter2D& arbiter, const EntityHandle& bodyA, const EntityHandle& bodyB, void* userdata)>;
|
||||
using ContactPreSolveCallback = std::function<bool(PhysicsSystem2D& world, Nz::Arbiter2D& arbiter, const EntityHandle& bodyA, const EntityHandle& bodyB, void* userdata)>;
|
||||
using ContactPostSolveCallback = std::function<void(PhysicsSystem2D& world, Nz::Arbiter2D& arbiter, const EntityHandle& bodyA, const EntityHandle& bodyB, void* userdata)>;
|
||||
using ContactStartCallback = std::function<bool(PhysicsSystem2D& world, Nz::Arbiter2D& arbiter, const EntityHandle& bodyA, const EntityHandle& bodyB, void* userdata)>;
|
||||
|
||||
using DebugDrawCircleCallback = std::function<void(const Nz::Vector2f& origin, const Nz::RadianAnglef& rotation, float radius, Nz::Color outlineColor, Nz::Color fillColor, void* userdata)>;
|
||||
using DebugDrawDotCallback = std::function<void(const Nz::Vector2f& origin, float radius, Nz::Color color, void* userdata)>;
|
||||
using DebugDrawPolygonCallback = std::function<void(const Nz::Vector2f* vertices, std::size_t vertexCount, float radius, Nz::Color outlineColor, Nz::Color fillColor, void* userdata)>;
|
||||
using DebugDrawSegmentCallback = std::function<void(const Nz::Vector2f& first, const Nz::Vector2f& second, Nz::Color color, void* userdata)>;
|
||||
using DebugDrawTickSegmentCallback = std::function<void(const Nz::Vector2f& first, const Nz::Vector2f& second, float thickness, Nz::Color outlineColor, Nz::Color fillColor, void* userdata)>;
|
||||
using DebugDrawGetColorCallback = std::function<Nz::Color(const EntityHandle& body, std::size_t shapeIndex, void* userdata)>;
|
||||
|
||||
public:
|
||||
struct Callback;
|
||||
struct DebugDrawOptions;
|
||||
struct NearestQueryResult;
|
||||
struct RaycastHit;
|
||||
|
||||
PhysicsSystem2D();
|
||||
PhysicsSystem2D(const PhysicsSystem2D& system);
|
||||
~PhysicsSystem2D() = default;
|
||||
|
||||
Nz::PhysWorld2D& GetWorld();
|
||||
const Nz::PhysWorld2D& GetWorld() const;
|
||||
void DebugDraw(const DebugDrawOptions& options, bool drawShapes = true, bool drawConstraints = true, bool drawCollisions = true);
|
||||
|
||||
inline float GetDamping() const;
|
||||
inline Nz::Vector2f GetGravity() const;
|
||||
inline std::size_t GetIterationCount() const;
|
||||
inline std::size_t GetMaxStepCount() const;
|
||||
inline float GetStepSize() const;
|
||||
|
||||
bool NearestBodyQuery(const Nz::Vector2f& from, float maxDistance, Nz::UInt32 collisionGroup, Nz::UInt32 categoryMask, Nz::UInt32 collisionMask, EntityHandle* nearestBody = nullptr);
|
||||
bool NearestBodyQuery(const Nz::Vector2f& from, float maxDistance, Nz::UInt32 collisionGroup, Nz::UInt32 categoryMask, Nz::UInt32 collisionMask, NearestQueryResult* result);
|
||||
|
||||
bool RaycastQuery(const Nz::Vector2f& from, const Nz::Vector2f& to, float radius, Nz::UInt32 collisionGroup, Nz::UInt32 categoryMask, Nz::UInt32 collisionMask, std::vector<RaycastHit>* hitInfos);
|
||||
bool RaycastQueryFirst(const Nz::Vector2f& from, const Nz::Vector2f& to, float radius, Nz::UInt32 collisionGroup, Nz::UInt32 categoryMask, Nz::UInt32 collisionMask, RaycastHit* hitInfo = nullptr);
|
||||
|
||||
void RegionQuery(const Nz::Rectf& boundingBox, Nz::UInt32 collisionGroup, Nz::UInt32 categoryMask, Nz::UInt32 collisionMask, std::vector<EntityHandle>* bodies);
|
||||
|
||||
void RegisterCallbacks(unsigned int collisionId, Callback callbacks);
|
||||
void RegisterCallbacks(unsigned int collisionIdA, unsigned int collisionIdB, Callback callbacks);
|
||||
|
||||
inline void SetDamping(float dampingValue);
|
||||
inline void SetGravity(const Nz::Vector2f& gravity);
|
||||
inline void SetIterationCount(std::size_t iterationCount);
|
||||
inline void SetMaxStepCount(std::size_t maxStepCount);
|
||||
inline void SetStepSize(float stepSize);
|
||||
|
||||
inline void UseSpatialHash(float cellSize, std::size_t entityCount);
|
||||
|
||||
struct Callback
|
||||
{
|
||||
ContactEndCallback endCallback = nullptr;
|
||||
ContactPreSolveCallback preSolveCallback = nullptr;
|
||||
ContactPostSolveCallback postSolveCallback = nullptr;
|
||||
ContactStartCallback startCallback = nullptr;
|
||||
void* userdata;
|
||||
};
|
||||
|
||||
struct DebugDrawOptions
|
||||
{
|
||||
Nz::Color constraintColor;
|
||||
Nz::Color collisionPointColor;
|
||||
Nz::Color shapeOutlineColor;
|
||||
|
||||
DebugDrawCircleCallback circleCallback;
|
||||
DebugDrawGetColorCallback colorCallback;
|
||||
DebugDrawDotCallback dotCallback;
|
||||
DebugDrawPolygonCallback polygonCallback;
|
||||
DebugDrawSegmentCallback segmentCallback;
|
||||
DebugDrawTickSegmentCallback thickSegmentCallback;
|
||||
|
||||
void* userdata;
|
||||
};
|
||||
|
||||
struct NearestQueryResult
|
||||
{
|
||||
EntityHandle nearestBody;
|
||||
Nz::Vector2f closestPoint;
|
||||
Nz::Vector2f fraction;
|
||||
float distance;
|
||||
};
|
||||
|
||||
struct RaycastHit
|
||||
{
|
||||
EntityHandle body;
|
||||
Nz::Vector2f hitPos;
|
||||
Nz::Vector2f hitNormal;
|
||||
float fraction;
|
||||
};
|
||||
|
||||
static SystemIndex systemIndex;
|
||||
|
||||
private:
|
||||
void CreatePhysWorld() const;
|
||||
const EntityHandle& GetEntityFromBody(const Nz::RigidBody2D& body) const;
|
||||
inline Nz::PhysWorld2D& GetPhysWorld();
|
||||
inline const Nz::PhysWorld2D& GetPhysWorld() const;
|
||||
void OnEntityValidation(Entity* entity, bool justAdded) override;
|
||||
void OnUpdate(float elapsedTime) override;
|
||||
|
||||
EntityList m_dynamicObjects;
|
||||
EntityList m_staticObjects;
|
||||
mutable std::unique_ptr<Nz::PhysWorld2D> m_world; ///TODO: std::optional (Should I make a Nz::Optional class?)
|
||||
mutable std::unique_ptr<Nz::PhysWorld2D> m_physWorld; ///TODO: std::optional (Should I make a Nz::Optional class?)
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -4,17 +4,72 @@
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
inline float PhysicsSystem2D::GetDamping() const
|
||||
{
|
||||
return GetPhysWorld().GetDamping();
|
||||
}
|
||||
|
||||
inline Nz::Vector2f PhysicsSystem2D::GetGravity() const
|
||||
{
|
||||
return GetPhysWorld().GetGravity();
|
||||
}
|
||||
|
||||
inline std::size_t PhysicsSystem2D::GetIterationCount() const
|
||||
{
|
||||
return GetPhysWorld().GetIterationCount();
|
||||
}
|
||||
|
||||
inline std::size_t PhysicsSystem2D::GetMaxStepCount() const
|
||||
{
|
||||
return GetPhysWorld().GetMaxStepCount();
|
||||
}
|
||||
|
||||
inline float PhysicsSystem2D::GetStepSize() const
|
||||
{
|
||||
return GetPhysWorld().GetStepSize();
|
||||
}
|
||||
|
||||
inline void PhysicsSystem2D::SetDamping(float dampingValue)
|
||||
{
|
||||
GetPhysWorld().SetDamping(dampingValue);
|
||||
}
|
||||
|
||||
inline void PhysicsSystem2D::SetGravity(const Nz::Vector2f& gravity)
|
||||
{
|
||||
GetPhysWorld().SetGravity(gravity);
|
||||
}
|
||||
|
||||
inline void PhysicsSystem2D::SetIterationCount(std::size_t iterationCount)
|
||||
{
|
||||
GetPhysWorld().SetIterationCount(iterationCount);
|
||||
}
|
||||
|
||||
inline void PhysicsSystem2D::SetMaxStepCount(std::size_t maxStepCount)
|
||||
{
|
||||
GetPhysWorld().SetMaxStepCount(maxStepCount);
|
||||
}
|
||||
|
||||
inline void PhysicsSystem2D::SetStepSize(float stepSize)
|
||||
{
|
||||
GetPhysWorld().SetStepSize(stepSize);
|
||||
}
|
||||
|
||||
inline void PhysicsSystem2D::UseSpatialHash(float cellSize, std::size_t entityCount)
|
||||
{
|
||||
GetPhysWorld().UseSpatialHash(cellSize, entityCount);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Gets the physical world
|
||||
* \return A reference to the physical world
|
||||
*/
|
||||
|
||||
inline Nz::PhysWorld2D& PhysicsSystem2D::GetWorld()
|
||||
inline Nz::PhysWorld2D& PhysicsSystem2D::GetPhysWorld()
|
||||
{
|
||||
if (!m_world)
|
||||
if (!m_physWorld)
|
||||
CreatePhysWorld();
|
||||
|
||||
return *m_world;
|
||||
return *m_physWorld;
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -22,11 +77,11 @@ namespace Ndk
|
||||
* \return A constant reference to the physical world
|
||||
*/
|
||||
|
||||
inline const Nz::PhysWorld2D& PhysicsSystem2D::GetWorld() const
|
||||
inline const Nz::PhysWorld2D& PhysicsSystem2D::GetPhysWorld() const
|
||||
{
|
||||
if (!m_world)
|
||||
if (!m_physWorld)
|
||||
CreatePhysWorld();
|
||||
|
||||
return *m_world;
|
||||
return *m_physWorld;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,12 +25,13 @@ namespace Ndk
|
||||
{
|
||||
public:
|
||||
RenderSystem();
|
||||
inline RenderSystem(const RenderSystem& renderSystem);
|
||||
~RenderSystem() = default;
|
||||
|
||||
template<typename T> T& ChangeRenderTechnique();
|
||||
inline Nz::AbstractRenderTechnique& ChangeRenderTechnique(std::unique_ptr<Nz::AbstractRenderTechnique>&& renderTechnique);
|
||||
|
||||
inline void EnableCulling(bool enable);
|
||||
|
||||
inline const Nz::BackgroundRef& GetDefaultBackground() const;
|
||||
inline const Nz::Matrix4f& GetCoordinateSystemMatrix() const;
|
||||
inline Nz::Vector3f GetGlobalForward() const;
|
||||
@@ -38,6 +39,8 @@ namespace Ndk
|
||||
inline Nz::Vector3f GetGlobalUp() const;
|
||||
inline Nz::AbstractRenderTechnique& GetRenderTechnique() const;
|
||||
|
||||
inline bool IsCullingEnabled() const;
|
||||
|
||||
inline void SetDefaultBackground(Nz::BackgroundRef background);
|
||||
inline void SetGlobalForward(const Nz::Vector3f& direction);
|
||||
inline void SetGlobalRight(const Nz::Vector3f& direction);
|
||||
@@ -72,6 +75,7 @@ namespace Ndk
|
||||
Nz::RenderTexture m_shadowRT;
|
||||
bool m_coordinateSystemInvalidated;
|
||||
bool m_forceRenderQueueInvalidation;
|
||||
bool m_isCullingEnabled;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
// This file is part of the "Nazara Development Kit"
|
||||
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
|
||||
|
||||
#include <NDK/Systems/RenderSystem.hpp>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
/*!
|
||||
@@ -26,7 +28,24 @@ namespace Ndk
|
||||
inline Nz::AbstractRenderTechnique& RenderSystem::ChangeRenderTechnique(std::unique_ptr<Nz::AbstractRenderTechnique>&& renderTechnique)
|
||||
{
|
||||
m_renderTechnique = std::move(renderTechnique);
|
||||
return *m_renderTechnique.get();
|
||||
return *m_renderTechnique;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Enables/disables object culling
|
||||
*
|
||||
* Object culling is an algorithm used by the render system to detect invisible objects (which will not appear on screen) before they are rendered.
|
||||
* This includes Frustum Culling and potentially Occlusion Culling.
|
||||
*
|
||||
* Disabling this is not recommended, as the system will draw every object in the world which could induce a performance loss.
|
||||
*
|
||||
* \param enable Whether to enable or disable culling
|
||||
*
|
||||
* \see IsCullingEnabled
|
||||
*/
|
||||
inline void RenderSystem::EnableCulling(bool enable)
|
||||
{
|
||||
m_isCullingEnabled = enable;
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -89,6 +108,17 @@ namespace Ndk
|
||||
return *m_renderTechnique.get();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Query if culling is enabled (enabled by default)
|
||||
* \return True if culling is enabled, false otherwise
|
||||
*
|
||||
* \see EnableCulling
|
||||
*/
|
||||
inline bool RenderSystem::IsCullingEnabled() const
|
||||
{
|
||||
return m_isCullingEnabled;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Sets the background used for rendering
|
||||
*
|
||||
|
||||
48
SDK/include/NDK/Widgets/BoxLayout.hpp
Normal file
48
SDK/include/NDK/Widgets/BoxLayout.hpp
Normal file
@@ -0,0 +1,48 @@
|
||||
// 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
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef NDK_WIDGETS_BOXLAYOUT_HPP
|
||||
#define NDK_WIDGETS_BOXLAYOUT_HPP
|
||||
|
||||
#include <NDK/Prerequisites.hpp>
|
||||
#include <NDK/BaseWidget.hpp>
|
||||
#include <NDK/Widgets/Enums.hpp>
|
||||
#include <vector>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
class NDK_API BoxLayout : public BaseWidget
|
||||
{
|
||||
public:
|
||||
inline BoxLayout(BaseWidget* parent, BoxLayoutOrientation orientation);
|
||||
BoxLayout(const BoxLayout&) = delete;
|
||||
BoxLayout(BoxLayout&&) = default;
|
||||
~BoxLayout() = default;
|
||||
|
||||
void Layout() override;
|
||||
|
||||
BoxLayout& operator=(const BoxLayout&) = delete;
|
||||
BoxLayout& operator=(BoxLayout&&) = default;
|
||||
|
||||
private:
|
||||
struct ChildInfo
|
||||
{
|
||||
BaseWidget* widget;
|
||||
bool isConstrained;
|
||||
float maximumSize;
|
||||
float minimumSize;
|
||||
float size;
|
||||
};
|
||||
|
||||
std::vector<ChildInfo> m_childInfos;
|
||||
BoxLayoutOrientation m_orientation;
|
||||
float m_spacing;
|
||||
};
|
||||
}
|
||||
|
||||
#include <NDK/Widgets/BoxLayout.inl>
|
||||
|
||||
#endif // NDK_WIDGETS_BOXLAYOUT_HPP
|
||||
15
SDK/include/NDK/Widgets/BoxLayout.inl
Normal file
15
SDK/include/NDK/Widgets/BoxLayout.inl
Normal file
@@ -0,0 +1,15 @@
|
||||
// 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 <NDK/Widgets/BoxLayout.hpp>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
BoxLayout::BoxLayout(BaseWidget* parent, BoxLayoutOrientation orientation) :
|
||||
BaseWidget(parent),
|
||||
m_orientation(orientation),
|
||||
m_spacing(5.f)
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -23,15 +23,11 @@ namespace Ndk
|
||||
class NDK_API ButtonWidget : public BaseWidget
|
||||
{
|
||||
public:
|
||||
ButtonWidget(BaseWidget* parent = nullptr);
|
||||
ButtonWidget(BaseWidget* parent);
|
||||
ButtonWidget(const ButtonWidget&) = delete;
|
||||
ButtonWidget(ButtonWidget&&) = default;
|
||||
~ButtonWidget() = default;
|
||||
|
||||
//virtual ButtonWidget* Clone() const = 0;
|
||||
|
||||
void ResizeToContent() override;
|
||||
|
||||
inline const Nz::Color& GetColor() const;
|
||||
inline const Nz::Color& GetCornerColor() const;
|
||||
inline const Nz::Color& GetHoverColor() const;
|
||||
|
||||
@@ -93,6 +93,10 @@ namespace Ndk
|
||||
{
|
||||
m_textSprite->Update(drawer);
|
||||
|
||||
Nz::Vector2f textSize = Nz::Vector2f(m_textSprite->GetBoundingVolume().obb.localBox.GetLengths());
|
||||
SetMinimumSize(textSize);
|
||||
SetPreferredSize(textSize + Nz::Vector2f(20.f, 10.f));
|
||||
|
||||
Layout();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// Copyright (C) 2017 Samy Bensaid
|
||||
// Copyright (C) 2017 Samy Bensaid
|
||||
// This file is part of the "Nazara Development Kit"
|
||||
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
|
||||
|
||||
@@ -28,7 +28,7 @@ namespace Ndk
|
||||
friend class Sdk;
|
||||
|
||||
public:
|
||||
CheckboxWidget(BaseWidget* parent = nullptr);
|
||||
CheckboxWidget(BaseWidget* parent);
|
||||
CheckboxWidget(const CheckboxWidget&) = delete;
|
||||
CheckboxWidget(CheckboxWidget&&) = default;
|
||||
~CheckboxWidget() = default;
|
||||
@@ -53,7 +53,6 @@ namespace Ndk
|
||||
void SetState(CheckboxState state);
|
||||
inline void SetTextMargin(float margin);
|
||||
|
||||
void ResizeToContent() override;
|
||||
inline void UpdateText(const Nz::AbstractTextDrawer& drawer);
|
||||
|
||||
|
||||
@@ -68,6 +67,7 @@ namespace Ndk
|
||||
|
||||
void Layout() override;
|
||||
void UpdateCheckbox();
|
||||
void UpdateSize();
|
||||
|
||||
void OnMouseButtonRelease(int x, int y, Nz::Mouse::Button button) override;
|
||||
inline bool ContainsCheckbox(int x, int y) const;
|
||||
|
||||
@@ -65,6 +65,7 @@ namespace Ndk
|
||||
m_checkboxBackgroundSprite->SetSize(size - GetCheckboxBorderSize() * 2.f);
|
||||
m_checkboxContentSprite->SetSize(GetCheckboxSize() - GetCheckboxBorderSize() * 2.f - Nz::Vector2f { 4.f, 4.f });
|
||||
|
||||
UpdateSize();
|
||||
Layout();
|
||||
}
|
||||
|
||||
@@ -77,6 +78,8 @@ namespace Ndk
|
||||
inline void CheckboxWidget::UpdateText(const Nz::AbstractTextDrawer& drawer)
|
||||
{
|
||||
m_textSprite->Update(drawer);
|
||||
|
||||
UpdateSize();
|
||||
Layout();
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,12 @@
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
enum BoxLayoutOrientation
|
||||
{
|
||||
BoxLayoutOrientation_Horizontal,
|
||||
BoxLayoutOrientation_Vertical
|
||||
};
|
||||
|
||||
enum CheckboxState
|
||||
{
|
||||
CheckboxState_Checked,
|
||||
|
||||
@@ -19,21 +19,19 @@ namespace Ndk
|
||||
class NDK_API ImageWidget : public BaseWidget
|
||||
{
|
||||
public:
|
||||
ImageWidget(BaseWidget* parent = nullptr);
|
||||
ImageWidget(BaseWidget* parent);
|
||||
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 SetTexture(const Nz::TextureRef& texture);
|
||||
inline void SetTextureCoords(const Nz::Rectf& coords);
|
||||
inline void SetTextureRect(const Nz::Rectui& rect);
|
||||
|
||||
|
||||
@@ -26,12 +26,13 @@ namespace Ndk
|
||||
m_sprite->SetColor(color);
|
||||
}
|
||||
|
||||
inline void ImageWidget::SetTexture(const Nz::TextureRef& texture, bool resizeToContent)
|
||||
inline void ImageWidget::SetTexture(const Nz::TextureRef& texture)
|
||||
{
|
||||
m_sprite->SetTexture(texture, false);
|
||||
|
||||
if (resizeToContent)
|
||||
ResizeToContent();
|
||||
Nz::Vector2f textureSize = Nz::Vector2f(Nz::Vector2ui(m_sprite->GetMaterial()->GetDiffuseMap()->GetSize()));
|
||||
SetMinimumSize(textureSize);
|
||||
SetPreferredSize(textureSize);
|
||||
}
|
||||
|
||||
inline void ImageWidget::SetTextureCoords(const Nz::Rectf& coords)
|
||||
|
||||
@@ -21,23 +21,17 @@ namespace Ndk
|
||||
class NDK_API LabelWidget : public BaseWidget
|
||||
{
|
||||
public:
|
||||
LabelWidget(BaseWidget* parent = nullptr);
|
||||
LabelWidget(BaseWidget* parent);
|
||||
LabelWidget(const LabelWidget&) = delete;
|
||||
LabelWidget(LabelWidget&&) = default;
|
||||
~LabelWidget() = default;
|
||||
|
||||
//virtual LabelWidget* Clone() const = 0;
|
||||
|
||||
void ResizeToContent() override;
|
||||
|
||||
inline void UpdateText(const Nz::AbstractTextDrawer& drawer);
|
||||
|
||||
LabelWidget& operator=(const LabelWidget&) = delete;
|
||||
LabelWidget& operator=(LabelWidget&&) = default;
|
||||
|
||||
private:
|
||||
void Layout() override;
|
||||
|
||||
EntityHandle m_textEntity;
|
||||
Nz::TextSpriteRef m_textSprite;
|
||||
};
|
||||
|
||||
@@ -9,5 +9,8 @@ namespace Ndk
|
||||
inline void LabelWidget::UpdateText(const Nz::AbstractTextDrawer& drawer)
|
||||
{
|
||||
m_textSprite->Update(drawer);
|
||||
|
||||
SetMinimumSize(Nz::Vector2f(m_textSprite->GetBoundingVolume().obb.localBox.GetLengths()));
|
||||
SetPreferredSize(Nz::Vector2f(m_textSprite->GetBoundingVolume().obb.localBox.GetLengths()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// Copyright (C) 2017 Samy Bensaid
|
||||
// Copyright (C) 2017 Samy Bensaid
|
||||
// This file is part of the "Nazara Development Kit"
|
||||
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
|
||||
|
||||
@@ -23,7 +23,7 @@ namespace Ndk
|
||||
friend class Sdk;
|
||||
|
||||
public:
|
||||
ProgressBarWidget(BaseWidget* parent = nullptr);
|
||||
ProgressBarWidget(BaseWidget* parent);
|
||||
ProgressBarWidget(const ProgressBarWidget&) = delete;
|
||||
ProgressBarWidget(ProgressBarWidget&&) = default;
|
||||
~ProgressBarWidget() = default;
|
||||
@@ -67,8 +67,6 @@ namespace Ndk
|
||||
inline void SetTextMargin(float margin);
|
||||
inline void SetTextColor(const Nz::Color& color);
|
||||
|
||||
inline void ResizeToContent() override {}
|
||||
|
||||
NazaraSignal(OnValueChanged, const ProgressBarWidget* /*progressBar*/);
|
||||
|
||||
private:
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// Copyright (C) 2017 Samy Bensaid
|
||||
// Copyright (C) 2017 Samy Bensaid
|
||||
// This file is part of the "Nazara Development Kit"
|
||||
// For conditions of distribution and use, see copyright notice in Prerequisites.hpp
|
||||
|
||||
@@ -148,9 +148,8 @@ namespace Ndk
|
||||
{
|
||||
if (IsTextEnabled())
|
||||
{
|
||||
Nz::Vector2f size = GetContentSize();
|
||||
m_textSprite->Update(Nz::SimpleTextDrawer::Draw(Nz::String::Number(m_value).Append('%'),
|
||||
static_cast<unsigned>(std::min(size.x, size.y) / 2.f), 0u, m_textColor));
|
||||
Nz::Vector2f size = GetSize();
|
||||
m_textSprite->Update(Nz::SimpleTextDrawer::Draw(Nz::String::Number(m_value).Append('%'), static_cast<unsigned int>(std::min(size.x, size.y) / 2.f), 0u, m_textColor));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,13 +11,17 @@
|
||||
#include <Nazara/Utility/SimpleTextDrawer.hpp>
|
||||
#include <NDK/BaseWidget.hpp>
|
||||
#include <NDK/Widgets/Enums.hpp>
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
class NDK_API TextAreaWidget : public BaseWidget
|
||||
{
|
||||
public:
|
||||
TextAreaWidget(BaseWidget* parent = nullptr);
|
||||
using CharacterFilter = std::function<bool(char32_t)>;
|
||||
|
||||
TextAreaWidget(BaseWidget* parent);
|
||||
TextAreaWidget(const TextAreaWidget&) = delete;
|
||||
TextAreaWidget(TextAreaWidget&&) = default;
|
||||
~TextAreaWidget() = default;
|
||||
@@ -28,10 +32,18 @@ namespace Ndk
|
||||
|
||||
//virtual TextAreaWidget* Clone() const = 0;
|
||||
|
||||
inline void EnableMultiline(bool enable = true);
|
||||
|
||||
inline void EnableMultiline(bool enable = true);
|
||||
inline void EnableTabWriting(bool enable = true);
|
||||
|
||||
inline void Erase(std::size_t glyphPosition);
|
||||
void Erase(std::size_t firstGlyph, std::size_t lastGlyph);
|
||||
void EraseSelection();
|
||||
|
||||
inline const CharacterFilter& GetCharacterFilter() const;
|
||||
inline unsigned int GetCharacterSize() const;
|
||||
inline const Nz::Vector2ui& GetCursorPosition() const;
|
||||
inline Nz::Vector2ui GetCursorPosition(std::size_t glyphIndex) const;
|
||||
inline const Nz::String& GetDisplayText() const;
|
||||
inline EchoMode GetEchoMode() const;
|
||||
inline std::size_t GetGlyphIndex() const;
|
||||
@@ -39,25 +51,30 @@ namespace Ndk
|
||||
inline const Nz::String& GetText() const;
|
||||
inline const Nz::Color& GetTextColor() const;
|
||||
|
||||
std::size_t GetHoveredGlyph(float x, float y) const;
|
||||
Nz::Vector2ui GetHoveredGlyph(float x, float y) const;
|
||||
|
||||
inline bool HasSelection() const;
|
||||
|
||||
inline bool IsMultilineEnabled() const;
|
||||
inline bool IsReadOnly() const;
|
||||
inline bool IsTabWritingEnabled() const;
|
||||
|
||||
inline void MoveCursor(int offset);
|
||||
inline void MoveCursor(const Nz::Vector2i& offset);
|
||||
|
||||
void ResizeToContent() override;
|
||||
|
||||
inline void SetCharacterSize(unsigned int characterSize);
|
||||
inline void SetCharacterFilter(CharacterFilter filter);
|
||||
void SetCharacterSize(unsigned int characterSize);
|
||||
inline void SetCursorPosition(std::size_t glyphIndex);
|
||||
inline void SetCursorPosition(Nz::Vector2ui cursorPosition);
|
||||
inline void SetEchoMode(EchoMode echoMode);
|
||||
inline void SetReadOnly(bool readOnly = true);
|
||||
inline void SetSelection(Nz::Vector2ui fromPosition, Nz::Vector2ui toPosition);
|
||||
inline void SetText(const Nz::String& text);
|
||||
inline void SetTextColor(const Nz::Color& text);
|
||||
|
||||
void Write(const Nz::String& text);
|
||||
inline void Write(const Nz::String& text);
|
||||
inline void Write(const Nz::String& text, const Nz::Vector2ui& glyphPosition);
|
||||
void Write(const Nz::String& text, std::size_t glyphPosition);
|
||||
|
||||
TextAreaWidget& operator=(const TextAreaWidget&) = delete;
|
||||
TextAreaWidget& operator=(TextAreaWidget&&) = default;
|
||||
@@ -65,6 +82,8 @@ namespace Ndk
|
||||
NazaraSignal(OnTextAreaCursorMove, const TextAreaWidget* /*textArea*/, std::size_t* /*newCursorPosition*/);
|
||||
NazaraSignal(OnTextAreaKeyBackspace, const TextAreaWidget* /*textArea*/, bool* /*ignoreDefaultAction*/);
|
||||
NazaraSignal(OnTextAreaKeyDown, const TextAreaWidget* /*textArea*/, bool* /*ignoreDefaultAction*/);
|
||||
NazaraSignal(OnTextAreaKeyEnd, const TextAreaWidget* /*textArea*/, bool* /*ignoreDefaultAction*/);
|
||||
NazaraSignal(OnTextAreaKeyHome, const TextAreaWidget* /*textArea*/, bool* /*ignoreDefaultAction*/);
|
||||
NazaraSignal(OnTextAreaKeyLeft, const TextAreaWidget* /*textArea*/, bool* /*ignoreDefaultAction*/);
|
||||
NazaraSignal(OnTextAreaKeyReturn, const TextAreaWidget* /*textArea*/, bool* /*ignoreDefaultAction*/);
|
||||
NazaraSignal(OnTextAreaKeyRight, const TextAreaWidget* /*textArea*/, bool* /*ignoreDefaultAction*/);
|
||||
@@ -80,21 +99,29 @@ namespace Ndk
|
||||
bool OnKeyPressed(const Nz::WindowEvent::KeyEvent& key) override;
|
||||
void OnKeyReleased(const Nz::WindowEvent::KeyEvent& key) override;
|
||||
void OnMouseButtonPress(int /*x*/, int /*y*/, Nz::Mouse::Button button) override;
|
||||
void OnMouseButtonRelease(int /*x*/, int /*y*/, Nz::Mouse::Button button) override;
|
||||
void OnMouseEnter() override;
|
||||
void OnMouseMoved(int x, int y, int deltaX, int deltaY) override;
|
||||
void OnTextEntered(char32_t character, bool repeated) override;
|
||||
|
||||
void RefreshCursor();
|
||||
void UpdateDisplayText();
|
||||
|
||||
CharacterFilter m_characterFilter;
|
||||
EchoMode m_echoMode;
|
||||
EntityHandle m_cursorEntity;
|
||||
EntityHandle m_textEntity;
|
||||
Nz::SimpleTextDrawer m_drawer;
|
||||
Nz::SpriteRef m_cursorSprite;
|
||||
Nz::String m_text;
|
||||
Nz::TextSpriteRef m_textSprite;
|
||||
Nz::Vector2ui m_cursorPosition;
|
||||
Nz::Vector2ui m_cursorPositionBegin;
|
||||
Nz::Vector2ui m_cursorPositionEnd;
|
||||
Nz::Vector2ui m_selectionCursor;
|
||||
std::vector<Nz::SpriteRef> m_cursorSprites;
|
||||
bool m_isMouseButtonDown;
|
||||
bool m_multiLineEnabled;
|
||||
bool m_readOnly;
|
||||
bool m_tabEnabled; // writes (Shift+)Tab character if set to true
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,8 @@ namespace Ndk
|
||||
{
|
||||
inline void TextAreaWidget::Clear()
|
||||
{
|
||||
m_cursorPosition.MakeZero();
|
||||
m_cursorPositionBegin.MakeZero();
|
||||
m_cursorPositionEnd.MakeZero();
|
||||
m_drawer.Clear();
|
||||
m_text.Clear();
|
||||
m_textSprite->Update(m_drawer);
|
||||
@@ -22,6 +23,21 @@ namespace Ndk
|
||||
m_multiLineEnabled = enable;
|
||||
}
|
||||
|
||||
inline void TextAreaWidget::EnableTabWriting(bool enable)
|
||||
{
|
||||
m_tabEnabled = enable;
|
||||
}
|
||||
|
||||
inline void TextAreaWidget::Erase(std::size_t glyphPosition)
|
||||
{
|
||||
Erase(glyphPosition, glyphPosition + 1U);
|
||||
}
|
||||
|
||||
inline const TextAreaWidget::CharacterFilter& TextAreaWidget::GetCharacterFilter() const
|
||||
{
|
||||
return m_characterFilter;
|
||||
}
|
||||
|
||||
inline unsigned int TextAreaWidget::GetCharacterSize() const
|
||||
{
|
||||
return m_drawer.GetCharacterSize();
|
||||
@@ -29,7 +45,30 @@ namespace Ndk
|
||||
|
||||
inline const Nz::Vector2ui& TextAreaWidget::GetCursorPosition() const
|
||||
{
|
||||
return m_cursorPosition;
|
||||
return m_cursorPositionBegin;
|
||||
}
|
||||
|
||||
Nz::Vector2ui TextAreaWidget::GetCursorPosition(std::size_t glyphIndex) const
|
||||
{
|
||||
glyphIndex = std::min(glyphIndex, m_drawer.GetGlyphCount());
|
||||
|
||||
std::size_t lineCount = m_drawer.GetLineCount();
|
||||
std::size_t line = 0U;
|
||||
for (std::size_t i = line + 1; i < lineCount; ++i)
|
||||
{
|
||||
if (m_drawer.GetLine(i).glyphIndex > glyphIndex)
|
||||
break;
|
||||
|
||||
line = i;
|
||||
}
|
||||
|
||||
const auto& lineInfo = m_drawer.GetLine(line);
|
||||
|
||||
Nz::Vector2ui cursorPos;
|
||||
cursorPos.y = static_cast<unsigned int>(line);
|
||||
cursorPos.x = static_cast<unsigned int>(glyphIndex - lineInfo.glyphIndex);
|
||||
|
||||
return cursorPos;
|
||||
}
|
||||
|
||||
inline const Nz::String& TextAreaWidget::GetDisplayText() const
|
||||
@@ -68,11 +107,21 @@ namespace Ndk
|
||||
return m_drawer.GetColor();
|
||||
}
|
||||
|
||||
inline bool Ndk::TextAreaWidget::IsMultilineEnabled() const
|
||||
inline bool TextAreaWidget::HasSelection() const
|
||||
{
|
||||
return m_cursorPositionBegin != m_cursorPositionEnd;
|
||||
}
|
||||
|
||||
inline bool TextAreaWidget::IsMultilineEnabled() const
|
||||
{
|
||||
return m_multiLineEnabled;
|
||||
}
|
||||
|
||||
inline bool TextAreaWidget::IsTabWritingEnabled() const
|
||||
{
|
||||
return m_tabEnabled;
|
||||
}
|
||||
|
||||
inline bool TextAreaWidget::IsReadOnly() const
|
||||
{
|
||||
return m_readOnly;
|
||||
@@ -80,7 +129,7 @@ namespace Ndk
|
||||
|
||||
inline void TextAreaWidget::MoveCursor(int offset)
|
||||
{
|
||||
std::size_t cursorGlyph = GetGlyphIndex(m_cursorPosition);
|
||||
std::size_t cursorGlyph = GetGlyphIndex(m_cursorPositionBegin);
|
||||
if (offset >= 0)
|
||||
SetCursorPosition(cursorGlyph + static_cast<std::size_t>(offset));
|
||||
else
|
||||
@@ -109,40 +158,24 @@ namespace Ndk
|
||||
}
|
||||
};
|
||||
|
||||
Nz::Vector2ui cursorPosition = m_cursorPosition;
|
||||
Nz::Vector2ui cursorPosition = m_cursorPositionBegin;
|
||||
cursorPosition.x = ClampOffset(static_cast<unsigned int>(cursorPosition.x), offset.x);
|
||||
cursorPosition.y = ClampOffset(static_cast<unsigned int>(cursorPosition.y), offset.y);
|
||||
|
||||
SetCursorPosition(cursorPosition);
|
||||
}
|
||||
|
||||
inline void TextAreaWidget::SetCharacterSize(unsigned int characterSize)
|
||||
inline void TextAreaWidget::SetCharacterFilter(CharacterFilter filter)
|
||||
{
|
||||
m_drawer.SetCharacterSize(characterSize);
|
||||
|
||||
UpdateDisplayText();
|
||||
m_characterFilter = std::move(filter);
|
||||
}
|
||||
|
||||
inline void TextAreaWidget::SetCursorPosition(std::size_t glyphIndex)
|
||||
{
|
||||
OnTextAreaCursorMove(this, &glyphIndex);
|
||||
|
||||
glyphIndex = std::min(glyphIndex, m_drawer.GetGlyphCount());
|
||||
|
||||
std::size_t lineCount = m_drawer.GetLineCount();
|
||||
std::size_t line = 0U;
|
||||
for (std::size_t i = line + 1; i < lineCount; ++i)
|
||||
{
|
||||
if (m_drawer.GetLine(i).glyphIndex > glyphIndex)
|
||||
break;
|
||||
|
||||
line = i;
|
||||
}
|
||||
|
||||
const auto& lineInfo = m_drawer.GetLine(line);
|
||||
|
||||
m_cursorPosition.y = static_cast<unsigned int>(line);
|
||||
m_cursorPosition.x = static_cast<unsigned int>(glyphIndex - lineInfo.glyphIndex);
|
||||
m_cursorPositionBegin = GetCursorPosition(glyphIndex);
|
||||
m_cursorPositionEnd = m_cursorPositionBegin;
|
||||
|
||||
RefreshCursor();
|
||||
}
|
||||
@@ -153,7 +186,7 @@ namespace Ndk
|
||||
if (cursorPosition.y >= lineCount)
|
||||
cursorPosition.y = static_cast<unsigned int>(lineCount - 1);
|
||||
|
||||
m_cursorPosition = cursorPosition;
|
||||
m_cursorPositionBegin = cursorPosition;
|
||||
|
||||
const auto& lineInfo = m_drawer.GetLine(cursorPosition.y);
|
||||
if (cursorPosition.y + 1 < lineCount)
|
||||
@@ -162,6 +195,8 @@ namespace Ndk
|
||||
cursorPosition.x = std::min(cursorPosition.x, static_cast<unsigned int>(nextLineInfo.glyphIndex - lineInfo.glyphIndex - 1));
|
||||
}
|
||||
|
||||
m_cursorPositionEnd = m_cursorPositionBegin;
|
||||
|
||||
std::size_t glyphIndex = lineInfo.glyphIndex + cursorPosition.x;
|
||||
|
||||
OnTextAreaCursorMove(this, &glyphIndex);
|
||||
@@ -182,6 +217,23 @@ namespace Ndk
|
||||
m_cursorEntity->Enable(!m_readOnly && HasFocus());
|
||||
}
|
||||
|
||||
inline void TextAreaWidget::SetSelection(Nz::Vector2ui fromPosition, Nz::Vector2ui toPosition)
|
||||
{
|
||||
///TODO: Check if position are valid
|
||||
|
||||
// Ensure begin is before end
|
||||
if (toPosition.y < fromPosition.y || (toPosition.y == fromPosition.y && toPosition.x < fromPosition.x))
|
||||
std::swap(fromPosition, toPosition);
|
||||
|
||||
if (m_cursorPositionBegin != fromPosition || m_cursorPositionEnd != toPosition)
|
||||
{
|
||||
m_cursorPositionBegin = fromPosition;
|
||||
m_cursorPositionEnd = toPosition;
|
||||
|
||||
RefreshCursor();
|
||||
}
|
||||
}
|
||||
|
||||
inline void TextAreaWidget::SetText(const Nz::String& text)
|
||||
{
|
||||
m_text = text;
|
||||
@@ -196,4 +248,14 @@ namespace Ndk
|
||||
|
||||
m_textSprite->Update(m_drawer);
|
||||
}
|
||||
|
||||
inline void TextAreaWidget::Write(const Nz::String& text)
|
||||
{
|
||||
Write(text, GetGlyphIndex(m_cursorPositionBegin));
|
||||
}
|
||||
|
||||
inline void TextAreaWidget::Write(const Nz::String& text, const Nz::Vector2ui& glyphPosition)
|
||||
{
|
||||
Write(text, GetGlyphIndex(glyphPosition));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,6 +67,8 @@ namespace Ndk
|
||||
inline void KillEntity(Entity* entity);
|
||||
inline void KillEntities(const EntityVector& list);
|
||||
|
||||
inline bool IsEntityDying(const Entity* entity) const;
|
||||
inline bool IsEntityDying(EntityId id) const;
|
||||
inline bool IsEntityValid(const Entity* entity) const;
|
||||
inline bool IsEntityIdValid(EntityId id) const;
|
||||
inline bool IsProfilerEnabled() const;
|
||||
|
||||
@@ -324,13 +324,34 @@ namespace Ndk
|
||||
KillEntity(entity);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Checks whether or not an entity is dying (has been killed this update)
|
||||
* \return true If the entity exists and is dying
|
||||
*
|
||||
* \param entity Pointer to the entity
|
||||
*/
|
||||
inline bool World::IsEntityDying(const Entity* entity) const
|
||||
{
|
||||
return entity && IsEntityDying(entity->GetId());
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Checks whether or not an entity is dying (has been killed this update)
|
||||
* \return true If it is the case, false if the entity is alive (and hasn't been killed yet) or if the entity id is invalid
|
||||
*
|
||||
* \param id Identifier of the entity
|
||||
*/
|
||||
inline bool World::IsEntityDying(EntityId id) const
|
||||
{
|
||||
return m_killedEntities.UnboundedTest(id);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Checks whether or not an entity is valid
|
||||
* \return true If it is the case
|
||||
*
|
||||
* \param entity Pointer to the entity
|
||||
*/
|
||||
|
||||
inline bool World::IsEntityValid(const Entity* entity) const
|
||||
{
|
||||
return entity && entity->GetWorld() == this && IsEntityIdValid(entity->GetId());
|
||||
|
||||
@@ -81,7 +81,7 @@ namespace Ndk
|
||||
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
|
||||
|
||||
m_backgroundEntity = CreateEntity(false);
|
||||
m_backgroundEntity = CreateEntity();
|
||||
m_backgroundEntity->AddComponent<GraphicsComponent>().Attach(m_backgroundSprite, -1);
|
||||
m_backgroundEntity->AddComponent<NodeComponent>().SetParent(this);
|
||||
|
||||
@@ -89,14 +89,14 @@ namespace Ndk
|
||||
}
|
||||
else
|
||||
{
|
||||
m_backgroundEntity->Kill();
|
||||
m_backgroundEntity.Reset();
|
||||
m_backgroundSprite.Reset();
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Checks if this widget has keyboard focus
|
||||
* \return true if widget has keyboard focus, false otherwhise
|
||||
* \return true if widget has keyboard focus, false otherwise
|
||||
*/
|
||||
bool BaseWidget::HasFocus() const
|
||||
{
|
||||
@@ -106,6 +106,19 @@ namespace Ndk
|
||||
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 Nz::Color& color)
|
||||
{
|
||||
m_backgroundColor = color;
|
||||
@@ -131,11 +144,6 @@ namespace Ndk
|
||||
m_canvas->SetKeyboardOwner(m_canvasIndex);
|
||||
}
|
||||
|
||||
void BaseWidget::SetSize(const Nz::Vector2f& size)
|
||||
{
|
||||
SetContentSize({std::max(size.x - m_padding.left - m_padding.right, 0.f), std::max(size.y - m_padding.top - m_padding.bottom, 0.f)});
|
||||
}
|
||||
|
||||
void BaseWidget::Show(bool show)
|
||||
{
|
||||
if (m_visible != show)
|
||||
@@ -155,7 +163,7 @@ namespace Ndk
|
||||
}
|
||||
}
|
||||
|
||||
const Ndk::EntityHandle& BaseWidget::CreateEntity(bool isContentEntity)
|
||||
const Ndk::EntityHandle& BaseWidget::CreateEntity()
|
||||
{
|
||||
const EntityHandle& newEntity = m_world->CreateEntity();
|
||||
newEntity->Enable(m_visible);
|
||||
@@ -163,7 +171,6 @@ namespace Ndk
|
||||
m_entities.emplace_back();
|
||||
WidgetEntity& widgetEntity = m_entities.back();
|
||||
widgetEntity.handle = newEntity;
|
||||
widgetEntity.isContent = isContentEntity;
|
||||
|
||||
return newEntity;
|
||||
}
|
||||
@@ -179,7 +186,7 @@ namespace Ndk
|
||||
void BaseWidget::Layout()
|
||||
{
|
||||
if (m_backgroundEntity)
|
||||
m_backgroundSprite->SetSize(m_contentSize.x + m_padding.left + m_padding.right, m_contentSize.y + m_padding.top + m_padding.bottom);
|
||||
m_backgroundSprite->SetSize(m_size.x, m_size.y);
|
||||
|
||||
UpdatePositionAndSize();
|
||||
}
|
||||
@@ -282,16 +289,12 @@ namespace Ndk
|
||||
Nz::Vector2f widgetPos = Nz::Vector2f(GetPosition());
|
||||
Nz::Vector2f widgetSize = GetSize();
|
||||
|
||||
Nz::Vector2f contentPos = widgetPos + GetContentOrigin();
|
||||
Nz::Vector2f contentSize = GetContentSize();
|
||||
|
||||
Nz::Recti fullBounds(Nz::Rectf(widgetPos.x, widgetPos.y, widgetSize.x, widgetSize.y));
|
||||
Nz::Recti contentBounds(Nz::Rectf(contentPos.x, contentPos.y, contentSize.x, contentSize.y));
|
||||
for (WidgetEntity& widgetEntity : m_entities)
|
||||
{
|
||||
const Ndk::EntityHandle& entity = widgetEntity.handle;
|
||||
if (entity->HasComponent<GraphicsComponent>())
|
||||
entity->GetComponent<GraphicsComponent>().SetScissorRect((widgetEntity.isContent) ? contentBounds : fullBounds);
|
||||
entity->GetComponent<GraphicsComponent>().SetScissorRect(fullBounds);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,10 +7,6 @@
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
void Canvas::ResizeToContent()
|
||||
{
|
||||
}
|
||||
|
||||
std::size_t Canvas::RegisterWidget(BaseWidget* widget)
|
||||
{
|
||||
WidgetEntry box;
|
||||
|
||||
@@ -24,7 +24,6 @@ namespace Ndk
|
||||
*
|
||||
* \remark Produces a NazaraAssert if the entity has no physics component and has no static body
|
||||
*/
|
||||
|
||||
void CollisionComponent2D::SetGeom(Nz::Collider2DRef geom)
|
||||
{
|
||||
m_geom = std::move(geom);
|
||||
@@ -56,7 +55,7 @@ namespace Ndk
|
||||
|
||||
NazaraAssert(entityWorld, "Entity must have world");
|
||||
NazaraAssert(entityWorld->HasSystem<PhysicsSystem2D>(), "World must have a physics system");
|
||||
Nz::PhysWorld2D& physWorld = entityWorld->GetSystem<PhysicsSystem2D>().GetWorld();
|
||||
Nz::PhysWorld2D& physWorld = entityWorld->GetSystem<PhysicsSystem2D>().GetPhysWorld();
|
||||
|
||||
m_staticBody = std::make_unique<Nz::RigidBody2D>(&physWorld, 0.f, m_geom);
|
||||
m_staticBody->SetUserdata(reinterpret_cast<void*>(static_cast<std::ptrdiff_t>(m_entity->GetId())));
|
||||
|
||||
10
SDK/src/NDK/Components/DebugComponent.cpp
Normal file
10
SDK/src/NDK/Components/DebugComponent.cpp
Normal file
@@ -0,0 +1,10 @@
|
||||
// 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 <NDK/Components/DebugComponent.hpp>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
ComponentIndex DebugComponent::componentIndex;
|
||||
}
|
||||
@@ -22,6 +22,7 @@ namespace Ndk
|
||||
*/
|
||||
void GraphicsComponent::AddToRenderQueue(Nz::AbstractRenderQueue* renderQueue) const
|
||||
{
|
||||
EnsureBoundingVolumesUpdate();
|
||||
EnsureTransformMatrixUpdate();
|
||||
|
||||
RenderSystem& renderSystem = m_entity->GetWorld()->GetSystem<RenderSystem>();
|
||||
@@ -30,7 +31,6 @@ namespace Ndk
|
||||
{
|
||||
if (!object.dataUpdated)
|
||||
{
|
||||
object.data.transformMatrix = Nz::Matrix4f::ConcatenateAffine(renderSystem.GetCoordinateSystemMatrix(), Nz::Matrix4f::ConcatenateAffine(object.data.localMatrix, m_transformMatrix));
|
||||
object.renderable->UpdateData(&object.data);
|
||||
object.dataUpdated = true;
|
||||
}
|
||||
@@ -39,6 +39,34 @@ namespace Ndk
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Adds the renderable elements to the render queue if their bounding volume intersects with the frustum
|
||||
*
|
||||
* \param frustum Queue to be added
|
||||
* \param renderQueue Queue to be added
|
||||
*/
|
||||
void GraphicsComponent::AddToRenderQueueByCulling(const Nz::Frustumf& frustum, Nz::AbstractRenderQueue* renderQueue) const
|
||||
{
|
||||
EnsureBoundingVolumesUpdate();
|
||||
EnsureTransformMatrixUpdate();
|
||||
|
||||
RenderSystem& renderSystem = m_entity->GetWorld()->GetSystem<RenderSystem>();
|
||||
|
||||
for (const Renderable& object : m_renderables)
|
||||
{
|
||||
if (frustum.Contains(object.boundingVolume))
|
||||
{
|
||||
if (!object.dataUpdated)
|
||||
{
|
||||
object.renderable->UpdateData(&object.data);
|
||||
object.dataUpdated = true;
|
||||
}
|
||||
|
||||
object.renderable->AddToRenderQueue(renderQueue, object.data, m_scissorRect);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Attaches a renderable to the entity with a specific matrix
|
||||
*
|
||||
@@ -60,12 +88,13 @@ namespace Ndk
|
||||
for (std::size_t i = 0; i < materialCount; ++i)
|
||||
RegisterMaterial(entry.renderable->GetMaterial(i));
|
||||
|
||||
InvalidateBoundingVolume();
|
||||
InvalidateAABB();
|
||||
ForceCullingInvalidation();
|
||||
}
|
||||
|
||||
void GraphicsComponent::ConnectInstancedRenderableSignals(Renderable& entry)
|
||||
{
|
||||
entry.renderableBoundingVolumeInvalidationSlot.Connect(entry.renderable->OnInstancedRenderableInvalidateBoundingVolume, [this](const Nz::InstancedRenderable*) { InvalidateBoundingVolume(); });
|
||||
entry.renderableBoundingVolumeInvalidationSlot.Connect(entry.renderable->OnInstancedRenderableInvalidateBoundingVolume, [this](const Nz::InstancedRenderable*) { InvalidateAABB(); });
|
||||
entry.renderableDataInvalidationSlot.Connect(entry.renderable->OnInstancedRenderableInvalidateData, std::bind(&GraphicsComponent::InvalidateRenderableData, this, std::placeholders::_1, std::placeholders::_2, m_renderables.size() - 1));
|
||||
entry.renderableMaterialInvalidationSlot.Connect(entry.renderable->OnInstancedRenderableInvalidateMaterial, this, &GraphicsComponent::InvalidateRenderableMaterial);
|
||||
entry.renderableReleaseSlot.Connect(entry.renderable->OnInstancedRenderableRelease, this, &GraphicsComponent::Detach);
|
||||
@@ -82,8 +111,7 @@ namespace Ndk
|
||||
r.dataUpdated = false;
|
||||
r.renderable->InvalidateData(&r.data, flags);
|
||||
|
||||
for (VolumeCullingEntry& entry : m_volumeCullingEntries)
|
||||
entry.listEntry.ForceInvalidation();
|
||||
ForceCullingInvalidation();
|
||||
}
|
||||
|
||||
void GraphicsComponent::InvalidateRenderableMaterial(const Nz::InstancedRenderable* renderable, std::size_t skinIndex, std::size_t matIndex, const Nz::MaterialRef& newMat)
|
||||
@@ -234,11 +262,10 @@ namespace Ndk
|
||||
NazaraUnused(node);
|
||||
|
||||
// Our view matrix depends on NodeComponent position/rotation
|
||||
InvalidateBoundingVolume();
|
||||
InvalidateAABB();
|
||||
InvalidateTransformMatrix();
|
||||
|
||||
for (VolumeCullingEntry& entry : m_volumeCullingEntries)
|
||||
entry.listEntry.ForceInvalidation(); //< Force invalidation on movement
|
||||
ForceCullingInvalidation(); //< Force invalidation on movement for now (FIXME)
|
||||
}
|
||||
|
||||
void GraphicsComponent::UnregisterMaterial(Nz::Material* material)
|
||||
@@ -263,35 +290,32 @@ namespace Ndk
|
||||
* \brief Updates the bounding volume
|
||||
*/
|
||||
|
||||
void GraphicsComponent::UpdateBoundingVolume() const
|
||||
void GraphicsComponent::UpdateBoundingVolumes() const
|
||||
{
|
||||
EnsureTransformMatrixUpdate();
|
||||
|
||||
m_boundingVolume.MakeNull();
|
||||
for (const Renderable& r : m_renderables)
|
||||
{
|
||||
Nz::BoundingVolumef boundingVolume = r.renderable->GetBoundingVolume();
|
||||
|
||||
// Adjust renderable bounding volume by local matrix
|
||||
if (boundingVolume.IsFinite())
|
||||
{
|
||||
Nz::Boxf localBox = boundingVolume.obb.localBox;
|
||||
Nz::Vector3f newPos = r.data.localMatrix * localBox.GetPosition();
|
||||
Nz::Vector3f newLengths = r.data.localMatrix * localBox.GetLengths();
|
||||
|
||||
boundingVolume.Set(Nz::Boxf(newPos.x, newPos.y, newPos.z, newLengths.x, newLengths.y, newLengths.z));
|
||||
}
|
||||
|
||||
m_boundingVolume.ExtendTo(boundingVolume);
|
||||
}
|
||||
|
||||
RenderSystem& renderSystem = m_entity->GetWorld()->GetSystem<RenderSystem>();
|
||||
|
||||
m_boundingVolume.Update(Nz::Matrix4f::ConcatenateAffine(renderSystem.GetCoordinateSystemMatrix(), m_transformMatrix));
|
||||
m_boundingVolumeUpdated = true;
|
||||
m_aabb.Set(-1.f, -1.f, -1.f);
|
||||
for (const Renderable& r : m_renderables)
|
||||
{
|
||||
r.boundingVolume = r.renderable->GetBoundingVolume();
|
||||
r.data.transformMatrix = Nz::Matrix4f::ConcatenateAffine(renderSystem.GetCoordinateSystemMatrix(), Nz::Matrix4f::ConcatenateAffine(r.data.localMatrix, m_transformMatrix));
|
||||
if (r.boundingVolume.IsFinite())
|
||||
{
|
||||
r.boundingVolume.Update(r.data.transformMatrix);
|
||||
|
||||
for (VolumeCullingEntry& entry : m_volumeCullingEntries)
|
||||
entry.listEntry.UpdateVolume(m_boundingVolume);
|
||||
if (m_aabb.IsValid())
|
||||
m_aabb.ExtendTo(r.boundingVolume.aabb);
|
||||
else
|
||||
m_aabb.Set(r.boundingVolume.aabb);
|
||||
}
|
||||
}
|
||||
|
||||
m_boundingVolumesUpdated = true;
|
||||
|
||||
for (CullingBoxEntry& entry : m_cullingBoxEntries)
|
||||
entry.listEntry.UpdateBox(m_aabb);
|
||||
}
|
||||
|
||||
/*!
|
||||
|
||||
@@ -29,7 +29,7 @@ namespace Ndk
|
||||
World* entityWorld = m_entity->GetWorld();
|
||||
NazaraAssert(entityWorld->HasSystem<PhysicsSystem2D>(), "World must have a 2D physics system");
|
||||
|
||||
Nz::PhysWorld2D& world = entityWorld->GetSystem<PhysicsSystem2D>().GetWorld();
|
||||
Nz::PhysWorld2D& world = entityWorld->GetSystem<PhysicsSystem2D>().GetPhysWorld();
|
||||
|
||||
Nz::Collider2DRef geom;
|
||||
if (m_entity->HasComponent<CollisionComponent2D>())
|
||||
|
||||
@@ -14,26 +14,8 @@ namespace Ndk
|
||||
* \brief NDK class that represents an entity in a world
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \brief Constructs a Entity object by move semantic
|
||||
*
|
||||
* \param entity Entity to move into this
|
||||
*/
|
||||
|
||||
Entity::Entity(Entity&& entity) :
|
||||
HandledObject(std::move(entity)),
|
||||
m_components(std::move(entity.m_components)),
|
||||
m_containedInLists(std::move(entity.m_containedInLists)),
|
||||
m_componentBits(std::move(entity.m_componentBits)),
|
||||
m_removedComponentBits(std::move(entity.m_removedComponentBits)),
|
||||
m_systemBits(std::move(entity.m_systemBits)),
|
||||
m_id(entity.m_id),
|
||||
m_world(entity.m_world),
|
||||
m_enabled(entity.m_enabled),
|
||||
m_valid(entity.m_valid)
|
||||
{
|
||||
entity.m_world = nullptr;
|
||||
}
|
||||
// Must exists in .cpp file because of BaseComponent unique_ptr
|
||||
Entity::Entity(Entity&&) noexcept = default;
|
||||
|
||||
/*!
|
||||
* \brief Constructs a Entity object linked to a world and with an id
|
||||
@@ -41,7 +23,6 @@ namespace Ndk
|
||||
* \param world World in which the entity interact
|
||||
* \param id Identifier of the entity
|
||||
*/
|
||||
|
||||
Entity::Entity(World* world, EntityId id) :
|
||||
m_id(id),
|
||||
m_world(world)
|
||||
@@ -53,7 +34,6 @@ namespace Ndk
|
||||
*
|
||||
* \see Destroy
|
||||
*/
|
||||
|
||||
Entity::~Entity()
|
||||
{
|
||||
if (m_world && m_valid)
|
||||
@@ -153,13 +133,22 @@ namespace Ndk
|
||||
/*!
|
||||
* \brief Invalidates the entity
|
||||
*/
|
||||
|
||||
void Entity::Invalidate()
|
||||
{
|
||||
// We alert everyone that we have been updated
|
||||
m_world->Invalidate(m_id);
|
||||
}
|
||||
|
||||
|
||||
/*!
|
||||
* \brief Checks if the entity has been killed this update
|
||||
* \return True if the entity is currently dying and will be dead at next world refresh
|
||||
*/
|
||||
bool Entity::IsDying() const
|
||||
{
|
||||
return m_world->IsEntityDying(m_id);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Creates the entity
|
||||
*/
|
||||
|
||||
@@ -67,7 +67,7 @@ namespace Ndk
|
||||
|
||||
music.BindMethod("IsLooping", &Nz::Music::IsLooping);
|
||||
|
||||
music.BindMethod("OpenFromFile", &Nz::Music::OpenFromFile, Nz::MusicParams());
|
||||
music.BindMethod("OpenFromFile", &Nz::Music::OpenFromFile, Nz::SoundStreamParams());
|
||||
|
||||
music.BindMethod("Pause", &Nz::Music::Pause);
|
||||
music.BindMethod("Play", &Nz::Music::Play);
|
||||
@@ -138,7 +138,7 @@ namespace Ndk
|
||||
|
||||
soundBuffer.BindMethod("IsValid", &Nz::SoundBuffer::IsValid);
|
||||
|
||||
soundBuffer.BindMethod("LoadFromFile", &Nz::SoundBuffer::LoadFromFile, Nz::SoundBufferParams());
|
||||
soundBuffer.BindStaticMethod("LoadFromFile", &Nz::SoundBuffer::LoadFromFile, Nz::SoundBufferParams());
|
||||
|
||||
soundBuffer.BindStaticMethod("IsFormatSupported", &Nz::SoundBuffer::IsFormatSupported);
|
||||
|
||||
|
||||
@@ -181,8 +181,6 @@ namespace Ndk
|
||||
material.BindMethod("IsShadowCastingEnabled", &Nz::Material::IsShadowCastingEnabled);
|
||||
material.BindMethod("IsShadowReceiveEnabled", &Nz::Material::IsShadowReceiveEnabled);
|
||||
|
||||
material.BindMethod("LoadFromFile", &Nz::Material::LoadFromFile, Nz::MaterialParams());
|
||||
|
||||
material.BindMethod("Reset", &Nz::Material::Reset);
|
||||
|
||||
material.BindMethod("SetAlphaThreshold", &Nz::Material::SetAlphaThreshold);
|
||||
@@ -205,6 +203,7 @@ namespace Ndk
|
||||
material.BindMethod("SetSrcBlend", &Nz::Material::SetSrcBlend);
|
||||
|
||||
material.BindStaticMethod("GetDefault", &Nz::Material::GetDefault);
|
||||
material.BindStaticMethod("LoadFromFile", &Nz::Material::LoadFromFile, Nz::MaterialParams());
|
||||
|
||||
material.BindMethod("SetAlphaMap", [] (Nz::LuaState& lua, Nz::MaterialRef& instance, std::size_t /*argumentCount*/) -> int
|
||||
{
|
||||
@@ -308,8 +307,6 @@ namespace Ndk
|
||||
//modelClass.SetMethod("GetMesh", &Nz::Model::GetMesh);
|
||||
|
||||
model.BindMethod("IsAnimated", &Nz::Model::IsAnimated);
|
||||
model.BindMethod("LoadFromFile", &Nz::Model::LoadFromFile, Nz::ModelParameters());
|
||||
|
||||
|
||||
model.BindMethod("SetMaterial", [] (Nz::LuaState& lua, Nz::Model* instance, std::size_t argumentCount) -> int
|
||||
{
|
||||
@@ -371,6 +368,8 @@ namespace Ndk
|
||||
|
||||
//modelClass.SetMethod("SetMesh", &Nz::Model::SetMesh);
|
||||
//modelClass.SetMethod("SetSequence", &Nz::Model::SetSequence);
|
||||
|
||||
model.BindStaticMethod("LoadFromFile", &Nz::Model::LoadFromFile, Nz::ModelParameters());
|
||||
}
|
||||
|
||||
/*********************************** Nz::Sprite ***********************************/
|
||||
|
||||
@@ -266,14 +266,15 @@ namespace Ndk
|
||||
state.SetGlobal("ResolveError");
|
||||
|
||||
// Nz::SocketError
|
||||
static_assert(Nz::SocketError_Max + 1 == 15, "Nz::ResolveError has been updated but change was not reflected to Lua binding");
|
||||
state.PushTable(0, 15);
|
||||
static_assert(Nz::SocketError_Max + 1 == 16, "Nz::SocketError has been updated but change was not reflected to Lua binding");
|
||||
state.PushTable(0, 16);
|
||||
{
|
||||
state.PushField("AddressNotAvailable", Nz::SocketError_AddressNotAvailable);
|
||||
state.PushField("ConnectionClosed", Nz::SocketError_ConnectionClosed);
|
||||
state.PushField("ConnectionRefused", Nz::SocketError_ConnectionRefused);
|
||||
state.PushField("DatagramSize", Nz::SocketError_DatagramSize);
|
||||
state.PushField("Internal", Nz::SocketError_Internal);
|
||||
state.PushField("Interrupted", Nz::SocketError_Interrupted);
|
||||
state.PushField("Packet", Nz::SocketError_Packet);
|
||||
state.PushField("NetworkError", Nz::SocketError_NetworkError);
|
||||
state.PushField("NoError", Nz::SocketError_NoError);
|
||||
|
||||
@@ -44,21 +44,6 @@ namespace Ndk
|
||||
texture.BindMethod("InvalidateMipmaps", &Nz::Texture::InvalidateMipmaps);
|
||||
texture.BindMethod("IsValid", &Nz::Texture::IsValid);
|
||||
|
||||
texture.BindMethod("LoadFromFile", &Nz::Texture::LoadFromFile, true, Nz::ImageParams());
|
||||
//bool LoadFromImage(const Image& image, bool generateMipmaps = true);
|
||||
//bool LoadFromMemory(const void* data, std::size_t size, const ImageParams& params = ImageParams(), bool generateMipmaps = true);
|
||||
//bool LoadFromStream(Stream& stream, const ImageParams& params = ImageParams(), bool generateMipmaps = true);
|
||||
|
||||
texture.BindMethod("LoadArrayFromFile", &Nz::Texture::LoadArrayFromFile, Nz::Vector2ui(2, 2), true, Nz::ImageParams());
|
||||
//bool LoadArrayFromImage(const Image& image, bool generateMipmaps = true, const Vector2ui& atlasSize = Vector2ui(2, 2));
|
||||
//bool LoadArrayFromMemory(const void* data, std::size_t size, const ImageParams& imageParams = ImageParams(), bool generateMipmaps = true, const Vector2ui& atlasSize = Vector2ui(2, 2));
|
||||
//bool LoadArrayFromStream(Stream& stream, const ImageParams& imageParams = ImageParams(), bool generateMipmaps = true, const Vector2ui& atlasSize = Vector2ui(2, 2));
|
||||
|
||||
//bool LoadCubemapFromFile(const String& filePath, const ImageParams& imageParams = ImageParams(), bool generateMipmaps = true, const CubemapParams& cubemapParams = CubemapParams());
|
||||
//bool LoadCubemapFromImage(const Image& image, bool generateMipmaps = true, const CubemapParams& params = CubemapParams());
|
||||
//bool LoadCubemapFromMemory(const void* data, std::size_t size, const ImageParams& imageParams = ImageParams(), bool generateMipmaps = true, const CubemapParams& cubemapParams = CubemapParams());
|
||||
//bool LoadCubemapFromStream(Stream& stream, const ImageParams& imageParams = ImageParams(), bool generateMipmaps = true, const CubemapParams& cubemapParams = CubemapParams());
|
||||
|
||||
texture.BindMethod("LoadFaceFromFile", &Nz::Texture::LoadFaceFromFile, Nz::ImageParams());
|
||||
//bool LoadFaceFromMemory(CubemapFace face, const void* data, std::size_t size, const ImageParams& params = ImageParams());
|
||||
//bool LoadFaceFromStream(CubemapFace face, Stream& stream, const ImageParams& params = ImageParams());
|
||||
@@ -71,6 +56,21 @@ namespace Ndk
|
||||
texture.BindStaticMethod("IsFormatSupported", &Nz::Texture::IsFormatSupported);
|
||||
texture.BindStaticMethod("IsMipmappingSupported", &Nz::Texture::IsMipmappingSupported);
|
||||
texture.BindStaticMethod("IsTypeSupported", &Nz::Texture::IsTypeSupported);
|
||||
|
||||
texture.BindStaticMethod("LoadFromFile", &Nz::Texture::LoadFromFile, true, Nz::ImageParams());
|
||||
//TextureRef LoadFromImage(const Image& image, bool generateMipmaps = true);
|
||||
//TextureRef LoadFromMemory(const void* data, std::size_t size, const ImageParams& params = ImageParams(), bool generateMipmaps = true);
|
||||
//TextureRef LoadFromStream(Stream& stream, const ImageParams& params = ImageParams(), bool generateMipmaps = true);
|
||||
|
||||
texture.BindStaticMethod("LoadArrayFromFile", &Nz::Texture::LoadArrayFromFile, Nz::Vector2ui(2, 2), true, Nz::ImageParams());
|
||||
//TextureRef LoadArrayFromImage(const Image& image, bool generateMipmaps = true, const Vector2ui& atlasSize = Vector2ui(2, 2));
|
||||
//TextureRef LoadArrayFromMemory(const void* data, std::size_t size, const ImageParams& imageParams = ImageParams(), bool generateMipmaps = true, const Vector2ui& atlasSize = Vector2ui(2, 2));
|
||||
//TextureRef LoadArrayFromStream(Stream& stream, const ImageParams& imageParams = ImageParams(), bool generateMipmaps = true, const Vector2ui& atlasSize = Vector2ui(2, 2));
|
||||
|
||||
//TextureRef LoadCubemapFromFile(const String& filePath, const ImageParams& imageParams = ImageParams(), bool generateMipmaps = true, const CubemapParams& cubemapParams = CubemapParams());
|
||||
//TextureRef LoadCubemapFromImage(const Image& image, bool generateMipmaps = true, const CubemapParams& params = CubemapParams());
|
||||
//TextureRef LoadCubemapFromMemory(const void* data, std::size_t size, const ImageParams& imageParams = ImageParams(), bool generateMipmaps = true, const CubemapParams& cubemapParams = CubemapParams());
|
||||
//TextureRef LoadCubemapFromStream(Stream& stream, const ImageParams& imageParams = ImageParams(), bool generateMipmaps = true, const CubemapParams& cubemapParams = CubemapParams());
|
||||
}
|
||||
|
||||
/*********************************** Nz::TextureLibrary ***********************************/
|
||||
|
||||
@@ -148,8 +148,6 @@ namespace Ndk
|
||||
|
||||
font.BindMethod("Precache", (bool(Nz::Font::*)(unsigned int, Nz::UInt32, const Nz::String&) const) &Nz::Font::Precache);
|
||||
|
||||
font.BindMethod("OpenFromFile", &Nz::Font::OpenFromFile, Nz::FontParams());
|
||||
|
||||
font.BindMethod("SetGlyphBorder", &Nz::Font::SetGlyphBorder);
|
||||
font.BindMethod("SetMinimumStepSize", &Nz::Font::SetMinimumStepSize);
|
||||
|
||||
@@ -157,6 +155,8 @@ namespace Ndk
|
||||
font.BindStaticMethod("GetDefaultGlyphBorder", &Nz::Font::GetDefaultGlyphBorder);
|
||||
font.BindStaticMethod("GetDefaultMinimumStepSize", &Nz::Font::GetDefaultMinimumStepSize);
|
||||
|
||||
font.BindStaticMethod("OpenFromFile", &Nz::Font::OpenFromFile, Nz::FontParams());
|
||||
|
||||
font.BindStaticMethod("SetDefaultGlyphBorder", &Nz::Font::SetDefaultGlyphBorder);
|
||||
font.BindStaticMethod("SetDefaultMinimumStepSize", &Nz::Font::SetDefaultMinimumStepSize);
|
||||
}
|
||||
|
||||
@@ -28,11 +28,13 @@
|
||||
|
||||
#ifndef NDK_SERVER
|
||||
#include <NDK/Components/CameraComponent.hpp>
|
||||
#include <NDK/Components/DebugComponent.hpp>
|
||||
#include <NDK/Components/LightComponent.hpp>
|
||||
#include <NDK/Components/ListenerComponent.hpp>
|
||||
#include <NDK/Components/GraphicsComponent.hpp>
|
||||
#include <NDK/Components/ParticleEmitterComponent.hpp>
|
||||
#include <NDK/Components/ParticleGroupComponent.hpp>
|
||||
#include <NDK/Systems/DebugSystem.hpp>
|
||||
#include <NDK/Systems/ParticleSystem.hpp>
|
||||
#include <NDK/Systems/ListenerSystem.hpp>
|
||||
#include <NDK/Systems/RenderSystem.hpp>
|
||||
@@ -95,6 +97,7 @@ namespace Ndk
|
||||
#ifndef NDK_SERVER
|
||||
// Client components
|
||||
InitializeComponent<CameraComponent>("NdkCam");
|
||||
InitializeComponent<DebugComponent>("NdkDebug");
|
||||
InitializeComponent<LightComponent>("NdkLight");
|
||||
InitializeComponent<ListenerComponent>("NdkList");
|
||||
InitializeComponent<GraphicsComponent>("NdkGfx");
|
||||
@@ -113,6 +116,7 @@ namespace Ndk
|
||||
|
||||
#ifndef NDK_SERVER
|
||||
// Client systems
|
||||
InitializeSystem<DebugSystem>();
|
||||
InitializeSystem<ListenerSystem>();
|
||||
InitializeSystem<ParticleSystem>();
|
||||
InitializeSystem<RenderSystem>();
|
||||
|
||||
433
SDK/src/NDK/Systems/DebugSystem.cpp
Normal file
433
SDK/src/NDK/Systems/DebugSystem.cpp
Normal file
@@ -0,0 +1,433 @@
|
||||
// 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 <NDK/Systems/DebugSystem.hpp>
|
||||
#include <Nazara/Core/Primitive.hpp>
|
||||
#include <Nazara/Graphics/Model.hpp>
|
||||
#include <Nazara/Utility/IndexIterator.hpp>
|
||||
#include <Nazara/Utility/Mesh.hpp>
|
||||
#include <Nazara/Utility/StaticMesh.hpp>
|
||||
#include <NDK/Components/CollisionComponent3D.hpp>
|
||||
#include <NDK/Components/DebugComponent.hpp>
|
||||
#include <NDK/Components/GraphicsComponent.hpp>
|
||||
#include <NDK/Components/NodeComponent.hpp>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
namespace
|
||||
{
|
||||
class DebugRenderable : public Nz::InstancedRenderable
|
||||
{
|
||||
public:
|
||||
DebugRenderable(Ndk::Entity* owner, Nz::IndexBufferRef indexBuffer, Nz::VertexBufferRef vertexBuffer) :
|
||||
m_entityOwner(owner),
|
||||
m_indexBuffer(std::move(indexBuffer)),
|
||||
m_vertexBuffer(std::move(vertexBuffer))
|
||||
{
|
||||
ResetMaterials(1);
|
||||
|
||||
m_meshData.indexBuffer = m_indexBuffer;
|
||||
m_meshData.primitiveMode = Nz::PrimitiveMode_LineList;
|
||||
m_meshData.vertexBuffer = m_vertexBuffer;
|
||||
}
|
||||
|
||||
void UpdateBoundingVolume(InstanceData* /*instanceData*/) const override
|
||||
{
|
||||
}
|
||||
|
||||
void MakeBoundingVolume() const override
|
||||
{
|
||||
// We generate an infinite bounding volume so that we're always considered for rendering when culling does occurs
|
||||
// (bounding volume culling happens only if GraphicsComponent AABB partially fail)
|
||||
m_boundingVolume.MakeInfinite();
|
||||
}
|
||||
|
||||
protected:
|
||||
Ndk::EntityHandle m_entityOwner;
|
||||
Nz::IndexBufferRef m_indexBuffer;
|
||||
Nz::MeshData m_meshData;
|
||||
Nz::VertexBufferRef m_vertexBuffer;
|
||||
};
|
||||
|
||||
class AABBDebugRenderable : public DebugRenderable
|
||||
{
|
||||
public:
|
||||
AABBDebugRenderable(Ndk::Entity* owner, Nz::MaterialRef globalMaterial, Nz::MaterialRef localMaterial, Nz::IndexBufferRef indexBuffer, Nz::VertexBufferRef vertexBuffer) :
|
||||
DebugRenderable(owner, std::move(indexBuffer), std::move(vertexBuffer)),
|
||||
m_globalMaterial(std::move(globalMaterial)),
|
||||
m_localMaterial(std::move(localMaterial))
|
||||
{
|
||||
}
|
||||
|
||||
void AddToRenderQueue(Nz::AbstractRenderQueue* renderQueue, const InstanceData& instanceData, const Nz::Recti& scissorRect) const override
|
||||
{
|
||||
NazaraAssert(m_entityOwner, "DebugRenderable has no owner");
|
||||
|
||||
const DebugComponent& entityDebug = m_entityOwner->GetComponent<DebugComponent>();
|
||||
const GraphicsComponent& entityGfx = m_entityOwner->GetComponent<GraphicsComponent>();
|
||||
|
||||
auto DrawBox = [&](const Nz::Boxf& box, const Nz::MaterialRef& mat)
|
||||
{
|
||||
Nz::Matrix4f transformMatrix = Nz::Matrix4f::Identity();
|
||||
transformMatrix.SetScale(box.GetLengths());
|
||||
transformMatrix.SetTranslation(box.GetCenter());
|
||||
|
||||
renderQueue->AddMesh(0, mat, m_meshData, Nz::Boxf::Zero(), transformMatrix, scissorRect);
|
||||
};
|
||||
|
||||
DrawBox(entityGfx.GetAABB(), m_globalMaterial);
|
||||
|
||||
for (std::size_t i = 0; i < entityGfx.GetAttachedRenderableCount(); ++i)
|
||||
{
|
||||
const Nz::BoundingVolumef& boundingVolume = entityGfx.GetBoundingVolume(i);
|
||||
if (boundingVolume.IsFinite())
|
||||
DrawBox(boundingVolume.aabb, m_localMaterial);
|
||||
}
|
||||
}
|
||||
|
||||
std::unique_ptr<InstancedRenderable> Clone() const override
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
private:
|
||||
Nz::MaterialRef m_globalMaterial;
|
||||
Nz::MaterialRef m_localMaterial;
|
||||
};
|
||||
|
||||
class OBBDebugRenderable : public DebugRenderable
|
||||
{
|
||||
public:
|
||||
OBBDebugRenderable(Ndk::Entity* owner, Nz::MaterialRef material, Nz::IndexBufferRef indexBuffer, Nz::VertexBufferRef vertexBuffer) :
|
||||
DebugRenderable(owner, std::move(indexBuffer), std::move(vertexBuffer)),
|
||||
m_material(std::move(material))
|
||||
{
|
||||
}
|
||||
|
||||
void AddToRenderQueue(Nz::AbstractRenderQueue* renderQueue, const InstanceData& instanceData, const Nz::Recti& scissorRect) const override
|
||||
{
|
||||
NazaraAssert(m_entityOwner, "DebugRenderable has no owner");
|
||||
|
||||
const DebugComponent& entityDebug = m_entityOwner->GetComponent<DebugComponent>();
|
||||
const GraphicsComponent& entityGfx = m_entityOwner->GetComponent<GraphicsComponent>();
|
||||
|
||||
auto DrawBox = [&](const Nz::Boxf& box, const Nz::Matrix4f& transformMatrix)
|
||||
{
|
||||
Nz::Matrix4f boxMatrix = Nz::Matrix4f::Identity();
|
||||
boxMatrix.SetScale(box.GetLengths());
|
||||
boxMatrix.SetTranslation(box.GetCenter());
|
||||
boxMatrix.ConcatenateAffine(transformMatrix);
|
||||
|
||||
renderQueue->AddMesh(0, m_material, m_meshData, Nz::Boxf::Zero(), boxMatrix, scissorRect);
|
||||
};
|
||||
|
||||
for (std::size_t i = 0; i < entityGfx.GetAttachedRenderableCount(); ++i)
|
||||
{
|
||||
const Nz::BoundingVolumef& boundingVolume = entityGfx.GetBoundingVolume(i);
|
||||
if (boundingVolume.IsFinite())
|
||||
DrawBox(boundingVolume.obb.localBox, entityGfx.GetTransformMatrix(i));
|
||||
}
|
||||
}
|
||||
|
||||
std::unique_ptr<InstancedRenderable> Clone() const override
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
private:
|
||||
Nz::MaterialRef m_material;
|
||||
};
|
||||
}
|
||||
|
||||
/*!
|
||||
* \ingroup NDK
|
||||
* \class Ndk::DebugSystem
|
||||
* \brief NDK class that represents the debug system
|
||||
*
|
||||
* \remark This system is enabled if the entity owns the trait: DebugComponent and GraphicsComponent
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \brief Constructs an DebugSystem object by default
|
||||
*/
|
||||
DebugSystem::DebugSystem()
|
||||
{
|
||||
Requires<DebugComponent, GraphicsComponent, NodeComponent>();
|
||||
SetUpdateOrder(1000); //< Update last
|
||||
}
|
||||
|
||||
std::pair<Nz::IndexBufferRef, Nz::VertexBufferRef> DebugSystem::GetBoxMesh()
|
||||
{
|
||||
if (!m_boxMeshIndexBuffer)
|
||||
{
|
||||
std::array<Nz::UInt16, 24> indices = {
|
||||
{
|
||||
0, 1,
|
||||
1, 2,
|
||||
2, 3,
|
||||
3, 0,
|
||||
|
||||
4, 5,
|
||||
5, 6,
|
||||
6, 7,
|
||||
7, 4,
|
||||
|
||||
0, 4,
|
||||
1, 5,
|
||||
2, 6,
|
||||
3, 7
|
||||
}
|
||||
};
|
||||
|
||||
m_boxMeshIndexBuffer = Nz::IndexBuffer::New(false, Nz::UInt32(indices.size()), Nz::DataStorage_Hardware, 0);
|
||||
m_boxMeshIndexBuffer->Fill(indices.data(), 0, Nz::UInt32(indices.size()));
|
||||
}
|
||||
|
||||
if (!m_boxMeshVertexBuffer)
|
||||
{
|
||||
Nz::Boxf box(-0.5f, -0.5f, -0.5f, 1.f, 1.f, 1.f);
|
||||
|
||||
std::array<Nz::Vector3f, 8> positions = {
|
||||
{
|
||||
box.GetCorner(Nz::BoxCorner_FarLeftBottom),
|
||||
box.GetCorner(Nz::BoxCorner_NearLeftBottom),
|
||||
box.GetCorner(Nz::BoxCorner_NearRightBottom),
|
||||
box.GetCorner(Nz::BoxCorner_FarRightBottom),
|
||||
box.GetCorner(Nz::BoxCorner_FarLeftTop),
|
||||
box.GetCorner(Nz::BoxCorner_NearLeftTop),
|
||||
box.GetCorner(Nz::BoxCorner_NearRightTop),
|
||||
box.GetCorner(Nz::BoxCorner_FarRightTop)
|
||||
}
|
||||
};
|
||||
|
||||
m_boxMeshVertexBuffer = Nz::VertexBuffer::New(Nz::VertexDeclaration::Get(Nz::VertexLayout_XYZ), Nz::UInt32(positions.size()), Nz::DataStorage_Hardware, 0);
|
||||
m_boxMeshVertexBuffer->Fill(positions.data(), 0, Nz::UInt32(positions.size()));
|
||||
}
|
||||
|
||||
return { m_boxMeshIndexBuffer, m_boxMeshVertexBuffer };
|
||||
}
|
||||
|
||||
void DebugSystem::OnEntityValidation(Entity* entity, bool /*justAdded*/)
|
||||
{
|
||||
static constexpr int DebugDrawOrder = 1'000;
|
||||
|
||||
DebugComponent& entityDebug = entity->GetComponent<DebugComponent>();
|
||||
GraphicsComponent& entityGfx = entity->GetComponent<GraphicsComponent>();
|
||||
NodeComponent& entityNode = entity->GetComponent<NodeComponent>();
|
||||
|
||||
DebugDrawFlags enabledFlags = entityDebug.GetEnabledFlags();
|
||||
DebugDrawFlags flags = entityDebug.GetFlags();
|
||||
|
||||
DebugDrawFlags flagsToEnable = flags & ~enabledFlags;
|
||||
for (std::size_t i = 0; i <= static_cast<std::size_t>(DebugDraw::Max); ++i)
|
||||
{
|
||||
DebugDraw option = static_cast<DebugDraw>(i);
|
||||
if (flagsToEnable & option)
|
||||
{
|
||||
switch (option)
|
||||
{
|
||||
case DebugDraw::Collider3D:
|
||||
{
|
||||
const Nz::Boxf& obb = entityGfx.GetAABB();
|
||||
|
||||
Nz::InstancedRenderableRef renderable = GenerateCollision3DMesh(entity);
|
||||
if (renderable)
|
||||
{
|
||||
renderable->SetPersistent(false);
|
||||
|
||||
entityGfx.Attach(renderable, Nz::Matrix4f::Translate(obb.GetCenter() - entityNode.GetPosition()), DebugDrawOrder);
|
||||
}
|
||||
|
||||
entityDebug.UpdateDebugRenderable(option, std::move(renderable));
|
||||
break;
|
||||
}
|
||||
|
||||
case DebugDraw::GraphicsAABB:
|
||||
{
|
||||
auto indexVertexBuffers = GetBoxMesh();
|
||||
|
||||
Nz::InstancedRenderableRef renderable = new AABBDebugRenderable(entity, GetGlobalAABBMaterial(), GetLocalAABBMaterial(), indexVertexBuffers.first, indexVertexBuffers.second);
|
||||
renderable->SetPersistent(false);
|
||||
|
||||
entityGfx.Attach(renderable, Nz::Matrix4f::Identity(), DebugDrawOrder);
|
||||
|
||||
entityDebug.UpdateDebugRenderable(option, std::move(renderable));
|
||||
break;
|
||||
}
|
||||
|
||||
case DebugDraw::GraphicsOBB:
|
||||
{
|
||||
auto indexVertexBuffers = GetBoxMesh();
|
||||
|
||||
Nz::InstancedRenderableRef renderable = new OBBDebugRenderable(entity, GetOBBMaterial(), indexVertexBuffers.first, indexVertexBuffers.second);
|
||||
renderable->SetPersistent(false);
|
||||
|
||||
entityGfx.Attach(renderable, Nz::Matrix4f::Identity(), DebugDrawOrder);
|
||||
|
||||
entityDebug.UpdateDebugRenderable(option, std::move(renderable));
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
DebugDrawFlags flagsToDisable = enabledFlags & ~flags;
|
||||
for (std::size_t i = 0; i <= static_cast<std::size_t>(DebugDraw::Max); ++i)
|
||||
{
|
||||
DebugDraw option = static_cast<DebugDraw>(i);
|
||||
if (flagsToDisable & option)
|
||||
entityGfx.Detach(entityDebug.GetDebugRenderable(option));
|
||||
}
|
||||
|
||||
entityDebug.UpdateEnabledFlags(flags);
|
||||
}
|
||||
|
||||
void DebugSystem::OnUpdate(float elapsedTime)
|
||||
{
|
||||
// Nothing to do
|
||||
}
|
||||
|
||||
Nz::InstancedRenderableRef DebugSystem::GenerateBox(Nz::Boxf box)
|
||||
{
|
||||
Nz::MeshRef mesh = Nz::Mesh::New();
|
||||
mesh->CreateStatic();
|
||||
|
||||
mesh->BuildSubMesh(Nz::Primitive::Box(box.GetLengths()));
|
||||
mesh->SetMaterialCount(1);
|
||||
|
||||
Nz::ModelRef model = Nz::Model::New();
|
||||
model->SetMesh(mesh);
|
||||
model->SetMaterial(0, GetOBBMaterial());
|
||||
|
||||
return model;
|
||||
}
|
||||
|
||||
Nz::InstancedRenderableRef DebugSystem::GenerateCollision3DMesh(Entity* entity)
|
||||
{
|
||||
if (entity->HasComponent<CollisionComponent3D>())
|
||||
{
|
||||
CollisionComponent3D& entityCollision = entity->GetComponent<CollisionComponent3D>();
|
||||
const Nz::Collider3DRef& geom = entityCollision.GetGeom();
|
||||
|
||||
std::vector<Nz::Vector3f> vertices;
|
||||
std::vector<std::size_t> indices;
|
||||
|
||||
geom->ForEachPolygon([&](const float* polygonVertices, std::size_t vertexCount)
|
||||
{
|
||||
std::size_t firstIndex = vertices.size();
|
||||
|
||||
for (std::size_t i = 0; i < vertexCount; ++i)
|
||||
{
|
||||
const float* vertexData = &polygonVertices[i * 3];
|
||||
vertices.emplace_back(vertexData[0], vertexData[1], vertexData[2]);
|
||||
}
|
||||
|
||||
for (std::size_t i = 0; i < vertexCount - 1; ++i)
|
||||
{
|
||||
indices.push_back(firstIndex + i);
|
||||
indices.push_back(firstIndex + i + 1);
|
||||
}
|
||||
|
||||
indices.push_back(firstIndex + vertexCount - 1);
|
||||
indices.push_back(firstIndex);
|
||||
});
|
||||
|
||||
Nz::IndexBufferRef indexBuffer = Nz::IndexBuffer::New(vertices.size() > 0xFFFF, Nz::UInt32(indices.size()), Nz::DataStorage_Hardware, 0);
|
||||
Nz::IndexMapper indexMapper(indexBuffer, Nz::BufferAccess_WriteOnly);
|
||||
|
||||
Nz::IndexIterator indexPtr = indexMapper.begin();
|
||||
for (std::size_t index : indices)
|
||||
*indexPtr++ = static_cast<Nz::UInt32>(index);
|
||||
|
||||
indexMapper.Unmap();
|
||||
|
||||
Nz::VertexBufferRef vertexBuffer = Nz::VertexBuffer::New(Nz::VertexDeclaration::Get(Nz::VertexLayout_XYZ), Nz::UInt32(vertices.size()), Nz::DataStorage_Hardware, 0);
|
||||
vertexBuffer->Fill(vertices.data(), 0, Nz::UInt32(vertices.size()));
|
||||
|
||||
Nz::MeshRef mesh = Nz::Mesh::New();
|
||||
mesh->CreateStatic();
|
||||
|
||||
Nz::StaticMeshRef subMesh = Nz::StaticMesh::New(vertexBuffer, indexBuffer);
|
||||
subMesh->SetPrimitiveMode(Nz::PrimitiveMode_LineList);
|
||||
subMesh->SetMaterialIndex(0);
|
||||
subMesh->GenerateAABB();
|
||||
|
||||
mesh->SetMaterialCount(1);
|
||||
mesh->AddSubMesh(subMesh);
|
||||
|
||||
Nz::ModelRef model = Nz::Model::New();
|
||||
model->SetMesh(mesh);
|
||||
model->SetMaterial(0, GetCollisionMaterial());
|
||||
|
||||
return model;
|
||||
}
|
||||
else
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Nz::MaterialRef DebugSystem::GetGlobalAABBMaterial()
|
||||
{
|
||||
if (!m_globalAabbMaterial)
|
||||
{
|
||||
m_globalAabbMaterial = Nz::Material::New();
|
||||
m_globalAabbMaterial->EnableFaceCulling(false);
|
||||
m_globalAabbMaterial->EnableDepthBuffer(true);
|
||||
m_globalAabbMaterial->SetDiffuseColor(Nz::Color::Orange);
|
||||
m_globalAabbMaterial->SetFaceFilling(Nz::FaceFilling_Line);
|
||||
m_globalAabbMaterial->SetLineWidth(2.f);
|
||||
}
|
||||
|
||||
return m_globalAabbMaterial;
|
||||
}
|
||||
|
||||
Nz::MaterialRef DebugSystem::GetLocalAABBMaterial()
|
||||
{
|
||||
if (!m_localAabbMaterial)
|
||||
{
|
||||
m_localAabbMaterial = Nz::Material::New();
|
||||
m_localAabbMaterial->EnableFaceCulling(false);
|
||||
m_localAabbMaterial->EnableDepthBuffer(true);
|
||||
m_localAabbMaterial->SetDiffuseColor(Nz::Color::Red);
|
||||
m_localAabbMaterial->SetFaceFilling(Nz::FaceFilling_Line);
|
||||
m_localAabbMaterial->SetLineWidth(2.f);
|
||||
}
|
||||
|
||||
return m_localAabbMaterial;
|
||||
}
|
||||
|
||||
Nz::MaterialRef DebugSystem::GetCollisionMaterial()
|
||||
{
|
||||
if (!m_collisionMaterial)
|
||||
{
|
||||
m_collisionMaterial = Nz::Material::New();
|
||||
m_collisionMaterial->EnableFaceCulling(false);
|
||||
m_collisionMaterial->EnableDepthBuffer(true);
|
||||
m_collisionMaterial->SetDiffuseColor(Nz::Color::Blue);
|
||||
m_collisionMaterial->SetFaceFilling(Nz::FaceFilling_Line);
|
||||
m_collisionMaterial->SetLineWidth(2.f);
|
||||
}
|
||||
|
||||
return m_collisionMaterial;
|
||||
}
|
||||
|
||||
Nz::MaterialRef DebugSystem::GetOBBMaterial()
|
||||
{
|
||||
if (!m_obbMaterial)
|
||||
{
|
||||
m_obbMaterial = Nz::Material::New();
|
||||
m_obbMaterial->EnableFaceCulling(false);
|
||||
m_obbMaterial->EnableDepthBuffer(true);
|
||||
m_obbMaterial->SetDiffuseColor(Nz::Color::Green);
|
||||
m_obbMaterial->SetFaceFilling(Nz::FaceFilling_Line);
|
||||
m_obbMaterial->SetLineWidth(2.f);
|
||||
}
|
||||
|
||||
return m_obbMaterial;
|
||||
}
|
||||
|
||||
SystemIndex DebugSystem::systemIndex;
|
||||
}
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
#include <NDK/Systems/PhysicsSystem2D.hpp>
|
||||
#include <Nazara/Physics2D/RigidBody2D.hpp>
|
||||
#include <NDK/World.hpp>
|
||||
#include <NDK/Components/CollisionComponent2D.hpp>
|
||||
#include <NDK/Components/NodeComponent.hpp>
|
||||
#include <NDK/Components/PhysicsComponent2D.hpp>
|
||||
@@ -33,9 +34,108 @@ namespace Ndk
|
||||
|
||||
void PhysicsSystem2D::CreatePhysWorld() const
|
||||
{
|
||||
NazaraAssert(!m_world, "Physics world should not be created twice");
|
||||
NazaraAssert(!m_physWorld, "Physics world should not be created twice");
|
||||
|
||||
m_world = std::make_unique<Nz::PhysWorld2D>();
|
||||
m_physWorld = std::make_unique<Nz::PhysWorld2D>();
|
||||
}
|
||||
|
||||
void PhysicsSystem2D::DebugDraw(const DebugDrawOptions& options, bool drawShapes, bool drawConstraints, bool drawCollisions)
|
||||
{
|
||||
Nz::PhysWorld2D::DebugDrawOptions worldOptions{ options.constraintColor, options.collisionPointColor, options.shapeOutlineColor };
|
||||
|
||||
if (options.colorCallback)
|
||||
{
|
||||
worldOptions.colorCallback = [&options, this](Nz::RigidBody2D& body, std::size_t shapeIndex, void* userdata)
|
||||
{
|
||||
return options.colorCallback(GetEntityFromBody(body), shapeIndex, userdata);
|
||||
};
|
||||
}
|
||||
|
||||
worldOptions.circleCallback = options.circleCallback;
|
||||
worldOptions.dotCallback = options.dotCallback;
|
||||
worldOptions.polygonCallback = options.polygonCallback;
|
||||
worldOptions.segmentCallback = options.segmentCallback;
|
||||
worldOptions.thickSegmentCallback = options.thickSegmentCallback;
|
||||
|
||||
worldOptions.userdata = options.userdata;
|
||||
|
||||
GetPhysWorld().DebugDraw(worldOptions, drawShapes, drawConstraints, drawCollisions);
|
||||
}
|
||||
|
||||
const EntityHandle& PhysicsSystem2D::GetEntityFromBody(const Nz::RigidBody2D& body) const
|
||||
{
|
||||
auto entityId = static_cast<EntityId>(reinterpret_cast<std::uintptr_t>(body.GetUserdata()));
|
||||
|
||||
auto& world = GetWorld();
|
||||
|
||||
NazaraAssert(world.IsEntityIdValid(entityId), "All Bodies of this world must be part of the physics world by using PhysicsComponent");
|
||||
|
||||
return world.GetEntity(entityId);
|
||||
}
|
||||
|
||||
bool PhysicsSystem2D::NearestBodyQuery(const Nz::Vector2f& from, float maxDistance, Nz::UInt32 collisionGroup, Nz::UInt32 categoryMask, Nz::UInt32 collisionMask, EntityHandle* nearestBody)
|
||||
{
|
||||
Nz::RigidBody2D* body;
|
||||
bool res = GetPhysWorld().NearestBodyQuery(from, maxDistance, collisionGroup, categoryMask, collisionMask, &body);
|
||||
|
||||
(*nearestBody) = GetEntityFromBody(*body);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
bool PhysicsSystem2D::NearestBodyQuery(const Nz::Vector2f& from, float maxDistance, Nz::UInt32 collisionGroup, Nz::UInt32 categoryMask, Nz::UInt32 collisionMask, NearestQueryResult* result)
|
||||
{
|
||||
Nz::PhysWorld2D::NearestQueryResult queryResult;
|
||||
bool res = GetPhysWorld().NearestBodyQuery(from, maxDistance, collisionGroup, categoryMask, collisionMask, &queryResult);
|
||||
|
||||
result->nearestBody = GetEntityFromBody(*queryResult.nearestBody);
|
||||
result->closestPoint = std::move(queryResult.closestPoint);
|
||||
result->fraction = std::move(queryResult.fraction);
|
||||
result->distance = queryResult.distance;
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
bool PhysicsSystem2D::RaycastQuery(const Nz::Vector2f& from, const Nz::Vector2f& to, float radius, Nz::UInt32 collisionGroup, Nz::UInt32 categoryMask, Nz::UInt32 collisionMask, std::vector<RaycastHit>* hitInfos)
|
||||
{
|
||||
std::vector<Nz::PhysWorld2D::RaycastHit> queryResult;
|
||||
bool res = GetPhysWorld().RaycastQuery(from, to, radius, collisionGroup, categoryMask, collisionMask, &queryResult);
|
||||
|
||||
for (auto& hitResult : queryResult)
|
||||
{
|
||||
hitInfos->push_back({
|
||||
GetEntityFromBody(*hitResult.nearestBody),
|
||||
std::move(hitResult.hitPos),
|
||||
std::move(hitResult.hitNormal),
|
||||
hitResult.fraction
|
||||
});
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
bool PhysicsSystem2D::RaycastQueryFirst(const Nz::Vector2f& from, const Nz::Vector2f& to, float radius, Nz::UInt32 collisionGroup, Nz::UInt32 categoryMask, Nz::UInt32 collisionMask, RaycastHit* hitInfo)
|
||||
{
|
||||
Nz::PhysWorld2D::RaycastHit queryResult;
|
||||
bool res = GetPhysWorld().RaycastQueryFirst(from, to, radius, collisionGroup, categoryMask, collisionMask, &queryResult);
|
||||
|
||||
hitInfo->body = GetEntityFromBody(*queryResult.nearestBody);
|
||||
hitInfo->hitPos = std::move(queryResult.hitPos);
|
||||
hitInfo->hitNormal = std::move(queryResult.hitNormal);
|
||||
hitInfo->fraction = queryResult.fraction;
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
void PhysicsSystem2D::RegionQuery(const Nz::Rectf& boundingBox, Nz::UInt32 collisionGroup, Nz::UInt32 categoryMask, Nz::UInt32 collisionMask, std::vector<EntityHandle>* bodies)
|
||||
{
|
||||
std::vector<Nz::RigidBody2D*> queryResult;
|
||||
GetPhysWorld().RegionQuery(boundingBox, collisionGroup, categoryMask, collisionMask, &queryResult);
|
||||
|
||||
for (auto& body : queryResult)
|
||||
{
|
||||
bodies->emplace_back(GetEntityFromBody(*body));
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -47,18 +147,34 @@ namespace Ndk
|
||||
|
||||
void PhysicsSystem2D::OnEntityValidation(Entity* entity, bool justAdded)
|
||||
{
|
||||
// It's possible our entity got revalidated because of the addition/removal of a PhysicsComponent3D
|
||||
if (!justAdded)
|
||||
if (entity->HasComponent<PhysicsComponent2D>())
|
||||
{
|
||||
// We take the opposite array from which the entity should belong to
|
||||
auto& entities = (entity->HasComponent<PhysicsComponent2D>()) ? m_staticObjects : m_dynamicObjects;
|
||||
entities.Remove(entity);
|
||||
if (entity->GetComponent<PhysicsComponent2D>().IsNodeSynchronizationEnabled())
|
||||
m_dynamicObjects.Insert(entity);
|
||||
else
|
||||
m_dynamicObjects.Remove(entity);
|
||||
|
||||
m_staticObjects.Remove(entity);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_dynamicObjects.Remove(entity);
|
||||
m_staticObjects.Insert(entity);
|
||||
|
||||
// If entities just got added to the system, teleport them to their NodeComponent position/rotation
|
||||
// This will prevent the physics engine to mess with the scene while correcting position/rotation
|
||||
if (justAdded)
|
||||
{
|
||||
auto& collision = entity->GetComponent<CollisionComponent2D>();
|
||||
auto& node = entity->GetComponent<NodeComponent>();
|
||||
|
||||
Nz::RigidBody2D* physObj = collision.GetStaticBody();
|
||||
physObj->SetPosition(Nz::Vector2f(node.GetPosition()));
|
||||
//physObj->SetRotation(node.GetRotation());
|
||||
}
|
||||
}
|
||||
|
||||
auto& entities = (entity->HasComponent<PhysicsComponent2D>()) ? m_dynamicObjects : m_staticObjects;
|
||||
entities.Insert(entity);
|
||||
|
||||
if (!m_world)
|
||||
if (!m_physWorld)
|
||||
CreatePhysWorld();
|
||||
}
|
||||
|
||||
@@ -70,10 +186,10 @@ namespace Ndk
|
||||
|
||||
void PhysicsSystem2D::OnUpdate(float elapsedTime)
|
||||
{
|
||||
if (!m_world)
|
||||
if (!m_physWorld)
|
||||
return;
|
||||
|
||||
m_world->Step(elapsedTime);
|
||||
m_physWorld->Step(elapsedTime);
|
||||
|
||||
for (const Ndk::EntityHandle& entity : m_dynamicObjects)
|
||||
{
|
||||
@@ -81,7 +197,7 @@ namespace Ndk
|
||||
PhysicsComponent2D& phys = entity->GetComponent<PhysicsComponent2D>();
|
||||
|
||||
Nz::RigidBody2D* body = phys.GetRigidBody();
|
||||
node.SetRotation(Nz::EulerAnglesf(0.f, 0.f, body->GetRotation()), Nz::CoordSys_Global);
|
||||
node.SetRotation(body->GetRotation(), Nz::CoordSys_Global);
|
||||
node.SetPosition(Nz::Vector3f(body->GetPosition(), node.GetPosition(Nz::CoordSys_Global).z), Nz::CoordSys_Global);
|
||||
}
|
||||
|
||||
@@ -124,5 +240,87 @@ namespace Ndk
|
||||
}
|
||||
}
|
||||
|
||||
void PhysicsSystem2D::RegisterCallbacks(unsigned int collisionId, Callback callbacks)
|
||||
{
|
||||
Nz::PhysWorld2D::Callback worldCallbacks;
|
||||
|
||||
if (callbacks.endCallback)
|
||||
{
|
||||
worldCallbacks.endCallback = [this, cb = std::move(callbacks.endCallback)](Nz::PhysWorld2D& world, Nz::Arbiter2D& arbiter, Nz::RigidBody2D& bodyA, Nz::RigidBody2D& bodyB, void* userdata)
|
||||
{
|
||||
cb(*this, arbiter, GetEntityFromBody(bodyA), GetEntityFromBody(bodyB), userdata);
|
||||
};
|
||||
}
|
||||
|
||||
if (callbacks.preSolveCallback)
|
||||
{
|
||||
worldCallbacks.preSolveCallback = [this, cb = std::move(callbacks.preSolveCallback)](Nz::PhysWorld2D& world, Nz::Arbiter2D& arbiter, Nz::RigidBody2D& bodyA, Nz::RigidBody2D& bodyB, void* userdata)
|
||||
{
|
||||
return cb(*this, arbiter, GetEntityFromBody(bodyA), GetEntityFromBody(bodyB), userdata);
|
||||
};
|
||||
}
|
||||
|
||||
if (callbacks.postSolveCallback)
|
||||
{
|
||||
worldCallbacks.postSolveCallback = [this, cb = std::move(callbacks.postSolveCallback)](Nz::PhysWorld2D& world, Nz::Arbiter2D& arbiter, Nz::RigidBody2D& bodyA, Nz::RigidBody2D& bodyB, void* userdata)
|
||||
{
|
||||
cb(*this, arbiter, GetEntityFromBody(bodyA), GetEntityFromBody(bodyB), userdata);
|
||||
};
|
||||
}
|
||||
|
||||
if (callbacks.startCallback)
|
||||
{
|
||||
worldCallbacks.startCallback = [this, cb = std::move(callbacks.startCallback)](Nz::PhysWorld2D& world, Nz::Arbiter2D& arbiter, Nz::RigidBody2D& bodyA, Nz::RigidBody2D& bodyB, void* userdata)
|
||||
{
|
||||
return cb(*this, arbiter, GetEntityFromBody(bodyA), GetEntityFromBody(bodyB), userdata);
|
||||
};
|
||||
}
|
||||
|
||||
worldCallbacks.userdata = callbacks.userdata;
|
||||
|
||||
m_physWorld->RegisterCallbacks(collisionId, worldCallbacks);
|
||||
}
|
||||
|
||||
void PhysicsSystem2D::RegisterCallbacks(unsigned int collisionIdA, unsigned int collisionIdB, Callback callbacks)
|
||||
{
|
||||
Nz::PhysWorld2D::Callback worldCallbacks{};
|
||||
|
||||
if (callbacks.endCallback)
|
||||
{
|
||||
worldCallbacks.endCallback = [this, cb = std::move(callbacks.endCallback)](Nz::PhysWorld2D& world, Nz::Arbiter2D& arbiter, Nz::RigidBody2D& bodyA, Nz::RigidBody2D& bodyB, void* userdata)
|
||||
{
|
||||
cb(*this, arbiter, GetEntityFromBody(bodyA), GetEntityFromBody(bodyB), userdata);
|
||||
};
|
||||
}
|
||||
|
||||
if (callbacks.preSolveCallback)
|
||||
{
|
||||
worldCallbacks.preSolveCallback = [this, cb = std::move(callbacks.preSolveCallback)](Nz::PhysWorld2D& world, Nz::Arbiter2D& arbiter, Nz::RigidBody2D& bodyA, Nz::RigidBody2D& bodyB, void* userdata)
|
||||
{
|
||||
return cb(*this, arbiter, GetEntityFromBody(bodyA), GetEntityFromBody(bodyB), userdata);
|
||||
};
|
||||
}
|
||||
|
||||
if (callbacks.postSolveCallback)
|
||||
{
|
||||
worldCallbacks.postSolveCallback = [this, cb = std::move(callbacks.postSolveCallback)](Nz::PhysWorld2D& world, Nz::Arbiter2D& arbiter, Nz::RigidBody2D& bodyA, Nz::RigidBody2D& bodyB, void* userdata)
|
||||
{
|
||||
cb(*this, arbiter, GetEntityFromBody(bodyA), GetEntityFromBody(bodyB), userdata);
|
||||
};
|
||||
}
|
||||
|
||||
if (callbacks.startCallback)
|
||||
{
|
||||
worldCallbacks.startCallback = [this, cb = std::move(callbacks.startCallback)](Nz::PhysWorld2D& world, Nz::Arbiter2D& arbiter, Nz::RigidBody2D& bodyA, Nz::RigidBody2D& bodyB, void* userdata)
|
||||
{
|
||||
return cb(*this, arbiter, GetEntityFromBody(bodyA), GetEntityFromBody(bodyB), userdata);
|
||||
};
|
||||
}
|
||||
|
||||
worldCallbacks.userdata = callbacks.userdata;
|
||||
|
||||
m_physWorld->RegisterCallbacks(collisionIdA, collisionIdB, worldCallbacks);
|
||||
}
|
||||
|
||||
SystemIndex PhysicsSystem2D::systemIndex;
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <Nazara/Graphics/ColorBackground.hpp>
|
||||
#include <Nazara/Graphics/ForwardRenderTechnique.hpp>
|
||||
#include <Nazara/Graphics/SceneData.hpp>
|
||||
#include <Nazara/Graphics/SkinningManager.hpp>
|
||||
#include <Nazara/Graphics/SkyboxBackground.hpp>
|
||||
#include <Nazara/Math/Rect.hpp>
|
||||
#include <Nazara/Renderer/Renderer.hpp>
|
||||
@@ -34,7 +35,8 @@ namespace Ndk
|
||||
RenderSystem::RenderSystem() :
|
||||
m_coordinateSystemMatrix(Nz::Matrix4f::Identity()),
|
||||
m_coordinateSystemInvalidated(true),
|
||||
m_forceRenderQueueInvalidation(false)
|
||||
m_forceRenderQueueInvalidation(false),
|
||||
m_isCullingEnabled(true)
|
||||
{
|
||||
ChangeRenderTechnique<Nz::ForwardRenderTechnique>();
|
||||
SetDefaultBackground(Nz::ColorBackground::New());
|
||||
@@ -183,6 +185,8 @@ namespace Ndk
|
||||
m_coordinateSystemInvalidated = false;
|
||||
}
|
||||
|
||||
Nz::SkinningManager::Skin();
|
||||
|
||||
UpdateDynamicReflections();
|
||||
UpdatePointSpotShadowMaps();
|
||||
|
||||
@@ -194,27 +198,36 @@ namespace Ndk
|
||||
|
||||
Nz::AbstractRenderQueue* renderQueue = m_renderTechnique->GetRenderQueue();
|
||||
|
||||
// To make sure the bounding volume used by the culling list is updated
|
||||
// To make sure the bounding volumes used by the culling list is updated
|
||||
for (const Ndk::EntityHandle& drawable : m_drawables)
|
||||
{
|
||||
GraphicsComponent& graphicsComponent = drawable->GetComponent<GraphicsComponent>();
|
||||
graphicsComponent.EnsureBoundingVolumeUpdate();
|
||||
graphicsComponent.EnsureBoundingVolumesUpdate();
|
||||
}
|
||||
|
||||
bool forceInvalidation = false;
|
||||
|
||||
std::size_t visibilityHash = m_drawableCulling.Cull(camComponent.GetFrustum(), &forceInvalidation);
|
||||
const Nz::Frustumf& frustum = camComponent.GetFrustum();
|
||||
|
||||
std::size_t visibilityHash;
|
||||
if (m_isCullingEnabled)
|
||||
visibilityHash = m_drawableCulling.Cull(frustum, &forceInvalidation);
|
||||
else
|
||||
visibilityHash = m_drawableCulling.FillWithAllEntries(&forceInvalidation);
|
||||
|
||||
// Always regenerate renderqueue if particle groups are present for now (FIXME)
|
||||
if (!m_particleGroups.empty())
|
||||
if (!m_lights.empty() || !m_particleGroups.empty())
|
||||
forceInvalidation = true;
|
||||
|
||||
if (camComponent.UpdateVisibility(visibilityHash) || m_forceRenderQueueInvalidation || forceInvalidation)
|
||||
{
|
||||
renderQueue->Clear();
|
||||
for (const GraphicsComponent* gfxComponent : m_drawableCulling)
|
||||
for (const GraphicsComponent* gfxComponent : m_drawableCulling.GetFullyVisibleResults())
|
||||
gfxComponent->AddToRenderQueue(renderQueue);
|
||||
|
||||
for (const GraphicsComponent* gfxComponent : m_drawableCulling.GetPartiallyVisibleResults())
|
||||
gfxComponent->AddToRenderQueueByCulling(frustum, renderQueue);
|
||||
|
||||
for (const Ndk::EntityHandle& light : m_lights)
|
||||
{
|
||||
LightComponent& lightComponent = light->GetComponent<LightComponent>();
|
||||
|
||||
122
SDK/src/NDK/Widgets/BoxLayout.cpp
Normal file
122
SDK/src/NDK/Widgets/BoxLayout.cpp
Normal file
@@ -0,0 +1,122 @@
|
||||
// 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 <NDK/Widgets/BoxLayout.hpp>
|
||||
#include <Nazara/Core/Log.hpp>
|
||||
#include <Nazara/Core/MemoryHelper.hpp>
|
||||
#include <cassert>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
void BoxLayout::Layout()
|
||||
{
|
||||
std::size_t axis1, axis2;
|
||||
|
||||
switch (m_orientation)
|
||||
{
|
||||
case BoxLayoutOrientation_Horizontal:
|
||||
axis1 = 0; //< x
|
||||
axis2 = 1; //< y
|
||||
break;
|
||||
|
||||
case BoxLayoutOrientation_Vertical:
|
||||
axis1 = 1; //< y
|
||||
axis2 = 0; //< x
|
||||
break;
|
||||
|
||||
default:
|
||||
assert(false);
|
||||
break;
|
||||
}
|
||||
|
||||
m_childInfos.clear();
|
||||
|
||||
// Handle size
|
||||
ForEachWidgetChild([&](BaseWidget* child)
|
||||
{
|
||||
if (!child->IsVisible())
|
||||
return;
|
||||
|
||||
m_childInfos.emplace_back();
|
||||
auto& info = m_childInfos.back();
|
||||
info.isConstrained = false;
|
||||
info.maximumSize = child->GetMaximumSize()[axis1];
|
||||
info.minimumSize = child->GetMinimumSize()[axis1];
|
||||
info.size = info.minimumSize;
|
||||
info.widget = child;
|
||||
});
|
||||
|
||||
Nz::Vector2f layoutSize = GetSize();
|
||||
|
||||
float availableSpace = layoutSize[axis1] - m_spacing * (m_childInfos.size() - 1);
|
||||
float remainingSize = availableSpace;
|
||||
for (auto& info : m_childInfos)
|
||||
remainingSize -= info.minimumSize;
|
||||
|
||||
// Okay this algorithm is FAR from perfect but I couldn't figure a way other than this one
|
||||
std::size_t unconstrainedChildCount = m_childInfos.size();
|
||||
|
||||
bool hasUnconstrainedChilds = false;
|
||||
for (std::size_t i = 0; i < m_childInfos.size(); ++i)
|
||||
{
|
||||
if (remainingSize <= 0.0001f)
|
||||
break;
|
||||
|
||||
float evenSize = remainingSize / unconstrainedChildCount;
|
||||
|
||||
for (auto& info : m_childInfos)
|
||||
{
|
||||
if (info.isConstrained)
|
||||
continue;
|
||||
|
||||
float previousSize = info.size;
|
||||
|
||||
info.size += evenSize;
|
||||
if (info.size > info.maximumSize)
|
||||
{
|
||||
unconstrainedChildCount--;
|
||||
|
||||
evenSize += (info.size - info.maximumSize) / unconstrainedChildCount;
|
||||
info.isConstrained = true;
|
||||
info.size = info.maximumSize;
|
||||
}
|
||||
else
|
||||
hasUnconstrainedChilds = true;
|
||||
|
||||
remainingSize -= info.size - previousSize;
|
||||
}
|
||||
|
||||
if (!hasUnconstrainedChilds)
|
||||
break;
|
||||
}
|
||||
|
||||
float spacing = m_spacing + remainingSize / (m_childInfos.size() - 1);
|
||||
|
||||
for (auto& info : m_childInfos)
|
||||
{
|
||||
Nz::Vector2f newSize = info.widget->GetSize();
|
||||
newSize[axis1] = info.size;
|
||||
|
||||
info.widget->Resize(newSize);
|
||||
}
|
||||
|
||||
// Handle position
|
||||
float cursor = 0.f;
|
||||
bool first = true;
|
||||
for (auto& info : m_childInfos)
|
||||
{
|
||||
if (first)
|
||||
first = false;
|
||||
else
|
||||
cursor += spacing;
|
||||
|
||||
Nz::Vector2f position = Nz::Vector2f(0.f, 0.f);
|
||||
position[axis1] = cursor;
|
||||
|
||||
info.widget->SetPosition(position);
|
||||
|
||||
cursor += info.size;
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -30,13 +30,13 @@ namespace Ndk
|
||||
m_gradientSprite->SetCornerColor(Nz::RectCorner_RightBottom, m_cornerColor);
|
||||
m_gradientSprite->SetMaterial(Nz::Material::New("Basic2D"));
|
||||
|
||||
m_gradientEntity = CreateEntity(false);
|
||||
m_gradientEntity = CreateEntity();
|
||||
m_gradientEntity->AddComponent<NodeComponent>().SetParent(this);
|
||||
m_gradientEntity->AddComponent<GraphicsComponent>().Attach(m_gradientSprite);
|
||||
|
||||
m_textSprite = Nz::TextSprite::New();
|
||||
|
||||
m_textEntity = CreateEntity(true);
|
||||
m_textEntity = CreateEntity();
|
||||
m_textEntity->AddComponent<NodeComponent>().SetParent(this);
|
||||
m_textEntity->AddComponent<GraphicsComponent>().Attach(m_textSprite, 1);
|
||||
|
||||
@@ -73,22 +73,15 @@ namespace Ndk
|
||||
return s_pressCornerColor;
|
||||
}
|
||||
|
||||
void ButtonWidget::ResizeToContent()
|
||||
{
|
||||
SetContentSize(Nz::Vector2f(m_textSprite->GetBoundingVolume().obb.localBox.GetLengths()));
|
||||
}
|
||||
|
||||
void ButtonWidget::Layout()
|
||||
{
|
||||
BaseWidget::Layout();
|
||||
|
||||
m_gradientSprite->SetSize(GetSize());
|
||||
Nz::Vector2f size = GetSize();
|
||||
m_gradientSprite->SetSize(size);
|
||||
|
||||
Nz::Vector2f origin = GetContentOrigin();
|
||||
const Nz::Vector2f& contentSize = GetContentSize();
|
||||
|
||||
Nz::Boxf textBox = m_textEntity->GetComponent<GraphicsComponent>().GetBoundingVolume().obb.localBox;
|
||||
m_textEntity->GetComponent<NodeComponent>().SetPosition(origin.x + contentSize.x / 2 - textBox.width / 2, origin.y + contentSize.y / 2 - textBox.height / 2);
|
||||
Nz::Boxf textBox = m_textEntity->GetComponent<GraphicsComponent>().GetAABB();
|
||||
m_textEntity->GetComponent<NodeComponent>().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)
|
||||
|
||||
@@ -28,19 +28,19 @@ namespace Ndk
|
||||
m_checkboxContentSprite = Nz::Sprite::New(Nz::Material::New("Translucent2D"));
|
||||
m_textSprite = Nz::TextSprite::New();
|
||||
|
||||
m_checkboxBorderEntity = CreateEntity(false);
|
||||
m_checkboxBorderEntity = CreateEntity();
|
||||
m_checkboxBorderEntity->AddComponent<NodeComponent>().SetParent(this);
|
||||
m_checkboxBorderEntity->AddComponent<GraphicsComponent>().Attach(m_checkboxBorderSprite);
|
||||
|
||||
m_checkboxBackgroundEntity = CreateEntity(false);
|
||||
m_checkboxBackgroundEntity = CreateEntity();
|
||||
m_checkboxBackgroundEntity->AddComponent<NodeComponent>().SetParent(this);
|
||||
m_checkboxBackgroundEntity->AddComponent<GraphicsComponent>().Attach(m_checkboxBackgroundSprite, 1);
|
||||
|
||||
m_checkboxContentEntity = CreateEntity(true);
|
||||
m_checkboxContentEntity = CreateEntity();
|
||||
m_checkboxContentEntity->AddComponent<NodeComponent>().SetParent(this);
|
||||
m_checkboxContentEntity->AddComponent<GraphicsComponent>().Attach(m_checkboxContentSprite, 2);
|
||||
|
||||
m_textEntity = CreateEntity(true);
|
||||
m_textEntity = CreateEntity();
|
||||
m_textEntity->AddComponent<NodeComponent>().SetParent(this);
|
||||
m_textEntity->AddComponent<GraphicsComponent>().Attach(m_textSprite);
|
||||
|
||||
@@ -57,14 +57,14 @@ namespace Ndk
|
||||
#include <NDK/Resources/checkmark.png.h>
|
||||
};
|
||||
|
||||
Nz::TextureRef checkmarkTexture = Nz::Texture::New();
|
||||
if (!checkmarkTexture->LoadFromMemory(r_checkmark, sizeof(r_checkmark) / sizeof(r_checkmark[0])))
|
||||
Nz::TextureRef checkmarkTexture = Nz::Texture::LoadFromMemory(r_checkmark, sizeof(r_checkmark) / sizeof(r_checkmark[0]));
|
||||
if (!checkmarkTexture)
|
||||
{
|
||||
NazaraError("Failed to load embedded checkmark");
|
||||
return false;
|
||||
}
|
||||
|
||||
Nz::TextureLibrary::Register("Ndk::CheckboxWidget::checkmark", checkmarkTexture);
|
||||
Nz::TextureLibrary::Register("Ndk::CheckboxWidget::checkmark", std::move(checkmarkTexture));
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -108,33 +108,20 @@ namespace Ndk
|
||||
return m_state;
|
||||
}
|
||||
|
||||
void CheckboxWidget::ResizeToContent()
|
||||
{
|
||||
Nz::Vector3f textSize = m_textSprite->GetBoundingVolume().obb.localBox.GetLengths();
|
||||
Nz::Vector2f checkboxSize = GetCheckboxSize();
|
||||
|
||||
Nz::Vector2f finalSize { checkboxSize.x + (m_adaptativeMargin ? checkboxSize.x / 2.f : m_textMargin) + textSize.x, std::max(textSize.y, checkboxSize.y) };
|
||||
SetContentSize(finalSize);
|
||||
}
|
||||
|
||||
void CheckboxWidget::Layout()
|
||||
{
|
||||
BaseWidget::Layout();
|
||||
|
||||
Nz::Vector2f origin = GetContentOrigin();
|
||||
Nz::Vector2f checkboxSize = GetCheckboxSize();
|
||||
Nz::Vector2f borderSize = GetCheckboxBorderSize();
|
||||
|
||||
m_checkboxBorderEntity->GetComponent<NodeComponent>().SetPosition(origin);
|
||||
m_checkboxBackgroundEntity->GetComponent<NodeComponent>().SetPosition(origin + borderSize);
|
||||
m_checkboxBackgroundEntity->GetComponent<NodeComponent>().SetPosition(borderSize);
|
||||
|
||||
Nz::Vector3f checkboxBox = m_checkboxContentSprite->GetBoundingVolume().obb.localBox.GetLengths();
|
||||
m_checkboxContentEntity->GetComponent<NodeComponent>().SetPosition(origin.x + checkboxSize.x / 2.f - checkboxBox.x / 2.f,
|
||||
origin.y + checkboxSize.y / 2.f - checkboxBox.y / 2.f);
|
||||
m_checkboxContentEntity->GetComponent<NodeComponent>().SetPosition(checkboxSize.x / 2.f - checkboxBox.x / 2.f, checkboxSize.y / 2.f - checkboxBox.y / 2.f);
|
||||
|
||||
Nz::Vector3f textBox = m_textSprite->GetBoundingVolume().obb.localBox.GetLengths();
|
||||
m_textEntity->GetComponent<NodeComponent>().SetPosition(origin.x + checkboxSize.x + (m_adaptativeMargin ? checkboxSize.x / 2.f : m_textMargin),
|
||||
origin.y + checkboxSize.y / 2.f - textBox.y / 2.f);
|
||||
m_textEntity->GetComponent<NodeComponent>().SetPosition(checkboxSize.x + (m_adaptativeMargin ? checkboxSize.x / 2.f : m_textMargin), checkboxSize.y / 2.f - textBox.y / 2.f);
|
||||
}
|
||||
|
||||
void CheckboxWidget::OnMouseButtonRelease(int x, int y, Nz::Mouse::Button button)
|
||||
@@ -178,4 +165,14 @@ namespace Ndk
|
||||
m_checkboxContentSprite->SetTexture(Nz::TextureRef {});
|
||||
}
|
||||
}
|
||||
|
||||
void CheckboxWidget::UpdateSize()
|
||||
{
|
||||
Nz::Vector3f textSize = m_textSprite->GetBoundingVolume().obb.localBox.GetLengths();
|
||||
Nz::Vector2f checkboxSize = GetCheckboxSize();
|
||||
|
||||
Nz::Vector2f finalSize { checkboxSize.x + (m_adaptativeMargin ? checkboxSize.x / 2.f : m_textMargin) + textSize.x, std::max(textSize.y, checkboxSize.y) };
|
||||
SetMinimumSize(finalSize);
|
||||
SetPreferredSize(finalSize);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ namespace Ndk
|
||||
ImageWidget::ImageWidget(BaseWidget* parent) :
|
||||
BaseWidget(parent)
|
||||
{
|
||||
m_entity = CreateEntity(true);
|
||||
m_entity = CreateEntity();
|
||||
m_entity->AddComponent<NodeComponent>();
|
||||
auto& gfx = m_entity->AddComponent<GraphicsComponent>();
|
||||
|
||||
@@ -19,19 +19,10 @@ namespace Ndk
|
||||
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);
|
||||
m_sprite->SetSize(GetSize());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,22 +13,10 @@ namespace Ndk
|
||||
{
|
||||
m_textSprite = Nz::TextSprite::New();
|
||||
|
||||
m_textEntity = CreateEntity(true);
|
||||
m_textEntity = CreateEntity();
|
||||
m_textEntity->AddComponent<GraphicsComponent>().Attach(m_textSprite);
|
||||
m_textEntity->AddComponent<NodeComponent>().SetParent(this);
|
||||
|
||||
Layout();
|
||||
}
|
||||
|
||||
void LabelWidget::Layout()
|
||||
{
|
||||
BaseWidget::Layout();
|
||||
|
||||
m_textEntity->GetComponent<NodeComponent>().SetPosition(GetContentOrigin());
|
||||
}
|
||||
|
||||
void LabelWidget::ResizeToContent()
|
||||
{
|
||||
SetContentSize(Nz::Vector2f(m_textSprite->GetBoundingVolume().obb.localBox.GetLengths()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,11 +30,11 @@ namespace Ndk
|
||||
SetBarColor(s_barColor, s_barCornerColor);
|
||||
|
||||
|
||||
m_borderEntity = CreateEntity(false);
|
||||
m_borderEntity = CreateEntity();
|
||||
m_borderEntity->AddComponent<NodeComponent>().SetParent(this);
|
||||
m_borderEntity->AddComponent<GraphicsComponent>().Attach(m_borderSprite);
|
||||
|
||||
m_barEntity = CreateEntity(true);
|
||||
m_barEntity = CreateEntity();
|
||||
m_barEntity->AddComponent<NodeComponent>().SetParent(this);
|
||||
GraphicsComponent& graphics = m_barEntity->AddComponent<GraphicsComponent>();
|
||||
|
||||
@@ -43,7 +43,7 @@ namespace Ndk
|
||||
|
||||
|
||||
m_textSprite = Nz::TextSprite::New();
|
||||
m_textEntity = CreateEntity(true);
|
||||
m_textEntity = CreateEntity();
|
||||
|
||||
m_textEntity->AddComponent<NodeComponent>().SetParent(this);
|
||||
m_textEntity->AddComponent<GraphicsComponent>().Attach(m_textSprite);
|
||||
@@ -76,8 +76,7 @@ namespace Ndk
|
||||
|
||||
void ProgressBarWidget::Layout()
|
||||
{
|
||||
Nz::Vector2f origin = GetContentOrigin();
|
||||
Nz::Vector2f size = GetContentSize();
|
||||
Nz::Vector2f size = GetSize();
|
||||
Nz::Vector2f progressBarSize = size;
|
||||
|
||||
if (IsTextEnabled())
|
||||
@@ -85,7 +84,7 @@ namespace Ndk
|
||||
UpdateText();
|
||||
|
||||
Nz::Vector3f textSize = m_textSprite->GetBoundingVolume().obb.localBox.GetLengths();
|
||||
m_textEntity->GetComponent<NodeComponent>().SetPosition(origin.x + size.x - textSize.x, origin.y + size.y / 2.f - textSize.y);
|
||||
m_textEntity->GetComponent<NodeComponent>().SetPosition(size.x - textSize.x, size.y / 2.f - textSize.y);
|
||||
|
||||
progressBarSize -= { textSize.x + m_textMargin, 0.f };
|
||||
}
|
||||
@@ -96,7 +95,6 @@ namespace Ndk
|
||||
m_barBackgroundSprite->SetSize(progressBarSize - (borderSize * 2.f));
|
||||
m_barSprite->SetSize((progressBarSize.x - (borderSize.x * 2.f)) / 100.f * static_cast<float>(m_value), progressBarSize.y - (borderSize.y * 2.f));
|
||||
|
||||
m_borderEntity->GetComponent<NodeComponent>().SetPosition(origin.x, origin.y);
|
||||
m_barEntity->GetComponent<NodeComponent>().SetPosition(origin.x + borderSize.x, origin.y + borderSize.y);
|
||||
m_barEntity->GetComponent<NodeComponent>().SetPosition(borderSize.x, borderSize.y);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
#include <NDK/Widgets/TextAreaWidget.hpp>
|
||||
#include <Nazara/Core/Unicode.hpp>
|
||||
#include <Nazara/Utility/Font.hpp>
|
||||
#include <NDK/Components/GraphicsComponent.hpp>
|
||||
#include <NDK/Components/NodeComponent.hpp>
|
||||
|
||||
@@ -11,28 +12,32 @@ namespace Ndk
|
||||
{
|
||||
TextAreaWidget::TextAreaWidget(BaseWidget* parent) :
|
||||
BaseWidget(parent),
|
||||
m_characterFilter(),
|
||||
m_echoMode(EchoMode_Normal),
|
||||
m_cursorPosition(0U, 0U),
|
||||
m_cursorPositionBegin(0U, 0U),
|
||||
m_cursorPositionEnd(0U, 0U),
|
||||
m_isMouseButtonDown(false),
|
||||
m_multiLineEnabled(false),
|
||||
m_readOnly(false)
|
||||
m_readOnly(false),
|
||||
m_tabEnabled(false)
|
||||
{
|
||||
m_cursorSprite = Nz::Sprite::New();
|
||||
m_cursorSprite->SetColor(Nz::Color::Black);
|
||||
m_cursorSprite->SetSize(1.f, float(m_drawer.GetFont()->GetSizeInfo(m_drawer.GetCharacterSize()).lineHeight));
|
||||
|
||||
m_cursorEntity = CreateEntity(true);
|
||||
m_cursorEntity->AddComponent<GraphicsComponent>().Attach(m_cursorSprite, 10);
|
||||
m_cursorEntity = CreateEntity();
|
||||
m_cursorEntity->AddComponent<GraphicsComponent>();
|
||||
m_cursorEntity->AddComponent<NodeComponent>().SetParent(this);
|
||||
m_cursorEntity->GetComponent<NodeComponent>().SetPosition(5.f, 3.f);
|
||||
m_cursorEntity->Enable(false);
|
||||
|
||||
m_textSprite = Nz::TextSprite::New();
|
||||
|
||||
m_textEntity = CreateEntity(true);
|
||||
m_textEntity = CreateEntity();
|
||||
m_textEntity->AddComponent<GraphicsComponent>().Attach(m_textSprite);
|
||||
m_textEntity->AddComponent<NodeComponent>().SetParent(this);
|
||||
m_textEntity->GetComponent<NodeComponent>().SetPosition(5.f, 3.f);
|
||||
|
||||
SetCursor(Nz::SystemCursor_Text);
|
||||
SetCharacterSize(GetCharacterSize()); //< Actualize minimum / preferred size
|
||||
|
||||
EnableBackground(true);
|
||||
Layout();
|
||||
}
|
||||
|
||||
@@ -72,7 +77,44 @@ namespace Ndk
|
||||
OnTextChanged(this, m_text);
|
||||
}
|
||||
|
||||
std::size_t TextAreaWidget::GetHoveredGlyph(float x, float y) const
|
||||
void TextAreaWidget::Erase(std::size_t firstGlyph, std::size_t lastGlyph)
|
||||
{
|
||||
if (firstGlyph > lastGlyph)
|
||||
std::swap(firstGlyph, lastGlyph);
|
||||
|
||||
std::size_t textLength = m_text.GetLength();
|
||||
if (firstGlyph > textLength)
|
||||
return;
|
||||
|
||||
Nz::String newText;
|
||||
if (firstGlyph > 0)
|
||||
{
|
||||
std::size_t characterPosition = m_text.GetCharacterPosition(firstGlyph - 1);
|
||||
NazaraAssert(characterPosition != Nz::String::npos, "Invalid character position");
|
||||
|
||||
newText.Append(m_text.SubString(0, characterPosition));
|
||||
}
|
||||
|
||||
if (lastGlyph < textLength)
|
||||
{
|
||||
std::size_t characterPosition = m_text.GetCharacterPosition(lastGlyph);
|
||||
NazaraAssert(characterPosition != Nz::String::npos, "Invalid character position");
|
||||
|
||||
newText.Append(m_text.SubString(characterPosition));
|
||||
}
|
||||
|
||||
SetText(newText);
|
||||
}
|
||||
|
||||
void TextAreaWidget::EraseSelection()
|
||||
{
|
||||
if (!HasSelection())
|
||||
return;
|
||||
|
||||
Erase(GetGlyphIndex(m_cursorPositionBegin), GetGlyphIndex(m_cursorPositionEnd));
|
||||
}
|
||||
|
||||
Nz::Vector2ui TextAreaWidget::GetHoveredGlyph(float x, float y) const
|
||||
{
|
||||
std::size_t glyphCount = m_drawer.GetGlyphCount();
|
||||
if (glyphCount > 0)
|
||||
@@ -88,7 +130,8 @@ namespace Ndk
|
||||
|
||||
std::size_t upperLimit = (line != lineCount - 1) ? m_drawer.GetLine(line + 1).glyphIndex : glyphCount + 1;
|
||||
|
||||
std::size_t i = m_drawer.GetLine(line).glyphIndex;
|
||||
std::size_t firstLineGlyph = m_drawer.GetLine(line).glyphIndex;
|
||||
std::size_t i = firstLineGlyph;
|
||||
for (; i < upperLimit - 1; ++i)
|
||||
{
|
||||
Nz::Rectf bounds = m_drawer.GetGlyph(i).bounds;
|
||||
@@ -96,32 +139,46 @@ namespace Ndk
|
||||
break;
|
||||
}
|
||||
|
||||
return i;
|
||||
return Nz::Vector2ui(i - firstLineGlyph, line);
|
||||
}
|
||||
|
||||
return 0;
|
||||
return Nz::Vector2ui::Zero();
|
||||
}
|
||||
|
||||
void TextAreaWidget::ResizeToContent()
|
||||
void TextAreaWidget::SetCharacterSize(unsigned int characterSize)
|
||||
{
|
||||
SetContentSize(Nz::Vector2f(m_textSprite->GetBoundingVolume().obb.localBox.GetLengths()));
|
||||
m_drawer.SetCharacterSize(characterSize);
|
||||
|
||||
std::size_t fontCount = m_drawer.GetFontCount();
|
||||
unsigned int lineHeight = 0;
|
||||
int spaceAdvance = 0;
|
||||
for (std::size_t i = 0; i < fontCount; ++i)
|
||||
{
|
||||
Nz::Font* font = m_drawer.GetFont(i);
|
||||
|
||||
const Nz::Font::SizeInfo& sizeInfo = font->GetSizeInfo(characterSize);
|
||||
lineHeight = std::max(lineHeight, sizeInfo.lineHeight);
|
||||
spaceAdvance = std::max(spaceAdvance, sizeInfo.spaceAdvance);
|
||||
}
|
||||
|
||||
Nz::Vector2f size = { float(spaceAdvance), float(lineHeight) + 5.f };
|
||||
SetMinimumSize(size);
|
||||
SetPreferredSize({ size.x * 6.f, size.y });
|
||||
}
|
||||
|
||||
void TextAreaWidget::Write(const Nz::String& text)
|
||||
void TextAreaWidget::Write(const Nz::String& text, std::size_t glyphPosition)
|
||||
{
|
||||
std::size_t cursorGlyph = GetGlyphIndex(m_cursorPosition);
|
||||
|
||||
if (cursorGlyph >= m_drawer.GetGlyphCount())
|
||||
if (glyphPosition >= m_drawer.GetGlyphCount())
|
||||
{
|
||||
AppendText(text);
|
||||
SetCursorPosition(m_drawer.GetGlyphCount());
|
||||
}
|
||||
else
|
||||
{
|
||||
m_text.Insert(m_text.GetCharacterPosition(cursorGlyph), text);
|
||||
m_text.Insert(m_text.GetCharacterPosition(glyphPosition), text);
|
||||
SetText(m_text);
|
||||
|
||||
SetCursorPosition(cursorGlyph + text.GetLength());
|
||||
SetCursorPosition(glyphPosition + text.GetLength());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -129,8 +186,6 @@ namespace Ndk
|
||||
{
|
||||
BaseWidget::Layout();
|
||||
|
||||
m_textEntity->GetComponent<NodeComponent>().SetPosition(GetContentOrigin());
|
||||
|
||||
RefreshCursor();
|
||||
}
|
||||
|
||||
@@ -156,20 +211,11 @@ namespace Ndk
|
||||
{
|
||||
case Nz::Keyboard::Delete:
|
||||
{
|
||||
std::size_t cursorGlyph = GetGlyphIndex(m_cursorPosition);
|
||||
if (HasSelection())
|
||||
EraseSelection();
|
||||
else
|
||||
Erase(GetGlyphIndex(m_cursorPositionBegin));
|
||||
|
||||
std::size_t textLength = m_text.GetLength();
|
||||
if (cursorGlyph > textLength)
|
||||
return true;
|
||||
|
||||
Nz::String newText;
|
||||
if (cursorGlyph > 0)
|
||||
newText.Append(m_text.SubString(0, m_text.GetCharacterPosition(cursorGlyph) - 1));
|
||||
|
||||
if (cursorGlyph < textLength)
|
||||
newText.Append(m_text.SubString(m_text.GetCharacterPosition(cursorGlyph + 1)));
|
||||
|
||||
SetText(newText);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -181,10 +227,42 @@ namespace Ndk
|
||||
if (ignoreDefaultAction)
|
||||
return true;
|
||||
|
||||
if (HasSelection())
|
||||
SetCursorPosition(m_cursorPositionEnd);
|
||||
|
||||
MoveCursor({0, 1});
|
||||
return true;
|
||||
}
|
||||
|
||||
case Nz::Keyboard::End:
|
||||
{
|
||||
bool ignoreDefaultAction = false;
|
||||
OnTextAreaKeyEnd(this, &ignoreDefaultAction);
|
||||
|
||||
if (ignoreDefaultAction)
|
||||
return true;
|
||||
|
||||
std::size_t lineCount = m_drawer.GetLineCount();
|
||||
if (key.control && lineCount > 0)
|
||||
SetCursorPosition({ static_cast<unsigned int>(m_drawer.GetLineGlyphCount(lineCount - 1)), static_cast<unsigned int>(lineCount - 1) });
|
||||
else
|
||||
SetCursorPosition({ static_cast<unsigned int>(m_drawer.GetLineGlyphCount(m_cursorPositionEnd.y)), m_cursorPositionEnd.y });
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
case Nz::Keyboard::Home:
|
||||
{
|
||||
bool ignoreDefaultAction = false;
|
||||
OnTextAreaKeyHome(this, &ignoreDefaultAction);
|
||||
|
||||
if (ignoreDefaultAction)
|
||||
return true;
|
||||
|
||||
SetCursorPosition({ 0U, key.control ? 0U : m_cursorPositionEnd.y });
|
||||
return true;
|
||||
}
|
||||
|
||||
case Nz::Keyboard::Left:
|
||||
{
|
||||
bool ignoreDefaultAction = false;
|
||||
@@ -193,7 +271,33 @@ namespace Ndk
|
||||
if (ignoreDefaultAction)
|
||||
return true;
|
||||
|
||||
MoveCursor(-1);
|
||||
if (HasSelection())
|
||||
SetCursorPosition(m_cursorPositionBegin);
|
||||
else if (key.control)
|
||||
{
|
||||
std::size_t index = GetGlyphIndex(m_cursorPositionBegin);
|
||||
|
||||
if (index == 0)
|
||||
return true;
|
||||
|
||||
std::size_t spaceIndex = m_text.FindLast(' ', index - 2);
|
||||
std::size_t endlIndex = m_text.FindLast('\n', index - 1);
|
||||
|
||||
if ((spaceIndex > endlIndex || endlIndex == Nz::String::npos) && spaceIndex != Nz::String::npos)
|
||||
SetCursorPosition(spaceIndex + 1);
|
||||
else if (endlIndex != Nz::String::npos)
|
||||
{
|
||||
if (index == endlIndex + 1)
|
||||
SetCursorPosition(endlIndex);
|
||||
else
|
||||
SetCursorPosition(endlIndex + 1);
|
||||
}
|
||||
else
|
||||
SetCursorPosition({ 0U, m_cursorPositionBegin.y });
|
||||
}
|
||||
else
|
||||
MoveCursor(-1);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -205,7 +309,34 @@ namespace Ndk
|
||||
if (ignoreDefaultAction)
|
||||
return true;
|
||||
|
||||
MoveCursor(1);
|
||||
if (HasSelection())
|
||||
SetCursorPosition(m_cursorPositionEnd);
|
||||
else if (key.control)
|
||||
{
|
||||
std::size_t index = GetGlyphIndex(m_cursorPositionEnd);
|
||||
std::size_t spaceIndex = m_text.Find(' ', index);
|
||||
std::size_t endlIndex = m_text.Find('\n', index);
|
||||
|
||||
if (spaceIndex < endlIndex && spaceIndex != Nz::String::npos)
|
||||
{
|
||||
if (m_text.GetSize() > spaceIndex)
|
||||
SetCursorPosition(spaceIndex + 1);
|
||||
else
|
||||
SetCursorPosition({ static_cast<unsigned int>(m_drawer.GetLineGlyphCount(m_cursorPositionEnd.y)), m_cursorPositionEnd.y });
|
||||
}
|
||||
else if (endlIndex != Nz::String::npos)
|
||||
{
|
||||
if (index == endlIndex)
|
||||
SetCursorPosition(endlIndex + 1);
|
||||
else
|
||||
SetCursorPosition(endlIndex);
|
||||
}
|
||||
else
|
||||
SetCursorPosition({ static_cast<unsigned int>(m_drawer.GetLineGlyphCount(m_cursorPositionEnd.y)), m_cursorPositionEnd.y });
|
||||
}
|
||||
else
|
||||
MoveCursor(1);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -217,10 +348,65 @@ namespace Ndk
|
||||
if (ignoreDefaultAction)
|
||||
return true;
|
||||
|
||||
if (HasSelection())
|
||||
SetCursorPosition(m_cursorPositionBegin);
|
||||
|
||||
MoveCursor({0, -1});
|
||||
return true;
|
||||
}
|
||||
|
||||
case Nz::Keyboard::Tab:
|
||||
{
|
||||
if (!m_tabEnabled)
|
||||
return false;
|
||||
|
||||
if (HasSelection())
|
||||
{
|
||||
for(unsigned line = m_cursorPositionBegin.y; line <= m_cursorPositionEnd.y; ++line)
|
||||
{
|
||||
const Nz::Vector2ui cursorPositionBegin = m_cursorPositionBegin;
|
||||
const Nz::Vector2ui cursorPositionEnd = m_cursorPositionEnd;
|
||||
|
||||
if (key.shift)
|
||||
{
|
||||
if (m_drawer.GetLineGlyphCount(line) == 0)
|
||||
continue;
|
||||
|
||||
std::size_t firstGlyph = GetGlyphIndex({ 0U, line });
|
||||
|
||||
if (m_text[m_text.GetCharacterPosition(firstGlyph)] == '\t')
|
||||
{
|
||||
Erase(firstGlyph);
|
||||
SetSelection(cursorPositionBegin - (cursorPositionBegin.y == line && cursorPositionBegin.x != 0U ? Nz::Vector2ui { 1U, 0U } : Nz::Vector2ui {}),
|
||||
cursorPositionEnd - (cursorPositionEnd.y == line && cursorPositionEnd.x != 0U ? Nz::Vector2ui { 1U, 0U } : Nz::Vector2ui {}));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Write(Nz::String('\t'), { 0U, line });
|
||||
SetSelection(cursorPositionBegin + (cursorPositionBegin.y == line && cursorPositionBegin.x != 0U ? Nz::Vector2ui { 1U, 0U } : Nz::Vector2ui {}),
|
||||
cursorPositionEnd + (cursorPositionEnd.y == line ? Nz::Vector2ui { 1U, 0U } : Nz::Vector2ui {}));
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (key.shift)
|
||||
{
|
||||
std::size_t currentGlyph = GetGlyphIndex(m_cursorPositionBegin);
|
||||
|
||||
if (currentGlyph > 0 && m_text[m_text.GetCharacterPosition(currentGlyph - 1U)] == '\t') // Check if previous glyph is a tab
|
||||
{
|
||||
Erase(currentGlyph - 1U);
|
||||
|
||||
if (m_cursorPositionBegin.x < static_cast<unsigned int>(m_drawer.GetLineGlyphCount(m_cursorPositionBegin.y)))
|
||||
MoveCursor(-1);
|
||||
}
|
||||
}
|
||||
else
|
||||
Write(Nz::String('\t'));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
@@ -236,11 +422,39 @@ namespace Ndk
|
||||
{
|
||||
SetFocus();
|
||||
|
||||
const Padding& padding = GetPadding();
|
||||
SetCursorPosition(GetHoveredGlyph(float(x - padding.left), float(y - padding.top)));
|
||||
Nz::Vector2ui hoveredGlyph = GetHoveredGlyph(float(x) - 5.f, float(y) - 5.f);
|
||||
|
||||
// Shift extends selection
|
||||
if (Nz::Keyboard::IsKeyPressed(Nz::Keyboard::LShift) || Nz::Keyboard::IsKeyPressed(Nz::Keyboard::RShift))
|
||||
SetSelection(hoveredGlyph, m_selectionCursor);
|
||||
else
|
||||
{
|
||||
SetCursorPosition(hoveredGlyph);
|
||||
m_selectionCursor = m_cursorPositionBegin;
|
||||
}
|
||||
|
||||
m_isMouseButtonDown = true;
|
||||
}
|
||||
}
|
||||
|
||||
void TextAreaWidget::OnMouseButtonRelease(int, int, Nz::Mouse::Button button)
|
||||
{
|
||||
if (button == Nz::Mouse::Left)
|
||||
m_isMouseButtonDown = false;
|
||||
}
|
||||
|
||||
void TextAreaWidget::OnMouseEnter()
|
||||
{
|
||||
if (!Nz::Mouse::IsButtonPressed(Nz::Mouse::Left))
|
||||
m_isMouseButtonDown = false;
|
||||
}
|
||||
|
||||
void TextAreaWidget::OnMouseMoved(int x, int y, int deltaX, int deltaY)
|
||||
{
|
||||
if (m_isMouseButtonDown)
|
||||
SetSelection(m_selectionCursor, GetHoveredGlyph(float(x) - 5.f, float(y) - 3.f));
|
||||
}
|
||||
|
||||
void TextAreaWidget::OnTextEntered(char32_t character, bool /*repeated*/)
|
||||
{
|
||||
if (m_readOnly)
|
||||
@@ -253,20 +467,30 @@ namespace Ndk
|
||||
bool ignoreDefaultAction = false;
|
||||
OnTextAreaKeyBackspace(this, &ignoreDefaultAction);
|
||||
|
||||
std::size_t cursorGlyph = GetGlyphIndex(m_cursorPosition);
|
||||
if (ignoreDefaultAction || cursorGlyph == 0)
|
||||
std::size_t cursorGlyphBegin = GetGlyphIndex(m_cursorPositionBegin);
|
||||
std::size_t cursorGlyphEnd = GetGlyphIndex(m_cursorPositionEnd);
|
||||
|
||||
if (ignoreDefaultAction || cursorGlyphEnd == 0)
|
||||
break;
|
||||
|
||||
Nz::String newText;
|
||||
// When a text is selected, delete key does the same as delete and leave the character behind it
|
||||
if (HasSelection())
|
||||
EraseSelection();
|
||||
else
|
||||
{
|
||||
Nz::String newText;
|
||||
|
||||
if (cursorGlyph > 1)
|
||||
newText.Append(m_text.SubString(0, m_text.GetCharacterPosition(cursorGlyph - 1) - 1));
|
||||
if (cursorGlyphBegin > 1)
|
||||
newText.Append(m_text.SubString(0, m_text.GetCharacterPosition(cursorGlyphBegin - 1) - 1));
|
||||
|
||||
if (cursorGlyph < m_text.GetLength())
|
||||
newText.Append(m_text.SubString(m_text.GetCharacterPosition(cursorGlyph)));
|
||||
if (cursorGlyphEnd < m_text.GetLength())
|
||||
newText.Append(m_text.SubString(m_text.GetCharacterPosition(cursorGlyphEnd)));
|
||||
|
||||
MoveCursor(-1);
|
||||
SetText(newText);
|
||||
// Move cursor before setting text (to prevent SetText to move our cursor)
|
||||
MoveCursor(-1);
|
||||
|
||||
SetText(newText);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -279,15 +503,21 @@ namespace Ndk
|
||||
if (ignoreDefaultAction || !m_multiLineEnabled)
|
||||
break;
|
||||
|
||||
if (HasSelection())
|
||||
EraseSelection();
|
||||
|
||||
Write(Nz::String('\n'));
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
{
|
||||
if (Nz::Unicode::GetCategory(character) == Nz::Unicode::Category_Other_Control)
|
||||
if (Nz::Unicode::GetCategory(character) == Nz::Unicode::Category_Other_Control || (m_characterFilter && !m_characterFilter(character)))
|
||||
break;
|
||||
|
||||
if (HasSelection())
|
||||
EraseSelection();
|
||||
|
||||
Write(Nz::String::Unicode(character));
|
||||
break;
|
||||
}
|
||||
@@ -299,24 +529,66 @@ namespace Ndk
|
||||
if (m_readOnly)
|
||||
return;
|
||||
|
||||
const auto& lineInfo = m_drawer.GetLine(m_cursorPosition.y);
|
||||
std::size_t cursorGlyph = GetGlyphIndex(m_cursorPosition);
|
||||
|
||||
std::size_t glyphCount = m_drawer.GetGlyphCount();
|
||||
float position;
|
||||
if (glyphCount > 0 && lineInfo.glyphIndex < cursorGlyph)
|
||||
std::size_t selectionLineCount = m_cursorPositionEnd.y - m_cursorPositionBegin.y + 1;
|
||||
std::size_t oldSpriteCount = m_cursorSprites.size();
|
||||
if (m_cursorSprites.size() != selectionLineCount)
|
||||
{
|
||||
const auto& glyph = m_drawer.GetGlyph(std::min(cursorGlyph, glyphCount - 1));
|
||||
position = glyph.bounds.x;
|
||||
if (cursorGlyph >= glyphCount)
|
||||
position += glyph.bounds.width;
|
||||
m_cursorSprites.resize(m_cursorPositionEnd.y - m_cursorPositionBegin.y + 1);
|
||||
for (std::size_t i = oldSpriteCount; i < m_cursorSprites.size(); ++i)
|
||||
{
|
||||
m_cursorSprites[i] = Nz::Sprite::New();
|
||||
m_cursorSprites[i]->SetMaterial(Nz::Material::New("Translucent2D"));
|
||||
}
|
||||
}
|
||||
else
|
||||
position = 0.f;
|
||||
|
||||
Nz::Vector2f contentOrigin = GetContentOrigin();
|
||||
float lineHeight = float(m_drawer.GetFont()->GetSizeInfo(m_drawer.GetCharacterSize()).lineHeight);
|
||||
|
||||
m_cursorEntity->GetComponent<NodeComponent>().SetPosition(contentOrigin.x + position, contentOrigin.y + lineInfo.bounds.y);
|
||||
GraphicsComponent& gfxComponent = m_cursorEntity->GetComponent<GraphicsComponent>();
|
||||
gfxComponent.Clear();
|
||||
|
||||
for (unsigned int i = m_cursorPositionBegin.y; i <= m_cursorPositionEnd.y; ++i)
|
||||
{
|
||||
const auto& lineInfo = m_drawer.GetLine(i);
|
||||
|
||||
Nz::SpriteRef& cursorSprite = m_cursorSprites[i - m_cursorPositionBegin.y];
|
||||
if (i == m_cursorPositionBegin.y || i == m_cursorPositionEnd.y)
|
||||
{
|
||||
auto GetGlyphPos = [&](unsigned int localGlyphPos)
|
||||
{
|
||||
std::size_t cursorGlyph = GetGlyphIndex({ localGlyphPos, i });
|
||||
|
||||
std::size_t glyphCount = m_drawer.GetGlyphCount();
|
||||
float position;
|
||||
if (glyphCount > 0 && lineInfo.glyphIndex < cursorGlyph)
|
||||
{
|
||||
const auto& glyph = m_drawer.GetGlyph(std::min(cursorGlyph, glyphCount - 1));
|
||||
position = glyph.bounds.x;
|
||||
if (cursorGlyph >= glyphCount)
|
||||
position += glyph.bounds.width;
|
||||
}
|
||||
else
|
||||
position = 0.f;
|
||||
|
||||
return position;
|
||||
};
|
||||
|
||||
float beginX = (i == m_cursorPositionBegin.y) ? GetGlyphPos(m_cursorPositionBegin.x) : 0.f;
|
||||
float endX = (i == m_cursorPositionEnd.y) ? GetGlyphPos(m_cursorPositionEnd.x) : lineInfo.bounds.width;
|
||||
float spriteSize = std::max(endX - beginX, 1.f);
|
||||
|
||||
cursorSprite->SetColor((m_cursorPositionBegin == m_cursorPositionEnd) ? Nz::Color::Black : Nz::Color(0, 0, 0, 50));
|
||||
cursorSprite->SetSize(spriteSize, float(m_drawer.GetFont()->GetSizeInfo(m_drawer.GetCharacterSize()).lineHeight));
|
||||
|
||||
gfxComponent.Attach(cursorSprite, Nz::Matrix4f::Translate({ beginX, lineInfo.bounds.y, 0.f }));
|
||||
}
|
||||
else
|
||||
{
|
||||
cursorSprite->SetColor(Nz::Color(0, 0, 0, 50));
|
||||
cursorSprite->SetSize(lineInfo.bounds.width, float(m_drawer.GetFont()->GetSizeInfo(m_drawer.GetCharacterSize()).lineHeight));
|
||||
|
||||
gfxComponent.Attach(cursorSprite, Nz::Matrix4f::Translate({ 0.f, lineInfo.bounds.y, 0.f }));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void TextAreaWidget::UpdateDisplayText()
|
||||
@@ -335,6 +607,6 @@ namespace Ndk
|
||||
|
||||
m_textSprite->Update(m_drawer);
|
||||
|
||||
SetCursorPosition(m_cursorPosition); //< Refresh cursor position (prevent it from being outside of the text)
|
||||
SetCursorPosition(m_cursorPositionBegin); //< Refresh cursor position (prevent it from being outside of the text)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include <NDK/Systems/VelocitySystem.hpp>
|
||||
|
||||
#ifndef NDK_SERVER
|
||||
#include <NDK/Systems/DebugSystem.hpp>
|
||||
#include <NDK/Systems/ListenerSystem.hpp>
|
||||
#include <NDK/Systems/ParticleSystem.hpp>
|
||||
#include <NDK/Systems/RenderSystem.hpp>
|
||||
@@ -47,6 +48,7 @@ namespace Ndk
|
||||
AddSystem<VelocitySystem>();
|
||||
|
||||
#ifndef NDK_SERVER
|
||||
AddSystem<DebugSystem>();
|
||||
AddSystem<ListenerSystem>();
|
||||
AddSystem<ParticleSystem>();
|
||||
AddSystem<RenderSystem>();
|
||||
|
||||
Reference in New Issue
Block a user