view.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /**
  2. * @file view.h
  3. * GUI: View API
  4. */
  5. #pragma once
  6. #include <input/input.h>
  7. #include "icon_animation.h"
  8. #include "canvas.h"
  9. #include <stddef.h>
  10. #include <stdint.h>
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. /** Hides drawing view_port */
  15. #define VIEW_NONE 0xFFFFFFFF
  16. /** Ignore navigation event */
  17. #define VIEW_IGNORE 0xFFFFFFFE
  18. typedef enum {
  19. ViewOrientationHorizontal,
  20. ViewOrientationHorizontalFlip,
  21. ViewOrientationVertical,
  22. ViewOrientationVerticalFlip,
  23. } ViewOrientation;
  24. /** View, anonymous type */
  25. typedef struct View View;
  26. /** View Draw callback
  27. * @param canvas, pointer to canvas
  28. * @param view_model, pointer to context
  29. * @warning called from GUI thread
  30. */
  31. typedef void (*ViewDrawCallback)(Canvas* canvas, void* model);
  32. /** View Input callback
  33. * @param event, pointer to input event data
  34. * @param context, pointer to context
  35. * @return true if event handled, false if event ignored
  36. * @warning called from GUI thread
  37. */
  38. typedef bool (*ViewInputCallback)(InputEvent* event, void* context);
  39. /** View Custom callback
  40. * @param event, number of custom event
  41. * @param context, pointer to context
  42. * @return true if event handled, false if event ignored
  43. */
  44. typedef bool (*ViewCustomCallback)(uint32_t event, void* context);
  45. /** View navigation callback
  46. * @param context, pointer to context
  47. * @return next view id
  48. * @warning called from GUI thread
  49. */
  50. typedef uint32_t (*ViewNavigationCallback)(void* context);
  51. /** View callback
  52. * @param context, pointer to context
  53. * @warning called from GUI thread
  54. */
  55. typedef void (*ViewCallback)(void* context);
  56. /** View Update Callback Called upon model change, need to be propagated to GUI
  57. * throw ViewPort update
  58. * @param view, pointer to view
  59. * @param context, pointer to context
  60. * @warning called from GUI thread
  61. */
  62. typedef void (*ViewUpdateCallback)(View* view, void* context);
  63. /** View model types */
  64. typedef enum {
  65. /** Model is not allocated */
  66. ViewModelTypeNone,
  67. /** Model consist of atomic types and/or partial update is not critical for rendering.
  68. * Lock free.
  69. */
  70. ViewModelTypeLockFree,
  71. /** Model access is guarded with mutex.
  72. * Locking gui thread.
  73. */
  74. ViewModelTypeLocking,
  75. } ViewModelType;
  76. /** Allocate and init View
  77. * @return View instance
  78. */
  79. View* view_alloc();
  80. /** Free View
  81. *
  82. * @param view instance
  83. */
  84. void view_free(View* view);
  85. /** Tie IconAnimation with View
  86. *
  87. * @param view View instance
  88. * @param icon_animation IconAnimation instance
  89. */
  90. void view_tie_icon_animation(View* view, IconAnimation* icon_animation);
  91. /** Set View Draw callback
  92. *
  93. * @param view View instance
  94. * @param callback draw callback
  95. */
  96. void view_set_draw_callback(View* view, ViewDrawCallback callback);
  97. /** Set View Input callback
  98. *
  99. * @param view View instance
  100. * @param callback input callback
  101. */
  102. void view_set_input_callback(View* view, ViewInputCallback callback);
  103. /** Set View Custom callback
  104. *
  105. * @param view View instance
  106. * @param callback input callback
  107. */
  108. void view_set_custom_callback(View* view, ViewCustomCallback callback);
  109. /** Set Navigation Previous callback
  110. *
  111. * @param view View instance
  112. * @param callback input callback
  113. */
  114. void view_set_previous_callback(View* view, ViewNavigationCallback callback);
  115. /** Set Enter callback
  116. *
  117. * @param view View instance
  118. * @param callback callback
  119. */
  120. void view_set_enter_callback(View* view, ViewCallback callback);
  121. /** Set Exit callback
  122. *
  123. * @param view View instance
  124. * @param callback callback
  125. */
  126. void view_set_exit_callback(View* view, ViewCallback callback);
  127. /** Set Update callback
  128. *
  129. * @param view View instance
  130. * @param callback callback
  131. */
  132. void view_set_update_callback(View* view, ViewUpdateCallback callback);
  133. /** Set View Draw callback
  134. *
  135. * @param view View instance
  136. * @param context context for callbacks
  137. */
  138. void view_set_update_callback_context(View* view, void* context);
  139. /** Set View Draw callback
  140. *
  141. * @param view View instance
  142. * @param context context for callbacks
  143. */
  144. void view_set_context(View* view, void* context);
  145. /** Set View Orientation
  146. *
  147. * @param view View instance
  148. * @param orientation either vertical or horizontal
  149. */
  150. void view_set_orientation(View* view, ViewOrientation orientation);
  151. /** Allocate view model.
  152. *
  153. * @param view View instance
  154. * @param type View Model Type
  155. * @param size size
  156. */
  157. void view_allocate_model(View* view, ViewModelType type, size_t size);
  158. /** Free view model data memory.
  159. *
  160. * @param view View instance
  161. */
  162. void view_free_model(View* view);
  163. /** Get view model data
  164. *
  165. * @param view View instance
  166. *
  167. * @return pointer to model data
  168. * @warning Don't forget to commit model changes
  169. */
  170. void* view_get_model(View* view);
  171. /** Commit view model
  172. *
  173. * @param view View instance
  174. * @param update true if you want to emit view update, false otherwise
  175. */
  176. void view_commit_model(View* view, bool update);
  177. #ifdef __cplusplus
  178. }
  179. #endif
  180. #ifdef __cplusplus
  181. #define with_view_model_cpp(view, type, var, code, update) \
  182. { \
  183. type var = static_cast<type>(view_get_model(view)); \
  184. {code}; \
  185. view_commit_model(view, update); \
  186. }
  187. #else
  188. /** With clause for view model
  189. *
  190. * @param view View instance pointer
  191. * @param type View model type
  192. * @param code Code block that will be executed between model lock and unlock
  193. * @param update Bool flag, if true, view will be updated after code block. Can be variable, so code block can decide if update is needed.
  194. *
  195. */
  196. #define with_view_model(view, type, code, update) \
  197. { \
  198. type = view_get_model(view); \
  199. {code}; \
  200. view_commit_model(view, update); \
  201. }
  202. #endif