gui.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include "wasm_app.h"
  8. static void btn_event_cb(wgl_obj_t btn, wgl_event_t event);
  9. uint32_t count = 0;
  10. char count_str[11] = { 0 };
  11. wgl_obj_t hello_world_label;
  12. wgl_obj_t count_label;
  13. wgl_obj_t btn1;
  14. wgl_obj_t label_count1;
  15. int label_count1_value = 0;
  16. char label_count1_str[11] = { 0 };
  17. void timer1_update(user_timer_t timer1)
  18. {
  19. if ((count % 100) == 0) {
  20. snprintf(count_str, sizeof(count_str), "%d", count / 100);
  21. wgl_label_set_text(count_label, count_str);
  22. }
  23. ++count;
  24. }
  25. void on_init()
  26. {
  27. char text[32] = {0};
  28. hello_world_label = wgl_label_create((wgl_obj_t)NULL, (wgl_obj_t)NULL);
  29. wgl_label_set_text(hello_world_label, "Hello world!");
  30. wgl_label_get_text(hello_world_label, text, sizeof(text));
  31. printf("Label text %d %s \n", wgl_label_get_text_length(hello_world_label), text);
  32. wgl_obj_align(hello_world_label, (wgl_obj_t)NULL, WGL_ALIGN_IN_TOP_LEFT, 0, 0);
  33. count_label = wgl_label_create((wgl_obj_t)NULL, (wgl_obj_t)NULL);
  34. wgl_obj_align(count_label, (wgl_obj_t)NULL, WGL_ALIGN_IN_TOP_MID, 0, 0);
  35. btn1 = wgl_btn_create((wgl_obj_t)NULL, (wgl_obj_t)NULL); /*Create a button on the currently loaded screen*/
  36. wgl_obj_set_event_cb(btn1, btn_event_cb); /*Set function to be called when the button is released*/
  37. wgl_obj_align(btn1, (wgl_obj_t)NULL, WGL_ALIGN_CENTER, 0, 0); /*Align below the label*/
  38. /*Create a label on the button*/
  39. wgl_obj_t btn_label = wgl_label_create(btn1, (wgl_obj_t)NULL);
  40. wgl_label_set_text(btn_label, "Click ++");
  41. label_count1 = wgl_label_create((wgl_obj_t)NULL, (wgl_obj_t)NULL);
  42. wgl_label_set_text(label_count1, "0");
  43. wgl_obj_align(label_count1, (wgl_obj_t)NULL, WGL_ALIGN_IN_BOTTOM_MID, 0, 0);
  44. /* set up a timer */
  45. user_timer_t timer;
  46. timer = api_timer_create(10, true, false, timer1_update);
  47. api_timer_restart(timer, 10);
  48. }
  49. static void btn_event_cb(wgl_obj_t btn, wgl_event_t event)
  50. {
  51. if(event == WGL_EVENT_RELEASED) {
  52. label_count1_value++;
  53. snprintf(label_count1_str, sizeof(label_count1_str),
  54. "%d", label_count1_value);
  55. wgl_label_set_text(label_count1, label_count1_str);
  56. }
  57. }