Added a 2-second timer to autosave personal notes, based on frames idle after editing and ticked by OnGameFrameUpdate. (#3155)

This commit is contained in:
Malkierian 2023-09-01 11:43:25 -07:00 committed by GitHub
parent dd37d4f9b8
commit 644ab7f498
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -262,6 +262,22 @@ struct ItemTrackerNumbers {
int currentAmmo; int currentAmmo;
}; };
static ImVector<char> 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 IsValidSaveFile() {
bool validSave = gSaveContext.fileNum >= 0 && gSaveContext.fileNum <= 2; bool validSave = gSaveContext.fileNum >= 0 && gSaveContext.fileNum <= 2;
return validSave; return validSave;
@ -623,8 +639,6 @@ void DrawSong(ItemTrackerItem item) {
UIWidgets::SetLastItemHoverText(SohUtils::GetQuestItemName(item.id)); UIWidgets::SetLastItemHoverText(SohUtils::GetQuestItemName(item.id));
} }
static ImVector<char> itemTrackerNotes;
void DrawNotes(bool resizeable = false) { void DrawNotes(bool resizeable = false) {
ImGui::BeginGroup(); ImGui::BeginGroup();
int iconSize = CVarGetInteger("gItemTrackerIconSize", 36); 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); ImVec2 size = resizeable ? ImVec2(-FLT_MIN, ImGui::GetContentRegionAvail().y) : ImVec2(((iconSize + iconSpacing) * 6) - 8, 200);
ItemTrackerNotes::TrackerNotesInputTextMultiline("##ItemTrackerNotes", &itemTrackerNotes, size, ImGuiInputTextFlags_AllowTabInput); if (ItemTrackerNotes::TrackerNotesInputTextMultiline("##ItemTrackerNotes", &itemTrackerNotes, size, ImGuiInputTextFlags_AllowTabInput)) {
if (ImGui::IsItemDeactivatedAfterEdit() && IsValidSaveFile()) { notesNeedSave = true;
CVarSetString(("gItemTrackerNotes" + std::to_string(gSaveContext.fileNum)).c_str(), std::string(std::begin(itemTrackerNotes), std::end(itemTrackerNotes)).c_str()); notesIdleFrames = 0;
LUS::Context::GetInstance()->GetWindow()->GetGui()->SaveConsoleVariablesOnNextTick(); }
if ((ImGui::IsItemDeactivatedAfterEdit() || (notesNeedSave && notesIdleFrames > notesMaxIdleFrames)) && IsValidSaveFile()) {
notesNeedSave = false;
SaveNotes(gSaveContext.fileNum);
} }
ImGui::EndGroup(); ImGui::EndGroup();
} }
@ -1116,4 +1133,5 @@ void ItemTrackerWindow::InitElement() {
CVarSetString(("gItemTrackerNotes" + std::to_string(fileNum)).c_str(), ""); CVarSetString(("gItemTrackerNotes" + std::to_string(fileNum)).c_str(), "");
LUS::Context::GetInstance()->GetWindow()->GetGui()->SaveConsoleVariablesOnNextTick(); LUS::Context::GetInstance()->GetWindow()->GetGui()->SaveConsoleVariablesOnNextTick();
}); });
GameInteractor::Instance->RegisterGameHook<GameInteractor::OnGameFrameUpdate>(ItemTrackerOnFrame);
} }