Skip to content

Commit

Permalink
Added some filewatch improvements
Browse files Browse the repository at this point in the history
Added some more infastructure to allow testing of both asset system and new material system
  • Loading branch information
tomheeleynz committed Aug 16, 2022
1 parent ac13c70 commit dfa6677
Show file tree
Hide file tree
Showing 16 changed files with 182 additions and 48 deletions.
6 changes: 6 additions & 0 deletions Arcane/src/Arcane.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,9 @@
/////////////////////////////////////////////////////
#include "Arcane/Utils/Utils.h"
#include "Arcane/Utils/FileWatcher.h"


/////////////////////////////////////////////////////
////////// Assets
/////////////////////////////////////////////////////
#include "Arcane/Assets/MeshAsset.h"
3 changes: 2 additions & 1 deletion Arcane/src/Arcane/Assets/Asset.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ namespace Arcane
{
enum class AssetType
{
MESH
MESH,
TEXTURE
};

class Asset
Expand Down
15 changes: 15 additions & 0 deletions Arcane/src/Arcane/Assets/TextureAsset.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "TextureAsset.h"

namespace Arcane
{
TextureAsset::TextureAsset(std::filesystem::path filepath)
{
SetAssetType(AssetType::TEXTURE);
m_Texture = Texture::Create(filepath.string());
}

Texture* TextureAsset::GetTexture()
{
return m_Texture;
}
}
18 changes: 18 additions & 0 deletions Arcane/src/Arcane/Assets/TextureAsset.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include <filesystem>
#include "Asset.h"
#include "Arcane/Renderer/Texture.h"

namespace Arcane
{
class TextureAsset : public Asset
{
public:
TextureAsset(std::filesystem::path filepath);

Texture* GetTexture();
private:
Texture* m_Texture;
};
}
2 changes: 2 additions & 0 deletions Arcane/src/Arcane/Core/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ namespace Arcane

// ImGui
void RenderImGui();

AssetDatabase& GetAssetDatabase() { return *m_AssetDatabase; }
private:
Window* m_Window;
static Application* s_Instance;
Expand Down
2 changes: 1 addition & 1 deletion Arcane/src/Arcane/ECS/Component.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace Arcane

struct MeshComponent
{
Mesh* mesh;
Mesh* mesh = nullptr;
std::string filepath;
};

Expand Down
5 changes: 4 additions & 1 deletion Arcane/src/Arcane/Scene/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace Arcane
entt::entity newHandle = m_Registry.create();
Entity* newEntity = new Entity(newHandle, this);
newEntity->AddComponent<TagComponent>(name);
newEntity->AddComponent<TransformComponent>();
return newEntity;
}

Expand Down Expand Up @@ -43,7 +44,9 @@ namespace Arcane
auto& mesh = view.get<MeshComponent>(entity);
auto& transform = view.get<TransformComponent>(entity);
auto& meshRenderer = view.get<MeshRendererComponent>(entity);
m_SceneRenderer->SubmitMesh(mesh.mesh, transform, meshRenderer.material);

if (mesh.mesh != nullptr)
m_SceneRenderer->SubmitMesh(mesh.mesh, transform, meshRenderer.material);
}
}
m_SceneRenderer->RenderScene();
Expand Down
47 changes: 31 additions & 16 deletions Arcane/src/Arcane/Utils/FileWatcher.cpp
Original file line number Diff line number Diff line change
@@ -1,45 +1,60 @@
#include <nlohmann/json.hpp>
#include "FileWatcher.h"

