MapEditor design fleshed out but render code not yet tested

Gui_Panel_Refactor
Joeyrp 4 years ago
parent f4e5de912d
commit 22fee91f15

@ -15,17 +15,47 @@ namespace lunarium
// COLOR // COLOR
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
Color::Color(float _r, float _g, float _b, float _a) 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() 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 // RECTANGLE
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////

@ -55,16 +55,25 @@ namespace lunarium
struct struct
{ {
float Red; float R;
float Green; float G;
float Blue; float B;
float Alpha; float A;
}; };
}; };
Color(); Color();
Color(float _r, float _g, float _b, float _a = 1.0f); 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();
}; };
///////////////////////////////////////////////// /////////////////////////////////////////////////

@ -186,7 +186,7 @@ namespace lunarium
void OglGraphics::SetClearColor(Color c) void OglGraphics::SetClearColor(Color c)
{ {
mClearColor = 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 Color OglGraphics::GetClearColor() const
@ -284,7 +284,7 @@ namespace lunarium
glm::mat4 model = glm::mat4(1.0f); glm::mat4 model = glm::mat4(1.0f);
model = glm::rotate(model, angle, glm::vec3(0.0f, 0.0f, 1.0f)); model = glm::rotate(model, angle, glm::vec3(0.0f, 0.0f, 1.0f));
mShapeShader.SetUniformMatrix("model", 1, glm::value_ptr(model)); 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); glBindVertexArray(mRectVAO);
GLfloat lineVerts[6][4] = { GLfloat lineVerts[6][4] = {
@ -312,7 +312,7 @@ namespace lunarium
mShapeShader.SetUniformMatrix("projection", 1, glm::value_ptr(mProjection)); mShapeShader.SetUniformMatrix("projection", 1, glm::value_ptr(mProjection));
glm::mat4 model = glm::mat4(1.0f); glm::mat4 model = glm::mat4(1.0f);
mShapeShader.SetUniformMatrix("model", 1, glm::value_ptr(model)); 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); glBindVertexArray(mEllipseVAO);
GLfloat points[360][2]; GLfloat points[360][2];
@ -339,7 +339,7 @@ namespace lunarium
mShapeShader.SetUniformMatrix("projection", 1, glm::value_ptr(mProjection)); mShapeShader.SetUniformMatrix("projection", 1, glm::value_ptr(mProjection));
glm::mat4 model = glm::mat4(1.0f); glm::mat4 model = glm::mat4(1.0f);
mShapeShader.SetUniformMatrix("model", 1, glm::value_ptr(model)); 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); glBindVertexArray(mEllipseVAO);
GLfloat points[362][2]; GLfloat points[362][2];
@ -369,7 +369,7 @@ namespace lunarium
model = glm::translate(model, glm::vec3(position.x, position.y, 0.0f)); 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)); model = glm::rotate(model, angle, glm::vec3(0.0f, 0.0f, 1.0f));
mShapeShader.SetUniformMatrix("model", 1, glm::value_ptr(model)); 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); glBindVertexArray(mRectVAO);
glBindBuffer(GL_ARRAY_BUFFER, mRectVBO); 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::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)); model = glm::scale(model, glm::vec3(box.HalfWidth * 2, box.HalfHeight * 2, 1.0f));
mShapeShader.SetUniformMatrix("model", 1, glm::value_ptr(model)); 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); glBindVertexArray(mRectVAO);
GLfloat vertices[6][4] = { 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::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)); model = glm::rotate(model, angle, glm::vec3(0.0f, 0.0f, 1.0f));
mShapeShader.SetUniformMatrix("model", 1, glm::value_ptr(model)); 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); glBindVertexArray(mRectVAO);
GLfloat lineVerts[4][4] = { GLfloat lineVerts[4][4] = {
@ -500,7 +500,7 @@ namespace lunarium
mImageShader.SetUniformf("uvManip", { xScale, xOffset, yScale /** -1.0f*/, yOffset}); mImageShader.SetUniformf("uvManip", { xScale, xOffset, yScale /** -1.0f*/, yOffset});
mImageShader.SetUniformMatrix("model", 1, glm::value_ptr(trans)); mImageShader.SetUniformMatrix("model", 1, glm::value_ptr(trans));
mImageShader.SetUniformMatrix("projection", 1, glm::value_ptr(mProjection)); 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); glActiveTexture(GL_TEXTURE0);

@ -189,7 +189,7 @@ namespace lunarium
// NOTE: String drawing code based on code from: https://learnopengl.com/In-Practice/Text-Rendering // NOTE: String drawing code based on code from: https://learnopengl.com/In-Practice/Text-Rendering
mTextShader.MakeActive(); mTextShader.MakeActive();
mTextShader.SetUniformMatrix("projection", 1, glm::value_ptr(projection)); 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); glActiveTexture(GL_TEXTURE0);
glBindVertexArray(mTextVAO); glBindVertexArray(mTextVAO);

