Files
multiboxer/DD2Switcher/SettingsForm.cs

274 lines
12 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
namespace DD2Switcher {
public partial class SettingsForm : Form {
private TabControl tabControl;
private TabPage windowsTab;
private TabPage sequenceTab;
private FlowLayoutPanel windowsPanel;
private Label sequenceKeybindLabel;
private TextBox sequenceKeybindTextBox;
public SettingsForm() {
InitializeComponent();
LoadIcon();
RefreshWindowsList();
}
private void InitializeComponent() {
this.tabControl = new System.Windows.Forms.TabControl();
this.windowsTab = new System.Windows.Forms.TabPage();
this.windowsPanel = new System.Windows.Forms.FlowLayoutPanel();
this.sequenceTab = new System.Windows.Forms.TabPage();
this.sequenceKeybindTextBox = new System.Windows.Forms.TextBox();
this.sequenceKeybindLabel = new System.Windows.Forms.Label();
this.tabControl.SuspendLayout();
this.windowsTab.SuspendLayout();
this.sequenceTab.SuspendLayout();
this.SuspendLayout();
//
// tabControl
//
this.tabControl.Controls.Add(this.windowsTab);
this.tabControl.Controls.Add(this.sequenceTab);
this.tabControl.Location = new System.Drawing.Point(12, 12);
this.tabControl.Name = "tabControl";
this.tabControl.SelectedIndex = 0;
this.tabControl.Size = new System.Drawing.Size(576, 396);
this.tabControl.TabIndex = 0;
//
// windowsTab
//
this.windowsTab.Controls.Add(this.windowsPanel);
this.windowsTab.Location = new System.Drawing.Point(4, 22);
this.windowsTab.Name = "windowsTab";
this.windowsTab.Padding = new System.Windows.Forms.Padding(3);
this.windowsTab.Size = new System.Drawing.Size(568, 370);
this.windowsTab.TabIndex = 0;
this.windowsTab.Text = "Windows";
this.windowsTab.UseVisualStyleBackColor = true;
//
// windowsPanel
//
this.windowsPanel.AutoScroll = true;
this.windowsPanel.BackColor = System.Drawing.Color.White;
this.windowsPanel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.windowsPanel.Location = new System.Drawing.Point(6, 6);
this.windowsPanel.Name = "windowsPanel";
this.windowsPanel.Padding = new System.Windows.Forms.Padding(10);
this.windowsPanel.Size = new System.Drawing.Size(556, 358);
this.windowsPanel.TabIndex = 0;
//
// sequenceTab
//
this.sequenceTab.Controls.Add(this.sequenceKeybindTextBox);
this.sequenceTab.Controls.Add(this.sequenceKeybindLabel);
this.sequenceTab.Location = new System.Drawing.Point(4, 22);
this.sequenceTab.Name = "sequenceTab";
this.sequenceTab.Padding = new System.Windows.Forms.Padding(3);
this.sequenceTab.Size = new System.Drawing.Size(568, 370);
this.sequenceTab.TabIndex = 1;
this.sequenceTab.Text = "Sequence";
this.sequenceTab.UseVisualStyleBackColor = true;
//
// sequenceKeybindTextBox
//
this.sequenceKeybindTextBox.Location = new System.Drawing.Point(126, 17);
this.sequenceKeybindTextBox.Name = "sequenceKeybindTextBox";
this.sequenceKeybindTextBox.Size = new System.Drawing.Size(111, 20);
this.sequenceKeybindTextBox.TabIndex = 1;
this.sequenceKeybindTextBox.Text = Program.SequenceKeybind.ToString();
this.sequenceKeybindTextBox.Leave += new System.EventHandler(this.sequenceKeybindTextBox_Leave);
//
// sequenceKeybindLabel
//
this.sequenceKeybindLabel.AutoSize = true;
this.sequenceKeybindLabel.Location = new System.Drawing.Point(20, 20);
this.sequenceKeybindLabel.Name = "sequenceKeybindLabel";
this.sequenceKeybindLabel.Size = new System.Drawing.Size(100, 13);
this.sequenceKeybindLabel.TabIndex = 0;
this.sequenceKeybindLabel.Text = "Sequence Keybind:";
//
// SettingsForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(600, 420);
this.Controls.Add(this.tabControl);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "SettingsForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "DD2Switcher Settings";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.SettingsForm_FormClosing);
this.tabControl.ResumeLayout(false);
this.windowsTab.ResumeLayout(false);
this.sequenceTab.ResumeLayout(false);
this.sequenceTab.PerformLayout();
this.ResumeLayout(false);
}
private void closeButton_Click(object sender, EventArgs e) {
this.Close();
}
private void refreshButton_Click(object sender, EventArgs e) {
RefreshWindowsList();
}
private void RefreshWindowsList() {
windowsPanel.Controls.Clear();
// Get tracked windows from Program
var trackedWindows = GetTrackedWindows();
if (trackedWindows.Count == 0) {
var noWindowsLabel = new Label();
noWindowsLabel.Text = "No windows currently tracked";
noWindowsLabel.AutoSize = true;
noWindowsLabel.ForeColor = Color.Gray;
noWindowsLabel.Font = new Font("Segoe UI", 10F);
windowsPanel.Controls.Add(noWindowsLabel);
return;
}
for (int i = 0; i < trackedWindows.Count; i++) {
var window = trackedWindows[i];
if (window == null)
continue;
var windowPanelForm = new WindowPanelForm();
windowPanelForm.WindowIndex = i;
windowPanelForm.WindowProcess = window;
windowPanelForm.IsFirst = (i == Program.FirstIndex);
windowPanelForm.IsLast = (i == Program.LastIndex);
windowPanelForm.UpdateDisplay();
windowPanelForm.PickClicked += (sender, index) => {
// 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) {
Program.LastIndex = index;
} else {
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);
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 closer to last, so last moves
if (index >= Program.FirstIndex) {
Program.LastIndex = index;
} else {
Program.FirstIndex = Program.LastIndex;
Program.LastIndex = index;
}
}
Console.WriteLine($"Updated indices: First={Program.FirstIndex}, Last={Program.LastIndex}");
}
// 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);
RefreshWindowsList();
};
windowsPanel.Controls.Add(windowPanelForm);
}
}
private string GetFirstLastText(int index) {
if (index == Program.FirstIndex && index == Program.LastIndex) {
return "First & Last";
} else if (index == Program.FirstIndex) {
return "First";
} else if (index == Program.LastIndex) {
return "Last";
}
return "";
}
private List<Process> GetTrackedWindows() {
var windows = Program.GetTrackedWindows();
return windows.ToList();
}
private void UntrackWindow(int index) {
Program.UntrackWindow(index);
}
private void LoadIcon() {
try {
this.Icon = new Icon(System.IO.Path.Combine(
System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location),
"app.ico"));
} catch {
// Use default icon if custom icon not found
}
}
private void sequenceKeybindTextBox_Leave(object sender, EventArgs e) {
Console.WriteLine($"sequenceKeybindTextBox_Leave called with text: {sequenceKeybindTextBox.Text}");
try {
KeysConverter converter = new KeysConverter();
Keys newKey = (Keys)converter.ConvertFromString(sequenceKeybindTextBox.Text);
Console.WriteLine($"Converted key: {newKey} (code: {(int)newKey})");
Program.UpdateSequenceHotkey(newKey);
Console.WriteLine("Keybind updated successfully!");
} catch (Exception ex) {
Console.WriteLine($"Error converting key: {ex.Message}");
sequenceKeybindTextBox.Text = Program.SequenceKeybind.ToString();
}
}
private void SettingsForm_FormClosing(object sender, FormClosingEventArgs e) {
Console.WriteLine("SettingsForm closing - saving keybind");
try {
KeysConverter converter = new KeysConverter();
Keys newKey = (Keys)converter.ConvertFromString(sequenceKeybindTextBox.Text);
Console.WriteLine($"Saving keybind on close: {newKey}");
Program.UpdateSequenceHotkey(newKey);
} catch (Exception ex) {
Console.WriteLine($"Error saving keybind on close: {ex.Message}");
}
}
}
}