Made Inspector actual do stuff. ( makes GameObjects move with transform component )

This commit is contained in:
OusmBlueNinja 2024-12-25 18:53:17 -06:00
parent 15084799bd
commit e660a7ffd3
5 changed files with 111 additions and 84 deletions

View File

@ -2,7 +2,7 @@
# Compiler and Flags # Compiler and Flags
CXX := g++ CXX := g++
CXXFLAGS := -Wall -Wextra -std=c++17 CXXFLAGS := -Wall -Wextra -std=c++17 -g
# Directories # Directories
SRC_DIR := src SRC_DIR := src

View File

@ -218,7 +218,7 @@ void MyEngine::Run()
// Pseudocode: // Pseudocode:
GameObject cube; GameObject cube;
cube.transform.position = glm::vec3(0.f, 0.f, 0.f); cube.transform.position = glm::vec3(0.f, 0.f, 0.f);
cube.transform.rotation = glm::vec3(0.f, 0.f, 0.f); cube.transform.rotation = glm::vec3(0.f, 0.5f, 0.f);
cube.transform.scale = glm::vec3(1.f, 1.f, 1.f); cube.transform.scale = glm::vec3(1.f, 1.f, 1.f);
// Suppose we loaded a VAO, an EBO with 36 indices for the cube, // Suppose we loaded a VAO, an EBO with 36 indices for the cube,
@ -255,10 +255,8 @@ void MyEngine::Run()
// Show main DockSpace // Show main DockSpace
ShowDockSpace(); ShowDockSpace();
static Transform myTransform;
static Script myScript;
m_InspectorWindow->Show(myTransform, myScript); m_InspectorWindow->Show();
// Show our windows // Show our windows
m_RenderWindow->Show(); // The spinning triangle as ImGui::Image m_RenderWindow->Show(); // The spinning triangle as ImGui::Image

View File

