forked from alexChurkin/TruckRemoteServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainForm.cs
168 lines (142 loc) · 5.21 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using System;
using System.Net;
using System.Linq;
using System.Windows.Forms;
using System.Collections.Generic;
namespace TruckRemoteServer
{
public partial class MainForm : Form
{
public UDPServer server;
public MainForm()
{
InitializeComponent();
}
private void InitialSetup()
{
ShowIpInLabel();
decimal port = Properties.Settings.Default.Port;
numericUpPort.Value = port;
//Notify Icon
notifyIconTray.Visible = false;
notifyIconTray.ContextMenuStrip = contextMenuStripNotifyIconTray;
server = new UDPServer(labelStatus, buttonStartStopServer);
ControlMappingRW ControlMapping = new ControlMappingRW();
ControlMapping.LoadControlMapping();
server.port = (int)port;
if (Properties.Settings.Default.StartServerOnStartup)
{
buttonStartStopServer.Text = "Start";
server.Start();
}
if (Properties.Settings.Default.StartMinimized)
this.WindowState = FormWindowState.Minimized;
StartStopButtonProperties();
}
public void ShowIpInLabel()
{
IPHostEntry ipEntry = Dns.GetHostEntry(Dns.GetHostName());
IPAddress[] addr = ipEntry.AddressList;
string addr192String = string.Empty;
foreach (IPAddress address in addr)
{
string addressString = address.ToString();
if (addressString.StartsWith("192"))
{
addr192String = addressString;
break;
}
}
if (addr192String != string.Empty)
{
labelIp.Text = addr192String;
}
else
{
labelIp.Text = addr[0].ToString();
}
}
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();
}
internal void ButtonStop_Click(object sender, EventArgs e)
{
server.Stop();
}
internal void ButtonStart_Click(object sender, EventArgs e)
{
server.port = (int) numericUpPort.Value;
server.Start();
}
internal void StartStopButtonProperties()
{
if (server.enabled && buttonStartStopServer.Text == "Start")
{
buttonStartStopServer.Text = "Stop";
buttonStartStopServer.Click -= ButtonStart_Click;
buttonStartStopServer.Click += ButtonStop_Click;
StartStopServerToolStripMenuItem.Text = "Stop Server";
StartStopServerToolStripMenuItem.Click -= ButtonStart_Click;
StartStopServerToolStripMenuItem.Click += ButtonStop_Click;
}
else if (!server.enabled && buttonStartStopServer.Text == "Stop")
{
buttonStartStopServer.Text = "Start";
buttonStartStopServer.Click -= ButtonStop_Click;
buttonStartStopServer.Click += ButtonStart_Click;
StartStopServerToolStripMenuItem.Text = "Start Server";
StartStopServerToolStripMenuItem.Click -= ButtonStop_Click;
StartStopServerToolStripMenuItem.Click += ButtonStart_Click;
}
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
server.Stop();
InputEmulator.ReleaseJoy();
}
private void controlMappingToolStripMenuItem_Click(object sender, EventArgs e)
{
ControlMappingForm controlMappingForm = new ControlMappingForm();
controlMappingForm.ShowDialog();
}
private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
{
ProgramSettingsForm Form = new ProgramSettingsForm();
Form.ShowDialog();
}
private void MainForm_Load(object sender, EventArgs e)
{
InitialSetup();
server.UpdateUiState();
}
private void MainForm_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized && Properties.Settings.Default.MinimizeToTray)
{
Hide();
notifyIconTray.Visible = true;
this.ShowInTaskbar = false;
}
}
private void notifyIconTray_Click(object sender, EventArgs e)
{
}
private void notifyIconTray_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Show();
this.WindowState = FormWindowState.Normal;
notifyIconTray.Visible = false;
this.ShowInTaskbar = true;
}
}
}
}