@ -27,7 +27,7 @@ namespace lunarium { namespace gui
// Map Editor // Map Editor
PT_MAP_CANVAS, PT_MAP_CANVAS,
PT_TILE_PALLET, PT_TILE_SET_VIEW,
PT_TILE_PROPERTIES, PT_TILE_PROPERTIES,
PT_MAP_PROPERTIES, PT_MAP_PROPERTIES,
PT_MAP_PREVIEW, PT_MAP_PREVIEW,

@ -11,7 +11,9 @@ set(EDITOR_SRC
"panels/propertiesView.cpp" "panels/propertiesView.cpp"
"tools/map_editor/map_editor.cpp" "tools/map_editor/map_editor.cpp"
"tools/map_editor/tile_map.cpp" "tools/map_editor/tile_map.cpp"
"tools/map_editor/tile_set.cpp"
"tools/map_editor/panels/map_canvas.cpp" "tools/map_editor/panels/map_canvas.cpp"
"tools/map_editor/panels/tile_set_view.cpp"
) )
add_library(editor ${EDITOR_SRC}) add_library(editor ${EDITOR_SRC})

@ -27,22 +27,23 @@
using namespace lunarium::gui; using namespace lunarium::gui;
// ERROR LOG MACRO // 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 lunarium
{ {
namespace editor namespace editor
{ {
uint32_t Editor::LogCat = -1;
Editor::Editor() Editor::Editor()
: mLogCat(-1), mpPath(nullptr), mDoNewProject(false), mDoOpenProject(false), : mpPath(nullptr), mDoNewProject(false), mDoOpenProject(false),
mDoSaveProject(false), mDoSaveAs(false), mNextWindowID(0), mpMapEditor(nullptr) mDoSaveProject(false), mDoSaveAs(false), mNextWindowID(0), mpMapEditor(nullptr)
{ {
} }
OpRes Editor::Initialize() OpRes Editor::Initialize()
{ {
mLogCat = Logger::RegisterCategory("EDITOR"); LogCat = Logger::RegisterCategory("EDITOR");
// Initialize internal data // Initialize internal data
DataManager::Initialize(); DataManager::Initialize();
@ -50,16 +51,16 @@ namespace editor
// Init editor panels // Init editor panels
mAboutPanel.SetOpen(false); 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)) if (Failed(res))
{ {
return res; return res;
} }
mPanelManager.AddPanel(new AssetBrowser("")).LogIfFailed(mLogCat); mPanelManager.AddPanel(new AssetBrowser("")).LogIfFailed(LogCat);
mPanelManager.AddPanel(new WorldTree()).LogIfFailed(mLogCat); mPanelManager.AddPanel(new WorldTree()).LogIfFailed(LogCat);
mPanelManager.AddPanel(new WorldView()).LogIfFailed(mLogCat); mPanelManager.AddPanel(new WorldView()).LogIfFailed(LogCat);
mPanelManager.AddPanel(new PropertiesView()).LogIfFailed(mLogCat); mPanelManager.AddPanel(new PropertiesView()).LogIfFailed(LogCat);
return OpRes::OK(); return OpRes::OK();
} }
@ -112,7 +113,7 @@ namespace editor
uint32_t Editor::GetLogCat() const uint32_t Editor::GetLogCat() const
{ {
return mLogCat; return LogCat;
} }
unsigned int Editor::GetNextWindowID() 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; return false;
} }
@ -187,7 +188,7 @@ namespace editor
if (!mFileBrowser.OpenInDirectory("")) if (!mFileBrowser.OpenInDirectory(""))
{ {
mDoNewProject = false; 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 else
@ -195,20 +196,20 @@ namespace editor
if (mFileBrowser.GetResult() == FileBrowser::Result::OK) if (mFileBrowser.GetResult() == FileBrowser::Result::OK)
{ {
mpPath = mFileBrowser.GetSelectedItem(); 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 // Generate new project at mpPath
OpRes result = mProject.GenerateProject(mpPath->filename().string(), mpPath->parent_path()); OpRes result = mProject.GenerateProject(mpPath->filename().string(), mpPath->parent_path());
if (Failed(result)) 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")); ((AssetBrowser*)mPanelManager.GetPanel(PanelType::PT_ASSET_BROWSER))->SetAssetDirectory(*mpPath / std::filesystem::path("contents/assets"));
mDoNewProject = false; mDoNewProject = false;
} }
else if (mFileBrowser.GetResult() == FileBrowser::Result::CANCEL) 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; mDoNewProject = false;
} }
@ -224,7 +225,7 @@ namespace editor
if (!mFileBrowser.OpenInDirectory("")) if (!mFileBrowser.OpenInDirectory(""))
{ {
mDoNewProject = false; 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 else
@ -232,20 +233,20 @@ namespace editor
if (mFileBrowser.GetResult() == FileBrowser::Result::OK) if (mFileBrowser.GetResult() == FileBrowser::Result::OK)
{ {
mpPath = mFileBrowser.GetSelectedItem(); 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 // Open project at mpPath
OpRes result = mProject.LoadProject(*mpPath); OpRes result = mProject.LoadProject(*mpPath);
if (Failed(result)) 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")); ((AssetBrowser*)mPanelManager.GetPanel(PanelType::PT_ASSET_BROWSER))->SetAssetDirectory(*mpPath / std::filesystem::path("contents/assets"));
mDoOpenProject = false; mDoOpenProject = false;
} }
else if (mFileBrowser.GetResult() == FileBrowser::Result::CANCEL) 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; mDoOpenProject = false;
} }
} }

