dolphin.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #include "dolphin_i.h"
  2. bool dolphin_view_first_start_input(InputEvent* event, void* context) {
  3. furi_assert(event);
  4. furi_assert(context);
  5. Dolphin* dolphin = context;
  6. if(event->type == InputTypeShort) {
  7. if(event->key == InputKeyLeft) {
  8. with_view_model(
  9. dolphin->idle_view_first_start, (DolphinViewFirstStartModel * model) {
  10. if(model->page > 0) model->page--;
  11. return true;
  12. });
  13. } else if(event->key == InputKeyRight) {
  14. uint32_t page;
  15. with_view_model(
  16. dolphin->idle_view_first_start, (DolphinViewFirstStartModel * model) {
  17. page = ++model->page;
  18. return true;
  19. });
  20. if(page > 8) {
  21. dolphin_save(dolphin);
  22. view_dispatcher_switch_to_view(dolphin->idle_view_dispatcher, DolphinViewIdleMain);
  23. }
  24. }
  25. }
  26. // All events consumed
  27. return true;
  28. }
  29. bool dolphin_view_idle_main_input(InputEvent* event, void* context) {
  30. furi_assert(event);
  31. furi_assert(context);
  32. Dolphin* dolphin = context;
  33. if(event->type == InputTypeShort) {
  34. if(event->key == InputKeyOk) {
  35. with_value_mutex(
  36. dolphin->menu_vm, (Menu * menu) { menu_ok(menu); });
  37. } else if(event->key == InputKeyUp) {
  38. view_dispatcher_switch_to_view(dolphin->idle_view_dispatcher, DolphinViewIdleStats);
  39. } else if(event->key == InputKeyDown) {
  40. view_dispatcher_switch_to_view(dolphin->idle_view_dispatcher, DolphinViewIdleDebug);
  41. }
  42. }
  43. // All events consumed
  44. return true;
  45. }
  46. bool dolphin_view_idle_stats_input(InputEvent* event, void* context) {
  47. furi_assert(event);
  48. furi_assert(context);
  49. Dolphin* dolphin = context;
  50. if(event->type != InputTypeShort) return false;
  51. if(event->key == InputKeyLeft) {
  52. dolphin_deed(dolphin, DolphinDeedWrong);
  53. } else if(event->key == InputKeyRight) {
  54. dolphin_deed(dolphin, DolphinDeedIButtonRead);
  55. } else if(event->key == InputKeyOk) {
  56. dolphin_save(dolphin);
  57. } else {
  58. return false;
  59. }
  60. return true;
  61. }
  62. Dolphin* dolphin_alloc() {
  63. Dolphin* dolphin = furi_alloc(sizeof(Dolphin));
  64. // Message queue
  65. dolphin->event_queue = osMessageQueueNew(8, sizeof(DolphinEvent), NULL);
  66. furi_check(dolphin->event_queue);
  67. // State
  68. dolphin->state = dolphin_state_alloc();
  69. // Menu
  70. dolphin->menu_vm = furi_record_open("menu");
  71. // GUI
  72. dolphin->idle_view_dispatcher = view_dispatcher_alloc();
  73. // First start View
  74. dolphin->idle_view_first_start = view_alloc();
  75. view_allocate_model(
  76. dolphin->idle_view_first_start, ViewModelTypeLockFree, sizeof(DolphinViewFirstStartModel));
  77. view_set_context(dolphin->idle_view_first_start, dolphin);
  78. view_set_draw_callback(dolphin->idle_view_first_start, dolphin_view_first_start_draw);
  79. view_set_input_callback(dolphin->idle_view_first_start, dolphin_view_first_start_input);
  80. view_dispatcher_add_view(
  81. dolphin->idle_view_dispatcher, DolphinViewFirstStart, dolphin->idle_view_first_start);
  82. // Main Idle View
  83. dolphin->idle_view_main = view_alloc();
  84. view_set_context(dolphin->idle_view_main, dolphin);
  85. view_set_draw_callback(dolphin->idle_view_main, dolphin_view_idle_main_draw);
  86. view_set_input_callback(dolphin->idle_view_main, dolphin_view_idle_main_input);
  87. view_dispatcher_add_view(
  88. dolphin->idle_view_dispatcher, DolphinViewIdleMain, dolphin->idle_view_main);
  89. // Stats Idle View
  90. dolphin->idle_view_stats = view_alloc();
  91. view_set_context(dolphin->idle_view_stats, dolphin);
  92. view_allocate_model(
  93. dolphin->idle_view_stats, ViewModelTypeLockFree, sizeof(DolphinViewIdleStatsModel));
  94. view_set_draw_callback(dolphin->idle_view_stats, dolphin_view_idle_stats_draw);
  95. view_set_input_callback(dolphin->idle_view_stats, dolphin_view_idle_stats_input);
  96. view_set_previous_callback(dolphin->idle_view_stats, dolphin_view_idle_back);
  97. view_dispatcher_add_view(
  98. dolphin->idle_view_dispatcher, DolphinViewIdleStats, dolphin->idle_view_stats);
  99. // Debug Idle View
  100. dolphin->idle_view_debug = view_alloc();
  101. view_set_draw_callback(dolphin->idle_view_debug, dolphin_view_idle_debug_draw);
  102. view_set_previous_callback(dolphin->idle_view_debug, dolphin_view_idle_back);
  103. view_dispatcher_add_view(
  104. dolphin->idle_view_dispatcher, DolphinViewIdleDebug, dolphin->idle_view_debug);
  105. return dolphin;
  106. }
  107. void dolphin_save(Dolphin* dolphin) {
  108. furi_assert(dolphin);
  109. DolphinEvent event;
  110. event.type = DolphinEventTypeSave;
  111. furi_check(osMessageQueuePut(dolphin->event_queue, &event, 0, osWaitForever) == osOK);
  112. }
  113. void dolphin_deed(Dolphin* dolphin, DolphinDeed deed) {
  114. furi_assert(dolphin);
  115. DolphinEvent event;
  116. event.type = DolphinEventTypeDeed;
  117. event.deed = deed;
  118. furi_check(osMessageQueuePut(dolphin->event_queue, &event, 0, osWaitForever) == osOK);
  119. }
  120. int32_t dolphin_task() {
  121. Dolphin* dolphin = dolphin_alloc();
  122. Gui* gui = furi_record_open("gui");
  123. view_dispatcher_attach_to_gui(dolphin->idle_view_dispatcher, gui, ViewDispatcherTypeWindow);
  124. if(dolphin_state_load(dolphin->state)) {
  125. view_dispatcher_switch_to_view(dolphin->idle_view_dispatcher, DolphinViewIdleMain);
  126. } else {
  127. view_dispatcher_switch_to_view(dolphin->idle_view_dispatcher, DolphinViewFirstStart);
  128. }
  129. with_view_model(
  130. dolphin->idle_view_stats, (DolphinViewIdleStatsModel * model) {
  131. model->icounter = dolphin_state_get_icounter(dolphin->state);
  132. model->butthurt = dolphin_state_get_butthurt(dolphin->state);
  133. return true;
  134. });
  135. furi_record_create("dolphin", dolphin);
  136. DolphinEvent event;
  137. while(1) {
  138. furi_check(osMessageQueueGet(dolphin->event_queue, &event, NULL, osWaitForever) == osOK);
  139. if(event.type == DolphinEventTypeDeed) {
  140. dolphin_state_on_deed(dolphin->state, event.deed);
  141. with_view_model(
  142. dolphin->idle_view_stats, (DolphinViewIdleStatsModel * model) {
  143. model->icounter = dolphin_state_get_icounter(dolphin->state);
  144. model->butthurt = dolphin_state_get_butthurt(dolphin->state);
  145. return true;
  146. });
  147. } else if(event.type == DolphinEventTypeSave) {
  148. dolphin_state_save(dolphin->state);
  149. }
  150. }
  151. return 0;
  152. }