From c3582cf5eed1bcc68c50268024ed1209e8f203f5 Mon Sep 17 00:00:00 2001 From: Diren D Bharwani Date: Sat, 7 Jan 2023 16:14:55 +0800 Subject: [PATCH 01/24] Added a rotate method with quaternions for Vector3 --- SHADE_Engine/src/Math/Transform/SHTransform.cpp | 12 +++++++++++- SHADE_Managed/src/Math/Vector3.cxx | 4 ++++ SHADE_Managed/src/Math/Vector3.hxx | 17 +++++++++++++++-- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/SHADE_Engine/src/Math/Transform/SHTransform.cpp b/SHADE_Engine/src/Math/Transform/SHTransform.cpp index ef7c5fda..7be2a60c 100644 --- a/SHADE_Engine/src/Math/Transform/SHTransform.cpp +++ b/SHADE_Engine/src/Math/Transform/SHTransform.cpp @@ -35,7 +35,17 @@ namespace SHADE : position { pos } , orientation { SHQuaternion::FromEuler(rot) } , scale { scl } - {} + { + ComputeTRS(); + } + + SHTransform::SHTransform(const SHVec3& pos, const SHQuaternion& quat, const SHVec3& scl) noexcept + : position { pos } + , orientation { quat } + , scale { scl } + { + ComputeTRS(); + } /*-----------------------------------------------------------------------------------*/ /* Operator Overload Definitions */ diff --git a/SHADE_Managed/src/Math/Vector3.cxx b/SHADE_Managed/src/Math/Vector3.cxx index d2862ac7..647174a2 100644 --- a/SHADE_Managed/src/Math/Vector3.cxx +++ b/SHADE_Managed/src/Math/Vector3.cxx @@ -167,6 +167,10 @@ namespace SHADE { return Convert::ToCLI(SHVec3::Rotate(Convert::ToNative(vec), Convert::ToNative(axis), radians)); } + Vector3 Vector3::Rotate(Vector3 vec, Quaternion quat) + { + return Convert::ToCLI(SHVec3::Rotate(Convert::ToNative(vec), Convert::ToNative(quat))); + } Vector3 Vector3::Min(Vector3 lhs, Vector3 rhs) { float lx = lhs.x, rx = rhs.x; diff --git a/SHADE_Managed/src/Math/Vector3.hxx b/SHADE_Managed/src/Math/Vector3.hxx index 76b11420..31a373ba 100644 --- a/SHADE_Managed/src/Math/Vector3.hxx +++ b/SHADE_Managed/src/Math/Vector3.hxx @@ -19,10 +19,17 @@ of DigiPen Institute of Technology is prohibited. // Project Includes #include "Vector2.hxx" -value struct Quaternion; - namespace SHADE { + /*---------------------------------------------------------------------------------*/ + /* Forward Declarations */ + /*-------------------------------------------------------------------------- --- */ + value struct Quaternion; + + /*---------------------------------------------------------------------------------*/ + /* Type Definitions */ + /*-------------------------------------------------------------------------- --- */ + /// /// CLR version of SHADE Engine's Vector3 class that represents a 3-Dimensional Vector. /// Designed to closely match Unity's Vector3 struct. @@ -308,6 +315,12 @@ namespace SHADE /// The Vector3 that represents the rotated vector. static Vector3 Rotate(Vector3 vec, Vector3 axis, float radians); /// + /// Rotates a Vector3 using a Quaternion. + /// + /// A Vector3 to rotate. + /// A Quaternion to rotate the vector with. + static Vector3 Rotate(Vector3 vec, Quaternion quat); + /// /// Computes and returns a Vector3 that is made from the smallest components of /// the two specified Vector3s. /// From 222bda9a13436cfc71a0fbd8f7043fb5ee4ec686 Mon Sep 17 00:00:00 2001 From: Diren D Bharwani Date: Sat, 7 Jan 2023 16:16:35 +0800 Subject: [PATCH 02/24] Replaced Transform's Forward.get with new rotate method --- SHADE_Managed/src/Components/Transform.cxx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/SHADE_Managed/src/Components/Transform.cxx b/SHADE_Managed/src/Components/Transform.cxx index bc61eff3..d5b38967 100644 --- a/SHADE_Managed/src/Components/Transform.cxx +++ b/SHADE_Managed/src/Components/Transform.cxx @@ -107,8 +107,7 @@ namespace SHADE Vector3 Transform::Forward::get() { - const SHVec3 DIRECTION = SHVec3::Rotate(-SHVec3::UnitZ, Convert::ToNative(GlobalRotation)); - return Convert::ToCLI(DIRECTION); + return Vector3::Rotate(Vector3::Forward, GlobalRotation); } /*-----------------------------------------------------------------------------------*/ From 98bfbc1048fca2e11683ce8947c18d65fc5f561d Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Sun, 8 Jan 2023 01:06:50 +0800 Subject: [PATCH 03/24] Added DrawWireCapsule for debug draw (doesn't support orientation changes yet) --- .../MiddleEnd/Interface/SHDebugDrawSystem.cpp | 58 ++++++++++++++++++ .../MiddleEnd/Interface/SHDebugDrawSystem.h | 23 ++++++++ .../MiddleEnd/Interface/SHGraphicsSystem.cpp | 2 + .../MiddleEnd/Interface/SHGraphicsSystem.h | 5 +- .../MiddleEnd/Meshes/SHPrimitiveGenerator.cpp | 59 +++++++++++++++++++ .../MiddleEnd/Meshes/SHPrimitiveGenerator.h | 41 +++++++++++++ SHADE_Engine/src/Tools/SHDebugDraw.cpp | 8 +++ SHADE_Engine/src/Tools/SHDebugDraw.h | 24 ++++++++ 8 files changed, 218 insertions(+), 2 deletions(-) diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.cpp index b57249de..fdd21fd4 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.cpp @@ -207,6 +207,11 @@ namespace SHADE drawSphere(getMeshBatch(true, depthTested), matrix, color); } + void SHDebugDrawSystem::DrawWireCapsule(const SHVec3& position, const SHQuaternion& rotation, float height, float radius, const SHColour& color, bool depthTested) + { + drawWireCapsule(getLineBatch(depthTested), getMeshBatch(false, depthTested), position, rotation, height, radius, color); + } + /*-----------------------------------------------------------------------------------*/ /* Persistent Draw Functions */ /*-----------------------------------------------------------------------------------*/ @@ -264,6 +269,12 @@ namespace SHADE markPersistentDrawsDirty(); } + void SHDebugDrawSystem::DrawPersistentWireCapsule(const SHVec3& position, const SHQuaternion& rotation, float height, float radius, const SHColour& color, bool depthTested) + { + drawWireCapsule(getPersistentLineBatch(depthTested), getPersistentMeshBatch(false, depthTested), position, rotation, height, radius, color); + markPersistentDrawsDirty(); + } + void SHDebugDrawSystem::ClearPersistentDraws() { for (auto& batch : persistentLineBatches) @@ -348,6 +359,53 @@ namespace SHADE ); } + void SHDebugDrawSystem::drawWireCapsule(LinesBatch& lineBatch, MeshBatch& meshBatch, const SHVec3& position, const SHQuaternion& rotation, float height, float radius, const SHColour& color) + { + // Get local axis vectors + const SHVec3 LOCAL_UP = SHVec3::Up; + const SHVec3 LOCAL_RIGHT = SHVec3::Right; + const SHVec3 LOCAL_FORWARD = SHVec3::Forward; + + // Rotate the circle + SHQuaternion circleOrientation = SHQuaternion::FromEuler(SHVec3(SHMath::DegreesToRadians(90.0), 0.0f, 0.0f)); + + // Compute top and bottom of the cylinder + const SHVec3 HALF_UP = LOCAL_UP * (height * 0.5f - radius); + const SHVec3 TOP_POS = position + HALF_UP; + const SHVec3 BOT_POS = position - HALF_UP; + + // Render circles + const SHVec3 CIRCLE_SCALE = SHVec3(radius * 2.0f, radius * 2.0, radius * 2.0); + drawCircle(meshBatch, SHMatrix::Transform(TOP_POS, circleOrientation, CIRCLE_SCALE), color); + drawCircle(meshBatch, SHMatrix::Transform(BOT_POS, circleOrientation, CIRCLE_SCALE), color); + + // Render connecting lines + drawLine(lineBatch, TOP_POS + LOCAL_RIGHT * radius, BOT_POS + LOCAL_RIGHT * radius, color); + drawLine(lineBatch, TOP_POS - LOCAL_RIGHT * radius, BOT_POS - LOCAL_RIGHT * radius, color); + drawLine(lineBatch, TOP_POS + LOCAL_FORWARD * radius, BOT_POS + LOCAL_FORWARD * radius, color); + drawLine(lineBatch, TOP_POS - LOCAL_FORWARD * radius, BOT_POS - LOCAL_FORWARD * radius, color); + + // Render caps + const SHVec3 RADIUS_SCALE = SHVec3(radius * 2.0, radius * 2.0f, radius * 2.0); + const SHMatrix TOP_CAP_MAT = SHMatrix::Transform(TOP_POS, rotation, RADIUS_SCALE); + drawMesh + ( + gfxSystem->GetMeshPrimitive(PrimitiveType::LineCapsuleCap), + meshBatch, TOP_CAP_MAT, color + ); + const SHMatrix BOT_CAP_MAT = SHMatrix::Transform + ( + BOT_POS, + rotation * SHQuaternion::FromEuler(SHVec3(SHMath::DegreesToRadians(180.0), 0.0f, 0.0f)), + RADIUS_SCALE + ); + drawMesh + ( + gfxSystem->GetMeshPrimitive(PrimitiveType::LineCapsuleCap), + meshBatch, BOT_CAP_MAT, color + ); + } + /*-----------------------------------------------------------------------------------*/ /* Helper Batch Functions - Lines */ /*-----------------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.h index 2978d68e..3997b6df 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.h @@ -163,6 +163,17 @@ namespace SHADE /// Colour to draw with. /// Whether or not drawn object will be occluded. void DrawSphere(const SHMatrix& matrix, const SHColour& color = SHColour::WHITE, bool depthTested = false); + /// + /// Draws the outline of a capsule. + /// + /// Position of the wireframe capsule. + /// Rotation of the capsule. + /// Height of the overall capsule. + /// Radius of the capsule. + /// + /// Colour to draw with. + /// Whether or not drawn object will be occluded. + void DrawWireCapsule(const SHVec3& position, const SHQuaternion& rotation, float height, float radius, const SHColour& color = SHColour::WHITE, bool depthTested = false); /*---------------------------------------------------------------------------------*/ /* Persistent Draw Functions */ @@ -269,6 +280,17 @@ namespace SHADE /// Whether or not drawn object will be occluded. void DrawPersistentSphere(const SHMatrix& matrix, const SHColour& color = SHColour::WHITE, bool depthTested = false); /// + /// Draws a persistent outline of a capsule. + /// + /// Position of the wireframe capsule. + /// Rotation of the capsule. + /// Height of the overall capsule. + /// Radius of the capsule. + /// + /// Colour to draw with. + /// Whether or not drawn object will be occluded. + void DrawPersistentWireCapsule(const SHVec3& position, const SHQuaternion& rotation, float height, float radius, const SHColour& color = SHColour::WHITE, bool depthTested = false); + /// /// Clears any persistent drawn debug primitives. /// void ClearPersistentDraws(); @@ -386,6 +408,7 @@ namespace SHADE void drawCube(MeshBatch& batch, const SHMatrix& transformMatrix, const SHColour& color); void drawSphere(MeshBatch& batch, const SHMatrix& transformMatrix, const SHColour& color); void drawCircle(MeshBatch& batch, const SHMatrix& transformMatrix, const SHColour& color); + void drawWireCapsule(LinesBatch& lineBatch, MeshBatch& meshBatch, const SHVec3& position, const SHQuaternion& rotation, float height, float radius, const SHColour& color); /*---------------------------------------------------------------------------------*/ /* Helper Batch Functions - Lines */ diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp index b1256921..f3bcc8f7 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp @@ -419,6 +419,7 @@ namespace SHADE primitiveMeshes[static_cast(PrimitiveType::Sphere)] = SHPrimitiveGenerator::Sphere(meshLibrary); primitiveMeshes[static_cast(PrimitiveType::LineCube)] = SHPrimitiveGenerator::LineCube(meshLibrary); primitiveMeshes[static_cast(PrimitiveType::LineCircle)] = SHPrimitiveGenerator::LineCircle(meshLibrary); + primitiveMeshes[static_cast(PrimitiveType::LineCapsuleCap)] = SHPrimitiveGenerator::LineCapsuleCap(meshLibrary); BuildMeshBuffers(); // Create default materials @@ -819,6 +820,7 @@ namespace SHADE case PrimitiveType::Sphere: case PrimitiveType::LineCube: case PrimitiveType::LineCircle: + case PrimitiveType::LineCapsuleCap: return primitiveMeshes[static_cast(type)]; default: return {}; diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h index 40148e05..707862f9 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h @@ -72,9 +72,10 @@ namespace SHADE Cube, Sphere, LineCube, - LineCircle + LineCircle, + LineCapsuleCap }; - static constexpr int MAX_PRIMITIVE_TYPES = 4; + static constexpr int MAX_PRIMITIVE_TYPES = 5; enum class DebugDrawPipelineType { LineNoDepthTest, diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.cpp index 444a6630..d5a0073f 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.cpp @@ -29,6 +29,7 @@ namespace SHADE SHMeshData SHPrimitiveGenerator::sphereMesh; SHMeshData SHPrimitiveGenerator::lineCubeMesh; SHMeshData SHPrimitiveGenerator::lineCircleMesh; + SHMeshData SHPrimitiveGenerator::lineCapsuleCapMesh; /*-----------------------------------------------------------------------------------*/ /* Primitive Generation Functions */ @@ -392,6 +393,64 @@ namespace SHADE return addMeshDataTo(lineCircleMesh, gfxSystem); } + SHADE::SHMeshData SHPrimitiveGenerator::LineCapsuleCap() noexcept + { + SHMeshData mesh; + + // Have multiple semi-circles for the cap + static constexpr int SPLITS = 36; + static constexpr float ANGLE_INCREMENTS = (std::numbers::pi_v * 2.0f) / static_cast(SPLITS); + + /* X-Axis */ + // Generate points of the circle + for (int i = 0; i <= SPLITS / 2; ++i) + { + const float ANGLE = ANGLE_INCREMENTS * i; + mesh.VertexPositions.emplace_back(cos(ANGLE) * 0.5f, sin(ANGLE) * 0.5f, 0.0f); + } + + // Generate lines of the circle + for (int i = 1; i <= SPLITS / 2; ++i) + { + mesh.Indices.emplace_back(static_cast(i - 1)); + mesh.Indices.emplace_back(static_cast(i)); + } + + /* Z-Axis */ + // Generate points of the circle + for (int i = 0; i <= SPLITS / 2; ++i) + { + const float ANGLE = ANGLE_INCREMENTS * i; + mesh.VertexPositions.emplace_back(0.0f, sin(ANGLE) * 0.5f, cos(ANGLE) * 0.5f); + } + + // Generate lines of the circle + for (int i = 2 + SPLITS / 2; i <= SPLITS + 1; ++i) + { + mesh.Indices.emplace_back(static_cast(i - 1)); + mesh.Indices.emplace_back(static_cast(i)); + } + + mesh.VertexNormals.resize(mesh.VertexPositions.size()); + mesh.VertexTangents.resize(mesh.VertexPositions.size()); + mesh.VertexTexCoords.resize(mesh.VertexPositions.size()); + + return mesh; + } + Handle SHPrimitiveGenerator::LineCapsuleCap(SHMeshLibrary& meshLibrary) noexcept + { + if (lineCapsuleCapMesh.VertexPositions.empty()) + lineCapsuleCapMesh = LineCapsuleCap(); + + return addMeshDataTo(lineCapsuleCapMesh, meshLibrary); + } + Handle SHPrimitiveGenerator::LineCapsuleCap(SHGraphicsSystem& gfxSystem) noexcept + { + if (lineCapsuleCapMesh.VertexPositions.empty()) + lineCapsuleCapMesh = LineCapsuleCap(); + + return addMeshDataTo(lineCapsuleCapMesh, gfxSystem); + } /*-----------------------------------------------------------------------------------*/ /* Helper Functions */ /*-----------------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.h b/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.h index 9bcd2f3c..e8c7c3fb 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.h @@ -194,6 +194,46 @@ namespace SHADE */ /***********************************************************************************/ [[nodiscard]] static Handle LineCircle(SHGraphicsSystem& gfxSystem) noexcept; + /***********************************************************************************/ + /*! + \brief + Produces a circle that is comprised only of lines with no diagonal lines and + store the data in a SHMeshData object. + + \return + SHMeshData object containing vertex data for the line circle. + */ + /***********************************************************************************/ + [[nodiscard]] static SHMeshData LineCapsuleCap() noexcept; + /***********************************************************************************/ + /*! + \brief + Produces a line circle and constructs a SHMesh using the SHGraphicsSystem + provided. + + \param meshLibrary + Reference to the SHMeshLibrary to produce and store a line circle mesh in. + + \return + SHMesh object that points to the generated line circle mesh in the SHMeshLibrary. + */ + /***********************************************************************************/ + [[nodiscard]] static Handle LineCapsuleCap(SHMeshLibrary& meshLibrary) noexcept; + /***********************************************************************************/ + /*! + \brief + Produces a line circle and constructs a SHMesh using the SHGraphicsSystem + provided. + + \param gfxSystem + Reference to the SHGraphicsSystem to produce and store a line circle mesh in. + + \return + SHMesh object that points to the generated line circle mesh in the + SHGraphicsSystem. + */ + /***********************************************************************************/ + [[nodiscard]] static Handle LineCapsuleCap(SHGraphicsSystem& gfxSystem) noexcept; private: /*---------------------------------------------------------------------------------*/ @@ -209,5 +249,6 @@ namespace SHADE static SHMeshData sphereMesh; static SHMeshData lineCubeMesh; static SHMeshData lineCircleMesh; + static SHMeshData lineCapsuleCapMesh; }; } \ No newline at end of file diff --git a/SHADE_Engine/src/Tools/SHDebugDraw.cpp b/SHADE_Engine/src/Tools/SHDebugDraw.cpp index 02eca592..ee040d8b 100644 --- a/SHADE_Engine/src/Tools/SHDebugDraw.cpp +++ b/SHADE_Engine/src/Tools/SHDebugDraw.cpp @@ -128,6 +128,10 @@ namespace SHADE dbgDrawSys->DrawWireSphere(SHMatrix::Transform(center, SHQuaternion(), scale), color, depthTested); } + void SHDebugDraw::WireCapsule(const SHVec3& position, const SHQuaternion& rotation, float height, float radius, const SHColour& color, bool depthTested) + { + dbgDrawSys->DrawWireCapsule(position, rotation, height, radius, color, depthTested); + } /*-----------------------------------------------------------------------------------*/ /* Persistent Draw Functions */ /*-----------------------------------------------------------------------------------*/ @@ -216,6 +220,10 @@ namespace SHADE dbgDrawSys->DrawPersistentWireSphere(SHMatrix::Transform(center, SHQuaternion(), scale), color, depthTested); } + void SHDebugDraw::Persistent::WireCapsule(const SHVec3& position, const SHQuaternion& rotation, float height, float radius, const SHColour& color, bool depthTested) + { + dbgDrawSys->DrawPersistentWireCapsule(position, rotation, height, radius, color, depthTested); + } void SHDebugDraw::Persistent::ClearDraws() { dbgDrawSys->ClearPersistentDraws(); diff --git a/SHADE_Engine/src/Tools/SHDebugDraw.h b/SHADE_Engine/src/Tools/SHDebugDraw.h index c28b93e6..3d7bee2f 100644 --- a/SHADE_Engine/src/Tools/SHDebugDraw.h +++ b/SHADE_Engine/src/Tools/SHDebugDraw.h @@ -194,6 +194,18 @@ namespace SHADE /// Colour to draw with. /// Whether or not drawn object will be occluded. static void WireSphere(const SHVec3& center, const SHVec3& scale, const SHVec4& color = SHColour::WHITE, bool depthTested = false); + /// + /// Draws the outline of a capsule. + /// This will remain drawn until ClearDraws() is called. + /// + /// Position of the wireframe capsule. + /// Rotation of the capsule. + /// Height of the overall capsule. + /// Radius of the capsule. + /// + /// Colour to draw with. + /// Whether or not drawn object will be occluded. + void WireCapsule(const SHVec3& position, const SHQuaternion& rotation, float height, float radius, const SHColour& color = SHColour::WHITE, bool depthTested = false); /*---------------------------------------------------------------------------------*/ /* Persistent Draw Function Class "Folder" */ @@ -366,6 +378,18 @@ namespace SHADE /// Whether or not drawn object will be occluded. static void WireSphere(const SHVec3& center, const SHVec3& scale, const SHVec4& color = SHColour::WHITE, bool depthTested = false); /// + /// Draws the outline of a capsule. + /// This will remain drawn until ClearDraws() is called. + /// + /// Position of the wireframe capsule. + /// Rotation of the capsule. + /// Height of the overall capsule. + /// Radius of the capsule. + /// + /// Colour to draw with. + /// Whether or not drawn object will be occluded. + void WireCapsule(const SHVec3& position, const SHQuaternion& rotation, float height, float radius, const SHColour& color = SHColour::WHITE, bool depthTested = false); + /// /// Clears any persistent drawn debug primitives. /// static void ClearDraws(); From f44e7b7a1c2a17d41a0047049428192565463e6a Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Sun, 8 Jan 2023 01:43:40 +0800 Subject: [PATCH 04/24] Debug draw capsule now works with different orientations --- .../Graphics/MiddleEnd/Interface/SHDebugDrawSystem.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.cpp index fdd21fd4..ae8c62b2 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.cpp @@ -362,12 +362,12 @@ namespace SHADE void SHDebugDrawSystem::drawWireCapsule(LinesBatch& lineBatch, MeshBatch& meshBatch, const SHVec3& position, const SHQuaternion& rotation, float height, float radius, const SHColour& color) { // Get local axis vectors - const SHVec3 LOCAL_UP = SHVec3::Up; - const SHVec3 LOCAL_RIGHT = SHVec3::Right; - const SHVec3 LOCAL_FORWARD = SHVec3::Forward; + const SHVec3 LOCAL_UP = SHVec3::Rotate(SHVec3::Up, rotation); + const SHVec3 LOCAL_RIGHT = SHVec3::Rotate(SHVec3::Right, rotation); + const SHVec3 LOCAL_FORWARD = SHVec3::Rotate(SHVec3::Forward, rotation); // Rotate the circle - SHQuaternion circleOrientation = SHQuaternion::FromEuler(SHVec3(SHMath::DegreesToRadians(90.0), 0.0f, 0.0f)); + SHQuaternion circleOrientation = SHQuaternion::FromEuler(SHVec3(SHMath::DegreesToRadians(90.0), 0.0f, 0.0f)) * rotation; // Compute top and bottom of the cylinder const SHVec3 HALF_UP = LOCAL_UP * (height * 0.5f - radius); @@ -396,7 +396,7 @@ namespace SHADE const SHMatrix BOT_CAP_MAT = SHMatrix::Transform ( BOT_POS, - rotation * SHQuaternion::FromEuler(SHVec3(SHMath::DegreesToRadians(180.0), 0.0f, 0.0f)), + SHQuaternion::FromEuler(SHVec3(SHMath::DegreesToRadians(180.0), 0.0f, 0.0f)) * rotation, RADIUS_SCALE ); drawMesh From 686e141efaad45351a836dd91e42a792b52055c2 Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Sun, 8 Jan 2023 01:46:52 +0800 Subject: [PATCH 05/24] Fixed comment for SHPrimitiveGenerator::LineCapsuleCap --- .../Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.h b/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.h index e8c7c3fb..839d0ef6 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Meshes/SHPrimitiveGenerator.h @@ -197,7 +197,7 @@ namespace SHADE /***********************************************************************************/ /*! \brief - Produces a circle that is comprised only of lines with no diagonal lines and + Produces a cap of a wireframe capsule that is comprised only of lines and store the data in a SHMeshData object. \return @@ -208,8 +208,8 @@ namespace SHADE /***********************************************************************************/ /*! \brief - Produces a line circle and constructs a SHMesh using the SHGraphicsSystem - provided. + Produces a cap of a wireframe capsule that is comprised only of lines and + constructs a SHMesh using the SHGraphicsSystem provided. \param meshLibrary Reference to the SHMeshLibrary to produce and store a line circle mesh in. @@ -222,8 +222,8 @@ namespace SHADE /***********************************************************************************/ /*! \brief - Produces a line circle and constructs a SHMesh using the SHGraphicsSystem - provided. + Produces a cap of a wireframe capsule that is comprised only of lines and + constructs a SHMesh using the SHGraphicsSystem provided. \param gfxSystem Reference to the SHGraphicsSystem to produce and store a line circle mesh in. From baaeb9ee102d2b80b1b32d007f91d095cb5d0aaa Mon Sep 17 00:00:00 2001 From: mushgunAX Date: Sun, 8 Jan 2023 21:05:09 +0800 Subject: [PATCH 06/24] Input Binding File I/O --- Assets/Bindings.SHConfig | 52 +++++++ SHADE_Engine/src/Input/SHInputManager.cpp | 169 ++++++++++++++++++++++ SHADE_Engine/src/Input/SHInputManager.h | 11 ++ 3 files changed, 232 insertions(+) create mode 100644 Assets/Bindings.SHConfig diff --git a/Assets/Bindings.SHConfig b/Assets/Bindings.SHConfig new file mode 100644 index 00000000..e0023603 --- /dev/null +++ b/Assets/Bindings.SHConfig @@ -0,0 +1,52 @@ +4 +Horizontal +0 +0 +5 +0.2 +5 +0 +2 +39 +68 +2 +37 +65 +2 +3 +16 +1 +2 +Mouse Wheel +3 +0 +1 +0.2 +1 +0 +0 +0 +0 +0 +Mouse X +1 +0 +1 +0.2 +1 +0 +0 +0 +0 +0 +Mouse Y +2 +0 +1 +0.2 +1 +0 +0 +0 +0 +0 diff --git a/SHADE_Engine/src/Input/SHInputManager.cpp b/SHADE_Engine/src/Input/SHInputManager.cpp index cec78648..a94b66d4 100644 --- a/SHADE_Engine/src/Input/SHInputManager.cpp +++ b/SHADE_Engine/src/Input/SHInputManager.cpp @@ -11,6 +11,7 @@ #pragma once #include +#include #include "SHInputManager.h" #include "../Tools/SHException.h" @@ -99,6 +100,174 @@ namespace SHADE } } + //The Binding File format presently goes as such: + /* + * Name + Binding Type Enum + Inverted Bool + Gravity Double + Dead Double + Sensitivity Double + Snap Bool + PositiveKeyCode count + PositiveKeyCodes + NegativeKeyCode count + NegativeKeyCodes + PositiveControllerCode Count + PositiveControllerCodes + NegativeControllerCode Count + NegativeControllerCodes + */ + void SHInputManager::SaveBindings(std::string const& targetFile) noexcept + { + std::ofstream file; + file.open("../../Assets/Bindings.SHConfig"); + + //File cannot be written to + if (!file) return; + + //First write the number of bindings + file << bindings.size() << std::endl; + + for (auto& b : bindings) + { + //Name + file << b.first << std::endl; + + //Data + auto& lbd = b.second; + + file << static_cast(lbd.bindingType) << std::endl; + file << static_cast(lbd.inverted) << std::endl; + file << lbd.gravity << std::endl; + file << lbd.dead << std::endl; + file << lbd.sensitivity << std::endl; + file << static_cast(lbd.snap) << std::endl; + + //Bindings + file << lbd.positiveKeyCodes.size() << std::endl; + for (auto kc : lbd.positiveKeyCodes) + file << static_cast(kc) << std::endl; + file << lbd.negativeKeyCodes.size() << std::endl; + for (auto kc : lbd.negativeKeyCodes) + file << static_cast(kc) << std::endl; + file << lbd.positiveControllerCodes.size() << std::endl; + for (auto cc : lbd.positiveControllerCodes) + file << static_cast(cc) << std::endl; + file << lbd.negativeControllerCodes.size() << std::endl; + for (auto cc : lbd.negativeControllerCodes) + file << static_cast(cc) << std::endl; + } + + file.close(); + } + + void SHInputManager::LoadBindings(std::string const& sourceFile) noexcept + { + std::ifstream file; + file.open("../../Assets/Bindings.SHConfig"); + + //Check + if (!file) return; + + //Erase + ClearBindings(); + + //Read + std::string read; + + int count = 0; + std::getline(file, read); + count = std::stoi(read); + + std::string bindingName; + for (int b = 0; b < count; ++b) + { + //Name + std::getline(file, read); + SHLOGV_CRITICAL("Binding Name: {}", read); + bindingName = read; + AddBinding(bindingName); + + //Type + std::getline(file, read); + SHLOGV_CRITICAL("Binding Type: {}", read); + SetBindingType(bindingName, static_cast(std::stoi(read))); + + //Inversion + std::getline(file, read); + SHLOGV_CRITICAL("Inversion: {}", read); + SetBindingInverted(bindingName, static_cast(std::stoi(read))); + + //Gravity + std::getline(file, read); + SHLOGV_CRITICAL("Gravity: {}", read); + SetBindingGravity(bindingName, std::stod(read)); + + //Dead + std::getline(file, read); + SHLOGV_CRITICAL("Dead: {}", read); + SetBindingDead(bindingName, std::stod(read)); + + //Sensitivity + std::getline(file, read); + SHLOGV_CRITICAL("Sensitivity: {}", read); + SetBindingSensitivity(bindingName, std::stod(read)); + + //Snap + std::getline(file, read); + SHLOGV_CRITICAL("Snap: {}", read); + SetBindingSnap(bindingName, static_cast(std::stoi(read))); + + int count = 0; + //Positive Key Codes + std::getline(file, read); + SHLOGV_CRITICAL("Positive Key Count: {}", read); + count = std::stoi(read); + for (int i = 0; i < count; ++i) + { + std::getline(file, read); + SHLOGV_CRITICAL("Positive Key: {}", read); + AddBindingPositiveKeyCode(bindingName, static_cast(std::stoi(read))); + } + + //Negative Key Codes + std::getline(file, read); + SHLOGV_CRITICAL("Negative Key Count: {}", read); + count = std::stoi(read); + for (int i = 0; i < count; ++i) + { + std::getline(file, read); + SHLOGV_CRITICAL("Negative Key: {}", read); + AddBindingNegativeKeyCode(bindingName, static_cast(std::stoi(read))); + } + + //Positive Controller Codes + std::getline(file, read); + SHLOGV_CRITICAL("Positive Controller Count: {}", read); + count = std::stoi(read); + for (int i = 0; i < count; ++i) + { + std::getline(file, read); + SHLOGV_CRITICAL("Positive Controller: {}", read); + AddBindingPositiveControllerCode(bindingName, static_cast(std::stoi(read))); + } + + //Negative Controller Codes + std::getline(file, read); + SHLOGV_CRITICAL("Negative Controller Count: {}", read); + count = std::stoi(read); + for (int i = 0; i < count; ++i) + { + std::getline(file, read); + SHLOGV_CRITICAL("Negative Controller: {}", read); + AddBindingNegativeControllerCode(bindingName, static_cast(std::stoi(read))); + } + } + + file.close(); + } + void SHInputManager::UpdateInput(double dt) noexcept { //Keyboard and Mouse Buttons//////////////////////////////////////////////// diff --git a/SHADE_Engine/src/Input/SHInputManager.h b/SHADE_Engine/src/Input/SHInputManager.h index 3f708124..977a2d08 100644 --- a/SHADE_Engine/src/Input/SHInputManager.h +++ b/SHADE_Engine/src/Input/SHInputManager.h @@ -681,6 +681,17 @@ namespace SHADE return controllersReleasedTime[controllerNum][static_cast(code)]; } + /*------------------------------------------------------------------------*/ + /* Binding I/O */ + /*------------------------------------------------------------------------*/ + + //Save bindings registered into a file + static void SaveBindings(std::string const& targetFile = "Assets/InputBindings.SHConfig") noexcept; + + //Load and register bindings from a file + //The current list of bindings will be overwritten, so save them somewhere else before loading + static void LoadBindings(std::string const& sourceFile = "Assets/InputBindings.SHConfig") noexcept; + /*------------------------------------------------------------------------*/ /* Binding Functions */ /*------------------------------------------------------------------------*/ From 4123e76a7d991add7f03f57b2b6ba9512b5f92b0 Mon Sep 17 00:00:00 2001 From: mushgunAX Date: Sun, 8 Jan 2023 21:36:19 +0800 Subject: [PATCH 07/24] Checking Input Binding I/O --- SHADE_Engine/src/Input/SHInputManager.cpp | 21 ++++----------------- SHADE_Engine/src/Input/SHInputManager.h | 4 ++-- 2 files changed, 6 insertions(+), 19 deletions(-) diff --git a/SHADE_Engine/src/Input/SHInputManager.cpp b/SHADE_Engine/src/Input/SHInputManager.cpp index a94b66d4..b8f329b9 100644 --- a/SHADE_Engine/src/Input/SHInputManager.cpp +++ b/SHADE_Engine/src/Input/SHInputManager.cpp @@ -102,6 +102,8 @@ namespace SHADE //The Binding File format presently goes as such: /* + * Binding count + * (For each binding:) * Name Binding Type Enum Inverted Bool @@ -121,7 +123,7 @@ namespace SHADE void SHInputManager::SaveBindings(std::string const& targetFile) noexcept { std::ofstream file; - file.open("../../Assets/Bindings.SHConfig"); + file.open(targetFile); //File cannot be written to if (!file) return; @@ -165,7 +167,7 @@ namespace SHADE void SHInputManager::LoadBindings(std::string const& sourceFile) noexcept { std::ifstream file; - file.open("../../Assets/Bindings.SHConfig"); + file.open(sourceFile); //Check if (!file) return; @@ -185,82 +187,67 @@ namespace SHADE { //Name std::getline(file, read); - SHLOGV_CRITICAL("Binding Name: {}", read); bindingName = read; AddBinding(bindingName); //Type std::getline(file, read); - SHLOGV_CRITICAL("Binding Type: {}", read); SetBindingType(bindingName, static_cast(std::stoi(read))); //Inversion std::getline(file, read); - SHLOGV_CRITICAL("Inversion: {}", read); SetBindingInverted(bindingName, static_cast(std::stoi(read))); //Gravity std::getline(file, read); - SHLOGV_CRITICAL("Gravity: {}", read); SetBindingGravity(bindingName, std::stod(read)); //Dead std::getline(file, read); - SHLOGV_CRITICAL("Dead: {}", read); SetBindingDead(bindingName, std::stod(read)); //Sensitivity std::getline(file, read); - SHLOGV_CRITICAL("Sensitivity: {}", read); SetBindingSensitivity(bindingName, std::stod(read)); //Snap std::getline(file, read); - SHLOGV_CRITICAL("Snap: {}", read); SetBindingSnap(bindingName, static_cast(std::stoi(read))); int count = 0; //Positive Key Codes std::getline(file, read); - SHLOGV_CRITICAL("Positive Key Count: {}", read); count = std::stoi(read); for (int i = 0; i < count; ++i) { std::getline(file, read); - SHLOGV_CRITICAL("Positive Key: {}", read); AddBindingPositiveKeyCode(bindingName, static_cast(std::stoi(read))); } //Negative Key Codes std::getline(file, read); - SHLOGV_CRITICAL("Negative Key Count: {}", read); count = std::stoi(read); for (int i = 0; i < count; ++i) { std::getline(file, read); - SHLOGV_CRITICAL("Negative Key: {}", read); AddBindingNegativeKeyCode(bindingName, static_cast(std::stoi(read))); } //Positive Controller Codes std::getline(file, read); - SHLOGV_CRITICAL("Positive Controller Count: {}", read); count = std::stoi(read); for (int i = 0; i < count; ++i) { std::getline(file, read); - SHLOGV_CRITICAL("Positive Controller: {}", read); AddBindingPositiveControllerCode(bindingName, static_cast(std::stoi(read))); } //Negative Controller Codes std::getline(file, read); - SHLOGV_CRITICAL("Negative Controller Count: {}", read); count = std::stoi(read); for (int i = 0; i < count; ++i) { std::getline(file, read); - SHLOGV_CRITICAL("Negative Controller: {}", read); AddBindingNegativeControllerCode(bindingName, static_cast(std::stoi(read))); } } diff --git a/SHADE_Engine/src/Input/SHInputManager.h b/SHADE_Engine/src/Input/SHInputManager.h index 977a2d08..01ef6c42 100644 --- a/SHADE_Engine/src/Input/SHInputManager.h +++ b/SHADE_Engine/src/Input/SHInputManager.h @@ -686,11 +686,11 @@ namespace SHADE /*------------------------------------------------------------------------*/ //Save bindings registered into a file - static void SaveBindings(std::string const& targetFile = "Assets/InputBindings.SHConfig") noexcept; + static void SaveBindings(std::string const& targetFile = "../../Assets/Bindings.SHConfig") noexcept; //Load and register bindings from a file //The current list of bindings will be overwritten, so save them somewhere else before loading - static void LoadBindings(std::string const& sourceFile = "Assets/InputBindings.SHConfig") noexcept; + static void LoadBindings(std::string const& sourceFile = "../../Assets/Bindings.SHConfig") noexcept; /*------------------------------------------------------------------------*/ /* Binding Functions */ From 7dbd0b93b3cff3bb85bb48565e7a85ee68cb0caf Mon Sep 17 00:00:00 2001 From: mushgunAX Date: Sun, 8 Jan 2023 21:39:48 +0800 Subject: [PATCH 08/24] Minor comment fix --- SHADE_Engine/src/Input/SHInputManager.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SHADE_Engine/src/Input/SHInputManager.h b/SHADE_Engine/src/Input/SHInputManager.h index 01ef6c42..30ee00e8 100644 --- a/SHADE_Engine/src/Input/SHInputManager.h +++ b/SHADE_Engine/src/Input/SHInputManager.h @@ -689,7 +689,7 @@ namespace SHADE static void SaveBindings(std::string const& targetFile = "../../Assets/Bindings.SHConfig") noexcept; //Load and register bindings from a file - //The current list of bindings will be overwritten, so save them somewhere else before loading + //If specified file exists, the current list of bindings will be overwritten, so save them somewhere else before loading static void LoadBindings(std::string const& sourceFile = "../../Assets/Bindings.SHConfig") noexcept; /*------------------------------------------------------------------------*/ From b293b28a56f66dfefd476c706f0cb5a2c7a3aec4 Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Mon, 9 Jan 2023 07:14:40 +0800 Subject: [PATCH 09/24] Button fix --- Assets/Application.SHConfig | 2 +- Assets/Scenes/UI Test.shade.shmeta | 2 +- SHADE_Engine/src/UI/SHUISystem.cpp | 54 ++++++++++++++++++++---------- SHADE_Engine/src/UI/SHUISystem.h | 2 ++ 4 files changed, 41 insertions(+), 19 deletions(-) diff --git a/Assets/Application.SHConfig b/Assets/Application.SHConfig index c9b34a7a..bc619d3b 100644 --- a/Assets/Application.SHConfig +++ b/Assets/Application.SHConfig @@ -1,4 +1,4 @@ Start in Fullscreen: false -Starting Scene ID: 94246101 +Starting Scene ID: 93618245 Window Size: {x: 1920, y: 1080} Window Title: SHADE Engine \ No newline at end of file diff --git a/Assets/Scenes/UI Test.shade.shmeta b/Assets/Scenes/UI Test.shade.shmeta index cf38916e..f64d5657 100644 --- a/Assets/Scenes/UI Test.shade.shmeta +++ b/Assets/Scenes/UI Test.shade.shmeta @@ -1,3 +1,3 @@ Name: UI Test -ID: 87707373 +ID: 92351881 Type: 5 diff --git a/SHADE_Engine/src/UI/SHUISystem.cpp b/SHADE_Engine/src/UI/SHUISystem.cpp index 1387bc6b..00aa0cda 100644 --- a/SHADE_Engine/src/UI/SHUISystem.cpp +++ b/SHADE_Engine/src/UI/SHUISystem.cpp @@ -149,9 +149,9 @@ namespace SHADE SHVec4 topExtent4 = uiComp->GetMatrix() * SHVec4(-comp.size.x * 0.5f, comp.size.y * 0.5f , 0.0f,1.0f); SHVec4 btmExtent4 = uiComp->GetMatrix() * SHVec4(comp.size.x * 0.5f , -comp.size.y * 0.5f , 0.0f, 1.0f); - SHVec2 topExtent{ topExtent4.x,-topExtent4.y }; - SHVec2 btmExtent{ btmExtent4.x,-btmExtent4.y }; - + SHVec2 topExtent{ topExtent4.x,topExtent4.y }; + SHVec2 btmExtent{ btmExtent4.x,btmExtent4.y }; + //SHLOG_INFO("TopExtent: {}, {}", topExtent.x, topExtent.y); SHVec2 windowSize; @@ -160,23 +160,24 @@ namespace SHADE windowSize = SHEditorWindowManager::GetEditorWindow()->windowSize; mousePos = SHEditorWindowManager::GetEditorWindow()->viewportMousePos; - - - + //SHLOG_INFO("MousePos: {}, {}", mousePos.x, mousePos.y); + //mousePos /= windowSize; #endif - SHVec2 camSize{ cameraSystem->GetCameraWidthHeight(0).x , cameraSystem->GetCameraWidthHeight(0).y }; + SHVec2 camSize{ cameraSystem->GetCameraWidthHeight(0)}; - topExtent += camSize * 0.5f; - btmExtent += camSize * 0.5f; + topExtent = CanvasToScreenPoint(topExtent); + btmExtent = CanvasToScreenPoint(btmExtent); - //Convert everything to using ratios - topExtent /= camSize; - btmExtent /= camSize; + //SHLOG_INFO("TopExtent Screen Point: {}, {}, Cam size: {}, {}", topExtent.x, topExtent.y, camSize.x, camSize.y); - mousePos /= windowSize; + //Convert everything to using ratios + //topExtent /= camSize; + //btmExtent /= camSize; + + //SHLOG_INFO("mousePos: {} , {}", mousePos.x, mousePos.y); comp.isClicked = false; @@ -187,6 +188,7 @@ namespace SHADE if (SHInputManager::GetKeyUp(SHInputManager::SH_KEYCODE::LMB)) { comp.isClicked = true; + //SHLOG_INFO("BUTTON CLICKED"); } //SHLOG_INFO("BUTTON HOVERED"); @@ -202,11 +204,11 @@ namespace SHADE if (SHComponentManager::HasComponent(comp.GetEID())) { - //auto renderable = SHComponentManager::GetComponent_s(comp.GetEID()); - //auto texture = SHResourceManager::Get(comp.GetDefaultTexture()); + auto renderable = SHComponentManager::GetComponent_s(comp.GetEID()); + auto texture = SHResourceManager::Get(comp.GetDefaultTexture()); - //auto material = renderable->GetModifiableMaterial(); - //material->SetProperty("texture", comp.GetDefaultTexture()); + auto material = renderable->GetModifiableMaterial(); + material->SetProperty("texture", comp.GetDefaultTexture()); } } @@ -221,4 +223,22 @@ namespace SHADE } } + SHVec2 SHUISystem::CanvasToScreenPoint(SHVec2& const canvasPoint) noexcept + { + SHVec2 result{canvasPoint}; + + auto cameraSystem = SHSystemManager::GetSystem(); + + SHVec2 camSize{ cameraSystem->GetCameraWidthHeight(0) }; + + + result.y *= -1.0f; + result += camSize * 0.5f; + + return result; + } + + + + }//end namespace diff --git a/SHADE_Engine/src/UI/SHUISystem.h b/SHADE_Engine/src/UI/SHUISystem.h index 04e057ad..21518f16 100644 --- a/SHADE_Engine/src/UI/SHUISystem.h +++ b/SHADE_Engine/src/UI/SHUISystem.h @@ -65,6 +65,8 @@ namespace SHADE void UpdateUIComponent(SHUIComponent& comp) noexcept; void UpdateButtonComponent(SHButtonComponent& comp) noexcept; void UpdateCanvasComponent(SHCanvasComponent& comp) noexcept; + SHVec2 CanvasToScreenPoint(SHVec2& const canvasPoint) noexcept; + }; From 5f11a931c78ab54aa6b706b9cf8f3690d3bf7af2 Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Mon, 9 Jan 2023 09:24:20 +0800 Subject: [PATCH 10/24] Added Toggle Button, Fixed canvas Scalar --- Assets/Scenes/UI Test.shade | 24 +++- Assets/Scenes/UI Test.shade.shmeta | 2 +- .../Inspector/SHEditorInspector.cpp | 6 + .../src/Serialization/SHSerialization.cpp | 4 + SHADE_Engine/src/UI/SHButtonComponent.cpp | 2 +- SHADE_Engine/src/UI/SHButtonComponent.h | 1 - .../src/UI/SHToggleButtonComponent.cpp | 59 +++++++++ SHADE_Engine/src/UI/SHToggleButtonComponent.h | 48 ++++++++ SHADE_Engine/src/UI/SHUISystem.cpp | 116 +++++++++++++----- SHADE_Engine/src/UI/SHUISystem.h | 2 + 10 files changed, 229 insertions(+), 35 deletions(-) create mode 100644 SHADE_Engine/src/UI/SHToggleButtonComponent.cpp create mode 100644 SHADE_Engine/src/UI/SHToggleButtonComponent.h diff --git a/Assets/Scenes/UI Test.shade b/Assets/Scenes/UI Test.shade index e8ee4df2..0aea6f72 100644 --- a/Assets/Scenes/UI Test.shade +++ b/Assets/Scenes/UI Test.shade @@ -1,7 +1,7 @@ - EID: 0 Name: Canvas IsActive: true - NumberOfChildren: 1 + NumberOfChildren: 2 Components: Canvas Component: Canvas Width: 10 @@ -14,7 +14,7 @@ NumberOfChildren: 0 Components: Transform Component: - Translate: {x: 0, y: 0, z: 0} + Translate: {x: -3.5999999, y: 3.0999999, z: 0} Rotate: {x: 0, y: 0, z: 0} Scale: {x: 1, y: 1, z: 1} IsActive: true @@ -28,6 +28,26 @@ Clicked Texture: 0 IsActive: true Scripts: ~ +- EID: 5 + Name: Default + IsActive: true + NumberOfChildren: 0 + Components: + Transform Component: + Translate: {x: 153.399994, y: 0, z: 0} + Rotate: {x: 0, y: 0, z: 0} + Scale: {x: 1, y: 1, z: 1} + IsActive: true + Renderable Component: + Mesh: 141771688 + Material: 129340704 + IsActive: true + Toggle Button Component: + Non Toggled Texture: 0 + Toggled Texture: 0 + Value: false + IsActive: true + Scripts: ~ - EID: 1 Name: Camera IsActive: true diff --git a/Assets/Scenes/UI Test.shade.shmeta b/Assets/Scenes/UI Test.shade.shmeta index f64d5657..a889afd5 100644 --- a/Assets/Scenes/UI Test.shade.shmeta +++ b/Assets/Scenes/UI Test.shade.shmeta @@ -1,3 +1,3 @@ Name: UI Test -ID: 92351881 +ID: 94041256 Type: 5 diff --git a/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorInspector.cpp b/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorInspector.cpp index 83647da7..ed6ea6bb 100644 --- a/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorInspector.cpp +++ b/SHADE_Engine/src/Editor/EditorWindow/Inspector/SHEditorInspector.cpp @@ -21,6 +21,7 @@ #include "UI/SHUIComponent.h" #include "UI/SHCanvasComponent.h" #include "UI/SHButtonComponent.h" +#include "UI/SHToggleButtonComponent.h" #include "SHEditorComponentView.h" #include "AudioSystem/SHAudioListenerComponent.h" #include "Graphics/MiddleEnd/TextRendering/SHTextRenderableComponent.h" @@ -154,6 +155,10 @@ namespace SHADE { DrawComponent(buttonComponent); } + if (auto toggleButton = SHComponentManager::GetComponent_s(eid)) + { + DrawComponent(toggleButton); + } ImGui::Separator(); // Render Scripts SHScriptEngine* scriptEngine = static_cast(SHSystemManager::GetSystem()); @@ -167,6 +172,7 @@ namespace SHADE DrawAddComponentButton(eid); DrawAddComponentButton(eid); DrawAddComponentButton(eid); + DrawAddComponentButton(eid); // Components that require Transforms diff --git a/SHADE_Engine/src/Serialization/SHSerialization.cpp b/SHADE_Engine/src/Serialization/SHSerialization.cpp index 99e4fa41..89e947fc 100644 --- a/SHADE_Engine/src/Serialization/SHSerialization.cpp +++ b/SHADE_Engine/src/Serialization/SHSerialization.cpp @@ -18,6 +18,7 @@ #include "Physics/Interface/SHRigidBodyComponent.h" #include "UI/SHCanvasComponent.h" #include "UI/SHButtonComponent.h" +#include "UI/SHToggleButtonComponent.h" #include "ECS_Base/Managers/SHSystemManager.h" #include "Graphics/MiddleEnd/Lights/SHLightComponent.h" #include "Scripting/SHScriptEngine.h" @@ -218,6 +219,7 @@ namespace SHADE AddComponentToComponentNode(components, eid); AddComponentToComponentNode(components, eid); + AddComponentToComponentNode(components, eid); AddConvComponentToComponentNode(components, eid); @@ -275,6 +277,7 @@ namespace SHADE AddComponentID(componentIDList, componentsNode); AddComponentID(componentIDList, componentsNode); + AddComponentID(componentIDList, componentsNode); AddComponentID(componentIDList, componentsNode); return componentIDList; @@ -356,6 +359,7 @@ namespace SHADE SHSerializationHelper::InitializeComponentFromNode(componentsNode, eid); SHSerializationHelper::InitializeComponentFromNode(componentsNode, eid); + SHSerializationHelper::InitializeComponentFromNode(componentsNode, eid); SHSerializationHelper::ConvertNodeToComponent(componentsNode, eid); SHSerializationHelper::InitializeComponentFromNode(componentsNode, eid); } diff --git a/SHADE_Engine/src/UI/SHButtonComponent.cpp b/SHADE_Engine/src/UI/SHButtonComponent.cpp index 7b275128..8e87b10e 100644 --- a/SHADE_Engine/src/UI/SHButtonComponent.cpp +++ b/SHADE_Engine/src/UI/SHButtonComponent.cpp @@ -5,7 +5,7 @@ namespace SHADE { SHButtonComponent::SHButtonComponent() - :size(1.0f), offset(0.0f), isHovered(false), isClicked(false), + :size(1.0f), isHovered(false), isClicked(false), defaultTexture(0), hoveredTexture(0), clickedTexture(0) { } diff --git a/SHADE_Engine/src/UI/SHButtonComponent.h b/SHADE_Engine/src/UI/SHButtonComponent.h index 39790b6a..be6ada3e 100644 --- a/SHADE_Engine/src/UI/SHButtonComponent.h +++ b/SHADE_Engine/src/UI/SHButtonComponent.h @@ -18,7 +18,6 @@ namespace SHADE virtual ~SHButtonComponent() = default; SHVec2 size; - SHVec2 offset; AssetID GetClickedTexture() const noexcept; AssetID GetDefaultTexture() const noexcept; diff --git a/SHADE_Engine/src/UI/SHToggleButtonComponent.cpp b/SHADE_Engine/src/UI/SHToggleButtonComponent.cpp new file mode 100644 index 00000000..527323ea --- /dev/null +++ b/SHADE_Engine/src/UI/SHToggleButtonComponent.cpp @@ -0,0 +1,59 @@ +#include "SHpch.h" +#include "SHToggleButtonComponent.h" + + +namespace SHADE +{ + SHToggleButtonComponent::SHToggleButtonComponent() + :size(1.0f), isHovered(false), isClicked(false), value(false), + defaultTexture(0), toggledTexture(0) + { + } + + AssetID SHToggleButtonComponent::GetDefaultTexture() const noexcept + { + return defaultTexture; + } + + AssetID SHToggleButtonComponent::GetToggledTexture() const noexcept + { + return toggledTexture; + } + + bool SHToggleButtonComponent::GetValue() const noexcept + { + return value; + } + + + void SHToggleButtonComponent::SetDefaultTexture(AssetID texture) noexcept + { + defaultTexture = texture; + } + + void SHToggleButtonComponent::SetToggledTexture(AssetID texture) noexcept + { + toggledTexture = texture; + } + + void SHToggleButtonComponent::SetValue(bool value) noexcept + { + this->value = value; + } + +} + + +RTTR_REGISTRATION +{ + using namespace SHADE; + using namespace rttr; + + registration::class_("Toggle Button Component") + .property("Non Toggled Texture", &SHToggleButtonComponent::GetDefaultTexture, &SHToggleButtonComponent::SetDefaultTexture) + .property("Toggled Texture", &SHToggleButtonComponent::GetToggledTexture, &SHToggleButtonComponent::SetToggledTexture) + .property("Value", &SHToggleButtonComponent::GetValue, &SHToggleButtonComponent::SetValue) + ; + + +} \ No newline at end of file diff --git a/SHADE_Engine/src/UI/SHToggleButtonComponent.h b/SHADE_Engine/src/UI/SHToggleButtonComponent.h new file mode 100644 index 00000000..3217c892 --- /dev/null +++ b/SHADE_Engine/src/UI/SHToggleButtonComponent.h @@ -0,0 +1,48 @@ +#pragma once + +#include + +#include "SH_API.h" +#include "ECS_Base/Components/SHComponent.h" +#include "Math/Vector/SHVec3.h" +#include "Math/Vector/SHVec2.h" +#include "Assets/SHAssetMacros.h" + +namespace SHADE +{ + + class SH_API SHToggleButtonComponent final: public SHComponent + { + public: + SHToggleButtonComponent(); + virtual ~SHToggleButtonComponent() = default; + + SHVec2 size; + + AssetID GetToggledTexture() const noexcept; + AssetID GetDefaultTexture() const noexcept; + bool GetValue() const noexcept; + + + void SetDefaultTexture(AssetID texture) noexcept; + void SetToggledTexture(AssetID texture) noexcept; + void SetValue(bool value) noexcept; + + + + friend class SHUISystem; + private: + + bool isHovered; + bool isClicked; + bool value; + AssetID defaultTexture; + AssetID toggledTexture; + + + + RTTR_ENABLE() + }; + + +} \ No newline at end of file diff --git a/SHADE_Engine/src/UI/SHUISystem.cpp b/SHADE_Engine/src/UI/SHUISystem.cpp index 00aa0cda..f3b5cad8 100644 --- a/SHADE_Engine/src/UI/SHUISystem.cpp +++ b/SHADE_Engine/src/UI/SHUISystem.cpp @@ -103,7 +103,7 @@ namespace SHADE { auto transform = SHComponentManager::GetComponent(comp.GetEID()); if (canvasComp != nullptr) - comp.localToCanvasMatrix = canvasComp->GetMatrix()* transform->GetTRS(); + comp.localToCanvasMatrix = transform->GetTRS() * canvasComp->GetMatrix(); else comp.localToCanvasMatrix = transform->GetTRS(); } @@ -139,47 +139,38 @@ namespace SHADE void SHUISystem::UpdateButtonComponent(SHButtonComponent& comp) noexcept { - if (!SHComponentManager::HasComponent(comp.GetEID()) || !SHComponentManager::HasComponent(comp.GetEID())) + if (!SHComponentManager::HasComponent(comp.GetEID())) { return; } auto cameraSystem = SHSystemManager::GetSystem(); auto uiComp = SHComponentManager::GetComponent(comp.GetEID()); - SHVec4 topExtent4 = uiComp->GetMatrix() * SHVec4(-comp.size.x * 0.5f, comp.size.y * 0.5f , 0.0f,1.0f); - SHVec4 btmExtent4 = uiComp->GetMatrix() * SHVec4(comp.size.x * 0.5f , -comp.size.y * 0.5f , 0.0f, 1.0f); + SHVec4 topExtent4 = SHMatrix::Translate(-comp.size.x * 0.5f, comp.size.y * 0.5f, 0.0f) * uiComp->GetMatrix() * SHVec4(0.0f,0.0f, 0.0f,1.0f); + SHVec4 btmExtent4 = SHMatrix::Translate(comp.size.x * 0.5f, -comp.size.y * 0.5f, 0.0f) * uiComp->GetMatrix() * SHVec4(0.0f,0.0f, 0.0f,1.0f); + //SHVec4 btmExtent4 = uiComp->GetMatrix() * SHVec4(comp.size.x * 0.5f , -comp.size.y * 0.5f , 0.0f, 1.0f); SHVec2 topExtent{ topExtent4.x,topExtent4.y }; SHVec2 btmExtent{ btmExtent4.x,btmExtent4.y }; - //SHLOG_INFO("TopExtent: {}, {}", topExtent.x, topExtent.y); - - SHVec2 windowSize; + SHVec2 mousePos; + SHVec2 windowSize; #ifdef SHEDITOR - windowSize = SHEditorWindowManager::GetEditorWindow()->windowSize; mousePos = SHEditorWindowManager::GetEditorWindow()->viewportMousePos; - //SHLOG_INFO("MousePos: {}, {}", mousePos.x, mousePos.y); - //mousePos /= windowSize; + //mousePos.y = windowSize.y - mousePos.y; + mousePos /= windowSize; #endif - - SHVec2 camSize{ cameraSystem->GetCameraWidthHeight(0)}; + //SHLOG_INFO("TopExtent: {}, {}", topExtent.x, topExtent.y) topExtent = CanvasToScreenPoint(topExtent); btmExtent = CanvasToScreenPoint(btmExtent); - - //SHLOG_INFO("TopExtent Screen Point: {}, {}, Cam size: {}, {}", topExtent.x, topExtent.y, camSize.x, camSize.y); + topExtent /= camSize; + btmExtent /= camSize; - //Convert everything to using ratios - //topExtent /= camSize; - //btmExtent /= camSize; - - - - //SHLOG_INFO("mousePos: {} , {}", mousePos.x, mousePos.y); comp.isClicked = false; if (mousePos.x >= topExtent.x && mousePos.x <= btmExtent.x && mousePos.y >= topExtent.y && mousePos.y <= btmExtent.y) @@ -188,30 +179,88 @@ namespace SHADE if (SHInputManager::GetKeyUp(SHInputManager::SH_KEYCODE::LMB)) { comp.isClicked = true; - //SHLOG_INFO("BUTTON CLICKED"); } - //SHLOG_INFO("BUTTON HOVERED"); - - + SHLOG_INFO("HOVERED") } else { comp.isHovered = false; - - //SHLOG_INFO("BUTTON NOT HOVERED") + SHLOG_INFO("NOT HOVERED") } if (SHComponentManager::HasComponent(comp.GetEID())) { - auto renderable = SHComponentManager::GetComponent_s(comp.GetEID()); + /*auto renderable = SHComponentManager::GetComponent_s(comp.GetEID()); auto texture = SHResourceManager::Get(comp.GetDefaultTexture()); auto material = renderable->GetModifiableMaterial(); - material->SetProperty("texture", comp.GetDefaultTexture()); + material->SetProperty("texture", comp.GetDefaultTexture());*/ } } + + void SHUISystem::UpdateToggleButtonComponent(SHToggleButtonComponent& comp) noexcept + { + if (!SHComponentManager::HasComponent(comp.GetEID())) + { + return; + } + auto cameraSystem = SHSystemManager::GetSystem(); + auto uiComp = SHComponentManager::GetComponent(comp.GetEID()); + + SHVec4 topExtent4 = uiComp->GetMatrix() * SHVec4(-comp.size.x * 0.5f, comp.size.y * 0.5f, 0.0f, 1.0f); + SHVec4 btmExtent4 = uiComp->GetMatrix() * SHVec4(comp.size.x * 0.5f, -comp.size.y * 0.5f, 0.0f, 1.0f); + + SHVec2 topExtent{ topExtent4.x,topExtent4.y }; + SHVec2 btmExtent{ btmExtent4.x,btmExtent4.y }; + + SHVec2 mousePos; + SHVec2 windowSize; +#ifdef SHEDITOR + windowSize = SHEditorWindowManager::GetEditorWindow()->windowSize; + mousePos = SHEditorWindowManager::GetEditorWindow()->viewportMousePos; + mousePos /= windowSize; +#endif + + SHVec2 camSize{ cameraSystem->GetCameraWidthHeight(0) }; + + + + topExtent = CanvasToScreenPoint(topExtent); + btmExtent = CanvasToScreenPoint(btmExtent); + topExtent /= camSize; + btmExtent /= camSize; + + + comp.isClicked = false; + if (mousePos.x >= topExtent.x && mousePos.x <= btmExtent.x + && mousePos.y >= topExtent.y && mousePos.y <= btmExtent.y) + { + comp.isHovered = true; + if (SHInputManager::GetKeyUp(SHInputManager::SH_KEYCODE::LMB)) + { + comp.isClicked = true; + comp.value = !comp.value; + } + } + else + { + comp.isHovered = false; + } + + + if (SHComponentManager::HasComponent(comp.GetEID())) + { + /*auto renderable = SHComponentManager::GetComponent_s(comp.GetEID()); + auto texture = SHResourceManager::Get(comp.GetDefaultTexture()); + + auto material = renderable->GetModifiableMaterial(); + material->SetProperty("texture", comp.GetDefaultTexture());*/ + } + } + + void SHUISystem::UpdateButtonsRoutine::Execute(double dt) noexcept { SHUISystem* system = (SHUISystem*)GetSystem(); @@ -221,6 +270,13 @@ namespace SHADE if (SHSceneManager::CheckNodeAndComponentsActive(comp.GetEID())) system->UpdateButtonComponent(comp); } + + auto& toggleButtonDense = SHComponentManager::GetDense(); + for (auto& comp : toggleButtonDense) + { + if (SHSceneManager::CheckNodeAndComponentsActive(comp.GetEID())) + system->UpdateToggleButtonComponent(comp); + } } SHVec2 SHUISystem::CanvasToScreenPoint(SHVec2& const canvasPoint) noexcept @@ -230,7 +286,7 @@ namespace SHADE auto cameraSystem = SHSystemManager::GetSystem(); SHVec2 camSize{ cameraSystem->GetCameraWidthHeight(0) }; - + //camSize.y *= -1.0f; result.y *= -1.0f; result += camSize * 0.5f; diff --git a/SHADE_Engine/src/UI/SHUISystem.h b/SHADE_Engine/src/UI/SHUISystem.h index 21518f16..a5ba8c4f 100644 --- a/SHADE_Engine/src/UI/SHUISystem.h +++ b/SHADE_Engine/src/UI/SHUISystem.h @@ -5,6 +5,7 @@ #include "ECS_Base/System/SHSystemRoutine.h" #include "SHUIComponent.h" #include "SHButtonComponent.h" +#include "SHToggleButtonComponent.h" #include "SHCanvasComponent.h" #include "Scene/SHSceneGraph.h" #include "Scene/SHSceneManager.h" @@ -64,6 +65,7 @@ namespace SHADE private: void UpdateUIComponent(SHUIComponent& comp) noexcept; void UpdateButtonComponent(SHButtonComponent& comp) noexcept; + void UpdateToggleButtonComponent(SHToggleButtonComponent& comp) noexcept; void UpdateCanvasComponent(SHCanvasComponent& comp) noexcept; SHVec2 CanvasToScreenPoint(SHVec2& const canvasPoint) noexcept; From e89b6f5c4cc421555a58e2973202a5dcb02cc944 Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Mon, 9 Jan 2023 09:38:53 +0800 Subject: [PATCH 11/24] Fixing button --- Assets/Scenes/UI Test.shade.shmeta | 2 +- SHADE_Engine/src/UI/SHUISystem.cpp | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Assets/Scenes/UI Test.shade.shmeta b/Assets/Scenes/UI Test.shade.shmeta index a889afd5..e65c0b8f 100644 --- a/Assets/Scenes/UI Test.shade.shmeta +++ b/Assets/Scenes/UI Test.shade.shmeta @@ -1,3 +1,3 @@ Name: UI Test -ID: 94041256 +ID: 96041206 Type: 5 diff --git a/SHADE_Engine/src/UI/SHUISystem.cpp b/SHADE_Engine/src/UI/SHUISystem.cpp index f3b5cad8..571eb167 100644 --- a/SHADE_Engine/src/UI/SHUISystem.cpp +++ b/SHADE_Engine/src/UI/SHUISystem.cpp @@ -145,10 +145,11 @@ namespace SHADE } auto cameraSystem = SHSystemManager::GetSystem(); auto uiComp = SHComponentManager::GetComponent(comp.GetEID()); + //auto canvasComp = SHComponentManager::GetComponent_s(uiComp->canvasID); - SHVec4 topExtent4 = SHMatrix::Translate(-comp.size.x * 0.5f, comp.size.y * 0.5f, 0.0f) * uiComp->GetMatrix() * SHVec4(0.0f,0.0f, 0.0f,1.0f); - SHVec4 btmExtent4 = SHMatrix::Translate(comp.size.x * 0.5f, -comp.size.y * 0.5f, 0.0f) * uiComp->GetMatrix() * SHVec4(0.0f,0.0f, 0.0f,1.0f); - //SHVec4 btmExtent4 = uiComp->GetMatrix() * SHVec4(comp.size.x * 0.5f , -comp.size.y * 0.5f , 0.0f, 1.0f); + SHVec4 topExtent4 = SHMatrix::Translate(-comp.size.x * 0.5f, comp.size.y * 0.5f, 0.0f) * uiComp->GetMatrix() * SHVec4(0.0f, 0.0f, 0.0f, 1.0f); + SHVec4 btmExtent4 = SHMatrix::Translate(comp.size.x * 0.5f, -comp.size.y * 0.5f, 0.0f) * uiComp->GetMatrix() * SHVec4(0.0f,0.0f, 0.0f,1.0f); + SHVec2 topExtent{ topExtent4.x,topExtent4.y }; SHVec2 btmExtent{ btmExtent4.x,btmExtent4.y }; From 88491ffbd80c4b022a1c91dfe91e7024c36cae51 Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Mon, 9 Jan 2023 09:56:46 +0800 Subject: [PATCH 12/24] ToggleButton fix --- Assets/Scenes/UI Test.shade.shmeta | 2 +- SHADE_Engine/src/UI/SHUISystem.cpp | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/Assets/Scenes/UI Test.shade.shmeta b/Assets/Scenes/UI Test.shade.shmeta index e65c0b8f..ad821810 100644 --- a/Assets/Scenes/UI Test.shade.shmeta +++ b/Assets/Scenes/UI Test.shade.shmeta @@ -1,3 +1,3 @@ Name: UI Test -ID: 96041206 +ID: 88543249 Type: 5 diff --git a/SHADE_Engine/src/UI/SHUISystem.cpp b/SHADE_Engine/src/UI/SHUISystem.cpp index 571eb167..95663bc0 100644 --- a/SHADE_Engine/src/UI/SHUISystem.cpp +++ b/SHADE_Engine/src/UI/SHUISystem.cpp @@ -161,7 +161,9 @@ namespace SHADE windowSize = SHEditorWindowManager::GetEditorWindow()->windowSize; mousePos = SHEditorWindowManager::GetEditorWindow()->viewportMousePos; //mousePos.y = windowSize.y - mousePos.y; + SHLOG_INFO("mouse pos: {}, {}", mousePos.x, mousePos.y) mousePos /= windowSize; + SHLOG_INFO("mouse pos normalized: {}, {}", mousePos.x, mousePos.y) #endif SHVec2 camSize{ cameraSystem->GetCameraWidthHeight(0)}; @@ -169,6 +171,7 @@ namespace SHADE topExtent = CanvasToScreenPoint(topExtent); btmExtent = CanvasToScreenPoint(btmExtent); + //SHLOG_INFO("TopExtent: {}, {} Btm Extent: {}, {}", topExtent.x, topExtent.y, btmExtent.x, btmExtent.y) topExtent /= camSize; btmExtent /= camSize; @@ -210,8 +213,9 @@ namespace SHADE auto cameraSystem = SHSystemManager::GetSystem(); auto uiComp = SHComponentManager::GetComponent(comp.GetEID()); - SHVec4 topExtent4 = uiComp->GetMatrix() * SHVec4(-comp.size.x * 0.5f, comp.size.y * 0.5f, 0.0f, 1.0f); - SHVec4 btmExtent4 = uiComp->GetMatrix() * SHVec4(comp.size.x * 0.5f, -comp.size.y * 0.5f, 0.0f, 1.0f); + SHVec4 topExtent4 = SHMatrix::Translate(-comp.size.x * 0.5f, comp.size.y * 0.5f, 0.0f) * uiComp->GetMatrix() * SHVec4(0.0f, 0.0f, 0.0f, 1.0f); + SHVec4 btmExtent4 = SHMatrix::Translate(comp.size.x * 0.5f, -comp.size.y * 0.5f, 0.0f) * uiComp->GetMatrix() * SHVec4(0.0f, 0.0f, 0.0f, 1.0f); + SHVec2 topExtent{ topExtent4.x,topExtent4.y }; SHVec2 btmExtent{ btmExtent4.x,btmExtent4.y }; From 4f63558f4021d2fea154b59f83cfa0ec77022bae Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Mon, 9 Jan 2023 10:44:36 +0800 Subject: [PATCH 13/24] Added GetComponents to Component Manager --- .../ECS_Base/Managers/SHComponentManager.h | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/SHADE_Engine/src/ECS_Base/Managers/SHComponentManager.h b/SHADE_Engine/src/ECS_Base/Managers/SHComponentManager.h index 8921fbce..9fcbf6f8 100644 --- a/SHADE_Engine/src/ECS_Base/Managers/SHComponentManager.h +++ b/SHADE_Engine/src/ECS_Base/Managers/SHComponentManager.h @@ -23,7 +23,7 @@ #include "SH_API.h" #include "Events/SHEventManager.hpp" - +#include #include namespace SHADE @@ -151,6 +151,32 @@ namespace SHADE return (componentSet.GetSparseSet()->GetElement_s(EntityHandleGenerator::GetIndex(entityID))); } + /*!************************************************************************* + * \brief + * Gets the Component of the entity with the specified entityID + * + * This is the safe version of GetComponent_s which does a HasComponent to make + * sure that the entity has such a component and returns nullptr if it doesn't + * + * This safe version also checks if the sparse set of this component type + * has been created in SHComponentManager and creates one if it doesn't + * + * @tparam T... + * Pack of Types for all the Components to get. + * \param entityID + * EntityID of the entity that we are trying to get the component of. + * \return + * A tuple of pointers to all the components specified. + * Returns nullptr if the entity does not contain such a component. + ***************************************************************************/ + + template + static std::enable_if_t<(... && std::is_base_of_v), std::tuple> GetComponents(EntityID entityID) noexcept + { + return std::make_tuple(GetComponent_s(entityID)...); + } + + /*!************************************************************************* * \brief * Gets the Component of the entity with the specified entityID From 28829213c9a310d9dd7719746f2b97bed252fe4a Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Mon, 9 Jan 2023 10:53:56 +0800 Subject: [PATCH 14/24] merge --- Assets/Scenes/UI_Test.shade.shmeta | 4 ++-- SHADE_Engine/src/Serialization/SHSerialization.cpp | 2 +- SHADE_Engine/src/UI/SHUISystem.cpp | 10 +++++----- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Assets/Scenes/UI_Test.shade.shmeta b/Assets/Scenes/UI_Test.shade.shmeta index ad821810..f32277e0 100644 --- a/Assets/Scenes/UI_Test.shade.shmeta +++ b/Assets/Scenes/UI_Test.shade.shmeta @@ -1,3 +1,3 @@ -Name: UI Test -ID: 88543249 +Name: UI_Test +ID: 97161771 Type: 5 diff --git a/SHADE_Engine/src/Serialization/SHSerialization.cpp b/SHADE_Engine/src/Serialization/SHSerialization.cpp index e92a4d5f..a055d91c 100644 --- a/SHADE_Engine/src/Serialization/SHSerialization.cpp +++ b/SHADE_Engine/src/Serialization/SHSerialization.cpp @@ -350,7 +350,7 @@ namespace SHADE SHSerializationHelper::InitializeComponentFromNode(componentsNode, eid); SHSerializationHelper::InitializeComponentFromNode(componentsNode, eid); SHSerializationHelper::InitializeComponentFromNode(componentsNode, eid); - SHSerializationHelper::ConvertNodeToComponent(componentsNode, eid); + //SHSerializationHelper::ConvertNodeToComponent(componentsNode, eid); SHSerializationHelper::InitializeComponentFromNode(componentsNode, eid); SHSerializationHelper::InitializeComponentFromNode(componentsNode, eid); } diff --git a/SHADE_Engine/src/UI/SHUISystem.cpp b/SHADE_Engine/src/UI/SHUISystem.cpp index 95663bc0..4d175e5f 100644 --- a/SHADE_Engine/src/UI/SHUISystem.cpp +++ b/SHADE_Engine/src/UI/SHUISystem.cpp @@ -161,9 +161,9 @@ namespace SHADE windowSize = SHEditorWindowManager::GetEditorWindow()->windowSize; mousePos = SHEditorWindowManager::GetEditorWindow()->viewportMousePos; //mousePos.y = windowSize.y - mousePos.y; - SHLOG_INFO("mouse pos: {}, {}", mousePos.x, mousePos.y) - mousePos /= windowSize; - SHLOG_INFO("mouse pos normalized: {}, {}", mousePos.x, mousePos.y) + //SHLOG_INFO("mouse pos: {}, {}", mousePos.x, mousePos.y) + //mousePos /= windowSize; + //SHLOG_INFO("mouse pos normalized: {}, {}", mousePos.x, mousePos.y) #endif SHVec2 camSize{ cameraSystem->GetCameraWidthHeight(0)}; @@ -172,8 +172,8 @@ namespace SHADE topExtent = CanvasToScreenPoint(topExtent); btmExtent = CanvasToScreenPoint(btmExtent); //SHLOG_INFO("TopExtent: {}, {} Btm Extent: {}, {}", topExtent.x, topExtent.y, btmExtent.x, btmExtent.y) - topExtent /= camSize; - btmExtent /= camSize; + //topExtent /= camSize; + //btmExtent /= camSize; comp.isClicked = false; if (mousePos.x >= topExtent.x && mousePos.x <= btmExtent.x From 402bd165226338bfe6ed0c7a7cb69bd78a710484 Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Mon, 9 Jan 2023 17:18:22 +0800 Subject: [PATCH 15/24] fix for editor window size --- Assets/Application.SHConfig | 2 +- SHADE_Engine/src/UI/SHUISystem.cpp | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Assets/Application.SHConfig b/Assets/Application.SHConfig index 5673556d..3cf8846b 100644 --- a/Assets/Application.SHConfig +++ b/Assets/Application.SHConfig @@ -1,4 +1,4 @@ Start in Fullscreen: false -Starting Scene ID: 97158628 +Starting Scene ID: 97161771 Window Size: {x: 1920, y: 1080} Window Title: SHADE Engine \ No newline at end of file diff --git a/SHADE_Engine/src/UI/SHUISystem.cpp b/SHADE_Engine/src/UI/SHUISystem.cpp index 4d175e5f..eef63a73 100644 --- a/SHADE_Engine/src/UI/SHUISystem.cpp +++ b/SHADE_Engine/src/UI/SHUISystem.cpp @@ -158,10 +158,11 @@ namespace SHADE SHVec2 mousePos; SHVec2 windowSize; #ifdef SHEDITOR - windowSize = SHEditorWindowManager::GetEditorWindow()->windowSize; + windowSize = SHEditorWindowManager::GetEditorWindow()->beginContentRegionAvailable; mousePos = SHEditorWindowManager::GetEditorWindow()->viewportMousePos; //mousePos.y = windowSize.y - mousePos.y; - //SHLOG_INFO("mouse pos: {}, {}", mousePos.x, mousePos.y) + SHLOG_INFO("mouse pos: {}, {}", mousePos.x, mousePos.y) + SHLOG_INFO("window size: {}, {}", windowSize.x, windowSize.y) //mousePos /= windowSize; //SHLOG_INFO("mouse pos normalized: {}, {}", mousePos.x, mousePos.y) #endif From 356ec24cc20f93693cef18aa6ff165c3f1cfa299 Mon Sep 17 00:00:00 2001 From: mushgunAX Date: Mon, 9 Jan 2023 17:21:24 +0800 Subject: [PATCH 16/24] Change default pathing for binding file I/O --- Assets/Bindings.SHConfig | 51 ------------------------- SHADE_Engine/src/Input/SHInputManager.h | 5 ++- 2 files changed, 3 insertions(+), 53 deletions(-) diff --git a/Assets/Bindings.SHConfig b/Assets/Bindings.SHConfig index e0023603..573541ac 100644 --- a/Assets/Bindings.SHConfig +++ b/Assets/Bindings.SHConfig @@ -1,52 +1 @@ -4 -Horizontal -0 -0 -5 -0.2 -5 -0 -2 -39 -68 -2 -37 -65 -2 -3 -16 -1 -2 -Mouse Wheel -3 -0 -1 -0.2 -1 -0 -0 -0 -0 -0 -Mouse X -1 -0 -1 -0.2 -1 -0 -0 -0 -0 -0 -Mouse Y -2 -0 -1 -0.2 -1 -0 -0 -0 -0 0 diff --git a/SHADE_Engine/src/Input/SHInputManager.h b/SHADE_Engine/src/Input/SHInputManager.h index 30ee00e8..680035c3 100644 --- a/SHADE_Engine/src/Input/SHInputManager.h +++ b/SHADE_Engine/src/Input/SHInputManager.h @@ -14,6 +14,7 @@ #include #include #include "../../SHADE_Managed/src/SHpch.h" +#include "../../SHADE_Engine/src/Assets/SHAssetMacros.h" #include "SH_API.h" #pragma comment(lib, "xinput.lib") @@ -686,11 +687,11 @@ namespace SHADE /*------------------------------------------------------------------------*/ //Save bindings registered into a file - static void SaveBindings(std::string const& targetFile = "../../Assets/Bindings.SHConfig") noexcept; + static void SaveBindings(std::string const& targetFile = std::string(ASSET_ROOT) + "/Bindings.SHConfig") noexcept; //Load and register bindings from a file //If specified file exists, the current list of bindings will be overwritten, so save them somewhere else before loading - static void LoadBindings(std::string const& sourceFile = "../../Assets/Bindings.SHConfig") noexcept; + static void LoadBindings(std::string const& sourceFile = std::string(ASSET_ROOT) + "/Bindings.SHConfig") noexcept; /*------------------------------------------------------------------------*/ /* Binding Functions */ From 8b2297f45160b86cf4d2713114498f92d9b8cfba Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Mon, 9 Jan 2023 17:40:21 +0800 Subject: [PATCH 17/24] CreateSparseSet --- SHADE_Engine/src/UI/SHUISystem.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/SHADE_Engine/src/UI/SHUISystem.cpp b/SHADE_Engine/src/UI/SHUISystem.cpp index eef63a73..cd2bef82 100644 --- a/SHADE_Engine/src/UI/SHUISystem.cpp +++ b/SHADE_Engine/src/UI/SHUISystem.cpp @@ -20,6 +20,7 @@ namespace SHADE SHComponentManager::CreateComponentSparseSet(); SHComponentManager::CreateComponentSparseSet(); SHComponentManager::CreateComponentSparseSet(); + SHComponentManager::CreateComponentSparseSet(); } void SHUISystem::Exit() From 5aa7bfe03e5b1937d43f570db51cef6786862e01 Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Mon, 16 Jan 2023 07:34:44 +0800 Subject: [PATCH 18/24] button fixed --- Assets/Editor/Editor.SHConfig | 4 ++++ Assets/Scenes/UI Test.shade.shmeta | 2 +- SHADE_Engine/src/UI/SHUISystem.cpp | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) create mode 100644 Assets/Editor/Editor.SHConfig diff --git a/Assets/Editor/Editor.SHConfig b/Assets/Editor/Editor.SHConfig new file mode 100644 index 00000000..51425027 --- /dev/null +++ b/Assets/Editor/Editor.SHConfig @@ -0,0 +1,4 @@ +Start Maximized: true +Working Scene ID: 97161771 +Window Size: {x: 1920, y: 1080} +Style: 0 \ No newline at end of file diff --git a/Assets/Scenes/UI Test.shade.shmeta b/Assets/Scenes/UI Test.shade.shmeta index ad821810..9b0a85f5 100644 --- a/Assets/Scenes/UI Test.shade.shmeta +++ b/Assets/Scenes/UI Test.shade.shmeta @@ -1,3 +1,3 @@ Name: UI Test -ID: 88543249 +ID: 92992815 Type: 5 diff --git a/SHADE_Engine/src/UI/SHUISystem.cpp b/SHADE_Engine/src/UI/SHUISystem.cpp index 95663bc0..cd4ed79a 100644 --- a/SHADE_Engine/src/UI/SHUISystem.cpp +++ b/SHADE_Engine/src/UI/SHUISystem.cpp @@ -158,7 +158,7 @@ namespace SHADE SHVec2 mousePos; SHVec2 windowSize; #ifdef SHEDITOR - windowSize = SHEditorWindowManager::GetEditorWindow()->windowSize; + windowSize = SHEditorWindowManager::GetEditorWindow()->beginContentRegionAvailable; mousePos = SHEditorWindowManager::GetEditorWindow()->viewportMousePos; //mousePos.y = windowSize.y - mousePos.y; SHLOG_INFO("mouse pos: {}, {}", mousePos.x, mousePos.y) From 5190c490c9e3a6805cd9b27126c07d1e4852c6fb Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Mon, 16 Jan 2023 11:36:12 +0800 Subject: [PATCH 19/24] added events --- Assets/Scenes/UI Test.shade | 4 +- Assets/Scenes/UI Test.shade.shmeta | 2 +- SHADE_Engine/src/Events/SHEventDefines.h | 1 + .../src/UI/Events/SHButtonClickEvent.h | 16 +++ SHADE_Engine/src/UI/SHSliderComponent.cpp | 39 ++++++++ SHADE_Engine/src/UI/SHSliderComponent.h | 41 ++++++++ SHADE_Engine/src/UI/SHUISystem.cpp | 98 ++++++++++++++++--- SHADE_Engine/src/UI/SHUISystem.h | 16 ++- 8 files changed, 196 insertions(+), 21 deletions(-) create mode 100644 SHADE_Engine/src/UI/Events/SHButtonClickEvent.h create mode 100644 SHADE_Engine/src/UI/SHSliderComponent.cpp create mode 100644 SHADE_Engine/src/UI/SHSliderComponent.h diff --git a/Assets/Scenes/UI Test.shade b/Assets/Scenes/UI Test.shade index 0aea6f72..d9ce5026 100644 --- a/Assets/Scenes/UI Test.shade +++ b/Assets/Scenes/UI Test.shade @@ -34,7 +34,7 @@ NumberOfChildren: 0 Components: Transform Component: - Translate: {x: 153.399994, y: 0, z: 0} + Translate: {x: 0, y: -3.9000001, z: 0} Rotate: {x: 0, y: 0, z: 0} Scale: {x: 1, y: 1, z: 1} IsActive: true @@ -45,7 +45,7 @@ Toggle Button Component: Non Toggled Texture: 0 Toggled Texture: 0 - Value: false + Value: true IsActive: true Scripts: ~ - EID: 1 diff --git a/Assets/Scenes/UI Test.shade.shmeta b/Assets/Scenes/UI Test.shade.shmeta index 9b0a85f5..0ce3b040 100644 --- a/Assets/Scenes/UI Test.shade.shmeta +++ b/Assets/Scenes/UI Test.shade.shmeta @@ -1,3 +1,3 @@ Name: UI Test -ID: 92992815 +ID: 92642496 Type: 5 diff --git a/SHADE_Engine/src/Events/SHEventDefines.h b/SHADE_Engine/src/Events/SHEventDefines.h index d7bbf5f0..1ad458c4 100644 --- a/SHADE_Engine/src/Events/SHEventDefines.h +++ b/SHADE_Engine/src/Events/SHEventDefines.h @@ -18,4 +18,5 @@ constexpr SHEventIdentifier SH_PHYSICS_COLLIDER_REMOVED_EVENT { 9 }; constexpr SHEventIdentifier SH_EDITOR_ON_PLAY_EVENT { 10 }; constexpr SHEventIdentifier SH_EDITOR_ON_PAUSE_EVENT { 11 }; constexpr SHEventIdentifier SH_EDITOR_ON_STOP_EVENT { 12 }; +constexpr SHEventIdentifier SH_BUTTON_CLICK_EVENT { 13 }; diff --git a/SHADE_Engine/src/UI/Events/SHButtonClickEvent.h b/SHADE_Engine/src/UI/Events/SHButtonClickEvent.h new file mode 100644 index 00000000..35bcdc61 --- /dev/null +++ b/SHADE_Engine/src/UI/Events/SHButtonClickEvent.h @@ -0,0 +1,16 @@ +#pragma once + + +#include "ECS_Base/SHECSMacros.h" + +namespace SHADE +{ + struct SHButtonClickEvent + { + EntityID EID; + // value of the toggle button, default to false if its a button and not a toggle button + bool value{false}; + }; + + +} diff --git a/SHADE_Engine/src/UI/SHSliderComponent.cpp b/SHADE_Engine/src/UI/SHSliderComponent.cpp new file mode 100644 index 00000000..56d1d89b --- /dev/null +++ b/SHADE_Engine/src/UI/SHSliderComponent.cpp @@ -0,0 +1,39 @@ +#include "SHpch.h" +#include "SHSliderComponent.h" + +namespace SHADE +{ + SHSliderComponent::SHSliderComponent() + :size(1.0f), isHovered(false), isClicked(false), value(0.0f) + { + + } + + + float SHSliderComponent::GetValue() const noexcept + { + return value; + } + + void SHSliderComponent::SetValue(float value) noexcept + { + this->value = value; + } + + + +} + + +RTTR_REGISTRATION +{ + using namespace SHADE; + using namespace rttr; + + registration::class_("Slider Component") + .property("Slider Value", &SHSliderComponent::GetValue, &SHSliderComponent::SetValue) + + ; + + +} \ No newline at end of file diff --git a/SHADE_Engine/src/UI/SHSliderComponent.h b/SHADE_Engine/src/UI/SHSliderComponent.h new file mode 100644 index 00000000..bdc57c7e --- /dev/null +++ b/SHADE_Engine/src/UI/SHSliderComponent.h @@ -0,0 +1,41 @@ +#pragma once + +#include + +#include "SH_API.h" +#include "ECS_Base/Components/SHComponent.h" +#include "Math/Vector/SHVec3.h" +#include "Math/Vector/SHVec2.h" +#include "Assets/SHAssetMacros.h" + +namespace SHADE +{ + + class SH_API SHSliderComponent final: public SHComponent + { + public: + SHSliderComponent(); + virtual ~SHSliderComponent() = default; + + SHVec2 size; + + + float GetValue() const noexcept; + + + void SetValue(float value) noexcept; + + + friend class SHUISystem; + private: + + bool isHovered; + bool isClicked; + + float value; + + RTTR_ENABLE() + }; + + +} \ No newline at end of file diff --git a/SHADE_Engine/src/UI/SHUISystem.cpp b/SHADE_Engine/src/UI/SHUISystem.cpp index cd4ed79a..bb98939b 100644 --- a/SHADE_Engine/src/UI/SHUISystem.cpp +++ b/SHADE_Engine/src/UI/SHUISystem.cpp @@ -10,6 +10,13 @@ #include "Editor/SHEditor.h" #include "Resource/SHResourceManager.h" #include "Input/SHInputManager.h" +#include "SHUIComponent.h" +#include "SHButtonComponent.h" +#include "SHToggleButtonComponent.h" +#include "SHSliderComponent.h" +#include "SHCanvasComponent.h" +#include "Events/SHEventManager.hpp" +#include "Events/SHButtonClickEvent.h" namespace SHADE { @@ -164,32 +171,60 @@ namespace SHADE SHLOG_INFO("mouse pos: {}, {}", mousePos.x, mousePos.y) mousePos /= windowSize; SHLOG_INFO("mouse pos normalized: {}, {}", mousePos.x, mousePos.y) + + + +#else + + int x, y; + SHInputManager::GetMouseScreenPosition(&x, &y); + mousePos.x = x; + mousePos.y = y; + auto ws = SHSystemManager::GetSystem()->GetWindow()->GetWindowSize(); + windowSize = { ws.first,ws.second }; + mousePos /= windowSize; #endif SHVec2 camSize{ cameraSystem->GetCameraWidthHeight(0)}; //SHLOG_INFO("TopExtent: {}, {}", topExtent.x, topExtent.y) - topExtent = CanvasToScreenPoint(topExtent); - btmExtent = CanvasToScreenPoint(btmExtent); + topExtent = CanvasToScreenPoint(topExtent,true); + btmExtent = CanvasToScreenPoint(btmExtent,true); //SHLOG_INFO("TopExtent: {}, {} Btm Extent: {}, {}", topExtent.x, topExtent.y, btmExtent.x, btmExtent.y) - topExtent /= camSize; - btmExtent /= camSize; + comp.isClicked = false; if (mousePos.x >= topExtent.x && mousePos.x <= btmExtent.x && mousePos.y >= topExtent.y && mousePos.y <= btmExtent.y) { comp.isHovered = true; +#ifdef SHEDITOR + if (SHSystemManager::GetSystem()->editorState == SHEditor::State::PLAY) + { + if (SHInputManager::GetKeyUp(SHInputManager::SH_KEYCODE::LMB)) + { + comp.isClicked = true; + SHButtonClickEvent clickEvent; + clickEvent.EID = comp.GetEID(); + SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_CLICK_EVENT); + } + } +#else if (SHInputManager::GetKeyUp(SHInputManager::SH_KEYCODE::LMB)) { comp.isClicked = true; + SHButtonClickEvent clickEvent; + clickEvent.EID = comp.GetEID(); + SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_CLICK_EVENT); } - SHLOG_INFO("HOVERED") +#endif + + //SHLOG_INFO("HOVERED") } else { comp.isHovered = false; - SHLOG_INFO("NOT HOVERED") + //SHLOG_INFO("NOT HOVERED") } @@ -222,9 +257,25 @@ namespace SHADE SHVec2 mousePos; SHVec2 windowSize; + #ifdef SHEDITOR - windowSize = SHEditorWindowManager::GetEditorWindow()->windowSize; + windowSize = SHEditorWindowManager::GetEditorWindow()->beginContentRegionAvailable; mousePos = SHEditorWindowManager::GetEditorWindow()->viewportMousePos; + //mousePos.y = windowSize.y - mousePos.y; + SHLOG_INFO("mouse pos: {}, {}", mousePos.x, mousePos.y) + mousePos /= windowSize; + SHLOG_INFO("mouse pos normalized: {}, {}", mousePos.x, mousePos.y) + + + +#else + + int x, y; + SHInputManager::GetMouseScreenPosition(&x, &y); + mousePos.x = x; + mousePos.y = y; + auto ws = SHSystemManager::GetSystem()->GetWindow()->GetWindowSize(); + windowSize = { ws.first,ws.second }; mousePos /= windowSize; #endif @@ -232,10 +283,9 @@ namespace SHADE - topExtent = CanvasToScreenPoint(topExtent); - btmExtent = CanvasToScreenPoint(btmExtent); - topExtent /= camSize; - btmExtent /= camSize; + topExtent = CanvasToScreenPoint(topExtent,true); + btmExtent = CanvasToScreenPoint(btmExtent, true); + comp.isClicked = false; @@ -243,11 +293,30 @@ namespace SHADE && mousePos.y >= topExtent.y && mousePos.y <= btmExtent.y) { comp.isHovered = true; +#ifdef SHEDITOR + if (SHSystemManager::GetSystem()->editorState == SHEditor::State::PLAY) + { + if (SHInputManager::GetKeyUp(SHInputManager::SH_KEYCODE::LMB)) + { + comp.isClicked = true; + comp.value = !comp.value; + SHButtonClickEvent clickEvent; + clickEvent.EID = comp.GetEID(); + clickEvent.value = comp.value; + SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_CLICK_EVENT); + } + } +#else if (SHInputManager::GetKeyUp(SHInputManager::SH_KEYCODE::LMB)) { comp.isClicked = true; comp.value = !comp.value; + SHButtonClickEvent clickEvent; + clickEvent.EID = comp.GetEID(); + clickEvent.value = comp.value; + SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_CLICK_EVENT); } +#endif } else { @@ -284,7 +353,7 @@ namespace SHADE } } - SHVec2 SHUISystem::CanvasToScreenPoint(SHVec2& const canvasPoint) noexcept + SHVec2 SHUISystem::CanvasToScreenPoint(SHVec2& const canvasPoint, bool normalized) noexcept { SHVec2 result{canvasPoint}; @@ -296,7 +365,10 @@ namespace SHADE result.y *= -1.0f; result += camSize * 0.5f; - return result; + if (normalized) + return result / camSize; + else + return result; } diff --git a/SHADE_Engine/src/UI/SHUISystem.h b/SHADE_Engine/src/UI/SHUISystem.h index a5ba8c4f..ae1091ec 100644 --- a/SHADE_Engine/src/UI/SHUISystem.h +++ b/SHADE_Engine/src/UI/SHUISystem.h @@ -3,15 +3,20 @@ #include "SH_API.h" #include "ECS_Base/System/SHSystem.h" #include "ECS_Base/System/SHSystemRoutine.h" -#include "SHUIComponent.h" -#include "SHButtonComponent.h" -#include "SHToggleButtonComponent.h" -#include "SHCanvasComponent.h" + #include "Scene/SHSceneGraph.h" #include "Scene/SHSceneManager.h" +#include "Math/Vector/SHVec2.h" namespace SHADE { + + class SHButtonComponent; + class SHUIComponent; + class SHToggleButtonComponent; + class SHSliderComponent; + class SHCanvasComponent; + class SH_API SHUISystem final: public SHSystem { public: @@ -67,7 +72,8 @@ namespace SHADE void UpdateButtonComponent(SHButtonComponent& comp) noexcept; void UpdateToggleButtonComponent(SHToggleButtonComponent& comp) noexcept; void UpdateCanvasComponent(SHCanvasComponent& comp) noexcept; - SHVec2 CanvasToScreenPoint(SHVec2& const canvasPoint) noexcept; + + SHVec2 CanvasToScreenPoint(SHVec2& const canvasPoint, bool normalized) noexcept; From 02ba0c6dc91b8ca172659c3499617f4729a75a6d Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Mon, 16 Jan 2023 11:44:34 +0800 Subject: [PATCH 20/24] merge --- Assets/Scenes/UI_Test.shade.shmeta | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Assets/Scenes/UI_Test.shade.shmeta b/Assets/Scenes/UI_Test.shade.shmeta index 0ce3b040..77355480 100644 --- a/Assets/Scenes/UI_Test.shade.shmeta +++ b/Assets/Scenes/UI_Test.shade.shmeta @@ -1,3 +1,3 @@ -Name: UI Test -ID: 92642496 +Name: UI_Test +ID: 87244611 Type: 5 From a41354f2cedf88697a94b3cb2e4668c693688712 Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Mon, 16 Jan 2023 14:35:16 +0800 Subject: [PATCH 21/24] Added changing texture of buttons --- Assets/Application.SHConfig | 2 +- Assets/Scenes/UI_Test.shade | 2 +- SHADE_Engine/src/UI/SHButtonComponent.h | 4 +- SHADE_Engine/src/UI/SHToggleButtonComponent.h | 3 + SHADE_Engine/src/UI/SHUISystem.cpp | 120 ++++++++++++------ 5 files changed, 89 insertions(+), 42 deletions(-) diff --git a/Assets/Application.SHConfig b/Assets/Application.SHConfig index 5673556d..ee5e42a8 100644 --- a/Assets/Application.SHConfig +++ b/Assets/Application.SHConfig @@ -1,4 +1,4 @@ Start in Fullscreen: false -Starting Scene ID: 97158628 +Starting Scene ID: 87244611 Window Size: {x: 1920, y: 1080} Window Title: SHADE Engine \ No newline at end of file diff --git a/Assets/Scenes/UI_Test.shade b/Assets/Scenes/UI_Test.shade index d9ce5026..0026a48b 100644 --- a/Assets/Scenes/UI_Test.shade +++ b/Assets/Scenes/UI_Test.shade @@ -14,7 +14,7 @@ NumberOfChildren: 0 Components: Transform Component: - Translate: {x: -3.5999999, y: 3.0999999, z: 0} + Translate: {x: 0, y: 0, z: 0} Rotate: {x: 0, y: 0, z: 0} Scale: {x: 1, y: 1, z: 1} IsActive: true diff --git a/SHADE_Engine/src/UI/SHButtonComponent.h b/SHADE_Engine/src/UI/SHButtonComponent.h index be6ada3e..3aac09e6 100644 --- a/SHADE_Engine/src/UI/SHButtonComponent.h +++ b/SHADE_Engine/src/UI/SHButtonComponent.h @@ -32,8 +32,10 @@ namespace SHADE friend class SHUISystem; private: - + //Set to true when mouse is hovering over the button. bool isHovered; + //This is set to true when the mouse clicks down, and set back to false when mouse releases. + //The event for the button click will be broadcasted when mouse release. bool isClicked; AssetID defaultTexture; AssetID hoveredTexture; diff --git a/SHADE_Engine/src/UI/SHToggleButtonComponent.h b/SHADE_Engine/src/UI/SHToggleButtonComponent.h index 3217c892..2c77f3ba 100644 --- a/SHADE_Engine/src/UI/SHToggleButtonComponent.h +++ b/SHADE_Engine/src/UI/SHToggleButtonComponent.h @@ -33,7 +33,10 @@ namespace SHADE friend class SHUISystem; private: + //Set to true when mouse is hovering over the button. bool isHovered; + //This is set to true when the mouse clicks down, and set back to false when mouse releases. + //The event for the button click will be broadcasted when mouse release. bool isClicked; bool value; AssetID defaultTexture; diff --git a/SHADE_Engine/src/UI/SHUISystem.cpp b/SHADE_Engine/src/UI/SHUISystem.cpp index bb98939b..84f6a21c 100644 --- a/SHADE_Engine/src/UI/SHUISystem.cpp +++ b/SHADE_Engine/src/UI/SHUISystem.cpp @@ -9,6 +9,7 @@ #include "Editor/EditorWindow/ViewportWindow/SHEditorViewport.h" #include "Editor/SHEditor.h" #include "Resource/SHResourceManager.h" +#include "Assets/SHAssetManager.h" #include "Input/SHInputManager.h" #include "SHUIComponent.h" #include "SHButtonComponent.h" @@ -168,9 +169,9 @@ namespace SHADE windowSize = SHEditorWindowManager::GetEditorWindow()->beginContentRegionAvailable; mousePos = SHEditorWindowManager::GetEditorWindow()->viewportMousePos; //mousePos.y = windowSize.y - mousePos.y; - SHLOG_INFO("mouse pos: {}, {}", mousePos.x, mousePos.y) + //SHLOG_INFO("mouse pos: {}, {}", mousePos.x, mousePos.y) mousePos /= windowSize; - SHLOG_INFO("mouse pos normalized: {}, {}", mousePos.x, mousePos.y) + //SHLOG_INFO("mouse pos normalized: {}, {}", mousePos.x, mousePos.y) @@ -193,31 +194,25 @@ namespace SHADE //SHLOG_INFO("TopExtent: {}, {} Btm Extent: {}, {}", topExtent.x, topExtent.y, btmExtent.x, btmExtent.y) - comp.isClicked = false; + //comp.isClicked = false; if (mousePos.x >= topExtent.x && mousePos.x <= btmExtent.x && mousePos.y >= topExtent.y && mousePos.y <= btmExtent.y) { comp.isHovered = true; -#ifdef SHEDITOR - if (SHSystemManager::GetSystem()->editorState == SHEditor::State::PLAY) - { - if (SHInputManager::GetKeyUp(SHInputManager::SH_KEYCODE::LMB)) + #ifdef SHEDITOR + //if (SHSystemManager::GetSystem()->editorState == SHEditor::State::PLAY) + { + if (SHInputManager::GetKeyDown(SHInputManager::SH_KEYCODE::LMB)) + { + comp.isClicked = true; + } + } + #else + if (SHInputManager::GetKeyDown(SHInputManager::SH_KEYCODE::LMB)) { comp.isClicked = true; - SHButtonClickEvent clickEvent; - clickEvent.EID = comp.GetEID(); - SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_CLICK_EVENT); } - } -#else - if (SHInputManager::GetKeyUp(SHInputManager::SH_KEYCODE::LMB)) - { - comp.isClicked = true; - SHButtonClickEvent clickEvent; - clickEvent.EID = comp.GetEID(); - SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_CLICK_EVENT); - } -#endif + #endif //SHLOG_INFO("HOVERED") } @@ -226,15 +221,46 @@ namespace SHADE comp.isHovered = false; //SHLOG_INFO("NOT HOVERED") } - + if (comp.isClicked && SHInputManager::GetKeyUp(SHInputManager::SH_KEYCODE::LMB)) + { + comp.isClicked = false; + SHButtonClickEvent clickEvent; + clickEvent.EID = comp.GetEID(); + SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_CLICK_EVENT); + } if (SHComponentManager::HasComponent(comp.GetEID())) { - /*auto renderable = SHComponentManager::GetComponent_s(comp.GetEID()); + auto renderable = SHComponentManager::GetComponent_s(comp.GetEID()); auto texture = SHResourceManager::Get(comp.GetDefaultTexture()); auto material = renderable->GetModifiableMaterial(); - material->SetProperty("texture", comp.GetDefaultTexture());*/ + if(!comp.isHovered && !comp.isClicked) + if (SHAssetManager::GetType(comp.GetDefaultTexture()) == AssetType::TEXTURE) + { + material->SetProperty("data.textureIndex", comp.GetDefaultTexture()); + //SHLOG_INFO("SETTING DEFAULT TEXTURE") + } + + if (comp.isHovered) + { + if (SHAssetManager::GetType(comp.GetHoveredTexture()) == AssetType::TEXTURE) + { + material->SetProperty("data.textureIndex", comp.GetHoveredTexture()); + //SHLOG_INFO("SETTING HOVERED TEXTURE") + } + } + else + { + if (SHAssetManager::GetType(comp.GetClickedTexture()) == AssetType::TEXTURE) + { + material->SetProperty("data.textureIndex", comp.GetClickedTexture()); + SHLOG_INFO("SETTING CLICKED TEXTURE") + } + } + + + } } @@ -262,9 +288,9 @@ namespace SHADE windowSize = SHEditorWindowManager::GetEditorWindow()->beginContentRegionAvailable; mousePos = SHEditorWindowManager::GetEditorWindow()->viewportMousePos; //mousePos.y = windowSize.y - mousePos.y; - SHLOG_INFO("mouse pos: {}, {}", mousePos.x, mousePos.y) + //SHLOG_INFO("mouse pos: {}, {}", mousePos.x, mousePos.y) mousePos /= windowSize; - SHLOG_INFO("mouse pos normalized: {}, {}", mousePos.x, mousePos.y) + //SHLOG_INFO("mouse pos normalized: {}, {}", mousePos.x, mousePos.y) @@ -296,25 +322,15 @@ namespace SHADE #ifdef SHEDITOR if (SHSystemManager::GetSystem()->editorState == SHEditor::State::PLAY) { - if (SHInputManager::GetKeyUp(SHInputManager::SH_KEYCODE::LMB)) + if (SHInputManager::GetKeyDown(SHInputManager::SH_KEYCODE::LMB)) { comp.isClicked = true; - comp.value = !comp.value; - SHButtonClickEvent clickEvent; - clickEvent.EID = comp.GetEID(); - clickEvent.value = comp.value; - SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_CLICK_EVENT); } } #else - if (SHInputManager::GetKeyUp(SHInputManager::SH_KEYCODE::LMB)) + if (SHInputManager::GetKeyDown(SHInputManager::SH_KEYCODE::LMB)) { comp.isClicked = true; - comp.value = !comp.value; - SHButtonClickEvent clickEvent; - clickEvent.EID = comp.GetEID(); - clickEvent.value = comp.value; - SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_CLICK_EVENT); } #endif } @@ -324,13 +340,39 @@ namespace SHADE } + if (comp.isClicked && SHInputManager::GetKeyUp(SHInputManager::SH_KEYCODE::LMB)) + { + comp.isClicked = false; + comp.value = !comp.value; + SHButtonClickEvent clickEvent; + clickEvent.EID = comp.GetEID(); + clickEvent.value = comp.value; + SHEventManager::BroadcastEvent(clickEvent, SH_BUTTON_CLICK_EVENT); + } + if (SHComponentManager::HasComponent(comp.GetEID())) { - /*auto renderable = SHComponentManager::GetComponent_s(comp.GetEID()); + auto renderable = SHComponentManager::GetComponent_s(comp.GetEID()); auto texture = SHResourceManager::Get(comp.GetDefaultTexture()); auto material = renderable->GetModifiableMaterial(); - material->SetProperty("texture", comp.GetDefaultTexture());*/ + if (comp.GetValue() == false) + { + if (SHAssetManager::GetType(comp.GetDefaultTexture()) == AssetType::TEXTURE) + { + material->SetProperty("data.textureIndex", comp.GetDefaultTexture()); + //SHLOG_INFO("SETTING DEFAULT TEXTURE") + } + } + else + { + if (SHAssetManager::GetType(comp.GetToggledTexture()) == AssetType::TEXTURE) + { + material->SetProperty("data.textureIndex", comp.GetToggledTexture()); + //SHLOG_INFO("SETTING DEFAULT TEXTURE") + } + } + } } From b60304457977b9e874b3ff44606a25f5f0916966 Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Mon, 16 Jan 2023 14:39:35 +0800 Subject: [PATCH 22/24] Fixed validation errors caused by debug draw and fixed SHDebugDraw::WireCapsule not being static --- .../Graphics/MiddleEnd/Interface/SHDebugDrawSystem.cpp | 9 ++++++++- SHADE_Engine/src/Tools/SHDebugDraw.h | 4 ++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.cpp index ae8c62b2..5a694516 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHDebugDrawSystem.cpp @@ -104,6 +104,10 @@ namespace SHADE auto subPass = renderGraph->GetNode("Debug Draw")->GetSubpass("Debug Draw"); subPass->AddExteriorDrawCalls([this](Handle cmdBuffer, Handle renderer, uint32_t frameIndex) { + // Set line width first + cmdBuffer->SetLineWidth(LineWidth); + + // Draw const uint32_t FRAME_IDX = gfxSystem->GetCurrentFrameIndex(); cmdBuffer->BeginLabeledSegment("SHDebugDraw (No Depth Test)"); { @@ -128,6 +132,10 @@ namespace SHADE auto subPassWithDepth = renderGraph->GetNode("Debug Draw with Depth")->GetSubpass("Debug Draw with Depth"); subPassWithDepth->AddExteriorDrawCalls([this](Handle cmdBuffer, Handle renderer, uint32_t frameIndex) { + // Set line width first + cmdBuffer->SetLineWidth(LineWidth); + + // Draw const uint32_t FRAME_IDX = gfxSystem->GetCurrentFrameIndex(); cmdBuffer->BeginLabeledSegment("SHDebugDraw (Depth Tested)"); { @@ -506,7 +514,6 @@ namespace SHADE if (batch.NumPoints[frameIndex] > 0) { cmdBuffer->BindPipeline(batch.Pipeline); - cmdBuffer->SetLineWidth(LineWidth); cmdBuffer->BindVertexBuffer(0, batch.VertexBuffers[frameIndex], 0); cmdBuffer->DrawArrays(batch.NumPoints[frameIndex], 1, 0, 0); } diff --git a/SHADE_Engine/src/Tools/SHDebugDraw.h b/SHADE_Engine/src/Tools/SHDebugDraw.h index 3d7bee2f..c775a514 100644 --- a/SHADE_Engine/src/Tools/SHDebugDraw.h +++ b/SHADE_Engine/src/Tools/SHDebugDraw.h @@ -205,7 +205,7 @@ namespace SHADE /// /// Colour to draw with. /// Whether or not drawn object will be occluded. - void WireCapsule(const SHVec3& position, const SHQuaternion& rotation, float height, float radius, const SHColour& color = SHColour::WHITE, bool depthTested = false); + static void WireCapsule(const SHVec3& position, const SHQuaternion& rotation, float height, float radius, const SHColour& color = SHColour::WHITE, bool depthTested = false); /*---------------------------------------------------------------------------------*/ /* Persistent Draw Function Class "Folder" */ @@ -388,7 +388,7 @@ namespace SHADE /// /// Colour to draw with. /// Whether or not drawn object will be occluded. - void WireCapsule(const SHVec3& position, const SHQuaternion& rotation, float height, float radius, const SHColour& color = SHColour::WHITE, bool depthTested = false); + static void WireCapsule(const SHVec3& position, const SHQuaternion& rotation, float height, float radius, const SHColour& color = SHColour::WHITE, bool depthTested = false); /// /// Clears any persistent drawn debug primitives. /// From 1e351366afccf40c5723f087ad2b4fd4fd7be9ec Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Mon, 16 Jan 2023 14:44:20 +0800 Subject: [PATCH 23/24] fix clicked texture to take priority over hovered texture --- SHADE_Engine/src/UI/SHUISystem.cpp | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/SHADE_Engine/src/UI/SHUISystem.cpp b/SHADE_Engine/src/UI/SHUISystem.cpp index 84f6a21c..11808038 100644 --- a/SHADE_Engine/src/UI/SHUISystem.cpp +++ b/SHADE_Engine/src/UI/SHUISystem.cpp @@ -232,7 +232,7 @@ namespace SHADE if (SHComponentManager::HasComponent(comp.GetEID())) { auto renderable = SHComponentManager::GetComponent_s(comp.GetEID()); - auto texture = SHResourceManager::Get(comp.GetDefaultTexture()); + //auto texture = SHResourceManager::Get(comp.GetDefaultTexture()); auto material = renderable->GetModifiableMaterial(); if(!comp.isHovered && !comp.isClicked) @@ -240,9 +240,16 @@ namespace SHADE { material->SetProperty("data.textureIndex", comp.GetDefaultTexture()); //SHLOG_INFO("SETTING DEFAULT TEXTURE") + } + else if (comp.isClicked) + { + if (SHAssetManager::GetType(comp.GetClickedTexture()) == AssetType::TEXTURE) + { + material->SetProperty("data.textureIndex", comp.GetClickedTexture()); + //SHLOG_INFO("SETTING CLICKED TEXTURE") + } } - - if (comp.isHovered) + else { if (SHAssetManager::GetType(comp.GetHoveredTexture()) == AssetType::TEXTURE) { @@ -250,14 +257,6 @@ namespace SHADE //SHLOG_INFO("SETTING HOVERED TEXTURE") } } - else - { - if (SHAssetManager::GetType(comp.GetClickedTexture()) == AssetType::TEXTURE) - { - material->SetProperty("data.textureIndex", comp.GetClickedTexture()); - SHLOG_INFO("SETTING CLICKED TEXTURE") - } - } @@ -353,7 +352,7 @@ namespace SHADE if (SHComponentManager::HasComponent(comp.GetEID())) { auto renderable = SHComponentManager::GetComponent_s(comp.GetEID()); - auto texture = SHResourceManager::Get(comp.GetDefaultTexture()); + //auto texture = SHResourceManager::Get(comp.GetDefaultTexture()); auto material = renderable->GetModifiableMaterial(); if (comp.GetValue() == false) From cdb5102630f84e10757bb2b01860cd880b067fd8 Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Mon, 16 Jan 2023 14:51:06 +0800 Subject: [PATCH 24/24] Added a 0 check for button textures. --- SHADE_Engine/src/UI/SHUISystem.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/SHADE_Engine/src/UI/SHUISystem.cpp b/SHADE_Engine/src/UI/SHUISystem.cpp index 11808038..c75af66f 100644 --- a/SHADE_Engine/src/UI/SHUISystem.cpp +++ b/SHADE_Engine/src/UI/SHUISystem.cpp @@ -236,14 +236,14 @@ namespace SHADE auto material = renderable->GetModifiableMaterial(); if(!comp.isHovered && !comp.isClicked) - if (SHAssetManager::GetType(comp.GetDefaultTexture()) == AssetType::TEXTURE) + if (comp.GetDefaultTexture() != 0 && SHAssetManager::GetType(comp.GetDefaultTexture()) == AssetType::TEXTURE) { material->SetProperty("data.textureIndex", comp.GetDefaultTexture()); //SHLOG_INFO("SETTING DEFAULT TEXTURE") } else if (comp.isClicked) { - if (SHAssetManager::GetType(comp.GetClickedTexture()) == AssetType::TEXTURE) + if (comp.GetClickedTexture() != 0 && SHAssetManager::GetType(comp.GetClickedTexture()) == AssetType::TEXTURE) { material->SetProperty("data.textureIndex", comp.GetClickedTexture()); //SHLOG_INFO("SETTING CLICKED TEXTURE") @@ -251,7 +251,7 @@ namespace SHADE } else { - if (SHAssetManager::GetType(comp.GetHoveredTexture()) == AssetType::TEXTURE) + if (comp.GetHoveredTexture() != 0 && SHAssetManager::GetType(comp.GetHoveredTexture()) == AssetType::TEXTURE) { material->SetProperty("data.textureIndex", comp.GetHoveredTexture()); //SHLOG_INFO("SETTING HOVERED TEXTURE") @@ -357,7 +357,7 @@ namespace SHADE auto material = renderable->GetModifiableMaterial(); if (comp.GetValue() == false) { - if (SHAssetManager::GetType(comp.GetDefaultTexture()) == AssetType::TEXTURE) + if (comp.GetDefaultTexture()!= 0 && SHAssetManager::GetType(comp.GetDefaultTexture()) == AssetType::TEXTURE) { material->SetProperty("data.textureIndex", comp.GetDefaultTexture()); //SHLOG_INFO("SETTING DEFAULT TEXTURE") @@ -365,7 +365,7 @@ namespace SHADE } else { - if (SHAssetManager::GetType(comp.GetToggledTexture()) == AssetType::TEXTURE) + if (comp.GetToggledTexture() != 0 && SHAssetManager::GetType(comp.GetToggledTexture()) == AssetType::TEXTURE) { material->SetProperty("data.textureIndex", comp.GetToggledTexture()); //SHLOG_INFO("SETTING DEFAULT TEXTURE")