flipchess.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #include "flipchess.h"
  2. #include "helpers/flipchess_haptic.h"
  3. bool flipchess_custom_event_callback(void* context, uint32_t event) {
  4. furi_assert(context);
  5. FlipChess* app = context;
  6. return scene_manager_handle_custom_event(app->scene_manager, event);
  7. }
  8. void flipchess_tick_event_callback(void* context) {
  9. furi_assert(context);
  10. FlipChess* app = context;
  11. scene_manager_handle_tick_event(app->scene_manager);
  12. }
  13. //leave app if back button pressed
  14. bool flipchess_navigation_event_callback(void* context) {
  15. furi_assert(context);
  16. FlipChess* app = context;
  17. return scene_manager_handle_back_event(app->scene_manager);
  18. }
  19. static void text_input_callback(void* context) {
  20. furi_assert(context);
  21. FlipChess* app = context;
  22. bool handled = false;
  23. // check that there is text in the input
  24. if(strlen(app->input_text) > 0) {
  25. if(app->input_state == FlipChessTextInputGame) {
  26. if(app->import_game == 1) {
  27. strncpy(app->import_game_text, app->input_text, TEXT_SIZE);
  28. uint8_t status = FlipChessStatusNone;
  29. if(status == FlipChessStatusNone) {
  30. //notification_message(app->notification, &sequence_blink_cyan_100);
  31. flipchess_play_happy_bump(app);
  32. } else {
  33. //notification_message(app->notification, &sequence_blink_red_100);
  34. flipchess_play_long_bump(app);
  35. }
  36. }
  37. // reset input state
  38. app->input_state = FlipChessTextInputDefault;
  39. handled = true;
  40. view_dispatcher_switch_to_view(app->view_dispatcher, FlipChessViewIdMenu);
  41. }
  42. }
  43. if(!handled) {
  44. // reset input state
  45. app->input_state = FlipChessTextInputDefault;
  46. view_dispatcher_switch_to_view(app->view_dispatcher, FlipChessViewIdMenu);
  47. }
  48. }
  49. FlipChess* flipchess_app_alloc() {
  50. FlipChess* app = malloc(sizeof(FlipChess));
  51. app->gui = furi_record_open(RECORD_GUI);
  52. app->notification = furi_record_open(RECORD_NOTIFICATION);
  53. //Turn backlight on, believe me this makes testing your app easier
  54. notification_message(app->notification, &sequence_display_backlight_on);
  55. //Scene additions
  56. app->view_dispatcher = view_dispatcher_alloc();
  57. view_dispatcher_enable_queue(app->view_dispatcher);
  58. app->scene_manager = scene_manager_alloc(&flipchess_scene_handlers, app);
  59. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  60. view_dispatcher_set_navigation_event_callback(
  61. app->view_dispatcher, flipchess_navigation_event_callback);
  62. view_dispatcher_set_tick_event_callback(
  63. app->view_dispatcher, flipchess_tick_event_callback, 100);
  64. view_dispatcher_set_custom_event_callback(
  65. app->view_dispatcher, flipchess_custom_event_callback);
  66. app->submenu = submenu_alloc();
  67. // Settings
  68. app->haptic = FlipChessHapticOn;
  69. app->white_mode = FlipChessPlayerHuman;
  70. app->black_mode = FlipChessPlayerAI1;
  71. // Startscreen
  72. app->sound = 0;
  73. // Main menu
  74. app->import_game = 0;
  75. // Text input
  76. app->input_state = FlipChessTextInputDefault;
  77. view_dispatcher_add_view(
  78. app->view_dispatcher, FlipChessViewIdMenu, submenu_get_view(app->submenu));
  79. app->flipchess_startscreen = flipchess_startscreen_alloc();
  80. view_dispatcher_add_view(
  81. app->view_dispatcher,
  82. FlipChessViewIdStartscreen,
  83. flipchess_startscreen_get_view(app->flipchess_startscreen));
  84. app->flipchess_scene_1 = flipchess_scene_1_alloc();
  85. view_dispatcher_add_view(
  86. app->view_dispatcher,
  87. FlipChessViewIdScene1,
  88. flipchess_scene_1_get_view(app->flipchess_scene_1));
  89. app->variable_item_list = variable_item_list_alloc();
  90. view_dispatcher_add_view(
  91. app->view_dispatcher,
  92. FlipChessViewIdSettings,
  93. variable_item_list_get_view(app->variable_item_list));
  94. app->text_input = text_input_alloc();
  95. text_input_set_result_callback(
  96. app->text_input,
  97. text_input_callback,
  98. (void*)app,
  99. app->input_text,
  100. TEXT_BUFFER_SIZE,
  101. //clear default text
  102. true);
  103. text_input_set_header_text(app->text_input, "Input");
  104. view_dispatcher_add_view(
  105. app->view_dispatcher, FlipChessViewIdTextInput, text_input_get_view(app->text_input));
  106. //End Scene Additions
  107. return app;
  108. }
  109. void flipchess_app_free(FlipChess* app) {
  110. furi_assert(app);
  111. // Scene manager
  112. scene_manager_free(app->scene_manager);
  113. text_input_free(app->text_input);
  114. // View Dispatcher
  115. view_dispatcher_remove_view(app->view_dispatcher, FlipChessViewIdMenu);
  116. view_dispatcher_remove_view(app->view_dispatcher, FlipChessViewIdScene1);
  117. view_dispatcher_remove_view(app->view_dispatcher, FlipChessViewIdSettings);
  118. view_dispatcher_remove_view(app->view_dispatcher, FlipChessViewIdTextInput);
  119. submenu_free(app->submenu);
  120. view_dispatcher_free(app->view_dispatcher);
  121. furi_record_close(RECORD_GUI);
  122. app->gui = NULL;
  123. app->notification = NULL;
  124. //Remove whatever is left
  125. //memzero(app, sizeof(FlipChess));
  126. free(app);
  127. }
  128. int32_t flipchess_app(void* p) {
  129. UNUSED(p);
  130. FlipChess* app = flipchess_app_alloc();
  131. // Disabled because causes exit on custom firmwares such as RM
  132. /*if(!furi_hal_region_is_provisioned()) {
  133. flipchess_app_free(app);
  134. return 1;
  135. }*/
  136. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  137. scene_manager_next_scene(
  138. app->scene_manager, FlipChessSceneStartscreen); //Start with start screen
  139. //scene_manager_next_scene(app->scene_manager, FlipChessSceneMenu); //if you want to directly start with Menu
  140. furi_hal_random_init();
  141. // furi_hal_power_suppress_charge_enter();
  142. view_dispatcher_run(app->view_dispatcher);
  143. // furi_hal_power_suppress_charge_exit();
  144. flipchess_app_free(app);
  145. return 0;
  146. }