dolphin.c 6.0 KB

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