Merge branch 'master' into console-widget
This commit is contained in:
@@ -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