Added in Raycasting through Physics #226

Merged
direnbharwani merged 5 commits from SP3-2-Physics into main 2022-11-19 16:24:50 +08:00
30 changed files with 592 additions and 125 deletions
Showing only changes of commit a4e5a1c269 - Show all commits

View File

@ -33,6 +33,7 @@
#include "Physics/System/SHPhysicsSystem.h"
#include "Physics/System/SHPhysicsDebugDrawSystem.h"
#include "Scripting/SHScriptEngine.h"
#include "UI/SHUISystem.h"
// Components
#include "Graphics/MiddleEnd/Interface/SHRenderable.h"
@ -77,6 +78,7 @@ namespace Sandbox
SHSystemManager::CreateSystem<SHAudioSystem>();
SHSystemManager::CreateSystem<SHCameraSystem>();
SHSystemManager::CreateSystem<SHUISystem>();
SHSystemManager::CreateSystem<SHGraphicsSystem>();
@ -120,6 +122,8 @@ namespace Sandbox
SHSystemManager::RegisterRoutine<SHGraphicsSystem, SHGraphicsSystem::BeginRoutine>();
//SHSystemManager::RegisterRoutine<SHCameraSystem, SHCameraSystem::EditorCameraUpdate>();
SHSystemManager::RegisterRoutine<SHUISystem, SHUISystem::AddUIComponentRoutine>();
SHSystemManager::RegisterRoutine<SHUISystem, SHUISystem::UpdateUIMatrixRoutine>();
SHSystemManager::RegisterRoutine<SHCameraSystem, SHCameraSystem::CameraSystemUpdate>();
#ifdef SHEDITOR

View File

