1 //The MIT License (MIT)
2 //
3 //Copyright (c) 2017 Rock_On, 2018 The openBVE Project
4 //
5 //Permission is hereby granted, free of charge, to any person obtaining a copy of
6 //this software and associated documentation files (the "Software"), to deal in
7 //the Software without restriction, including without limitation the rights to
8 //use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 //the Software, and to permit persons to whom the Software is furnished to do so,
10 //subject to the following conditions:
11 //
12 //The above copyright notice and this permission notice shall be included in all
13 //copies or substantial portions of the Software.
14 //
15 //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 //FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 //COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 //IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 //CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 
22 using System;
23 using System.Collections.Generic;
24 using System.IO;
25 using System.Windows.Forms;
26 using System.Xml.Serialization;
27 
28 namespace SanYingInput
29 {
30 
31 	public partial class ConfigForm : Form
32 	{
33 
34 		public struct ConfigFormSaveData
35 		{
36 			public Guid guid;
37 
38 			public int switchS;
39 			public int switchA1;
40 			public int switchA2;
41 			public int switchB1;
42 			public int switchB2;
43 			public int switchC1;
44 			public int switchC2;
45 			public int switchD;
46 			public int switchE;
47 			public int switchF;
48 			public int switchG;
49 			public int switchH;
50 			public int switchI;
51 			public int switchJ;
52 			public int switchK;
53 			public int switchL;
54 			public int switchReverserFront;
55 			public int switchReverserNeutral;
56 			public int switchReverserBack;
57 			public int switchHorn1;
58 			public int switchHorn2;
59 			public int switchMusicHorn;
60 			public int switchConstSpeed;
61 		};
62 
63 		public enum AxisType
64 		{
65 			axisNothing = 0,
66 			axisX = 1,
67 			axisY,
68 			axisZ,
69 			axisRx,
70 			axisRy,
71 			axisRz,
72 		}
73 
74 		private ConfigFormSaveData m_saveData;
75 		private const string FileName = "SanYingInput.xml";
76 
77 		private string m_configFilePath;
78 
79 		private int _notchPosition = 0;
80 		private int _reverserPosition = 0;
81 
82 		public ConfigFormSaveData Configuration
83 		{
84 			get
85 			{
86 				return m_saveData;
87 			}
88 		}
89 
loadConfigurationFile(string path)90 		public void loadConfigurationFile(string path)
91 		{
92 			m_configFilePath = path;
93 
94 			try
95 			{
96 				XmlSerializer serializer = new XmlSerializer(typeof(ConfigFormSaveData));
97 				FileStream fs = new FileStream(Path.Combine(path, FileName), FileMode.Open);
98 				m_saveData = (ConfigFormSaveData)serializer.Deserialize(fs);
99 				fs.Close();
100 			}
101 			catch
102 			{
103 			}
104 		}
105 
saveConfigurationFile(string path)106 		public void saveConfigurationFile(string path)
107 		{
108 			if (!Directory.Exists(path))
109 			{
110 				Directory.CreateDirectory(path);
111 			}
112 
113 			path = Path.Combine(path, FileName);
114 
115 			XmlSerializer serializer = new XmlSerializer(typeof(ConfigFormSaveData));
116 			FileStream fs = new FileStream(path, FileMode.Create);
117 
118 			serializer.Serialize(fs, m_saveData);
119 			fs.Close();
120 		}
121 
ConfigForm()122 		public ConfigForm()
123 		{
124 			InitializeComponent();
125 
126 			List<string> axisArray = new List<string>();
127 
128 			axisArray.Add("OFF");
129 			axisArray.Add("X");
130 			axisArray.Add("Y");
131 			axisArray.Add("Z");
132 			axisArray.Add("Rx");
133 			axisArray.Add("Ry");
134 			axisArray.Add("Rz");
135 
136 			m_saveData = new ConfigFormSaveData();
137 
138 			m_saveData.switchS = fromSwitchString("OFF");
139 			m_saveData.switchA1 = fromSwitchString("OFF");
140 			m_saveData.switchA2 = fromSwitchString("OFF");
141 			m_saveData.switchB1 = fromSwitchString("OFF");
142 			m_saveData.switchB2 = fromSwitchString("OFF");
143 			m_saveData.switchC1 = fromSwitchString("OFF");
144 			m_saveData.switchC2 = fromSwitchString("OFF");
145 			m_saveData.switchD = fromSwitchString("OFF");
146 			m_saveData.switchE = fromSwitchString("OFF");
147 			m_saveData.switchF = fromSwitchString("OFF");
148 			m_saveData.switchG = fromSwitchString("OFF");
149 			m_saveData.switchH = fromSwitchString("OFF");
150 			m_saveData.switchI = fromSwitchString("OFF");
151 			m_saveData.switchJ = fromSwitchString("OFF");
152 			m_saveData.switchK = fromSwitchString("OFF");
153 			m_saveData.switchL = fromSwitchString("OFF");
154 			m_saveData.switchReverserFront = fromSwitchString("OFF");
155 			m_saveData.switchReverserNeutral = fromSwitchString("OFF");
156 			m_saveData.switchReverserBack = fromSwitchString("OFF");
157 			m_saveData.switchHorn1 = fromSwitchString("OFF");
158 			m_saveData.switchHorn2 = fromSwitchString("OFF");
159 			m_saveData.switchMusicHorn = fromSwitchString("OFF");
160 			m_saveData.switchConstSpeed = fromSwitchString("OFF");
161 		}
162 
toSwitchString(int n)163 		private string toSwitchString(int n)
164 		{
165 			switch (n)
166 			{
167 				case -1:
168 					return "OFF";
169 				default:
170 					return toString(n);
171 			}
172 		}
173 
fromSwitchString(string s)174 		private int fromSwitchString(string s)
175 		{
176 			switch (s)
177 			{
178 				case "OFF":
179 					return -1;
180 				default:
181 					return fromString(s);
182 			}
183 		}
184 
restoreConfiguration(ConfigFormSaveData saveData)185 		private void restoreConfiguration(ConfigFormSaveData saveData)
186 		{
187 			txtSwS.Text = toSwitchString(saveData.switchS);
188 			txtSwA1.Text = toSwitchString(saveData.switchA1);
189 			txtSwA2.Text = toSwitchString(saveData.switchA2);
190 			txtSwB1.Text = toSwitchString(saveData.switchB1);
191 			txtSwB2.Text = toSwitchString(saveData.switchB2);
192 			txtSwC1.Text = toSwitchString(saveData.switchC1);
193 			txtSwC2.Text = toSwitchString(saveData.switchC2);
194 			txtSwD.Text = toSwitchString(saveData.switchD);
195 			txtSwE.Text = toSwitchString(saveData.switchE);
196 			txtSwF.Text = toSwitchString(saveData.switchF);
197 			txtSwG.Text = toSwitchString(saveData.switchG);
198 			txtSwH.Text = toSwitchString(saveData.switchH);
199 			txtSwI.Text = toSwitchString(saveData.switchI);
200 			txtSwJ.Text = toSwitchString(saveData.switchJ);
201 			txtSwK.Text = toSwitchString(saveData.switchK);
202 			txtSwL.Text = toSwitchString(saveData.switchL);
203 			txtSwReverserFront.Text = toSwitchString(saveData.switchReverserFront);
204 			txtSwReverserNeutral.Text = toSwitchString(saveData.switchReverserNeutral);
205 			txtSwReverserBack.Text = toSwitchString(saveData.switchReverserBack);
206 			txtSwHorn1.Text = toSwitchString(saveData.switchHorn1);
207 			txtSwHorn2.Text = toSwitchString(saveData.switchHorn2);
208 			txtSwMusicHorn.Text = toSwitchString(saveData.switchMusicHorn);
209 			txtSwConstSpeed.Text = toSwitchString(saveData.switchConstSpeed);
210 		}
211 
saveConfiguration()212 		private ConfigFormSaveData saveConfiguration()
213 		{
214 			ConfigFormSaveData saveData = new ConfigFormSaveData();
215 
216 			if (JoystickApi.currentDevice != -1)
217 			{
218 				saveData.guid = JoystickApi.GetGuid();
219 			}
220 
221 			saveData.switchS = fromSwitchString(txtSwS.Text);
222 			saveData.switchA1 = fromSwitchString(txtSwA1.Text);
223 			saveData.switchA2 = fromSwitchString(txtSwA2.Text);
224 			saveData.switchB1 = fromSwitchString(txtSwB1.Text);
225 			saveData.switchB2 = fromSwitchString(txtSwB2.Text);
226 			saveData.switchC1 = fromSwitchString(txtSwC1.Text);
227 			saveData.switchC2 = fromSwitchString(txtSwC2.Text);
228 			saveData.switchD = fromSwitchString(txtSwD.Text);
229 			saveData.switchE = fromSwitchString(txtSwE.Text);
230 			saveData.switchF = fromSwitchString(txtSwF.Text);
231 			saveData.switchG = fromSwitchString(txtSwG.Text);
232 			saveData.switchH = fromSwitchString(txtSwH.Text);
233 			saveData.switchI = fromSwitchString(txtSwI.Text);
234 			saveData.switchJ = fromSwitchString(txtSwJ.Text);
235 			saveData.switchK = fromSwitchString(txtSwK.Text);
236 			saveData.switchL = fromSwitchString(txtSwL.Text);
237 			saveData.switchReverserFront = fromSwitchString(txtSwReverserFront.Text);
238 			saveData.switchReverserNeutral = fromSwitchString(txtSwReverserNeutral.Text);
239 			saveData.switchReverserBack = fromSwitchString(txtSwReverserBack.Text);
240 			saveData.switchHorn1 = fromSwitchString(txtSwHorn1.Text);
241 			saveData.switchHorn2 = fromSwitchString(txtSwHorn2.Text);
242 			saveData.switchMusicHorn = fromSwitchString(txtSwMusicHorn.Text);
243 			saveData.switchConstSpeed = fromSwitchString(txtSwConstSpeed.Text);
244 
245 			return saveData;
246 		}
247 
enumerateDevices()248 		public void enumerateDevices()
249 		{
250 			JoystickApi.EnumerateJoystick();
251 			int joyNum = JoystickManager.AttachedJoysticks.Length;
252 
253 			cmbJoySelect.Items.Clear();
254 
255 			if (joyNum == 0) return;
256 
257 			cmbJoySelect.MaxDropDownItems = joyNum;
258 
259 			for (int i = 0; i < joyNum; ++i)
260 			{
261 				cmbJoySelect.Items.Add(JoystickManager.AttachedJoysticks[i].Name);
262 
263 				if (m_saveData.guid == JoystickManager.AttachedJoysticks[i].Guid)
264 				{
265 					JoystickApi.SelectJoystick(i);
266 					cmbJoySelect.SelectedIndex = i;
267 				}
268 			}
269 
270 			if (cmbJoySelect.SelectedIndex == -1)
271 			{
272 				cmbJoySelect.SelectedIndex = joyNum - 1;
273 			}
274 		}
275 
toString(int n)276 		private string toString(int n)
277 		{
278 			return (n + 1).ToString();
279 		}
280 
fromString(string s)281 		private int fromString(string s)
282 		{
283 			int n;
284 
285 			try
286 			{
287 				n = int.Parse(s);
288 			}
289 			catch
290 			{
291 				n = 0;
292 			}
293 
294 			return n - 1;
295 		}
296 
ConfigForm_Load(object sender, EventArgs e)297 		private void ConfigForm_Load(object sender, EventArgs e)
298 		{
299 		}
300 
btnSave_Click(object sender, EventArgs e)301 		private void btnSave_Click(object sender, EventArgs e)
302 		{
303 			m_saveData = saveConfiguration();
304 			saveConfigurationFile(m_configFilePath);
305 			this.Close();
306 		}
307 
btnClose_Click(object sender, EventArgs e)308 		private void btnClose_Click(object sender, EventArgs e)
309 		{
310 			this.Close();
311 		}
312 
configurateSwitch()313 		private void configurateSwitch()
314 		{
315 			int buttonNum = 6;
316 
317 			var currentButtonState = JoystickApi.GetButtonsState();
318 
319 			if (currentButtonState.Count < buttonNum)
320 				return;
321 
322 			if (JoystickApi.lastButtonState.Count < buttonNum)
323 				return;
324 
325 			for (int i = 0; i < buttonNum; ++i)
326 			{
327 				if (currentButtonState[i] != JoystickApi.lastButtonState[i])
328 				{
329 					if (txtSwS.Focused)
330 					{
331 						txtSwS.Text = toSwitchString(i);
332 					}
333 					else if (txtSwA1.Focused)
334 					{
335 						txtSwA1.Text = toSwitchString(i);
336 					}
337 					else if (txtSwA2.Focused)
338 					{
339 						txtSwA2.Text = toSwitchString(i);
340 					}
341 					else if (txtSwB1.Focused)
342 					{
343 						txtSwB1.Text = toSwitchString(i);
344 					}
345 					else if (txtSwB2.Focused)
346 					{
347 						txtSwB2.Text = toSwitchString(i);
348 					}
349 					else if (txtSwC1.Focused)
350 					{
351 						txtSwC1.Text = toSwitchString(i);
352 					}
353 					else if (txtSwC2.Focused)
354 					{
355 						txtSwC2.Text = toSwitchString(i);
356 					}
357 					else if (txtSwD.Focused)
358 					{
359 						txtSwD.Text = toSwitchString(i);
360 					}
361 					else if (txtSwE.Focused)
362 					{
363 						txtSwE.Text = toSwitchString(i);
364 					}
365 					else if (txtSwF.Focused)
366 					{
367 						txtSwF.Text = toSwitchString(i);
368 					}
369 					else if (txtSwG.Focused)
370 					{
371 						txtSwG.Text = toSwitchString(i);
372 					}
373 					else if (txtSwH.Focused)
374 					{
375 						txtSwH.Text = toSwitchString(i);
376 					}
377 					else if (txtSwI.Focused)
378 					{
379 						txtSwI.Text = toSwitchString(i);
380 					}
381 					else if (txtSwJ.Focused)
382 					{
383 						txtSwJ.Text = toSwitchString(i);
384 					}
385 					else if (txtSwK.Focused)
386 					{
387 						txtSwK.Text = toSwitchString(i);
388 					}
389 					else if (txtSwL.Focused)
390 					{
391 						txtSwL.Text = toSwitchString(i);
392 					}
393 					else if (txtSwReverserFront.Focused)
394 					{
395 						txtSwReverserFront.Text = toSwitchString(i);
396 					}
397 					else if (txtSwReverserNeutral.Focused)
398 					{
399 						txtSwReverserNeutral.Text = toSwitchString(i);
400 					}
401 					else if (txtSwReverserBack.Focused)
402 					{
403 						txtSwReverserBack.Text = toSwitchString(i);
404 					}
405 					else if (txtSwHorn1.Focused)
406 					{
407 						txtSwHorn1.Text = toSwitchString(i);
408 					}
409 					else if (txtSwHorn2.Focused)
410 					{
411 						txtSwHorn2.Text = toSwitchString(i);
412 					}
413 					else if (txtSwMusicHorn.Focused)
414 					{
415 						txtSwMusicHorn.Text = toSwitchString(i);
416 					}
417 					else if (txtSwConstSpeed.Focused)
418 					{
419 						txtSwConstSpeed.Text = toSwitchString(i);
420 					}
421 
422 					break;
423 				}
424 			}
425 		}
426 
timer1_Tick(object sender, EventArgs e)427 		private void timer1_Tick(object sender, EventArgs e)
428 		{
429 			JoystickApi.Update();
430 
431 			if (JoystickApi.currentDevice != -1)
432 			{
433 				var buttonsState = JoystickApi.GetButtonsState();
434 				var axises = JoystickApi.GetAxises();
435 
436 				int lastNotchPosition = _notchPosition;
437 				if (InputTranslator.TranslateNotchPosition(buttonsState, out _notchPosition))
438 				{
439 					_notchPosition = lastNotchPosition;
440 				}
441 				InputTranslator.TranslateReverserPosition(axises, out _reverserPosition);
442 
443 				{
444 					uint notchButtonsState;
445 
446 					InputTranslator.MakeBitFromNotchButtons(buttonsState, out notchButtonsState);
447 
448 					txtInfoBt7.Text = ((notchButtonsState & (uint)InputTranslator.BT7_10_Pressed.BT7) != 0) ? "1" : "0";
449 					txtInfoBt8.Text = ((notchButtonsState & (uint)InputTranslator.BT7_10_Pressed.BT8) != 0) ? "1" : "0";
450 					txtInfoBt9.Text = ((notchButtonsState & (uint)InputTranslator.BT7_10_Pressed.BT9) != 0) ? "1" : "0";
451 					txtInfoBt10.Text = ((notchButtonsState & (uint)InputTranslator.BT7_10_Pressed.BT10) != 0) ? "1" : "0";
452 				}
453 
454 				{
455 					if (_reverserPosition > 0)
456 					{
457 						txtInfoUp.Text = "1";
458 						txtInfoDown.Text = "0";
459 					}
460 					else if (_reverserPosition < 0)
461 					{
462 						txtInfoUp.Text = "0";
463 						txtInfoDown.Text = "1";
464 					}
465 					else
466 					{
467 						txtInfoUp.Text = "0";
468 						txtInfoDown.Text = "0";
469 					}
470 				}
471 
472 				string notchPositionString;
473 				string reverserPositionString;
474 
475 				if (_notchPosition > 0)
476 				{
477 					notchPositionString = string.Format("P{0}", _notchPosition);
478 				}
479 				else if (_notchPosition < 0)
480 				{
481 					if (_notchPosition > -9)
482 					{
483 						notchPositionString = string.Format("B{0}", _notchPosition);
484 					}
485 					else
486 					{
487 						notchPositionString = "EB";
488 					}
489 				}
490 				else
491 				{
492 					notchPositionString = "N";
493 				}
494 
495 				if (_reverserPosition > 0)
496 				{
497 					reverserPositionString = "F";
498 				}
499 				else if (_reverserPosition < 0)
500 				{
501 					reverserPositionString = "B";
502 				}
503 				else
504 				{
505 					reverserPositionString = "N";
506 				}
507 
508 				labelTch.Text = notchPositionString;
509 				labelReverser.Text = reverserPositionString;
510 
511 				configurateSwitch();
512 			}
513 			else
514 			{
515 				enumerateDevices();
516 			}
517 		}
518 
ConfigForm_FormClosed(object sender, FormClosedEventArgs e)519 		private void ConfigForm_FormClosed(object sender, FormClosedEventArgs e)
520 		{
521 			timer1.Enabled = false;
522 		}
523 
ConfigForm_Shown(object sender, EventArgs e)524 		private void ConfigForm_Shown(object sender, EventArgs e)
525 		{
526 			timer1.Enabled = true;
527 
528 			enumerateDevices();
529 			restoreConfiguration(m_saveData);
530 		}
531 
cmbJoySelect_SelectedIndexChanged(object sender, EventArgs e)532 		private void cmbJoySelect_SelectedIndexChanged(object sender, EventArgs e)
533 		{
534 			if (cmbJoySelect.SelectedIndex != -1)
535 			{
536 				JoystickApi.SelectJoystick(cmbJoySelect.SelectedIndex);
537 			}
538 		}
539 
deconfigurateSwitch(object sender, KeyEventArgs e)540 		private void deconfigurateSwitch(object sender, KeyEventArgs e)
541 		{
542 			TextBox me = (TextBox)sender;
543 
544 			if (e.KeyCode == Keys.Delete)
545 			{
546 				me.Text = "OFF";
547 			}
548 		}
549 	}
550 }
551