Menu mouse capturing

This commit is contained in:
Mid 2025-08-28 19:14:02 +03:00
parent 6d5ba6037c
commit bf60cac302
2 changed files with 66 additions and 35 deletions

View File

@ -296,8 +296,12 @@ static bool screen_ev(struct k3MEvent *ev, uint8_t *ud) {
struct k3MScreen *this = (void*) ev->target;
if(ev->code == k3M_EVENT_MOUSE_PRESS || ev->code == k3M_EVENT_MOUSE_RELEASE || ev->code == k3M_EVENT_MOUSE_MOTION) {
struct k3MObj *innermost = (void*) this;
struct k3MObj *innermost;
if(this->mouseCapture) {
innermost = this->mouseCapture;
} else {
innermost = (struct k3MObj*) this;
while(innermost->childCount != 0) {
bool foundInner = false;
@ -322,6 +326,7 @@ static bool screen_ev(struct k3MEvent *ev, uint8_t *ud) {
}
}
}
if(innermost != this->lastHover) {
if(this->lastHover) {
@ -455,6 +460,18 @@ struct k3MScreen *k3MScreen() {
return ret;
}
bool k3MCaptureMouse(struct k3MScreen *screen, struct k3MObj *capt) {
if(screen->mouseCapture) {
return false;
}
screen->mouseCapture = capt;
return true;
}
void k3MReleaseMouse(struct k3MScreen *screen) {
screen->mouseCapture = NULL;
}
static bool textbutton_draw(struct k3MEvent *ev, uint8_t *ud) {
struct k3MTextButton *this = (void*) ev->target;
@ -647,6 +664,8 @@ static bool scrollbox_mouseshit(struct k3MEvent *ev, uint8_t *ud) {
struct k3MScrollbox *this = (void*) ev->target;
if(ev->code == k3M_EVENT_MOUSE_PRESS) {
if(k3MCaptureMouse((struct k3MScreen*) ev->original, this)) {
struct k3MProperty *prop = k3MFindProperty(ev->target, k3M_PROP_SCROLL_ANYWHERE, false);
bool scrollAnywhere = prop ? prop->si[0] : false;
@ -666,8 +685,16 @@ static bool scrollbox_mouseshit(struct k3MEvent *ev, uint8_t *ud) {
this->mouseX = ev->mouse.x;
this->mouseY = ev->mouse.y;
return !!this->mouseHeld;
}
} else if(ev->code == k3M_EVENT_MOUSE_RELEASE) {
if(this->mouseHeld) {
this->mouseHeld = false;
k3MReleaseMouse((struct k3MScreen*) ev->original);
return true;
}
} else if(ev->code == k3M_EVENT_MOUSE_MOTION) {
if(this->mouseHeld) {
@ -694,6 +721,8 @@ static bool scrollbox_mouseshit(struct k3MEvent *ev, uint8_t *ud) {
k3MArrange(contentBox);
return true;
}
}

View File

@ -179,6 +179,8 @@ struct k3MScreen {
struct k3MObj *mouseCapture;
};
struct k3MScreen *k3MScreen();
bool k3MCaptureMouse(struct k3MScreen*, struct k3MObj*);
void k3MReleaseMouse(struct k3MScreen*);
struct k3MTextButton {
struct k3MObj;