view.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #pragma once
  2. #include <input/input.h>
  3. #include "canvas.h"
  4. #include <stddef.h>
  5. #include <stdint.h>
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. /* Hides drawing view_port */
  10. #define VIEW_NONE 0xFFFFFFFF
  11. /* Ignore navigation event */
  12. #define VIEW_IGNORE 0xFFFFFFFE
  13. /* Deatch from gui, deallocate Views and ViewDispatcher
  14. * BE SUPER CAREFUL, deallocation happens automatically on GUI thread
  15. * You ARE NOT owning ViewDispatcher and Views instances
  16. */
  17. #define VIEW_DESTROY 0xFFFFFFFA
  18. /* View, anonymous type */
  19. typedef struct View View;
  20. /* View Draw callback
  21. * @param canvas, pointer to canvas
  22. * @param view_model, pointer to context
  23. * @warning called from GUI thread
  24. */
  25. typedef void (*ViewDrawCallback)(Canvas* canvas, void* model);
  26. /* View Input callback
  27. * @param event, pointer to input event data
  28. * @param context, pointer to context
  29. * @return true if event handled, false if event ignored
  30. * @warning called from GUI thread
  31. */
  32. typedef bool (*ViewInputCallback)(InputEvent* event, void* context);
  33. /* View navigation callback
  34. * @param context, pointer to context
  35. * @return next view id
  36. * @warning called from GUI thread
  37. */
  38. typedef uint32_t (*ViewNavigationCallback)(void* context);
  39. /* View callback
  40. * @param context, pointer to context
  41. * @warning called from GUI thread
  42. */
  43. typedef void (*ViewCallback)(void* context);
  44. /* View Update Callback
  45. * Called upon model change, need to be propagated to GUI throw ViewPort update
  46. * @param view, pointer to view
  47. * @param context, pointer to context
  48. * @warning called from GUI thread
  49. */
  50. typedef void (*ViewUpdateCallback)(View* view, void* context);
  51. /* View model types */
  52. typedef enum {
  53. /* Model is not allocated */
  54. ViewModelTypeNone,
  55. /* Model consist of atomic types and/or partial update is not critical for rendering.
  56. * Lock free.
  57. */
  58. ViewModelTypeLockFree,
  59. /* Model access is guarded with mutex.
  60. * Locking gui thread.
  61. */
  62. ViewModelTypeLocking,
  63. } ViewModelType;
  64. /* Allocate and init View
  65. * @return pointer to View
  66. */
  67. View* view_alloc();
  68. /* Free View
  69. * @param pointer to View
  70. */
  71. void view_free(View* view);
  72. /* Set View Draw callback
  73. * @param view, pointer to View
  74. * @param callback, draw callback
  75. */
  76. void view_set_draw_callback(View* view, ViewDrawCallback callback);
  77. /* Set View Draw callback
  78. * @param view, pointer to View
  79. * @param callback, input callback
  80. */
  81. void view_set_input_callback(View* view, ViewInputCallback callback);
  82. /* Set Navigation Previous callback
  83. * @param view, pointer to View
  84. * @param callback, input callback
  85. */
  86. void view_set_previous_callback(View* view, ViewNavigationCallback callback);
  87. /* Set Navigation Next callback
  88. * @param view, pointer to View
  89. * @param callback, input callback
  90. */
  91. void view_set_next_callback(View* view, ViewNavigationCallback callback);
  92. /* Set Enter callback
  93. * @param view, pointer to View
  94. * @param callback, callback
  95. */
  96. void view_set_enter_callback(View* view, ViewCallback callback);
  97. /* Set Exit callback
  98. * @param view, pointer to View
  99. * @param callback, callback
  100. */
  101. void view_set_exit_callback(View* view, ViewCallback callback);
  102. /* Set Update callback
  103. * @param view, pointer to View
  104. * @param callback, callback
  105. */
  106. void view_set_update_callback(View* view, ViewUpdateCallback callback);
  107. /* Set View Draw callback
  108. * @param view, pointer to View
  109. * @param context, context for callbacks
  110. */
  111. void view_set_update_callback_context(View* view, void* context);
  112. /* Set View Draw callback
  113. * @param view, pointer to View
  114. * @param context, context for callbacks
  115. */
  116. void view_set_context(View* view, void* context);
  117. /* Allocate view model.
  118. * @param view, pointer to View
  119. * @param type, View Model Type
  120. * @param size, size
  121. */
  122. void view_allocate_model(View* view, ViewModelType type, size_t size);
  123. /* Free view model data memory.
  124. * @param view, pointer to View
  125. */
  126. void view_free_model(View* view);
  127. /* Get view model data
  128. * @param view, pointer to View
  129. * @return pointer to model data
  130. * @warning Don't forget to commit model changes
  131. */
  132. void* view_get_model(View* view);
  133. /* Commit view model
  134. * @param view, pointer to View
  135. * @param update, true if you want to emit view update, false otherwise
  136. */
  137. void view_commit_model(View* view, bool update);
  138. #ifdef __cplusplus
  139. }
  140. #endif
  141. #ifdef __cplusplus
  142. #define with_view_model_cpp(view, type, var, function_body) \
  143. { \
  144. type* p = static_cast<type*>(view_get_model(view)); \
  145. bool update = [&](type * var) function_body(p); \
  146. view_commit_model(view, update); \
  147. }
  148. #else
  149. /*
  150. * With clause for view model
  151. * @param view, View instance pointer
  152. * @param function_body a (){} lambda declaration, executed within you parent function context
  153. * @return true if you want to emit view update, false otherwise
  154. */
  155. #define with_view_model(view, function_body) \
  156. { \
  157. void* p = view_get_model(view); \
  158. bool update = ({ bool __fn__ function_body __fn__; })(p); \
  159. view_commit_model(view, update); \
  160. }
  161. #endif