hex_viewer_scene_1.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #include "../hex_viewer.h"
  2. #include <furi.h>
  3. #include <furi_hal.h>
  4. #include <input/input.h>
  5. #include <gui/elements.h>
  6. #include <dolphin/dolphin.h>
  7. struct HexViewerScene1 {
  8. View* view;
  9. HexViewerScene1Callback callback;
  10. void* context;
  11. };
  12. typedef struct {
  13. int some_value;
  14. } HexViewerScene1Model;
  15. void hex_viewer_scene_1_set_callback(
  16. HexViewerScene1* instance,
  17. HexViewerScene1Callback callback,
  18. void* context) {
  19. furi_assert(instance);
  20. furi_assert(callback);
  21. instance->callback = callback;
  22. instance->context = context;
  23. }
  24. void hex_viewer_scene_1_draw(Canvas* canvas, HexViewerScene1Model* model) {
  25. UNUSED(model);
  26. canvas_clear(canvas);
  27. canvas_set_color(canvas, ColorBlack);
  28. canvas_set_font(canvas, FontPrimary);
  29. canvas_draw_str_aligned(canvas, 0, 10, AlignLeft, AlignTop, "This is Scene 1");
  30. canvas_set_font(canvas, FontSecondary);
  31. canvas_draw_str_aligned(canvas, 0, 22, AlignLeft, AlignTop, "An empty scene to be");
  32. canvas_draw_str_aligned(canvas, 0, 32, AlignLeft, AlignTop, "used as hex_viewer");
  33. }
  34. static void hex_viewer_scene_1_model_init(HexViewerScene1Model* const model) {
  35. model->some_value = 1;
  36. }
  37. bool hex_viewer_scene_1_input(InputEvent* event, void* context) {
  38. furi_assert(context);
  39. HexViewerScene1* instance = context;
  40. if (event->type == InputTypeRelease) {
  41. switch(event->key) {
  42. case InputKeyBack:
  43. with_view_model(
  44. instance->view,
  45. HexViewerScene1Model * model,
  46. {
  47. UNUSED(model);
  48. instance->callback(HexViewerCustomEventScene1Back, instance->context);
  49. },
  50. true);
  51. break;
  52. case InputKeyLeft:
  53. case InputKeyRight:
  54. case InputKeyUp:
  55. case InputKeyDown:
  56. case InputKeyOk:
  57. with_view_model(
  58. instance->view,
  59. HexViewerScene1Model* model,
  60. {
  61. UNUSED(model);
  62. },
  63. true);
  64. break;
  65. case InputKeyMAX:
  66. break;
  67. }
  68. }
  69. return true;
  70. }
  71. void hex_viewer_scene_1_exit(void* context) {
  72. furi_assert(context);
  73. }
  74. void hex_viewer_scene_1_enter(void* context) {
  75. furi_assert(context);
  76. HexViewerScene1* instance = (HexViewerScene1*)context;
  77. with_view_model(
  78. instance->view,
  79. HexViewerScene1Model * model,
  80. {
  81. hex_viewer_scene_1_model_init(model);
  82. },
  83. true
  84. );
  85. }
  86. HexViewerScene1* hex_viewer_scene_1_alloc() {
  87. HexViewerScene1* instance = malloc(sizeof(HexViewerScene1));
  88. instance->view = view_alloc();
  89. view_allocate_model(instance->view, ViewModelTypeLocking, sizeof(HexViewerScene1Model));
  90. view_set_context(instance->view, instance); // furi_assert crashes in events without this
  91. view_set_draw_callback(instance->view, (ViewDrawCallback)hex_viewer_scene_1_draw);
  92. view_set_input_callback(instance->view, hex_viewer_scene_1_input);
  93. view_set_enter_callback(instance->view, hex_viewer_scene_1_enter);
  94. view_set_exit_callback(instance->view, hex_viewer_scene_1_exit);
  95. with_view_model(
  96. instance->view,
  97. HexViewerScene1Model * model,
  98. {
  99. hex_viewer_scene_1_model_init(model);
  100. },
  101. true
  102. );
  103. return instance;
  104. }
  105. void hex_viewer_scene_1_free(HexViewerScene1* instance) {
  106. furi_assert(instance);
  107. with_view_model(
  108. instance->view,
  109. HexViewerScene1Model * model,
  110. {
  111. UNUSED(model);
  112. },
  113. true);
  114. view_free(instance->view);
  115. free(instance);
  116. }
  117. View* hex_viewer_scene_1_get_view(HexViewerScene1* instance) {
  118. furi_assert(instance);
  119. return instance->view;
  120. }