@ -143,6 +143,18 @@ namespace SHADE
return result;
}
AssetType SHAssetManager::GetType(AssetID id) noexcept
{
if (assetCollection.contains(id))
{
return assetCollection[id].type;
}
SHLOG_WARNING("AssetID {}, does not belong to an asset", id)
return AssetType::INVALID;
}
std::optional<SHADE::SHAsset> SHAssetManager::GetAsset(AssetID id) noexcept
{
if (assetCollection.contains(id))

View File

@ -52,6 +52,8 @@ namespace SHADE
static std::vector<SHAsset> GetAllAssets() noexcept;
static std::optional<SHAsset> GetAsset(AssetID id) noexcept;
static AssetType GetType(AssetID id) noexcept;
/****************************************************************************
* \brief Create record for new resource. CAN ONLY CREATE FOR CUSTOM
* RESOURCES CREATED BY THE ENGINE.

View File

@ -12,7 +12,7 @@ namespace SHADE
:yaw(0.0f), pitch(0.0f), roll(0.0f)
, width(1920.0f), height(1080.0f), zNear(0.01f), zFar(10000.0f), fov(90.0f), movementSpeed(1.0f), turnSpeed(0.5f)
, perspProj(true), dirtyView(true), dirtyProj(true)
, viewMatrix(), projMatrix()
, viewMatrix(), perspProjMatrix(), orthoProjMatrix()
, position(), offset()
{
ComponentFamily::GetID<SHCameraComponent>();
@ -213,9 +213,22 @@ namespace SHADE
const SHMatrix& SHCameraComponent::GetProjMatrix() const noexcept
{
return projMatrix;
if (perspProj)
return perspProjMatrix;
else
return orthoProjMatrix;
}
const SHMatrix& SHCameraComponent::GetOrthoMatrix() const noexcept
{
return orthoProjMatrix;
}
const SHMatrix& SHCameraComponent::GetPerspectiveMatrix() const noexcept
{
return orthoProjMatrix;
}
//void SHCameraComponent::SetMainCamera(size_t directorCameraIndex) noexcept
//{
// auto system = SHSystemManager::GetSystem<SHCameraSystem>();

View File

@ -29,7 +29,8 @@ namespace SHADE
SHMatrix viewMatrix;
SHMatrix projMatrix;
SHMatrix perspProjMatrix;
SHMatrix orthoProjMatrix;
SHVec3 position;
bool perspProj;
@ -37,6 +38,7 @@ namespace SHADE
public:
friend class SHCameraSystem;
@ -78,6 +80,8 @@ namespace SHADE
const SHMatrix& GetViewMatrix() const noexcept;
const SHMatrix& GetProjMatrix() const noexcept;
const SHMatrix& GetOrthoMatrix() const noexcept;
const SHMatrix& GetPerspectiveMatrix() const noexcept;
//void SetMainCamera(size_t cameraDirectorIndex = 0) noexcept;

View File

@ -15,36 +15,42 @@ namespace SHADE
}
SHMatrix SHCameraDirector::GetViewMatrix() const noexcept
SHMatrix const& SHCameraDirector::GetViewMatrix() const noexcept
{
return viewMatrix;
}
SHMatrix SHCameraDirector::GetProjMatrix() const noexcept
SHMatrix const& SHCameraDirector::GetProjMatrix() const noexcept
{
return projMatrix;
}
SHMatrix SHCameraDirector::GetVPMatrix() const noexcept
SHMatrix const& SHCameraDirector::GetVPMatrix() const noexcept
{
return projMatrix * viewMatrix;
}
SHCameraComponent* SHCameraDirector::GetMainCameraComponent() noexcept
{
if (mainCameraEID == MAX_EID)
{
auto& dense = SHComponentManager::GetDense<SHCameraComponent>();
if (dense.size() == 0)
{
return nullptr;
}
mainCameraEID = dense[0].GetEID();
}
SHCameraComponent* camComponent = SHComponentManager::GetComponent_s<SHCameraComponent>(mainCameraEID);
if (!camComponent)
{
SHLOG_WARNING("Camera Director warning: Entity does not have a camera");
}
}
void SHCameraDirector::UpdateMatrix() noexcept
{
if (mainCameraEID == MAX_EID)
{
auto& dense = SHComponentManager::GetDense<SHCameraComponent>();
if (dense.size() == 0)
{
return;
}
mainCameraEID = dense[0].GetEID();
}
SHCameraComponent* camComponent = SHComponentManager::GetComponent_s<SHCameraComponent>(mainCameraEID);
if (!camComponent)
{
SHLOG_WARNING("Camera Director warning: Entity does not have a camera");
}
else
SHCameraComponent* camComponent = GetMainCameraComponent();
if(camComponent)
{
viewMatrix = camComponent->GetViewMatrix();
projMatrix = camComponent->GetProjMatrix();
@ -62,6 +68,24 @@ namespace SHADE
mainCameraEID = camera.GetEID();
}
SHMatrix const& SHCameraDirector::GetOrthoMatrix() noexcept
{
SHCameraComponent* camComponent = GetMainCameraComponent();
if (camComponent)
return camComponent->GetOrthoMatrix();
else
return SHMatrix::Identity;
}
SHMatrix const& SHCameraDirector::GetPerspectiveMatrix() noexcept
{
SHCameraComponent* camComponent = GetMainCameraComponent();
if (camComponent)
return camComponent->GetPerspectiveMatrix();
else
return SHMatrix::Identity;
}
}

View File

@ -23,19 +23,19 @@ namespace SHADE
EntityID transitionCameraEID;
SHMatrix GetViewMatrix() const noexcept;
SHMatrix GetProjMatrix() const noexcept;
SHMatrix GetVPMatrix() const noexcept;
SHMatrix const& GetViewMatrix() const noexcept;
SHMatrix const& GetProjMatrix() const noexcept;
SHMatrix const& GetVPMatrix() const noexcept;
void UpdateMatrix() noexcept;
void SetMainCamera(SHCameraComponent& cam) noexcept;
SHMatrix const& GetOrthoMatrix() noexcept;
SHMatrix const& GetPerspectiveMatrix() noexcept;
private:
SHMatrix viewMatrix;
SHMatrix projMatrix;
protected:
SHMatrix viewMatrix;
SHMatrix projMatrix;
SHCameraComponent* GetMainCameraComponent() noexcept;
};

View File

@ -107,7 +107,11 @@ namespace SHADE
editorCamera.SetPitch(0.0f);
editorCamera.SetYaw(0.0f);
editorCamera.SetRoll(0.0f);
editorCamera.SetWidth(1080.0f);
editorCamera.SetHeight(720.0f);
editorCamera.SetFar(10000000.0f);
editorCamera.movementSpeed = 2.0f;
editorCamera.perspProj = true;
SHComponentManager::CreateComponentSparseSet<SHCameraComponent>();
SHComponentManager::CreateComponentSparseSet<SHCameraArmComponent>();
@ -210,39 +214,43 @@ namespace SHADE
}
if (camera.dirtyProj == true)
{
if (camera.perspProj == true)
{
//Perspective projection matrix.
const float ASPECT_RATIO = (camera.GetAspectRatio());
const float TAN_HALF_FOV = tan(SHMath::DegreesToRadians(camera.fov) * 0.5f);
camera.projMatrix = SHMatrix::Identity;
camera.projMatrix(0, 0) = 1.0f / (ASPECT_RATIO * TAN_HALF_FOV);
camera.projMatrix(1, 1) = 1.0f / TAN_HALF_FOV;
camera.projMatrix(2, 2) = camera.zFar / (camera.zFar - camera.zNear);
camera.projMatrix(3, 3) = 0.0f;
camera.perspProjMatrix = SHMatrix::Identity;
camera.perspProjMatrix(0, 0) = 1.0f / (ASPECT_RATIO * TAN_HALF_FOV);
camera.perspProjMatrix(1, 1) = 1.0f / TAN_HALF_FOV;
camera.perspProjMatrix(2, 2) = camera.zFar / (camera.zFar - camera.zNear);
camera.perspProjMatrix(3, 3) = 0.0f;
camera.projMatrix(3, 2) = 1.0f;
camera.projMatrix(2, 3) = -(camera.zFar * camera.zNear) / (camera.zFar - camera.zNear);
camera.perspProjMatrix(3, 2) = 1.0f;
camera.perspProjMatrix(2, 3) = -(camera.zFar * camera.zNear) / (camera.zFar - camera.zNear);
//Orthographic projection matrix
const float right = camera.GetWidth() * 0.5f;
const float left = -right;
const float top = camera.GetHeight() * 0.5f;
const float btm = -top;
const float n = camera.GetNear();
const float f = camera.GetFar();
camera.orthoProjMatrix = SHMatrix::Identity;
camera.orthoProjMatrix(0, 0) = 2.0f / (right - left);
camera.orthoProjMatrix(1, 1) = 2.0f / (btm - top);
camera.orthoProjMatrix(2, 2) = 1.0f / (f-n);
camera.orthoProjMatrix(0, 3) = -(right + left) / (right - left);
camera.orthoProjMatrix(1, 3) = -(btm + top) / (btm - top);
camera.orthoProjMatrix(2, 3) = -n / (f-n);
camera.orthoProjMatrix(3, 3) = 1.0f;
camera.orthoProjMatrix = SHMatrix::OrthographicRH(camera.GetWidth(), camera.GetHeight(), camera.GetNear(), camera.GetFar());
//camera.projMatrix.Transpose();
camera.dirtyProj = false;
}
else
{
//const float R = camera.width * 0.5f;
//const float L = -R;
//const float T = camera.height * 0.5f;
//const float B = -T;
//camera.projMatrix = SHMatrix::Identity;
//camera.projMatrix(0, 0) = 2.0f / (R - L);
//camera.projMatrix(1, 1) = 2.0f / (B - T);
//camera.projMatrix(2, 2) = 1.0f / (camera.zFar - camera.zNear);
//camera.projMatrix(3, 0) = -(R + L) / (R - L);
//camera.projMatrix(3, 1) = -(B + T) / (B - T);
//camera.projMatrix(3, 2) = -camera.zNear / (camera.zFar - camera.zNear);
camera.dirtyProj = false;
}
}
}
@ -252,8 +260,6 @@ namespace SHADE
SHVec3 up = { 0.0f,1.0f,0.0f };
target = SHVec3::RotateX(target, SHMath::DegreesToRadians(camera.pitch));
target = SHVec3::RotateY(target, SHMath::DegreesToRadians(camera.yaw));
target += camera.position;
@ -287,6 +293,9 @@ namespace SHADE
if (SHSceneManager::CheckNodeAndComponentsActive<SHCameraComponent>(cam.GetEID()))
system->UpdateCameraComponent(cam);
}
for (auto& handle : system->directorHandleList)
{
handle->UpdateMatrix();

View File

@ -37,8 +37,6 @@ namespace SHADE
void Exit (void);
friend class EditorCameraUpdate;
class SH_API CameraSystemUpdate final: public SHSystemRoutine
{
public:

View File

@ -218,7 +218,7 @@ namespace SHADE
EntityID result = MAX_EID;
for (auto& entity : entityVec)
{
if (entity->name == name)
if (entity && entity->name == name)
result = entity->GetEID();
}
return result;

View File

@ -5,6 +5,7 @@
namespace SHADE
{
bool SHDragDrop::hasDragDrop = false;
SHDragDrop::DragDropTag SHDragDrop::currentDragDropTag{};
bool SHDragDrop::BeginSource(ImGuiDragDropFlags const flags)
{ return ImGui::BeginDragDropSource(flags); }
@ -16,6 +17,10 @@ namespace SHADE
{ return ImGui::BeginDragDropTarget(); }
void SHDragDrop::EndTarget()
{ ImGui::EndDragDropTarget(); hasDragDrop = false;}
{
ImGui::EndDragDropTarget();
hasDragDrop = false;
currentDragDropTag = {};
}
}

View File

@ -19,9 +19,13 @@ namespace SHADE
static void EndSource();
template<typename T>
static bool SetPayload(std::string_view const type, T* object, ImGuiCond const cond = 0)
static bool SetPayload(DragDropTag const& type, T* object, ImGuiCond const cond = 0)
{
hasDragDrop = ImGui::SetDragDropPayload(type.data(), static_cast<void*>(object), sizeof(T), cond);
ImGui::SetDragDropPayload(type.data(), static_cast<void*>(object), sizeof(T), cond);
hasDragDrop = true;
currentDragDropTag = type;
return hasDragDrop;
}
@ -32,13 +36,16 @@ namespace SHADE
static void EndTarget();
template<typename T>
static T* AcceptPayload(std::string_view const type, ImGuiDragDropFlags const flags = 0)
static T* AcceptPayload(DragDropTag const& type, ImGuiDragDropFlags const flags = 0)
{
if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload(type.data(), flags))
{
return static_cast<T*>(payload->Data);
}
return nullptr;
}
static bool hasDragDrop;
static DragDropTag currentDragDropTag;
};
}

View File

@ -109,7 +109,7 @@ namespace SHADE
ImVec2 vertLineEnd = vertLineStart;
for (auto const& subFolder : subFolders)
{
const float horizontalLineSize = 8.0f;
const float horizontalLineSize = (subFolder->subFolders.empty() && subFolder->files.empty()) ? 25.0f : 8.0f;
const ImRect childRect = RecursivelyDrawTree(subFolder);
const float midPoint = (childRect.Min.y + childRect.Max.y) * 0.5f;
drawList->AddLine(ImVec2(vertLineStart.x, midPoint), ImVec2(vertLineStart.x + horizontalLineSize, midPoint), treeLineColor, 1);
@ -117,7 +117,7 @@ namespace SHADE
}
for (auto& file : files)
{
const float horizontalLineSize = 25.0f;
const float horizontalLineSize = (file.assetMeta && !file.assetMeta->subAssets.empty()) ? 8.0f : 25.0f;
const ImRect childRect = DrawFile(file);
const float midPoint = (childRect.Min.y + childRect.Max.y) * 0.5f;
drawList->AddLine(ImVec2(vertLineStart.x, midPoint), ImVec2(vertLineStart.x + horizontalLineSize, midPoint), treeLineColor, 1);
@ -182,7 +182,10 @@ namespace SHADE
return nodeRect;
}
if(file.assetMeta)
DrawAsset(file.assetMeta, file.ext);
{
const ImRect childRect = DrawAsset(file.assetMeta, file.ext);
return childRect;
}
}
ImRect SHAssetBrowser::DrawAsset(SHAsset const* const asset, FileExt const& ext /*= ""*/) noexcept

