Skip to content

Commit

Permalink
save scene from editor
Browse files Browse the repository at this point in the history
  • Loading branch information
goopey7 committed Dec 30, 2023
1 parent 2f2b8f9 commit 1645c37
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 25 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ file(GLOB STB_IMAGE_HEADER "libs/stb/stb_image.h")
add_executable(${PROJECT_NAME} ${ENGINE_SRC_FILES} ${STB_VORBIS_SOURCE})

#define app type
#target_compile_definitions(${PROJECT_NAME} PRIVATE GOOP_APPTYPE_EDITOR=1)
target_compile_definitions(${PROJECT_NAME} PRIVATE GOOP_APPTYPE_EDITOR=1)

# define systems
target_compile_definitions(${PROJECT_NAME} PRIVATE GOOP_RENDERER_VULKAN)
Expand Down
1 change: 1 addition & 0 deletions cfg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"game_file":"lvl.json","start_scene":"lvl_1"}
72 changes: 67 additions & 5 deletions editor/EditorApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ goop::App* goop::createEditor(int argc, char** argv, App* game, goop::Scene* sce
return new EditorApp(game, scene);
}

void EditorApp::init()
void EditorApp::init()
{
auto cfgOpt = loadJson("cfg.json");
if (!cfgOpt.has_value())
Expand All @@ -28,7 +28,7 @@ void EditorApp::init()
};

std::ofstream file("cfg.json");
file << cfg;
file << cfg.dump(2);
file.close();

cfgOpt = loadJson("cfg.json");
Expand All @@ -48,9 +48,8 @@ void EditorApp::init()

lvl["scenes"] = scenesArr;

std::string gameFile = cfg["game_file"];
std::ofstream file(gameFile.c_str());
file << lvl;
std::ofstream file(cfg["game_file"].get<std::string>().c_str());
file << lvl.dump(2);
file.close();

lvlOpt = loadJson(cfg["game_file"]);
Expand Down Expand Up @@ -96,13 +95,33 @@ void EditorApp::gui()
ImGui::DockSpace(dockspaceID, ImVec2(0.0f, 0.0f), dockspaceFlags);
ImGui::End();

// Menu bar (top of window)
ImGui::BeginMainMenuBar();
if (ImGui::Button(shouldPlay ? "Pause" : "Play"))
{
shouldPlay = !shouldPlay;
}
if (ImGui::Button("Save"))
{
json sceneJson = scene->saveScene();
json cfg = loadJson("cfg.json").value();
json lvl = loadJson(cfg["game_file"]).value();
for (json& sceneObj : lvl["scenes"])
{
if (sceneObj["name"] == scene->getScene()["name"])
{
sceneObj = sceneJson;
break;
}
}
std::ofstream file(cfg["game_file"].get<std::string>().c_str());
file << lvl.dump(2);
file.close();
}
ImGui::Text("%s", scene->getScene()["name"].get<std::string>().c_str());
ImGui::EndMainMenuBar();

// Game Viewport
auto r = goop::sys::gRenderer.get();
ImGui::Begin("Viewport");
ImVec2 max = ImGui::GetWindowContentRegionMax();
Expand All @@ -115,5 +134,48 @@ void EditorApp::gui()
ImGui::Image(r->getViewTexture(), viewportSize);
ImGui::End();

// Scene View
ImGui::Begin("Scene");
ImGui::Text("Scene View");
if (ImGui::Button("Add Entity"))
{
popupOpen = true;
ImVec2 centerPos = ImVec2(viewport->WorkPos.x + viewport->WorkSize.x * 0.5f,
viewport->WorkPos.y + viewport->WorkSize.y * 0.5f);
ImGui::SetNextWindowPos(centerPos, ImGuiCond_Always, ImVec2(0.5f, 0.5f));
}
ImGui::End();

