From 0b7aaa2f99397595bb9211ca90a8875d6f832031 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Sun, 31 Aug 2025 21:43:04 +0200 Subject: [PATCH] refactor(DD2Switcher): simplify sequence index handling and settings loading --- DD2Switcher/Program.cs | 46 ++++++++++++++++++------------------- DD2Switcher/SettingsForm.cs | 12 +++++++++- 2 files changed, 34 insertions(+), 24 deletions(-) diff --git a/DD2Switcher/Program.cs b/DD2Switcher/Program.cs index 813b080..9e91a67 100644 --- a/DD2Switcher/Program.cs +++ b/DD2Switcher/Program.cs @@ -48,25 +48,21 @@ namespace DD2Switcher { public static void StartSequenceMode() { Console.WriteLine($"StartSequenceMode called. FirstIndex: {FirstIndex}, LastIndex: {LastIndex}"); - // Compute default indices if not set by user - if (FirstIndex == -1 || LastIndex == -1) { - FirstIndex = FindFirstNonNullWindow(); - LastIndex = FindLastNonNullWindow(); - Console.WriteLine($"Computed default indices: FirstIndex={FirstIndex}, LastIndex={LastIndex}"); + // Compute indices only when sequence starts, not stored permanently + int sequenceFirstIndex = FirstIndex; + int sequenceLastIndex = LastIndex; - // Ensure LastIndex is different from FirstIndex if there are multiple windows - if (FirstIndex != -1 && LastIndex != -1 && FirstIndex == LastIndex) { - int nextWindow = FindNextNonNullWindow(FirstIndex + 1); - if (nextWindow != -1) { - LastIndex = nextWindow; - Console.WriteLine($"Adjusted LastIndex to: {LastIndex} (different from FirstIndex)"); - } - } + // If no user indices, use absolute first and last windows + if (sequenceFirstIndex == -1 || sequenceLastIndex == -1) { + sequenceFirstIndex = FindFirstNonNullWindow(); + sequenceLastIndex = FindLastNonNullWindow(); + Console.WriteLine( + $"Computed sequence indices: FirstIndex={sequenceFirstIndex}, LastIndex={sequenceLastIndex}"); } - if (FirstIndex >= 0 && LastIndex >= 0 && FirstIndex <= LastIndex) { + if (sequenceFirstIndex >= 0 && sequenceLastIndex >= 0 && sequenceFirstIndex <= sequenceLastIndex) { CurrentState = SequenceState.PROCESSING; - CurrentSequenceIndex = FirstIndex; + CurrentSequenceIndex = sequenceFirstIndex; Console.WriteLine($"Starting sequence mode, tabbing to index {CurrentSequenceIndex + 1}"); TabTo(CurrentSequenceIndex + 1); // Tab to first window CurrentState = SequenceState.WAITING; @@ -77,18 +73,22 @@ namespace DD2Switcher { } public static void NextSequenceStep() { - Console.WriteLine( - $"NextSequenceStep called. State: {CurrentState}, CurrentIndex: {CurrentSequenceIndex}, LastIndex: {LastIndex}"); + Console.WriteLine($"NextSequenceStep called. State: {CurrentState}, CurrentIndex: {CurrentSequenceIndex}"); if (CurrentState == SequenceState.INACTIVE) return; CurrentState = SequenceState.PROCESSING; CurrentSequenceIndex++; Console.WriteLine($"Advanced to index {CurrentSequenceIndex}"); - if (CurrentSequenceIndex > LastIndex) { + + // Get the actual last index for this sequence (computed or user-set) + int sequenceLastIndex = (LastIndex != -1) ? LastIndex : FindLastNonNullWindow(); + + if (CurrentSequenceIndex > sequenceLastIndex) { // End of sequence - tab back to first + int sequenceFirstIndex = (FirstIndex != -1) ? FirstIndex : FindFirstNonNullWindow(); Console.WriteLine("End of sequence reached, tabbing back to first window"); - TabTo(FirstIndex + 1); + TabTo(sequenceFirstIndex + 1); ExitSequenceMode(); } else { Console.WriteLine($"Tabbing to index {CurrentSequenceIndex + 1}"); @@ -116,11 +116,11 @@ namespace DD2Switcher { string json = File.ReadAllText(settingsPath); Console.WriteLine($"Read JSON: {json}"); var settings = JsonSerializer.Deserialize(json); - FirstIndex = settings.FirstIndex; - LastIndex = settings.LastIndex; + // DO NOT load FirstIndex and LastIndex - they should be set by user only + // FirstIndex = settings.FirstIndex; + // LastIndex = settings.LastIndex; SequenceKeybind = settings.SequenceKeybind; - Console.WriteLine( - $"Loaded settings: First={FirstIndex}, Last={LastIndex}, Keybind={SequenceKeybind}"); + Console.WriteLine($"Loaded settings: Keybind={SequenceKeybind} (First/Last indices NOT loaded)"); } else { Console.WriteLine($"Settings file does not exist at: {settingsPath}"); Console.WriteLine("Using default settings"); diff --git a/DD2Switcher/SettingsForm.cs b/DD2Switcher/SettingsForm.cs index 80bc67c..539da9a 100644 --- a/DD2Switcher/SettingsForm.cs +++ b/DD2Switcher/SettingsForm.cs @@ -151,10 +151,18 @@ namespace DD2Switcher { windowPanelForm.UpdateDisplay(); windowPanelForm.PickClicked += (sender, index) => { - if (Program.FirstIndex == -1) { + // If clicking on a window that's already first or last, remove that setting + if (index == Program.FirstIndex) { + Program.FirstIndex = -1; + Console.WriteLine($"Removed first index: {index}"); + } else if (index == Program.LastIndex) { + Program.LastIndex = -1; + Console.WriteLine($"Removed last index: {index}"); + } else if (Program.FirstIndex == -1) { // First pick - set both first and last Program.FirstIndex = index; Program.LastIndex = index; + Console.WriteLine($"Set first and last index: {index}"); } else if (Program.LastIndex == -1) { // Second pick - set last (must be >= first) if (index >= Program.FirstIndex) { @@ -163,6 +171,7 @@ namespace DD2Switcher { Program.LastIndex = Program.FirstIndex; Program.FirstIndex = index; } + Console.WriteLine($"Set last index: {Program.LastIndex}"); } else { // Subsequent picks - determine which becomes first int distanceToFirst = Math.Abs(index - Program.FirstIndex); @@ -185,6 +194,7 @@ namespace DD2Switcher { Program.LastIndex = index; } } + Console.WriteLine($"Updated indices: First={Program.FirstIndex}, Last={Program.LastIndex}"); } // Ensure last >= first