add project work

This commit is contained in:
2025-10-28 13:48:36 +08:00
parent 9e0a2963e8
commit 875a77ff5b
22 changed files with 498 additions and 80 deletions

View File

@ -14,14 +14,13 @@
#include <Hazel/Utils/PlatformUtils.h> #include <Hazel/Utils/PlatformUtils.h>
#include "imgui_internal.h" #include "imgui_internal.h"
#include "Hazel/Project/Project.h"
#include "Hazel/Scripting/ScriptEngine.h" #include "Hazel/Scripting/ScriptEngine.h"
namespace Hazel namespace Hazel
{ {
extern const std::filesystem::path g_AssetPath;
EditorLayer::EditorLayer() EditorLayer::EditorLayer()
: Layer("HazelEditor"), m_CameraController((float)Application::Get().GetWindow().GetWidth() / (float)Application::Get().GetWindow().GetHeight()) : Layer("HazelEditor"), m_CameraController((float)Application::Get().GetWindow().GetWidth() / (float)Application::Get().GetWindow().GetHeight())
{ {
@ -61,10 +60,12 @@ namespace Hazel
auto commandLineArgs = Application::Get().GetSpecification().commandLineArgs; auto commandLineArgs = Application::Get().GetSpecification().commandLineArgs;
if (commandLineArgs.count > 1) if (commandLineArgs.count > 1)
{ {
auto scenePath = commandLineArgs.args[1]; auto projectFilePath = commandLineArgs.args[1];
SceneSerializer sceneSerializer(m_ActiveScene); OpenProject(projectFilePath);
}else
sceneSerializer.Deserialize(scenePath); {
if (!OpenProject())
Application::Get().Close();
} }
} }
@ -275,10 +276,12 @@ namespace Hazel
// ImGui::MenuItem("Fullscreen", NULL, &opt_fullscreen); // ImGui::MenuItem("Fullscreen", NULL, &opt_fullscreen);
// ImGui::MenuItem("Padding", NULL, &opt_padding); // ImGui::MenuItem("Padding", NULL, &opt_padding);
// ImGui::Separator(); // ImGui::Separator();
if (ImGui::MenuItem("New", "Ctrl+N")) if (ImGui::MenuItem("Open Project", "Ctrl+O"))
OpenProject();
ImGui::Separator();
if (ImGui::MenuItem("New Scene", "Ctrl+N"))
NewScene(); NewScene();
if (ImGui::MenuItem("Open...", "Ctrl+O"))
OpenScene();
if (ImGui::MenuItem("Save", "Ctrl+S", false, m_ActiveScene ? true : false)) if (ImGui::MenuItem("Save", "Ctrl+S", false, m_ActiveScene ? true : false))
SaveScene(); SaveScene();
if (ImGui::MenuItem("Save As...", "Ctrl+Shift+S")) if (ImGui::MenuItem("Save As...", "Ctrl+Shift+S"))
@ -305,7 +308,7 @@ namespace Hazel
// Scene Hierachy Panel // Scene Hierachy Panel
m_SceneHierachyPanel.OnImGuiRender(); m_SceneHierachyPanel.OnImGuiRender();
m_ContentBroswerPanel.OnImGuiRender(); m_ContentBroswerPanel->OnImGuiRender();
// Render Status // Render Status
{ {
@ -391,7 +394,7 @@ namespace Hazel
if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("CONTENT_BROSWER_ITEM")) if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("CONTENT_BROSWER_ITEM"))
{ {
const wchar_t* path = (const wchar_t*)payload->Data; const wchar_t* path = (const wchar_t*)payload->Data;
OpenScene(std::filesystem::path(g_AssetPath) / path); OpenScene(path);
} }
ImGui::EndDragDropTarget(); ImGui::EndDragDropTarget();
} }
@ -543,6 +546,37 @@ namespace Hazel
} }
} }
void EditorLayer::NewProject()
{
Project::New();
}
bool EditorLayer::OpenProject()
{
std::string path = FileDiaglogs::OpenFile("Hazel Project (*.proj)\0*.proj\0");
if (path.empty())
return false;
OpenProject(path);
return true;
}
void EditorLayer::OpenProject(const std::filesystem::path& projectPath)
{
if (Project::Load(projectPath))
{
ScriptEngine::Init();
auto startScenePath = Project::GetAssetsFileSystemPath(Project::GetActive()->GetConfig().StartScene);
OpenScene(startScenePath);
m_ContentBroswerPanel = CreateScope<ContentBroswerPanel>();
}
}
void EditorLayer::SaveProject() const
{
}
void EditorLayer::SerializeScene(Ref<Scene> scene, const std::filesystem::path& scenePath) const void EditorLayer::SerializeScene(Ref<Scene> scene, const std::filesystem::path& scenePath) const
{ {
const SceneSerializer serializer(scene); const SceneSerializer serializer(scene);
@ -555,7 +589,12 @@ namespace Hazel
return; return;
Entity selectedEntity = m_SceneHierachyPanel.GetSelectedEntity(); Entity selectedEntity = m_SceneHierachyPanel.GetSelectedEntity();
if (selectedEntity) if (selectedEntity)
m_EditorScene->DuplicateEntity(selectedEntity); {
Entity newEntity = m_EditorScene->DuplicateEntity(selectedEntity);
m_SceneHierachyPanel.SetSelectedEntity(newEntity);
}
} }
void EditorLayer::OnScenePlay() void EditorLayer::OnScenePlay()
@ -649,7 +688,7 @@ namespace Hazel
else if (Input::IsKeyPressed(KeyCode::S)) else if (Input::IsKeyPressed(KeyCode::S))
SaveScene(); SaveScene();
else if (Input::IsKeyPressed(KeyCode::O)) else if (Input::IsKeyPressed(KeyCode::O))
OpenScene(); OpenProject();
else if (Input::IsKeyPressed(KeyCode::N)) else if (Input::IsKeyPressed(KeyCode::N))
NewScene(); NewScene();
else if (Input::IsKeyPressed(KeyCode::D)) else if (Input::IsKeyPressed(KeyCode::D))
@ -668,6 +707,19 @@ namespace Hazel
else if (Input::IsKeyPressed(KeyCode::R)) else if (Input::IsKeyPressed(KeyCode::R))
ChangeOptMode(ImGuizmo::OPERATION::ROTATE); ChangeOptMode(ImGuizmo::OPERATION::ROTATE);
} }
if (Input::IsKeyPressed(KeyCode::DELETE))
{
if (Application::Get().GetImGuiLayer()->GetActiveWidgetID() == 0)
{
Entity selectedEntity = m_SceneHierachyPanel.GetSelectedEntity();
if (selectedEntity)
{
m_SceneHierachyPanel.SetSelectedEntity({});
m_ActiveScene->DestroyEntity(selectedEntity);
}
}
}
} }
} }