@ -1,21 +1,19 @@
#include "InspectorWindow.h" #include "InspectorWindow.h"
#include <cstdio> // for debugging or printing if needed #include <cstdio> // for debugging or printing if needed
#include <cstring> // for strcpy, if needed #include <cstring> // for strcpy, if needed
float* glmVec3ToFloatArray(const glm::vec3& vec) { #include <glm/gtc/type_ptr.hpp> // Required for glm::value_ptr
static float outArray[3];
outArray[0] = vec.x;
outArray[1] = vec.y;
outArray[2] = vec.z;
return outArray;
}
void InspectorWindow::Show(Transform& transform, Script& script) #include <vector>
extern std::vector<GameObject> m_GameObjects;
void InspectorWindow::Show()
{ {
// Increase window/item spacing for a cleaner look // Increase window/item spacing for a cleaner look
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(12, 12)); ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(12, 12));
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(6, 4)); ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(6, 4));
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(10, 10)); ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(10, 10));
if (ImGui::Begin("Inspector")) if (ImGui::Begin("Inspector"))
{ {
@ -34,6 +32,8 @@ void InspectorWindow::Show(Transform& transform, Script& script)
if (transformOpen) if (transformOpen)
{ {
Transform* transform = &m_GameObjects[0].transform;
if (ImGui::IsItemHovered()) if (ImGui::IsItemHovered())
{ {
ImGui::BeginTooltip(); ImGui::BeginTooltip();
@ -50,30 +50,45 @@ void InspectorWindow::Show(Transform& transform, Script& script)
{ {
// We'll assign colors for X, Y, Z buttons // We'll assign colors for X, Y, Z buttons
// (normal, hovered, active) // (normal, hovered, active)
static const ImVec4 colX = ImVec4(1.0f, 0.4f, 0.4f, 1.0f); static const ImVec4 colX = ImVec4(1.0f, 0.4f, 0.4f, 1.0f);
static const ImVec4 colXHover = ImVec4(1.0f, 0.6f, 0.6f, 1.0f); static const ImVec4 colXHover = ImVec4(1.0f, 0.6f, 0.6f, 1.0f);
static const ImVec4 colXActive= ImVec4(1.0f, 0.2f, 0.2f, 1.0f); static const ImVec4 colXActive = ImVec4(1.0f, 0.2f, 0.2f, 1.0f);
static const ImVec4 colY = ImVec4(0.4f, 1.0f, 0.4f, 1.0f); static const ImVec4 colY = ImVec4(0.4f, 1.0f, 0.4f, 1.0f);
static const ImVec4 colYHover = ImVec4(0.6f, 1.0f, 0.6f, 1.0f); static const ImVec4 colYHover = ImVec4(0.6f, 1.0f, 0.6f, 1.0f);
static const ImVec4 colYActive= ImVec4(0.2f, 1.0f, 0.2f, 1.0f); static const ImVec4 colYActive = ImVec4(0.2f, 1.0f, 0.2f, 1.0f);
static const ImVec4 colZ = ImVec4(0.4f, 0.4f, 1.0f, 1.0f); static const ImVec4 colZ = ImVec4(0.4f, 0.4f, 1.0f, 1.0f);
static const ImVec4 colZHover = ImVec4(0.6f, 0.6f, 1.0f, 1.0f); static const ImVec4 colZHover = ImVec4(0.6f, 0.6f, 1.0f, 1.0f);
static const ImVec4 colZActive= ImVec4(0.2f, 0.2f, 1.0f, 1.0f); static const ImVec4 colZActive = ImVec4(0.2f, 0.2f, 1.0f, 1.0f);
const char* axisNames[3] = { "X", "Y", "Z" }; const char *axisNames[3] = {"X", "Y", "Z"};
// We'll reference transform.position here // We'll reference transform.position here
float* pos = glmVec3ToFloatArray(transform.position); float *pos = glm::value_ptr(transform->position);
ImGui::PushID("PositionRow"); ImGui::PushID("PositionRow");
for (int i = 0; i < 3; i++) for (int i = 0; i < 3; i++)
{ {
// Determine color set // Determine color set
ImVec4 col, colH, colA; ImVec4 col, colH, colA;
if (i == 0) { col = colX; colH = colXHover; colA = colXActive; } if (i == 0)
else if (i == 1) { col = colY; colH = colYHover; colA = colYActive; } {
else { col = colZ; colH = colZHover; colA = colZActive; } col = colX;
colH = colXHover;
colA = colXActive;
}
else if (i == 1)
{
col = colY;
colH = colYHover;
colA = colYActive;
}
else
{
col = colZ;
colH = colZHover;
colA = colZActive;
}
// Push color style for button // Push color style for button
ImGui::PushStyleColor(ImGuiCol_Button, col); ImGui::PushStyleColor(ImGuiCol_Button, col);
@ -92,7 +107,8 @@ void InspectorWindow::Show(Transform& transform, Script& script)
ImGui::SetNextItemWidth(60.0f); ImGui::SetNextItemWidth(60.0f);
ImGui::DragFloat((std::string("##Pos") + axisNames[i]).c_str(), &pos[i], 0.1f); ImGui::DragFloat((std::string("##Pos") + axisNames[i]).c_str(), &pos[i], 0.1f);
if (i < 2) ImGui::SameLine(0, 15); if (i < 2)
ImGui::SameLine(0, 15);
} }
ImGui::PopID(); ImGui::PopID();
} }
@ -108,8 +124,8 @@ void InspectorWindow::Show(Transform& transform, Script& script)
{ {
// Same approach, but referencing transform.rotation // Same approach, but referencing transform.rotation
const char* axisNames[3] = { "X", "Y", "Z" }; const char *axisNames[3] = {"X", "Y", "Z"};
float* rot = glmVec3ToFloatArray(transform.rotation); float *rot = glm::value_ptr(transform->rotation);
// We can reuse the same color sets // We can reuse the same color sets
ImGui::PushID("RotationRow"); ImGui::PushID("RotationRow");
@ -117,20 +133,23 @@ void InspectorWindow::Show(Transform& transform, Script& script)
{ {
// Decide color sets for X, Y, Z // Decide color sets for X, Y, Z
ImVec4 col, colH, colA; ImVec4 col, colH, colA;
if (i == 0) { if (i == 0)
{
col = ImVec4(1.0f, 0.4f, 0.4f, 1.0f); col = ImVec4(1.0f, 0.4f, 0.4f, 1.0f);
colH= ImVec4(1.0f, 0.6f, 0.6f, 1.0f); colH = ImVec4(1.0f, 0.6f, 0.6f, 1.0f);
colA= ImVec4(1.0f, 0.2f, 0.2f, 1.0f); colA = ImVec4(1.0f, 0.2f, 0.2f, 1.0f);
} }
else if (i == 1) { else if (i == 1)
{
col = ImVec4(0.4f, 1.0f, 0.4f, 1.0f); col = ImVec4(0.4f, 1.0f, 0.4f, 1.0f);
colH= ImVec4(0.6f, 1.0f, 0.6f, 1.0f); colH = ImVec4(0.6f, 1.0f, 0.6f, 1.0f);
colA= ImVec4(0.2f, 1.0f, 0.2f, 1.0f); colA = ImVec4(0.2f, 1.0f, 0.2f, 1.0f);
} }
else { else
{
col = ImVec4(0.4f, 0.4f, 1.0f, 1.0f); col = ImVec4(0.4f, 0.4f, 1.0f, 1.0f);
colH= ImVec4(0.6f, 0.6f, 1.0f, 1.0f); colH = ImVec4(0.6f, 0.6f, 1.0f, 1.0f);
colA= ImVec4(0.2f, 0.2f, 1.0f, 1.0f); colA = ImVec4(0.2f, 0.2f, 1.0f, 1.0f);
} }
ImGui::PushStyleColor(ImGuiCol_Button, col); ImGui::PushStyleColor(ImGuiCol_Button, col);
@ -147,7 +166,8 @@ void InspectorWindow::Show(Transform& transform, Script& script)
ImGui::SetNextItemWidth(60.0f); ImGui::SetNextItemWidth(60.0f);
ImGui::DragFloat((std::string("##Rot") + axisNames[i]).c_str(), &rot[i], 0.1f); ImGui::DragFloat((std::string("##Rot") + axisNames[i]).c_str(), &rot[i], 0.1f);
if (i < 2) ImGui::SameLine(0, 15); if (i < 2)
ImGui::SameLine(0, 15);
} }
ImGui::PopID(); ImGui::PopID();
} }
@ -162,28 +182,31 @@ void InspectorWindow::Show(Transform& transform, Script& script)
ImGui::Spacing(); ImGui::Spacing();
{ {
const char* axisNames[3] = { "X", "Y", "Z" }; const char *axisNames[3] = {"X", "Y", "Z"};
float* scl = glmVec3ToFloatArray(transform.scale); float *scl = glm::value_ptr(transform->scale);
ImGui::PushID("ScaleRow"); ImGui::PushID("ScaleRow");
for (int i = 0; i < 3; i++) for (int i = 0; i < 3; i++)
{ {
// same color approach // same color approach
ImVec4 col, colH, colA; ImVec4 col, colH, colA;
if (i == 0) { if (i == 0)
{
col = ImVec4(1.0f, 0.4f, 0.4f, 1.0f); col = ImVec4(1.0f, 0.4f, 0.4f, 1.0f);
colH= ImVec4(1.0f, 0.6f, 0.6f, 1.0f); colH = ImVec4(1.0f, 0.6f, 0.6f, 1.0f);
colA= ImVec4(1.0f, 0.2f, 0.2f, 1.0f); colA = ImVec4(1.0f, 0.2f, 0.2f, 1.0f);
} }
else if (i == 1) { else if (i == 1)
{
col = ImVec4(0.4f, 1.0f, 0.4f, 1.0f); col = ImVec4(0.4f, 1.0f, 0.4f, 1.0f);
colH= ImVec4(0.6f, 1.0f, 0.6f, 1.0f); colH = ImVec4(0.6f, 1.0f, 0.6f, 1.0f);
colA= ImVec4(0.2f, 1.0f, 0.2f, 1.0f); colA = ImVec4(0.2f, 1.0f, 0.2f, 1.0f);
} }
else { else
{
col = ImVec4(0.4f, 0.4f, 1.0f, 1.0f); col = ImVec4(0.4f, 0.4f, 1.0f, 1.0f);
colH= ImVec4(0.6f, 0.6f, 1.0f, 1.0f); colH = ImVec4(0.6f, 0.6f, 1.0f, 1.0f);
colA= ImVec4(0.2f, 0.2f, 1.0f, 1.0f); colA = ImVec4(0.2f, 0.2f, 1.0f, 1.0f);
} }
ImGui::PushStyleColor(ImGuiCol_Button, col); ImGui::PushStyleColor(ImGuiCol_Button, col);
@ -200,8 +223,8 @@ void InspectorWindow::Show(Transform& transform, Script& script)
ImGui::SetNextItemWidth(60.0f); ImGui::SetNextItemWidth(60.0f);
ImGui::DragFloat((std::string("##Scl") + axisNames[i]).c_str(), &scl[i], 0.1f); ImGui::DragFloat((std::string("##Scl") + axisNames[i]).c_str(), &scl[i], 0.1f);
if (i < 2)
if (i < 2) ImGui::SameLine(0, 15); ImGui::SameLine(0, 15);
} }
ImGui::PopID(); ImGui::PopID();
} }
@ -216,40 +239,41 @@ void InspectorWindow::Show(Transform& transform, Script& script)
// 2) SCRIPT // 2) SCRIPT
// =========================== // ===========================
// We keep script text in white // We keep script text in white
if (ImGui::CollapsingHeader("Script##Main", ImGuiTreeNodeFlags_DefaultOpen)) // if (ImGui::CollapsingHeader("Script##Main", ImGuiTreeNodeFlags_DefaultOpen))
{ //{
if (ImGui::IsItemHovered()) // if (ImGui::IsItemHovered())
{ // {
ImGui::BeginTooltip(); // ImGui::BeginTooltip();
ImGui::TextUnformatted("Attach a script or logic component here."); // ImGui::TextUnformatted("Attach a script or logic component here.");
ImGui::EndTooltip(); // ImGui::EndTooltip();
} // }
ImGui::TextUnformatted("Script Name:"); // ImGui::TextUnformatted("Script Name:");
ImGui::SameLine(); // ImGui::SameLine();
{ // {
char buffer[128]; // char buffer[128];
std::snprintf(buffer, sizeof(buffer), "%s", script.scriptName.c_str()); // std::snprintf(buffer, sizeof(buffer), "%s", script.scriptName.c_str());
ImGui::SetNextItemWidth(-1); // ImGui::SetNextItemWidth(-1);
if (ImGui::InputText("##ScriptName", buffer, sizeof(buffer))) // if (ImGui::InputText("##ScriptName", buffer, sizeof(buffer)))
{ // {
script.scriptName = buffer; // script.scriptName = buffer;
} // }
} // }
ImGui::Spacing(); // ImGui::Spacing();
ImGui::TextUnformatted("Script Enabled:"); // ImGui::TextUnformatted("Script Enabled:");
ImGui::SameLine(); // ImGui::SameLine();
ImGui::Checkbox("##ScriptEnabled", &script.enabled); // ImGui::Checkbox("##ScriptEnabled", &script.enabled);
ImGui::Spacing(); // ImGui::Spacing();
ImGui::Separator(); // ImGui::Separator();
} //}
}
ImGui::End(); ImGui::End();
} //
// Restore style // Restore style
ImGui::PopStyleVar(3); ImGui::PopStyleVar(3);
} }

View File

@ -26,9 +26,10 @@ public:
// Show the window (call each frame) // Show the window (call each frame)
// Pass references to your components, so any changes get applied to them. // Pass references to your components, so any changes get applied to them.
void Show(Transform& transform, Script& script); void Show();
private: private:
// You can store additional state or styling here if needed // You can store additional state or styling here if needed
// e.g. bool m_SomeInternalFlag = false; // e.g. bool m_SomeInternalFlag = false;
}; };

