添加box2d库

This commit is contained in:
2025-06-18 16:43:05 +08:00
parent 91af2392ed
commit 1f9b53609e
11 changed files with 424 additions and 28 deletions

View File

@ -38,6 +38,9 @@ namespace Hazel
m_LogoTexture = Texture2D::Create("assets/textures/iceLogo.png");
m_CheckerBoardTexture = Texture2D::Create("assets/textures/Checkerboard.png");
m_PlayIcon = Texture2D::Create("Resources/Icons/PlayButton.png");
m_StopIcon = Texture2D::Create("Resources/Icons/PauseButton.png");
m_ActiveScene = CreateRef<Scene>();
m_EditorCamera = EditorCamera(45.0f, 1.667f, 0.1f, 1000.0f);
@ -66,11 +69,6 @@ namespace Hazel
}
// update camera
if (m_ViewportFocused)
{
m_CameraController.OnUpdate(ts);
m_EditorCamera.OnUpdate(ts);
}
// update Renderer
m_FrameBuffer->Bind();
@ -79,10 +77,21 @@ namespace Hazel
m_FrameBuffer->ClearAttachment(1, -1);
// Renderer2D::BeginScene(m_CameraController.GetCamera());
switch (m_SceneState)
{
case SceneState::Play:
m_ActiveScene->OnUpdateRuntime(ts);
break;
case SceneState::Edit:
if (m_ViewportFocused)
m_CameraController.OnUpdate(ts);
// update Scene
m_ActiveScene->OnUpdateEditor(ts, m_EditorCamera);
m_EditorCamera.OnUpdate(ts);
m_ActiveScene->OnUpdateEditor(ts, m_EditorCamera);
break;
}
// Renderer2D::BeginScene(m_CameraController.GetCamera());
auto [mx, my] = ImGui::GetMousePos();
mx -= m_ViewPortBounds[0].x;
@ -275,17 +284,27 @@ namespace Hazel
ImGuizmo::SetRect(windowPos.x, windowPos.y, windowWidth, windowHeight);
// auto cameraEntity = m_ActiveScene->GetPrimaryCameraEntity();
if (m_GizmoType != -1)
{
// const auto& camera = cameraEntity.GetComponent<CameraComponent>().Camera;
glm::mat4 cameraProjection;
glm::mat4 cameraView;
if (m_SceneState == SceneState::Play)
{
auto cameraEntity = m_ActiveScene->GetPrimaryCameraEntity();
const auto& camera = cameraEntity.GetComponent<CameraComponent>().Camera;
cameraProjection = camera.GetProjection();
cameraView = glm::inverse(cameraEntity.GetComponent<TransformComponent>().GetTransform());
}else if (m_SceneState == SceneState::Edit)
{
cameraProjection = m_EditorCamera.GetProjection();
cameraView = m_EditorCamera.GetViewMatrix();
}
auto& tc = selectedEntity.GetComponent<TransformComponent>();
// const glm::mat4& cameraProjection = camera.GetProjection();
// glm::mat4 cameraView = glm::inverse(cameraEntity.GetComponent<TransformComponent>().GetTransform());
glm::mat4 cameraProjection = m_EditorCamera.GetProjection();
glm::mat4 cameraView = m_EditorCamera.GetViewMatrix();
glm::mat4 transform = tc.GetTransform();
bool snap = SDL_GetModState() & SDL_KMOD_CTRL;
float snapValue = 0.5f;
@ -298,7 +317,7 @@ namespace Hazel
float snapValues[3] = {snapValue, snapValue, snapValue};
if (ImGuizmo::Manipulate(glm::value_ptr(cameraView), glm::value_ptr(cameraProjection),
ImGuizmo::OPERATION(m_GizmoType), ImGuizmo::LOCAL,
static_cast<ImGuizmo::OPERATION>(m_GizmoType), ImGuizmo::WORLD,
glm::value_ptr(transform), nullptr, snap ? snapValues : nullptr) && !Input::IsKeyPressed(SDL_SCANCODE_LALT))
{
if (ImGuizmo::IsUsing())
@ -321,6 +340,9 @@ namespace Hazel
ImGui::End();
ImGui::PopStyleVar();
}
UI_ToolBar();
ImGui::End();
}
}
@ -417,4 +439,51 @@ namespace Hazel
break;
}
}
void EditorLayer::UI_ToolBar()
{
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 2));
ImGui::PushStyleVar(ImGuiStyleVar_ItemInnerSpacing, ImVec2(0, 0));
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0, 0, 0, 0));
auto& colors = ImGui::GetStyle().Colors;
auto& buttonHovered = colors[ImGuiCol_ButtonHovered];
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(buttonHovered.x, buttonHovered.y, buttonHovered.z, 0.5f));
auto& buttonActive = colors[ImGuiCol_ButtonActive];
ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(buttonActive.x, buttonActive.y, buttonActive.z, 0.5f));
ImGui::Begin("##ToolBar", nullptr, ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse);
// ImGui::Begin("##ToolBar", nullptr);
float size = ImGui::GetWindowHeight() - 10.0f;
Ref<Texture2D> icon = m_SceneState == SceneState::Edit ? m_PlayIcon : m_StopIcon;
ImGui::SetCursorPosX(ImGui::GetWindowContentRegionMax().x * 0.5f - size * 0.5f);
ImGui::SetCursorPosX(ImGui::GetWindowContentRegionMax().x * 0.5f - (size * 0.5f));
if (ImGui::ImageButton("toolbar", icon->GetRendererID(), ImVec2{size, size}, ImVec2{0, 0}, ImVec2{1, 1}))
{
if (m_SceneState == SceneState::Edit)
OnScenePlay();
else if (m_SceneState == SceneState::Play)
OnSceneStop();
}
ImGui::PopStyleVar(2);
ImGui::PopStyleColor(3);
ImGui::End();
}
void EditorLayer::OnScenePlay()
{
m_SceneState = SceneState::Play;
m_ActiveScene->OnRuntimeStart();
}
void EditorLayer::OnSceneStop()
{
m_SceneState = SceneState::Edit;
m_ActiveScene->OnRuntimeStop();
}
}

