scene-controller.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #include <map>
  2. #include <forward_list>
  3. #include <initializer_list>
  4. #define GENERIC_SCENE_ENUM_VALUES Uninitalized, Exit, Start
  5. #define GENERIC_EVENT_ENUM_VALUES Tick, Back
  6. /**
  7. * @brief Controller for scene navigation in application
  8. *
  9. * @tparam TScene generic scene class
  10. * @tparam TApp application class
  11. */
  12. template <typename TScene, typename TApp> class SceneController {
  13. public:
  14. /**
  15. * @brief Add scene to scene container
  16. *
  17. * @param scene_index scene index
  18. * @param scene_pointer scene object pointer
  19. */
  20. void add_scene(typename TApp::SceneType scene_index, TScene* scene_pointer) {
  21. furi_check(scenes.count(scene_index) == 0);
  22. scenes[scene_index] = scene_pointer;
  23. }
  24. /**
  25. * @brief Switch to next scene and store current scene in previous scenes list
  26. *
  27. * @param scene_index next scene index
  28. * @param need_restore true, if we want the scene to restore its parameters
  29. */
  30. void switch_to_next_scene(typename TApp::SceneType scene_index, bool need_restore = false) {
  31. previous_scenes_list.push_front(current_scene_index);
  32. switch_to_scene(scene_index, need_restore);
  33. }
  34. /**
  35. * @brief Switch to next scene without ability to return to current scene
  36. *
  37. * @param scene_index next scene index
  38. * @param need_restore true, if we want the scene to restore its parameters
  39. */
  40. void switch_to_scene(typename TApp::SceneType scene_index, bool need_restore = false) {
  41. if(scene_index != TApp::SceneType::Exit) {
  42. scenes[current_scene_index]->on_exit(app);
  43. current_scene_index = scene_index;
  44. scenes[current_scene_index]->on_enter(app, need_restore);
  45. }
  46. }
  47. /**
  48. * @brief Search the scene in the list of previous scenes and switch to it
  49. *
  50. * @param scene_index_list list of scene indexes to which you want to switch
  51. */
  52. void search_and_switch_to_previous_scene(
  53. const std::initializer_list<typename TApp::SceneType>& scene_index_list) {
  54. auto previous_scene_index = TApp::SceneType::Start;
  55. bool scene_found = false;
  56. while(!scene_found) {
  57. previous_scene_index = get_previous_scene_index();
  58. for(const auto& element : scene_index_list) {
  59. if(previous_scene_index == element) {
  60. scene_found = true;
  61. break;
  62. }
  63. }
  64. }
  65. switch_to_scene(previous_scene_index, true);
  66. }
  67. /**
  68. * @brief Start application main cycle
  69. *
  70. * @param tick_length_ms tick event length in milliseconds
  71. */
  72. void process(uint32_t tick_length_ms = 100) {
  73. typename TApp::Event event;
  74. bool consumed;
  75. bool exit = false;
  76. current_scene_index = TApp::SceneType::Start;
  77. scenes[current_scene_index]->on_enter(app, false);
  78. while(!exit) {
  79. app->view_controller.receive_event(&event);
  80. consumed = scenes[current_scene_index]->on_event(app, &event);
  81. if(!consumed) {
  82. if(event.type == TApp::EventType::Back) {
  83. exit = switch_to_previous_scene();
  84. }
  85. }
  86. };
  87. scenes[current_scene_index]->on_exit(app);
  88. }
  89. /**
  90. * @brief Switch to previous scene
  91. *
  92. * @param count how many steps back
  93. * @return true if app need to exit
  94. */
  95. bool switch_to_previous_scene(uint8_t count = 1) {
  96. auto previous_scene_index = TApp::SceneType::Start;
  97. for(uint8_t i = 0; i < count; i++) previous_scene_index = get_previous_scene_index();
  98. if(previous_scene_index == TApp::SceneType::Exit) return true;
  99. switch_to_scene(previous_scene_index, true);
  100. return false;
  101. }
  102. /**
  103. * @brief Construct a new Scene Controller object
  104. *
  105. * @param app_pointer pointer to application class
  106. */
  107. SceneController(TApp* app_pointer) {
  108. app = app_pointer;
  109. current_scene_index = TApp::SceneType::Uninitalized;
  110. }
  111. /**
  112. * @brief Destroy the Scene Controller object
  113. *
  114. */
  115. ~SceneController() {
  116. for(auto& it : scenes) delete it.second;
  117. }
  118. private:
  119. /**
  120. * @brief Scenes pointers container
  121. *
  122. */
  123. std::map<typename TApp::SceneType, TScene*> scenes;
  124. /**
  125. * @brief List of indexes of previous scenes
  126. *
  127. */
  128. std::forward_list<typename TApp::SceneType> previous_scenes_list;
  129. /**
  130. * @brief Current scene index holder
  131. *
  132. */
  133. typename TApp::SceneType current_scene_index;
  134. /**
  135. * @brief Application pointer holder
  136. *
  137. */
  138. TApp* app;
  139. /**
  140. * @brief Get the previous scene index
  141. *
  142. * @return previous scene index
  143. */
  144. typename TApp::SceneType get_previous_scene_index() {
  145. auto scene_index = TApp::SceneType::Exit;
  146. if(!previous_scenes_list.empty()) {
  147. scene_index = previous_scenes_list.front();
  148. previous_scenes_list.pop_front();
  149. }
  150. return scene_index;
  151. }
  152. };