View File

@ -38,6 +38,11 @@ namespace Hazel
void NewScene(); void NewScene();
void SaveScene() const; void SaveScene() const;
void NewProject();
bool OpenProject();
void OpenProject(const std::filesystem::path& projectPath);
void SaveProject() const;
void SerializeScene(Ref<Scene> scene, const std::filesystem::path& scenePath) const; void SerializeScene(Ref<Scene> scene, const std::filesystem::path& scenePath) const;
void OnDuplicateEntity(); void OnDuplicateEntity();
@ -70,8 +75,9 @@ namespace Hazel
Ref<FrameBuffer> m_FrameBuffer; Ref<FrameBuffer> m_FrameBuffer;
Ref<FrameBuffer> m_RuntimeFrameBuffer; Ref<FrameBuffer> m_RuntimeFrameBuffer;
// Panels
SceneHierachyPanel m_SceneHierachyPanel; SceneHierachyPanel m_SceneHierachyPanel;
ContentBroswerPanel m_ContentBroswerPanel; Scope<ContentBroswerPanel> m_ContentBroswerPanel;
int m_GizmoType = -1; int m_GizmoType = -1;
int m_GizmoMode = -1; int m_GizmoMode = -1;

View File

@ -8,13 +8,13 @@
#include <../../../../Hazel/vendor/imgui/imgui.h> #include <../../../../Hazel/vendor/imgui/imgui.h>
#include <string> #include <string>
#include "Hazel/Project/Project.h"
namespace Hazel namespace Hazel
{ {
extern const std::filesystem::path g_AssetPath = "assets";
ContentBroswerPanel::ContentBroswerPanel() ContentBroswerPanel::ContentBroswerPanel()
: m_CurrentDirectory(g_AssetPath) : m_BaseDirectory(Project::GetAssetsDirectory()), m_CurrentDirectory(m_BaseDirectory)
{ {
m_DirectoryIcon = Texture2D::Create("Resources/Icons/ContentBrowser/DirectoryIcon.png"); m_DirectoryIcon = Texture2D::Create("Resources/Icons/ContentBrowser/DirectoryIcon.png");
m_FileIcon = Texture2D::Create("Resources/Icons/ContentBrowser/FileIcon.png"); m_FileIcon = Texture2D::Create("Resources/Icons/ContentBrowser/FileIcon.png");
@ -28,7 +28,7 @@ namespace Hazel
ImGui::Text("%s", m_CurrentDirectory.string().c_str()); ImGui::Text("%s", m_CurrentDirectory.string().c_str());
ImGui::Separator(); ImGui::Separator();
if (m_CurrentDirectory != std::filesystem::path(g_AssetPath)) if (m_CurrentDirectory != std::filesystem::path(m_BaseDirectory))
{ {
if (ImGui::Button("..")) if (ImGui::Button(".."))
{ {
@ -60,7 +60,7 @@ namespace Hazel
if (ImGui::BeginDragDropSource()) if (ImGui::BeginDragDropSource())
{ {
auto relativePath = std::filesystem::relative(path, g_AssetPath); auto relativePath = std::filesystem::relative(path);
const wchar_t* itemPath = relativePath.c_str(); const wchar_t* itemPath = relativePath.c_str();
ImGui::SetDragDropPayload("CONTENT_BROSWER_ITEM", itemPath, (wcslen(itemPath) + 1) * sizeof(wchar_t), ImGuiCond_Once); ImGui::SetDragDropPayload("CONTENT_BROSWER_ITEM", itemPath, (wcslen(itemPath) + 1) * sizeof(wchar_t), ImGuiCond_Once);

View File

@ -19,6 +19,7 @@ namespace Hazel
void OnImGuiRender(); void OnImGuiRender();
private: private:
std::filesystem::path m_BaseDirectory;
std::filesystem::path m_CurrentDirectory; std::filesystem::path m_CurrentDirectory;
Ref<Texture2D> m_DirectoryIcon; Ref<Texture2D> m_DirectoryIcon;

View File

@ -9,13 +9,12 @@
#include <imgui_internal.h> #include <imgui_internal.h>
#include <glm/gtc/type_ptr.hpp> #include <glm/gtc/type_ptr.hpp>
#include <Hazel/Scene/Components.h> #include <Hazel/Scene/Components.h>
#include "Hazel/UI/UI.h"
#include "Hazel/Scripting/ScriptEngine.h" #include "Hazel/Scripting/ScriptEngine.h"
namespace Hazel namespace Hazel
{ {
extern const std::filesystem::path g_AssetPath;
SceneHierachyPanel::SceneHierachyPanel(const Ref<Scene>& context) SceneHierachyPanel::SceneHierachyPanel(const Ref<Scene>& context)
{ {
SetContext(context); SetContext(context);
@ -99,7 +98,7 @@ namespace Hazel
if (entityDeleted) if (entityDeleted)
{ {
m_Context->DestoryEntity(entity); m_Context->DestroyEntity(entity);
if (m_SelectionContext == entity) if (m_SelectionContext == entity)
{ {
m_SelectionContext = {}; m_SelectionContext = {};
@ -365,21 +364,15 @@ namespace Hazel
DrawComponent<ScriptComponent>("Script", entity, [entity, &scene = m_Context](auto& component) mutable DrawComponent<ScriptComponent>("Script", entity, [entity, &scene = m_Context](auto& component) mutable
{ {
bool scriptClassExists = ScriptEngine::ClassExists(component.ClassName); bool scriptClassExists = ScriptEngine::ClassExists(component.ClassName);
bool setStyleFlag = false;
static char buffer[64] = {}; static char buffer[64] = {};
strcpy_s(buffer, sizeof(buffer), component.ClassName.c_str()); strcpy_s(buffer, sizeof(buffer), component.ClassName.c_str());
if (!scriptClassExists) UI::ScopedStyleColor textColor(ImGuiCol_Text, ImVec4(0.9f, 0.2f, 0.3f, 1.0f), !scriptClassExists);
{
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(0.9f, 0.2f, 0.3f, 1.0f));
setStyleFlag = true;
}
if (ImGui::InputText("Class", buffer, sizeof(buffer))) if (ImGui::InputText("Class", buffer, sizeof(buffer)))
{ {
component.ClassName = buffer; component.ClassName = buffer;
scriptClassExists = ScriptEngine::ClassExists(component.ClassName); return;
} }
@ -447,10 +440,6 @@ namespace Hazel
} }
} }
if (setStyleFlag)
ImGui::PopStyleColor();
}); });
DrawComponent<SpriteRendererComponent>("Sprite Renderer", entity, [](auto& component) DrawComponent<SpriteRendererComponent>("Sprite Renderer", entity, [](auto& component)
@ -471,7 +460,7 @@ namespace Hazel
if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("CONTENT_BROSWER_ITEM")) if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("CONTENT_BROSWER_ITEM"))
{ {
const wchar_t* path = (const wchar_t*)payload->Data; const wchar_t* path = (const wchar_t*)payload->Data;
std::filesystem::path texturePath = std::filesystem::path(g_AssetPath) / path; const std::filesystem::path texturePath(path);
component.Texture = Texture2D::Create(texturePath.string()); component.Texture = Texture2D::Create(texturePath.string());
} }
ImGui::EndDragDropTarget(); ImGui::EndDragDropTarget();

View File

@ -32,6 +32,16 @@ namespace Hazel
[MethodImplAttribute(MethodImplOptions.InternalCall)] [MethodImplAttribute(MethodImplOptions.InternalCall)]
internal extern static void RigidBody2DComponent_ApplyLinearImpulseToCenter(ulong entityID, ref Vector2 impulse, bool wake); internal extern static void RigidBody2DComponent_ApplyLinearImpulseToCenter(ulong entityID, ref Vector2 impulse, bool wake);
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal extern static void RigidBody2DComponent_GetLinearVelocity(ulong entityID, out Vector2 LinearVelocity);
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal extern static RigidBody2DComponent.BodyType RigidBody2DComponent_GetType(ulong entityID);
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal extern static void RigidBody2DComponent_SetType(ulong entityID, RigidBody2DComponent.BodyType bodyType);
[MethodImplAttribute(MethodImplOptions.InternalCall)] [MethodImplAttribute(MethodImplOptions.InternalCall)]
internal extern static bool Input_IsKeyDown(KeyCode keycode); internal extern static bool Input_IsKeyDown(KeyCode keycode);
} }

View File

@ -21,6 +21,23 @@ namespace Hazel
public class RigidBody2DComponent : Component public class RigidBody2DComponent : Component
{ {
public enum BodyType { Static = 0, Dynamic, Kinematic}
public Vector2 LinearVelocity
{
get
{
InternalCalls.RigidBody2DComponent_GetLinearVelocity(Entity.ID, out Vector2 velocity);
return velocity;
}
}
public BodyType Type
{
get => InternalCalls.RigidBody2DComponent_GetType(Entity.ID);
set => InternalCalls.RigidBody2DComponent_SetType(Entity.ID, value);
}
public void ApplyLinearImpulse(Vector2 impulse, Vector2 point, bool wake) { public void ApplyLinearImpulse(Vector2 impulse, Vector2 point, bool wake) {
InternalCalls.RigidBody2DComponent_ApplyLinearImpulse(Entity.ID, ref impulse, ref point, wake); InternalCalls.RigidBody2DComponent_ApplyLinearImpulse(Entity.ID, ref impulse, ref point, wake);
} }

View File

@ -1,3 +1,4 @@
using System;
namespace Hazel namespace Hazel
{ {
@ -26,6 +27,16 @@ namespace Hazel
{ {
return new Vector2(a.X + b.X, a.Y + b.Y); return new Vector2(a.X + b.X, a.Y + b.Y);
} }
public float LengthSquared()
{
return X * X + Y * Y;
}
public float Length()
{
return (float)Math.Sqrt(LengthSquared());
}
} }
public struct Vector3 public struct Vector3

View File

@ -32,7 +32,6 @@ namespace Hazel {
m_Window->SetWindowResizeEventCallback(std::bind(&Application::OnWindowResize, this, std::placeholders::_1)); m_Window->SetWindowResizeEventCallback(std::bind(&Application::OnWindowResize, this, std::placeholders::_1));
Renderer::Init(); Renderer::Init();
ScriptEngine::Init();
m_imguiLayer = new ImGuiLayer(); m_imguiLayer = new ImGuiLayer();
PushOverlay(m_imguiLayer); PushOverlay(m_imguiLayer);

View File

@ -10,6 +10,7 @@
#include <Hazel/Core/Application.h> #include <Hazel/Core/Application.h>
#include <Hazel/Debug/Instrumentor.h> #include <Hazel/Debug/Instrumentor.h>
#include "ImGuizmo.h" #include "ImGuizmo.h"
#include "imgui_internal.h"
namespace Hazel namespace Hazel
{ {
@ -111,6 +112,11 @@ namespace Hazel
} }
} }
uint32_t ImGuiLayer::GetActiveWidgetID() const
{
return GImGui->ActiveId;
}
void ImGuiLayer::SetDarkThemeColors() void ImGuiLayer::SetDarkThemeColors()
{ {
auto& colors = ImGui::GetStyle().Colors; auto& colors = ImGui::GetStyle().Colors;

View File

@ -24,6 +24,8 @@ namespace Hazel {
void BlockEvents(const bool block) {m_BlockEvent = block;} void BlockEvents(const bool block) {m_BlockEvent = block;}
uint32_t GetActiveWidgetID() const;
private: private:
void SetDarkThemeColors(); void SetDarkThemeColors();

View File

@ -0,0 +1,41 @@
//
// Created by sfd on 25-10-27.
//
#ifndef PHYSICS2D_H
#define PHYSICS2D_H
#include "box2d/types.h"
#include "Hazel/Core/Log.h"
#include "Hazel/Scene/Components.h"
namespace Hazel::Utils
{
inline b2BodyType RigidBodyTypeToBox2DBodyType(const RigidBody2DComponent::BodyType type)
{
switch (type)
{
case RigidBody2DComponent::BodyType::Static: return b2_staticBody;
case RigidBody2DComponent::BodyType::Dynamic: return b2_dynamicBody;
case RigidBody2DComponent::BodyType::Kinematic: return b2_kinematicBody;
}
HZ_CORE_ERROR("Unknown body type");
return b2_staticBody;
}
inline RigidBody2DComponent::BodyType Rigidbody2DTypeFromBox2DBody(const b2BodyType bodyType)
{
switch (bodyType)
{
case b2_staticBody: return RigidBody2DComponent::BodyType::Static;
case b2_dynamicBody: return RigidBody2DComponent::BodyType::Dynamic;
case b2_kinematicBody: return RigidBody2DComponent::BodyType::Kinematic;
}
HZ_CORE_ASSERT(false, "Unknown body type");
return RigidBody2DComponent::BodyType::Static;
}
}
#endif //PHYSICS2D_H

View File

@ -0,0 +1,45 @@
//
// Created by sfd on 25-10-27.
//
#include "Project.h"
#include "ProjectSerializer.h"
namespace Hazel
{
Ref<Project> Project::New()
{
s_ActiveProject = CreateRef<Project>();
return s_ActiveProject;
}
Ref<Project> Project::Load(const std::filesystem::path& path)
{
Ref<Project> project = CreateRef<Project>();
ProjectSerializer serializer(project);
if (serializer.Deserialize(path))
{
project->m_ProjectDirectory = path;
s_ActiveProject = project;
return project;
}
return nullptr;
}
bool Project::SaveActive(const std::filesystem::path& path)
{
ProjectSerializer serializer(s_ActiveProject);
if (serializer.Serialize(path))
{
s_ActiveProject->m_ProjectDirectory = path.parent_path();
return true;
}
return false;
}
}

View File

@ -0,0 +1,65 @@
//
// Created by sfd on 25-10-27.
//
#ifndef PROJECT_H
#define PROJECT_H
#include <filesystem>
#include <string>
#include "Hazel/Core/assert.h"
#include "Hazel/Core/Core.h"
namespace Hazel
{
struct ProjectConfig
{
std::string Name = "Untitled";
std::filesystem::path StartScene;
std::filesystem::path AssetsDirectory;
std::filesystem::path ScriptModulePath;
};
class HAZEL_API Project {
public:
static const std::filesystem::path& GetProjectDirectory()
{
HZ_CORE_ASSERT(s_ActiveProject);
return s_ActiveProject->m_ProjectDirectory;
}
static std::filesystem::path GetAssetsDirectory()
{
HZ_CORE_ASSERT(s_ActiveProject);
return GetProjectDirectory() / s_ActiveProject->m_Config.AssetsDirectory;
}
static std::filesystem::path GetAssetsFileSystemPath(const std::filesystem::path& path)
{
HZ_CORE_ASSERT(s_ActiveProject);
return GetAssetsDirectory() / path;
}
ProjectConfig& GetConfig() { return m_Config; }
static Ref<Project> GetActive() { return s_ActiveProject; }
static Ref<Project> New();
static Ref<Project> Load(const std::filesystem::path& path);
static bool SaveActive(const std::filesystem::path& path);
private:
ProjectConfig m_Config;
std::filesystem::path m_ProjectDirectory;
inline static Ref<Project> s_ActiveProject;
};
}
#endif //PROJECT_H

View File

@ -0,0 +1,71 @@
//
// Created by sfd on 25-10-27.
//
#include "ProjectSerializer.h"
#include <fstream>
#include "Project.h"
#include "yaml-cpp/yaml.h"
namespace Hazel
{
ProjectSerializer::ProjectSerializer(const Ref<Project>& project)
: m_Project(project)
{
}
bool ProjectSerializer::Serialize(const std::filesystem::path& path) const
{
const auto& config = m_Project->GetConfig();
YAML::Emitter out;
{
out << YAML::BeginMap; // Root
out << YAML::Key << "Project" << YAML::Value;
{
out << YAML::BeginMap; // Project
out << YAML::Key << "Name" << YAML::Value << config.Name;
out << YAML::Key << "StartScene" << YAML::Value << config.StartScene.string();
out << YAML::Key << "AssetsDirectory" << YAML::Value << config.AssetsDirectory.string();
out << YAML::Key << "ScriptModulePath" << YAML::Value << config.ScriptModulePath.string();
out << YAML::EndMap;
}
}
std::ofstream fout(path);
fout << out.c_str();
return true;
}
bool ProjectSerializer::Deserialize(const std::filesystem::path& path) const
{
auto& config = m_Project->GetConfig();
YAML::Node data;
try
{
data = YAML::LoadFile(path.string());
}catch (YAML::Exception& e)
{
HZ_CORE_ERROR("failed to load project file: {0}\n {1}", path.string(),e.what());
return false;
}
auto projectNode = data["Project"];
if (!projectNode)
return false;
config.Name = projectNode["Name"].as<std::string>();
config.StartScene = projectNode["StartScene"].as<std::string>();
config.AssetsDirectory = projectNode["AssetsDirectory"].as<std::string>();
config.ScriptModulePath = projectNode["ScriptModulePath"].as<std::string>();
return true;
}
}

View File

@ -0,0 +1,30 @@
//
// Created by sfd on 25-10-27.
//
#ifndef PROJECTSERIALIZER_H
#define PROJECTSERIALIZER_H
#include <filesystem>
#include "Hazel/Core/Core.h"
namespace Hazel
{
class Project;
class HAZEL_API ProjectSerializer {
public:
ProjectSerializer(const Ref<Project>& project);
bool Serialize(const std::filesystem::path& path) const;
bool Deserialize(const std::filesystem::path& path) const;
private:
Ref<Project> m_Project;
};
}
#endif //PROJECTSERIALIZER_H

View File

@ -15,23 +15,12 @@
#include "box2d/box2d.h" #include "box2d/box2d.h"
#include "box2d/id.h" #include "box2d/id.h"
#include "Hazel/Core/assert.h" #include "Hazel/Core/assert.h"
#include "Hazel/Physics/Physics2D.h"
#include "Hazel/Scripting/ScriptEngine.h" #include "Hazel/Scripting/ScriptEngine.h"
namespace Hazel namespace Hazel
{ {
static b2BodyType RigidBodyTypeToBox2DBodyType(RigidBody2DComponent::BodyType type)
{
switch (type)
{
case RigidBody2DComponent::BodyType::Static: return b2_staticBody;
case RigidBody2DComponent::BodyType::Dynamic: return b2_dynamicBody;
case RigidBody2DComponent::BodyType::Kinematic: return b2_kinematicBody;
}
HZ_CORE_ERROR("Unknown body type");
return b2_staticBody;
}
Scene::Scene() Scene::Scene()
{ {
@ -105,16 +94,7 @@ namespace Hazel
// Copy all components except IDComponent and TagComponent // Copy all components except IDComponent and TagComponent
CopyComponent(AllComponents{}, dstSceneRegistry, srcSceneRegistry, enttMap); CopyComponent(AllComponents{}, dstSceneRegistry, srcSceneRegistry, enttMap);
/*
CopyComponent<TransformComponent>(dstSceneRegistry, srcSceneRegistry, enttMap);
CopyComponent<SpriteRendererComponent>(dstSceneRegistry, srcSceneRegistry, enttMap);
CopyComponent<CircleRendererComponent>(dstSceneRegistry, srcSceneRegistry, enttMap);
CopyComponent<CameraComponent>(dstSceneRegistry, srcSceneRegistry, enttMap);
CopyComponent<NativeScriptComponent>(dstSceneRegistry, srcSceneRegistry, enttMap);
CopyComponent<RigidBody2DComponent>(dstSceneRegistry, srcSceneRegistry, enttMap);
CopyComponent<BoxCollider2DComponent>(dstSceneRegistry, srcSceneRegistry, enttMap);
CopyComponent<CircleCollider2DComponent>(dstSceneRegistry, srcSceneRegistry, enttMap);
*/
return newScene; return newScene;
} }
@ -136,10 +116,10 @@ namespace Hazel
return entity; return entity;
} }
void Scene::DestoryEntity(const Entity entity) void Scene::DestroyEntity(Entity entity)
{ {
m_EnttMap.erase(entity.GetUUID());
m_Registry.destroy(entity); m_Registry.destroy(entity);
m_EnttMap.erase((entt::id_type)entity);
} }
@ -162,7 +142,7 @@ namespace Hazel
auto& rb2D = entity.GetComponent<RigidBody2DComponent>(); auto& rb2D = entity.GetComponent<RigidBody2DComponent>();
b2BodyDef bodyDef = b2DefaultBodyDef(); b2BodyDef bodyDef = b2DefaultBodyDef();
bodyDef.type = RigidBodyTypeToBox2DBodyType(rb2D.Type); bodyDef.type = Utils::RigidBodyTypeToBox2DBodyType(rb2D.Type);
bodyDef.position = {transform.Translation.x, transform.Translation.y}; bodyDef.position = {transform.Translation.x, transform.Translation.y};
float angle = transform.Rotation.z; float angle = transform.Rotation.z;
bodyDef.rotation = b2Rot{cos(angle), sin(angle)}; bodyDef.rotation = b2Rot{cos(angle), sin(angle)};
@ -443,21 +423,13 @@ namespace Hazel
} }
} }
void Scene::DuplicateEntity(Entity entity) Entity Scene::DuplicateEntity(Entity entity)
{ {
Entity newEntity = CreateEntity(entity.GetName() + " copy"); Entity newEntity = CreateEntity(entity.GetName() + "_copy");
CopyComponentIfExists(AllComponents{}, newEntity, entity); CopyComponentIfExists(AllComponents{}, newEntity, entity);
/*
CopyComponentIfExists<TransformComponent>(newEntity, entity); return newEntity;
CopyComponentIfExists<SpriteRendererComponent>(newEntity, entity);
CopyComponentIfExists<CircleRendererComponent>(newEntity, entity);
CopyComponentIfExists<CameraComponent>(newEntity, entity);
CopyComponentIfExists<NativeScriptComponent>(newEntity, entity);
CopyComponentIfExists<RigidBody2DComponent>(newEntity, entity);
CopyComponentIfExists<BoxCollider2DComponent>(newEntity, entity);
CopyComponentIfExists<CircleCollider2DComponent>(newEntity, entity);
*/
} }
Entity Scene::FindEntityByName(std::string_view name) Entity Scene::FindEntityByName(std::string_view name)

View File

@ -29,7 +29,7 @@ namespace Hazel
Entity CreateEntity(const std::string& name = std::string()); Entity CreateEntity(const std::string& name = std::string());
Entity CreateEntityWithUUID(UUID uuid, const std::string& name = std::string()); Entity CreateEntityWithUUID(UUID uuid, const std::string& name = std::string());
void DestoryEntity(Entity entity); void DestroyEntity(Entity entity);
void OnRuntimeStart(); void OnRuntimeStart();
void OnRuntimeStop(); void OnRuntimeStop();
@ -42,7 +42,7 @@ namespace Hazel
void OnUpdateEditor(TimeStep& ts, const EditorCamera& camera); void OnUpdateEditor(TimeStep& ts, const EditorCamera& camera);
void OnViewportResize(uint32_t width, uint32_t height); void OnViewportResize(uint32_t width, uint32_t height);
void DuplicateEntity(Entity entity); Entity DuplicateEntity(Entity entity);
Entity FindEntityByName(std::string_view name); Entity FindEntityByName(std::string_view name);
Entity GetPrimaryCameraEntity(); Entity GetPrimaryCameraEntity();

View File

@ -8,6 +8,7 @@
#include "Components.h" #include "Components.h"
#include "Entity.h" #include "Entity.h"
#include "Hazel/Project/Project.h"
#include "Hazel/Scripting/ScriptEngine.h" #include "Hazel/Scripting/ScriptEngine.h"
#include "yaml-cpp/yaml.h" #include "yaml-cpp/yaml.h"
@ -541,7 +542,12 @@ namespace Hazel
sprite.Color = spriteRendererComponent["Color"].as<glm::vec4>(); sprite.Color = spriteRendererComponent["Color"].as<glm::vec4>();
sprite.TilingFactor = spriteRendererComponent["TilingFactor"].as<float>(); sprite.TilingFactor = spriteRendererComponent["TilingFactor"].as<float>();
if (spriteRendererComponent["TexturePath"]) if (spriteRendererComponent["TexturePath"])
sprite.Texture = Texture2D::Create(spriteRendererComponent["TexturePath"].as<std::string>()); {
std::string texturePath = spriteRendererComponent["TexturePath"].as<std::string>();
auto path = Project::GetAssetsFileSystemPath(texturePath);
sprite.Texture = Texture2D::Create(path.string());
// sprite.Texture = Texture2D::Create(spriteRendererComponent["TexturePath"].as<std::string>());
}
} }
auto circleRendererComponent = entity["CircleRendererComponent"]; auto circleRendererComponent = entity["CircleRendererComponent"];

View File

@ -12,6 +12,7 @@
#include "Hazel/Core/Application.h" #include "Hazel/Core/Application.h"
#include "Hazel/Core/Buffer.h" #include "Hazel/Core/Buffer.h"
#include "Hazel/Core/FileSystem.h" #include "Hazel/Core/FileSystem.h"
#include "Hazel/Project/Project.h"
#include "mono/jit/jit.h" #include "mono/jit/jit.h"
#include "mono/metadata/assembly.h" #include "mono/metadata/assembly.h"
#include "mono/metadata/attrdefs.h" #include "mono/metadata/attrdefs.h"
@ -170,7 +171,12 @@ namespace Hazel
bool AssemblyReloading = false; bool AssemblyReloading = false;
// Mono C# debug // Mono C# debug
#ifdef HZ_DEBUG
bool EnableDebugging = true; bool EnableDebugging = true;
#else
bool EnableDebugging = false;
#endif
// runtime // runtime
Scene* SceneContext = nullptr; Scene* SceneContext = nullptr;
@ -204,7 +210,8 @@ namespace Hazel
return; return;
} }
status = LoadAppAssemble("Resources/Scripts/Sandbox.dll"); const auto modulePath = Project::GetAssetsDirectory() / Project::GetActive()->GetConfig().ScriptModulePath;
status = LoadAppAssemble(modulePath);
if (!status) if (!status)
{ {
HZ_CORE_ERROR("[ScriptEngine] Could not load app assembly."); HZ_CORE_ERROR("[ScriptEngine] Could not load app assembly.");
@ -498,7 +505,11 @@ namespace Hazel
MonoObject* ScriptEngine::GetManagedInstance(const UUID uuid) MonoObject* ScriptEngine::GetManagedInstance(const UUID uuid)
{ {
HZ_CORE_ASSERT(s_Data->EntityInstances.contains(uuid),"could not find entity"); if (!s_Data->EntityInstances.contains(uuid))
{
HZ_CORE_ERROR("could not find entity : {0}", (uint64_t)uuid);
return nullptr;
}
return s_Data->EntityInstances.at(uuid)->GetManagedObject(); return s_Data->EntityInstances.at(uuid)->GetManagedObject();
} }

View File

@ -12,6 +12,7 @@
#include "Hazel/Core/assert.h" #include "Hazel/Core/assert.h"
#include "mono/jit/jit.h" #include "mono/jit/jit.h"
#include "box2d/box2d.h" #include "box2d/box2d.h"
#include "Hazel/Physics/Physics2D.h"
namespace Hazel namespace Hazel
{ {
@ -123,6 +124,41 @@ namespace Hazel
b2Body_ApplyLinearImpulseToCenter(rb2d.RuntimeBody, b2Vec2(impules->x, impules->y), wake); b2Body_ApplyLinearImpulseToCenter(rb2d.RuntimeBody, b2Vec2(impules->x, impules->y), wake);
} }
static void RigidBody2DComponent_GetLinearVelocity(const UUID entityID, glm::vec2* outLinearVelocity)
{
Scene* scene = ScriptEngine::GetSceneContext();
HZ_CORE_ASSERT(scene, "sceneContext is Not exist!");
Entity entity = scene->GetEntityByUUID(entityID);
HZ_CORE_ASSERT(entity, "entity is Not exist!");
const auto& rb2d = entity.GetComponent<RigidBody2DComponent>();
auto [x, y] = b2Body_GetLinearVelocity(rb2d.RuntimeBody);
*outLinearVelocity = {x,y};
}
static RigidBody2DComponent::BodyType RigidBody2DComponent_GetType(const UUID entityID)
{
Scene* scene = ScriptEngine::GetSceneContext();
HZ_CORE_ASSERT(scene, "sceneContext is Not exist!");
Entity entity = scene->GetEntityByUUID(entityID);
HZ_CORE_ASSERT(entity, "entity is Not exist!");
const auto& rb2d = entity.GetComponent<RigidBody2DComponent>();
return Utils::Rigidbody2DTypeFromBox2DBody(b2Body_GetType(rb2d.RuntimeBody));
}
static void RigidBody2DComponent_SetType(const UUID entityID, RigidBody2DComponent::BodyType type)
{
Scene* scene = ScriptEngine::GetSceneContext();
HZ_CORE_ASSERT(scene, "sceneContext is Not exist!");
Entity entity = scene->GetEntityByUUID(entityID);
HZ_CORE_ASSERT(entity, "entity is Not exist!");
const auto& rb2d = entity.GetComponent<RigidBody2DComponent>();
b2Body_SetType(rb2d.RuntimeBody, Utils::RigidBodyTypeToBox2DBodyType(type));
}
static bool Input_IsKeyDown(const KeyCode keycode) static bool Input_IsKeyDown(const KeyCode keycode)
{ {
@ -181,6 +217,11 @@ namespace Hazel
HZ_ADD_INTERNAL_CALL(RigidBody2DComponent_ApplyLinearImpulse); HZ_ADD_INTERNAL_CALL(RigidBody2DComponent_ApplyLinearImpulse);
HZ_ADD_INTERNAL_CALL(RigidBody2DComponent_ApplyLinearImpulseToCenter); HZ_ADD_INTERNAL_CALL(RigidBody2DComponent_ApplyLinearImpulseToCenter);
HZ_ADD_INTERNAL_CALL(RigidBody2DComponent_GetLinearVelocity);
HZ_ADD_INTERNAL_CALL(RigidBody2DComponent_GetType);
HZ_ADD_INTERNAL_CALL(RigidBody2DComponent_SetType);
HZ_ADD_INTERNAL_CALL(Input_IsKeyDown); HZ_ADD_INTERNAL_CALL(Input_IsKeyDown);
} }
} }

43
Hazel/src/Hazel/UI/UI.h Normal file
View File

@ -0,0 +1,43 @@
//
// Created by sfd on 25-10-27.
//
#ifndef UI_H
#define UI_H
#include "imgui.h"
namespace Hazel::UI
{
struct ScopedStyleColor
{
ScopedStyleColor() = default;
ScopedStyleColor(const ImGuiCol idx, const ImVec4 color, const bool predicate = true)
: m_Set(predicate)
{
if (predicate)
ImGui::PushStyleColor(idx, color);
}
ScopedStyleColor(const ImGuiCol idx, const ImU32 color, const bool predicate = true)
: m_Set(predicate)
{
if (predicate)
ImGui::PushStyleColor(idx, color);
}
~ScopedStyleColor()
{
if (m_Set)
ImGui::PopStyleColor();
}
private:
bool m_Set = false;
};
}
#endif //UI_H