view.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. typedef enum {
  19. ViewOrientationHorizontal,
  20. ViewOrientationVertical,
  21. } ViewOrientation;
  22. /* View, anonymous type */
  23. typedef struct View View;
  24. /* View Draw callback
  25. * @param canvas, pointer to canvas
  26. * @param view_model, pointer to context
  27. * @warning called from GUI thread
  28. */
  29. typedef void (*ViewDrawCallback)(Canvas* canvas, void* model);
  30. /* View Input callback
  31. * @param event, pointer to input event data
  32. * @param context, pointer to context
  33. * @return true if event handled, false if event ignored
  34. * @warning called from GUI thread
  35. */
  36. typedef bool (*ViewInputCallback)(InputEvent* event, void* context);
  37. /* View Custom callback
  38. * @param event, number of custom event
  39. * @param context, pointer to context
  40. * @return true if event handled, false if event ignored
  41. */
  42. typedef bool (*ViewCustomCallback)(uint32_t event, void* context);
  43. /* View navigation callback
  44. * @param context, pointer to context
  45. * @return next view id
  46. * @warning called from GUI thread
  47. */
  48. typedef uint32_t (*ViewNavigationCallback)(void* context);
  49. /* View callback
  50. * @param context, pointer to context
  51. * @warning called from GUI thread
  52. */
  53. typedef void (*ViewCallback)(void* context);
  54. /* View Update Callback
  55. * Called upon model change, need to be propagated to GUI throw ViewPort update
  56. * @param view, pointer to view
  57. * @param context, pointer to context
  58. * @warning called from GUI thread
  59. */
  60. typedef void (*ViewUpdateCallback)(View* view, void* context);
  61. /* View model types */
  62. typedef enum {
  63. /* Model is not allocated */
  64. ViewModelTypeNone,
  65. /* Model consist of atomic types and/or partial update is not critical for rendering.
  66. * Lock free.
  67. */
  68. ViewModelTypeLockFree,
  69. /* Model access is guarded with mutex.
  70. * Locking gui thread.
  71. */
  72. ViewModelTypeLocking,
  73. } ViewModelType;
  74. /* Allocate and init View
  75. * @return pointer to View
  76. */
  77. View* view_alloc();
  78. /* Free View
  79. * @param pointer to View
  80. */
  81. void view_free(View* view);
  82. /* Set View Draw callback
  83. * @param view, pointer to View
  84. * @param callback, draw callback
  85. */
  86. void view_set_draw_callback(View* view, ViewDrawCallback callback);
  87. /* Set View Input callback
  88. * @param view, pointer to View
  89. * @param callback, input callback
  90. */
  91. void view_set_input_callback(View* view, ViewInputCallback callback);
  92. /* Set View Custom callback
  93. * @param view, pointer to View
  94. * @param callback, input callback
  95. */
  96. void view_set_custom_callback(View* view, ViewCustomCallback callback);
  97. /* Set Navigation Previous callback
  98. * @param view, pointer to View
  99. * @param callback, input callback
  100. */
  101. void view_set_previous_callback(View* view, ViewNavigationCallback callback);
  102. /* Set Navigation Next callback
  103. * @param view, pointer to View
  104. * @param callback, input callback
  105. */
  106. void view_set_next_callback(View* view, ViewNavigationCallback callback);
  107. /* Set Enter callback
  108. * @param view, pointer to View
  109. * @param callback, callback
  110. */
  111. void view_set_enter_callback(View* view, ViewCallback callback);
  112. /* Set Exit callback
  113. * @param view, pointer to View
  114. * @param callback, callback
  115. */
  116. void view_set_exit_callback(View* view, ViewCallback callback);
  117. /* Set Update callback
  118. * @param view, pointer to View
  119. * @param callback, callback
  120. */
  121. void view_set_update_callback(View* view, ViewUpdateCallback callback);
  122. /* Set View Draw callback
  123. * @param view, pointer to View
  124. * @param context, context for callbacks
  125. */
  126. void view_set_update_callback_context(View* view, void* context);
  127. /* Set View Draw callback
  128. * @param view, pointer to View
  129. * @param context, context for callbacks
  130. */
  131. void view_set_context(View* view, void* context);
  132. /* Set View Orientation
  133. * @param view, pointer to View
  134. * @param orientation, either vertical or horizontal
  135. */
  136. void view_set_orientation(View* view, ViewOrientation orientation);
  137. /* Allocate view model.
  138. * @param view, pointer to View
  139. * @param type, View Model Type
  140. * @param size, size
  141. */
  142. void view_allocate_model(View* view, ViewModelType type, size_t size);
  143. /* Free view model data memory.
  144. * @param view, pointer to View
  145. */
  146. void view_free_model(View* view);
  147. /* Get view model data
  148. * @param view, pointer to View
  149. * @return pointer to model data
  150. * @warning Don't forget to commit model changes
  151. */
  152. void* view_get_model(View* view);
  153. /* Commit view model
  154. * @param view, pointer to View
  155. * @param update, true if you want to emit view update, false otherwise
  156. */
  157. void view_commit_model(View* view, bool update);
  158. #ifdef __cplusplus
  159. }
  160. #endif
  161. #ifdef __cplusplus
  162. #define with_view_model_cpp(view, type, var, function_body) \
  163. { \
  164. type* p = static_cast<type*>(view_get_model(view)); \
  165. bool update = [&](type * var) function_body(p); \
  166. view_commit_model(view, update); \
  167. }
  168. #else
  169. /*
  170. * With clause for view model
  171. * @param view, View instance pointer
  172. * @param function_body a (){} lambda declaration, executed within you parent function context
  173. * @return true if you want to emit view update, false otherwise
  174. */
  175. #define with_view_model(view, function_body) \
  176. { \
  177. void* p = view_get_model(view); \
  178. bool update = ({ bool __fn__ function_body __fn__; })(p); \
  179. view_commit_model(view, update); \
  180. }
  181. #endif