lv_example_event_4.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include "../lv_examples.h"
  2. #if LV_BUILD_EXAMPLES
  3. static int n = 3;
  4. static lv_obj_t * label = NULL;
  5. static void timer_cb(lv_timer_t * timer)
  6. {
  7. if(n < 3 || n > 32) {
  8. n = 3;
  9. }
  10. else {
  11. static uint32_t old_tick = 0;
  12. uint32_t tick = lv_tick_get();
  13. if(!old_tick) {
  14. old_tick = tick;
  15. }
  16. if(tick - old_tick > 3000) {
  17. n++;
  18. lv_label_set_text_fmt(label, "%d sides", n);
  19. old_tick = tick;
  20. }
  21. }
  22. lv_obj_invalidate(timer->user_data);
  23. }
  24. static void event_cb(lv_event_t * e)
  25. {
  26. /*The original target of the event. Can be the buttons or the container*/
  27. lv_draw_ctx_t * draw_ctx = lv_event_get_draw_ctx(e);
  28. lv_draw_rect_dsc_t draw_dsc;
  29. lv_draw_rect_dsc_init(&draw_dsc);
  30. draw_dsc.bg_color = lv_palette_main(LV_PALETTE_LIGHT_GREEN);
  31. draw_dsc.bg_opa = LV_OPA_COVER;
  32. lv_point_t points[32];
  33. int i, r = 150;
  34. uint32_t tick = lv_tick_get();
  35. for(i = 0; i < n; i++) {
  36. int angle = i * 360 / n + ((tick % 36000) / 100);
  37. lv_coord_t x = 150 + (r * lv_trigo_cos(angle) >> LV_TRIGO_SHIFT), y =
  38. 150 + (r * lv_trigo_sin(angle) >> LV_TRIGO_SHIFT);
  39. points[i].x = x;
  40. points[i].y = y;
  41. }
  42. lv_draw_polygon(draw_ctx, &draw_dsc, points, n);
  43. }
  44. /**
  45. * Demonstrate event bubbling
  46. */
  47. void lv_example_event_4(void)
  48. {
  49. lv_obj_t * cont = lv_obj_create(lv_scr_act());
  50. lv_obj_remove_style_all(cont);
  51. lv_obj_set_size(cont, 300, 300);
  52. label = lv_label_create(cont);
  53. lv_label_set_text_fmt(label, "%d sides", n);
  54. lv_obj_center(label);
  55. lv_obj_add_event_cb(cont, event_cb, LV_EVENT_DRAW_MAIN, NULL);
  56. lv_timer_create(timer_cb, 17, cont);
  57. }
  58. #endif