view.h 5.4 KB

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