flipchess_startscreen.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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, 1, 33, &I_Auth_62x31);
  29. canvas_set_font(canvas, FontPrimary);
  30. canvas_draw_str(canvas, 10, 23, "Chess");
  31. canvas_set_font(canvas, FontSecondary);
  32. canvas_draw_str(canvas, 10, 11, "How about a nice game of...");
  33. canvas_draw_str(canvas, 99, 23, FLIPCHESS_VERSION);
  34. elements_button_right(canvas, "Start");
  35. }
  36. static void flipchess_startscreen_model_init(FlipChessStartscreenModel* const model) {
  37. model->some_value = 1;
  38. }
  39. bool flipchess_startscreen_input(InputEvent* event, void* context) {
  40. furi_assert(context);
  41. FlipChessStartscreen* instance = context;
  42. if(event->type == InputTypeRelease) {
  43. switch(event->key) {
  44. case InputKeyBack:
  45. with_view_model(
  46. instance->view,
  47. FlipChessStartscreenModel * model,
  48. {
  49. UNUSED(model);
  50. instance->callback(FlipChessCustomEventStartscreenBack, instance->context);
  51. },
  52. true);
  53. break;
  54. case InputKeyLeft:
  55. case InputKeyRight:
  56. case InputKeyUp:
  57. case InputKeyDown:
  58. case InputKeyOk:
  59. with_view_model(
  60. instance->view,
  61. FlipChessStartscreenModel * model,
  62. {
  63. UNUSED(model);
  64. instance->callback(FlipChessCustomEventStartscreenOk, instance->context);
  65. },
  66. true);
  67. break;
  68. case InputKeyMAX:
  69. break;
  70. }
  71. }
  72. return true;
  73. }
  74. void flipchess_startscreen_exit(void* context) {
  75. furi_assert(context);
  76. }
  77. void flipchess_startscreen_enter(void* context) {
  78. furi_assert(context);
  79. FlipChessStartscreen* instance = (FlipChessStartscreen*)context;
  80. with_view_model(
  81. instance->view,
  82. FlipChessStartscreenModel * model,
  83. { flipchess_startscreen_model_init(model); },
  84. true);
  85. }
  86. FlipChessStartscreen* flipchess_startscreen_alloc() {
  87. FlipChessStartscreen* instance = malloc(sizeof(FlipChessStartscreen));
  88. instance->view = view_alloc();
  89. view_allocate_model(instance->view, ViewModelTypeLocking, sizeof(FlipChessStartscreenModel));
  90. view_set_context(instance->view, instance); // furi_assert crashes in events without this
  91. view_set_draw_callback(instance->view, (ViewDrawCallback)flipchess_startscreen_draw);
  92. view_set_input_callback(instance->view, flipchess_startscreen_input);
  93. //view_set_enter_callback(instance->view, flipchess_startscreen_enter);
  94. //view_set_exit_callback(instance->view, flipchess_startscreen_exit);
  95. with_view_model(
  96. instance->view,
  97. FlipChessStartscreenModel * model,
  98. { flipchess_startscreen_model_init(model); },
  99. true);
  100. return instance;
  101. }
  102. void flipchess_startscreen_free(FlipChessStartscreen* instance) {
  103. furi_assert(instance);
  104. with_view_model(
  105. instance->view, FlipChessStartscreenModel * model, { UNUSED(model); }, true);
  106. view_free(instance->view);
  107. free(instance);
  108. }
  109. View* flipchess_startscreen_get_view(FlipChessStartscreen* instance) {
  110. furi_assert(instance);
  111. return instance->view;
  112. }