Skip to content

Commit

Permalink
Added a 2-second timer to autosave personal notes, based on frames id…
Browse files Browse the repository at this point in the history
…le after editing and ticked by `OnGameFrameUpdate`. (HarbourMasters#3155)
  • Loading branch information
Malkierian authored Sep 1, 2023
1 parent dd37d4f commit 644ab7f
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions soh/soh/Enhancements/randomizer/randomizer_item_tracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,22 @@ struct ItemTrackerNumbers {
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 validSave = gSaveContext.fileNum >= 0 && gSaveContext.fileNum <= 2;
return validSave;
Expand Down Expand Up @@ -623,8 +639,6 @@ void DrawSong(ItemTrackerItem item) {
UIWidgets::SetLastItemHoverText(SohUtils::GetQuestItemName(item.id));
}

static ImVector<char> itemTrackerNotes;

void DrawNotes(bool resizeable = false) {
ImGui::BeginGroup();
int iconSize = CVarGetInteger("gItemTrackerIconSize", 36);
Expand All @@ -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();
}
Expand Down Expand Up @@ -1116,4 +1133,5 @@ void ItemTrackerWindow::InitElement() {
CVarSetString(("gItemTrackerNotes" + std::to_string(fileNum)).c_str(), "");
LUS::Context::GetInstance()->GetWindow()->GetGui()->SaveConsoleVariablesOnNextTick();
});
GameInteractor::Instance->RegisterGameHook<GameInteractor::OnGameFrameUpdate>(ItemTrackerOnFrame);
}

0 comments on commit 644ab7f

Please sign in to comment.