Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Hazel/src/Hazel/Core/Window.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ namespace Hazel {
virtual void SetVSync(bool enabled) = 0;
virtual bool IsVSync() const = 0;

virtual void SetTitle(const std::string& title) = 0;

virtual void* GetNativeWindow() const = 0;

static Scope<Window> Create(const WindowProps& props = WindowProps());
Expand Down
5 changes: 5 additions & 0 deletions Hazel/src/Hazel/Scene/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ namespace Hazel {
return {};
}

void Scene::SetName(const std::string& name)
{
m_Name = name;
}

template<typename T>
void Scene::OnComponentAdded(Entity entity, T& component)
{
Expand Down
5 changes: 5 additions & 0 deletions Hazel/src/Hazel/Scene/Scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,18 @@ namespace Hazel {
void OnViewportResize(uint32_t width, uint32_t height);

Entity GetPrimaryCameraEntity();

void SetName(const std::string& name);
const std::string& GetName() const { return m_Name; }
private:
template<typename T>
void OnComponentAdded(Entity entity, T& component);
private:
entt::registry m_Registry;
uint32_t m_ViewportWidth = 0, m_ViewportHeight = 0;

std::string m_Name = "Untitled";

friend class Entity;
friend class SceneSerializer;
friend class SceneHierarchyPanel;
Expand Down
3 changes: 2 additions & 1 deletion Hazel/src/Hazel/Scene/SceneSerializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ namespace Hazel {
{
YAML::Emitter out;
out << YAML::BeginMap;
out << YAML::Key << "Scene" << YAML::Value << "Untitled";
out << YAML::Key << "Scene" << YAML::Value << m_Scene->GetName();
out << YAML::Key << "Entities" << YAML::Value << YAML::BeginSeq;
m_Scene->m_Registry.each([&](auto entityID)
{
Expand Down Expand Up @@ -196,6 +196,7 @@ namespace Hazel {

std::string sceneName = data["Scene"].as<std::string>();
HZ_CORE_TRACE("Deserializing scene '{0}'", sceneName);
m_Scene->SetName(sceneName);

auto entities = data["Entities"];
if (entities)
Expand Down
5 changes: 5 additions & 0 deletions Hazel/src/Platform/Windows/WindowsWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,9 @@ namespace Hazel {
return m_Data.VSync;
}

void WindowsWindow::SetTitle(const std::string& title)
{
m_Data.Title = title;
glfwSetWindowTitle(m_Window, title.c_str());
}
}
4 changes: 3 additions & 1 deletion Hazel/src/Platform/Windows/WindowsWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ namespace Hazel {
void SetVSync(bool enabled) override;
bool IsVSync() const override;

void SetTitle(const std::string& title) override;

virtual void* GetNativeWindow() const { return m_Window; }
private:
virtual void Init(const WindowProps& props);
Expand All @@ -43,4 +45,4 @@ namespace Hazel {
WindowData m_Data;
};

}
}
2 changes: 1 addition & 1 deletion Hazelnut/assets/scenes/3DExample.hazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Scene: Untitled
Scene: 3DExample
Entities:
- Entity: 12837192831273
TagComponent:
Expand Down
2 changes: 1 addition & 1 deletion Hazelnut/assets/scenes/Example.hazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Scene: Untitled
Scene: Example
Entities:
- Entity: 12837192831273
TagComponent:
Expand Down
2 changes: 1 addition & 1 deletion Hazelnut/assets/scenes/PinkCube.hazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Scene: Untitled
Scene: PinkCube
Entities:
- Entity: 12837192831273
TagComponent:
Expand Down
68 changes: 53 additions & 15 deletions Hazelnut/src/EditorLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,25 @@ namespace Hazel {
fbSpec.Height = 720;
m_Framebuffer = Framebuffer::Create(fbSpec);

m_ActiveScene = CreateRef<Scene>();
// When setting the active scene later in this method, we try to resize its viewport
// If viewport size is still 0, we'll assert
// So set it to some valid value now
// Then later in OnUpdate()/OnImGuiRender() fix it to the correct value
m_ViewportSize = { fbSpec.Width, fbSpec.Height };

// Try to load a scene from command line args
// If it fails (no args, invalid scene, etc.) then fallback to default new untitled scene
bool loadedScene = false;

auto commandLineArgs = Application::Get().GetCommandLineArgs();
if (commandLineArgs.Count > 1)
{
auto sceneFilePath = commandLineArgs[1];
SceneSerializer serializer(m_ActiveScene);
serializer.Deserialize(sceneFilePath);
loadedScene = OpenScene(std::filesystem::path(commandLineArgs[1]));
}

if (!loadedScene)
{
NewScene();
}

m_EditorCamera = EditorCamera(30.0f, 1.778f, 0.1f, 1000.0f);
Expand Down Expand Up @@ -97,8 +108,6 @@ namespace Hazel {
m_CameraEntity.AddComponent<NativeScriptComponent>().Bind<CameraController>();
m_SecondCamera.AddComponent<NativeScriptComponent>().Bind<CameraController>();
#endif

m_SceneHierarchyPanel.SetContext(m_ActiveScene);
}

void EditorLayer::OnDetach()
Expand Down Expand Up @@ -460,9 +469,9 @@ namespace Hazel {

void EditorLayer::NewScene()
{
m_ActiveScene = CreateRef<Scene>();
m_ActiveScene->OnViewportResize((uint32_t)m_ViewportSize.x, (uint32_t)m_ViewportSize.y);
m_SceneHierarchyPanel.SetContext(m_ActiveScene);
Ref<Scene> newScene = CreateRef<Scene>();
SetActiveScene(newScene);
SetEditorScenePath(std::filesystem::path());
}

void EditorLayer::OpenScene()
Expand All @@ -472,22 +481,24 @@ namespace Hazel {
OpenScene(filepath);
}

void EditorLayer::OpenScene(const std::filesystem::path& path)
bool EditorLayer::OpenScene(const std::filesystem::path& path)
{
if (path.extension().string() != ".hazel")
{
HZ_WARN("Could not load {0} - not a scene file", path.filename().string());
return;
return false;
}

Ref<Scene> newScene = CreateRef<Scene>();
SceneSerializer serializer(newScene);
if (serializer.Deserialize(path.string()))
if (!serializer.Deserialize(path.string()))
{
m_ActiveScene = newScene;
m_ActiveScene->OnViewportResize((uint32_t)m_ViewportSize.x, (uint32_t)m_ViewportSize.y);
m_SceneHierarchyPanel.SetContext(m_ActiveScene);
return false;
}

SetActiveScene(newScene);
SetEditorScenePath(path);
return true;
}

void EditorLayer::SaveSceneAs()
Expand All @@ -497,6 +508,7 @@ namespace Hazel {
{
SceneSerializer serializer(m_ActiveScene);
serializer.Serialize(filepath);
SetEditorScenePath(std::filesystem::path(filepath));
}
}

Expand All @@ -510,5 +522,31 @@ namespace Hazel {
m_SceneState = SceneState::Edit;

}

void EditorLayer::SetActiveScene(const Ref<Scene>& activeScene)
{
HZ_ASSERT(activeScene, "EditorLayer ActiveScene cannot be null");

m_ActiveScene = activeScene;
m_ActiveScene->OnViewportResize((uint32_t)m_ViewportSize.x, (uint32_t)m_ViewportSize.y);

m_SceneHierarchyPanel.SetContext(m_ActiveScene);

SyncWindowTitle();
}

void EditorLayer::SetEditorScenePath(const std::filesystem::path& path)
{
m_EditorScenePath = path;
SyncWindowTitle();
}

void EditorLayer::SyncWindowTitle()
{
std::string title = "Hazelnut";
title += " - " + m_ActiveScene->GetName();
title += " (" + (m_EditorScenePath.empty() ? "unsaved" : m_EditorScenePath.string()) + ")";

Application::Get().GetWindow().SetTitle(title);
}
}
8 changes: 7 additions & 1 deletion Hazelnut/src/EditorLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@ namespace Hazel {

void NewScene();
void OpenScene();
void OpenScene(const std::filesystem::path& path);
bool OpenScene(const std::filesystem::path& path);
void SaveSceneAs();

void OnScenePlay();
void OnSceneStop();

void SetActiveScene(const Ref<Scene>& activeScene);
void SetEditorScenePath(const std::filesystem::path& path);
void SyncWindowTitle();

// UI Panels
void UI_Toolbar();
private:
Expand All @@ -43,6 +47,8 @@ namespace Hazel {
Ref<Framebuffer> m_Framebuffer;

Ref<Scene> m_ActiveScene;
std::filesystem::path m_EditorScenePath;

Entity m_SquareEntity;
Entity m_CameraEntity;
Entity m_SecondCamera;
Expand Down