flipchess_startscreen.c 5.0 KB

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