diff --git a/Editor/src/ComponentsPanel/NetScriptPanel.cpp b/Editor/src/ComponentsPanel/NetScriptPanel.cpp index ae4a4cfe..3d51746c 100644 --- a/Editor/src/ComponentsPanel/NetScriptPanel.cpp +++ b/Editor/src/ComponentsPanel/NetScriptPanel.cpp @@ -179,6 +179,35 @@ void NetScriptPanel::Draw(Nuake::Entity entity) field.Value = currentValue; } + if (field.Type == Nuake::NetScriptExposedVarType::Vector2) + { + if (!field.Value.has_value()) + { + field.Value = field.DefaultValue; + } + + + Nuake::Vector2 currentValue = std::any_cast(field.Value); + const std::string sliderName = "##" + field.Name + "slider"; + ImGuiHelper::DrawVec2(sliderName, ¤tValue); + + field.Value = currentValue; + } + + if (field.Type == Nuake::NetScriptExposedVarType::Vector3) + { + if (!field.Value.has_value()) + { + field.Value = field.DefaultValue; + } + + Nuake::Vector3 currentValue = std::any_cast(field.Value); + const std::string sliderName = "##" + field.Name + "slider"; + ImGuiHelper::DrawVec3(sliderName, ¤tValue); + + field.Value = currentValue; + } + ImGui::TableNextColumn(); } } diff --git a/Nuake/src/Scene/Components/NetScriptComponent.h b/Nuake/src/Scene/Components/NetScriptComponent.h index 14c55331..9d4e86d8 100644 --- a/Nuake/src/Scene/Components/NetScriptComponent.h +++ b/Nuake/src/Scene/Components/NetScriptComponent.h @@ -1,5 +1,6 @@ #pragma once #include "src/Core/Core.h" +#include "src/Core/Maths.h" #include "src/Core/FileSystem.h" #include "src/Core/Logger.h" #include "src/Resource/Serializable.h" @@ -70,6 +71,30 @@ namespace Nuake { varJ["Value"] = std::any_cast(e.Value); varJ["DefaultValue"] = std::any_cast(e.DefaultValue); break; + case NetScriptExposedVarType::Vector2: + { + Vector2 value = std::any_cast(e.Value); + varJ["Value"][0] = value.x; + varJ["Value"][1] = value.y; + + value = std::any_cast(e.DefaultValue); + varJ["DefaultValue"][0] = value.x; + varJ["DefaultValue"][1] = value.y; + break; + } + case NetScriptExposedVarType::Vector3: + { + Vector3 value = std::any_cast(e.Value); + varJ["Value"][0] = value.x; + varJ["Value"][1] = value.y; + varJ["Value"][2] = value.z; + + value = std::any_cast(e.DefaultValue); + varJ["DefaultValue"][0] = value.x; + varJ["DefaultValue"][1] = value.y; + varJ["DefaultValue"][2] = value.z; + break; + } default: varJ["Value"] = 0; break; @@ -121,6 +146,31 @@ namespace Nuake { exposedVar.Value = (std::string)exposedVarJson["Value"]; exposedVar.DefaultValue = (std::string)exposedVarJson["DefaultValue"]; break; + case NetScriptExposedVarType::Vector2: + { + float x = exposedVarJson["Value"][0]; + float y = exposedVarJson["Value"][1]; + + exposedVar.Value = Vector2(x, y); + + x = exposedVarJson["DefaultValue"][0]; + y = exposedVarJson["DefaultValue"][1]; + + exposedVar.DefaultValue = Vector2(x, y); + break; + } + case NetScriptExposedVarType::Vector3: + { + float x = exposedVarJson["Value"][0]; + float y = exposedVarJson["Value"][1]; + float z = exposedVarJson["Value"][2]; + exposedVar.Value = Vector3(x, y, z); + + x = exposedVarJson["DefaultValue"][0]; + y = exposedVarJson["DefaultValue"][1]; + z = exposedVarJson["DefaultValue"][2]; + exposedVar.DefaultValue = Vector3(x, y, z); + } default: exposedVar.DefaultValue = 0; break; diff --git a/Nuake/src/Scene/Entities/ImGuiHelper.h b/Nuake/src/Scene/Entities/ImGuiHelper.h index 08338584..dde80b31 100644 --- a/Nuake/src/Scene/Entities/ImGuiHelper.h +++ b/Nuake/src/Scene/Entities/ImGuiHelper.h @@ -8,7 +8,8 @@ class ImGuiHelper { public: - static void DrawVec3(const std::string label, glm::vec3* values, float resetValue = 0.0f, float columnWidth = 100.0, float rate = 0.1f, float min = 0.0f) { + static void DrawVec3(const std::string label, glm::vec3* values, float resetValue = 0.0f, float columnWidth = 100.0, float rate = 0.1f, float min = 0.0f) + { ImGuiIO& io = ImGui::GetIO(); auto boldFont = io.Fonts->Fonts[0]; @@ -68,4 +69,51 @@ public: ImGui::PopID(); //ImGui::Text("Hello"); } + + static void DrawVec2(const std::string label, glm::vec2* values, float resetValue = 0.0f, float columnWidth = 100.0, float rate = 0.1f, float min = 0.0f) + { + ImGuiIO& io = ImGui::GetIO(); + auto boldFont = io.Fonts->Fonts[0]; + + ImGui::PushID(label.c_str()); + + float availWidth = (ImGui::GetContentRegionAvail().x / 3.0f) - 9.0f; + ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2{ 0, 0 }); + + float lineHeight = GImGui->Font->FontSize + GImGui->Style.FramePadding.y * 2.0f; + ImVec2 buttonSize = { 3.0f, lineHeight }; + + ImGui::PushStyleColor(ImGuiCol_Button, ImVec4{ 0.8f, 0.1f, 0.15f, 1.0f }); + ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4{ 0.9f, 0.2f, 0.2f, 1.0f }); + ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4{ 0.8f, 0.1f, 0.15f, 1.0f }); + ImGui::PushFont(boldFont); + if (ImGui::Button("###X", buttonSize)) + values->x = resetValue; + ImGui::PopFont(); + ImGui::PopStyleColor(3); + + ImGui::SameLine(); + ImGui::PushItemWidth(availWidth); + ImGui::DragFloat("##X", &values->x, rate, min, 0.0f, "%.2f"); + ImGui::PopItemWidth(); + ImGui::SameLine(); + + ImGui::PushStyleColor(ImGuiCol_Button, ImVec4{ 0.2f, 0.7f, 0.2f, 1.0f }); + ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4{ 0.3f, 0.8f, 0.3f, 1.0f }); + ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4{ 0.2f, 0.7f, 0.2f, 1.0f }); + ImGui::PushFont(boldFont); + if (ImGui::Button("Y", buttonSize)) + values->y = resetValue; + ImGui::PopFont(); + ImGui::PopStyleColor(3); + ImGui::SameLine(); + ImGui::PushItemWidth(availWidth); + ImGui::DragFloat("##Y", &values->y, rate, 0.0f, 0.0f, "%.2f"); + ImGui::PopItemWidth(); + + ImGui::PopStyleVar(); + + ImGui::PopID(); + //ImGui::Text("Hello"); + } }; \ No newline at end of file diff --git a/Nuake/src/Scripting/ScriptingEngineNet.cpp b/Nuake/src/Scripting/ScriptingEngineNet.cpp index b321b422..64aeb43f 100644 --- a/Nuake/src/Scripting/ScriptingEngineNet.cpp +++ b/Nuake/src/Scripting/ScriptingEngineNet.cpp @@ -198,7 +198,7 @@ namespace Nuake exposedVar.Name = f.GetName(); auto typeName = f.GetType().GetFullName(); - ExposedVarTypes varType = ExposedVarTypes::Float; + ExposedVarTypes varType = ExposedVarTypes::Unsupported; if (typeName == "System.Single") { varType = ExposedVarTypes::Float; @@ -215,10 +215,21 @@ namespace Nuake { varType = ExposedVarTypes::Bool; } + else if (typeName == "System.Numerics.Vector2") + { + varType = ExposedVarTypes::Vector2; + } + else if (typeName == "System.Numerics.Vector3") + { + varType = ExposedVarTypes::Vector3; + } - exposedVar.Type = varType; + if (varType != ExposedVarTypes::Unsupported) + { + exposedVar.Type = varType; + gameScriptObject.exposedVars.push_back(exposedVar); + } - gameScriptObject.exposedVars.push_back(exposedVar); Logger::Log("Exposed field detected: " + std::string(f.GetName()) + " of type " + std::string(f.GetType().GetFullName()) ); } } @@ -300,6 +311,16 @@ namespace Nuake exposedVar.Value = (bool)classInstance.GetFieldValue(varName); break; } + case ExposedVarTypes::Vector2: + { + exposedVar.Value = (Vector2)classInstance.GetFieldValue(varName); + break; + } + case ExposedVarTypes::Vector3: + { + exposedVar.Value = (Vector3)classInstance.GetFieldValue(varName); + break; + } case ExposedVarTypes::Entity: { exposedVar.Value = classInstance.GetFieldValue(varName); diff --git a/Nuake/src/Scripting/ScriptingEngineNet.h b/Nuake/src/Scripting/ScriptingEngineNet.h index 3fb3c492..80430e85 100644 --- a/Nuake/src/Scripting/ScriptingEngineNet.h +++ b/Nuake/src/Scripting/ScriptingEngineNet.h @@ -26,7 +26,8 @@ namespace Nuake { Entity, Vector2, Vector3, - Vector4 + Vector4, + Unsupported }; struct ExposedVar