view.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 Draw callback
  19. * @param canvas, pointer to canvas
  20. * @param view_model, pointer to context
  21. * @warning called from GUI thread
  22. */
  23. typedef void (*ViewDrawCallback)(Canvas* canvas, void* model);
  24. /* View Input callback
  25. * @param event, pointer to input event data
  26. * @param context, pointer to context
  27. * @return true if event handled, false if event ignored
  28. * @warning called from GUI thread
  29. */
  30. typedef bool (*ViewInputCallback)(InputEvent* event, void* context);
  31. /* View navigation callback
  32. * @param context, pointer to context
  33. * @return next view id
  34. * @warning called from GUI thread
  35. */
  36. typedef uint32_t (*ViewNavigationCallback)(void* context);
  37. /* View model types */
  38. typedef enum {
  39. /* Model is not allocated */
  40. ViewModelTypeNone,
  41. /* Model consist of atomic types and/or partial update is not critical for rendering.
  42. * Lock free.
  43. */
  44. ViewModelTypeLockFree,
  45. /* Model access is guarded with mutex.
  46. * Locking gui thread.
  47. */
  48. ViewModelTypeLocking,
  49. } ViewModelType;
  50. typedef struct View View;
  51. /* Allocate and init View
  52. * @return pointer to View
  53. */
  54. View* view_alloc();
  55. /* Free View
  56. * @param pointer to View
  57. */
  58. void view_free(View* view);
  59. /* Set View Draw callback
  60. * @param view, pointer to View
  61. * @param callback, draw callback
  62. */
  63. void view_set_draw_callback(View* view, ViewDrawCallback callback);
  64. /* Set View Draw callback
  65. * @param view, pointer to View
  66. * @param callback, input callback
  67. */
  68. void view_set_input_callback(View* view, ViewInputCallback callback);
  69. /* Set Navigation Previous callback
  70. * @param view, pointer to View
  71. * @param callback, input callback
  72. */
  73. void view_set_previous_callback(View* view, ViewNavigationCallback callback);
  74. /* Set Navigation Next callback
  75. * @param view, pointer to View
  76. * @param callback, input callback
  77. */
  78. void view_set_next_callback(View* view, ViewNavigationCallback callback);
  79. /* Set View Draw callback
  80. * @param view, pointer to View
  81. * @param context, context for callbacks
  82. */
  83. void view_set_context(View* view, void* context);
  84. /* Allocate view model.
  85. * @param view, pointer to View
  86. * @param type, View Model Type
  87. * @param size, size
  88. */
  89. void view_allocate_model(View* view, ViewModelType type, size_t size);
  90. /* Free view model data memory.
  91. * @param view, pointer to View
  92. */
  93. void view_free_model(View* view);
  94. /* Get view model data
  95. * @param view, pointer to View
  96. * @return pointer to model data
  97. * @warning Don't forget to commit model changes
  98. */
  99. void* view_get_model(View* view);
  100. /* Commit view model
  101. * @param view, pointer to View
  102. */
  103. void view_commit_model(View* view);
  104. /*
  105. * With clause for view model
  106. * @param view, View instance pointer
  107. * @param function_body a (){} lambda declaration,
  108. * executed within you parent function context.
  109. */
  110. #define with_view_model(view, function_body) \
  111. { \
  112. void* p = view_get_model(view); \
  113. ({ void __fn__ function_body __fn__; })(p); \
  114. view_commit_model(view); \
  115. }
  116. #ifdef __cplusplus
  117. }
  118. #endif