diff --git a/src/core/core.cpp b/src/core/core.cpp index 63c0c60..151cdba 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -131,6 +131,8 @@ namespace lunarium if (mErrorLogFile.is_open()) Logger::GetInstance()->AddListener(new FileListener(mErrorLogFile, LogLevel::ERROR | LogLevel::FATAL_ERROR)); + + Logger::GetInstance()->SetAllowRepeats(true); // Init the Debug log window OpRes result; diff --git a/src/input/inputManager.cpp b/src/input/inputManager.cpp index 0bcd46a..6113f98 100644 --- a/src/input/inputManager.cpp +++ b/src/input/inputManager.cpp @@ -55,7 +55,7 @@ namespace lunarium for (int i = 0; i < KeyCodeList.size(); 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 if (IsMouseButton(KeyCodeList[i])) diff --git a/src/internal_libs/utils/logger.cpp b/src/internal_libs/utils/logger.cpp index cf70e42..b82e4d8 100644 --- a/src/internal_libs/utils/logger.cpp +++ b/src/internal_libs/utils/logger.cpp @@ -209,6 +209,7 @@ namespace lunarium Logger::Logger() { + mAllowRepeats = false; // Register built-in levels RegisterLevel("INFO"); @@ -325,6 +326,11 @@ namespace lunarium mListeners.clear(); } + void Logger::SetAllowRepeats(bool allow) + { + mAllowRepeats = allow; + } + void Logger::ClearBacklog() { mBacklog.clear(); @@ -363,41 +369,45 @@ namespace lunarium bool track = true; // Check for repeats - for (int i = 0; i < mBacklog.size(); i++) + if (!mAllowRepeats) { - LogMessage& prevMsg = mBacklog[i]; - if (prevMsg.Message == lm.Message) + for (int i = 0; i < mBacklog.size(); i++) { - - if (prevMsg.RepeatAlertSent) - { - 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) + LogMessage& prevMsg = mBacklog[i]; + if (prevMsg.Message == lm.Message) { - 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!]"; - prevMsg.RepeatAlertSent = true; - track = false; + send = false; + break; } - 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; - break; + prevMsg.SentTimeStamp = timeStamp; + break; + } } } + if (!send) diff --git a/src/internal_libs/utils/logger.h b/src/internal_libs/utils/logger.h index 1a3beda..fcfcb3e 100644 --- a/src/internal_libs/utils/logger.h +++ b/src/internal_libs/utils/logger.h @@ -152,6 +152,7 @@ namespace lunarium void FreeAllListeners(); void ClearBacklog(); + void SetAllowRepeats(bool allow); // The message argument and the variable argument list work just like // printf. Use the same formatters as printf when calling this function. @@ -171,6 +172,7 @@ namespace lunarium std::vector mBacklog; char mBuffer[BUFFER_SIZE]; HighResTimer mTimer; + bool mAllowRepeats; private: Logger(); diff --git a/src/run_modes/editor/editor.cpp b/src/run_modes/editor/editor.cpp index 9a8da58..01c84a3 100644 --- a/src/run_modes/editor/editor.cpp +++ b/src/run_modes/editor/editor.cpp @@ -48,6 +48,8 @@ namespace editor // Initialize internal data DataManager::Initialize(); + ImGui::GetIO().ConfigWindowsMoveFromTitleBarOnly = true; + // Init editor panels mAboutPanel.SetOpen(false); 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 60c7ab1..0a5f388 100644 --- a/src/run_modes/editor/tools/map_editor/map_editor.cpp +++ b/src/run_modes/editor/tools/map_editor/map_editor.cpp @@ -211,7 +211,7 @@ namespace lunarium { namespace editor // For now just directly load the file and make a tileset out of it 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); Image* i = new Image(); 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 3d00f8b..ffc2c16 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 @@ -13,6 +13,7 @@ #include #include #include +#include #include @@ -21,14 +22,56 @@ 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) + mViewOffset({0, 0}), mViewZoom(1.0f), mMouseDown(false) { } 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) { @@ -37,17 +80,29 @@ namespace lunarium { namespace editor 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(); Core::Graphics().SetClearColor(Color::Transparent()); Core::GetInstance().BeginRenderToTexture(mFrameBuffer).LogIfFailed(Editor::LogCat); + 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(); Core::Graphics().SetClearColor(prev); - //stbi_flip_vertically_on_write(1); - stbi_write_png("tileset_test_image.png", mpViewImage->GetWidth(), mpViewImage->GetHeight(), 4, - mpViewImage->GetData(), mpViewImage->GetWidth() * 4); + // //stbi_flip_vertically_on_write(1); + // stbi_write_png("tileset_test_image.png", mpViewImage->GetWidth(), mpViewImage->GetHeight(), 4, + // mpViewImage->GetData(), mpViewImage->GetWidth() * 4); } + mInvalidate = false; } } @@ -56,31 +111,74 @@ namespace lunarium { namespace editor if (!mIsOpen) 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(); 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; - + ImGui::PopStyleVar(3); + 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; + + // bool is_in_window = x >= 0.0f && x < size.x && y >= 0.0f && y < size.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) { - 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 + // 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(); return mIsOpen; } @@ -88,6 +186,7 @@ namespace lunarium { namespace editor void TileSetView::SetTileSet(TileSet* set) { mpTileSet = set; + Invalidate(true); } TileSet* TileSetView::GetTileSet() @@ -95,10 +194,16 @@ namespace lunarium { namespace editor return mpTileSet; } + + Vec2i TileSetView::GetSelectedTile() + { + return mSelectedTile; + } + void TileSetView::Invalidate(bool remake_frame_buffer) { - mpViewImage = nullptr; - if (remake_frame_buffer) + mInvalidate = true; + if (remake_frame_buffer && mFrameBuffer != -1) { Core::Graphics().DestroyRenderTexture(mFrameBuffer); mFrameBuffer = -1; 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 index 15d4860..491e70c 100644 --- 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 @@ -11,6 +11,7 @@ #include #include +#include namespace lunarium { class Image; } @@ -29,15 +30,24 @@ namespace lunarium { namespace editor void SetTileSet(TileSet* set); TileSet* GetTileSet(); + Vec2i GetSelectedTile(); + void Invalidate(bool remake_frame_buffer = false); private: MapEditor* mpEditor; TileSet* mpTileSet; int mFrameBuffer; + bool mInvalidate; Image* mpViewImage; Vec2i mViewOffset; float mViewZoom; + Vec2i mSelectedTile; + + bool mMouseDown; + Vec2f mWorkAreaPos; + Vec2f mWorkAreaSize; + Vec2f mScrollOffset; }; }} diff --git a/src/run_modes/tester/tester.cpp b/src/run_modes/tester/tester.cpp index 7a4c7b8..eeb2d09 100644 --- a/src/run_modes/tester/tester.cpp +++ b/src/run_modes/tester/tester.cpp @@ -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); } @@ -79,6 +84,12 @@ namespace lunarium { mpScene->OnRender(g); } + + + void Tester::OnKeyPress(InputManager::KeyPress kp) + { + Logger::Log(mLogCat, LogLevel::INFO_VERBOSE, "Key Press Event: %s", kp.Key.Name.c_str()); + } } diff --git a/src/run_modes/tester/tester.h b/src/run_modes/tester/tester.h index 61e5dc0..ad8fba2 100644 --- a/src/run_modes/tester/tester.h +++ b/src/run_modes/tester/tester.h @@ -25,6 +25,7 @@ namespace lunarium void Shutdown(); void OnTick(double delta); void OnRender(IGraphics* g); + void OnKeyPress(InputManager::KeyPress kp); void SwitchScene(int id); diff --git a/src/window/window.cpp b/src/window/window.cpp index 5529fdd..1a4063e 100644 --- a/src/window/window.cpp +++ b/src/window/window.cpp @@ -132,8 +132,13 @@ namespace lunarium 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; } diff --git a/src/window/window.h b/src/window/window.h index ba4decc..dd0aae6 100644 --- a/src/window/window.h +++ b/src/window/window.h @@ -28,7 +28,7 @@ namespace lunarium bool IsInit() const; GLFWwindow* GetWindow(); - bool IsKeyDown(int key) const; + bool IsKeyDown(int key, bool mouse_button = false) const; glm::vec2 GetCursorPos() const; void SetCursorPos(glm::vec2 pos);