OMOTE/Platformio/src/applicationInternal/scenes/sceneHandler.cpp

111 lines
4.6 KiB
C++
Raw Normal View History

2024-03-10 14:27:46 -04:00
#include <string>
#include <lvgl.h>
#include "applicationInternal/gui/guiBase.h"
#include "applicationInternal/gui/guiMemoryOptimizer.h"
2024-03-10 14:27:46 -04:00
#include "applicationInternal/scenes/sceneRegistry.h"
#include "applicationInternal/hardware/hardwarePresenter.h"
#include "applicationInternal/commandHandler.h"
#include "scenes/scene__default.h"
2024-03-10 14:27:46 -04:00
void setLabelActiveScene() {
if ((SceneLabel != NULL) && sceneExists(gui_memoryOptimizer_getActiveSceneName())) {
lv_label_set_text(SceneLabel, gui_memoryOptimizer_getActiveSceneName().c_str());
}
}
2024-03-10 14:27:46 -04:00
void handleScene(uint16_t command, commandData commandData, std::string additionalPayload = "") {
2024-03-25 17:08:51 -04:00
// FORCE can be either as second payload in commandData
// e.g. register_command(&SCENE_TV_FORCE , makeCommandData(SCENE, {scene_name_TV, "FORCE"}));
// or as additionalPayload, used by gui_sceneSelection.cpp
// e.g. executeCommand(activate_scene_command, "FORCE");
2024-03-10 14:27:46 -04:00
auto current = commandData.commandPayloads.begin();
std::string scene_name = *current;
2024-03-25 17:08:51 -04:00
// we can have a second payload
std::string isForcePayload = "";
++current;
if (current != commandData.commandPayloads.end()) {
isForcePayload = *current;
}
// do not really switch scene, but show sceneSelection gui. From that on, we are in the main_gui_list.
if (scene_name == scene_name_selection) {
guis_doTabCreationAfterGUIlistChanged(MAIN_GUI_LIST);
2024-03-25 17:08:51 -04:00
return;
}
2024-03-10 14:27:46 -04:00
// do not switch scene, but navigate to the prev or next gui in the currently active list of guis
if ((scene_name == scene_gui_next) || (scene_name == scene_gui_prev)) {
if (scene_name == scene_gui_prev) {
if (gui_memoryOptimizer_getActiveTabID() == 0) {
Serial.println("scene: cannot navigate to prev gui, because there is none");
} else {
Serial.println("scene: will navigate to prev gui");
setActiveTab(gui_memoryOptimizer_getActiveTabID() -1, LV_ANIM_ON, true);
}
} else if (scene_name == scene_gui_next) {
if (!gui_memoryOptimizer_isTabIDInMemory(gui_memoryOptimizer_getActiveTabID() +1)) {
Serial.println("scene: cannot navigate to next gui, because there is none");
} else {
Serial.println("scene: will navigate to next gui");
setActiveTab(gui_memoryOptimizer_getActiveTabID() +1, LV_ANIM_ON, true);
}
}
return;
}
2024-03-10 14:27:46 -04:00
// check if we know the new scene
if (!sceneExists(scene_name)) {
Serial.printf("scene: cannot start scene %s, because it is unknown\r\n", scene_name.c_str());
return;
} else {
Serial.printf("scene: will switch from old scene %s to new scene %s\r\n", gui_memoryOptimizer_getActiveSceneName().c_str(), scene_name.c_str());
2024-03-10 14:27:46 -04:00
}
2024-03-25 17:08:51 -04:00
// do not activate the same scene again, only when forced to do so (e.g. by long press on the gui or when selected by hardware key)
bool callEndAndStartSequences;
if ((scene_name == gui_memoryOptimizer_getActiveSceneName()) && ((isForcePayload != "FORCE") && (additionalPayload != "FORCE"))) {
2024-03-25 17:08:51 -04:00
Serial.printf("scene: will not start scene again, because it is already active\r\n");
callEndAndStartSequences = false;
} else if ((scene_name == gui_memoryOptimizer_getActiveSceneName()) && ((isForcePayload == "FORCE") || (additionalPayload == "FORCE"))) {
2024-03-25 17:08:51 -04:00
Serial.printf("scene: scene is already active, but FORCE was set, so start scene again\r\n");
callEndAndStartSequences = true;
} else {
// this is the default when switching to a different scene
callEndAndStartSequences = true;
}
2024-03-10 14:27:46 -04:00
if (SceneLabel != NULL) {lv_label_set_text(SceneLabel, "changing...");}
gui_loop();
2024-03-25 17:08:51 -04:00
if (callEndAndStartSequences) {
// end old scene
if (!sceneExists(gui_memoryOptimizer_getActiveSceneName()) && (gui_memoryOptimizer_getActiveSceneName() != "")) {
Serial.printf("scene: WARNING: cannot end scene %s, because it is unknown\r\n", gui_memoryOptimizer_getActiveSceneName().c_str());
2024-03-25 17:08:51 -04:00
} else {
if (gui_memoryOptimizer_getActiveSceneName() != "") {
Serial.printf("scene: will call end sequence for scene %s\r\n", gui_memoryOptimizer_getActiveSceneName().c_str());
scene_end_sequence_from_registry(gui_memoryOptimizer_getActiveSceneName());
2024-03-25 17:08:51 -04:00
}
2024-03-10 14:27:46 -04:00
}
2024-03-25 17:08:51 -04:00
// start new scene
Serial.printf("scene: will call start sequence for scene %s\r\n", scene_name.c_str());
scene_start_sequence_from_registry(scene_name);
2024-03-10 14:27:46 -04:00
}
gui_memoryOptimizer_setActiveSceneName(scene_name);
2024-03-10 14:27:46 -04:00
if (SceneLabel != NULL) {lv_label_set_text(SceneLabel, gui_memoryOptimizer_getActiveSceneName().c_str());}
2024-03-10 14:27:46 -04:00
Serial.printf("scene: scene handling finished, new scene %s is active\r\n", gui_memoryOptimizer_getActiveSceneName().c_str());
2024-03-25 17:08:51 -04:00
guis_doTabCreationAfterGUIlistChanged(SCENE_GUI_LIST);
2024-03-10 14:27:46 -04:00
}