From 07f49d9e7ed3252d1494cb2595b4c73c0d990377 Mon Sep 17 00:00:00 2001 From: MrLetsplay Date: Sun, 15 Oct 2023 21:27:50 +0200 Subject: [PATCH] Fix element click & focus --- src/kekengine/cpp/common/defaults.cpp | 20 +++++--------------- src/kekengine/cpp/ui/ui.cpp | 17 +++++------------ src/kekengine/include/ui.h | 2 +- 3 files changed, 11 insertions(+), 28 deletions(-) diff --git a/src/kekengine/cpp/common/defaults.cpp b/src/kekengine/cpp/common/defaults.cpp index 9835e68..346bfa6 100644 --- a/src/kekengine/cpp/common/defaults.cpp +++ b/src/kekengine/cpp/common/defaults.cpp @@ -52,7 +52,6 @@ static void doUIMouseHover(double x, double y) { static void doUIMouseClick(double x, double y, GLFWMouseButton button) { UIElement *clickedElement = nullptr; - UIElement *focusedElement = nullptr; for(UIElement *element : kekData.ui->elements) { UIPoint childPos = element->getPosition(); @@ -61,28 +60,19 @@ static void doUIMouseClick(double x, double y, GLFWMouseButton button) { UIBounds bounds = element->getBounds(); if(!bounds.contains(clickedAt)) continue; - if(element->clickable) clickedElement = element; - if(element->focusable) focusedElement = element; + clickedElement = element; } if(clickedElement) { UIPoint childPos = clickedElement->getPosition(); UIPoint clickedAt = UIPoint((int) x - childPos.x, (int) y - childPos.y); clickedElement->clickAll(clickedAt, UIPoint((int) x, (int) y), button); - } - if(button == GLFWMouseButton::LEFT && focusedElement != kekData.ui->focusedElement) { - if(kekData.ui->focusedElement != nullptr) { - kekData.ui->focusedElement->focusExit(); + if(button == GLFWMouseButton::LEFT) { + clickedElement->focusEnterAll(clickedAt, UIPoint((int) x, (int) y)); } - - if(focusedElement) { - UIPoint childPos = focusedElement->getPosition(); - UIPoint clickedAt = UIPoint((int) x - childPos.x, (int) y - childPos.y); - focusedElement->focusEnterAll(clickedAt, UIPoint((int) x, (int) y)); - } - - kekData.ui->focusedElement = focusedElement; + } else { + UI::unfocusElement(kekData.ui->focusedElement); } } diff --git a/src/kekengine/cpp/ui/ui.cpp b/src/kekengine/cpp/ui/ui.cpp index 9331465..9263639 100644 --- a/src/kekengine/cpp/ui/ui.cpp +++ b/src/kekengine/cpp/ui/ui.cpp @@ -175,7 +175,6 @@ void UIElement::hoverExitAll() { void UIElement::clickAll(UIPoint pos, UIPoint screenPos, GLFWMouseButton button) { UIElement *clickedChild = nullptr; for(UIElement *child : children) { - if(!child->clickable) continue; UIPoint childPos = child->getPosition(); int relX = pos.x - childPos.x; int relY = pos.y - childPos.y; @@ -187,12 +186,12 @@ void UIElement::clickAll(UIPoint pos, UIPoint screenPos, GLFWMouseButton button) if(clickedChild != nullptr) { UIPoint childPos = clickedChild->getPosition(); clickedChild->clickAll(UIPoint(pos.x - childPos.x, pos.y - childPos.y), screenPos, button); - } else { + } else if(clickable) { click(pos, screenPos, button); } } -bool UIElement::focusEnterAll(UIPoint pos, UIPoint screenPos) { +void UIElement::focusEnterAll(UIPoint pos, UIPoint screenPos) { UIElement *focusedChild = nullptr; for(UIElement *child : children) { UIPoint childPos = child->getPosition(); @@ -203,18 +202,12 @@ bool UIElement::focusEnterAll(UIPoint pos, UIPoint screenPos) { focusedChild = child; } - if(focusedChild) { + if(focusedChild != nullptr) { UIPoint childPos = focusedChild->getPosition(); - bool focusable = focusedChild->focusEnterAll(UIPoint(pos.x - childPos.x, pos.y - childPos.y), screenPos); - if(focusable) return true; - } - - if(focusable) { + focusedChild->focusEnterAll(UIPoint(pos.x - childPos.x, pos.y - childPos.y), screenPos); + } else if(focusable) { UI::focusElement(this); - return true; } - - return false; } UIElement *UIElement::dragEnterAll(UIPoint pos, UIPoint screenPos) { diff --git a/src/kekengine/include/ui.h b/src/kekengine/include/ui.h index fc55828..3c7a439 100644 --- a/src/kekengine/include/ui.h +++ b/src/kekengine/include/ui.h @@ -184,7 +184,7 @@ class UIElement { void clickAll(UIPoint pos, UIPoint screenPos, GLFWMouseButton button); virtual void click(UIPoint pos, UIPoint screenPos, GLFWMouseButton button){}; - bool focusEnterAll(UIPoint pos, UIPoint screenPos); + void focusEnterAll(UIPoint pos, UIPoint screenPos); virtual void focusEnter(){}; virtual void focusExit(){};