flipchess.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. strcpy(app->import_game_text, app->input_text);
  28. int status = FlipChessStatusSuccess;
  29. if(status == FlipChessStatusSuccess) {
  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(app->view_dispatcher, flipchess_custom_event_callback);
  65. app->submenu = submenu_alloc();
  66. // Settings
  67. app->haptic = FlipChessHapticOn;
  68. app->white_mode = FlipChessPlayerHuman;
  69. app->black_mode = FlipChessPlayerAI1;
  70. // Main menu
  71. app->import_game = 0;
  72. // Text input
  73. app->input_state = FlipChessTextInputDefault;
  74. view_dispatcher_add_view(
  75. app->view_dispatcher, FlipChessViewIdMenu, submenu_get_view(app->submenu));
  76. app->flipchess_startscreen = flipchess_startscreen_alloc();
  77. view_dispatcher_add_view(
  78. app->view_dispatcher,
  79. FlipChessViewIdStartscreen,
  80. flipchess_startscreen_get_view(app->flipchess_startscreen));
  81. app->flipchess_scene_1 = flipchess_scene_1_alloc();
  82. view_dispatcher_add_view(
  83. app->view_dispatcher, FlipChessViewIdScene1, flipchess_scene_1_get_view(app->flipchess_scene_1));
  84. app->variable_item_list = variable_item_list_alloc();
  85. view_dispatcher_add_view(
  86. app->view_dispatcher,
  87. FlipChessViewIdSettings,
  88. variable_item_list_get_view(app->variable_item_list));
  89. app->text_input = text_input_alloc();
  90. text_input_set_result_callback(
  91. app->text_input,
  92. text_input_callback,
  93. (void*)app,
  94. app->input_text,
  95. TEXT_BUFFER_SIZE,
  96. //clear default text
  97. true);
  98. text_input_set_header_text(app->text_input, "Input");
  99. view_dispatcher_add_view(
  100. app->view_dispatcher, FlipChessViewIdTextInput, text_input_get_view(app->text_input));
  101. //End Scene Additions
  102. return app;
  103. }
  104. void flipchess_app_free(FlipChess* app) {
  105. furi_assert(app);
  106. // Scene manager
  107. scene_manager_free(app->scene_manager);
  108. text_input_free(app->text_input);
  109. // View Dispatcher
  110. view_dispatcher_remove_view(app->view_dispatcher, FlipChessViewIdMenu);
  111. view_dispatcher_remove_view(app->view_dispatcher, FlipChessViewIdScene1);
  112. view_dispatcher_remove_view(app->view_dispatcher, FlipChessViewIdSettings);
  113. view_dispatcher_remove_view(app->view_dispatcher, FlipChessViewIdTextInput);
  114. submenu_free(app->submenu);
  115. view_dispatcher_free(app->view_dispatcher);
  116. furi_record_close(RECORD_GUI);
  117. app->gui = NULL;
  118. app->notification = NULL;
  119. //Remove whatever is left
  120. //memzero(app, sizeof(FlipChess));
  121. free(app);
  122. }
  123. int32_t flipchess_app(void* p) {
  124. UNUSED(p);
  125. FlipChess* app = flipchess_app_alloc();
  126. // Disabled because causes exit on custom firmwares such as RM
  127. /*if(!furi_hal_region_is_provisioned()) {
  128. flipchess_app_free(app);
  129. return 1;
  130. }*/
  131. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  132. scene_manager_next_scene(
  133. app->scene_manager, FlipChessSceneStartscreen); //Start with start screen
  134. //scene_manager_next_scene(app->scene_manager, FlipChessSceneMenu); //if you want to directly start with Menu
  135. furi_hal_power_suppress_charge_enter();
  136. view_dispatcher_run(app->view_dispatcher);
  137. furi_hal_power_suppress_charge_exit();
  138. flipchess_app_free(app);
  139. return 0;
  140. }