添加简单文件资产视图, 添加资源拖拽功能

This commit is contained in:
2025-06-11 00:22:53 +08:00
parent f4aa895bbb
commit 91af2392ed
15 changed files with 194 additions and 10 deletions

View File

@ -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)

View File

@ -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);
}
}

View File

@ -11,6 +11,8 @@
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/ext/matrix_transform.hpp>
#include <Hazel/Renderer/Texture.h>
#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<Texture2D> Texture;
float TilingFactor = 1.0f;
SpriteRendererComponent() = default;
SpriteRendererComponent() = default;
SpriteRendererComponent(const SpriteRendererComponent&) = default;
SpriteRendererComponent(const glm::vec4& color) : Color(color)

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 119 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 357 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 667 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 438 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 115 B

View File

@ -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<Scene>();
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<Scene>();
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;

View File

@ -6,6 +6,7 @@
#define EDITORLAYER_H
#include <Hazel.h>
#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<FrameBuffer> m_FrameBuffer;
SceneHierachyPanel m_SceneHierachyPanel;
ContentBroswerPanel m_ContentBroswerPanel;
int m_GizmoType = 0;
};

View File

@ -0,0 +1,101 @@
//
// Created by sfd on 25-6-10.
//
#include "ContentBroswerPanel.h"
#include <filesystem>
#include <../../../../Hazel/vendor/imgui/imgui.h>
#include <string>
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<Texture2D> 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();
}
}

View File

@ -0,0 +1,32 @@
//
// Created by sfd on 25-6-10.
//
#ifndef CONTENTBROSWERPANEL_H
#define CONTENTBROSWERPANEL_H
#include <filesystem>
#include <Hazel/Core/Core.h>
#include <Hazel/Renderer/Texture.h>
namespace Hazel
{
class ContentBroswerPanel
{
public:
ContentBroswerPanel();
virtual ~ContentBroswerPanel() = default;
void OnImGuiRender();
private:
std::filesystem::path m_CurrentDirectory;
Ref<Texture2D> m_DirectoryIcon;
Ref<Texture2D> m_FileIcon;
};
}
#endif //CONTENTBROSWERPANEL_H

View File

@ -3,6 +3,8 @@
//
#include "SceneHierachyPanel.h"
#include <filesystem>
#include <imgui.h>
#include <imgui_internal.h>
#include <glm/gtc/type_ptr.hpp>
@ -10,6 +12,8 @@
namespace Hazel
{
extern const std::filesystem::path g_AssetPath;
SceneHierachyPanel::SceneHierachyPanel(const Ref<Scene>& context)
{
SetContext(context);
@ -336,6 +340,21 @@ namespace Hazel
DrawComponent<SpriteRendererComponent>("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);
});
}