@ -32,9 +32,13 @@ namespace lunarium
TT_MAP_EDITOR, TT_MAP_EDITOR,
}; };
class MapEditor; class MapEditor;
class Editor : public iRunMode class Editor : public iRunMode
{ {
public:
static uint32_t LogCat;
public: public:
Editor(); Editor();
OpRes Initialize(); OpRes Initialize();
@ -52,7 +56,6 @@ namespace lunarium
const Editor& operator=(const Editor&) = delete; const Editor& operator=(const Editor&) = delete;
private: // Data private: // Data
uint32_t mLogCat;
PanelManager mPanelManager; PanelManager mPanelManager;
Project mProject; Project mProject;

@ -16,6 +16,7 @@
// Panels // Panels
#include "panels/map_canvas.h" #include "panels/map_canvas.h"
#include "panels/tile_set_view.h"
namespace lunarium { namespace editor namespace lunarium { namespace editor
{ {
@ -40,6 +41,7 @@ namespace lunarium { namespace editor
} }
mPanelManager.AddPanel(new MapCanvas(this)).LogIfFailed(mpEditor->GetLogCat()); mPanelManager.AddPanel(new MapCanvas(this)).LogIfFailed(mpEditor->GetLogCat());
mPanelManager.AddPanel(new TileSetView(this)).LogIfFailed(mpEditor->GetLogCat());
Open(); Open();
@ -54,8 +56,6 @@ namespace lunarium { namespace editor
void MapEditor::OnTick(double delta) void MapEditor::OnTick(double delta)
{ {
mPanelManager.OnTick(delta); mPanelManager.OnTick(delta);
((MapCanvas*)mPanelManager.GetPanel(gui::PanelType::PT_MAP_CANVAS))->Render(&Core::Graphics());
} }
bool MapEditor::OnRender(lunarium::IGraphics* g) bool MapEditor::OnRender(lunarium::IGraphics* g)

@ -8,6 +8,7 @@
#include "map_canvas.h" #include "map_canvas.h"
#include "../map_editor.h" #include "../map_editor.h"
#include <editor/editor.h>
#include <gui/dearimgui/imgui.h> #include <gui/dearimgui/imgui.h>
#include <core/core.h> #include <core/core.h>
#include <graphics/graphics.h> #include <graphics/graphics.h>
@ -20,14 +21,37 @@ namespace lunarium { namespace editor
{ {
MapCanvas::MapCanvas(MapEditor* editor) MapCanvas::MapCanvas(MapEditor* editor)
: Panel(gui::PanelType::PT_MAP_CANVAS, "Map Canvas", gui::PanelDockZone::DDZ_CENTER, true), : 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) 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() bool MapCanvas::DoFrame()
@ -74,27 +98,19 @@ namespace lunarium { namespace editor
ImGui::Image((ImTextureID)mpCanvasImage->GetGLTextureID64(), ImVec2(mpCanvasImage->GetWidth(), mpCanvasImage->GetHeight())); ImGui::Image((ImTextureID)mpCanvasImage->GetGLTextureID64(), ImVec2(mpCanvasImage->GetWidth(), mpCanvasImage->GetHeight()));
} }
ImGui::End(); // TODO: If a tile on the map was changed this frame null out the canvas image so it will be redrawn
return mIsOpen;
}
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) void MapCanvas::SetTileMap(TileMap* pMap)
{ {
mMap = pMap; mMap = pMap;
}
void MapCanvas::SetCanvasImage(Image* image) // Force the image to redraw
{ mpCanvasImage = nullptr;
mpCanvasImage = image;
} }
void MapCanvas::SetSelectedTile(TileRef tile) void MapCanvas::SetSelectedTile(TileRef tile)
@ -107,7 +123,10 @@ namespace lunarium { namespace editor
return mSelectedTile; return mSelectedTile;
} }
void MapCanvas::MapSizeChanged()
{
mMapSizeChanged = true;
mpCanvasImage = nullptr;
}
}} }}

