refactor(Program.cs): restructure sequence state handling with dedicated handlers and new states

This commit is contained in:
2025-08-31 22:06:20 +02:00
parent 0b7aaa2f99
commit 833355cd60

View File

@@ -65,38 +65,14 @@ namespace DD2Switcher {
CurrentSequenceIndex = sequenceFirstIndex; CurrentSequenceIndex = sequenceFirstIndex;
Console.WriteLine($"Starting sequence mode, tabbing to index {CurrentSequenceIndex + 1}"); Console.WriteLine($"Starting sequence mode, tabbing to index {CurrentSequenceIndex + 1}");
TabTo(CurrentSequenceIndex + 1); // Tab to first window TabTo(CurrentSequenceIndex + 1); // Tab to first window
CurrentState = SequenceState.WAITING; CurrentState = SequenceState.WAITING_FOR_EVENT;
Console.WriteLine($"State changed to: {CurrentState}"); Console.WriteLine($"State changed to: {CurrentState}");
} else { } else {
Console.WriteLine("Cannot start sequence mode - invalid first/last indices"); 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() { public static void ExitSequenceMode() {
CurrentState = SequenceState.INACTIVE; CurrentState = SequenceState.INACTIVE;
@@ -218,7 +194,7 @@ namespace DD2Switcher {
public static int LastIndex { get; set; } = -1; public static int LastIndex { get; set; } = -1;
// Sequence mode state engine // 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 SequenceState CurrentState { get; set; } = SequenceState.INACTIVE;
public static int CurrentSequenceIndex { get; set; } = -1; public static int CurrentSequenceIndex { get; set; } = -1;
@@ -227,6 +203,74 @@ namespace DD2Switcher {
private static int ActiveIndex = -1; private static int ActiveIndex = -1;
private static bool AltPressed = false; private static bool AltPressed = false;
// State handlers
private static readonly Dictionary<SequenceState, Action> stateHandlers = new Dictionary<SequenceState, Action> {
{ 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 // Simple list to track last 5 windows
private static List<int> lastWindows = new List<int>(); private static List<int> lastWindows = new List<int>();
@@ -272,20 +316,14 @@ namespace DD2Switcher {
private delegate IntPtr MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam); 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) { private static IntPtr MouseHookCallback(int nCode, IntPtr wParam, IntPtr lParam) {
if (nCode >= 0) { if (nCode >= 0) {
int wParamInt = (int)wParam; int wParamInt = (int)wParam;
if (wParamInt == WM_LBUTTONDOWN || wParamInt == WM_RBUTTONDOWN || wParamInt == WM_MBUTTONDOWN) { 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}"); 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 // Handle sequence mode event detection with KeyUp
if (CurrentState == SequenceState.WAITING) { if (CurrentState == SequenceState.WAITING_FOR_EVENT) {
// Ignore the sequence keybind itself // Ignore the sequence keybind itself
if (e != (int)SequenceKeybind) { if (e != (int)SequenceKeybind) {
Console.WriteLine($"Key up detected in state: {CurrentState} - Key: {e}"); Console.WriteLine($"Key up detected in state: {CurrentState} - Key: {e}");
@@ -636,6 +674,9 @@ namespace DD2Switcher {
} else { } else {
Console.WriteLine($"Ignoring sequence keybind release: {e}"); 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!"); Console.WriteLine("Sequence keybind detected!");
if (CurrentState != SequenceState.INACTIVE) { if (CurrentState != SequenceState.INACTIVE) {
Console.WriteLine("Advancing sequence step"); Console.WriteLine("Advancing sequence step");
NextSequenceStep(); HandleSequenceEvent();
} else { } else {
Console.WriteLine("Starting sequence mode"); Console.WriteLine("Starting sequence mode");
StartSequenceMode(); StartSequenceMode();