Can now select tiles from tile set

Gui_Panel_Refactor
Joeyrp 4 years ago
parent 3377dda894
commit 38b231d42c

@ -132,6 +132,8 @@ namespace lunarium
if (mErrorLogFile.is_open()) if (mErrorLogFile.is_open())
Logger::GetInstance()->AddListener(new FileListener(mErrorLogFile, LogLevel::ERROR | LogLevel::FATAL_ERROR)); Logger::GetInstance()->AddListener(new FileListener(mErrorLogFile, LogLevel::ERROR | LogLevel::FATAL_ERROR));
Logger::GetInstance()->SetAllowRepeats(true);
// Init the Debug log window // Init the Debug log window
OpRes result; OpRes result;
mPanels[gui::PanelType::PT_CORE_CONSOLE] = new CoreConsole; mPanels[gui::PanelType::PT_CORE_CONSOLE] = new CoreConsole;

@ -55,7 +55,7 @@ namespace lunarium
for (int i = 0; i < KeyCodeList.size(); i++) for (int i = 0; i < KeyCodeList.size(); i++)
{ {
Keyboard::Key key = Keyboard::GetInstance()->GetKey((KeyCode)KeyCodeList[i]); Keyboard::Key key = Keyboard::GetInstance()->GetKey((KeyCode)KeyCodeList[i]);
bool keyIsDown = mpWindow->IsKeyDown(KeyCodeList[i]); bool keyIsDown = mpWindow->IsKeyDown(KeyCodeList[i], IsMouseButton(KeyCodeList[i]));
// Mouse Buttons are a special case // Mouse Buttons are a special case
if (IsMouseButton(KeyCodeList[i])) if (IsMouseButton(KeyCodeList[i]))

@ -209,6 +209,7 @@ namespace lunarium
Logger::Logger() Logger::Logger()
{ {
mAllowRepeats = false;
// Register built-in levels // Register built-in levels
RegisterLevel("INFO"); RegisterLevel("INFO");
@ -325,6 +326,11 @@ namespace lunarium
mListeners.clear(); mListeners.clear();
} }
void Logger::SetAllowRepeats(bool allow)
{
mAllowRepeats = allow;
}
void Logger::ClearBacklog() void Logger::ClearBacklog()
{ {
mBacklog.clear(); mBacklog.clear();
@ -363,43 +369,47 @@ namespace lunarium
bool track = true; bool track = true;
// Check for repeats // Check for repeats
for (int i = 0; i < mBacklog.size(); i++) if (!mAllowRepeats)
{ {
LogMessage& prevMsg = mBacklog[i]; for (int i = 0; i < mBacklog.size(); i++)
if (prevMsg.Message == lm.Message)
{ {
LogMessage& prevMsg = mBacklog[i];
if (prevMsg.RepeatAlertSent) if (prevMsg.Message == lm.Message)
{
send = false;
break;
}
// only suppress the messsage if it has been less than
// 1 second since it was last sent
double timeSinceLastSend = timeStamp - prevMsg.SentTimeStamp;
if (timeSinceLastSend <= 1.0)
{ {
prevMsg.Repeats += 1;
if (prevMsg.Repeats > 100 && !prevMsg.RepeatAlertSent) if (prevMsg.RepeatAlertSent)
{ {
lm.Message = lm.Message + " [This message has rapidly repeated over 100 times! No more repeats will be sent out!]"; send = false;
prevMsg.RepeatAlertSent = true; break;
track = false;
} }
else
// only suppress the messsage if it has been less than
// 1 second since it was last sent
double timeSinceLastSend = timeStamp - prevMsg.SentTimeStamp;
if (timeSinceLastSend <= 1.0)
{ {
send = false; prevMsg.Repeats += 1;
if (prevMsg.Repeats > 100 && !prevMsg.RepeatAlertSent)
{
lm.Message = lm.Message + " [This message has rapidly repeated over 100 times! No more repeats will be sent out!]";
prevMsg.RepeatAlertSent = true;
track = false;
}
else
{
send = false;
}
} }
}
prevMsg.SentTimeStamp = timeStamp; prevMsg.SentTimeStamp = timeStamp;
break; break;
}
} }
} }
if (!send) if (!send)
return; return;

@ -152,6 +152,7 @@ namespace lunarium
void FreeAllListeners(); void FreeAllListeners();
void ClearBacklog(); void ClearBacklog();
void SetAllowRepeats(bool allow);
// The message argument and the variable argument list work just like // The message argument and the variable argument list work just like
// printf. Use the same formatters as printf when calling this function. // printf. Use the same formatters as printf when calling this function.
@ -171,6 +172,7 @@ namespace lunarium
std::vector<LogMessage> mBacklog; std::vector<LogMessage> mBacklog;
char mBuffer[BUFFER_SIZE]; char mBuffer[BUFFER_SIZE];
HighResTimer mTimer; HighResTimer mTimer;
bool mAllowRepeats;
private: private:
Logger(); Logger();

@ -48,6 +48,8 @@ namespace editor
// Initialize internal data // Initialize internal data
DataManager::Initialize(); DataManager::Initialize();
ImGui::GetIO().ConfigWindowsMoveFromTitleBarOnly = true;
// Init editor panels // Init editor panels
mAboutPanel.SetOpen(false); mAboutPanel.SetOpen(false);

@ -211,7 +211,7 @@ namespace lunarium { namespace editor
// For now just directly load the file and make a tileset out of it // For now just directly load the file and make a tileset out of it
int w, h, n; int w, h, n;
//stbi_set_flip_vertically_on_load(1); stbi_set_flip_vertically_on_load(1);
unsigned char* buffer = stbi_load(path->string().c_str(), &w, &h, &n, 0); unsigned char* buffer = stbi_load(path->string().c_str(), &w, &h, &n, 0);
Image* i = new Image(); Image* i = new Image();

@ -13,6 +13,7 @@
#include <editor/editor.h> #include <editor/editor.h>
#include <core/core.h> #include <core/core.h>
#include <graphics/graphics.h> #include <graphics/graphics.h>
#include <sstream>
#include <utils/stb/std_image_write.h> #include <utils/stb/std_image_write.h>
@ -21,14 +22,56 @@ namespace lunarium { namespace editor
TileSetView::TileSetView(MapEditor* editor) TileSetView::TileSetView(MapEditor* editor)
: Panel(gui::PanelType::PT_TILE_SET_VIEW, "Tile Set View", gui::PanelDockZone::DDZ_RIGHT, true), : Panel(gui::PanelType::PT_TILE_SET_VIEW, "Tile Set View", gui::PanelDockZone::DDZ_RIGHT, true),
mpEditor(editor), mpTileSet(nullptr), mpViewImage(nullptr), mFrameBuffer(-1), mpEditor(editor), mpTileSet(nullptr), mpViewImage(nullptr), mFrameBuffer(-1),
mViewOffset({0, 0}), mViewZoom(1.0f) mViewOffset({0, 0}), mViewZoom(1.0f), mMouseDown(false)
{ {
} }
void TileSetView::Update(float delta) void TileSetView::Update(float delta)
{ {
if (!mpViewImage) if (ImGui::GetIO().MouseDown[ImGuiMouseButton_Left] && !mMouseDown)
{
mMouseDown = true;
float x = ImGui::GetMousePos().x;
float y = ImGui::GetMousePos().y;
//Logger::Log(Editor::LogCat, LogLevel::INFO_VERBOSE, "Mouse clicked at (%f, %f)", x, y);
// Adjust for window pos
x -= mWorkAreaPos.X;
y -= mWorkAreaPos.Y;
//Logger::Log(Editor::LogCat, LogLevel::INFO_VERBOSE, "Mouse Pos Adjusted (%f, %f)", x, y);
// Check that it's within the window
bool is_in_window = x >= 0.0f && x < mWorkAreaSize.X && y >= 0.0f && y < mWorkAreaSize.Y;
if (is_in_window && mpTileSet)
{
// Adjust for scrolling
x += mScrollOffset.X;
y += mScrollOffset.Y;
//Logger::Log(Editor::LogCat, LogLevel::INFO_VERBOSE, "Mouse Pos scrolled (%f, %f)", x, y);
// Find selected tile
mSelectedTile.X = x / mpTileSet->GetTileSize().Width;
mSelectedTile.Y = y / mpTileSet->GetTileSize().Height;
//Logger::Log(Editor::LogCat, LogLevel::INFO_VERBOSE, "Updating Tile Selection: tile (%d, %d)", mSelectedTile.X, mSelectedTile.Y);
Invalidate();
}
}
if (!ImGui::GetIO().MouseDown[ImGuiMouseButton_Left] && mMouseDown)
{
//Logger::Log(Editor::LogCat, LogLevel::INFO_VERBOSE, "Mouse Released");
mMouseDown = false;
}
if (mInvalidate)
{ {
if (mpTileSet) if (mpTileSet)
{ {
@ -37,17 +80,29 @@ namespace lunarium { namespace editor
mFrameBuffer = Core::Graphics().CreateRenderTexture(mpTileSet->GetImage()->GetWidth(), mpTileSet->GetImage()->GetHeight(), 4); mFrameBuffer = Core::Graphics().CreateRenderTexture(mpTileSet->GetImage()->GetWidth(), mpTileSet->GetImage()->GetHeight(), 4);
} }
//Logger::Log(Editor::LogCat, LogLevel::INFO_VERBOSE, "Invalidating tile set window");
Color prev = Core::Graphics().GetClearColor(); Color prev = Core::Graphics().GetClearColor();
Core::Graphics().SetClearColor(Color::Transparent()); Core::Graphics().SetClearColor(Color::Transparent());
Core::GetInstance().BeginRenderToTexture(mFrameBuffer).LogIfFailed(Editor::LogCat); Core::GetInstance().BeginRenderToTexture(mFrameBuffer).LogIfFailed(Editor::LogCat);
mpTileSet->Render(&Core::Graphics()); mpTileSet->Render(&Core::Graphics());
// Draw selected tile highlight
if (mSelectedTile.X >= 0 && mSelectedTile.Y >= 0)
{
Rectangle selection = mpTileSet->GetTileRect(mSelectedTile);
Core::Graphics().DrawBox(selection, Color::Red(), 1.5f);
}
mpViewImage = Core::GetInstance().EndRenderToTexture(); mpViewImage = Core::GetInstance().EndRenderToTexture();
Core::Graphics().SetClearColor(prev); Core::Graphics().SetClearColor(prev);
//stbi_flip_vertically_on_write(1); // //stbi_flip_vertically_on_write(1);
stbi_write_png("tileset_test_image.png", mpViewImage->GetWidth(), mpViewImage->GetHeight(), 4, // stbi_write_png("tileset_test_image.png", mpViewImage->GetWidth(), mpViewImage->GetHeight(), 4,
mpViewImage->GetData(), mpViewImage->GetWidth() * 4); // mpViewImage->GetData(), mpViewImage->GetWidth() * 4);
} }
mInvalidate = false;
} }
} }
@ -56,31 +111,74 @@ namespace lunarium { namespace editor
if (!mIsOpen) if (!mIsOpen)
return false; return false;
if (!ImGui::Begin(GetName(), &mIsOpen)) ImGui::PushStyleVar( ImGuiStyleVar_WindowRounding, 0.0f );
ImGui::PushStyleVar( ImGuiStyleVar_WindowBorderSize, 0.0f );
ImGui::PushStyleVar( ImGuiStyleVar_WindowPadding, ImVec2( 0.0f, 0.0f ) );
if (!ImGui::Begin(GetName(), &mIsOpen, ImGuiWindowFlags_HorizontalScrollbar))
{ {
ImGui::PopStyleVar(3);
ImGui::End(); ImGui::End();
return false; return false;
} }
ImVec2 pos = ImGui::GetWindowPos(); ImGui::PopStyleVar(3);
ImVec2 size = ImGui::GetWindowSize(); mWorkAreaPos = { ImGui::GetWindowPos().x, ImGui::GetWindowPos().y };
mWorkAreaSize = { ImGui::GetWindowSize().x, ImGui::GetWindowSize().y };
mScrollOffset = { ImGui::GetScrollX(), ImGui::GetScrollY() };
// Adjust pos and size to account for the title bar
mWorkAreaPos.Y += ImGui::GetFrameHeight();
mWorkAreaSize.Y -= ImGui::GetFrameHeight();
// Adjust for info box
// float child_height = 90.0f;
// size.y -= child_height;
// ImGuiIO& io = ImGui::GetIO();
// float x = io.MousePos.x - pos.x;
// float y = io.MousePos.y - pos.y;
// Adjust for the tab bar // bool is_in_window = x >= 0.0f && x < size.x && y >= 0.0f && y < size.y;
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;
// // Check for mouse click
// if (ImGui::IsMouseDown(ImGuiMouseButton_Left) && is_in_window && mpTileSet)
// {
// // Adjust for scrolling
// x += ImGui::GetScrollX();
// y += ImGui::GetScrollY();
// // Find selected tile
// mSelectedTile.X = x / mpTileSet->GetTileSize().Width;
// mSelectedTile.Y = y / mpTileSet->GetTileSize().Height;
// Invalidate();
// }
if (mpViewImage) if (mpViewImage)
{ {
ImGui::Image((ImTextureID)mpViewImage->GetGLTextureID64(), ImVec2(mpViewImage->GetWidth(), mpViewImage->GetHeight())); ImGui::Image((ImTextureID)mpViewImage->GetGLTextureID64(),
ImVec2(mpViewImage->GetWidth(), mpViewImage->GetHeight()), ImVec2(0, 1), ImVec2(1, 0)); // the last 2 params are flipping the image on the y
} }
// TODO: If a tile on the map was changed this frame null out the canvas image so it will be redrawn // TODO: If a tile on the map was changed this frame null out the canvas image so it will be redrawn
// ImGui::SetNextWindowPos(ImVec2(ImGui::GetWindowPos().x, ImGui::GetWindowPos().y + (ImGui::GetWindowSize().y - child_height)), ImGuiCond_Always);
// ImGui::BeginChild(ImGui::GetID(GetName()), ImVec2(size.x, child_height), true);
// std::ostringstream oss;
// oss << "Mouse Pos: (" << x << ", " << y << ")";
// if (mpTileSet)
// {
// oss << "\nSelected Tile Would be: (" << (int)x / mpTileSet->GetTileSize().Width << ", " << (int)y / mpTileSet->GetTileSize().Height << ")";
// Rectangle selection = mpTileSet->GetTileRect(mSelectedTile);
// oss << "\n Selection: (" << selection.left() << ", " << selection.top()
// << ") size: (" << selection.HalfWidth * 2 << ", " << selection.HalfHeight * 2 << ")";
// }
// ImGui::TextUnformatted(oss.str().c_str());
// ImGui::EndChild();
ImGui::End(); ImGui::End();
return mIsOpen; return mIsOpen;
} }
@ -88,6 +186,7 @@ namespace lunarium { namespace editor
void TileSetView::SetTileSet(TileSet* set) void TileSetView::SetTileSet(TileSet* set)
{ {
mpTileSet = set; mpTileSet = set;
Invalidate(true);
} }
TileSet* TileSetView::GetTileSet() TileSet* TileSetView::GetTileSet()
@ -95,10 +194,16 @@ namespace lunarium { namespace editor
return mpTileSet; return mpTileSet;
} }
Vec2i TileSetView::GetSelectedTile()
{
return mSelectedTile;
}
void TileSetView::Invalidate(bool remake_frame_buffer) void TileSetView::Invalidate(bool remake_frame_buffer)
{ {
mpViewImage = nullptr; mInvalidate = true;
if (remake_frame_buffer) if (remake_frame_buffer && mFrameBuffer != -1)
{ {
Core::Graphics().DestroyRenderTexture(mFrameBuffer); Core::Graphics().DestroyRenderTexture(mFrameBuffer);
mFrameBuffer = -1; mFrameBuffer = -1;

@ -11,6 +11,7 @@
#include <gui/panel.h> #include <gui/panel.h>
#include <core/types.h> #include <core/types.h>
#include <utils/highResTimer.h>
namespace lunarium { class Image; } namespace lunarium { class Image; }
@ -29,15 +30,24 @@ namespace lunarium { namespace editor
void SetTileSet(TileSet* set); void SetTileSet(TileSet* set);
TileSet* GetTileSet(); TileSet* GetTileSet();
Vec2i GetSelectedTile();
void Invalidate(bool remake_frame_buffer = false); void Invalidate(bool remake_frame_buffer = false);
private: private:
MapEditor* mpEditor; MapEditor* mpEditor;
TileSet* mpTileSet; TileSet* mpTileSet;
int mFrameBuffer; int mFrameBuffer;
bool mInvalidate;
Image* mpViewImage; Image* mpViewImage;
Vec2i mViewOffset; Vec2i mViewOffset;
float mViewZoom; float mViewZoom;
Vec2i mSelectedTile;
bool mMouseDown;
Vec2f mWorkAreaPos;
Vec2f mWorkAreaSize;
Vec2f mScrollOffset;
}; };
}} }}

@ -72,6 +72,11 @@ namespace lunarium
} }
} }
if (Core::Input().IsKeyDown(KeyCode::MOUSE_LEFT_BUTTON, true))
{
Logger::Log(mLogCat, LogLevel::INFO_VERBOSE, "Left mouse button pressed!");
}
mpScene->OnTick(delta); mpScene->OnTick(delta);
} }
@ -80,6 +85,12 @@ namespace lunarium
mpScene->OnRender(g); mpScene->OnRender(g);
} }
void Tester::OnKeyPress(InputManager::KeyPress kp)
{
Logger::Log(mLogCat, LogLevel::INFO_VERBOSE, "Key Press Event: %s", kp.Key.Name.c_str());
}
} }