namespace Arcane
{
FileWatcher::FileWatcher(std::string path_to_watch, std::chrono::duration<int, std::milli> delay)
{
m_Directory = path_to_watch;
for (auto& file : std::filesystem::recursive_directory_iterator(path_to_watch)) {
m_Paths[file.path().string()] = std::filesystem::last_write_time(file);
}
//for (auto& file : std::filesystem::directory_iterator(path_to_watch)) {

// m_Paths[file.path().string()] = std::filesystem::last_write_time(file);
//}
}

void FileWatcher::Start(const std::function<void(std::string, FileStatus)>& action)
void FileWatcher::Update()
{
auto it = m_Paths.begin();
while (it != m_Paths.end())
{
if (!std::filesystem::exists(it->first)) {
action(it->first, FileStatus::Deleted);
it = m_Paths.erase(it);
}
else {
it++;
}
}

for (auto& file : std::filesystem::recursive_directory_iterator(m_Directory)) {
for (auto& file : std::filesystem::directory_iterator(m_Directory)) {
auto current_file_last_write_time = std::filesystem::last_write_time(file);

if (!contains(file.path().string()))
{
m_Paths[file.path().string()] = current_file_last_write_time;
action(file.path().string(), FileStatus::Created);
}
else {
if (m_Paths[file.path().string()] != current_file_last_write_time) {
m_Paths[file.path().string()] = current_file_last_write_time;
action(file.path().string(), FileStatus::Modified);
if (file.path().extension() != ".arcmeta") {
if (!contains(file.path().string()))
{

FileInfo newFileInfo;
newFileInfo.name = file.path().stem().string();

if (file.is_regular_file())
newFileInfo.assetID = GetAssetID(file.path());
else
newFileInfo.isDirectory = true;

m_Paths[file.path().string()] = newFileInfo;
}
}
}
}

int FileWatcher::GetAssetID(std::filesystem::path metaPath)
{
std::string metaFileName = metaPath.stem().string() + ".arcmeta";
std::filesystem::path metaFile = metaPath.parent_path() / metaFileName;

std::ifstream i(metaFile.string());

nlohmann::json metaJson;
i >> metaJson;

return metaJson["id"];
}
}
16 changes: 14 additions & 2 deletions Arcane/src/Arcane/Utils/FileWatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <map>
#include <unordered_map>
#include <functional>
#include <fstream>

enum class FileStatus
{
Expand All @@ -14,17 +15,28 @@ enum class FileStatus
Modified
};

struct FileInfo
{
std::string name;
int assetID = -1;
bool isDirectory = false;
};

namespace Arcane
{
class FileWatcher
{
public:
FileWatcher(std::string path_to_watch, std::chrono::duration<int, std::milli> delay);
void Start(const std::function<void(std::string, FileStatus)>& action);
void Update();


std::unordered_map<std::string, FileInfo> GetPaths() { return m_Paths; }
private:
int GetAssetID(std::filesystem::path metaPath);
private:
std::chrono::duration<int, std::milli> m_Delay;
std::unordered_map<std::string, std::filesystem::file_time_type> m_Paths;
std::unordered_map<std::string, FileInfo> m_Paths;
std::string m_Directory;

bool contains(const std::string & key) {
Expand Down
34 changes: 17 additions & 17 deletions EnchantingTable/imgui.ini
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ DockId=0x39930E19,0

[Window][DockSpace Demo]
Pos=0,0
Size=2560,1377
Size=1920,1017
Collapsed=0

[Window][Entity Panel]
Expand All @@ -33,42 +33,42 @@ Collapsed=0
DockId=0x00000005,0

[Window][File Browser]
Pos=408,983
Size=1723,394
Pos=304,616
Size=1344,401
Collapsed=0
DockId=0x00000008,0

[Window][Viewport]
Pos=408,19
Size=1723,962
Pos=304,19
Size=1344,595
Collapsed=0
DockId=0x00000007,0

[Window][Scene]
Pos=0,19
Size=406,1358
Size=302,998
Collapsed=0
DockId=0x00000009,0

[Window][Entity]
Pos=2133,19
Size=427,1358
Pos=1650,19
Size=270,998
Collapsed=0
DockId=0x0000000C,0

[Docking][Data]
DockSpace ID=0x39930E19 Pos=184,152 Size=1072,842 CentralNode=1 Selected=0x4A17B156
DockSpace ID=0x3BC79352 Window=0x4647B76E Pos=0,19 Size=2560,1358 Split=X
DockNode ID=0x0000000B Parent=0x3BC79352 SizeRef=1331,1358 Split=X
DockNode ID=0x00000009 Parent=0x0000000B SizeRef=406,1181 Selected=0x18B8C0DE
DockNode ID=0x0000000A Parent=0x0000000B SizeRef=1723,1181 Split=X
DockNode ID=0x00000003 Parent=0x0000000A SizeRef=1857,1181 Split=X
DockSpace ID=0x3BC79352 Window=0x4647B76E Pos=0,19 Size=1920,998 Split=X
DockNode ID=0x00000009 Parent=0x3BC79352 SizeRef=252,1181 Selected=0x18B8C0DE
DockNode ID=0x0000000A Parent=0x3BC79352 SizeRef=1346,1181 Split=X
DockNode ID=0x0000000B Parent=0x0000000A SizeRef=1331,1358 Split=X
DockNode ID=0x00000003 Parent=0x0000000B SizeRef=1857,1181 Split=X
DockNode ID=0x00000005 Parent=0x00000003 SizeRef=409,1181 Selected=0x18F2AEBA
DockNode ID=0x00000006 Parent=0x00000003 SizeRef=1774,1181 Split=X
DockNode ID=0x00000001 Parent=0x00000006 SizeRef=1262,1181 Split=Y Selected=0x995B0CF8
DockNode ID=0x00000007 Parent=0x00000001 SizeRef=1178,962 CentralNode=1 Selected=0x995B0CF8
DockNode ID=0x00000008 Parent=0x00000001 SizeRef=1178,394 Selected=0x2389D759
DockNode ID=0x00000007 Parent=0x00000001 SizeRef=1178,595 CentralNode=1 Selected=0x995B0CF8
DockNode ID=0x00000008 Parent=0x00000001 SizeRef=1178,401 Selected=0x2389D759
DockNode ID=0x00000002 Parent=0x00000006 SizeRef=336,1181 Selected=0x4A17B156
DockNode ID=0x00000004 Parent=0x0000000A SizeRef=360,1181 Selected=0x2DDDAD1E
DockNode ID=0x0000000C Parent=0x3BC79352 SizeRef=267,1358 Selected=0x0984415E
DockNode ID=0x00000004 Parent=0x0000000B SizeRef=360,1181 Selected=0x2DDDAD1E
DockNode ID=0x0000000C Parent=0x0000000A SizeRef=267,1358 Selected=0x0984415E

Binary file added EnchantingTable/src/Assets/Textures/diffuse.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions EnchantingTable/src/EditorLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,17 @@ void EditorLayer::OnImGuiRender()
m_ViewportSize = { viewportSize.x, viewportSize.y };

Arcane::UI::Image(m_Viewport, viewportSize);

if (ImGui::BeginDragDropTarget())
{
const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("CURRENT_SELECTED_ASSET");

if (payload != nullptr) {
int assetID = *static_cast<int*>(payload->Data);
}
ImGui::EndDragDropTarget();
}

}
ImGui::End();

Expand Down
47 changes: 38 additions & 9 deletions EnchantingTable/src/Panels/EntityPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ void EntityPanel::Update()
{
ImGui::Begin("Entity");
{
if (ImGui::Button("Add Component")) {
AddMeshComponent(m_Context);
}

if (m_Context)
{
DrawComponents(m_Context);
Expand Down Expand Up @@ -62,25 +66,40 @@ void EntityPanel::DrawComponents(Arcane::Entity& entity)
if (entity.HasComponent<MeshComponent>())
{
DrawComponent<MeshComponent>("Mesh", entity, [](auto& component) {

// Create something i can add to
ImGui::Text("Mesh");
if (ImGui::BeginDragDropTarget())
{
const ImGuiPayload* payload = ImGui::AcceptDragDropPayload("CURRENT_SELECTED_ASSET");

if (payload != nullptr) {
int assetID = *static_cast<int*>(payload->Data);
MeshAsset* meshAsset = static_cast<MeshAsset*>(Arcane::Application::Get().GetAssetDatabase().GetAsset(assetID));
component.mesh = meshAsset->GetMesh();
}
ImGui::EndDragDropTarget();
}
});
}

if (entity.HasComponent<MeshRendererComponent>())
{
DrawComponent<MeshRendererComponent>("Mesh Renderer", entity, [](auto& component) {
Material* material = component.material;
for (Arcane::ShaderVariable variable : material->GetMaterialVariables())
{
if (variable.Type == Arcane::ShaderVariableType::Vec3)

if (material != nullptr) {
for (Arcane::ShaderVariable variable : material->GetMaterialVariables())
{
glm::vec3 currentValue = material->GetVec3(variable.offset);
if (variable.Type == Arcane::ShaderVariableType::Vec3)
{
glm::vec3 currentValue = material->GetVec3(variable.offset);

if (currentValue.x < 0)
currentValue = {0.0f, 0.0f, 0.0f};
if (currentValue.x < 0)
currentValue = {0.0f, 0.0f, 0.0f};

ImGui::ColorEdit3(variable.Name.c_str(), glm::value_ptr(currentValue));
material->WriteVec3(variable.offset, currentValue);
ImGui::ColorEdit3(variable.Name.c_str(), glm::value_ptr(currentValue));
material->WriteVec3(variable.offset, currentValue);
}
}
}
});
Expand All @@ -100,3 +119,13 @@ void EntityPanel::DrawComponents(Arcane::Entity& entity)
}
}

void EntityPanel::AddMeshComponent(Arcane::Entity& entity)
{
Arcane::MeshComponent meshComponent;
Arcane::MeshRendererComponent meshRendererComponent;

meshRendererComponent.material = Arcane::Material::Create(Arcane::ShaderLibrary::GetShader("Mesh"));
entity.AddComponent<Arcane::MeshComponent>(meshComponent);
entity.AddComponent<Arcane::MeshRendererComponent>(meshRendererComponent);
}

4 changes: 4 additions & 0 deletions EnchantingTable/src/Panels/EntityPanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ class EntityPanel

void Update();
void DrawComponents(Arcane::Entity& entity);

private:
void AddMeshComponent(Arcane::Entity& entity);

private:
Arcane::Entity m_Context;
};

0 comments on commit dfa6677

Please sign in to comment.