diff --git a/DD2Switcher/Program.cs b/DD2Switcher/Program.cs index 9e91a67..ba8dc54 100644 --- a/DD2Switcher/Program.cs +++ b/DD2Switcher/Program.cs @@ -65,38 +65,14 @@ namespace DD2Switcher { CurrentSequenceIndex = sequenceFirstIndex; Console.WriteLine($"Starting sequence mode, tabbing to index {CurrentSequenceIndex + 1}"); TabTo(CurrentSequenceIndex + 1); // Tab to first window - CurrentState = SequenceState.WAITING; + CurrentState = SequenceState.WAITING_FOR_EVENT; Console.WriteLine($"State changed to: {CurrentState}"); } else { Console.WriteLine("Cannot start sequence mode - invalid first/last indices"); } } - public static void NextSequenceStep() { - Console.WriteLine($"NextSequenceStep called. State: {CurrentState}, CurrentIndex: {CurrentSequenceIndex}"); - if (CurrentState == SequenceState.INACTIVE) - return; - CurrentState = SequenceState.PROCESSING; - CurrentSequenceIndex++; - Console.WriteLine($"Advanced to index {CurrentSequenceIndex}"); - - // 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(sequenceFirstIndex + 1); - ExitSequenceMode(); - } else { - Console.WriteLine($"Tabbing to index {CurrentSequenceIndex + 1}"); - TabTo(CurrentSequenceIndex + 1); - CurrentState = SequenceState.WAITING; - Console.WriteLine($"State changed to: {CurrentState}"); - } - } public static void ExitSequenceMode() { CurrentState = SequenceState.INACTIVE; @@ -218,7 +194,7 @@ namespace DD2Switcher { public static int LastIndex { get; set; } = -1; // Sequence mode state engine - public enum SequenceState { INACTIVE, WAITING, PROCESSING } + public enum SequenceState { INACTIVE, WAITING_FOR_EVENT, PROCESSING, WAITING_TO_ADVANCE } public static SequenceState CurrentState { get; set; } = SequenceState.INACTIVE; public static int CurrentSequenceIndex { get; set; } = -1; @@ -227,6 +203,74 @@ namespace DD2Switcher { private static int ActiveIndex = -1; private static bool AltPressed = false; + // State handlers + private static readonly Dictionary stateHandlers = new Dictionary { + { SequenceState.INACTIVE, HandleInactive }, + { SequenceState.WAITING_FOR_EVENT, HandleWaitingForEvent }, + { SequenceState.PROCESSING, HandleProcessing }, + { SequenceState.WAITING_TO_ADVANCE, HandleWaitingToAdvance } + }; + + private static void HandleInactive() { + // Inactive state - do nothing + } + + private static void HandleWaitingForEvent() { + Console.WriteLine($"Event detected in state: {CurrentState}, advancing to next step"); + + CurrentState = SequenceState.PROCESSING; + CurrentSequenceIndex++; + Console.WriteLine($"Advanced to index {CurrentSequenceIndex}"); + + // 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(sequenceFirstIndex + 1); + ExitSequenceMode(); + } else { + Console.WriteLine($"Tabbing to index {CurrentSequenceIndex + 1}"); + TabTo(CurrentSequenceIndex + 1); + CurrentState = SequenceState.WAITING_FOR_EVENT; + Console.WriteLine($"State changed to: {CurrentState}"); + } + } + + private static void HandleProcessing() { + // Processing state - do nothing, just a transition state + } + + private static void HandleWaitingToAdvance() { + Console.WriteLine($"Advancing sequence from WAITING_TO_ADVANCE state"); + + CurrentState = SequenceState.PROCESSING; + CurrentSequenceIndex++; + Console.WriteLine($"Advanced to index {CurrentSequenceIndex}"); + + // 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(sequenceFirstIndex + 1); + ExitSequenceMode(); + } else { + Console.WriteLine($"Tabbing to index {CurrentSequenceIndex + 1}"); + TabTo(CurrentSequenceIndex + 1); + CurrentState = SequenceState.WAITING_FOR_EVENT; + Console.WriteLine($"State changed to: {CurrentState}"); + } + } + + private static void HandleSequenceEvent() { + stateHandlers[CurrentState]?.Invoke(); + } + // Simple list to track last 5 windows private static List lastWindows = new List(); @@ -272,20 +316,14 @@ namespace DD2Switcher { private delegate IntPtr MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam); - private static void HandleSequenceEvent() { - if (CurrentState == SequenceState.WAITING) { - Console.WriteLine($"Event detected in state: {CurrentState}, advancing to next step"); - NextSequenceStep(); - } - } - private static IntPtr MouseHookCallback(int nCode, IntPtr wParam, IntPtr lParam) { if (nCode >= 0) { int wParamInt = (int)wParam; if (wParamInt == WM_LBUTTONDOWN || wParamInt == WM_RBUTTONDOWN || wParamInt == WM_MBUTTONDOWN) { - if (CurrentState == SequenceState.WAITING) { + if (CurrentState == SequenceState.WAITING_FOR_EVENT) { Console.WriteLine($"Mouse click detected in state: {CurrentState}"); - HandleSequenceEvent(); + CurrentState = SequenceState.WAITING_TO_ADVANCE; + Console.WriteLine($"State changed to: {CurrentState}"); } } } @@ -628,7 +666,7 @@ namespace DD2Switcher { } // Handle sequence mode event detection with KeyUp - if (CurrentState == SequenceState.WAITING) { + if (CurrentState == SequenceState.WAITING_FOR_EVENT) { // Ignore the sequence keybind itself if (e != (int)SequenceKeybind) { Console.WriteLine($"Key up detected in state: {CurrentState} - Key: {e}"); @@ -636,6 +674,9 @@ namespace DD2Switcher { } else { Console.WriteLine($"Ignoring sequence keybind release: {e}"); } + } else if (CurrentState == SequenceState.WAITING_TO_ADVANCE) { + // Ignore key events while waiting to advance + Console.WriteLine($"Ignoring key event in WAITING_TO_ADVANCE state: {e}"); } }; @@ -665,7 +706,7 @@ namespace DD2Switcher { Console.WriteLine("Sequence keybind detected!"); if (CurrentState != SequenceState.INACTIVE) { Console.WriteLine("Advancing sequence step"); - NextSequenceStep(); + HandleSequenceEvent(); } else { Console.WriteLine("Starting sequence mode"); StartSequenceMode();