gui.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <stdlib.h>
  17. #include <unistd.h>
  18. #include "wasm_app.h"
  19. static void btn_event_cb(wgl_obj_t btn, wgl_event_t event);
  20. uint32_t count = 0;
  21. char count_str[11] = { 0 };
  22. wgl_obj_t hello_world_label;
  23. wgl_obj_t count_label;
  24. wgl_obj_t btn1;
  25. wgl_obj_t label_count1;
  26. int label_count1_value = 0;
  27. char label_count1_str[11] = { 0 };
  28. void timer1_update(user_timer_t timer1)
  29. {
  30. if ((count % 100) == 0) {
  31. sprintf(count_str, "%d", count / 100);
  32. wgl_label_set_text(count_label, count_str);
  33. }
  34. ++count;
  35. }
  36. void on_init()
  37. {
  38. char text[32] = {0};
  39. hello_world_label = wgl_label_create((wgl_obj_t)NULL, (wgl_obj_t)NULL);
  40. wgl_label_set_text(hello_world_label, "Hello world!");
  41. wgl_label_get_text(hello_world_label, text, sizeof(text));
  42. printf("Label text %d %s \n", wgl_label_get_text_length(hello_world_label), text);
  43. wgl_obj_align(hello_world_label, (wgl_obj_t)NULL, WGL_ALIGN_IN_TOP_LEFT, 0, 0);
  44. count_label = wgl_label_create((wgl_obj_t)NULL, (wgl_obj_t)NULL);
  45. wgl_obj_align(count_label, (wgl_obj_t)NULL, WGL_ALIGN_IN_TOP_MID, 0, 0);
  46. btn1 = wgl_btn_create((wgl_obj_t)NULL, (wgl_obj_t)NULL); /*Create a button on the currently loaded screen*/
  47. wgl_obj_set_event_cb(btn1, btn_event_cb); /*Set function to be called when the button is released*/
  48. wgl_obj_align(btn1, (wgl_obj_t)NULL, WGL_ALIGN_CENTER, 0, 0); /*Align below the label*/
  49. /*Create a label on the button*/
  50. wgl_obj_t btn_label = wgl_label_create(btn1, (wgl_obj_t)NULL);
  51. wgl_label_set_text(btn_label, "Click ++");
  52. label_count1 = wgl_label_create((wgl_obj_t)NULL, (wgl_obj_t)NULL);
  53. wgl_label_set_text(label_count1, "0");
  54. wgl_obj_align(label_count1, (wgl_obj_t)NULL, WGL_ALIGN_IN_BOTTOM_MID, 0, 0);
  55. /* set up a timer */
  56. user_timer_t timer;
  57. timer = api_timer_create(10, true, false, timer1_update);
  58. api_timer_restart(timer, 10);
  59. }
  60. static void btn_event_cb(wgl_obj_t btn, wgl_event_t event)
  61. {
  62. if(event == WGL_EVENT_RELEASED) {
  63. label_count1_value++;
  64. sprintf(label_count1_str, "%d", label_count1_value);
  65. wgl_label_set_text(label_count1, label_count1_str);
  66. }
  67. }