@ -26,20 +26,24 @@ namespace lunarium { namespace editor
MapCanvas(MapEditor* editor); MapCanvas(MapEditor* editor);
void Update(float delta); void Update(float delta);
bool DoFrame(); bool DoFrame();
void Render(lunarium::IGraphics* g);
void SetTileMap(TileMap* pMap); void SetTileMap(TileMap* pMap);
void SetCanvasImage(Image* image);
void SetSelectedTile(TileRef tile); void SetSelectedTile(TileRef tile);
TileRef GetSelectedTile() const; TileRef GetSelectedTile() const;
// Force the image to redraw and recreate the frame buffer
void MapSizeChanged();
// void Invalidate();
private: private:
MapEditor* mpMapEditor; MapEditor* mpMapEditor;
std::string mMouseStatusInfo; std::string mMouseStatusInfo;
int mFrameBuffer;
Image* mpCanvasImage; Image* mpCanvasImage;
TileMap* mMap; TileMap* mMap;
TileRef mSelectedTile; TileRef mSelectedTile;
bool mMapSizeChanged;
private: private:

@ -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 <assets/types/image.h>
#include <editor/editor.h>
#include <core/core.h>
#include <graphics/graphics.h>
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;
}
}
}}

@ -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 <gui/panel.h>
#include <core/types.h>
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_

@ -7,22 +7,45 @@
******************************************************************************/ ******************************************************************************/
#include "tile_map.h" #include "tile_map.h"
#include "tile_set.h"
#include <core/core.h> #include <core/core.h>
#include <graphics/graphics.h> #include <graphics/graphics.h>
namespace lunarium { namespace editor namespace lunarium { namespace editor
{ {
TileMap::TileMap(std::map<int, TileSet*>* tilesets)
: mpTileSets(tilesets), mpMap(nullptr)
{
}
void TileMap::ClearMap() void TileMap::ClearMap()
{ {
for(int i = 0; i < mSizeInTiles.Width; i++) for(int i = 0; i < mSizeInTiles.Width; i++)
{ {
for (int j = 0; j < mSizeInTiles.Height; j++) 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) void TileMap::ResizeMap(Sizei size)
{ {
TileRef** new_map = new TileRef*[size.Width]; TileRef** new_map = new TileRef*[size.Width];
@ -55,6 +78,41 @@ namespace lunarium { namespace editor
void TileMap::Render(lunarium::IGraphics* g) 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);
} }
}} }}

@ -10,35 +10,43 @@
#define TILE_MAP_H_ #define TILE_MAP_H_
#include <core/types.h> #include <core/types.h>
#include <map>
namespace lunarium { class IGraphics; } namespace lunarium { class IGraphics; }
namespace lunarium { namespace editor namespace lunarium { namespace editor
{ {
class TileSet;
// Allows tiles from multiple tilesets to be used in one map // 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 // A tile ID of -1 will mean "no tile" and result in a transparent spot
// when rendered // when rendered
struct TileRef struct TileRef
{ {
int TileSetID; int TileSetID;
int TileID; Vec2i TileIndex;
}; };
class TileMap class TileMap
{ {
public: public:
TileMap(std::map<int, TileSet*>* tilesets);
void SetTile(TileRef, Vec2i location); void SetTile(TileRef, Vec2i location);
TileRef GetTile(Vec2i location); TileRef GetTile(Vec2i location);
void ClearMap(); void ClearMap();
void ResizeMap(Sizei size); void ResizeMap(Sizei size);
Sizei GetSizeInTiles();
Sizei GetSizeInPixels();
// Call during render to texture phase // Call during render to texture phase
void Render(lunarium::IGraphics* g); void Render(lunarium::IGraphics* g);
private: private:
TileRef** mpMap; TileRef** mpMap;
Sizei mSizeInTiles; Sizei mSizeInTiles;
std::map<int, TileSet*>* mpTileSets;
}; };
}} }}

@ -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 <editor/editor.h>
#include <graphics/graphics.h>
#include <assets/types/image.h>
#include <utils/logger.h>
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);
}
}}

@ -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 <core/types.h>
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_
Loading…
Cancel
Save