From 22fee91f157b787ef1eb5adec5834ad8abe2e303 Mon Sep 17 00:00:00 2001 From: Joeyrp Date: Wed, 16 Feb 2022 14:47:37 -0500 Subject: [PATCH] MapEditor design fleshed out but render code not yet tested --- src/core/types.cpp | 34 +++++- src/core/types.h | 17 ++- src/graphics/opengl/glGraphics.cpp | 16 +-- src/graphics/opengl/glText.cpp | 2 +- src/internal_libs/gui/panel_defs.h | 2 +- src/run_modes/editor/CMakeLists.txt | 2 + src/run_modes/editor/editor.cpp | 39 +++---- src/run_modes/editor/editor.h | 5 +- .../editor/tools/map_editor/map_editor.cpp | 4 +- .../tools/map_editor/panels/map_canvas.cpp | 55 ++++++---- .../tools/map_editor/panels/map_canvas.h | 8 +- .../tools/map_editor/panels/tile_set_view.cpp | 102 ++++++++++++++++++ .../tools/map_editor/panels/tile_set_view.h | 44 ++++++++ .../editor/tools/map_editor/tile_map.cpp | 60 ++++++++++- .../editor/tools/map_editor/tile_map.h | 10 +- .../editor/tools/map_editor/tile_set.cpp | 67 ++++++++++++ .../editor/tools/map_editor/tile_set.h | 40 +++++++ 17 files changed, 447 insertions(+), 60 deletions(-) create mode 100644 src/run_modes/editor/tools/map_editor/panels/tile_set_view.cpp create mode 100644 src/run_modes/editor/tools/map_editor/panels/tile_set_view.h create mode 100644 src/run_modes/editor/tools/map_editor/tile_set.cpp create mode 100644 src/run_modes/editor/tools/map_editor/tile_set.h diff --git a/src/core/types.cpp b/src/core/types.cpp index b6e9282..7c01bc4 100644 --- a/src/core/types.cpp +++ b/src/core/types.cpp @@ -15,17 +15,47 @@ namespace lunarium // COLOR //////////////////////////////////////////////////////////// Color::Color(float _r, float _g, float _b, float _a) - : Red(_r), Green(_g), Blue(_b), Alpha(_a) + : R(_r), G(_g), B(_b), A(_a) { } Color::Color() - : Red(0.0f), Green(0.0f), Blue(0.0f), Alpha(0.0f) + : R(0.0f), G(0.0f), B(0.0f), A(0.0f) { } + Color Color::White() + { + return Color(1.0f, 1.0f, 1.0f, 1.0f); + } + + Color Color::Black() + { + return Color(0.0f, 0.0f, 0.0f, 1.0f); + } + + Color Color::Red() + { + return Color(1.0f, 0.0f, 0.0f, 1.0f); + } + + Color Color::Green() + { + return Color(0.0f, 1.0f, 0.0f, 1.0f); + } + + Color Color::Blue() + { + return Color(0.0f, 0.0f, 1.0f, 1.0f); + } + + Color Color::Transparent() + { + return Color(0.0f, 0.0f, 0.0f, 0.0f); + } + //////////////////////////////////////////////////////////// // RECTANGLE //////////////////////////////////////////////////////////// diff --git a/src/core/types.h b/src/core/types.h index c013f57..6a15978 100644 --- a/src/core/types.h +++ b/src/core/types.h @@ -55,16 +55,25 @@ namespace lunarium struct { - float Red; - float Green; - float Blue; - float Alpha; + float R; + float G; + float B; + float A; }; }; Color(); Color(float _r, float _g, float _b, float _a = 1.0f); + + static Color White(); + static Color Black(); + static Color Red(); + static Color Green(); + static Color Blue(); + static Color Transparent(); }; + + ///////////////////////////////////////////////// diff --git a/src/graphics/opengl/glGraphics.cpp b/src/graphics/opengl/glGraphics.cpp index 8878459..642fa65 100644 --- a/src/graphics/opengl/glGraphics.cpp +++ b/src/graphics/opengl/glGraphics.cpp @@ -186,7 +186,7 @@ namespace lunarium void OglGraphics::SetClearColor(Color c) { mClearColor = c; - glClearColor(mClearColor.Red, mClearColor.Green, mClearColor.Blue, mClearColor.Alpha); + glClearColor(mClearColor.R, mClearColor.G, mClearColor.B, mClearColor.A); } Color OglGraphics::GetClearColor() const @@ -284,7 +284,7 @@ namespace lunarium glm::mat4 model = glm::mat4(1.0f); model = glm::rotate(model, angle, glm::vec3(0.0f, 0.0f, 1.0f)); mShapeShader.SetUniformMatrix("model", 1, glm::value_ptr(model)); - mShapeShader.SetUniformf("shapeColor", { color.Red, color.Green, color.Blue, color.Alpha }); + mShapeShader.SetUniformf("shapeColor", { color.R, color.G, color.B, color.A }); glBindVertexArray(mRectVAO); GLfloat lineVerts[6][4] = { @@ -312,7 +312,7 @@ namespace lunarium mShapeShader.SetUniformMatrix("projection", 1, glm::value_ptr(mProjection)); glm::mat4 model = glm::mat4(1.0f); mShapeShader.SetUniformMatrix("model", 1, glm::value_ptr(model)); - mShapeShader.SetUniformf("shapeColor", { color.Red, color.Green, color.Blue, color.Alpha }); + mShapeShader.SetUniformf("shapeColor", { color.R, color.G, color.B, color.A }); glBindVertexArray(mEllipseVAO); GLfloat points[360][2]; @@ -339,7 +339,7 @@ namespace lunarium mShapeShader.SetUniformMatrix("projection", 1, glm::value_ptr(mProjection)); glm::mat4 model = glm::mat4(1.0f); mShapeShader.SetUniformMatrix("model", 1, glm::value_ptr(model)); - mShapeShader.SetUniformf("shapeColor", { color.Red, color.Green, color.Blue, color.Alpha }); + mShapeShader.SetUniformf("shapeColor", { color.R, color.G, color.B, color.A }); glBindVertexArray(mEllipseVAO); GLfloat points[362][2]; @@ -369,7 +369,7 @@ namespace lunarium model = glm::translate(model, glm::vec3(position.x, position.y, 0.0f)); model = glm::rotate(model, angle, glm::vec3(0.0f, 0.0f, 1.0f)); mShapeShader.SetUniformMatrix("model", 1, glm::value_ptr(model)); - mShapeShader.SetUniformf("shapeColor", { color.Red, color.Green, color.Blue, color.Alpha }); + mShapeShader.SetUniformf("shapeColor", { color.R, color.G, color.B, color.A }); glBindVertexArray(mRectVAO); glBindBuffer(GL_ARRAY_BUFFER, mRectVBO); @@ -389,7 +389,7 @@ namespace lunarium model = glm::rotate(model, angle, glm::vec3(0.0f, 0.0f, 1.0f)); model = glm::scale(model, glm::vec3(box.HalfWidth * 2, box.HalfHeight * 2, 1.0f)); mShapeShader.SetUniformMatrix("model", 1, glm::value_ptr(model)); - mShapeShader.SetUniformf("shapeColor", { color.Red, color.Green, color.Blue, color.Alpha }); + mShapeShader.SetUniformf("shapeColor", { color.R, color.G, color.B, color.A }); glBindVertexArray(mRectVAO); GLfloat vertices[6][4] = { @@ -433,7 +433,7 @@ namespace lunarium model = glm::scale(model, glm::vec3(dimensions.HalfWidth * 2, dimensions.HalfHeight * 2, 1.0f)); model = glm::rotate(model, angle, glm::vec3(0.0f, 0.0f, 1.0f)); mShapeShader.SetUniformMatrix("model", 1, glm::value_ptr(model)); - mShapeShader.SetUniformf("shapeColor", { color.Red, color.Green, color.Blue, color.Alpha }); + mShapeShader.SetUniformf("shapeColor", { color.R, color.G, color.B, color.A }); glBindVertexArray(mRectVAO); GLfloat lineVerts[4][4] = { @@ -500,7 +500,7 @@ namespace lunarium mImageShader.SetUniformf("uvManip", { xScale, xOffset, yScale /** -1.0f*/, yOffset}); mImageShader.SetUniformMatrix("model", 1, glm::value_ptr(trans)); mImageShader.SetUniformMatrix("projection", 1, glm::value_ptr(mProjection)); - mImageShader.SetUniformf("spriteColor", { color.Red, color.Green, color.Blue, color.Alpha }); + mImageShader.SetUniformf("spriteColor", { color.R, color.G, color.B, color.A }); //} glActiveTexture(GL_TEXTURE0); diff --git a/src/graphics/opengl/glText.cpp b/src/graphics/opengl/glText.cpp index 873f83b..208ecc4 100644 --- a/src/graphics/opengl/glText.cpp +++ b/src/graphics/opengl/glText.cpp @@ -189,7 +189,7 @@ namespace lunarium // NOTE: String drawing code based on code from: https://learnopengl.com/In-Practice/Text-Rendering mTextShader.MakeActive(); mTextShader.SetUniformMatrix("projection", 1, glm::value_ptr(projection)); - mTextShader.SetUniformf("textColor", { color.Red, color.Green, color.Blue, color.Alpha }); + mTextShader.SetUniformf("textColor", { color.R, color.G, color.B, color.A }); glActiveTexture(GL_TEXTURE0); glBindVertexArray(mTextVAO); diff --git a/src/internal_libs/gui/panel_defs.h b/src/internal_libs/gui/panel_defs.h index 63c3d1b..afca703 100644 --- a/src/internal_libs/gui/panel_defs.h +++ b/src/internal_libs/gui/panel_defs.h @@ -27,7 +27,7 @@ namespace lunarium { namespace gui // Map Editor PT_MAP_CANVAS, - PT_TILE_PALLET, + PT_TILE_SET_VIEW, PT_TILE_PROPERTIES, PT_MAP_PROPERTIES, PT_MAP_PREVIEW, diff --git a/src/run_modes/editor/CMakeLists.txt b/src/run_modes/editor/CMakeLists.txt index fe8366c..87ca19c 100644 --- a/src/run_modes/editor/CMakeLists.txt +++ b/src/run_modes/editor/CMakeLists.txt @@ -11,7 +11,9 @@ set(EDITOR_SRC "panels/propertiesView.cpp" "tools/map_editor/map_editor.cpp" "tools/map_editor/tile_map.cpp" +"tools/map_editor/tile_set.cpp" "tools/map_editor/panels/map_canvas.cpp" +"tools/map_editor/panels/tile_set_view.cpp" ) add_library(editor ${EDITOR_SRC}) diff --git a/src/run_modes/editor/editor.cpp b/src/run_modes/editor/editor.cpp index 71c1d66..be6d2a6 100644 --- a/src/run_modes/editor/editor.cpp +++ b/src/run_modes/editor/editor.cpp @@ -27,22 +27,23 @@ using namespace lunarium::gui; // ERROR LOG MACRO -#define LOG_ERR_AND_RETURN(result) { if (Failed(result)) { Logger::Log(mLogCat, LogLevel::ERROR, result.Description.c_str()); return result; } } +#define LOG_ERR_AND_RETURN(result) { if (Failed(result)) { Logger::Log(LogCat, LogLevel::ERROR, result.Description.c_str()); return result; } } namespace lunarium { namespace editor { - + uint32_t Editor::LogCat = -1; + Editor::Editor() - : mLogCat(-1), mpPath(nullptr), mDoNewProject(false), mDoOpenProject(false), + : mpPath(nullptr), mDoNewProject(false), mDoOpenProject(false), mDoSaveProject(false), mDoSaveAs(false), mNextWindowID(0), mpMapEditor(nullptr) { } OpRes Editor::Initialize() { - mLogCat = Logger::RegisterCategory("EDITOR"); + LogCat = Logger::RegisterCategory("EDITOR"); // Initialize internal data DataManager::Initialize(); @@ -50,16 +51,16 @@ namespace editor // Init editor panels mAboutPanel.SetOpen(false); - OpRes res = mPanelManager.Initialize(this, "Lunarium editor", true).LogIfFailed(mLogCat); + OpRes res = mPanelManager.Initialize(this, "Lunarium editor", true).LogIfFailed(LogCat); if (Failed(res)) { return res; } - mPanelManager.AddPanel(new AssetBrowser("")).LogIfFailed(mLogCat); - mPanelManager.AddPanel(new WorldTree()).LogIfFailed(mLogCat); - mPanelManager.AddPanel(new WorldView()).LogIfFailed(mLogCat); - mPanelManager.AddPanel(new PropertiesView()).LogIfFailed(mLogCat); + mPanelManager.AddPanel(new AssetBrowser("")).LogIfFailed(LogCat); + mPanelManager.AddPanel(new WorldTree()).LogIfFailed(LogCat); + mPanelManager.AddPanel(new WorldView()).LogIfFailed(LogCat); + mPanelManager.AddPanel(new PropertiesView()).LogIfFailed(LogCat); return OpRes::OK(); } @@ -112,7 +113,7 @@ namespace editor uint32_t Editor::GetLogCat() const { - return mLogCat; + return LogCat; } unsigned int Editor::GetNextWindowID() @@ -134,7 +135,7 @@ namespace editor } } - Logger::Log(mLogCat, LogLevel::WARNING, "And unknown ToolType was passed into Editor::IsToolOpen: %n", type); + Logger::Log(LogCat, LogLevel::WARNING, "And unknown ToolType was passed into Editor::IsToolOpen: %n", type); return false; } @@ -187,7 +188,7 @@ namespace editor if (!mFileBrowser.OpenInDirectory("")) { mDoNewProject = false; - Logger::Log(mLogCat, LogLevel::ERROR, "Could not open the File Browser"); + Logger::Log(LogCat, LogLevel::ERROR, "Could not open the File Browser"); } } else @@ -195,20 +196,20 @@ namespace editor if (mFileBrowser.GetResult() == FileBrowser::Result::OK) { mpPath = mFileBrowser.GetSelectedItem(); - Logger::Log(mLogCat, LogLevel::INFO, "Generating new project at %s", mpPath->string().c_str()); + Logger::Log(LogCat, LogLevel::INFO, "Generating new project at %s", mpPath->string().c_str()); // Generate new project at mpPath OpRes result = mProject.GenerateProject(mpPath->filename().string(), mpPath->parent_path()); if (Failed(result)) { - Logger::Log(mLogCat, LogLevel::ERROR, "Could not create a new project: %s", result.Description); + Logger::Log(LogCat, LogLevel::ERROR, "Could not create a new project: %s", result.Description); } ((AssetBrowser*)mPanelManager.GetPanel(PanelType::PT_ASSET_BROWSER))->SetAssetDirectory(*mpPath / std::filesystem::path("contents/assets")); mDoNewProject = false; } else if (mFileBrowser.GetResult() == FileBrowser::Result::CANCEL) { - Logger::Log(mLogCat, LogLevel::INFO, "New Project operation cancelled"); + Logger::Log(LogCat, LogLevel::INFO, "New Project operation cancelled"); mDoNewProject = false; } @@ -224,7 +225,7 @@ namespace editor if (!mFileBrowser.OpenInDirectory("")) { mDoNewProject = false; - Logger::Log(mLogCat, LogLevel::ERROR, "Could not open the File Browser"); + Logger::Log(LogCat, LogLevel::ERROR, "Could not open the File Browser"); } } else @@ -232,20 +233,20 @@ namespace editor if (mFileBrowser.GetResult() == FileBrowser::Result::OK) { mpPath = mFileBrowser.GetSelectedItem(); - Logger::Log(mLogCat, LogLevel::INFO, "Generating new project at %s", mpPath->string().c_str()); + Logger::Log(LogCat, LogLevel::INFO, "Generating new project at %s", mpPath->string().c_str()); // Open project at mpPath OpRes result = mProject.LoadProject(*mpPath); if (Failed(result)) { - Logger::Log(mLogCat, LogLevel::ERROR, "Could not open project: %s -- reason: %s", mpPath->string().c_str(), result.Description); + Logger::Log(LogCat, LogLevel::ERROR, "Could not open project: %s -- reason: %s", mpPath->string().c_str(), result.Description); } ((AssetBrowser*)mPanelManager.GetPanel(PanelType::PT_ASSET_BROWSER))->SetAssetDirectory(*mpPath / std::filesystem::path("contents/assets")); mDoOpenProject = false; } else if (mFileBrowser.GetResult() == FileBrowser::Result::CANCEL) { - Logger::Log(mLogCat, LogLevel::INFO, "Open Project operation cancelled"); + Logger::Log(LogCat, LogLevel::INFO, "Open Project operation cancelled"); mDoOpenProject = false; } } diff --git a/src/run_modes/editor/editor.h b/src/run_modes/editor/editor.h index 2e4b0b5..91ea60b 100644 --- a/src/run_modes/editor/editor.h +++ b/src/run_modes/editor/editor.h @@ -32,9 +32,13 @@ namespace lunarium TT_MAP_EDITOR, }; + class MapEditor; class Editor : public iRunMode { + public: + static uint32_t LogCat; + public: Editor(); OpRes Initialize(); @@ -52,7 +56,6 @@ namespace lunarium const Editor& operator=(const Editor&) = delete; private: // Data - uint32_t mLogCat; PanelManager mPanelManager; Project mProject; 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 7c49649..bb070ce 100644 --- a/src/run_modes/editor/tools/map_editor/map_editor.cpp +++ b/src/run_modes/editor/tools/map_editor/map_editor.cpp @@ -16,6 +16,7 @@ // Panels #include "panels/map_canvas.h" +#include "panels/tile_set_view.h" namespace lunarium { namespace editor { @@ -40,6 +41,7 @@ namespace lunarium { namespace editor } mPanelManager.AddPanel(new MapCanvas(this)).LogIfFailed(mpEditor->GetLogCat()); + mPanelManager.AddPanel(new TileSetView(this)).LogIfFailed(mpEditor->GetLogCat()); Open(); @@ -54,8 +56,6 @@ namespace lunarium { namespace editor void MapEditor::OnTick(double delta) { mPanelManager.OnTick(delta); - - ((MapCanvas*)mPanelManager.GetPanel(gui::PanelType::PT_MAP_CANVAS))->Render(&Core::Graphics()); } bool MapEditor::OnRender(lunarium::IGraphics* g) diff --git a/src/run_modes/editor/tools/map_editor/panels/map_canvas.cpp b/src/run_modes/editor/tools/map_editor/panels/map_canvas.cpp index a009b54..fa6aede 100644 --- a/src/run_modes/editor/tools/map_editor/panels/map_canvas.cpp +++ b/src/run_modes/editor/tools/map_editor/panels/map_canvas.cpp @@ -8,6 +8,7 @@ #include "map_canvas.h" #include "../map_editor.h" +#include #include #include #include @@ -20,14 +21,37 @@ namespace lunarium { namespace editor { MapCanvas::MapCanvas(MapEditor* editor) : Panel(gui::PanelType::PT_MAP_CANVAS, "Map Canvas", gui::PanelDockZone::DDZ_CENTER, true), - mpMapEditor(editor), mpCanvasImage(nullptr), mMap(nullptr), mSelectedTile({-1, -1}) + mpMapEditor(editor), mpCanvasImage(nullptr), mMap(nullptr), mSelectedTile({-1, -1}), mFrameBuffer(-1), mMapSizeChanged(false) { } void MapCanvas::Update(float delta) { - + if (!mpCanvasImage) + { + if (mMap) + { + if (mMapSizeChanged) + { + Core::Graphics().DestroyRenderTexture(mFrameBuffer); + mFrameBuffer = -1; + mMapSizeChanged = false; + } + + if (mFrameBuffer == -1) + { + mFrameBuffer = Core::Graphics().CreateRenderTexture(mMap->GetSizeInPixels().Width, mMap->GetSizeInPixels().Height, 4); + } + + Color prev = Core::Graphics().GetClearColor(); + Core::Graphics().SetClearColor(Color::Transparent()); + Core::GetInstance().BeginRenderToTexture(mFrameBuffer).LogIfFailed(Editor::LogCat); + mMap->Render(&Core::Graphics()); + mpCanvasImage = Core::GetInstance().EndRenderToTexture(); + Core::Graphics().SetClearColor(prev); + } + } } bool MapCanvas::DoFrame() @@ -74,27 +98,19 @@ namespace lunarium { namespace editor ImGui::Image((ImTextureID)mpCanvasImage->GetGLTextureID64(), ImVec2(mpCanvasImage->GetWidth(), mpCanvasImage->GetHeight())); } - ImGui::End(); - return mIsOpen; - } + // TODO: If a tile on the map was changed this frame null out the canvas image so it will be redrawn - void MapCanvas::Render(lunarium::IGraphics* g) - { - // Render tile map - mMap->Render(g); - // Render grid if it's turned on + ImGui::End(); + return mIsOpen; } - void MapCanvas::SetTileMap(TileMap* pMap) { mMap = pMap; - } - void MapCanvas::SetCanvasImage(Image* image) - { - mpCanvasImage = image; + // Force the image to redraw + mpCanvasImage = nullptr; } void MapCanvas::SetSelectedTile(TileRef tile) @@ -106,8 +122,11 @@ namespace lunarium { namespace editor { return mSelectedTile; } - - - + void MapCanvas::MapSizeChanged() + { + mMapSizeChanged = true; + mpCanvasImage = nullptr; + } + }} \ No newline at end of file diff --git a/src/run_modes/editor/tools/map_editor/panels/map_canvas.h b/src/run_modes/editor/tools/map_editor/panels/map_canvas.h index c7a5068..2a4d9a1 100644 --- a/src/run_modes/editor/tools/map_editor/panels/map_canvas.h +++ b/src/run_modes/editor/tools/map_editor/panels/map_canvas.h @@ -26,20 +26,24 @@ namespace lunarium { namespace editor MapCanvas(MapEditor* editor); void Update(float delta); bool DoFrame(); - void Render(lunarium::IGraphics* g); void SetTileMap(TileMap* pMap); - void SetCanvasImage(Image* image); void SetSelectedTile(TileRef tile); TileRef GetSelectedTile() const; + // Force the image to redraw and recreate the frame buffer + void MapSizeChanged(); + // void Invalidate(); + private: MapEditor* mpMapEditor; std::string mMouseStatusInfo; + int mFrameBuffer; Image* mpCanvasImage; TileMap* mMap; TileRef mSelectedTile; + bool mMapSizeChanged; private: 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 new file mode 100644 index 0000000..6ada280 --- /dev/null +++ b/src/run_modes/editor/tools/map_editor/panels/tile_set_view.cpp @@ -0,0 +1,102 @@ +/****************************************************************************** +* File - tile_set_view.cpp +* Author - Joey Pollack +* Date - 2022/02/16 (y/m/d) +* Mod Date - 2022/02/16 (y/m/d) +* Description - panel for viewing the current tile set +******************************************************************************/ + +#include "tile_set_view.h" +#include "../map_editor.h" +#include "../tile_set.h" +#include +#include +#include +#include + + +namespace lunarium { namespace editor +{ + TileSetView::TileSetView(MapEditor* editor) + : Panel(gui::PanelType::PT_TILE_SET_VIEW, "Tile Set View", gui::PanelDockZone::DDZ_RIGHT, true), + mpEditor(editor), mpTileSet(nullptr), mpViewImage(nullptr), mFrameBuffer(-1), + mViewOffset({0, 0}), mViewZoom(1.0f) + { + + } + + void TileSetView::Update(float delta) + { + if (!mpViewImage) + { + if (mpTileSet) + { + if (mFrameBuffer == -1) + { + mFrameBuffer = Core::Graphics().CreateRenderTexture(mpTileSet->GetImage()->GetWidth(), mpTileSet->GetImage()->GetHeight(), 4); + } + + Color prev = Core::Graphics().GetClearColor(); + Core::Graphics().SetClearColor(Color::Transparent()); + Core::GetInstance().BeginRenderToTexture(mFrameBuffer).LogIfFailed(Editor::LogCat); + mpTileSet->Render(&Core::Graphics()); + mpViewImage = Core::GetInstance().EndRenderToTexture(); + Core::Graphics().SetClearColor(prev); + } + } + } + + bool TileSetView::DoFrame() + { + if (!mIsOpen) + return false; + + if (!ImGui::Begin(GetName(), &mIsOpen)) + { + ImGui::End(); + return false; + } + + ImVec2 pos = ImGui::GetWindowPos(); + ImVec2 size = ImGui::GetWindowSize(); + + // Adjust for the tab bar + pos.y += ImGui::GetFrameHeight(); + size.y -= ImGui::GetFrameHeight(); + ImGuiIO& io = ImGui::GetIO(); + float x = io.MousePos.x - pos.x; + float y = io.MousePos.y - pos.y; + + + if (mpViewImage) + { + ImGui::Image((ImTextureID)mpViewImage->GetGLTextureID64(), ImVec2(mpViewImage->GetWidth(), mpViewImage->GetHeight())); + } + + // TODO: If a tile on the map was changed this frame null out the canvas image so it will be redrawn + + + ImGui::End(); + return mIsOpen; + } + + void TileSetView::SetTileSet(TileSet* set) + { + mpTileSet = set; + } + + TileSet* TileSetView::GetTileSet() + { + return mpTileSet; + } + + void TileSetView::Invalidate(bool remake_frame_buffer) + { + mpViewImage = nullptr; + if (remake_frame_buffer) + { + Core::Graphics().DestroyRenderTexture(mFrameBuffer); + mFrameBuffer = -1; + } + } +}} \ No newline at end of file diff --git a/src/run_modes/editor/tools/map_editor/panels/tile_set_view.h b/src/run_modes/editor/tools/map_editor/panels/tile_set_view.h new file mode 100644 index 0000000..15d4860 --- /dev/null +++ b/src/run_modes/editor/tools/map_editor/panels/tile_set_view.h @@ -0,0 +1,44 @@ +/****************************************************************************** +* File - tile_set_view.h +* Author - Joey Pollack +* Date - 2022/02/16 (y/m/d) +* Mod Date - 2022/02/16 (y/m/d) +* Description - panel for viewing the current tile set +******************************************************************************/ + +#ifndef TILE_SET_VIEW_H_ +#define TILE_SET_VIEW_H_ + +#include +#include + +namespace lunarium { class Image; } + +namespace lunarium { namespace editor +{ + class MapEditor; + class TileSet; + class TileSetView : public gui::Panel + { + public: + TileSetView(MapEditor* editor); + + void Update(float delta); + bool DoFrame(); + + void SetTileSet(TileSet* set); + TileSet* GetTileSet(); + + void Invalidate(bool remake_frame_buffer = false); + + private: + MapEditor* mpEditor; + TileSet* mpTileSet; + int mFrameBuffer; + Image* mpViewImage; + Vec2i mViewOffset; + float mViewZoom; + }; +}} + +#endif // TILE_SET_VIEW_H_ \ No newline at end of file diff --git a/src/run_modes/editor/tools/map_editor/tile_map.cpp b/src/run_modes/editor/tools/map_editor/tile_map.cpp index 773fa01..ad3b515 100644 --- a/src/run_modes/editor/tools/map_editor/tile_map.cpp +++ b/src/run_modes/editor/tools/map_editor/tile_map.cpp @@ -7,21 +7,44 @@ ******************************************************************************/ #include "tile_map.h" +#include "tile_set.h" #include #include namespace lunarium { namespace editor { + + TileMap::TileMap(std::map* tilesets) + : mpTileSets(tilesets), mpMap(nullptr) + { + + } + void TileMap::ClearMap() { for(int i = 0; i < mSizeInTiles.Width; i++) { for (int j = 0; j < mSizeInTiles.Height; j++) { - mpMap[i][j] = { -1, -1 }; + mpMap[i][j] = { -1, {-1, -1} }; } } } + + + Sizei TileMap::GetSizeInTiles() + { + return mSizeInTiles; + } + + Sizei TileMap::GetSizeInPixels() + { + if (!mpMap) + return { 0, 0 }; + + Sizei tile_size = (*mpTileSets)[mpMap[0][0].TileSetID]->GetTileSize(); + return { tile_size.Width * mSizeInTiles.Width, tile_size.Height * mSizeInTiles.Height }; + } void TileMap::ResizeMap(Sizei size) { @@ -55,6 +78,41 @@ namespace lunarium { namespace editor void TileMap::Render(lunarium::IGraphics* g) { + if (!mpMap) + return; + // Check the size of the tiles - each tileset should have the same sized tiles for the same map + Sizei tile_size = (*mpTileSets)[mpMap[0][0].TileSetID]->GetTileSize(); + Sizei map_size_pixels = { tile_size.Width * mSizeInTiles.Width, tile_size.Height * mSizeInTiles.Height }; + + // Draw Map + for (int i = 0; i < mSizeInTiles.Width; i++) + { + for (int j = 0; j < mSizeInTiles.Height; j++) + { + // If there is no tile here skip this spot + if (mpMap[i][j].TileIndex.X == -1 || mpMap[i][j].TileIndex.Y == -1) + continue; + + TileSet* set = (*mpTileSets)[mpMap[i][j].TileSetID]; + Rectangle dest = Rectangle::MakeFromTopLeft(i * tile_size.Width, j * tile_size.Height, tile_size.Width, tile_size.Height); + Rectangle src = set->GetTileRect(mpMap[i][j].TileIndex); + + g->DrawImage(*set->GetImage(), src, dest, Color::White()); + } + } + + // Draw grid + for (int i = 0; i < mSizeInTiles.Width; i++) + { + g->DrawLine(glm::vec2(i * tile_size.Width, 0), glm::vec2(i * tile_size.Width, map_size_pixels.Height), Color::Black(), 1.0f); + } + g->DrawLine(glm::vec2(map_size_pixels.Width, 0), glm::vec2(map_size_pixels.Width, map_size_pixels.Height), Color::Black(), 1.0f); + + for (int j = 0; j < mSizeInTiles.Height; j++) + { + g->DrawLine(glm::vec2(0, j * tile_size.Height), glm::vec2(map_size_pixels.Width, j * tile_size.Height), Color::Black(), 1.0f); + } + g->DrawLine(glm::vec2(0, map_size_pixels.Height), glm::vec2(map_size_pixels.Width, map_size_pixels.Height), Color::Black(), 1.0f); } }} diff --git a/src/run_modes/editor/tools/map_editor/tile_map.h b/src/run_modes/editor/tools/map_editor/tile_map.h index 91baec7..9d1b81e 100644 --- a/src/run_modes/editor/tools/map_editor/tile_map.h +++ b/src/run_modes/editor/tools/map_editor/tile_map.h @@ -10,35 +10,43 @@ #define TILE_MAP_H_ #include +#include namespace lunarium { class IGraphics; } namespace lunarium { namespace editor { + class TileSet; + // Allows tiles from multiple tilesets to be used in one map // A tile ID of -1 will mean "no tile" and result in a transparent spot // when rendered struct TileRef { int TileSetID; - int TileID; + Vec2i TileIndex; }; class TileMap { public: + TileMap(std::map* tilesets); void SetTile(TileRef, Vec2i location); TileRef GetTile(Vec2i location); void ClearMap(); void ResizeMap(Sizei size); + Sizei GetSizeInTiles(); + Sizei GetSizeInPixels(); + // Call during render to texture phase void Render(lunarium::IGraphics* g); private: TileRef** mpMap; Sizei mSizeInTiles; + std::map* mpTileSets; }; }} diff --git a/src/run_modes/editor/tools/map_editor/tile_set.cpp b/src/run_modes/editor/tools/map_editor/tile_set.cpp new file mode 100644 index 0000000..89bc469 --- /dev/null +++ b/src/run_modes/editor/tools/map_editor/tile_set.cpp @@ -0,0 +1,67 @@ +/****************************************************************************** +* File - tile_set.h +* Author - Joey Pollack +* Date - 2022/02/16 (y/m/d) +* Mod Date - 2022/02/16 (y/m/d) +* Description - Manage a tile set for map making +******************************************************************************/ + +#include "tile_set.h" +#include +#include +#include +#include + +namespace lunarium { namespace editor +{ + TileSet::TileSet() + : mSetImage(nullptr), mTileSize({ 0, 0}) + { + + } + + void TileSet::SetImage(Image* image) + { + mSetImage = image; + } + + void TileSet::SetTileSize(Sizei size) + { + mTileSize = size; + mNumTiles.Width = mSetImage->GetWidth() / mTileSize.Width; + mNumTiles.Height = mSetImage->GetHeight() / mTileSize.Height; + } + + Image* TileSet::GetImage() + { + return mSetImage; + } + + Sizei TileSet::GetTileSize() + { + return mTileSize; + } + + Rectangle TileSet::GetTileRect(Vec2i index) + { + return Rectangle::MakeFromTopLeft(index.X * mTileSize.Width, index.Y * mTileSize.Height, mTileSize.Width, mTileSize.Height); + } + + void TileSet::Render(lunarium::IGraphics* g) + { + g->DrawImage(*mSetImage, glm::vec2(0, 0), Color::White()); + + // Draw grid + for (int i = 0; i < mNumTiles.Width; i++) + { + g->DrawLine(glm::vec2(i * mTileSize.Width, 0), glm::vec2(i * mTileSize.Width, mSetImage->GetHeight()), Color::Black(), 1.0f); + } + g->DrawLine(glm::vec2(mSetImage->GetWidth(), 0), glm::vec2(mSetImage->GetWidth(), mSetImage->GetHeight()), Color::Black(), 1.0f); + + for (int j = 0; j < mNumTiles.Height; j++) + { + g->DrawLine(glm::vec2(0, j * mTileSize.Height), glm::vec2(mSetImage->GetWidth(), j * mTileSize.Height), Color::Black(), 1.0f); + } + g->DrawLine(glm::vec2(0, mSetImage->GetHeight()), glm::vec2(mSetImage->GetWidth(), mSetImage->GetHeight()), Color::Black(), 1.0f); + } +}} \ No newline at end of file diff --git a/src/run_modes/editor/tools/map_editor/tile_set.h b/src/run_modes/editor/tools/map_editor/tile_set.h new file mode 100644 index 0000000..c790081 --- /dev/null +++ b/src/run_modes/editor/tools/map_editor/tile_set.h @@ -0,0 +1,40 @@ +/****************************************************************************** +* File - tile_set.h +* Author - Joey Pollack +* Date - 2022/02/16 (y/m/d) +* Mod Date - 2022/02/16 (y/m/d) +* Description - Manage a tile set for map making +******************************************************************************/ + +#ifndef TILE_SET_H_ +#define TILE_SET_H_ + +#include + +namespace lunarium { class Image; class IGraphics; } + +namespace lunarium { namespace editor +{ + class TileSet + { + public: + TileSet(); + + void SetImage(Image* image); + void SetTileSize(Sizei size); + + Image* GetImage(); + Sizei GetTileSize(); + Rectangle GetTileRect(Vec2i index); + + void Render(lunarium::IGraphics* g); + + private: + Image* mSetImage; + Sizei mTileSize; // in pixels, must be a square power of 2 + Sizei mNumTiles; + + }; +}} + +#endif // TILE_SET_H_ \ No newline at end of file