flipchess_startscreen.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #include "../flipchess.h"
  2. #include <furi.h>
  3. #include <furi_hal.h>
  4. #include <input/input.h>
  5. #include <gui/elements.h>
  6. #include "flipchess_icons.h"
  7. #include "helpers/flipchess_fonts.h"
  8. struct FlipChessStartscreen {
  9. View* view;
  10. FlipChessStartscreenCallback callback;
  11. void* context;
  12. };
  13. typedef struct {
  14. int some_value;
  15. } FlipChessStartscreenModel;
  16. void flipchess_startscreen_set_callback(
  17. FlipChessStartscreen* instance,
  18. FlipChessStartscreenCallback callback,
  19. void* context) {
  20. furi_assert(instance);
  21. furi_assert(callback);
  22. instance->callback = callback;
  23. instance->context = context;
  24. }
  25. void flipchess_startscreen_draw(Canvas* canvas, FlipChessStartscreenModel* model) {
  26. UNUSED(model);
  27. canvas_clear(canvas);
  28. canvas_set_color(canvas, ColorBlack);
  29. canvas_draw_icon(canvas, 0, 0, &I_FLIPR_128x64);
  30. const uint8_t text_x_pos = 2;
  31. const uint8_t text_y_pos = 12;
  32. canvas_set_custom_u8g2_font(canvas, _u8g2_font_lucasarts_scumm_subtitle_o_tr);
  33. canvas_draw_str(canvas, text_x_pos, text_y_pos, "Chess");
  34. canvas_set_font(canvas, FontSecondary);
  35. canvas_draw_str(canvas, 62, text_y_pos, FLIPCHESS_VERSION);
  36. //canvas_set_font(canvas, FontSecondary);
  37. //canvas_draw_str(canvas, 10, 11, "How about a nice game of...");
  38. //canvas_draw_str(canvas, 99, 40, FLIPCHESS_VERSION);
  39. //canvas_set_font(canvas, FontPrimary);
  40. //canvas_draw_str(canvas, 10, 23, "Chess");
  41. //canvas_draw_icon(canvas, 0, 40, &I_Background_128x11);
  42. //canvas_draw_str(canvas, 10, 61, "FLIPR");
  43. elements_button_left(canvas, "Sound");
  44. elements_button_right(canvas, "Silent");
  45. }
  46. static void flipchess_startscreen_model_init(FlipChessStartscreenModel* const model) {
  47. model->some_value = 1;
  48. }
  49. bool flipchess_startscreen_input(InputEvent* event, void* context) {
  50. furi_assert(context);
  51. FlipChessStartscreen* instance = context;
  52. FlipChess* app = instance->context;
  53. if(event->type == InputTypeRelease) {
  54. switch(event->key) {
  55. case InputKeyBack:
  56. with_view_model(
  57. instance->view,
  58. FlipChessStartscreenModel * model,
  59. {
  60. UNUSED(model);
  61. instance->callback(FlipChessCustomEventStartscreenBack, instance->context);
  62. },
  63. true);
  64. break;
  65. case InputKeyLeft:
  66. // sound on, haptic off
  67. app->sound = 1;
  68. app->haptic = FlipChessHapticOff;
  69. with_view_model(
  70. instance->view,
  71. FlipChessStartscreenModel * model,
  72. {
  73. UNUSED(model);
  74. instance->callback(FlipChessCustomEventStartscreenOk, instance->context);
  75. },
  76. true);
  77. break;
  78. case InputKeyRight:
  79. // sound off, haptic on
  80. app->sound = 0;
  81. app->haptic = FlipChessHapticOn;
  82. with_view_model(
  83. instance->view,
  84. FlipChessStartscreenModel * model,
  85. {
  86. UNUSED(model);
  87. instance->callback(FlipChessCustomEventStartscreenOk, instance->context);
  88. },
  89. true);
  90. break;
  91. case InputKeyUp:
  92. case InputKeyDown:
  93. case InputKeyOk:
  94. case InputKeyMAX:
  95. break;
  96. }
  97. }
  98. return true;
  99. }
  100. void flipchess_startscreen_exit(void* context) {
  101. furi_assert(context);
  102. }
  103. void flipchess_startscreen_enter(void* context) {
  104. furi_assert(context);
  105. FlipChessStartscreen* instance = (FlipChessStartscreen*)context;
  106. with_view_model(
  107. instance->view,
  108. FlipChessStartscreenModel * model,
  109. { flipchess_startscreen_model_init(model); },
  110. true);
  111. }
  112. FlipChessStartscreen* flipchess_startscreen_alloc() {
  113. FlipChessStartscreen* instance = malloc(sizeof(FlipChessStartscreen));
  114. instance->view = view_alloc();
  115. view_allocate_model(instance->view, ViewModelTypeLocking, sizeof(FlipChessStartscreenModel));
  116. view_set_context(instance->view, instance); // furi_assert crashes in events without this
  117. view_set_draw_callback(instance->view, (ViewDrawCallback)flipchess_startscreen_draw);
  118. view_set_input_callback(instance->view, flipchess_startscreen_input);
  119. //view_set_enter_callback(instance->view, flipchess_startscreen_enter);
  120. //view_set_exit_callback(instance->view, flipchess_startscreen_exit);
  121. with_view_model(
  122. instance->view,
  123. FlipChessStartscreenModel * model,
  124. { flipchess_startscreen_model_init(model); },
  125. true);
  126. return instance;
  127. }
  128. void flipchess_startscreen_free(FlipChessStartscreen* instance) {
  129. furi_assert(instance);
  130. with_view_model(instance->view, FlipChessStartscreenModel * model, { UNUSED(model); }, true);
  131. view_free(instance->view);
  132. free(instance);
  133. }
  134. View* flipchess_startscreen_get_view(FlipChessStartscreen* instance) {
  135. furi_assert(instance);
  136. return instance->view;
  137. }