dolphin.c 6.3 KB

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