From c3df5775d0b0b01ce16fd1b8fed527169768cb62 Mon Sep 17 00:00:00 2001 From: Joey Pollack Date: Wed, 1 Jun 2022 18:49:39 -0400 Subject: [PATCH] Asset Browser now has references to the actual EditorAsset objects and can detect single click selection and double click open actions --- docs/tasks/editor.todo | 1 + src/assets/types/asset.cpp | 13 +++- src/assets/types/asset.h | 6 ++ src/assets/types/script.cpp | 1 + src/run_modes/editor/component_guis.cpp | 2 +- .../editor/contents/content_manager.cpp | 13 ++++ .../editor/contents/content_manager.h | 2 + src/run_modes/editor/editor.cpp | 8 +-- src/run_modes/editor/editor.h | 5 +- src/run_modes/editor/panels/asset_browser.cpp | 44 +++++++++++-- src/run_modes/editor/panels/asset_browser.h | 4 +- .../editor/panels/properties_view.cpp | 2 +- src/run_modes/editor/panels/world_tree.cpp | 6 ++ src/world/world.cpp | 20 +++--- test_data/imgui.ini | 65 +++++++++++-------- 15 files changed, 136 insertions(+), 56 deletions(-) diff --git a/docs/tasks/editor.todo b/docs/tasks/editor.todo index 0597fa2..df96ff8 100644 --- a/docs/tasks/editor.todo +++ b/docs/tasks/editor.todo @@ -41,6 +41,7 @@ Editor: Scene Hierarchy (Tree View): Asset Viewer: + ✔ Get references to the EditorAsset objects instead of the raw file locations @done(22-06-01 18:48) ☐ Put files into a table with columns for the file Properties Tools: diff --git a/src/assets/types/asset.cpp b/src/assets/types/asset.cpp index b16a705..b759ce8 100644 --- a/src/assets/types/asset.cpp +++ b/src/assets/types/asset.cpp @@ -11,7 +11,13 @@ namespace lunarium { Asset::Asset(AssetType type) - : mType(type) + : mType(type), mUUID(0) + { + + } + + Asset::Asset(LUUID uuid, AssetType type) + : mType(type), mUUID(uuid) { } @@ -26,4 +32,9 @@ namespace lunarium return mType; } + LUUID Asset::GetUUID() const + { + return mUUID; + } + } \ No newline at end of file diff --git a/src/assets/types/asset.h b/src/assets/types/asset.h index ba98fea..969c947 100644 --- a/src/assets/types/asset.h +++ b/src/assets/types/asset.h @@ -9,6 +9,8 @@ #ifndef ASSET_H_ #define ASSET_H_ +#include + namespace lunarium { enum AssetType @@ -23,11 +25,15 @@ namespace lunarium { public: Asset(AssetType type = AssetType::ASSET_TYPE_UNKNOWN); + Asset(LUUID uuid, AssetType type = AssetType::ASSET_TYPE_UNKNOWN); virtual ~Asset() = 0; AssetType GetType() const; + + LUUID GetUUID() const; private: AssetType mType; + LUUID mUUID; }; } diff --git a/src/assets/types/script.cpp b/src/assets/types/script.cpp index 9c19dd6..16a01e9 100644 --- a/src/assets/types/script.cpp +++ b/src/assets/types/script.cpp @@ -13,6 +13,7 @@ namespace lunarium void Script::SetScript(const char* script) { mScript = script; + mScriptErrors.clear(); } const char* Script::GetScript() const diff --git a/src/run_modes/editor/component_guis.cpp b/src/run_modes/editor/component_guis.cpp index 9601e55..050c7f4 100644 --- a/src/run_modes/editor/component_guis.cpp +++ b/src/run_modes/editor/component_guis.cpp @@ -17,7 +17,7 @@ namespace lunarium { namespace editor { ImGui::Text("Tag:"); ImGui::SameLine(); - ImGui::InputText("##Tag", comp.Info.data(), comp.Info.capacity()) + ImGui::InputText("##Tag", comp.Info.data(), comp.Info.capacity()); } void CompGui::RenderTransformComp(TransformComponent& comp) diff --git a/src/run_modes/editor/contents/content_manager.cpp b/src/run_modes/editor/contents/content_manager.cpp index b9bff65..757aa1c 100644 --- a/src/run_modes/editor/contents/content_manager.cpp +++ b/src/run_modes/editor/contents/content_manager.cpp @@ -217,6 +217,19 @@ namespace lunarium { namespace editor } } + void ContentManager::GetAllAssetsInDirectory(std::vector& container, std::filesystem::path dir) + { + container.clear(); + for (auto iter = mAssets.begin(); iter != mAssets.end(); iter++) + { + auto temp = iter->second->GetFileLocation().remove_filename(); + if (temp == dir) + { + container.push_back(iter->second); + } + } + } + EditorAsset* ContentManager::GetAsset(uint64_t id) { auto iter = mAssets.find(id); diff --git a/src/run_modes/editor/contents/content_manager.h b/src/run_modes/editor/contents/content_manager.h index 9e3bdde..b1c9abf 100644 --- a/src/run_modes/editor/contents/content_manager.h +++ b/src/run_modes/editor/contents/content_manager.h @@ -38,8 +38,10 @@ namespace lunarium { namespace editor void GetAllAssetIDs(std::vector& container) const; void GetAllAssetsByType(std::vector& container, AssetType type) const; + void GetAllAssetsInDirectory(std::vector& container, std::filesystem::path dir); // dir should be relative to the asset dir EditorAsset* GetAsset(uint64_t id); + /// Add an asset that was generated by the editor (like tile maps or scripts) [[nodiscard]] OpRes AddGeneratedAsset(EditorAsset* asset, uint64_t& id); diff --git a/src/run_modes/editor/editor.cpp b/src/run_modes/editor/editor.cpp index e6a5011..317405d 100644 --- a/src/run_modes/editor/editor.cpp +++ b/src/run_modes/editor/editor.cpp @@ -68,17 +68,11 @@ namespace editor return res; } - mPanelManager.AddPanel(new AssetBrowser(""), mPanels.AssetBrowser).LogIfFailed(LogCat); + mPanelManager.AddPanel(new AssetBrowser("", this), mPanels.AssetBrowser).LogIfFailed(LogCat); mPanelManager.AddPanel(new WorldTree(), mPanels.WorldTree).LogIfFailed(LogCat); mPanelManager.AddPanel(new WorldView(), mPanels.WorldView).LogIfFailed(LogCat); mPanelManager.AddPanel(new PropertiesView(), mPanels.PropertiesView).LogIfFailed(LogCat); - // TEST ENTITY - mTestWorld = new lunarium::World; - mTestEntity = mTestWorld->CreateEntity(); - mTestWorld->GetEntity(mTestEntity)->AddComponent(); - ((PropertiesView*)mPanelManager.GetPanel(mPanels.PropertiesView))->SetSelection(mTestWorld->GetEntity(mTestEntity)); - return OpRes::OK(); } diff --git a/src/run_modes/editor/editor.h b/src/run_modes/editor/editor.h index f634f94..d137e65 100644 --- a/src/run_modes/editor/editor.h +++ b/src/run_modes/editor/editor.h @@ -52,6 +52,8 @@ namespace lunarium { namespace editor unsigned int GetNextWindowID(); bool IsToolOpen(ToolType type) const; + void ChangeWorld(lunarium::World* pWorld); + std::filesystem::path GetAssetBrowserLocation(); Project* GetProject(); @@ -61,7 +63,7 @@ namespace lunarium { namespace editor private: // Data PanelManager mPanelManager; - lunarium::World* mTestWorld; + lunarium::World* mWorld; Project mProject; // Tools @@ -89,7 +91,6 @@ namespace lunarium { namespace editor bool mDoSaveAs; // TEST DATA - LUUID mTestEntity; private: // HELPERS diff --git a/src/run_modes/editor/panels/asset_browser.cpp b/src/run_modes/editor/panels/asset_browser.cpp index 8d2c402..8c083dc 100644 --- a/src/run_modes/editor/panels/asset_browser.cpp +++ b/src/run_modes/editor/panels/asset_browser.cpp @@ -9,14 +9,17 @@ #include "asset_browser.h" #include #include +#include +#include +#include namespace lunarium { namespace editor { - AssetBrowser::AssetBrowser(std::filesystem::path dir) + AssetBrowser::AssetBrowser(std::filesystem::path dir, Editor* pEditor) : Panel("Asset Browser", gui::PanelDockZone::DDZ_BOTTOM, true), - mAssetDirectory(dir), mTreeRoot(nullptr), mSelectedNode(nullptr) + mAssetDirectory(dir), mTreeRoot(nullptr), mSelectedNode(nullptr), mpEditor(pEditor) { mTreeRoot = ReloadAssets(mAssetDirectory); mSelectedNode = mTreeRoot; @@ -72,15 +75,44 @@ namespace editor // Display each file in a row - for(auto const& dir_entry: std::filesystem::directory_iterator{mSelectedDir}) + // for(auto const& dir_entry: std::filesystem::directory_iterator{mSelectedDir}) + // { + // if (!std::filesystem::is_directory(dir_entry)) + // { + // // TODO: Turn into a table with file properties as columns + // ImGui::TextWrapped(dir_entry.path().filename().string().c_str()); + + // } + // } + + std::filesystem::path selected_final = mpEditor->GetProject()->MakeRelativeToAssets(mSelectedDir); + std::vector assets; + ContentManager::GetInstance().GetAllAssetsInDirectory(assets, selected_final); + + for (auto iter = assets.begin(); iter != assets.end(); iter++) { - if (!std::filesystem::is_directory(dir_entry)) + if (ImGui::Selectable((*iter)->GetFileLocation().stem().string().c_str())) { - // TODO: Turn into a table with file properties as columns - ImGui::TextWrapped(dir_entry.path().filename().string().c_str()); + // TODO: Show properties if this is new selection (meaning wasn't selected last frame) + Logger::Info(Editor::LogCat, "Asset selected. Properties shold show in the PropertiesView Panel"); + } + if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left)) + { + // TODO: Open relevant editor + Logger::Info(Editor::LogCat, "Asset double clicked on. Relevant editor should open!"); } } + + if (ImGui::BeginPopupContextWindow(0, 1, false)) + { + if (ImGui::Button("Click Me!")) + { + Logger::Info(Editor::LogCat, "Context menu button clicked!"); + } + + ImGui::EndPopup(); + } } } ImGui::EndChild(); diff --git a/src/run_modes/editor/panels/asset_browser.h b/src/run_modes/editor/panels/asset_browser.h index 755a512..8ff2888 100644 --- a/src/run_modes/editor/panels/asset_browser.h +++ b/src/run_modes/editor/panels/asset_browser.h @@ -10,6 +10,7 @@ #define ASSET_BROWSER_H_ #include +#include #include #include @@ -21,7 +22,7 @@ namespace editor class AssetBrowser : public gui::Panel { public: - AssetBrowser(std::filesystem::path dir); + AssetBrowser(std::filesystem::path dir, Editor* pEditor); void SetAssetDirectory(std::filesystem::path dir); @@ -42,6 +43,7 @@ namespace editor std::filesystem::path mSelectedDir; Node* mTreeRoot; Node* mSelectedNode; + Editor* mpEditor; private: Node* ReloadAssets(std::filesystem::path dir); diff --git a/src/run_modes/editor/panels/properties_view.cpp b/src/run_modes/editor/panels/properties_view.cpp index 50eaad9..09dd97e 100644 --- a/src/run_modes/editor/panels/properties_view.cpp +++ b/src/run_modes/editor/panels/properties_view.cpp @@ -17,7 +17,7 @@ namespace lunarium { namespace editor { PropertiesView::PropertiesView() - : Panel("Properties", gui::PanelDockZone::DDZ_RIGHT, true) + : Panel("Properties", gui::PanelDockZone::DDZ_RIGHT, true), mpSelectedAsset(nullptr), mpSelectedEntity(nullptr) { } diff --git a/src/run_modes/editor/panels/world_tree.cpp b/src/run_modes/editor/panels/world_tree.cpp index 3b21527..03f3e22 100644 --- a/src/run_modes/editor/panels/world_tree.cpp +++ b/src/run_modes/editor/panels/world_tree.cpp @@ -49,6 +49,12 @@ namespace editor ImGui::TreePop(); } + if (ImGui::BeginPopupContextWindow(0, ImGuiPopupFlags_MouseButtonRight)) + { + + ImGui::EndPopup(); + } + ImGui::End(); return true; } diff --git a/src/world/world.cpp b/src/world/world.cpp index 878a416..c7b780a 100644 --- a/src/world/world.cpp +++ b/src/world/world.cpp @@ -55,16 +55,6 @@ namespace lunarium } - // void World::AddGameObject(GameObject* pObj) - // { - - // } - - // bool World::RemoveGameObject(GameObject* pObj) - // { - - // } - void World::SetWorldScript(Script* pScript) { @@ -85,6 +75,12 @@ namespace lunarium { //Logger::Error(LogCategory::GAME_SYSTEM, "World::CreateEntity not implemented!"); Entity* new_ent = new Entity(*this); + + if (mEntities.find(new_ent->GetUUID()) != mEntities.end()) + { + Logger::Warn(LogCategory::GAME_SYSTEM, "UUID collision when creating new entity! UUID: %d", new_ent->GetUUID()); + } + mEntities[new_ent->GetUUID()] = new_ent; return new_ent->GetUUID(); } @@ -92,6 +88,10 @@ namespace lunarium Entity* World::GetEntity(LUUID id) { + if (mEntities.find(id) == mEntities.end()) + { + Logger::Warn(LogCategory::GAME_SYSTEM, "Entity with id: %d not found.", id); + } return mEntities[id]; } diff --git a/test_data/imgui.ini b/test_data/imgui.ini index 78b420f..60c8ead 100644 --- a/test_data/imgui.ini +++ b/test_data/imgui.ini @@ -1,3 +1,10 @@ +[Window][Asset Browser] +Pos=0,546 +Size=1280,174 +Collapsed=0 +DockId=0x00000003,0 +ClassId=0x00000001 + [Window][World Tree] Pos=0,24 Size=204,520 @@ -12,13 +19,6 @@ Collapsed=0 DockId=0x00000008,0 ClassId=0x00000001 -[Window][Asset Browser] -Pos=0,546 -Size=1280,174 -Collapsed=0 -DockId=0x00000003,0 -ClassId=0x00000001 - [Window][Properties] Pos=1024,24 Size=256,520 @@ -36,44 +36,55 @@ Pos=60,60 Size=400,400 Collapsed=0 +[Window][File Browser] +ViewportPos=526,598 +ViewportId=0x2389D759 +Size=600,400 +Collapsed=0 + [Window][Map Editor] -ViewportPos=416,108 +ViewportPos=622,126 ViewportId=0xDAA48CA2 -Size=1290,772 +Size=1056,762 Collapsed=0 [Window][Map Canvas] -ViewportPos=416,108 +ViewportPos=622,126 ViewportId=0xDAA48CA2 Pos=8,56 -Size=943,708 +Size=830,698 Collapsed=0 DockId=0x0000000E,0 -ClassId=0x000000C3 +ClassId=0x00000045 [Window][Tile Set View] -ViewportPos=416,108 +ViewportPos=622,126 ViewportId=0xDAA48CA2 -Pos=953,56 -Size=329,708 +Pos=840,56 +Size=208,698 Collapsed=0 DockId=0x0000000C,0 -ClassId=0x000000C3 +ClassId=0x00000045 -[Window][File Browser] -Pos=150,80 -Size=600,400 +[Window][DockSpaceViewport_11111111] +Size=1280,720 +Collapsed=0 + +[Window][Dear ImGui Demo] +ViewportPos=172,128 +ViewportId=0xE927CF2F +Size=550,680 Collapsed=0 [Docking][Data] -DockSpace ID=0x27CF68A1 Pos=424,164 Size=1274,708 Split=Y - DockNode ID=0x00000009 Parent=0x27CF68A1 SizeRef=1280,538 Split=X - DockNode ID=0x0000000B Parent=0x00000009 SizeRef=943,538 Split=X - DockNode ID=0x0000000D Parent=0x0000000B SizeRef=204,538 - DockNode ID=0x0000000E Parent=0x0000000B SizeRef=816,538 CentralNode=1 Selected=0xB3CA1100 - DockNode ID=0x0000000C Parent=0x00000009 SizeRef=329,538 Selected=0xB093288C - DockNode ID=0x0000000A Parent=0x27CF68A1 SizeRef=1280,180 -DockSpace ID=0x2F8DD699 Window=0xBEB76114 Pos=244,144 Size=1280,696 Split=Y +DockSpace ID=0x27CF68A1 Pos=630,182 Size=1040,698 Split=Y + DockNode ID=0x00000009 Parent=0x27CF68A1 SizeRef=1056,570 Split=X + DockNode ID=0x0000000B Parent=0x00000009 SizeRef=843,570 Split=X + DockNode ID=0x0000000D Parent=0x0000000B SizeRef=168,570 + DockNode ID=0x0000000E Parent=0x0000000B SizeRef=673,570 CentralNode=1 Selected=0xB3CA1100 + DockNode ID=0x0000000C Parent=0x00000009 SizeRef=211,570 Selected=0xB093288C + DockNode ID=0x0000000A Parent=0x27CF68A1 SizeRef=1056,190 +DockSpace ID=0x2F8DD699 Window=0xBEB76114 Pos=476,90 Size=1280,696 Split=Y DockNode ID=0x00000001 Parent=0x2F8DD699 SizeRef=1280,538 Split=X DockNode ID=0x00000005 Parent=0x00000001 SizeRef=1022,538 Split=X DockNode ID=0x00000007 Parent=0x00000005 SizeRef=204,538 Selected=0xFD1747F8