Fix element click & focus

This commit is contained in:
MrLetsplay 2023-10-15 21:27:50 +02:00
parent 5b10076720
commit 07f49d9e7e
Signed by: mr
SSH Key Fingerprint: SHA256:92jBH80vpXyaZHjaIl47pjRq+Yt7XGTArqQg1V7hSqg
3 changed files with 11 additions and 28 deletions

View File

@ -52,7 +52,6 @@ static void doUIMouseHover(double x, double y) {
static void doUIMouseClick(double x, double y, GLFWMouseButton button) { static void doUIMouseClick(double x, double y, GLFWMouseButton button) {
UIElement *clickedElement = nullptr; UIElement *clickedElement = nullptr;
UIElement *focusedElement = nullptr;
for(UIElement *element : kekData.ui->elements) { for(UIElement *element : kekData.ui->elements) {
UIPoint childPos = element->getPosition(); UIPoint childPos = element->getPosition();
@ -61,28 +60,19 @@ static void doUIMouseClick(double x, double y, GLFWMouseButton button) {
UIBounds bounds = element->getBounds(); UIBounds bounds = element->getBounds();
if(!bounds.contains(clickedAt)) continue; if(!bounds.contains(clickedAt)) continue;
if(element->clickable) clickedElement = element; clickedElement = element;
if(element->focusable) focusedElement = element;
} }
if(clickedElement) { if(clickedElement) {
UIPoint childPos = clickedElement->getPosition(); UIPoint childPos = clickedElement->getPosition();
UIPoint clickedAt = UIPoint((int) x - childPos.x, (int) y - childPos.y); UIPoint clickedAt = UIPoint((int) x - childPos.x, (int) y - childPos.y);
clickedElement->clickAll(clickedAt, UIPoint((int) x, (int) y), button); clickedElement->clickAll(clickedAt, UIPoint((int) x, (int) y), button);
}
if(button == GLFWMouseButton::LEFT && focusedElement != kekData.ui->focusedElement) { if(button == GLFWMouseButton::LEFT) {
if(kekData.ui->focusedElement != nullptr) { clickedElement->focusEnterAll(clickedAt, UIPoint((int) x, (int) y));
kekData.ui->focusedElement->focusExit();
} }
} else {
if(focusedElement) { UI::unfocusElement(kekData.ui->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;
} }
} }

View File

@ -175,7 +175,6 @@ void UIElement::hoverExitAll() {
void UIElement::clickAll(UIPoint pos, UIPoint screenPos, GLFWMouseButton button) { void UIElement::clickAll(UIPoint pos, UIPoint screenPos, GLFWMouseButton button) {
UIElement *clickedChild = nullptr; UIElement *clickedChild = nullptr;
for(UIElement *child : children) { for(UIElement *child : children) {
if(!child->clickable) continue;
UIPoint childPos = child->getPosition(); UIPoint childPos = child->getPosition();
int relX = pos.x - childPos.x; int relX = pos.x - childPos.x;
int relY = pos.y - childPos.y; int relY = pos.y - childPos.y;
@ -187,12 +186,12 @@ void UIElement::clickAll(UIPoint pos, UIPoint screenPos, GLFWMouseButton button)
if(clickedChild != nullptr) { if(clickedChild != nullptr) {
UIPoint childPos = clickedChild->getPosition(); UIPoint childPos = clickedChild->getPosition();
clickedChild->clickAll(UIPoint(pos.x - childPos.x, pos.y - childPos.y), screenPos, button); clickedChild->clickAll(UIPoint(pos.x - childPos.x, pos.y - childPos.y), screenPos, button);
} else { } else if(clickable) {
click(pos, screenPos, button); click(pos, screenPos, button);
} }
} }
bool UIElement::focusEnterAll(UIPoint pos, UIPoint screenPos) { void UIElement::focusEnterAll(UIPoint pos, UIPoint screenPos) {
UIElement *focusedChild = nullptr; UIElement *focusedChild = nullptr;
for(UIElement *child : children) { for(UIElement *child : children) {
UIPoint childPos = child->getPosition(); UIPoint childPos = child->getPosition();
@ -203,18 +202,12 @@ bool UIElement::focusEnterAll(UIPoint pos, UIPoint screenPos) {
focusedChild = child; focusedChild = child;
} }
if(focusedChild) { if(focusedChild != nullptr) {
UIPoint childPos = focusedChild->getPosition(); UIPoint childPos = focusedChild->getPosition();
bool focusable = focusedChild->focusEnterAll(UIPoint(pos.x - childPos.x, pos.y - childPos.y), screenPos); focusedChild->focusEnterAll(UIPoint(pos.x - childPos.x, pos.y - childPos.y), screenPos);
if(focusable) return true; } else if(focusable) {
}
if(focusable) {
UI::focusElement(this); UI::focusElement(this);
return true;
} }
return false;
} }
UIElement *UIElement::dragEnterAll(UIPoint pos, UIPoint screenPos) { UIElement *UIElement::dragEnterAll(UIPoint pos, UIPoint screenPos) {

View File

@ -184,7 +184,7 @@ class UIElement {
void clickAll(UIPoint pos, UIPoint screenPos, GLFWMouseButton button); void clickAll(UIPoint pos, UIPoint screenPos, GLFWMouseButton button);
virtual void click(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 focusEnter(){};
virtual void focusExit(){}; virtual void focusExit(){};