refactor(Program.cs): simplify sequence state machine by removing PROCESSING state
This commit is contained in:
@@ -61,7 +61,6 @@ namespace DD2Switcher {
|
||||
}
|
||||
|
||||
if (sequenceFirstIndex >= 0 && sequenceLastIndex >= 0 && sequenceFirstIndex <= sequenceLastIndex) {
|
||||
CurrentState = SequenceState.PROCESSING;
|
||||
CurrentSequenceIndex = sequenceFirstIndex;
|
||||
Console.WriteLine($"Starting sequence mode, tabbing to index {CurrentSequenceIndex + 1}");
|
||||
TabTo(CurrentSequenceIndex + 1); // Tab to first window
|
||||
@@ -194,7 +193,7 @@ namespace DD2Switcher {
|
||||
public static int LastIndex { get; set; } = -1;
|
||||
|
||||
// Sequence mode state engine
|
||||
public enum SequenceState { INACTIVE, WAITING_FOR_EVENT, PROCESSING, WAITING_TO_ADVANCE }
|
||||
public enum SequenceState { INACTIVE, WAITING_FOR_EVENT, WAITING_TO_ADVANCE, ADVANCE }
|
||||
|
||||
public static SequenceState CurrentState { get; set; } = SequenceState.INACTIVE;
|
||||
public static int CurrentSequenceIndex { get; set; } = -1;
|
||||
@@ -207,8 +206,8 @@ namespace DD2Switcher {
|
||||
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 }
|
||||
{ SequenceState.WAITING_TO_ADVANCE, HandleWaitingToAdvance },
|
||||
{ SequenceState.ADVANCE, HandleAdvance }
|
||||
};
|
||||
|
||||
private static void HandleInactive() {
|
||||
@@ -216,37 +215,27 @@ namespace DD2Switcher {
|
||||
}
|
||||
|
||||
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
|
||||
// Event happened, transition to waiting to advance
|
||||
Console.WriteLine($"Event detected in state: {CurrentState}, transitioning to WAITING_TO_ADVANCE");
|
||||
CurrentState = SequenceState.WAITING_TO_ADVANCE;
|
||||
Console.WriteLine($"State changed to: {CurrentState}");
|
||||
}
|
||||
|
||||
private static void HandleWaitingToAdvance() {
|
||||
Console.WriteLine($"Advancing sequence from WAITING_TO_ADVANCE state");
|
||||
// Start timer to advance after N milliseconds
|
||||
Console.WriteLine($"Starting 500ms timer in WAITING_TO_ADVANCE state");
|
||||
Task.Delay(500).ContinueWith(_ => {
|
||||
if (CurrentState == SequenceState.WAITING_TO_ADVANCE) {
|
||||
CurrentState = SequenceState.ADVANCE;
|
||||
Console.WriteLine($"Timer expired, transitioning to ADVANCE state");
|
||||
HandleSequenceEvent();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static void HandleAdvance() {
|
||||
Console.WriteLine($"Advancing sequence from ADVANCE state");
|
||||
|
||||
CurrentState = SequenceState.PROCESSING;
|
||||
CurrentSequenceIndex++;
|
||||
Console.WriteLine($"Advanced to index {CurrentSequenceIndex}");
|
||||
|
||||
@@ -322,8 +311,7 @@ namespace DD2Switcher {
|
||||
if (wParamInt == WM_LBUTTONDOWN || wParamInt == WM_RBUTTONDOWN || wParamInt == WM_MBUTTONDOWN) {
|
||||
if (CurrentState == SequenceState.WAITING_FOR_EVENT) {
|
||||
Console.WriteLine($"Mouse click detected in state: {CurrentState}");
|
||||
CurrentState = SequenceState.WAITING_TO_ADVANCE;
|
||||
Console.WriteLine($"State changed to: {CurrentState}");
|
||||
HandleSequenceEvent();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user