diff --git a/CMakeLists.txt b/CMakeLists.txt index 0abdbb8..aedfe26 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,6 +23,8 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) file(GLOB ASSETS Sandbox/assets/*) file(COPY ${ASSETS} DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/assets) +file(GLOB RESOURCES Sandbox/Resources/*) +file(COPY ${RESOURCES} DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/Resources) add_subdirectory(Hazel) add_subdirectory(Sandbox) \ No newline at end of file diff --git a/Hazel/src/Hazel/Renderer/Renderer2D.cpp b/Hazel/src/Hazel/Renderer/Renderer2D.cpp index 6c56a99..9be1dae 100644 --- a/Hazel/src/Hazel/Renderer/Renderer2D.cpp +++ b/Hazel/src/Hazel/Renderer/Renderer2D.cpp @@ -328,7 +328,7 @@ namespace Hazel FlushAndReset(); } - const glm::vec2 texCoords[] = {{0.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 1.0f}}; + const glm::vec2 texCoords[] = {{0.0f, 0.0f}, {1.0f, 0.0f}, {1.0f, 1.0f}, {0.0f, 1.0f}}; constexpr float textureIndex = 0.0f; constexpr float tilingFactor = 1.0f; @@ -534,6 +534,9 @@ namespace Hazel void Renderer2D::DrawSprite(const glm::mat4& transform, SpriteRendererComponent& src, int entityID) { - DrawQuad(transform, src.Color, entityID); + if (src.Texture) + DrawQuad(transform, src.Texture, src.TilingFactor, src.Color, entityID); + else + DrawQuad(transform, src.Color, entityID); } } diff --git a/Hazel/src/Hazel/Scene/Components.h b/Hazel/src/Hazel/Scene/Components.h index b165abd..fc347b1 100644 --- a/Hazel/src/Hazel/Scene/Components.h +++ b/Hazel/src/Hazel/Scene/Components.h @@ -11,6 +11,8 @@ #define GLM_ENABLE_EXPERIMENTAL #include +#include + #include "glm/glm.hpp" #include "glm/gtx/quaternion.hpp" @@ -45,8 +47,10 @@ namespace Hazel struct SpriteRendererComponent { glm::vec4 Color{1.0f, 1.0f, 1.0f, 1.0f}; + Ref Texture; + float TilingFactor = 1.0f; - SpriteRendererComponent() = default; + SpriteRendererComponent() = default; SpriteRendererComponent(const SpriteRendererComponent&) = default; SpriteRendererComponent(const glm::vec4& color) : Color(color) diff --git a/Sandbox/Resources/Icons/ContentBrowser/DirectoryIcon.png b/Sandbox/Resources/Icons/ContentBrowser/DirectoryIcon.png new file mode 100644 index 0000000..62cff34 Binary files /dev/null and b/Sandbox/Resources/Icons/ContentBrowser/DirectoryIcon.png differ diff --git a/Sandbox/Resources/Icons/ContentBrowser/FileIcon.png b/Sandbox/Resources/Icons/ContentBrowser/FileIcon.png new file mode 100644 index 0000000..0406dec Binary files /dev/null and b/Sandbox/Resources/Icons/ContentBrowser/FileIcon.png differ diff --git a/Sandbox/Resources/Icons/PauseButton.png b/Sandbox/Resources/Icons/PauseButton.png new file mode 100644 index 0000000..1345ae0 Binary files /dev/null and b/Sandbox/Resources/Icons/PauseButton.png differ diff --git a/Sandbox/Resources/Icons/PlayButton.png b/Sandbox/Resources/Icons/PlayButton.png new file mode 100644 index 0000000..e5d7ef4 Binary files /dev/null and b/Sandbox/Resources/Icons/PlayButton.png differ diff --git a/Sandbox/Resources/Icons/SimulateButton.png b/Sandbox/Resources/Icons/SimulateButton.png new file mode 100644 index 0000000..93c04e8 Binary files /dev/null and b/Sandbox/Resources/Icons/SimulateButton.png differ diff --git a/Sandbox/Resources/Icons/StepButton.png b/Sandbox/Resources/Icons/StepButton.png new file mode 100644 index 0000000..59630f8 Binary files /dev/null and b/Sandbox/Resources/Icons/StepButton.png differ diff --git a/Sandbox/Resources/Icons/StopButton.png b/Sandbox/Resources/Icons/StopButton.png new file mode 100644 index 0000000..ce99b3c Binary files /dev/null and b/Sandbox/Resources/Icons/StopButton.png differ diff --git a/Sandbox/src/Editor/EditorLayer.cpp b/Sandbox/src/Editor/EditorLayer.cpp index 861027c..4336783 100644 --- a/Sandbox/src/Editor/EditorLayer.cpp +++ b/Sandbox/src/Editor/EditorLayer.cpp @@ -16,6 +16,9 @@ namespace Hazel { + + extern const std::filesystem::path g_AssetPath; + EditorLayer::EditorLayer() : Layer("HazelEditor"), m_CameraController((float)Application::Get().GetWindow().GetWidth() / (float)Application::Get().GetWindow().GetHeight()) { @@ -191,6 +194,7 @@ namespace Hazel // Scene Hierachy Panel m_SceneHierachyPanel.OnImGuiRender(); + m_ContentBroswerPanel.OnImGuiRender(); // Render Status { @@ -236,6 +240,15 @@ namespace Hazel ImGui::Image(m_FrameBuffer->GetColorAttachmentID(), {m_ViewPortSize.x, m_ViewPortSize.y}, {0, 1}, {1, 0}); + if (ImGui::BeginDragDropTarget()) + { + if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("CONTENT_BROSWER_ITEM")) + { + const wchar_t* path = (const wchar_t*)payload->Data; + OpenScene(std::filesystem::path(g_AssetPath) / path); + } + ImGui::EndDragDropTarget(); + } auto windowsSize = ImGui::GetWindowSize(); ImVec2 minBound = ImGui::GetWindowPos(); @@ -326,14 +339,17 @@ namespace Hazel { std::string filepath = FileDiaglogs::OpenFile("Scene(*.scene, *.yaml)\0*.scene;*.yaml\0All files\0*.*\0\0"); if (!filepath.empty()) - { - m_ActiveScene = CreateRef(); - m_ActiveScene->OnViewportResize((uint32_t)m_ViewPortSize.x, (uint32_t)m_ViewPortSize.y); - m_SceneHierachyPanel.SetContext(m_ActiveScene); + OpenScene(filepath); + } - SceneSerializer serializer(m_ActiveScene); - serializer.Deserialize(filepath); - } + void EditorLayer::OpenScene(const std::filesystem::path& scenePath) + { + m_ActiveScene = CreateRef(); + m_ActiveScene->OnViewportResize((uint32_t)m_ViewPortSize.x, (uint32_t)m_ViewPortSize.y); + m_SceneHierachyPanel.SetContext(m_ActiveScene); + + SceneSerializer serializer(m_ActiveScene); + serializer.Deserialize(scenePath.string()); } void EditorLayer::NewScene() @@ -359,6 +375,7 @@ namespace Hazel if (e.button.clicks && m_ViewportHovered && !ImGuizmo::IsOver() && Input::IsMouseButtonPressed(SDL_BUTTON_LEFT) && !Input::IsKeyPressed(SDL_SCANCODE_LALT)) m_SceneHierachyPanel.SetSelectedEntity(m_HoveredEntity); +#define SHORTCUT_EXIT (SDL_KMOD_CTRL | SDLK_W) #define SHORTCUT_NEW (SDL_KMOD_CTRL | SDLK_N) #define SHORTCUT_OPEN (SDL_KMOD_CTRL | SDLK_O) #define SHORTCUT_SAVE_ALL (SDL_KMOD_CTRL | SDL_KMOD_SHIFT | SDLK_S) @@ -369,6 +386,9 @@ namespace Hazel switch (ctrl | shift | e.key.key) { + case SHORTCUT_EXIT: + Application::Get().Close(); + break; case SHORTCUT_NEW: NewScene(); break; diff --git a/Sandbox/src/Editor/EditorLayer.h b/Sandbox/src/Editor/EditorLayer.h index 9c9fccf..eae573a 100644 --- a/Sandbox/src/Editor/EditorLayer.h +++ b/Sandbox/src/Editor/EditorLayer.h @@ -6,6 +6,7 @@ #define EDITORLAYER_H #include +#include "Panels/ContentBroswerPanel.h" #include "Panels/SceneHierachyPanel.h" namespace Hazel @@ -25,6 +26,7 @@ namespace Hazel private: void SaveScene() const; void OpenScene(); + void OpenScene(const std::filesystem::path& scenePath); void NewScene(); void ChangeOptMode(unsigned int mode); @@ -49,6 +51,7 @@ namespace Hazel Ref m_FrameBuffer; SceneHierachyPanel m_SceneHierachyPanel; + ContentBroswerPanel m_ContentBroswerPanel; int m_GizmoType = 0; }; diff --git a/Sandbox/src/Editor/Panels/ContentBroswerPanel.cpp b/Sandbox/src/Editor/Panels/ContentBroswerPanel.cpp new file mode 100644 index 0000000..10f2ada --- /dev/null +++ b/Sandbox/src/Editor/Panels/ContentBroswerPanel.cpp @@ -0,0 +1,101 @@ +// +// Created by sfd on 25-6-10. +// + +#include "ContentBroswerPanel.h" + +#include +#include <../../../../Hazel/vendor/imgui/imgui.h> +#include + +namespace Hazel +{ + + extern const std::filesystem::path g_AssetPath = "assets"; + + ContentBroswerPanel::ContentBroswerPanel() + : m_CurrentDirectory(g_AssetPath) + { + m_DirectoryIcon = Texture2D::Create("Resources/Icons/ContentBrowser/DirectoryIcon.png"); + m_FileIcon = Texture2D::Create("Resources/Icons/ContentBrowser/FileIcon.png"); + } + + + void ContentBroswerPanel::OnImGuiRender() + { + ImGui::Begin("ContentBroswerPanel"); + + ImGui::Text("%s", m_CurrentDirectory.string().c_str()); + ImGui::Separator(); + + if (m_CurrentDirectory != std::filesystem::path(g_AssetPath)) + { + if (ImGui::Button("..")) + { + m_CurrentDirectory = m_CurrentDirectory.parent_path(); + } + } + + static float folderSize = 90.0f; + static float padding = 16.0f; + float cellSize = folderSize + padding; + + float panelWidth = ImGui::GetContentRegionAvail().x; + int columnsCount = (int)panelWidth / cellSize; + if (columnsCount <= 1) + columnsCount = 1; + + ImGui::Columns(columnsCount, 0, false); + + for (auto& directory : std::filesystem::directory_iterator(m_CurrentDirectory)) + { + auto& path = directory.path(); + auto relativePath = std::filesystem::relative(directory.path(), g_AssetPath); + std::string filePathString = relativePath.filename().string(); + + + ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0, 0, 0, 0)); + Ref icon = directory.is_directory() ? m_DirectoryIcon : m_FileIcon; + ImGui::ImageButton(std::to_string(hash_value(path)).c_str(),icon->GetRendererID(), {folderSize, folderSize}, {0, 1}, {1, 0}); + ImGui::PopStyleColor(); + + if (ImGui::BeginDragDropSource()) + { + const wchar_t* itemPath = relativePath.c_str(); + ImGui::SetDragDropPayload("CONTENT_BROSWER_ITEM", itemPath, (wcslen(itemPath) + 1) * sizeof(wchar_t), ImGuiCond_Once); + ImGui::EndDragDropSource(); + } + + if (ImGui::IsItemHovered() && ImGui::IsMouseClicked(ImGuiMouseButton_Left)) + { + if (directory.is_directory()) + m_CurrentDirectory /= path.filename(); + } + ImGui::TextWrapped(filePathString.c_str()); + ImGui::NextColumn(); + + /* + if (directory.is_directory()) + { + if (ImGui::Button(filePathString.c_str())) + { + m_CurrentDirectory /= path.filename(); + } + }else + { + if (ImGui::Button(filePathString.c_str())) + { + } + } + */ + + } + + ImGui::Columns(1); + + ImGui::SliderFloat("folder size", &folderSize, 16, 512); + ImGui::SliderFloat("padding", &padding, 0, 32); + + ImGui::End(); + } +} diff --git a/Sandbox/src/Editor/Panels/ContentBroswerPanel.h b/Sandbox/src/Editor/Panels/ContentBroswerPanel.h new file mode 100644 index 0000000..3a5df1c --- /dev/null +++ b/Sandbox/src/Editor/Panels/ContentBroswerPanel.h @@ -0,0 +1,32 @@ +// +// Created by sfd on 25-6-10. +// + +#ifndef CONTENTBROSWERPANEL_H +#define CONTENTBROSWERPANEL_H +#include +#include +#include + +namespace Hazel +{ + class ContentBroswerPanel + { + public: + ContentBroswerPanel(); + virtual ~ContentBroswerPanel() = default; + + void OnImGuiRender(); + private: + + std::filesystem::path m_CurrentDirectory; + + Ref m_DirectoryIcon; + Ref m_FileIcon; + + }; +} + + + +#endif //CONTENTBROSWERPANEL_H diff --git a/Sandbox/src/Editor/Panels/SceneHierachyPanel.cpp b/Sandbox/src/Editor/Panels/SceneHierachyPanel.cpp index cea3a70..f5b7e86 100644 --- a/Sandbox/src/Editor/Panels/SceneHierachyPanel.cpp +++ b/Sandbox/src/Editor/Panels/SceneHierachyPanel.cpp @@ -3,6 +3,8 @@ // #include "SceneHierachyPanel.h" + +#include #include #include #include @@ -10,6 +12,8 @@ namespace Hazel { + extern const std::filesystem::path g_AssetPath; + SceneHierachyPanel::SceneHierachyPanel(const Ref& context) { SetContext(context); @@ -336,6 +340,21 @@ namespace Hazel DrawComponent("Sprite Renderer", entity, [](auto& component) { ImGui::ColorEdit4("Color", glm::value_ptr(component.Color)); + // Texture + ImGui::Button("Texture", ImVec2(100.f, 0.0f)); + + if (ImGui::BeginDragDropTarget()) + { + if (const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("CONTENT_BROSWER_ITEM")) + { + const wchar_t* path = (const wchar_t*)payload->Data; + std::filesystem::path texturePath = std::filesystem::path(g_AssetPath) / path; + component.Texture = Texture2D::Create(texturePath.string()); + } + ImGui::EndDragDropTarget(); + } + + ImGui::DragFloat("Tiling Color", &component.TilingFactor, 0.1f, 0.0f, 100.f); }); }