From 0e3f2005d11804f208c84dd38832f2f76635bf7d Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Sun, 31 Aug 2025 20:35:39 +0200 Subject: [PATCH] Fix the fucking first and last index selection logic --- DD2Switcher/Program.cs | 19 +++++++---- DD2Switcher/SettingsForm.cs | 67 +++++++++++++++++++++++-------------- 2 files changed, 54 insertions(+), 32 deletions(-) diff --git a/DD2Switcher/Program.cs b/DD2Switcher/Program.cs index a4ba468..5b430d5 100644 --- a/DD2Switcher/Program.cs +++ b/DD2Switcher/Program.cs @@ -19,20 +19,25 @@ namespace DD2Switcher { public static void UntrackWindow(int index) { if (index >= 0 && index < NumProc) { windows[index] = null; - // Compact the array by shifting non-null elements to the left - for (int i = index; i < NumProc - 1; i++) { - windows[i] = windows[i + 1]; - } - windows[NumProc - 1] = null; // Update ActiveIndex if needed if (ActiveIndex == index) { ActiveIndex = -1; - } else if (ActiveIndex > index) { - ActiveIndex--; + } + + // Update first/last indices if needed + if (FirstIndex == index) { + FirstIndex = -1; + } + if (LastIndex == index) { + LastIndex = -1; } } } + + // Static properties for first/last selection persistence + public static int FirstIndex { get; set; } = -1; + public static int LastIndex { get; set; } = -1; private static int ActiveIndex = -1; private static bool AltPressed = false; diff --git a/DD2Switcher/SettingsForm.cs b/DD2Switcher/SettingsForm.cs index 368be5b..ebbc631 100644 --- a/DD2Switcher/SettingsForm.cs +++ b/DD2Switcher/SettingsForm.cs @@ -8,8 +8,6 @@ using System.Windows.Forms; namespace DD2Switcher { public partial class SettingsForm : Form { private FlowLayoutPanel windowsPanel; - private int firstIndex = -1; - private int lastIndex = -1; public SettingsForm() { InitializeComponent(); @@ -79,39 +77,58 @@ namespace DD2Switcher { var windowPanelForm = new WindowPanelForm(); windowPanelForm.WindowIndex = i; windowPanelForm.WindowProcess = window; - windowPanelForm.IsFirst = (i == firstIndex); - windowPanelForm.IsLast = (i == lastIndex); + windowPanelForm.IsFirst = (i == Program.FirstIndex); + windowPanelForm.IsLast = (i == Program.LastIndex); windowPanelForm.UpdateDisplay(); windowPanelForm.PickClicked += (sender, index) => { - if (firstIndex == -1) { + if (Program.FirstIndex == -1) { // First pick - set both first and last - firstIndex = index; - lastIndex = index; - } else if (lastIndex == -1) { - // Second pick - set last - lastIndex = index; + Program.FirstIndex = index; + Program.LastIndex = index; + } else if (Program.LastIndex == -1) { + // Second pick - set last (must be >= first) + if (index >= Program.FirstIndex) { + Program.LastIndex = index; + } else { + Program.LastIndex = Program.FirstIndex; + Program.FirstIndex = index; + } } else { // Subsequent picks - determine which becomes first - if (index < firstIndex) { - // New index is lower, so it becomes first - lastIndex = firstIndex; - firstIndex = index; + int distanceToFirst = Math.Abs(index - Program.FirstIndex); + int distanceToLast = Math.Abs(index - Program.LastIndex); + + if (distanceToFirst <= distanceToLast) { + // New index is closer to first, so first moves + if (index <= Program.LastIndex) { + Program.FirstIndex = index; + } else { + Program.LastIndex = Program.FirstIndex; + Program.FirstIndex = index; + } } else { - // New index is higher, so it becomes last - lastIndex = index; + // New index is closer to last, so last moves + if (index >= Program.FirstIndex) { + Program.LastIndex = index; + } else { + Program.FirstIndex = Program.LastIndex; + Program.LastIndex = index; + } } } + + // Ensure last >= first + if (Program.LastIndex < Program.FirstIndex) { + int tmp = Program.LastIndex; + Program.LastIndex = Program.FirstIndex; + Program.FirstIndex = tmp; + } + RefreshWindowsList(); }; windowPanelForm.UntrackClicked += (sender, index) => { UntrackWindow(index); - if (index == firstIndex) { - firstIndex = -1; - } - if (index == lastIndex) { - lastIndex = -1; - } RefreshWindowsList(); }; @@ -120,11 +137,11 @@ namespace DD2Switcher { } private string GetFirstLastText(int index) { - if (index == firstIndex && index == lastIndex) { + if (index == Program.FirstIndex && index == Program.LastIndex) { return "First & Last"; - } else if (index == firstIndex) { + } else if (index == Program.FirstIndex) { return "First"; - } else if (index == lastIndex) { + } else if (index == Program.LastIndex) { return "Last"; } return "";