view.h 5.4 KB

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