if (popupOpen)
{
ImGui::OpenPopup("AddEntity");

ImVec2 centerPos = ImVec2(viewport->WorkPos.x + viewport->WorkSize.x * 0.5f,
viewport->WorkPos.y + viewport->WorkSize.y * 0.5f);

ImVec2 popupSize = ImVec2(200, 100); // Adjust the size if needed
ImVec2 popupPos =
ImVec2(centerPos.x - popupSize.x * 0.5f, centerPos.y - popupSize.y * 0.5f);
ImGui::SetNextWindowPos(popupPos, ImGuiCond_Always);
ImGui::SetNextWindowSize(popupSize, ImGuiCond_Always);
}

// Popup
if (ImGui::BeginPopup("AddEntity"))
{
ImGui::Text("This is a Popup!");
if (ImGui::Button("Close"))
{
ImGui::CloseCurrentPopup();
popupOpen = false;
}
ImGui::EndPopup();
}

// Inspector
ImGui::Begin("Inspector");
ImGui::Text("Inspector");
ImGui::End();

game->gui();
}
5 changes: 3 additions & 2 deletions editor/EditorApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ class EditorApp : public goop::App
private:
ImGuiDockNodeFlags dockspaceFlags = ImGuiDockNodeFlags_None;
ImGuiDockNodeFlags windowFlags =
ImGuiWindowFlags_NoDocking | ImGuiWindowFlags_NoTitleBar |
ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove |
ImGuiWindowFlags_NoDocking | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse |
ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove |
ImGuiWindowFlags_NoBringToFrontOnFocus | ImGuiWindowFlags_NoNavFocus;

App* game;
bool shouldPlay = false;
bool popupOpen = false;
};
4 changes: 0 additions & 4 deletions goop/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ void Core::run()
#endif
app->gui();

#ifdef GOOP_APPTYPE_EDITOR
ImGui::ShowDemoWindow();
#endif

ImGui::Render();

if (sys::gRenderer->isMeshQueueEmpty())
Expand Down
55 changes: 54 additions & 1 deletion goop/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
#include "Scene.h"
#include "goop/Components.h"
#include "goop/Entity.h"
#include <glm/ext/matrix_transform.hpp>
#include <glm/common.hpp>
#include <glm/gtx/matrix_decompose.hpp>

using namespace goop;

Expand Down Expand Up @@ -32,6 +33,7 @@ Entity Scene::getEntity(const std::string& tag)

void Scene::loadScene(nlohmann::json& startScene)
{
sceneJson = startScene;
if (startScene["entities"].is_null())
{
return;
Expand Down Expand Up @@ -66,3 +68,54 @@ void Scene::loadScene(nlohmann::json& startScene)
}
}
}

nlohmann::json Scene::getScene() const { return sceneJson; }

nlohmann::json Scene::saveScene()
{
sceneJson["entities"] = json::array();
auto view = registry.view<TagComponent>();
for (auto& entity : view)
{
auto e = goop::Entity(entity, this);
json eJson;
eJson["name"] = e.getComponent<TagComponent>().tag;
eJson["components"] = json::array();
if (e.hasComponent<TransformComponent>())
{
json transformJson;
transformJson["type"] = "transform";
glm::mat4 transform = e.getComponent<TransformComponent>().transform;
glm::vec3 scale, skew, translation;
glm::quat orientation;
glm::vec4 perspective;
glm::decompose(transform, scale, orientation, translation, skew, perspective);

transformJson["position"]["x"] = translation.x;
transformJson["position"]["y"] = translation.y;
transformJson["position"]["z"] = translation.z;

glm::vec3 euler = glm::eulerAngles(orientation);
transformJson["rotation"]["x"] = glm::degrees(euler.x);
transformJson["rotation"]["y"] = glm::degrees(euler.y);
transformJson["rotation"]["z"] = glm::degrees(euler.z);

transformJson["scale"]["x"] = scale.x;
transformJson["scale"]["y"] = scale.y;
transformJson["scale"]["z"] = scale.z;

eJson["components"].push_back(transformJson);
}
if (e.hasComponent<MeshComponent>())
{
json meshJson;
meshJson["type"] = "mesh";
meshJson["path"] = e.getComponent<MeshComponent>().path;
eJson["components"].push_back(meshJson);
}

sceneJson["entities"].push_back(eJson);
}

return sceneJson;
}
4 changes: 4 additions & 0 deletions goop/Scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class Scene

