flipchess_startscreen.c 5.0 KB

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