🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
3.9 KiB

  1. #include <stdio.h>
  2. #include "SDL.h"
  3. /* !!! FIXME: rewrite this to be wired in to test framework. */
  4. #define RESIZE_BORDER 20
  5. const SDL_Rect drag_areas[] = {
  6. { 20, 20, 100, 100 },
  7. { 200, 70, 100, 100 },
  8. { 400, 90, 100, 100 }
  9. };
  10. static const SDL_Rect *areas = drag_areas;
  11. static int numareas = SDL_arraysize(drag_areas);
  12. static SDL_HitTestResult SDLCALL
  13. hitTest(SDL_Window *window, const SDL_Point *pt, void *data)
  14. {
  15. int i;
  16. int w, h;
  17. for (i = 0; i < numareas; i++) {
  18. if (SDL_PointInRect(pt, &areas[i])) {
  19. SDL_Log("HIT-TEST: DRAGGABLE\n");
  20. return SDL_HITTEST_DRAGGABLE;
  21. }
  22. }
  23. SDL_GetWindowSize(window, &w, &h);
  24. #define REPORT_RESIZE_HIT(name) \
  25. { \
  26. SDL_Log("HIT-TEST: RESIZE_" #name "\n"); \
  27. return SDL_HITTEST_RESIZE_##name; \
  28. }
  29. if (pt->x < RESIZE_BORDER && pt->y < RESIZE_BORDER) {
  30. REPORT_RESIZE_HIT(TOPLEFT);
  31. } else if (pt->x > RESIZE_BORDER && pt->x < w - RESIZE_BORDER && pt->y < RESIZE_BORDER) {
  32. REPORT_RESIZE_HIT(TOP);
  33. } else if (pt->x > w - RESIZE_BORDER && pt->y < RESIZE_BORDER) {
  34. REPORT_RESIZE_HIT(TOPRIGHT);
  35. } else if (pt->x > w - RESIZE_BORDER && pt->y > RESIZE_BORDER && pt->y < h - RESIZE_BORDER) {
  36. REPORT_RESIZE_HIT(RIGHT);
  37. } else if (pt->x > w - RESIZE_BORDER && pt->y > h - RESIZE_BORDER) {
  38. REPORT_RESIZE_HIT(BOTTOMRIGHT);
  39. } else if (pt->x < w - RESIZE_BORDER && pt->x > RESIZE_BORDER && pt->y > h - RESIZE_BORDER) {
  40. REPORT_RESIZE_HIT(BOTTOM);
  41. } else if (pt->x < RESIZE_BORDER && pt->y > h - RESIZE_BORDER) {
  42. REPORT_RESIZE_HIT(BOTTOMLEFT);
  43. } else if (pt->x < RESIZE_BORDER && pt->y < h - RESIZE_BORDER && pt->y > RESIZE_BORDER) {
  44. REPORT_RESIZE_HIT(LEFT);
  45. }
  46. SDL_Log("HIT-TEST: NORMAL\n");
  47. return SDL_HITTEST_NORMAL;
  48. }
  49. int main(int argc, char **argv)
  50. {
  51. int done = 0;
  52. SDL_Window *window;
  53. SDL_Renderer *renderer;
  54. /* !!! FIXME: check for errors. */
  55. SDL_Init(SDL_INIT_VIDEO);
  56. window = SDL_CreateWindow("Drag the red boxes", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_BORDERLESS | SDL_WINDOW_RESIZABLE);
  57. renderer = SDL_CreateRenderer(window, -1, 0);
  58. if (SDL_SetWindowHitTest(window, hitTest, NULL) == -1) {
  59. SDL_Log("Enabling hit-testing failed!\n");
  60. SDL_Quit();
  61. return 1;
  62. }
  63. while (!done) {
  64. SDL_Event e;
  65. int nothing_to_do = 1;
  66. SDL_SetRenderDrawColor(renderer, 0, 0, 127, 255);
  67. SDL_RenderClear(renderer);
  68. SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
  69. SDL_RenderFillRects(renderer, areas, SDL_arraysize(drag_areas));
  70. SDL_RenderPresent(renderer);
  71. while (SDL_PollEvent(&e)) {
  72. nothing_to_do = 0;
  73. switch (e.type) {
  74. case SDL_MOUSEBUTTONDOWN:
  75. SDL_Log("button down!\n");
  76. break;
  77. case SDL_MOUSEBUTTONUP:
  78. SDL_Log("button up!\n");
  79. break;
  80. case SDL_WINDOWEVENT:
  81. if (e.window.event == SDL_WINDOWEVENT_MOVED) {
  82. SDL_Log("Window event moved to (%d, %d)!\n", (int)e.window.data1, (int)e.window.data2);
  83. }
  84. break;
  85. case SDL_KEYDOWN:
  86. if (e.key.keysym.sym == SDLK_ESCAPE) {
  87. done = 1;
  88. } else if (e.key.keysym.sym == SDLK_x) {
  89. if (areas == NULL) {
  90. areas = drag_areas;
  91. numareas = SDL_arraysize(drag_areas);
  92. } else {
  93. areas = NULL;
  94. numareas = 0;
  95. }
  96. }
  97. break;
  98. case SDL_QUIT:
  99. done = 1;
  100. break;
  101. }
  102. }
  103. if (nothing_to_do) {
  104. SDL_Delay(50);
  105. }
  106. }
  107. SDL_Quit();
  108. return 0;
  109. }