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.Collections.Generic;
23 using OpenTK.Input;
24 
25 namespace SanYingInput
26 {
27 	/// <summary>
28 	/// The class which converts input of the joy-stick
29 	/// </summary>
30 	internal static class InputTranslator
31 	{
32 		/// <summary>
33 		/// Bit flag enumeration type to express each button state of BT7-10
34 		/// </summary>
35 		internal enum BT7_10_Pressed
36 		{
37 			/// <summary>
38 			/// The state that none of buttons are pressed
39 			/// </summary>
40 			None = 0x0,
41 
42 			/// <summary>
43 			/// The state that BT7 is pressed
44 			/// </summary>
45 			BT7 = 0x8,
46 
47 			/// <summary>
48 			/// The state that BT8 is pressed
49 			/// </summary>
50 			BT8 = 0x4,
51 
52 			/// <summary>
53 			/// The state that BT9 is pressed
54 			/// </summary>
55 			BT9 = 0x2,
56 
57 			/// <summary>
58 			/// The state that BT10 is pressed
59 			/// </summary>
60 			BT10 = 0x1
61 		};
62 
63 		/// <summary>
64 		/// Dictionary that stores the relations with the position of the notch and bit flag.
65 		/// </summary>
66 		private static readonly Dictionary<uint, int> _bitPatternMap = new Dictionary<uint, int>();
67 
68 		/// <summary>
69 		/// Static constructer
70 		/// </summary>
InputTranslator()71 		static InputTranslator()
72 		{
73 			_bitPatternMap.Add(0xF, 5);
74 			_bitPatternMap.Add(0xE, 4);
75 			_bitPatternMap.Add(0xD, 3);
76 			_bitPatternMap.Add(0xC, 2);
77 			_bitPatternMap.Add(0xB, 1);
78 
79 			_bitPatternMap.Add(0xA, 0);
80 
81 			_bitPatternMap.Add(0x9, -1);
82 			_bitPatternMap.Add(0x8, -2);
83 			_bitPatternMap.Add(0x7, -3);
84 			_bitPatternMap.Add(0x6, -4);
85 			_bitPatternMap.Add(0x5, -5);
86 			_bitPatternMap.Add(0x4, -6);
87 			_bitPatternMap.Add(0x3, -7);
88 			_bitPatternMap.Add(0x2, -8);
89 			_bitPatternMap.Add(0x1, -9);
90 		}
91 
92 		/// <summary>
93 		/// Function that convert into the position of the notch from the state of the button of the joy-stick.
94 		/// </summary>
95 		/// <param name="buttonsState">State of the button of the joy-stick</param>
96 		/// <param name="notchPosition">Position of the notch</param>
97 		/// <returns>Middle position or not</returns>
TranslateNotchPosition(List<ButtonState> buttonsState, out int notchPosition)98 		public static bool TranslateNotchPosition(List<ButtonState> buttonsState, out int notchPosition)
99 		{
100 			uint notchButtonsState;
101 			MakeBitFromNotchButtons(buttonsState, out notchButtonsState);
102 
103 			if (_bitPatternMap.ContainsKey(notchButtonsState))
104 			{
105 				notchPosition = _bitPatternMap[notchButtonsState];
106 				return false;
107 			}
108 			else
109 			{
110 				notchPosition = 0;
111 				return true;
112 			}
113 		}
114 
115 		/// <summary>
116 		/// Function that makes a bit flag expressing the state of the button about notch from the state of the button of the joy-stick.
117 		/// </summary>
118 		/// <param name="buttonsState">State of the button of the joy-stick</param>
119 		/// <param name="notchButtonsState">The bit flag which expresses the state of the button about notch</param>
MakeBitFromNotchButtons(List<ButtonState> buttonsState, out uint notchButtonsState)120 		public static void MakeBitFromNotchButtons(List<ButtonState> buttonsState, out uint notchButtonsState)
121 		{
122 			notchButtonsState = (uint)BT7_10_Pressed.None;
123 
124 			if (buttonsState.Count < 10)
125 			{
126 				return;
127 			}
128 
129 			if (buttonsState[6] == ButtonState.Pressed)
130 			{
131 				notchButtonsState |= (uint)BT7_10_Pressed.BT7;
132 			}
133 			if (buttonsState[7] == ButtonState.Pressed)
134 			{
135 				notchButtonsState |= (uint)BT7_10_Pressed.BT8;
136 			}
137 			if (buttonsState[8] == ButtonState.Pressed)
138 			{
139 				notchButtonsState |= (uint)BT7_10_Pressed.BT9;
140 			}
141 			if (buttonsState[9] == ButtonState.Pressed)
142 			{
143 				notchButtonsState |= (uint)BT7_10_Pressed.BT10;
144 			}
145 		}
146 
147 		/// <summary>
148 		/// Funtction that convert into the position of reverser from state of axial of the joy-stick.
149 		/// </summary>
150 		/// <param name="axises">State of axial of the joy-stick</param>
151 		/// <param name="reverserPosition">Position of reverser</param>
TranslateReverserPosition(List<double> axises, out int reverserPosition)152 		public static void TranslateReverserPosition(List<double> axises, out int reverserPosition)
153 		{
154 			reverserPosition = 0;
155 
156 			if (axises.Count < 2)
157 			{
158 				return;
159 			}
160 
161 			if (axises[1] > 0.5)
162 			{
163 				reverserPosition = -1;
164 			}
165 			else if (axises[1] < -0.5)
166 			{
167 				reverserPosition = 1;
168 			}
169 		}
170 	}
171 }
172