View File

@ -16,6 +16,10 @@
extern std::vector<GameObject> m_GameObjects; extern std::vector<GameObject> m_GameObjects;
#define CAM_FOV 45.0f
#define CAM_NEAR_PLAIN 0.1f
#define CAM_FAR_PLAIN 1000.0f
// Include your AssetManager & Shader headers // Include your AssetManager & Shader headers
#include "Engine/AssetManager.h" #include "Engine/AssetManager.h"
@ -202,7 +206,7 @@ void RenderWindow::RenderSceneToFBO()
// Define view and projection matrices once // Define view and projection matrices once
glm::mat4 view = glm::translate(glm::mat4(1.f), glm::vec3(0.f, 0.f, -5.f)); glm::mat4 view = glm::translate(glm::mat4(1.f), glm::vec3(0.f, 0.f, -5.f));
float aspect = (m_LastHeight != 0) ? (float)m_LastWidth / (float)m_LastHeight : 1.0f; float aspect = (m_LastHeight != 0) ? (float)m_LastWidth / (float)m_LastHeight : 1.0f;
glm::mat4 proj = glm::perspective(glm::radians(45.f), aspect, 0.1f, 100.f); glm::mat4 proj = glm::perspective(glm::radians(CAM_FOV), aspect, CAM_NEAR_PLAIN, CAM_FAR_PLAIN);
// Iterate over each GameObject and render it // Iterate over each GameObject and render it
for (auto& obj : m_GameObjects) for (auto& obj : m_GameObjects)