testpopup.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely.
  9. */
  10. /* Simple program: Move N sprites around on the screen as fast as possible */
  11. #include <SDL3/SDL_main.h>
  12. #include <SDL3/SDL_test_common.h>
  13. #include <SDL3/SDL_test_font.h>
  14. #ifdef SDL_PLATFORM_EMSCRIPTEN
  15. #include <emscripten/emscripten.h>
  16. #endif
  17. #include <stdlib.h>
  18. #define MENU_WIDTH 120
  19. #define MENU_HEIGHT 300
  20. #define TOOLTIP_DELAY 500
  21. #define TOOLTIP_WIDTH 175
  22. #define TOOLTIP_HEIGHT 32
  23. static SDLTest_CommonState *state;
  24. static int num_menus;
  25. static Uint64 tooltip_timer;
  26. static int done;
  27. static const SDL_Color colors[] = {
  28. { 0x7F, 0x00, 0x00, 0xFF },
  29. { 0x00, 0x7F, 0x00, 0xFF },
  30. { 0x00, 0x00, 0x7F, 0xFF }
  31. };
  32. struct PopupWindow
  33. {
  34. SDL_Window *win;
  35. SDL_Window *parent;
  36. SDL_Renderer *renderer;
  37. int idx;
  38. };
  39. static struct PopupWindow *menus;
  40. static struct PopupWindow tooltip;
  41. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  42. static void quit(int rc)
  43. {
  44. SDL_free(menus);
  45. menus = NULL;
  46. SDLTest_CleanupTextDrawing();
  47. SDLTest_CommonQuit(state);
  48. /* Let 'main()' return normally */
  49. if (rc != 0) {
  50. exit(rc);
  51. }
  52. }
  53. static int get_menu_index_by_window(SDL_Window *window)
  54. {
  55. int i;
  56. for (i = 0; i < num_menus; ++i) {
  57. if (menus[i].win == window) {
  58. return i;
  59. }
  60. }
  61. return -1;
  62. }
  63. static bool window_is_root(SDL_Window *window)
  64. {
  65. int i;
  66. for (i = 0; i < state->num_windows; ++i) {
  67. if (window == state->windows[i]) {
  68. return true;
  69. }
  70. }
  71. return false;
  72. }
  73. static bool create_popup(struct PopupWindow *new_popup, bool is_menu)
  74. {
  75. SDL_Window *focus;
  76. SDL_Window *new_win;
  77. SDL_Renderer *new_renderer;
  78. const int w = is_menu ? MENU_WIDTH : TOOLTIP_WIDTH;
  79. const int h = is_menu ? MENU_HEIGHT : TOOLTIP_HEIGHT;
  80. const int v_off = is_menu ? 0 : 32;
  81. const SDL_WindowFlags flags = is_menu ? SDL_WINDOW_POPUP_MENU : SDL_WINDOW_TOOLTIP;
  82. float x, y;
  83. focus = SDL_GetMouseFocus();
  84. SDL_GetMouseState(&x, &y);
  85. new_win = SDL_CreatePopupWindow(focus,
  86. (int)x, (int)y + v_off, w, h, flags);
  87. if (new_win) {
  88. new_renderer = SDL_CreateRenderer(new_win, state->renderdriver);
  89. new_popup->win = new_win;
  90. new_popup->renderer = new_renderer;
  91. new_popup->parent = focus;
  92. return true;
  93. }
  94. SDL_zerop(new_popup);
  95. return false;
  96. }
  97. static void close_popups(void)
  98. {
  99. int i;
  100. for (i = 0; i < num_menus; ++i) {
  101. /* Destroying the lowest level window recursively destroys the child windows */
  102. if (window_is_root(menus[i].parent)) {
  103. SDL_DestroyWindow(menus[i].win);
  104. }
  105. }
  106. SDL_free(menus);
  107. menus = NULL;
  108. num_menus = 0;
  109. /* If the tooltip was a child of a popup, it was recursively destroyed with the popup */
  110. if (!window_is_root(tooltip.parent)) {
  111. SDL_zero(tooltip);
  112. }
  113. }
  114. static void loop(void)
  115. {
  116. int i;
  117. char fmt_str[128];
  118. SDL_Event event;
  119. /* Check for events */
  120. while (SDL_PollEvent(&event)) {
  121. if (event.type == SDL_EVENT_MOUSE_MOTION) {
  122. /* Hide the tooltip and restart the timer if the mouse is moved */
  123. if (tooltip.win) {
  124. SDL_DestroyWindow(tooltip.win);
  125. SDL_zero(tooltip);
  126. }
  127. tooltip_timer = SDL_GetTicks() + TOOLTIP_DELAY;
  128. if (num_menus > 0 && event.motion.windowID == SDL_GetWindowID(menus[0].parent)) {
  129. int x = (int)event.motion.x;
  130. int y = (int)event.motion.y;
  131. SDL_SetWindowPosition(menus[0].win, x, y);
  132. }
  133. } else if (event.type == SDL_EVENT_MOUSE_BUTTON_DOWN) {
  134. /* Left click closes the popup menus */
  135. if (event.button.button == SDL_BUTTON_LEFT) {
  136. close_popups();
  137. } else if (event.button.button == SDL_BUTTON_RIGHT) {
  138. /* Create a new popup menu */
  139. menus = SDL_realloc(menus, sizeof(struct PopupWindow) * (num_menus + 1));
  140. if (create_popup(&menus[num_menus], true)) {
  141. ++num_menus;
  142. }
  143. }
  144. } else if (event.type == SDL_EVENT_KEY_DOWN) {
  145. if (event.key.key == SDLK_SPACE) {
  146. for (i = 0; i < num_menus; ++i) {
  147. if (SDL_GetWindowFlags(menus[i].win) & SDL_WINDOW_HIDDEN) {
  148. SDL_ShowWindow(menus[i].win);
  149. } else {
  150. SDL_HideWindow(menus[i].win);
  151. }
  152. }
  153. /* Don't process this event in SDLTest_CommonEvent() */
  154. continue;
  155. }
  156. }
  157. SDLTest_CommonEvent(state, &event, &done);
  158. }
  159. if (done) {
  160. return;
  161. }
  162. /* Show the tooltip if the delay period has elapsed */
  163. if (SDL_GetTicks() > tooltip_timer) {
  164. if (!tooltip.win) {
  165. create_popup(&tooltip, false);
  166. }
  167. }
  168. /* Draw the window */
  169. for (i = 0; i < state->num_windows; ++i) {
  170. SDL_Renderer *renderer = state->renderers[i];
  171. SDL_RenderClear(renderer);
  172. SDL_RenderPresent(renderer);
  173. }
  174. /* Draw the menus in alternating colors */
  175. for (i = 0; i < num_menus; ++i) {
  176. const SDL_Color *c = &colors[i % SDL_arraysize(colors)];
  177. SDL_Renderer *renderer = menus[i].renderer;
  178. SDL_SetRenderDrawColor(renderer, c->r, c->g, c->b, c->a);
  179. SDL_RenderClear(renderer);
  180. SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
  181. SDL_snprintf(fmt_str, sizeof(fmt_str), "Popup Menu %i", i);
  182. SDLTest_DrawString(renderer, 10.0f, 10.0f, fmt_str);
  183. SDL_RenderPresent(renderer);
  184. }
  185. /* Draw the tooltip */
  186. if (tooltip.win) {
  187. int menu_idx;
  188. SDL_SetRenderDrawColor(tooltip.renderer, 0x00, 0x00, 0x00, 0xFF);
  189. SDL_RenderClear(tooltip.renderer);
  190. SDL_SetRenderDrawColor(tooltip.renderer, 0xFF, 0xFF, 0xFF, 0xFF);
  191. menu_idx = get_menu_index_by_window(tooltip.parent);
  192. if (menu_idx >= 0) {
  193. SDL_snprintf(fmt_str, sizeof(fmt_str), "Tooltip for popup %i", menu_idx);
  194. } else {
  195. SDL_snprintf(fmt_str, sizeof(fmt_str), "Toplevel tooltip");
  196. }
  197. SDLTest_DrawString(tooltip.renderer, 10.0f, TOOLTIP_HEIGHT / 2, fmt_str);
  198. SDL_RenderPresent(tooltip.renderer);
  199. }
  200. }
  201. int main(int argc, char *argv[])
  202. {
  203. int i;
  204. /* Initialize test framework */
  205. state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
  206. if (!state) {
  207. return 1;
  208. }
  209. /* Parse commandline */
  210. if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
  211. return 1;
  212. }
  213. if (!SDLTest_CommonInit(state)) {
  214. SDLTest_CommonQuit(state);
  215. quit(2);
  216. }
  217. for (i = 0; i < state->num_windows; ++i) {
  218. SDL_Renderer *renderer = state->renderers[i];
  219. SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
  220. SDL_RenderClear(renderer);
  221. }
  222. /* Main render loop */
  223. done = 0;
  224. #ifdef SDL_PLATFORM_EMSCRIPTEN
  225. emscripten_set_main_loop(loop, 0, 1);
  226. #else
  227. while (!done) {
  228. loop();
  229. }
  230. #endif
  231. quit(0);
  232. /* keep the compiler happy ... */
  233. return 0;
  234. }