Skip to content

Commit

Permalink
More changes
Browse files Browse the repository at this point in the history
  • Loading branch information
tippesi committed Aug 8, 2024
1 parent ac5886d commit d37a683
Show file tree
Hide file tree
Showing 15 changed files with 330 additions and 166 deletions.
6 changes: 5 additions & 1 deletion src/editor/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ namespace Atlas::Editor {

// Everything that needs the config comes afterwards
Singletons::icons = CreateRef<Icons>();
Singletons::blockingOperation = CreateRef<BlockingOperation>();
Singletons::renderTarget = CreateRef<Renderer::RenderTarget>(1280, 720);
Singletons::pathTraceRenderTarget = CreateRef<Renderer::PathTracerRenderTarget>(1280, 720);
Singletons::mainRenderer = mainRenderer;
Expand Down Expand Up @@ -84,6 +85,9 @@ namespace Atlas::Editor {
ContentDiscovery::Update();
Singletons::imguiWrapper->Update(&window, deltaTime);

if (Singletons::blockingOperation->block)
return;

// Create new windows for fully loaded scenes
for (size_t i = 0; i < waitToLoadScenes.size(); i++) {
if (waitToLoadScenes[i].IsLoaded()) {
Expand Down Expand Up @@ -309,7 +313,7 @@ namespace Atlas::Editor {

if (saveScene) {
auto activeSceneWindow = sceneWindows.empty() ? nullptr : sceneWindows[activeSceneIdx];
activeSceneWindow->SaveScene();
activeSceneWindow->SaveScene();
}

ImGui::EndMainMenuBar();
Expand Down
28 changes: 28 additions & 0 deletions src/editor/BlockingOperation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

#include <future>
#include <string>

namespace Atlas::Editor {

class BlockingOperation {

public:
void Block(const std::string& text, std::function<void()> blocker) {
if (future.valid())
future.get();
block = true;
blockText = text;
future = std::async(std::launch::async, [blocker, &block = block] {
blocker();
block = false;
});
}

std::future<void> future;
std::string blockText;
bool block = false;

};

}
2 changes: 2 additions & 0 deletions src/editor/Singletons.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace Atlas::Editor {
Ref<Renderer::MainRenderer> Singletons::mainRenderer;
Ref<Icons> Singletons::icons;
Ref<Config> Singletons::config;
Ref<BlockingOperation> Singletons::blockingOperation;

void Singletons::Destruct() {

Expand All @@ -17,6 +18,7 @@ namespace Atlas::Editor {
mainRenderer.reset();
icons.reset();
config.reset();
blockingOperation.reset();

}

Expand Down
2 changes: 2 additions & 0 deletions src/editor/Singletons.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "renderer/MainRenderer.h"
#include "Icons.h"
#include "Config.h"
#include "BlockingOperation.h"

namespace Atlas::Editor {

Expand All @@ -20,6 +21,7 @@ namespace Atlas::Editor {
static Ref<Renderer::MainRenderer> mainRenderer;
static Ref<Icons> icons;
static Ref<Config> config;
static Ref<BlockingOperation> blockingOperation;

};

Expand Down
60 changes: 52 additions & 8 deletions src/editor/ui/panels/PopupPanels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <imgui_stdlib.h>

#include "DataCreator.h"
#include "Singletons.h"
#include "Notifications.h"

namespace Atlas::Editor::UI {

Expand All @@ -17,6 +19,7 @@ namespace Atlas::Editor::UI {

RenderNewScenePopup();
RenderImportScenePopup();
RenderBlockingPopup();

}

Expand Down Expand Up @@ -129,16 +132,23 @@ namespace Atlas::Editor::UI {
ImGui::SameLine();

if (ImGui::Button("Ok") || ImGui::IsKeyReleased(ImGuiKey_Enter)) {
auto scene = DataCreator::CreateSceneFromMesh(filename, minSize, maxSize,
octreeDepth, invertUVs, addRigidBodies, combineMeshes, makeMeshesStatic);
scene->name = name;
Singletons::blockingOperation->Block("Importing scene. Please wait...",
[&]() {
auto scene = DataCreator::CreateSceneFromMesh(filename, minSize, maxSize,
octreeDepth, invertUVs, addRigidBodies, combineMeshes, makeMeshesStatic);
scene->name = name;

bool alreadyExisted;
Atlas::ResourceManager<Scene::Scene>::AddResource(name, scene, alreadyExisted);
bool alreadyExisted;
Atlas::ResourceManager<Scene::Scene>::AddResource(name, scene, alreadyExisted);

if (alreadyExisted) {
Log::Warning("Scene couldn't be created due to scene with same name existing already");
}
if (alreadyExisted) {
Log::Warning("Scene couldn't be created due to scene with same name existing already");
Notifications::Push({ .message = "Error importing scene " + scene->name });
return;
}

Notifications::Push({ .message = "Imported scene " + scene->name });
});

isImportScenePopupVisible = false;
ImGui::CloseCurrentPopup();
Expand All @@ -150,6 +160,40 @@ namespace Atlas::Editor::UI {

}

void PopupPanels::RenderBlockingPopup() {

if (!ImGui::IsPopupOpen("Blocking popup") && !Singletons::blockingOperation->block)
return;

if (!ImGui::IsPopupOpen("Blocking popup")) {
ImGui::OpenPopup("Blocking popup");
}

ImGui::PushStyleColor(ImGuiCol_PopupBg, ImVec4(0.0f, 0.0f, 0.0f, 0.7f));
ImGui::PushStyleColor(ImGuiCol_Border, ImVec4(1.0f, 1.0f, 1.0f, 1.0f));
ImGui::PushStyleVar(ImGuiStyleVar_PopupBorderSize, 2.0f);

SetupPopupSize(0.2f, 0.05f);

if (ImGui::BeginPopupModal("Blocking popup", nullptr, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoTitleBar)) {

ImGui::SetKeyboardFocusHere();

ImGui::Text(Singletons::blockingOperation->blockText.c_str());

if (!Singletons::blockingOperation->block)
ImGui::CloseCurrentPopup();

ImGui::EndPopup();

}

ImGui::PopStyleVar();
ImGui::PopStyleColor();
ImGui::PopStyleColor();

}

void PopupPanels::SetupPopupSize(float horizontalFactor, float verticalFactor) {

auto viewport = ImGui::GetMainViewport();
Expand Down
2 changes: 2 additions & 0 deletions src/editor/ui/panels/PopupPanels.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ namespace Atlas::Editor::UI {

static void RenderImportScenePopup();

static void RenderBlockingPopup();

};

}
8 changes: 5 additions & 3 deletions src/editor/ui/panels/ViewportPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ namespace Atlas::Editor::UI {

ImGui::Begin(GetNameID());

bool isBlocked = Singletons::blockingOperation->block;

// Workaround for offsetted Gizmo without resize after a restart
// Seems like some ImGuizmo or ImGui config isn't properly updated
if (firstFrame) {
Expand All @@ -59,7 +61,7 @@ namespace Atlas::Editor::UI {
CreateRenderPass();
}

if (scene != nullptr && validRegion && isActiveWindow) {
if (scene != nullptr && validRegion && isActiveWindow && !isBlocked) {
auto& config = Singletons::config;

if (config->pathTrace) {
Expand Down Expand Up @@ -97,12 +99,12 @@ namespace Atlas::Editor::UI {
ImGui::Image(set, region);
}

if (drawOverlayFunc)
if (drawOverlayFunc && !isBlocked)
drawOverlayFunc();

ImGui::SetCursorPos(ImVec2(0.0f, 0.0f));

if (drawMenuBarFunc) {
if (drawMenuBarFunc && !isBlocked) {
isFocused |= ImGui::IsWindowFocused();

drawMenuBarFunc();
Expand Down
21 changes: 13 additions & 8 deletions src/editor/ui/windows/SceneWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ namespace Atlas::Editor::UI {
ImGui::SetWindowFocus();
}

bool isBlocked = Singletons::blockingOperation->block;

ImGuiID dsID = ImGui::GetID(dockSpaceNameID.c_str());

if (!ImGui::DockBuilderGetNode(dsID) || resetDockingLayout) {
Expand Down Expand Up @@ -143,7 +145,7 @@ namespace Atlas::Editor::UI {
}

// We want to update the scene after all panels have update their respective values/changed the scene
if (!isPlaying) {
if (!isPlaying && !isBlocked) {
// Temporarily disable all scene cameras, only let editor camera be main
std::map<ECS::Entity, bool> cameraMainMap;
auto cameraSubset = scene->GetSubset<CameraComponent>();
Expand Down Expand Up @@ -635,17 +637,20 @@ namespace Atlas::Editor::UI {
if (!scene.IsLoaded())
return;

cameraState = Scene::Entity::Backup(scene.Get(), cameraEntity);
Singletons::blockingOperation->Block("Saving scene. Please wait...",
[&]() {
cameraState = Scene::Entity::Backup(scene.Get(), cameraEntity);

scene->DestroyEntity(cameraEntity);
scene->DestroyEntity(cameraEntity);

Serializer::SerializeScene(scene.Get(), "scenes/" + std::string(scene->name) + ".aescene", true);

cameraEntity = Scene::Entity::Restore(scene.Get(), cameraState);
Serializer::SerializeScene(scene.Get(), "scenes/" + std::string(scene->name) + ".aescene", true);

cameraState.clear();
cameraEntity = Scene::Entity::Restore(scene.Get(), cameraState);

cameraState.clear();

Notifications::Push({ .message = "Saved scene " + scene->name });
Notifications::Push({ .message = "Saved scene " + scene->name });
});

}

Expand Down
67 changes: 50 additions & 17 deletions src/engine/Serializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,7 @@ namespace Atlas {
}

if (saveDependencies) {
auto meshes = scene->GetMeshes();
std::map<Hash, ResourceHandle<Material>> materials;

for (const auto& mesh : meshes) {
if (!mesh.IsLoaded()) continue;

Loader::MeshLoader::SaveMesh(mesh.Get(), mesh.GetResource()->path);

for (const auto& material : mesh->data.materials)
materials[material.GetID()] = material;
}

for (const auto& [_, material] : materials) {
if (!material.IsLoaded()) continue;

Loader::MaterialLoader::SaveMaterial(material.Get(), material.GetResource()->path);
}
SaveDependencies(scene);
}

fileStream.close();
Expand Down Expand Up @@ -150,4 +134,53 @@ namespace Atlas {

}

void Serializer::SaveDependencies(Ref<Scene::Scene> scene, bool multithreaded) {

auto meshes = scene->GetMeshes();
std::map<Hash, ResourceHandle<Material>> materials;

if (multithreaded) {
std::atomic_int32_t counter = 0;

auto workerCount = std::thread::hardware_concurrency();
std::vector<std::future<void>> workers;
for (uint32_t i = 0; i < workerCount; i++) {
workers.emplace_back(std::async(std::launch::async, [&]() {
auto i = counter++;

while (i < int32_t(meshes.size())) {
auto& mesh = meshes[i];

if (!mesh.IsLoaded()) continue;

Loader::MeshLoader::SaveMesh(mesh.Get(), mesh.GetResource()->path, true);

i = counter++;
}
}));
}

for (uint32_t i = 0; i < workerCount; i++) {
workers[i].get();
}
}

for (const auto& mesh : meshes) {
if (!mesh.IsLoaded()) continue;

if (!multithreaded)
Loader::MeshLoader::SaveMesh(mesh.Get(), mesh.GetResource()->path, true);

for (const auto& material : mesh->data.materials)
materials[material.GetID()] = material;
}

for (const auto& [_, material] : materials) {
if (!material.IsLoaded()) continue;

Loader::MaterialLoader::SaveMaterial(material.Get(), material.GetResource()->path);
}

}

}
2 changes: 1 addition & 1 deletion src/engine/Serializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Atlas {
static Scene::Entity DeserializePrefab(Ref<Scene::Scene> scene, const std::string& filename);

private:

static void SaveDependencies(Ref<Scene::Scene> scene, bool multithreaded = true);

};

Expand Down
Loading

0 comments on commit d37a683

Please sign in to comment.