From 73f2e06541e15a470eb136c46bcd1196fd890e4a Mon Sep 17 00:00:00 2001 From: Joey Pollack Date: Wed, 7 Sep 2022 15:46:08 -0400 Subject: [PATCH] Adds new events to the editor --- docs/tasks/Bugs.todo | 2 +- docs/tasks/core.todo | 32 ++++++++++--------- src/renderer/texture.cpp | 6 ++++ src/run_modes/editor/component_guis.cpp | 6 ++-- src/run_modes/editor/contents/World.cpp | 4 +-- .../editor/contents/content_manager.cpp | 12 ++++++- .../editor/contents/content_manager.h | 4 +++ src/run_modes/editor/contents/editor_asset.h | 3 +- src/run_modes/editor/contents/tile_map.cpp | 11 +++++-- src/run_modes/editor/contents/tile_map.h | 2 +- src/run_modes/editor/contents/tile_set.cpp | 5 ++- src/run_modes/editor/contents/tile_set.h | 2 +- src/run_modes/editor/contents/world.h | 2 +- src/run_modes/editor/editor.cpp | 29 ++++++++++++++--- src/run_modes/editor/editor.h | 2 ++ src/run_modes/editor/panels/about.cpp | 7 ++-- .../editor/panels/properties_view.cpp | 10 ++++-- src/run_modes/editor/panels/properties_view.h | 4 ++- .../editor/tools/map_editor/map_editor.cpp | 23 ++++++++++++- .../editor/tools/map_editor/map_editor.h | 1 + .../tools/map_editor/panels/tile_set_view.cpp | 1 + 21 files changed, 126 insertions(+), 42 deletions(-) diff --git a/docs/tasks/Bugs.todo b/docs/tasks/Bugs.todo index 8372ec1..dc75f6d 100644 --- a/docs/tasks/Bugs.todo +++ b/docs/tasks/Bugs.todo @@ -11,5 +11,5 @@ High Importance: Medium Importance: ☐ Lines do not rotate correctly ☐ Map Editor can be docked into the main window. The window IDs should prevent this. - ☐ Map Editor does not grab tile sets if the Map Editor is opened before a project is loaded + ✔ Map Editor does not grab tile sets if the Map Editor is opened before a project is loaded @done(22-09-07 15:00) ✔ Map Editor paints the wrong tiles when scrolling with the middle mouse button (this may have to do with the parent window having the scroll bar) @done(22-04-07 13:50) \ No newline at end of file diff --git a/docs/tasks/core.todo b/docs/tasks/core.todo index 499abfe..665879a 100644 --- a/docs/tasks/core.todo +++ b/docs/tasks/core.todo @@ -36,17 +36,17 @@ Core: Graphics: Re-write the renderer: - ☐ Implement BeginScene and EndScene - these are called from the World OnRender - ☐ Organize Buffers - ☐ Better FrameBuffer system - ☐ Texture class - Does NOT load from files - takes raw data to construct - ☐ Add view matrix to the shader @critical - ☐ Remove projection matrix from the renderer internals @critical - ☐ Move the openGL reference out of the Image class (OpenGL ID) and make the ID more generic - ☐ Add layer to interface API for setting the Images ID in a generic way - ☐ Implement batch rendering @high - ☐ Allow vertices to be submitted before rendering - ☐ Add texture sampler id to the vertex layout + ✔ Implement BeginScene and EndScene - these are called from the World OnRender @done(22-09-07 14:47) + ✔ Organize Buffers @done(22-09-07 14:47) + ✔ Better FrameBuffer system @done(22-09-07 14:47) + ✔ Texture class - Does NOT load from files - takes raw data to construct @done(22-09-07 14:47) + ✔ Add view matrix to the shader @critical @done(22-09-07 14:47) + ✔ Remove projection matrix from the renderer internals @critical @done(22-09-07 14:47) + ✔ Move the openGL reference out of the Image class (OpenGL ID) and make the ID more generic @done(22-09-07 14:47) + ✔ Add layer to interface API for setting the Images ID in a generic way @done(22-09-07 14:47) + ✔ Implement batch rendering @high @done(22-09-07 14:45) + ✔ Allow vertices to be submitted before rendering @done(22-09-07 14:45) + ✔ Add texture sampler id to the vertex layout @done(22-09-07 14:45) ✔ Decide on a font/text rendering system @done (9/7/2021, 1:39:53 PM) ✔ Add FreeType to the project @done (9/7/2021, 2:23:13 PM) ✔ Add a new class for font loading/management and text rendering @done (9/7/2021, 3:57:08 PM) @@ -56,7 +56,7 @@ Graphics: ✔ Adjust the font loading code to use the binary file buffer instead of ifstream @done (9/17/2021, 6:11:06 PM) ✔ Find a way to add rotation to shapes and images @done (10/29/2021, 7:35:14 PM) ✔ Add a DrawPolygon method that takes vertices and draws arbirary shapes @done (10/29/2021, 6:24:14 PM) - ☐ Allow DrawPolygon to add a texture to the polygon @low + ✘ Allow DrawPolygon to add a texture to the polygon @low @cancelled(22-09-07 14:48) ✔ Refactor the drawing code to allow for rotation with cleaner code @high @done (10/29/2021, 8:36:24 PM) ✔ Test rotation of images @done (11/1/2021, 2:11:13 PM) ✔ Fix line rotation @low @done (2/8/2022, 4:39:25 PM) @@ -64,8 +64,8 @@ Graphics: ✔ Allow an image size to be passed in for rendering to an image @high @done (2/3/2022, 4:07:33 PM) GUI: - ☐ Improve the GUI API! - ☐ Implement a better way to handle popup windows and context menus + ✔ Improve the GUI API! @done(22-09-07 14:48) + ✔ Implement a better way to handle popup windows and context menus @done(22-09-07 14:48) ✔ Dear ImGui class with basic initialization @done (9/10/2021, 1:42:19 PM) ✔ Debug log window @done (9/10/2021, 4:44:48 PM) ✔ Add key to show debug log window @done (9/13/2021, 6:47:44 PM) @@ -109,7 +109,9 @@ ECS: Components: ✔ Tag @done(22-06-23 15:49) - ☐ Transform + ✔ Transform @done(22-09-07 14:49) + ✔ Velocity @done(22-09-07 14:49) + ✔ Camera @done(22-09-07 14:49) ☐ SpriteRenderer ☐ Animation Controller ☐ Script diff --git a/src/renderer/texture.cpp b/src/renderer/texture.cpp index 89f61c9..4ee8088 100644 --- a/src/renderer/texture.cpp +++ b/src/renderer/texture.cpp @@ -65,8 +65,14 @@ namespace lunarium void Texture::Destroy(Texture** ppTex) { + if (!(*ppTex)) + { + return; + } + (*ppTex)->Unbind(); glDeleteTextures(1, &(*ppTex)->mGLID); + delete (*ppTex); (*ppTex) = nullptr; } diff --git a/src/run_modes/editor/component_guis.cpp b/src/run_modes/editor/component_guis.cpp index 30956bd..78eb144 100644 --- a/src/run_modes/editor/component_guis.cpp +++ b/src/run_modes/editor/component_guis.cpp @@ -83,10 +83,8 @@ namespace lunarium { namespace editor comp.Camera.SetPosition(pos); } - float rot = comp.Camera.GetRotation(); - ImGui::Text("Rotation Angle: "); - ImGui::SameLine(); - if (ImGui::DragFloat("##rotation", &rot, 0.5f, 0.0f, 360.0f)) + float rot = comp.Camera.GetRotation(); + if (ImGuiExt::FloatControl("Rotation", rot, 0.0f, 85.0f)) { comp.Camera.SetRotation(rot); } diff --git a/src/run_modes/editor/contents/World.cpp b/src/run_modes/editor/contents/World.cpp index 9957dfe..aa776e1 100644 --- a/src/run_modes/editor/contents/World.cpp +++ b/src/run_modes/editor/contents/World.cpp @@ -119,9 +119,9 @@ namespace lunarium { namespace editor return nlohmann::ordered_json(); } - void World::DrawProperties() + bool World::DrawProperties() { - + return false; } }} \ No newline at end of file diff --git a/src/run_modes/editor/contents/content_manager.cpp b/src/run_modes/editor/contents/content_manager.cpp index fd7ea8e..1cb7a9b 100644 --- a/src/run_modes/editor/contents/content_manager.cpp +++ b/src/run_modes/editor/contents/content_manager.cpp @@ -26,7 +26,7 @@ namespace lunarium { namespace editor ContentManager* ContentManager::mpInstance = nullptr; ContentManager::ContentManager() - : mpProject(nullptr), mNextID(0) + : mpProject(nullptr), mNextID(0), mpEditor(nullptr) { } @@ -50,6 +50,12 @@ namespace lunarium { namespace editor } } + + void ContentManager::SetEditor(Editor* pEditor) + { + mpEditor = pEditor; + } + OpRes ContentManager::Load(Project* project) { Unload(); @@ -149,6 +155,7 @@ namespace lunarium { namespace editor } mAssets[pAsset->mID] = pAsset; + mpEditor->OnNewAsset(pAsset); } return OpRes::OK(); @@ -273,6 +280,7 @@ namespace lunarium { namespace editor id = asset->mID; Save().LogIfFailed(Editor::LogCat, "Asset was created"); + mpEditor->OnNewAsset(asset); return OpRes::OK(); } @@ -309,6 +317,8 @@ namespace lunarium { namespace editor mAssets[pAsset->mID] = pAsset; id = pAsset->mID; + mpEditor->OnNewAsset(pAsset); + return OpRes::OK(); } diff --git a/src/run_modes/editor/contents/content_manager.h b/src/run_modes/editor/contents/content_manager.h index 4aeb733..c6b73a1 100644 --- a/src/run_modes/editor/contents/content_manager.h +++ b/src/run_modes/editor/contents/content_manager.h @@ -24,6 +24,7 @@ namespace lunarium { namespace editor class Project; class EditorAsset; + class Editor; class ContentManager { @@ -32,6 +33,8 @@ namespace lunarium { namespace editor static ContentManager& GetInstance(); static void FreeInstance(); + void SetEditor(Editor* pEditor); + [[nodiscard]] OpRes Load(Project* project); [[nodiscard]] OpRes Save(); void Unload(); @@ -55,6 +58,7 @@ namespace lunarium { namespace editor private: static ContentManager* mpInstance; + Editor* mpEditor; Project* mpProject; std::filesystem::path mContentFile; std::map mAssets; diff --git a/src/run_modes/editor/contents/editor_asset.h b/src/run_modes/editor/contents/editor_asset.h index 455f2cf..2e00e4d 100644 --- a/src/run_modes/editor/contents/editor_asset.h +++ b/src/run_modes/editor/contents/editor_asset.h @@ -31,7 +31,8 @@ namespace lunarium { namespace editor [[nodiscard]] virtual OpRes LoadRawFile() = 0; - virtual void DrawProperties() = 0; + /// @return returns true if the properties were updated + virtual bool DrawProperties() = 0; // [[nodiscard]] virtual OpRes LoadFromJSON(nlohmann::json& node) = 0; diff --git a/src/run_modes/editor/contents/tile_map.cpp b/src/run_modes/editor/contents/tile_map.cpp index fc91a7a..9621d03 100644 --- a/src/run_modes/editor/contents/tile_map.cpp +++ b/src/run_modes/editor/contents/tile_map.cpp @@ -153,6 +153,7 @@ namespace lunarium { namespace editor Sizei map_size_pixels = { mTileSize.Width * mSizeInTiles.Width, mTileSize.Height * mSizeInTiles.Height }; // Draw Map + std::vector warned_ids; for (int i = 0; i < mSizeInTiles.Width; i++) { for (int j = 0; j < mSizeInTiles.Height; j++) @@ -177,6 +178,12 @@ namespace lunarium { namespace editor } TileSet* set = mTileSets[id]; + + if (set->GetTileSize() != mTileSize) + { + Logger::Warn(Editor::LogCat, "Tile Map contains tiles from a differently sized Tile Set with id: %d", id); + } + Rectangle dest = Rectangle::MakeFromTopLeft(i * mTileSize.Width, j * mTileSize.Height, mTileSize.Width, mTileSize.Height); Rectangle src = set->GetTileRect(mpMap[i][j].TileIndex); @@ -204,8 +211,8 @@ namespace lunarium { namespace editor } - void TileMap::DrawDebugGUI() + bool TileMap::DrawProperties() { - + return false; } }} diff --git a/src/run_modes/editor/contents/tile_map.h b/src/run_modes/editor/contents/tile_map.h index ddaab40..3216326 100644 --- a/src/run_modes/editor/contents/tile_map.h +++ b/src/run_modes/editor/contents/tile_map.h @@ -42,7 +42,7 @@ namespace lunarium { namespace editor // Call during render to texture phase void Render(lunarium::Renderer2D* g); - void DrawProperties(); + bool DrawProperties(); void DrawDebugGUI(); diff --git a/src/run_modes/editor/contents/tile_set.cpp b/src/run_modes/editor/contents/tile_set.cpp index 9338cfe..4891b65 100644 --- a/src/run_modes/editor/contents/tile_set.cpp +++ b/src/run_modes/editor/contents/tile_set.cpp @@ -173,7 +173,7 @@ namespace lunarium { namespace editor } - void TileSet::DrawProperties() + bool TileSet::DrawProperties() { int size[2] = { mTileSize.Width, mTileSize.Height }; ImGui::Text("Tile Size: "); @@ -181,6 +181,9 @@ namespace lunarium { namespace editor if (ImGui::DragInt2("##Tile Size", size)) { SetTileSize({size[0], size[1]}); + return true; } + + return false; } }} \ No newline at end of file diff --git a/src/run_modes/editor/contents/tile_set.h b/src/run_modes/editor/contents/tile_set.h index 50dc469..0cb6add 100644 --- a/src/run_modes/editor/contents/tile_set.h +++ b/src/run_modes/editor/contents/tile_set.h @@ -28,7 +28,7 @@ namespace lunarium { namespace editor OpRes Deserialize(nlohmann::ordered_json& node); bool IsValidNode(nlohmann::ordered_json& node); nlohmann::ordered_json AsJSON(); - void DrawProperties(); + bool DrawProperties(); void SetTileSetID(int id); int GetTileSetID() const; diff --git a/src/run_modes/editor/contents/world.h b/src/run_modes/editor/contents/world.h index 3ece45a..2ce72c6 100644 --- a/src/run_modes/editor/contents/world.h +++ b/src/run_modes/editor/contents/world.h @@ -34,7 +34,7 @@ namespace lunarium { namespace editor [[nodiscord]] virtual bool IsValidNode(nlohmann::ordered_json& node); [[nodiscard]] virtual nlohmann::ordered_json AsJSON(); - void DrawProperties(); + bool DrawProperties(); lunarium::World* GetWorld(); void UnloadWorld(); diff --git a/src/run_modes/editor/editor.cpp b/src/run_modes/editor/editor.cpp index dafbaae..5e9e48b 100644 --- a/src/run_modes/editor/editor.cpp +++ b/src/run_modes/editor/editor.cpp @@ -7,6 +7,7 @@ ******************************************************************************/ #include "editor.h" +#include "editor/contents/editor_asset.h" #include #include @@ -27,7 +28,6 @@ // Tools #include "tools/map_editor/map_editor.h" -#include // Game objects #include @@ -61,6 +61,8 @@ namespace editor ImGui::GetIO().ConfigWindowsMoveFromTitleBarOnly = true; GUI::GetInstance().SetStyle(GuiStyle::STYLE_CHARCOAL); + ContentManager::GetInstance().SetEditor(this); + // Init editor panels mAboutPanel.SetOpen(false); @@ -73,7 +75,7 @@ namespace editor mPanelManager.AddPanel(new AssetBrowser("", this), mPanels.AssetBrowser).LogIfFailed(LogCat); mPanelManager.AddPanel(new WorldTree(this), mPanels.WorldTree).LogIfFailed(LogCat); mPanelManager.AddPanel(new WorldView(), mPanels.WorldView).LogIfFailed(LogCat); - mPanelManager.AddPanel(new PropertiesView(), mPanels.PropertiesView).LogIfFailed(LogCat); + mPanelManager.AddPanel(new PropertiesView(this), mPanels.PropertiesView).LogIfFailed(LogCat); return OpRes::OK(); } @@ -136,7 +138,7 @@ namespace editor if (mAboutPanel.IsOpen()) { - mAboutPanel.DoFrame(); + mAboutPanel.OnUIRender(); } } @@ -278,8 +280,8 @@ namespace editor ((AssetBrowser*)mPanelManager.GetPanel(mPanels.AssetBrowser))-> SetAssetDirectory(outPath.parent_path() / std::filesystem::path("contents/assets")); } - + if (mpMapEditor) mpMapEditor->ReloadTileSets(); } else if (result == FileSystem::DialogResult::CANCEL) { @@ -472,5 +474,24 @@ namespace editor { ((PropertiesView*)mPanelManager.GetPanel(mPanels.PropertiesView))->SetSelection(pAsset); } + + void Editor::OnNewAsset(EditorAsset* pAsset) + { + if (pAsset->GetType() == AssetType::EATYPE_TILE_SET) + { + if (mpMapEditor) + { + mpMapEditor->ReloadTileSets(); + } + } + } + + void Editor::OnAssetUpdate(EditorAsset* pAsset) + { + if (pAsset->GetType() == AssetType::EATYPE_TILE_SET) + { + if (mpMapEditor) mpMapEditor->ReloadTileSets(); + } + } } } \ No newline at end of file diff --git a/src/run_modes/editor/editor.h b/src/run_modes/editor/editor.h index 13b75c1..29f7c81 100644 --- a/src/run_modes/editor/editor.h +++ b/src/run_modes/editor/editor.h @@ -59,6 +59,8 @@ namespace lunarium { namespace editor void OnEntitySelect(lunarium::Entity* pEnt); void ChangeWorld(lunarium::World* pWorld); void OnAssetSelected(EditorAsset* pAsset); + void OnNewAsset(EditorAsset* pAsset); + void OnAssetUpdate(EditorAsset* pAsset); private: Editor(const Editor&) = delete; diff --git a/src/run_modes/editor/panels/about.cpp b/src/run_modes/editor/panels/about.cpp index 64334a9..20c0b58 100644 --- a/src/run_modes/editor/panels/about.cpp +++ b/src/run_modes/editor/panels/about.cpp @@ -16,18 +16,19 @@ namespace lunarium namespace editor { AboutPanel::AboutPanel() - : Panel("About", PanelDockZone::DDZ_NONE, false, (int)ImGuiWindowFlags_NoCollapse) + : Panel("About", PanelDockZone::DDZ_NONE, false, (ImGuiWindowFlags)ImGuiWindowFlags_NoCollapse + | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoDocking | ImGuiWindowFlags_NoResize) { } void AboutPanel::PreBegin() { - ImGui::SetNextWindowSize(ImVec2(300, ImGui::GetFrameHeight() * 20)); + ImGui::SetNextWindowSize(ImVec2(200, ImGui::GetFrameHeight() * 10)); } void AboutPanel::DoFrame() { - ImGui::TextWrapped("Lunarium Editor Version %s - Written by Joey Pollack", Version::GetVersion().ToString().c_str()); + ImGui::TextWrapped("Lunarium Editor Version%s\n\t- Written by Joey Pollack", Version::GetVersion().ToString().c_str()); } } } \ No newline at end of file diff --git a/src/run_modes/editor/panels/properties_view.cpp b/src/run_modes/editor/panels/properties_view.cpp index b47cb80..ab8be25 100644 --- a/src/run_modes/editor/panels/properties_view.cpp +++ b/src/run_modes/editor/panels/properties_view.cpp @@ -8,6 +8,7 @@ ******************************************************************************/ #include "properties_view.h" +#include "editor/editor.h" #include #include #include @@ -32,9 +33,9 @@ if (ImGui::Selectable(str_name)){\ namespace lunarium { namespace editor { - PropertiesView::PropertiesView() + PropertiesView::PropertiesView(Editor* pEditor) : Panel("Properties", PanelDockZone::DDZ_RIGHT, true, ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoScrollbar), - mpSelectedAsset(nullptr), mpSelectedEntity(nullptr), mIsLocked(false) + mpSelectedAsset(nullptr), mpSelectedEntity(nullptr), mIsLocked(false), mpEditor(pEditor) { // ADD COMPONENT POPUP AddPopup(Popup::ADD_COMP, "Add Component", [](Panel* p) @@ -183,7 +184,10 @@ namespace lunarium { namespace editor { if (mpSelectedAsset) { - mpSelectedAsset->DrawProperties(); + if (mpSelectedAsset->DrawProperties()) + { + mpEditor->OnAssetUpdate(mpSelectedAsset); + } } } diff --git a/src/run_modes/editor/panels/properties_view.h b/src/run_modes/editor/panels/properties_view.h index 5bc1d02..5fe9e61 100644 --- a/src/run_modes/editor/panels/properties_view.h +++ b/src/run_modes/editor/panels/properties_view.h @@ -20,11 +20,12 @@ namespace editor { class CustromProperty; class EditorAsset; + class Editor; class PropertiesView : public Panel { public: - PropertiesView(); + PropertiesView(Editor* pEditor); void SetSelection(Entity* pEntity); void SetSelection(EditorAsset* pAsset); @@ -33,6 +34,7 @@ namespace editor private: bool mIsLocked; + Editor* mpEditor; std::vector mCustomProps; // Only ONE of these should be set at a time diff --git a/src/run_modes/editor/tools/map_editor/map_editor.cpp b/src/run_modes/editor/tools/map_editor/map_editor.cpp index bf46f0c..475cedb 100644 --- a/src/run_modes/editor/tools/map_editor/map_editor.cpp +++ b/src/run_modes/editor/tools/map_editor/map_editor.cpp @@ -53,6 +53,13 @@ namespace lunarium { namespace editor Open(); + ReloadTileSets(); + + return OpRes::OK(); + } + + void MapEditor::ReloadTileSets() + { std::vector tile_sets; ContentManager::GetInstance().GetAllAssetsByType(tile_sets, AssetType::EATYPE_TILE_SET); for (int i = 0; i < tile_sets.size(); i++) @@ -62,7 +69,21 @@ namespace lunarium { namespace editor //((TileSetView*)mPanelManager.GetPanel(gui::PanelType::PT_TILE_SET_VIEW))->SetTileSet((TileSet*)tile_sets[i]); } - return OpRes::OK(); + if (mpMap) + { + // Add all tilesets that match the Map's tile size + ((TileSetView*)mPanelManager.GetPanel(mPanels.TileSetView))->ClearTileSets(); + for (auto iter = mTileSets.begin(); iter != mTileSets.end(); iter++) + { + if (iter->second->GetTileSize() == mpMap->GetTileSize()) + { + mpMap->AddTileSet(iter->second->GetTileSetID(), iter->second); + ((TileSetView*)mPanelManager.GetPanel(mPanels.TileSetView))->AddTileSet(iter->second); + } + } + + ((MapCanvas*)mPanelManager.GetPanel(mPanels.MapCanvas))->SetTileMap(mpMap); + } } void MapEditor::Shutdown() diff --git a/src/run_modes/editor/tools/map_editor/map_editor.h b/src/run_modes/editor/tools/map_editor/map_editor.h index 02d883c..251d5e5 100644 --- a/src/run_modes/editor/tools/map_editor/map_editor.h +++ b/src/run_modes/editor/tools/map_editor/map_editor.h @@ -49,6 +49,7 @@ namespace editor const std::map* GetTileSets() const; + void ReloadTileSets(); void ChangeSelectedTile(TileRef tile); diff --git a/src/run_modes/editor/tools/map_editor/panels/tile_set_view.cpp b/src/run_modes/editor/tools/map_editor/panels/tile_set_view.cpp index 2faef22..ad5ce20 100644 --- a/src/run_modes/editor/tools/map_editor/panels/tile_set_view.cpp +++ b/src/run_modes/editor/tools/map_editor/panels/tile_set_view.cpp @@ -168,6 +168,7 @@ namespace lunarium { namespace editor mTileSets.clear(); mpSelectedTileSet = nullptr; mSelectedTile = {-1, -1}; + Texture::Destroy(&mpViewImage); Invalidate(true); }