Editor Refactor
This refactor of the editor code makes the code more maintainable. All widget objects have now moved away from RAII and are now just allocated object that live for the entirety of the applications lifetime. This feels better as I am used to this style plus constantly pushing and popping objects from the stack seems a little wasteful (although I as of right now have no way to prove that it is ).
This commit is contained in:
68
Editor/src/AssetManagement/AssetFinder.h
Normal file
68
Editor/src/AssetManagement/AssetFinder.h
Normal file
@ -0,0 +1,68 @@
|
||||
#pragma once
|
||||
#include "../../YoggieEngine/src/YoggieEngine.h"
|
||||
#include "EditorWindow.h"
|
||||
#include "AssetManagement/AssetManager.h"
|
||||
|
||||
class AssetFinder : EditorWindow {
|
||||
public:
|
||||
AssetFinder() : EditorWindow("Assets") {}
|
||||
|
||||
void Draw() override {
|
||||
|
||||
|
||||
ImGui::DragInt("IconSize", &iconSize, 1, 30, 90);
|
||||
|
||||
|
||||
if (ImGui::BeginTable("##resources", 3))
|
||||
{
|
||||
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.f, 0.f, 0.f, 0.f));
|
||||
ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(0.f, 0.f, 0.f, 0.f));
|
||||
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(1.f, 1.f, 1.f, 0.2f));
|
||||
|
||||
YoggieEngine::Texture folderIcon = YoggieEngine::Texture("rsc/folderIcon.png");
|
||||
YoggieEngine::Texture assetIcon = YoggieEngine::Texture("rsc/assetIcon.png");
|
||||
|
||||
|
||||
int row = 0;
|
||||
int column = 0;
|
||||
for (auto& asset : AssetManager::assets) {
|
||||
if (column % 3 == 0) {
|
||||
ImGui::TableNextRow();
|
||||
column = 0;
|
||||
row++;
|
||||
}
|
||||
|
||||
ImGui::TableSetColumnIndex(column);
|
||||
|
||||
if (asset.isFolder) {
|
||||
ImGui::ImageButton(
|
||||
(ImTextureID)folderIcon.GetID(),
|
||||
ImVec2{ (float)iconSize,(float)iconSize });
|
||||
ImGui::Text(asset.GetName(), row);
|
||||
|
||||
}
|
||||
else {
|
||||
ImGui::ImageButton(
|
||||
(ImTextureID)assetIcon.GetID(),
|
||||
ImVec2{ (float)iconSize, (float)iconSize });
|
||||
ImGui::Text(asset.GetName(), row);
|
||||
|
||||
}
|
||||
|
||||
|
||||
column++;
|
||||
}
|
||||
|
||||
|
||||
ImGui::PopStyleColor(3);
|
||||
ImGui::EndTable();
|
||||
const GLuint textures[2]{ assetIcon.GetID(), folderIcon.GetID() };
|
||||
glDeleteTextures(2, textures);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
int iconSize = 60;
|
||||
};
|
||||
|
129
Editor/src/AssetManagement/SceneSerializer.cpp
Normal file
129
Editor/src/AssetManagement/SceneSerializer.cpp
Normal file
@ -0,0 +1,129 @@
|
||||
#include "SceneSerializer.h"
|
||||
#include "../../YoggieEngine/src/YoggieEngine.h"
|
||||
#include <yaml-cpp/yaml.h>
|
||||
#include <yaml-cpp/node/type.h>
|
||||
#include <filesystem>
|
||||
|
||||
void WriteFile(std::string& emitter, std::filesystem::path path)
|
||||
{
|
||||
std::cout << "Writing Scene file to: " << path.u8string() << std::endl;
|
||||
std::ofstream sceneFile;
|
||||
sceneFile.open(path.u8string());
|
||||
|
||||
sceneFile << emitter.c_str();
|
||||
|
||||
sceneFile.close();
|
||||
}
|
||||
|
||||
YAML::Emitter& operator<< (YAML::Emitter& emitter, glm::vec3& vector) {
|
||||
emitter << YAML::Flow << YAML::BeginSeq << vector.x << vector.y << vector.x << YAML::EndSeq;
|
||||
return emitter;
|
||||
}
|
||||
|
||||
|
||||
|
||||
std::string Serialize(YoggieEngine::Scene& scene) {
|
||||
YAML::Emitter emitter;
|
||||
|
||||
emitter << YAML::BeginMap;
|
||||
emitter << YAML::Key << "Scene" << YAML::Value << "test-Scene";
|
||||
emitter << YAML::Key << "Entities" << YAML::Value << YAML::BeginSeq;
|
||||
|
||||
scene.getReg().each([&emitter, &scene](entt::entity enttNumber) {
|
||||
|
||||
YoggieEngine::Entity entity = YoggieEngine::Entity(enttNumber, &scene);
|
||||
|
||||
emitter << YAML::BeginMap;
|
||||
emitter << YAML::Key << "Entity" << YAML::Value << entity.GetComponent<YoggieEngine::IdentifierComponent>().name;
|
||||
|
||||
if (entity.HasComponent<YoggieEngine::IdentifierComponent>()) {
|
||||
emitter << YAML::Key << "Ident";
|
||||
emitter << YAML::BeginMap;
|
||||
emitter << YAML::Value << entity.GetComponent<YoggieEngine::IdentifierComponent>().name;
|
||||
emitter << YAML::EndMap;
|
||||
}
|
||||
|
||||
if (entity.HasComponent<YoggieEngine::TransformComponent>()) {
|
||||
emitter << YAML::Key << "Transform" << YAML::Value;
|
||||
|
||||
emitter << YAML::BeginMap;
|
||||
|
||||
emitter << YAML::Key << "Position";
|
||||
emitter << YAML::Value << entity.GetComponent<YoggieEngine::TransformComponent>().Position;
|
||||
|
||||
emitter << YAML::Key << "Rotation";
|
||||
emitter << YAML::Value << entity.GetComponent<YoggieEngine::TransformComponent>().Rotation;
|
||||
|
||||
emitter << YAML::Key << "Scale";
|
||||
emitter << YAML::Value << entity.GetComponent<YoggieEngine::TransformComponent>().Scale;
|
||||
|
||||
emitter << YAML::EndMap;
|
||||
}
|
||||
|
||||
|
||||
if (entity.HasComponent<YoggieEngine::LightComponent>()) {
|
||||
emitter << YAML::Key << "Light";
|
||||
emitter << YAML::Value;
|
||||
emitter << YAML::BeginMap;
|
||||
emitter << YAML::Key << "Color";
|
||||
emitter << YAML::Value << entity.GetComponent<YoggieEngine::LightComponent>().Color;
|
||||
emitter << YAML::EndMap;
|
||||
}
|
||||
|
||||
|
||||
emitter << YAML::EndMap;
|
||||
});
|
||||
|
||||
emitter << YAML::EndSeq;
|
||||
emitter << YAML::EndMap;
|
||||
|
||||
return std::string(emitter.c_str());
|
||||
}
|
||||
|
||||
|
||||
void SaveScene(std::filesystem::path path, YoggieEngine::Scene& scene) {
|
||||
std::string YAMLString = Serialize(scene);
|
||||
WriteFile(YAMLString, path);
|
||||
}
|
||||
|
||||
|
||||
void LoadScene(std::filesystem::path path, YoggieEngine::Scene& scene)
|
||||
{
|
||||
auto sceneYAML = YAML::LoadFile(path.u8string());
|
||||
|
||||
if (!sceneYAML["Scene"]) {
|
||||
spdlog::error("Not a scene file!");
|
||||
return;
|
||||
}
|
||||
|
||||
scene.getReg().clear();
|
||||
|
||||
std::string SceneName = sceneYAML["Scene"].as<std::string>();
|
||||
|
||||
auto entities = sceneYAML["Entities"];
|
||||
|
||||
for (const auto& entity : entities) {
|
||||
|
||||
std::string entityID = entity["Ident"].as<std::string>();
|
||||
YoggieEngine::Entity SE = scene.AddEntity(entityID);
|
||||
if (entity["Transform"])
|
||||
{
|
||||
YoggieEngine::TransformComponent tc = SE.GetComponent <YoggieEngine::TransformComponent> ();
|
||||
auto positionNode = entity["Transform"]["Position"];
|
||||
tc.Position = glm::vec3(positionNode[0].as<float>(), positionNode[1].as<float>(), positionNode[2].as<float>());
|
||||
|
||||
auto rotationNode = entity["Transform"]["Rotation"];
|
||||
tc.Rotation = glm::vec3(rotationNode[0].as<float>(), rotationNode[1].as<float>(), rotationNode[2].as<float>());
|
||||
|
||||
auto scaleNode = entity["Transform"]["Scale"];
|
||||
tc.Scale = glm::vec3(scaleNode[0].as<float>(), scaleNode[1].as<float>(), scaleNode[2].as<float>());
|
||||
}
|
||||
if (entity["Light"]) {
|
||||
YoggieEngine::LightComponent lc = SE.AddComponent<YoggieEngine::LightComponent>();
|
||||
lc.Color = glm::vec3(entity["Light"]["Color"][0].as<float>(), entity["Light"]["Color"][1].as<float>(), entity["Light"]["Color"][2].as<float>());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
16
Editor/src/AssetManagement/SceneSerializer.h
Normal file
16
Editor/src/AssetManagement/SceneSerializer.h
Normal file
@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
#include <entt/entity/fwd.hpp>
|
||||
#include <glm/glm.hpp>
|
||||
#include <yaml-cpp/yaml.h>
|
||||
#include <filesystem>
|
||||
#include "../../YoggieEngine/src/YoggieEngine.h"
|
||||
|
||||
void WriteFile(std::string& emitter, std::filesystem::path path);
|
||||
|
||||
YAML::Emitter& operator<< (YAML::Emitter& emitter, glm::vec3& vector);
|
||||
|
||||
std::string Serialize(YoggieEngine::Scene& scene);
|
||||
|
||||
void SaveScene(std::filesystem::path path, YoggieEngine::Scene& scene);
|
||||
|
||||
void LoadScene(std::filesystem::path path, YoggieEngine::Scene& scene);
|
Reference in New Issue
Block a user