draw.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * src/draw.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 line draw demo program
  25. */
  26. #define UI_IMPLEMENTATION
  27. #include <ui.h>
  28. int16_t lines[1024] = { 0 };
  29. int main(int argc, char **argv)
  30. {
  31. int x, y, lx = -1, ly = -1, btn = 0;
  32. char *lang[] = { "Line demo" };
  33. ui_event_t *evt;
  34. ui_t ctx;
  35. ui_form_t form[2] = {
  36. { .type = UI_LINES, .ptr = lines },
  37. { .type = UI_END }
  38. };
  39. (void)argc; (void)argv;
  40. ui_init(&ctx, 1, lang, 640, 480, NULL);
  41. /* wait until user closes the window */
  42. while((evt = ui_event(&ctx, form))) {
  43. /* exit on Esc */
  44. if(evt->type == UI_EVT_KEY && evt->key[0] == 0x1b) break;
  45. /* save left button pressed state */
  46. if(evt->type == UI_EVT_MOUSE) btn = evt->btn & UI_BTN_L;
  47. /* save new coordinate */
  48. ui_getmouse(&ctx, &x, &y);
  49. if(btn && x != lx && y != ly) {
  50. memmove(&lines[2], &lines[0], sizeof(lines) - 4 * sizeof(lines[0]));
  51. lines[0] = lx = x;
  52. lines[1] = ly = y;
  53. /* set color to foreground color */
  54. form[0].value = ctx.theme[UI_FG];
  55. /* let the UI know that it needs to redraw */
  56. ui_refresh(&ctx);
  57. }
  58. }
  59. ui_free(&ctx);
  60. return 0;
  61. }