-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMainForm.cs
152 lines (138 loc) · 4.56 KB
/
MainForm.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
149
150
151
152
using System;
using System.Diagnostics;
using System.Drawing;
using System.Net;
using System.Windows.Forms;
using TruckRemoteServer.Setup;
namespace TruckRemoteServer
{
public partial class MainForm : Form, UDPServer.IStatusListener
{
private readonly UDPServer server;
public MainForm()
{
InitializeComponent();
int sensitivity = Properties.Settings.Default.Sensitivity;
int port = (int)Properties.Settings.Default.Port;
sensitivityTrackBar.Value = sensitivity;
labelSensitivity.Text = sensitivity.ToString();
PCController.SteeringSensitivity = sensitivity;
numericUpPort.Value = port;
server = new UDPServer(this, port);
}
protected override void OnShown(EventArgs e)
{
Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.AboveNormal;
ShowIpInLabel();
PluginInstaller installer = new PluginInstaller();
if (installer.Status == SetupStatus.Uninstalled)
{
OnStatusUpdate(false, false, false);
installer.Install(this);
server.Start();
} else
{
server.Start();
}
}
public void ShowIpInLabel()
{
IPAddress ip = NetworkUtil.GetLocalIp();
if (ip != null)
{
labelIp.Text = ip.ToString();
}
else
{
labelIp.Text = "Doesn't exist!";
}
}
public void OnStatusUpdate(bool isEnabled, bool controllerConnected, bool controllerPaused)
{
if (isEnabled)
{
SetButtonsIsListening(true);
if (controllerConnected)
{
if (controllerPaused)
{
ShowStatus("Controller paused", Color.ForestGreen);
}
else
{
ShowStatus("Controller active", Color.ForestGreen);
}
}
else
{
ShowStatus("Enabled", Color.ForestGreen);
}
}
else
{
SetButtonsIsListening(false);
ShowStatus("Disabled", Color.OrangeRed);
}
}
private void ShowStatus(string labelText, Color color)
{
try
{
labelStatus.BeginInvoke((MethodInvoker)delegate ()
{
labelStatus.Text = labelText;
labelStatus.ForeColor = color;
});
}
catch (Exception) { }
}
private void SetButtonsIsListening(bool isConnected)
{
try
{
buttonStop.BeginInvoke((MethodInvoker)delegate ()
{
buttonStop.Enabled = isConnected;
});
buttonStart.BeginInvoke((MethodInvoker)delegate ()
{
buttonStart.Enabled = !isConnected;
});
}
catch (Exception) { }
}
private void NumericUpPort_ValueChanged(object sender, EventArgs e)
{
if (numericUpPort.Text.Trim() == "" || numericUpPort.Text.Trim() == "0")
{
numericUpPort.ResetText();
Properties.Settings.Default.Port = 18250;
}
Properties.Settings.Default.Port = numericUpPort.Value;
Properties.Settings.Default.Save();
}
private void ButtonStop_Click(object sender, EventArgs e)
{
server.Shutdown();
buttonStop.Enabled = false;
buttonStart.Enabled = true;
}
private void ButtonStart_Click(object sender, EventArgs e)
{
server.port = (int)numericUpPort.Value;
server.Start();
}
private void SensitivityTrackBar_Scroll(object sender, EventArgs e)
{
PCController.SteeringSensitivity = sensitivityTrackBar.Value;
labelSensitivity.Text = (sensitivityTrackBar.Value).ToString();
Properties.Settings.Default.Sensitivity = sensitivityTrackBar.Value;
Properties.Settings.Default.Save();
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
server.Shutdown();
InputEmulator.ReleaseJoy();
}
}
}