screen.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * screen.cpp
  3. *
  4. * Created on: Jan 12, 2021
  5. * Author: David Original work by Jose (PTDreamer), 2017
  6. */
  7. #include "screen.h"
  8. #include "oled.h"
  9. void screen_addWidget(widget_t *widget, screen_t *scr) {
  10. widget_t *last_widget = NULL;
  11. if(scr->widgets) {
  12. last_widget = scr->widgets;
  13. while(last_widget->next_widget)
  14. last_widget = last_widget->next_widget;
  15. }
  16. widget->next_widget = NULL;
  17. widget->parent = scr;
  18. if(last_widget)
  19. last_widget->next_widget = widget;
  20. else
  21. scr->widgets = widget;
  22. }
  23. widget_t * screen_tabToWidget(screen_t * scr, uint8_t tab) {
  24. widget_t *last_widget = NULL;
  25. if(scr->widgets) {
  26. last_widget = scr->widgets;
  27. while(last_widget) {
  28. if(last_widget->type == widget_editable) {
  29. if(((editable_widget_t*)last_widget->content)->selectable.tab == tab)
  30. return last_widget;
  31. }
  32. last_widget = last_widget->next_widget;
  33. }
  34. }
  35. return last_widget;
  36. }
  37. int default_screenProcessInput(screen_t * scr, RE_Rotation_t input, RE_State_t *state) {
  38. int ret = -1;
  39. selectable_widget_t *sel = extractSelectablePartFromWidget(scr->current_widget);
  40. if(sel) {
  41. if(sel->processInput) {
  42. ret = sel->processInput(scr->current_widget, input, state);
  43. }
  44. }
  45. return ret;
  46. }
  47. void default_screenDraw(screen_t *scr) {
  48. widget_t *last_widget = NULL;
  49. if(scr->widgets) {
  50. if(scr->refresh==screenRefresh_eraseNow){
  51. FillBuffer(BLACK,fill_dma);
  52. }
  53. last_widget = scr->widgets;
  54. while(last_widget) {
  55. if(last_widget->draw){
  56. last_widget->draw(last_widget);
  57. }
  58. last_widget = last_widget->next_widget;
  59. }
  60. scr->refresh=screenRefresh_idle;
  61. }
  62. }
  63. void default_screenUpdate(screen_t *scr) {
  64. if(scr->widgets) {
  65. widget_t *last_widget = scr->widgets;
  66. while(last_widget) {
  67. if(last_widget->update){
  68. last_widget->update(last_widget);
  69. }
  70. last_widget = last_widget->next_widget;
  71. }
  72. }
  73. }
  74. void default_init(screen_t *scr) {
  75. if(scr->current_widget)
  76. return;
  77. if(!scr->widgets)
  78. return;
  79. int c = 1000;
  80. widget_t *w = scr->widgets;
  81. selectable_widget_t *sel;
  82. while(w) {
  83. sel = extractSelectablePartFromWidget(w);
  84. if(sel) {
  85. if(sel->tab < c) {
  86. c = sel->tab;
  87. }
  88. }
  89. w = w->next_widget;
  90. }
  91. w = scr->widgets;
  92. scr->current_widget = NULL;
  93. while(w) {
  94. sel = extractSelectablePartFromWidget(w);
  95. if(sel) {
  96. if(sel->tab == c) {
  97. scr->current_widget = w;
  98. if(sel->state == widget_idle)
  99. sel->state = widget_selected;
  100. return;
  101. }
  102. }
  103. w = w->next_widget;
  104. }
  105. }
  106. void screen_setDefaults(screen_t *scr) {
  107. scr->processInput = &default_screenProcessInput;
  108. scr->init = &default_init;
  109. scr->draw = &default_screenDraw;
  110. scr->update = &default_screenUpdate;
  111. scr->onEnter = NULL;
  112. scr->onExit = NULL;
  113. }