View File

@ -117,9 +117,12 @@ namespace SHADE
{
if(ImGui::IsDragDropActive())
{
ParentSelectedEntities(MAX_EID, draggingEntities);
draggingEntities.clear();
ImGui::ClearDragDrop();
if (SHDragDrop::currentDragDropTag == SHDragDrop::DRAG_EID)
{
ParentSelectedEntities(MAX_EID, draggingEntities);
draggingEntities.clear();
ImGui::ClearDragDrop();
}
}
}
ImGui::End();
@ -233,8 +236,9 @@ namespace SHADE
{
ParentSelectedEntities(eid, draggingEntities);
draggingEntities.clear();
SHDragDrop::EndTarget();
//ImGui::ClearDragDrop();
}
SHDragDrop::EndTarget();
}
//Context menu
@ -342,17 +346,12 @@ namespace SHADE
SHEntityManager::CreateEntity(MAX_EID, "DefaultChild", parentEID);
}
void SHHierarchyPanel::ParentSelectedEntities(EntityID parentEID, std::vector<EntityID> const& entities) const noexcept
void SHHierarchyPanel::ParentSelectedEntities(EntityID parentEID, std::vector<EntityID> const& entities) noexcept
{
auto const& sceneGraph = SHSceneManager::GetCurrentSceneGraph();
std::vector<EntityID> entitiesToParent{};
std::ranges::copy_if(entities, std::back_inserter(entitiesToParent), [&sceneGraph](EntityID const& eid)
{
if (sceneGraph.GetParent(eid)->GetEntityID() == MAX_EID)
return true;
return false;
});
std::vector<EntityID> entitiesToParent = CleanUpEIDList(entities);
//auto const editor = SHSystemManager::GetSystem<SHEditor>();
SHEntityParentCommand::EntityParentData entityParentData;
std::vector<EntityID> parentedEIDS;
@ -419,14 +418,7 @@ namespace SHADE
void SHHierarchyPanel::CopySelectedEntities()
{
const auto editor = SHSystemManager::GetSystem<SHEditor>();
auto const& sceneGraph = SHSceneManager::GetCurrentSceneGraph();
std::vector<EntityID> entitiesToCopy{};
std::ranges::copy_if(editor->selectedEntities, std::back_inserter(entitiesToCopy), [&sceneGraph](EntityID const& eid)
{
if(sceneGraph.GetParent(eid)->GetEntityID() == MAX_EID)
return true;
return false;
});
std::vector<EntityID> entitiesToCopy = CleanUpEIDList(editor->selectedEntities);
SHClipboardUtilities::WriteToClipboard(SHSerialization::SerializeEntitiesToString(entitiesToCopy));
}
@ -439,19 +431,25 @@ namespace SHADE
void SHHierarchyPanel::DeleteSelectedEntities()
{
const auto editor = SHSystemManager::GetSystem<SHEditor>();
std::vector<EntityID> entitiesToDelete = CleanUpEIDList(editor->selectedEntities);
SHCommandManager::PerformCommand(std::make_shared<SHDeleteEntitiesCommand>(entitiesToDelete));
}
std::vector<EntityID> SHHierarchyPanel::CleanUpEIDList(std::vector<EntityID> const& entities)
{
std::vector<EntityID> result;
auto const& sceneGraph = SHSceneManager::GetCurrentSceneGraph();
std::vector<EntityID> entitiesToDelete{};
std::ranges::copy_if(editor->selectedEntities, std::back_inserter(entitiesToDelete), [&sceneGraph, &selectedEntities = editor->selectedEntities](EntityID const& eid)
std::ranges::copy_if(entities, std::back_inserter(result), [&sceneGraph, &entities](EntityID const& eid)
{
EntityID parentEID = sceneGraph.GetParent(eid)->GetEntityID();
if (parentEID == MAX_EID)
return true;
else if(std::ranges::find(selectedEntities, parentEID) == selectedEntities.end())
if (std::ranges::find(entities, parentEID) == entities.end())
return true;
return false;
});
SHCommandManager::PerformCommand(std::make_shared<SHDeleteEntitiesCommand>(entitiesToDelete));
return result;
}
}//namespace SHADE

View File

@ -27,12 +27,13 @@ namespace SHADE
void DrawMenuBar() const noexcept;
ImRect RecursivelyDrawEntityNode(SHSceneNode* const);
void CreateChildEntity(EntityID parentEID) const noexcept;
void ParentSelectedEntities(EntityID parentEID, std::vector<EntityID> const& entities) const noexcept;
void ParentSelectedEntities(EntityID parentEID, std::vector<EntityID> const& entities) noexcept;
void SelectRangeOfEntities(EntityID beginEID, EntityID EndEID);
void SelectAllEntities();
void CopySelectedEntities();
void PasteEntities(EntityID parentEID = MAX_EID);
void DeleteSelectedEntities();
std::vector<EntityID> CleanUpEIDList(std::vector<EntityID> const& entities);
bool skipFrame = false;
std::string filter;
bool isAnyNodeSelected = false;

View File

@ -455,6 +455,11 @@ namespace SHADE
},
[component](AssetID const& id)
{
if(SHAssetManager::GetType(id) != AssetType::MESH)
{
SHLOG_WARNING("Attempted to assign non mesh asset to Renderable Mesh property!")
return;
}
component->SetMesh(SHResourceManager::LoadOrGet<SHMesh>(id));
SHResourceManager::FinaliseChanges();
}, SHDragDrop::DRAG_RESOURCE);
@ -470,6 +475,11 @@ namespace SHADE
},
[component](AssetID const& id)
{
if (SHAssetManager::GetType(id) != AssetType::MATERIAL)
{
SHLOG_WARNING("Attempted to assign non material asset to Renderable Mesh property!")
return;
}
auto gfxSystem = SHSystemManager::GetSystem<SHGraphicsSystem>();
component->SetMaterial(gfxSystem->AddOrGetBaseMaterialInstance(SHResourceManager::LoadOrGet<SHMaterial>(id)));
}, SHDragDrop::DRAG_RESOURCE);

