Changed drag area API to a hit-testing API.

There were several good arguments for this: it's how Windows works with
 WM_NCHITTEST, SDL doesn't need to manage a list of rects, it allows more
 control over the regions (how do you use rects to cleanly surround a circular
 button?), the callback can be more optimized than a iterating a list of
 rects, and you don't have to send an updated list of rects whenever the
 window resizes or layout changes.
This commit is contained in:
Ryan C. Gordon
2014-05-28 01:22:47 -04:00
parent 7a4ddcd8c6
commit 98c03f391d
15 changed files with 106 additions and 116 deletions

View File

@@ -12,7 +12,6 @@ TARGETS = \
loopwave$(EXE) \
testaudioinfo$(EXE) \
testautomation$(EXE) \
testdragareas$(EXE) \
testdraw2$(EXE) \
testdrawchessboard$(EXE) \
testdropfile$(EXE) \
@@ -24,6 +23,7 @@ TARGETS = \
testgles$(EXE) \
testgles2$(EXE) \
testhaptic$(EXE) \
testhittesting$(EXE) \
testrumble$(EXE) \
testhotplug$(EXE) \
testthread$(EXE) \
@@ -109,7 +109,7 @@ testintersections$(EXE): $(srcdir)/testintersections.c
testrelative$(EXE): $(srcdir)/testrelative.c
$(CC) -o $@ $^ $(CFLAGS) $(LIBS)
testdragareas$(EXE): $(srcdir)/testdragareas.c
testhittesting$(EXE): $(srcdir)/testhittesting.c
$(CC) -o $@ $^ $(CFLAGS) $(LIBS)
testdraw2$(EXE): $(srcdir)/testdraw2.c

View File

@@ -3,28 +3,42 @@
/* !!! FIXME: rewrite this to be wired in to test framework. */
const SDL_Rect drag_areas[] = {
{ 20, 20, 100, 100 },
{ 200, 70, 100, 100 },
{ 400, 90, 100, 100 }
};
static const SDL_Rect *areas = drag_areas;
static int numareas = SDL_arraysize(drag_areas);
static SDL_HitTestResult
hitTest(SDL_Window *window, const SDL_Point *pt, void *data)
{
int i;
for (i = 0; i < numareas; i++) {
if (SDL_PointInRect(pt, &areas[i])) {
return SDL_HITTEST_DRAGGABLE;
}
}
return SDL_HITTEST_NORMAL;
}
int main(int argc, char **argv)
{
int done = 0;
SDL_Window *window;
SDL_Renderer *renderer;
const SDL_Rect drag_areas[] = {
{ 20, 20, 100, 100 },
{ 200, 70, 100, 100 },
{ 400, 90, 100, 100 }
};
const SDL_Rect *areas = drag_areas;
int numareas = SDL_arraysize(drag_areas);
/* !!! FIXME: check for errors. */
SDL_Init(SDL_INIT_VIDEO);
window = SDL_CreateWindow("Drag the red boxes", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_BORDERLESS);
renderer = SDL_CreateRenderer(window, -1, 0);
if (SDL_SetWindowDragAreas(window, areas, numareas) == -1) {
fprintf(stderr, "Setting drag areas failed!\n");
if (SDL_SetWindowHitTest(window, hitTest, NULL) == -1) {
fprintf(stderr, "Enabling hit-testing failed!\n");
SDL_Quit();
return 1;
}
@@ -69,11 +83,6 @@ int main(int argc, char **argv)
areas = NULL;
numareas = 0;
}
if (SDL_SetWindowDragAreas(window, areas, numareas) == -1) {
fprintf(stderr, "Setting drag areas failed!\n");
SDL_Quit();
return 1;
}
}
break;