events.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * src/events.c
  3. * https://gitlab.com/bztsrc/smgui
  4. *
  5. * Copyright (C) 2024 bzt (bztsrc@gitlab), MIT license
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to
  9. * deal in the Software without restriction, including without limitation the
  10. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  11. * sell copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL ANY
  20. * DEVELOPER OR DISTRIBUTOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  21. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
  22. * IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * @brief State-Mode GUI events demo program
  25. */
  26. #define UI_IMPLEMENTATION
  27. #include <ui.h>
  28. int main(int argc, char **argv)
  29. {
  30. /* strings are gathered into an array so that you can change the language on-the-fly */
  31. char *lang[] = { "Events demo", "Events will be printed to stdout" };
  32. ui_event_t *evt;
  33. ui_t ctx;
  34. ui_form_t form[] = {
  35. { .type = UI_LABEL, .x = 5, .y = 5, .label = 1 },
  36. { .type = UI_END }
  37. };
  38. /* initialize the UI context, pass it the string array, the window size and icon */
  39. ui_init(&ctx, 2, lang, 640, 480, NULL);
  40. /* wait until user closes the window */
  41. while((evt = ui_event(&ctx, form))) {
  42. /* if Esc is pressed, exit (should be '\e', but not all C compiler likes that) */
  43. if(evt->type == UI_EVT_KEY && evt->key[0] == 0x1b) break;
  44. /* print event to stdout */
  45. switch(evt->type) {
  46. case UI_EVT_MOUSE:
  47. printf("mouse btn=%06x x=%d y=%d\n", evt->btn, evt->x, evt->y);
  48. break;
  49. case UI_EVT_GAMEPAD:
  50. printf("gamepad btn=%06x x=%d y=%d rx=%d ry=%d key=%d\n", evt->btn, evt->x, evt->y, evt->rx, evt->ry, evt->key[0]);
  51. break;
  52. case UI_EVT_KEY:
  53. printf("key btn=%06x key='%s'\n", evt->btn, evt->key);
  54. break;
  55. case UI_EVT_DROP:
  56. printf("drop btn=%06x x=%d y=%d fn='%s'\n", evt->btn, evt->x, evt->y, evt->fn);
  57. break;
  58. case UI_EVT_RESIZE:
  59. printf("resize x=%d y=%d\n", evt->x, evt->y);
  60. break;
  61. }
  62. }
  63. /* destroy window, free resources */
  64. ui_free(&ctx);
  65. (void)argc; (void)argv;
  66. return 0;
  67. }