View File

@ -18,6 +18,8 @@
#include "Physics/Interface/SHColliderComponent.h"
#include "Camera/SHCameraComponent.h"
#include "Camera/SHCameraArmComponent.h"
#include "UI/SHUIComponent.h"
#include "UI/SHCanvasComponent.h"
#include "SHEditorComponentView.h"
namespace SHADE
@ -132,6 +134,14 @@ namespace SHADE
{
DrawComponent(cameraArmComponent);
}
if (auto canvasComponent= SHComponentManager::GetComponent_s<SHCanvasComponent>(eid))
{
DrawComponent(canvasComponent);
}
if (auto uiComponent = SHComponentManager::GetComponent_s<SHUIComponent>(eid))
{
DrawComponent(uiComponent);
}
ImGui::Separator();
// Render Scripts
SHScriptEngine* scriptEngine = static_cast<SHScriptEngine*>(SHSystemManager::GetSystem<SHScriptEngine>());
@ -143,6 +153,7 @@ namespace SHADE
DrawAddComponentButton<SHCameraComponent>(eid);
DrawAddComponentButton<SHCameraArmComponent>(eid);
DrawAddComponentButton<SHLightComponent>(eid);
DrawAddComponentButton<SHCanvasComponent>(eid);
// Components that require Transforms

