2022-11-26 02:41:46 -07:00
|
|
|
/*
|
|
|
|
Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
|
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any damages
|
|
|
|
arising from the use of this software.
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it
|
|
|
|
freely.
|
|
|
|
*/
|
2022-11-26 21:43:38 -07:00
|
|
|
#include <SDL3/SDL.h>
|
2022-12-14 21:58:20 -07:00
|
|
|
#include <SDL3/SDL_main.h>
|
2015-06-21 09:33:46 -06:00
|
|
|
|
|
|
|
/* !!! FIXME: rewrite this to be wired in to test framework. */
|
|
|
|
|
|
|
|
#define RESIZE_BORDER 20
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2017-08-13 22:06:52 -06:00
|
|
|
static SDL_HitTestResult SDLCALL
|
2015-06-21 09:33:46 -06:00
|
|
|
hitTest(SDL_Window *window, const SDL_Point *pt, void *data)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int w, h;
|
|
|
|
|
|
|
|
for (i = 0; i < numareas; i++) {
|
2022-12-27 12:01:11 -07:00
|
|
|
if (SDL_IsPointInRect(pt, &areas[i])) {
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_Log("HIT-TEST: DRAGGABLE\n");
|
|
|
|
return SDL_HITTEST_DRAGGABLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_GetWindowSize(window, &w, &h);
|
|
|
|
|
2022-11-30 13:51:59 -07:00
|
|
|
#define REPORT_RESIZE_HIT(name) \
|
|
|
|
{ \
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_Log("HIT-TEST: RESIZE_" #name "\n"); \
|
2022-11-30 13:51:59 -07:00
|
|
|
return SDL_HITTEST_RESIZE_##name; \
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pt->x < RESIZE_BORDER && pt->y < RESIZE_BORDER) {
|
|
|
|
REPORT_RESIZE_HIT(TOPLEFT);
|
|
|
|
} else if (pt->x > RESIZE_BORDER && pt->x < w - RESIZE_BORDER && pt->y < RESIZE_BORDER) {
|
|
|
|
REPORT_RESIZE_HIT(TOP);
|
|
|
|
} else if (pt->x > w - RESIZE_BORDER && pt->y < RESIZE_BORDER) {
|
|
|
|
REPORT_RESIZE_HIT(TOPRIGHT);
|
|
|
|
} else if (pt->x > w - RESIZE_BORDER && pt->y > RESIZE_BORDER && pt->y < h - RESIZE_BORDER) {
|
|
|
|
REPORT_RESIZE_HIT(RIGHT);
|
|
|
|
} else if (pt->x > w - RESIZE_BORDER && pt->y > h - RESIZE_BORDER) {
|
|
|
|
REPORT_RESIZE_HIT(BOTTOMRIGHT);
|
|
|
|
} else if (pt->x < w - RESIZE_BORDER && pt->x > RESIZE_BORDER && pt->y > h - RESIZE_BORDER) {
|
|
|
|
REPORT_RESIZE_HIT(BOTTOM);
|
|
|
|
} else if (pt->x < RESIZE_BORDER && pt->y > h - RESIZE_BORDER) {
|
|
|
|
REPORT_RESIZE_HIT(BOTTOMLEFT);
|
|
|
|
} else if (pt->x < RESIZE_BORDER && pt->y < h - RESIZE_BORDER && pt->y > RESIZE_BORDER) {
|
|
|
|
REPORT_RESIZE_HIT(LEFT);
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_Log("HIT-TEST: NORMAL\n");
|
|
|
|
return SDL_HITTEST_NORMAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int done = 0;
|
|
|
|
SDL_Window *window;
|
|
|
|
SDL_Renderer *renderer;
|
|
|
|
|
|
|
|
/* !!! 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 | SDL_WINDOW_RESIZABLE);
|
2022-12-13 21:26:49 -07:00
|
|
|
renderer = SDL_CreateRenderer(window, NULL, 0);
|
2015-06-21 09:33:46 -06:00
|
|
|
|
|
|
|
if (SDL_SetWindowHitTest(window, hitTest, NULL) == -1) {
|
|
|
|
SDL_Log("Enabling hit-testing failed!\n");
|
|
|
|
SDL_Quit();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2022-11-27 09:38:43 -07:00
|
|
|
while (!done) {
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_Event e;
|
|
|
|
int nothing_to_do = 1;
|
|
|
|
|
|
|
|
SDL_SetRenderDrawColor(renderer, 0, 0, 127, 255);
|
|
|
|
SDL_RenderClear(renderer);
|
|
|
|
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
|
|
|
|
SDL_RenderFillRects(renderer, areas, SDL_arraysize(drag_areas));
|
|
|
|
SDL_RenderPresent(renderer);
|
|
|
|
|
|
|
|
while (SDL_PollEvent(&e)) {
|
|
|
|
nothing_to_do = 0;
|
|
|
|
|
2022-11-30 13:51:59 -07:00
|
|
|
switch (e.type) {
|
|
|
|
case SDL_MOUSEBUTTONDOWN:
|
|
|
|
SDL_Log("button down!\n");
|
|
|
|
break;
|
2015-06-21 09:33:46 -06:00
|
|
|
|
2022-11-30 13:51:59 -07:00
|
|
|
case SDL_MOUSEBUTTONUP:
|
|
|
|
SDL_Log("button up!\n");
|
|
|
|
break;
|
2015-06-21 09:33:46 -06:00
|
|
|
|
2022-12-22 08:20:48 -07:00
|
|
|
case SDL_WINDOWEVENT_MOVED:
|
|
|
|
SDL_Log("Window event moved to (%d, %d)!\n", (int)e.window.data1, (int)e.window.data2);
|
2022-11-30 13:51:59 -07:00
|
|
|
break;
|
2015-06-21 09:33:46 -06:00
|
|
|
|
2022-11-30 13:51:59 -07:00
|
|
|
case SDL_KEYDOWN:
|
|
|
|
if (e.key.keysym.sym == SDLK_ESCAPE) {
|
2015-06-21 09:33:46 -06:00
|
|
|
done = 1;
|
2022-11-30 13:51:59 -07:00
|
|
|
} else if (e.key.keysym.sym == SDLK_x) {
|
|
|
|
if (areas == NULL) {
|
|
|
|
areas = drag_areas;
|
|
|
|
numareas = SDL_arraysize(drag_areas);
|
|
|
|
} else {
|
|
|
|
areas = NULL;
|
|
|
|
numareas = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SDL_QUIT:
|
|
|
|
done = 1;
|
|
|
|
break;
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nothing_to_do) {
|
|
|
|
SDL_Delay(50);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_Quit();
|
|
|
|
return 0;
|
|
|
|
}
|