View File

@ -24,18 +24,25 @@ namespace Hazel
virtual void OnImGuiRender() override;
virtual void OnEvent(SDL_Event& e) override;
private:
void UI_ToolBar();
void OnScenePlay();
void OnSceneStop();
void SaveScene() const;
void OpenScene();
void OpenScene(const std::filesystem::path& scenePath);
void NewScene();
void ChangeOptMode(unsigned int mode);
private:
OrthographicCameraController m_CameraController;
Ref<Texture2D> m_LogoTexture;
Ref<Texture2D> m_CheckerBoardTexture;
Ref<Scene> m_ActiveScene;
EditorCamera m_EditorCamera;
@ -53,7 +60,16 @@ namespace Hazel
SceneHierachyPanel m_SceneHierachyPanel;
ContentBroswerPanel m_ContentBroswerPanel;
int m_GizmoType = 0;
int m_GizmoType = -1;
enum class SceneState
{
Edit = 0, Play = 1
};
SceneState m_SceneState = SceneState::Edit;
Ref<Texture2D> m_PlayIcon, m_StopIcon;
};
}

View File

@ -251,15 +251,40 @@ namespace Hazel
ImGui::OpenPopup("Add");
if (ImGui::BeginPopup("Add"))
{
if (ImGui::MenuItem("Camera"))
if (!m_SelectionContext.HasComponent<CameraComponent>())
{
m_SelectionContext.AddComponent<CameraComponent>();
ImGui::CloseCurrentPopup();
if (ImGui::MenuItem("Camera"))
{
m_SelectionContext.AddComponent<CameraComponent>();
ImGui::CloseCurrentPopup();
}
}
if (ImGui::MenuItem("Sprite"))
if (!m_SelectionContext.HasComponent<SpriteRendererComponent>())
{
m_SelectionContext.AddComponent<SpriteRendererComponent>();
ImGui::CloseCurrentPopup();
if (ImGui::MenuItem("Sprite"))
{
m_SelectionContext.AddComponent<SpriteRendererComponent>();
ImGui::CloseCurrentPopup();
}
}
if (!m_SelectionContext.HasComponent<RigidBody2DComponent>())
{
if (ImGui::MenuItem("RigidBody 2D"))
{
m_SelectionContext.AddComponent<RigidBody2DComponent>();
ImGui::CloseCurrentPopup();
}
}
if (!m_SelectionContext.HasComponent<BoxCollider2DComponent>())
{
if (ImGui::MenuItem("Box Collider 2D"))
{
m_SelectionContext.AddComponent<BoxCollider2DComponent>();
ImGui::CloseCurrentPopup();
}
}
ImGui::EndPopup();
@ -356,8 +381,48 @@ namespace Hazel
ImGui::DragFloat("Tiling Color", &component.TilingFactor, 0.1f, 0.0f, 100.f);
});
DrawComponent<RigidBody2DComponent>("Rigidbody 2D", entity, [](auto& component)
{
static const char* bodyTypeStrings[] = {"Static", "Dynamic", "Kinematic"};
const char* currentBodyTypeString = bodyTypeStrings[(int)component.Type];
if (ImGui::BeginCombo("Body Type", currentBodyTypeString))
{
for (int i = 0; i < 3; i++)
{
const bool isSelected = currentBodyTypeString == bodyTypeStrings[i];
if (ImGui::Selectable(bodyTypeStrings[i], isSelected))
{
currentBodyTypeString = bodyTypeStrings[i];
component.Type = (RigidBody2DComponent::BodyType)i;
}
if (isSelected)
{
ImGui::SetItemDefaultFocus();
}
}
ImGui::EndCombo();
}
ImGui::Checkbox("Fixed Rotation", &component.FixedRotation);
});
DrawComponent<BoxCollider2DComponent>("Box Collider 2D", entity, [](auto& component)
{
ImGui::DragFloat2("Offset", glm::value_ptr(component.Offset));
ImGui::DragFloat2("Size", glm::value_ptr(component.Size));
ImGui::DragFloat("Density", &component.Density, 0.01f, 0.0f);
ImGui::DragFloat("Friction", &component.Friction, 0.01f, 0.0f, 1.0f);
ImGui::DragFloat("Restitution", &component.Restitution, 0.01f, 0.0f, 1.0f);
ImGui::DragFloat("Restitution Threshold", &component.RestitutionThreshold, 0.01f, 0.0f);
});
}
void SceneHierachyPanel::SetSelectedEntity(const Entity entity)
{
m_SelectionContext = entity;

View File

@ -10,7 +10,7 @@ namespace Hazel
{
public:
HazelEditor()
: Application("Hazel Editor", 1600, 900)
: Application("Hazel Editor", 1920, 1080)
{
PushLayer(new EditorLayer());
}