From 5e1483e111197d7fe02b3bd49caa0e6d2a057787 Mon Sep 17 00:00:00 2001 From: Antoine Pilote Date: Thu, 12 Sep 2024 13:17:47 -0400 Subject: [PATCH] Added mouse position relative to viewport --- Nuake/src/Core/Input.cpp | 25 ++++++++++++++++++++++- Nuake/src/Core/Input.h | 11 ++++++++++ Nuake/src/UI/Parsers/CanvasParser.cpp | 25 +++++++++++++---------- Nuake/src/UI/Parsers/CanvasParser.h | 2 +- Nuake/src/UI/Parsers/StyleSheetParser.cpp | 4 ++-- Nuake/src/UI/UIInputManager.h | 4 ++-- 6 files changed, 54 insertions(+), 17 deletions(-) diff --git a/Nuake/src/Core/Input.cpp b/Nuake/src/Core/Input.cpp index c7e43063..40d9ed70 100644 --- a/Nuake/src/Core/Input.cpp +++ b/Nuake/src/Core/Input.cpp @@ -12,7 +12,9 @@ namespace Nuake bool Input::m_MouseButtons[5] = { false, false, false, false, false }; float Input::XScroll = 0.0f; float Input::YScroll = 0.0f; - + + Vector2 Input::ViewportPosition = Vector2(0, 0);; + Vector2 Input::ViewportSize = Vector2(0, 0); void Input::ScrollCallback(GLFWwindow* window, double xoffset, double yoffset) { XScroll = (float)xoffset; @@ -156,6 +158,27 @@ namespace Nuake glfwSetCursorPos(window, position.x, position.y); } + float Input::GetViewportMouseY() + { + return glm::clamp(GetMouseY() - ViewportPosition.y, 0.0f, ViewportSize.y); + } + + float Input::GetViewportMouseX() + { + return glm::clamp(GetMouseX() - ViewportPosition.x, 0.0f, ViewportSize.x); + } + + Vector2 Input::GetViewportMousePosition() + { + return glm::clamp(GetMousePosition() - ViewportPosition, { 0, 0 }, ViewportSize); + } + + void Input::SetViewportDimensions(const Vector2& pos, const Vector2& size) + { + ViewportPosition = pos; + ViewportSize = size; + } + #pragma endregion bool Input::Init() diff --git a/Nuake/src/Core/Input.h b/Nuake/src/Core/Input.h index cadab931..eec46f2e 100644 --- a/Nuake/src/Core/Input.h +++ b/Nuake/src/Core/Input.h @@ -137,6 +137,9 @@ namespace Nuake private: static bool m_MouseButtons[5]; static std::map m_Keys; + static Vector2 ViewportPosition; + static Vector2 ViewportSize; + public: static bool IsKeyPressed(Key keycode); static bool IsKeyDown(Key keycode); @@ -153,11 +156,19 @@ namespace Nuake static bool IsMouseButtonDown(int button); static bool IsMouseButtonReleased(int button); + // Get mouse position relative to window static float GetMouseX(); static float GetMouseY(); static Vector2 GetMousePosition(); static void SetMousePosition(const Vector2& position); + // Get mouse position relative to viewport + static float GetViewportMouseY(); + static float GetViewportMouseX(); + static Vector2 GetViewportMousePosition(); + static void SetViewportDimensions(const Vector2& pos, const Vector2& size); + + static bool Init(); static void Update(); diff --git a/Nuake/src/UI/Parsers/CanvasParser.cpp b/Nuake/src/UI/Parsers/CanvasParser.cpp index 8e120e97..af4d02d1 100644 --- a/Nuake/src/UI/Parsers/CanvasParser.cpp +++ b/Nuake/src/UI/Parsers/CanvasParser.cpp @@ -224,12 +224,12 @@ void CanvasParser::ScanFragment(tinyxml2::XMLElement* e, NodePtr node) } } -void CanvasParser::ScanCustomWidgets(tinyxml2::XMLElement* e, NodePtr node) +bool CanvasParser::ScanCustomWidgets(tinyxml2::XMLElement* e, NodePtr node) { const std::string nodeTypeName = e->Value(); if (!ScriptingEngineNet::Get().HasUIWidget(nodeTypeName)) { - return; + return false; } // Is template file valid? @@ -237,14 +237,9 @@ void CanvasParser::ScanCustomWidgets(tinyxml2::XMLElement* e, NodePtr node) if (!FileSystem::FileExists(customWidget.htmlPath)) { Logger::Log("Custom widget html file doesnt exist: " + nodeTypeName + " with HTML path: " + customWidget.htmlPath, "ui", CRITICAL); - return; + return false; } - // Allow to link between C# script and node using a UUID - UUID scriptingId = UUID(); - node->SetScriptingID(scriptingId); - customWidgetIDs.push_back(std::make_pair(scriptingId, nodeTypeName)); - // Parse load HTML file now const std::string& absoluteFilePath = FileSystem::RelativeToAbsolute(customWidget.htmlPath); tinyxml2::XMLDocument doc; @@ -256,6 +251,8 @@ void CanvasParser::ScanCustomWidgets(tinyxml2::XMLElement* e, NodePtr node) // Let's parse the file auto firstNode = doc.FirstChildElement(); IterateOverElement(firstNode, node); + + return true; } void CanvasParser::IterateOverElement(tinyxml2::XMLElement* e, NodePtr node) @@ -275,13 +272,19 @@ void CanvasParser::IterateOverElement(tinyxml2::XMLElement* e, NodePtr node) // Let's keep fragments for now as they remove // the need to create a C# class for simple templating. ScanFragment(current, node); + bool hasScript = ScanCustomWidgets(current, node); // Let's add custom widgets to the DOM. - ScanCustomWidgets(current, node); - NodePtr newNode = CreateNodeFromXML(current, id); if (newNode) { + if (hasScript) + { + UUID scriptingId = UUID(); + node->SetScriptingID(scriptingId); + customWidgetIDs.push_back(std::make_pair(scriptingId, current->Value())); + } + AddClassesToNode(current, newNode); AddModelIfToNode(current, newNode); AddModelClasses(current, newNode); @@ -338,7 +341,7 @@ Ref CanvasParser::Parse(const std::string& path) if (styleSheet) { std::string relativePath = path + "/../" + styleSheet->Value(); - if (FileSystem::FileExists(relativePath)) + if (FileSystem::FileExists(relativePath, true)) { auto styleSheet = StyleSheetParser::Get().Parse(relativePath); canvas->SetStyleSheet(styleSheet); diff --git a/Nuake/src/UI/Parsers/CanvasParser.h b/Nuake/src/UI/Parsers/CanvasParser.h index af1e9a42..8e115477 100644 --- a/Nuake/src/UI/Parsers/CanvasParser.h +++ b/Nuake/src/UI/Parsers/CanvasParser.h @@ -48,7 +48,7 @@ namespace NuakeUI private: void ScanFragment(tinyxml2::XMLElement* e, NodePtr node); - void ScanCustomWidgets(tinyxml2::XMLElement* e, NodePtr node); + bool ScanCustomWidgets(tinyxml2::XMLElement* e, NodePtr node); void WriteValueFromString(std::variant& var, const std::string& str); void IterateOverElement(tinyxml2::XMLElement* e, NodePtr node); diff --git a/Nuake/src/UI/Parsers/StyleSheetParser.cpp b/Nuake/src/UI/Parsers/StyleSheetParser.cpp index 17c09fc2..5d4a89c7 100644 --- a/Nuake/src/UI/Parsers/StyleSheetParser.cpp +++ b/Nuake/src/UI/Parsers/StyleSheetParser.cpp @@ -11,11 +11,11 @@ using namespace NuakeUI; std::shared_ptr StyleSheetParser::Parse(const std::string& path) { - assert(FileSystem::FileExists(path)); + assert(FileSystem::FileExists(path, true)); _parsingPath = path; - std::string fileContent = FileSystem::ReadFile(path); + std::string fileContent = FileSystem::ReadFile(path, true); auto data = katana_parse(fileContent.c_str(), fileContent.length(), KatanaParserModeStylesheet); auto styleSheet = StyleSheet::New(); diff --git a/Nuake/src/UI/UIInputManager.h b/Nuake/src/UI/UIInputManager.h index 1e8e520e..7516216a 100644 --- a/Nuake/src/UI/UIInputManager.h +++ b/Nuake/src/UI/UIInputManager.h @@ -34,12 +34,12 @@ namespace Nuake float GetMouseX() override { - return Input::GetMouseX(); + return Input::GetViewportMouseX(); } float GetMouseY() override { - return Input::GetMouseY(); + return Input::GetViewportMouseY(); } float GetScrollX() override