Added mouse position relative to viewport

This commit is contained in:
Antoine Pilote
2024-09-12 13:17:47 -04:00
parent c7621b31e0
commit 5e1483e111
6 changed files with 54 additions and 17 deletions

View File

@@ -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()

View File

@@ -137,6 +137,9 @@ namespace Nuake
private:
static bool m_MouseButtons[5];
static std::map<int, bool> 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();

View File

@@ -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<Canvas> 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);

View File

@@ -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<int, float, bool, std::string, char>& var, const std::string& str);
void IterateOverElement(tinyxml2::XMLElement* e, NodePtr node);

View File

@@ -11,11 +11,11 @@ using namespace NuakeUI;
std::shared_ptr<StyleSheet> 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();

View File

@@ -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