Entity createEntity(const std::string& tag = "Entity");
void loadScene(nlohmann::json& scene);
nlohmann::json getScene() const;
nlohmann::json saveScene();

template <typename T>
auto view() { return registry.view<T>(); }
Expand All @@ -27,5 +29,7 @@ class Scene
private:
entt::registry registry;
friend class Entity;

nlohmann::json sceneJson;
};
} // namespace goop
32 changes: 20 additions & 12 deletions imgui.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ Collapsed=0
DockId=0x00000006,0

[Window][Viewport]
Pos=8,27
Size=1806,1469
Pos=294,27
Size=1487,1435
Collapsed=0
DockId=0x00000005,0
DockId=0x00000007,0

[Window][Inspector]
Pos=8,27
Expand All @@ -27,14 +27,14 @@ Collapsed=0

[Window][Dockspace]
Pos=0,19
Size=2256,1485
Size=2252,1451
Collapsed=0

[Window][Example: Log]
Pos=248,884
Size=1787,548
Collapsed=0
DockId=0x00000005,0
DockId=0x00000007,0

[Window][Example: Console]
Pos=8,474
Expand All @@ -52,12 +52,20 @@ Pos=384,188
Size=276,255
Collapsed=0

[Window][Scene]
Pos=1783,27
Size=461,1435
Collapsed=0
DockId=0x00000008,0

[Docking][Data]
DockSpace ID=0x33675C32 Window=0x5B816B74 Pos=8,27 Size=2240,1469 Split=Y
DockNode ID=0x00000001 Parent=0x33675C32 SizeRef=1954,445 Split=X Selected=0x13926F0B
DockNode ID=0x00000003 Parent=0x00000001 SizeRef=284,941 Selected=0xE7039252
DockNode ID=0x00000004 Parent=0x00000001 SizeRef=1051,941 Split=X Selected=0x13926F0B
DockNode ID=0x00000005 Parent=0x00000004 SizeRef=1806,941 CentralNode=1 Selected=0x13926F0B
DockNode ID=0x00000006 Parent=0x00000004 SizeRef=432,941 Selected=0xE87781F4
DockNode ID=0x00000002 Parent=0x33675C32 SizeRef=1954,494 Selected=0xDE6E0C33
DockSpace ID=0x33675C32 Window=0x5B816B74 Pos=10,59 Size=2236,1435 Split=Y
DockNode ID=0x00000001 Parent=0x33675C32 SizeRef=1954,445 Split=X Selected=0x13926F0B
DockNode ID=0x00000003 Parent=0x00000001 SizeRef=284,941 Selected=0xE7039252
DockNode ID=0x00000004 Parent=0x00000001 SizeRef=1051,941 Split=X Selected=0x13926F0B
DockNode ID=0x00000005 Parent=0x00000004 SizeRef=1806,941 Split=X Selected=0x13926F0B
DockNode ID=0x00000007 Parent=0x00000005 SizeRef=1487,1435 CentralNode=1 Selected=0x13926F0B
DockNode ID=0x00000008 Parent=0x00000005 SizeRef=461,1435 Selected=0xE192E354
DockNode ID=0x00000006 Parent=0x00000004 SizeRef=432,941 Selected=0xE87781F4
DockNode ID=0x00000002 Parent=0x33675C32 SizeRef=1954,494 Selected=0xDE6E0C33

36 changes: 36 additions & 0 deletions lvl.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"scenes": [
{
"entities": [
{
"components": [
{
"position": {
"x": 0.0,
"y": 0.0,
"z": 0.0
},
"rotation": {
"x": 0.0,
"y": -0.0,
"z": 0.0
},
"scale": {
"x": 1.0,
"y": 1.0,
"z": 1.0
},
"type": "transform"
},
{
"path": "res/viking_room.obj",
"type": "mesh"
}
],
"name": "Viking Room"
}
],
"name": "lvl_1"
}
]
}

0 comments on commit 1645c37

Please sign in to comment.