View File

@ -14,6 +14,7 @@
#include "Camera/SHCameraSystem.h"
#include "FRC/SHFramerateController.h"
#include "../../SHEditorWidgets.hpp"
constexpr std::string_view windowName = "\xef\x80\x95 Viewport";
@ -193,6 +194,13 @@ namespace SHADE
if (isScale)
ImGui::PopStyleColor();
ImGui::EndDisabled();
auto camSystem = SHSystemManager::GetSystem<SHCameraSystem>();
auto editorCamera = camSystem->GetEditorCamera();
//ImGui::SetNextItemWidth(10.0f);
SHEditorWidgets::SliderFloat("CamSpeed", 0.0f, 5.0f, [editorCamera] {return editorCamera->movementSpeed; }, [editorCamera](float const& value) {editorCamera->movementSpeed = value; });
SHEditorWidgets::DragVec3("TurnSpeed", { "X", "Y", "Z" }, [editorCamera] {return editorCamera->turnSpeed; }, [editorCamera](SHVec3 const& value) {editorCamera->turnSpeed = value; });
ImGui::EndMenuBar();
}
}

View File

@ -422,7 +422,7 @@ namespace SHADE
ImGui::BeginGroup();
ImGui::PushID(label.data());
TextLabel(label);
bool changed = ImGui::InputText("##", &text, ImGuiInputTextFlags_ReadOnly, nullptr, nullptr);
bool changed = ImGui::InputText("##inputText", &text, ImGuiInputTextFlags_ReadOnly, nullptr, nullptr);
if(SHDragDrop::BeginTarget())
{
if(T* payload = SHDragDrop::AcceptPayload<T>(dragDropTag))
@ -454,7 +454,7 @@ namespace SHADE
ImGui::BeginGroup();
ImGui::PushID(label.data());
TextLabel(label);
const bool hasChange = ImGui::DragScalar("##", data_type, &value, speed, &p_min, &p_max, displayFormat, flags);
const bool hasChange = ImGui::DragScalar("##dragScalar", data_type, &value, speed, &p_min, &p_max, displayFormat, flags);
static bool startRecording = false;
if (hasChange)
{
@ -487,7 +487,7 @@ namespace SHADE
ImGui::BeginGroup();
ImGui::PushID(label.data());
TextLabel(label);
const bool hasChange = ImGui::DragFloat("##", &value, speed, p_min, p_max, displayFormat, flags);
const bool hasChange = ImGui::DragFloat("##dragFloat", &value, speed, p_min, p_max, displayFormat, flags);
static bool startRecording = false;
if (hasChange)
{
@ -520,7 +520,7 @@ namespace SHADE
ImGui::BeginGroup();
ImGui::PushID(label.data());
TextLabel(label);
const bool hasChange = ImGui::DragInt("##", &value, speed, p_min, p_max, displayFormat, flags);
const bool hasChange = ImGui::DragInt("##dragInt", &value, speed, p_min, p_max, displayFormat, flags);
static bool startRecording = false;
if (hasChange)
{
@ -553,7 +553,7 @@ namespace SHADE
ImGui::BeginGroup();
ImGui::PushID(label.data());
TextLabel(label);
bool const hasChange = ImGui::SliderScalar("##", data_type, &value, &min, &max, displayFormat, flags);
bool const hasChange = ImGui::SliderScalar("##sliderScalar", data_type, &value, &min, &max, displayFormat, flags);
static bool startRecording = false;
if (hasChange)
{
@ -587,7 +587,8 @@ namespace SHADE
ImGui::BeginGroup();
ImGui::PushID(label.data());
TextLabel(label);
bool const hasChange = ImGui::SliderFloat("##", &value, min, max, displayFormat, flags);
ImGui::SetNextItemWidth(ImGui::CalcTextSize(displayFormat).x + ImGui::GetStyle().ItemInnerSpacing.x * 2.0f);
bool const hasChange = ImGui::SliderFloat("##sliderFloat", &value, min, max, displayFormat, flags);
static bool startRecording = false;
if (hasChange)
{
@ -621,7 +622,7 @@ namespace SHADE
ImGui::BeginGroup();
ImGui::PushID(label.data());
TextLabel(label);
bool const hasChange = ImGui::SliderInt("##", &value, min, max, displayFormat, flags);
bool const hasChange = ImGui::SliderInt("##sliderInt", &value, min, max, displayFormat, flags);
static bool startRecording = false;
if (hasChange)
{

View File

@ -58,10 +58,9 @@ namespace SHADE
loadFunctions();
// Generate script assembly if it hasn't been before
if (!fileExists(std::string(MANAGED_SCRIPT_LIB_NAME) + ".dll"))
{
BuildScriptAssembly();
}
#ifndef _PUBLISH
BuildScriptAssembly();
#endif
// Initialise the CSharp Engine
csEngineInit();
@ -307,7 +306,7 @@ namespace SHADE
SHEventHandle SHScriptEngine::onEntityDestroyed(SHEventPtr eventPtr)
{
auto eventData = reinterpret_cast<const SHEventSpec<SHEntityDestroyedEvent>*>(eventPtr.get());
csScriptsRemoveAll(eventData->data->eid);
csScriptsRemoveAllImmediately(eventData->data->eid, true);
return eventData->handle;
}

View File

@ -12,8 +12,8 @@
#include "Assets/Asset Types/SHSceneAsset.h"
#include "Camera/SHCameraComponent.h"
#include "Graphics/MiddleEnd/Interface/SHRenderable.h"
#include "Math/Transform/SHTransformComponent.h"
#include "Graphics/MiddleEnd/Interface/SHRenderable.h"
#include "Physics/Interface/SHRigidBodyComponent.h"
#include "ECS_Base/Managers/SHSystemManager.h"
#include "Graphics/MiddleEnd/Lights/SHLightComponent.h"

View File

@ -0,0 +1,60 @@
#include "SHpch.h"
#include "SHCanvasComponent.h"
namespace SHADE
{
SHCanvasComponent::SHCanvasComponent()
:width(1),height(1), dirtyMatrix(false), canvasMatrix()
{
}
void SHCanvasComponent::SetCanvasSize(CanvasSizeType width, CanvasSizeType height) noexcept
{
this->width = width;
this->height = height;
}
void SHCanvasComponent::SetCanvasWidth(CanvasSizeType val) noexcept
{
width = val;
}
void SHCanvasComponent::SetCanvasHeight(CanvasSizeType val) noexcept
{
height = val;
}
SHCanvasComponent::CanvasSizeType SHCanvasComponent::GetCanvasWidth() const noexcept
{
return width;
}
SHCanvasComponent::CanvasSizeType SHCanvasComponent::GetCanvasHeight() const noexcept
{
return height;
}
SHMatrix const& SHCanvasComponent::GetMatrix() const noexcept
{
return canvasMatrix;
}
}
RTTR_REGISTRATION
{
using namespace SHADE;
using namespace rttr;
registration::class_<SHCanvasComponent>("Canvas Component")
.property("Canvas Width", &SHCanvasComponent::GetCanvasWidth, &SHCanvasComponent::SetCanvasWidth)
.property("Canvas Height", &SHCanvasComponent::GetCanvasHeight, &SHCanvasComponent::SetCanvasHeight)
;
}

View File

@ -1,7 +1,10 @@
#pragma once
#include <rttr/registration>
#include "SH_API.h"
#include "ECS_Base/Components/SHComponent.h"
#include "Math/SHMatrix.h"
namespace SHADE
@ -13,6 +16,9 @@ namespace SHADE
public:
friend class SHUISystem;
SHCanvasComponent();
~SHCanvasComponent() = default;
@ -20,15 +26,18 @@ namespace SHADE
void SetCanvasWidth(CanvasSizeType width) noexcept;
void SetCanvasHeight(CanvasSizeType height) noexcept;
CanvasSizeType const GetCanvasWidth() const noexcept;
CanvasSizeType const GetCanvasHeight() const noexcept;
CanvasSizeType GetCanvasWidth() const noexcept;
CanvasSizeType GetCanvasHeight() const noexcept;
SHMatrix const& GetMatrix() const noexcept;
private:
CanvasSizeType width;
CanvasSizeType height;
bool dirtyMatrix;
SHMatrix canvasMatrix;
RTTR_ENABLE()
};

View File

@ -0,0 +1,43 @@
#include "SHpch.h"
#include "SHUIComponent.h"
namespace SHADE
{
SHUIComponent::SHUIComponent()
{
}
SHMatrix const& SHUIComponent::GetMatrix()const noexcept
{
return localToCanvasMatrix;
}
EntityID SHUIComponent::GetCanvasID() const noexcept
{
return canvasID;
}
void SHUIComponent::SetCanvasID(EntityID id) noexcept
{
(void)id;
}
}
RTTR_REGISTRATION
{
using namespace SHADE;
using namespace rttr;
registration::class_<SHUIComponent>("UI Component")
.property("Canvas ID", &SHUIComponent::GetCanvasID, &SHUIComponent::SetCanvasID)
;
}

View File

@ -0,0 +1,33 @@
#pragma once
#include <rttr/registration>
#include "SH_API.h"
#include "ECS_Base/Components/SHComponent.h"
#include "Math/SHMatrix.h"
namespace SHADE
{
class SH_API SHUIComponent final: public SHComponent
{
public:
friend class SHUISystem;
SHUIComponent();
~SHUIComponent() = default;
SHMatrix const& GetMatrix() const noexcept;
EntityID GetCanvasID() const noexcept;
void SetCanvasID(EntityID id) noexcept;
private:
SHMatrix localToCanvasMatrix;
EntityID canvasID;
RTTR_ENABLE()
};
}

View File

@ -0,0 +1,113 @@
#include "SHpch.h"
#include "SHUISystem.h"
#include "ECS_Base/Managers/SHComponentManager.h"
#include "ECS_Base/Managers/SHSystemManager.h"
#include "Math/Transform/SHTransformComponent.h"
namespace SHADE
{
void SHUISystem::Init()
{
SystemFamily::GetID<SHUISystem>();
SHComponentManager::CreateComponentSparseSet<SHCanvasComponent>();
SHComponentManager::CreateComponentSparseSet<SHUIComponent>();
}
void SHUISystem::Exit()
{
}
void SHUISystem::AddUIComponentRoutine::Execute(double dt) noexcept
{
auto& dense = SHComponentManager::GetDense<SHCanvasComponent>();
auto& sceneGraph = SHSceneManager::GetCurrentSceneGraph();
//Go through all the canvases and make sure all the children has a UIComponent.
for (auto& canvas : dense)
{
auto& children = sceneGraph.GetChildren(canvas.GetEID());
for (auto& child : children)
{
RecurssiveUIComponentCheck(child, canvas);
}
}
//Go through all the UI Component and make sure the parent has a UI or Canvas Component
std::vector<EntityID> entitiesToRemove;
auto& UIDense = SHComponentManager::GetDense<SHUIComponent>();
for (auto& ui : UIDense)
{
SHSceneNode* parentNode = sceneGraph.GetParent(ui.GetEID());
if (parentNode == nullptr || !(SHComponentManager::HasComponent<SHUIComponent>(parentNode->GetEntityID()) || SHComponentManager::HasComponent<SHCanvasComponent>(parentNode->GetEntityID())))
entitiesToRemove.push_back(ui.GetEID());
}
for (auto& id : entitiesToRemove)
{
SHComponentManager::RemoveComponent<SHUIComponent>(id);
}
}
void SHUISystem::AddUIComponentRoutine::RecurssiveUIComponentCheck(SHSceneNode* node, SHCanvasComponent& canvas) noexcept
{
if (node == nullptr)
return;
EntityID eid = node->GetEntityID();
if(SHComponentManager::HasComponent<SHUIComponent>(eid) == false)
SHComponentManager::AddComponent<SHUIComponent>(eid);
else
{
SHComponentManager::GetComponent<SHUIComponent>(eid)->canvasID = canvas.GetEID();
}
auto& children = SHSceneManager::GetCurrentSceneGraph().GetChildren(eid);
for (auto& child : children)
{
RecurssiveUIComponentCheck(child, canvas);
}
}
void SHUISystem::UpdateUIMatrixRoutine::Execute(double dt) noexcept
{
SHUISystem* system = (SHUISystem* )GetSystem();
auto& dense = SHComponentManager::GetDense<SHUIComponent>();
for (auto& comp : dense)
{
system->UpdateUIComponent(comp);
}
}
void SHUISystem::UpdateUIComponent(SHUIComponent& comp) noexcept
{
auto canvasComp = SHComponentManager::GetComponent_s<SHCanvasComponent>(comp.canvasID);
if (SHComponentManager::HasComponent<SHTransformComponent>(comp.GetEID()))
{
auto transform = SHComponentManager::GetComponent<SHTransformComponent>(comp.GetEID());
if (canvasComp != nullptr)
comp.localToCanvasMatrix = canvasComp->GetMatrix() * transform->GetTRS();
else
comp.localToCanvasMatrix = transform->GetTRS();
}
else //If for some reason UI Components entities does not have a transform.
{
if (canvasComp != nullptr)
comp.localToCanvasMatrix = canvasComp->GetMatrix();
else
comp.localToCanvasMatrix = SHMatrix::Identity;
}
}
}//end namespace

View File

@ -0,0 +1,55 @@
#pragma once
#include "SH_API.h"
#include "ECS_Base/System/SHSystem.h"
#include "ECS_Base/System/SHSystemRoutine.h"
#include "SHUIComponent.h"
#include "SHCanvasComponent.h"
#include "Scene/SHSceneGraph.h"
#include "Scene/SHSceneManager.h"
namespace SHADE
{
class SH_API SHUISystem final: public SHSystem
{
public:
SHUISystem() = default;
~SHUISystem() = default;
class SH_API AddUIComponentRoutine final: public SHSystemRoutine
{
public:
AddUIComponentRoutine() :SHSystemRoutine("Add UI Component Routine", true) {};
virtual void Execute(double dt) noexcept override final;
private:
void RecurssiveUIComponentCheck(SHSceneNode* node, SHCanvasComponent& canvas) noexcept;
};
friend class AddUIComponentRoutine;
class SH_API UpdateUIMatrixRoutine final: public SHSystemRoutine
{
public:
UpdateUIMatrixRoutine() : SHSystemRoutine("Update UI Matrix Routine", true) {};
virtual void Execute(double dt) noexcept override final;
};
friend class UpdateUIMatrixRoutine;
void Init();
void Exit();
private:
void UpdateUIComponent(SHUIComponent& comp) noexcept;
};
}

View File

@ -402,8 +402,8 @@ namespace SHADE
System::Collections::Generic::List<Script^>^ scriptList = scripts[entity];
for each (Script ^ script in scriptList)
{
// Call OnDestroy only if indicated and also in play mode
if (callOnDestroy)
// Call OnDestroy only if indicated and also if the game has run
if (callOnDestroy && Application::IsPlaying || Application::IsPaused)
{
script->OnDestroy();
}
@ -469,7 +469,7 @@ namespace SHADE
script->OnDestroy();
}
auto entity = script->Owner.GetEntity();
auto scriptList = scripts[script->Owner.GetEntity()];
auto scriptList = scripts[script->Owner.GetEntity()]; // Unable to find here
scriptList->Remove(script);
if (scriptList->Count <= 0)
{

View File

@ -45,12 +45,22 @@ namespace SHADE
System::Collections::Generic::IEnumerable<FieldInfo^>^ fields = ReflectionUtilities::GetInstanceFields(object);
for each (FieldInfo^ field in fields)
{
// Ignore private and non-SerialiseField
if (!ReflectionUtilities::FieldIsSerialisable(field))
continue;
try
{
// Ignore private and non-SerialiseField
if (!ReflectionUtilities::FieldIsSerialisable(field))
continue;
// Serialise
writeFieldIntoYaml(field, object, scriptNode);
// Serialise
writeFieldIntoYaml(field, object, scriptNode);
}
catch (System::Exception^ e)
{
Debug::LogError
(
System::String::Format("[SerialisationUtilities] Failed to serialise field ({0}): {1}", field->Name, e->ToString())
);
}
}
scriptListNode.push_back(scriptNode);
@ -74,15 +84,25 @@ namespace SHADE
System::Collections::Generic::IEnumerable<FieldInfo^>^ fields = ReflectionUtilities::GetInstanceFields(object);
for each (FieldInfo^ field in fields)
{
// Ignore private and non-SerialiseField
if (!ReflectionUtilities::FieldIsSerialisable(field))
continue;
// Deserialise
const std::string FIELD_NAME = Convert::ToNative(field->Name);
if (yamlNode[FIELD_NAME])
try
{
writeYamlIntoField(field, object, yamlNode[FIELD_NAME]);
// Ignore private and non-SerialiseField
if (!ReflectionUtilities::FieldIsSerialisable(field))
continue;
// Deserialise
const std::string FIELD_NAME = Convert::ToNative(field->Name);
if (yamlNode[FIELD_NAME])
{
writeYamlIntoField(field, object, yamlNode[FIELD_NAME]);
}
}
catch (System::Exception^ e)
{
Debug::LogError
(
System::String::Format("[SerialisationUtilities] Failed to deserialise field ({0}): {1}", field->Name, e->ToString())
);
}
}
}

View File

@ -142,6 +142,31 @@ namespace SHADE
bool SerialisationUtilities::fieldAssignYaml(System::Reflection::FieldInfo^ fieldInfo, Object^ object, YAML::Node& node)
{
System::Object^ valueObj = fieldInfo->GetValue(object);
if (valueObj == nullptr)
{
if constexpr (std::is_same_v<FieldType, System::Enum>)
{
if (fieldInfo->FieldType->IsSubclassOf(System::Enum::typeid))
{
valueObj = 0;
}
}
else
{
if (fieldInfo->FieldType == FieldType::typeid)
{
if constexpr (std::is_same_v<FieldType, System::String>)
{
valueObj = "";
}
else
{
valueObj = FieldType();
}
}
}
}
if (varAssignYamlInternal<FieldType>(valueObj, node))
{
fieldInfo->SetValue(object, valueObj);