-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
150 lines (136 loc) · 5.17 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using EMotion.Client;
using System.Collections.ObjectModel;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Media;
using HandyControl.Controls;
using System.Threading.Tasks;
using System;
using System.Diagnostics;
using System.Net;
using System.Linq;
namespace EMotion
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : System.Windows.Window
{
private ControllerManager? controllerManager;
private QuickConnectThread? quickConnectThread;
private ObservableCollection<Item> controllersSource;
public MainWindow()
{
InitializeComponent();
controllersSource = new ObservableCollection<Item>
{
new Item() {Name = "add"}
};
try
{
controllerManager = new ControllerManager();
quickConnectThread = new QuickConnectThread(this);
quickConnectThread.start();
}
catch (Exception ex)
{
HandyControl.Controls.MessageBox.Show("请先安装VigemBus!");
Process.Start("explorer", "https://github.com/nefarius/ViGEmBus/releases/tag/v1.22.0");
Environment.Exit(0);
}
controllers.ItemsSource = controllersSource;
}
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (controllers.SelectedIndex != -1)
{
if (((Item)controllers.SelectedItem).Name == "add")
{
controllersSource[0] = new Item() { Name = "input" };
}
controllers.UnselectAll();
}
}
private async void confirmInput(object sender, RoutedEventArgs e)
{
System.Windows.Controls.TextBox ipInput = (System.Windows.Controls.TextBox)VisualTreeHelper.GetChild(VisualTreeHelper.GetParent(VisualTreeHelper.GetParent((Button)sender)), 1);
if (System.Text.RegularExpressions.Regex.IsMatch(ipInput.Text, "((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})(\\.((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})){3}") && !containsController(ipInput.Text))
{
var d = Dialog.Show<WaitDialog>();
controllerManager!.createController(ipInput.Text);
int i = 10;
while (!controllerManager.getStatus(ipInput.Text).HasValue && i > 0)
{
await Task.Delay(1000);
i--;
}
if (controllerManager.getStatus(ipInput.Text) ?? false)
{
controllersSource[0] = new Item() { Name = "add"};
controllersSource.Add(new Item() { Name = "controller", Value = ipInput.Text});
}
else
{
controllerManager!.destoryController(ipInput.Text);
Dialog.Show(new RemindDialog("连接失败"));
}
d.Close();
}
else
{
ipInput.SetValue(BorderBrushProperty, Brushes.Red);
}
}
private bool containsController(string ip)
{
bool res = false;
foreach(Item i in controllersSource)
{
if (i.Name == "controller" && i.Value == ip) res = true;
}
return res;
}
private void cancelInput(object sender, RoutedEventArgs e)
{
controllersSource[0] = new Item() { Name = "add" };
controllers.ItemsSource = controllersSource;
}
public void showQuickConnect(string ip)
{
foreach (Item i in controllersSource)
{
if (i.Value == ip) return;
}
Application.Current.Dispatcher.Invoke(new Action(() =>
{
controllersSource.Add(new Item() { Name = "quick" , Value = ip });
}));
}
private void quickConnectConfirm(object sender, RoutedEventArgs e)
{
Button button = ((Button)sender);
quickConnectThread!.ackQC(new IPEndPoint(IPAddress.Parse(((Item)button.Tag).Value), QuickConnectThread.PORT));
confirmInput(sender, e);
cancelQuickConnect(sender, e);
}
private void cancelQuickConnect(object sender, RoutedEventArgs e)
{
Button button = ((Button)sender);
controllersSource.Remove((Item) button.Tag);
}
private void disconnectController(object sender, RoutedEventArgs e)
{
Button button = ((Button)sender);
controllerManager!.destoryController(((Item)button.Tag).Value);
controllersSource.Remove((Item) button.Tag);
}
private void switchGamepad(object sender, RoutedEventArgs e)
{
RadioButton gamepad = (RadioButton)sender;
if (controllerManager!.isDS4((string)gamepad.Tag) ^ gamepad.Content.Equals("DS4"))
{
controllerManager.switchGamepad((string)gamepad.Tag);
}
}
}
}