@ -25,6 +25,7 @@ namespace lunarium
void Shutdown(); void Shutdown();
void OnTick(double delta); void OnTick(double delta);
void OnRender(IGraphics* g); void OnRender(IGraphics* g);
void OnKeyPress(InputManager::KeyPress kp);
void SwitchScene(int id); void SwitchScene(int id);

@ -132,8 +132,13 @@ namespace lunarium
glfwSetCursorPos(mpWindow, pos.x, pos.y); glfwSetCursorPos(mpWindow, pos.x, pos.y);
} }
bool Window::IsKeyDown(int key) const bool Window::IsKeyDown(int key, bool mouse_button) const
{ {
if (mouse_button)
{
return glfwGetMouseButton(mpWindow, key) == GLFW_PRESS;
}
return glfwGetKey(mpWindow, key) == GLFW_PRESS; return glfwGetKey(mpWindow, key) == GLFW_PRESS;
} }

@ -28,7 +28,7 @@ namespace lunarium
bool IsInit() const; bool IsInit() const;
GLFWwindow* GetWindow(); GLFWwindow* GetWindow();
bool IsKeyDown(int key) const; bool IsKeyDown(int key, bool mouse_button = false) const;
glm::vec2 GetCursorPos() const; glm::vec2 GetCursorPos() const;
void SetCursorPos(glm::vec2 pos); void SetCursorPos(glm::vec2 pos);

Loading…
Cancel
Save