From 644ab7f498fd789f8a52684892f277269f07268a Mon Sep 17 00:00:00 2001 From: Malkierian Date: Fri, 1 Sep 2023 11:43:25 -0700 Subject: [PATCH] Added a 2-second timer to autosave personal notes, based on frames idle after editing and ticked by `OnGameFrameUpdate`. (#3155) --- .../randomizer/randomizer_item_tracker.cpp | 30 +++++++++++++++---- 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/soh/soh/Enhancements/randomizer/randomizer_item_tracker.cpp b/soh/soh/Enhancements/randomizer/randomizer_item_tracker.cpp index c30a8d4c5..c82a40c7d 100644 --- a/soh/soh/Enhancements/randomizer/randomizer_item_tracker.cpp +++ b/soh/soh/Enhancements/randomizer/randomizer_item_tracker.cpp @@ -262,6 +262,22 @@ struct ItemTrackerNumbers { int currentAmmo; }; +static ImVector itemTrackerNotes; +uint32_t notesIdleFrames = 0; +bool notesNeedSave = false; +const uint32_t notesMaxIdleFrames = 40; // two seconds of game time, since OnGameFrameUpdate is used to tick + +void ItemTrackerOnFrame() { + if (notesNeedSave && notesIdleFrames <= notesMaxIdleFrames) { + notesIdleFrames++; + } +} + +void SaveNotes(uint32_t fileNum) { + CVarSetString(("gItemTrackerNotes" + std::to_string(fileNum)).c_str(), std::string(std::begin(itemTrackerNotes), std::end(itemTrackerNotes)).c_str()); + LUS::Context::GetInstance()->GetWindow()->GetGui()->SaveConsoleVariablesOnNextTick(); +} + bool IsValidSaveFile() { bool validSave = gSaveContext.fileNum >= 0 && gSaveContext.fileNum <= 2; return validSave; @@ -623,8 +639,6 @@ void DrawSong(ItemTrackerItem item) { UIWidgets::SetLastItemHoverText(SohUtils::GetQuestItemName(item.id)); } -static ImVector itemTrackerNotes; - void DrawNotes(bool resizeable = false) { ImGui::BeginGroup(); int iconSize = CVarGetInteger("gItemTrackerIconSize", 36); @@ -651,10 +665,13 @@ void DrawNotes(bool resizeable = false) { } }; ImVec2 size = resizeable ? ImVec2(-FLT_MIN, ImGui::GetContentRegionAvail().y) : ImVec2(((iconSize + iconSpacing) * 6) - 8, 200); - ItemTrackerNotes::TrackerNotesInputTextMultiline("##ItemTrackerNotes", &itemTrackerNotes, size, ImGuiInputTextFlags_AllowTabInput); - if (ImGui::IsItemDeactivatedAfterEdit() && IsValidSaveFile()) { - CVarSetString(("gItemTrackerNotes" + std::to_string(gSaveContext.fileNum)).c_str(), std::string(std::begin(itemTrackerNotes), std::end(itemTrackerNotes)).c_str()); - LUS::Context::GetInstance()->GetWindow()->GetGui()->SaveConsoleVariablesOnNextTick(); + if (ItemTrackerNotes::TrackerNotesInputTextMultiline("##ItemTrackerNotes", &itemTrackerNotes, size, ImGuiInputTextFlags_AllowTabInput)) { + notesNeedSave = true; + notesIdleFrames = 0; + } + if ((ImGui::IsItemDeactivatedAfterEdit() || (notesNeedSave && notesIdleFrames > notesMaxIdleFrames)) && IsValidSaveFile()) { + notesNeedSave = false; + SaveNotes(gSaveContext.fileNum); } ImGui::EndGroup(); } @@ -1116,4 +1133,5 @@ void ItemTrackerWindow::InitElement() { CVarSetString(("gItemTrackerNotes" + std::to_string(fileNum)).c_str(), ""); LUS::Context::GetInstance()->GetWindow()->GetGui()->SaveConsoleVariablesOnNextTick(); }); + GameInteractor::Instance->RegisterGameHook(ItemTrackerOnFrame); }