demo.c 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * src/demo.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 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. enum { WINDOW_TITLE, POPUP_TITLE, BUTTON_TITLE, EASY_TITLE, HARD_TITLE, VOLUME_TITLE };
  32. char *lang[] = { "Basic demo", "Show", "Button", "easy", "hard", "Volume:" };
  33. /* variables to store game states */
  34. int button = 0, difficulty = 0, volume = 25;
  35. /* form referencing those variables, you use a HTML flow like layout */
  36. ui_t ctx;
  37. ui_form_t popup[] = {
  38. { .type = UI_BUTTON, .flags = UI_FORCEBR, .ptr = &button, .value = 1, .label = BUTTON_TITLE },
  39. { .type = UI_RADIO, .flags = UI_NOBR, .ptr = &difficulty, .value = 0, .y = 5, .label = EASY_TITLE },
  40. { .type = UI_RADIO, .flags = UI_FORCEBR, .ptr = &difficulty, .value = 1, .x = 20, .label = HARD_TITLE },
  41. { .type = UI_LABEL, .flags = UI_NOBR, .y = 5, .label = VOLUME_TITLE },
  42. { .type = UI_SLIDER, .ptr = &volume, .max = 100 },
  43. { .type = UI_END }
  44. };
  45. ui_form_t form[] = {
  46. { .type = UI_POPUP, .align = UI_CENTER | UI_MIDDLE, .x = UI_PERCENT(50), .y = UI_PERCENT(50), .m = 10,
  47. .ptr = &popup, .label = POPUP_TITLE },
  48. { .type = UI_END }
  49. };
  50. /* initialize the UI context, pass it the string array, the window size and icon */
  51. ui_init(&ctx, sizeof(lang)/sizeof(lang[0]), lang, 640, 480, NULL);
  52. /* wait until user closes the window */
  53. while(ui_event(&ctx, form)) {
  54. /* handle button, you could do this from another thread if you want */
  55. if(button) {
  56. printf("button clicked\n");
  57. button = 0;
  58. ui_refresh(&ctx);
  59. }
  60. }
  61. /* destroy window, free resources */
  62. ui_free(&ctx);
  63. (void)argc; (void)argv;
  64. return 0;
  65. }