Prefab saving

This commit is contained in:
antopilo
2021-08-26 20:46:34 -04:00
parent 96f0cdb4e9
commit 3fb0a2cc81
6 changed files with 117 additions and 9 deletions

View File

@@ -29,6 +29,7 @@
#include "src/Scene/Systems/QuakeMapBuilder.h"
#include "src/Scene/Components/LightComponent.h"
#include "UIComponents/Viewport.h"
#include <src/Resource/Prefab.h>
namespace Nuake {
Ref<UI::UserInterface> userInterface;
@@ -159,7 +160,8 @@ namespace Nuake {
{
ImGuiTreeNodeFlags base_flags = ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_OpenOnDoubleClick | ImGuiTreeNodeFlags_SpanAvailWidth;
std::string name = e.GetComponent<NameComponent>().Name;
NameComponent& nameComponent = e.GetComponent<NameComponent>();
std::string name = nameComponent.Name;
ParentComponent& parent = e.GetComponent<ParentComponent>();
if (m_SelectedEntity == e)
@@ -172,8 +174,11 @@ namespace Nuake {
if (parent.Children.size() <= 0)
base_flags |= ImGuiTreeNodeFlags_Leaf;
if(nameComponent.IsPrefab)
ImGui::PushStyleColor(ImGuiCol_Text, IM_COL32(0,255,0,255));
bool open = ImGui::TreeNodeEx(name.c_str(), base_flags);
if(nameComponent.IsPrefab)
ImGui::PopStyleColor();
if (ImGui::BeginDragDropSource())
{
@@ -230,7 +235,12 @@ namespace Nuake {
p.HasParent = false;
}
}
ImGui::Selectable("Save as prefab");
if (ImGui::Selectable("Save as new prefab"))
{
Ref<Prefab> newPrefab = Prefab::CreatePrefabFromEntity(m_SelectedEntity);
std::string savePath = FileDialog::SaveFile("*.prefab");
newPrefab->SaveAs(savePath);
}
ImGui::EndPopup();
}
if (open)
@@ -256,7 +266,7 @@ namespace Nuake {
if (!scene)
return;
if (ImGui::Begin(" Environnement"))
if (ImGui::Begin("Environnement"))
{
auto env = Engine::GetCurrentScene()->GetEnvironment();
if (ImGui::CollapsingHeader("Procedural Sky"))
@@ -1307,6 +1317,8 @@ namespace Nuake {
DrawGizmos();
DrawRessourceWindow();
pInterface.DrawEntitySettings();
DrawViewport();
DrawSceneTree();
//DrawDirectoryExplorer();
@@ -1317,7 +1329,6 @@ namespace Nuake {
filesystem.Draw();
filesystem.DrawDirectoryExplorer();
pInterface.DrawEntitySettings();
if (m_ShowImGuiDemo)
ImGui::ShowDemoWindow();

View File

@@ -0,0 +1,38 @@
#pragma once
namespace Nuake {
class AABB
{
public:
Vector3 Min;
Vector3 Max;
// Transforms the bounding box and recalculate an axis aligned box
void Transform(Matrix4 transform)
{
Min = Vector3(transform * Vector4(Min, 0.0f));
Max = Vector3(transform * Vector4(Max, 0.0f));
if (Min.x > Max.x)
{
float max = Max.x;
Max.x = Min.x;
Min.x = max;
}
if (Min.y > Max.y)
{
float max = Max.y;
Max.y = Min.y;
Min.y = max;
}
if (Min.z > Max.z)
{
float max = Max.z;
Max.z = Min.z;
Min.z = max;
}
}
};
}

View File

@@ -0,0 +1,24 @@
#include "Prefab.h"
#include "src/Scene/Components/ParentComponent.h"
namespace Nuake {
Ref<Prefab> Prefab::CreatePrefabFromEntity(Entity entity)
{
Ref<Prefab> prefab = CreateRef<Prefab>();
ParentComponent parentC = entity.GetComponent<ParentComponent>();
prefab->EntityWalker(entity);
return prefab;
}
void Prefab::EntityWalker(Entity entity)
{
entity.GetComponent<NameComponent>().IsPrefab = true;
Entities.push_back(entity);
for (const Entity& e : entity.GetComponent<ParentComponent>().Children)
{
EntityWalker(e);
}
}
}

View File

@@ -2,14 +2,16 @@
#include <string>
#include <vector>
#include <src/Scene/Entities/Entity.h>
#include "src/Resource/Serializable.h"
namespace Nuake {
class Prefab
class Prefab : ISerializable
{
public:
std::string Path;
std::vector<Entity> Entities;
static Ref<Prefab> CreatePrefabFromEntity(Entity entity);
Prefab()
{
Path = "";
@@ -20,5 +22,36 @@ namespace Nuake {
{
Entities.push_back(entity);
}
void SaveAs(const std::string& path)
{
Path = path;
FileSystem::BeginWriteFile(path);
FileSystem::WriteLine(Serialize().dump(4));
FileSystem::EndWriteFile();
}
json Serialize()
{
BEGIN_SERIALIZE();
SERIALIZE_VAL(Path);
std::vector<json> entities = std::vector<json>();
for (Entity e : Entities)
entities.push_back(e.Serialize());
SERIALIZE_VAL_LBL("Entities", entities);
END_SERIALIZE();
}
bool Deserialize(const std::string& str)
{
BEGIN_DESERIALIZE();
Path = j["Path"];
return true;
}
private:
void EntityWalker(Entity entity);
};
}

View File

@@ -6,8 +6,9 @@ namespace Nuake {
class NameComponent
{
public:
int ID;
std::string Name = "Entity";
int ID;
bool IsPrefab = false;
json Serialize()
{

View File

@@ -157,7 +157,7 @@ namespace Nuake {
void Scene::DrawShadows()
{
auto meshView = m_Registry.view<TransformComponent, MeshComponent>();
auto quakeView = m_Registry.view<TransformComponent, QuakeMapComponent>();
auto quakeView = m_Registry.view<TransformComponent, BSPBrushComponent>();
auto view = m_Registry.view<TransformComponent, LightComponent>();
Ref<Camera> cam = m_EditorCamera;
@@ -177,6 +177,7 @@ namespace Nuake {
glCullFace(GL_BACK);
// Build a list of mesh to draw.
for (auto l : view)
{
auto [lightTransform, light] = view.get<TransformComponent, LightComponent>(l);