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) {
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);
}
}

View File

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

View File

@ -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(){};