Skip to content

implement hold state #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 51 additions & 7 deletions src/engine/ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,22 +295,36 @@ namespace UI
} \
} while(0)

#define loopinchildren(o, cx, cy, body) \
#define loopinchildren(o, cx, cy, body, inside) \
loopchildren(o, \
{ \
float o##x = cx - o->x; \
float o##y = cy - o->y; \
if(!inside) \
{ \
/* clamp ox, oy to o.w, o.h if needed */ \
/* ox = clamp(ox, 0.0f, o.w); */ \
/* oy = clamp(ox, 0.0f, o.h); */ \
body; \
} \
if(o##x >= 0 && o##x < o->w && o##y >= 0 && o##y < o->h) \
{ \
body; \
} \
})

#define loopinchildrenrev(o, cx, cy, body) \
#define loopinchildrenrev(o, cx, cy, body, inside) \
loopchildrenrev(o, \
{ \
float o##x = cx - o->x; \
float o##y = cy - o->y; \
if(!inside) \
{ \
/* clamp ox, oy to o.w, o.h if needed */ \
/* ox = clamp(ox, 0.0f, o.w); */ \
/* oy = clamp(ox, 0.0f, o.h); */ \
body; \
} \
if(o##x >= 0 && o##x < o->w && o##y >= 0 && o##y < o->h) \
{ \
body; \
Expand Down Expand Up @@ -372,7 +386,7 @@ namespace UI
{
Object *c = o->target(ox, oy);
if(c) return c;
});
}, true);
return NULL;
}

Expand Down Expand Up @@ -412,22 +426,35 @@ namespace UI
Object *c = o->hover(ox, oy);
if(c == o) { hoverx = ox; hovery = oy; }
if(c) return c;
});
}, true);
return NULL;
}

virtual void hovering(float cx, float cy)
{
}

virtual void hold(float cx, float cy, float &hx, float &hy, Object *c)
{
loopinchildrenrev(o, cx, cy,
{
if (o == c) { hx = ox; hy = oy; return; }
o->hold(ox, oy, hx, hy, c);
}, false);
}

virtual void holding(float cx, float cy)
{
}

virtual Object *select(float cx, float cy)
{
loopinchildrenrev(o, cx, cy,
{
Object *c = o->select(ox, oy);
if(c == o) { selectx = ox; selecty = oy; }
if(c) return c;
});
}, true);
return NULL;
}

Expand Down Expand Up @@ -494,10 +521,20 @@ namespace UI
Object *c = o->hover(ox, oy);
if(c == o) { hoverx = ox; hovery = oy; }
return c;
});
}, true);
return NULL;
}

virtual void hold(float cx, float cy, float &hx, float &hy, Object *c)
{
loopinchildrenrev(o, cx, cy,
{
if(!o->takesinput()) continue;
if (o == c) { hx = ox; hy = oy; return; }
o->hold(ox, oy, hx, hy, c);
}, false);
}

Object *select(float cx, float cy)
{
loopinchildrenrev(o, cx, cy,
Expand All @@ -507,7 +544,7 @@ namespace UI
if(c && i < children.length() - 1) { children.removeobj(o); children.add(o); }
if(c == o) { selectx = ox; selecty = oy; }
return c;
});
}, true);
return NULL;
}

Expand Down Expand Up @@ -3076,6 +3113,13 @@ namespace UI
{
hovering = world->hover(cursorx*world->w, cursory*world->h);
if(hovering) hovering->hovering(hoverx, hovery);
if(selected)
{
float hx, hy;
world->hold(cursorx*world->w, cursory*world->h, hx, hy,
selected);
selected->holding(hx, hy);
}
}
else hovering = selected = NULL;

Expand Down