1 using System.Collections.Generic;
2 
3 namespace OpenBveApi.Interface
4 {
5 	public static partial class Translations
6 	{
7 		/// <summary>Stores the current language-code</summary>
8 		public static string CurrentLanguageCode = "en-GB";
9 
10 		internal struct InterfaceString
11 		{
12 			/// <summary>The name of the string</summary>
13 			internal string Name;
14 			/// <summary>The translated string text</summary>
15 			internal string Text;
16 		}
17 
18 		/// <summary>Sets the in-game language</summary>
19 		/// <param name="Language">The language string to set</param>
SetInGameLanguage(string Language)20 		public static void SetInGameLanguage(string Language)
21 		{
22 			//Set command infos to the translated strings
23 			for (int i = 0; i < AvailableLanguages.Count; i++)
24 			{
25 				//This is a hack, but the commandinfos are used in too many places to twiddle with easily
26 				if (AvailableLanguages[i].LanguageCode == Language)
27 				{
28 					CommandInfos = AvailableLanguages[i].myCommandInfos;
29 					QuickReferences = AvailableLanguages[i].myQuickReferences;
30 					TranslatedKeys = AvailableLanguages[i].KeyInfos;
31 					break;
32 				}
33 			}
34 		}
35 
36 		/*
37 		 * This is used to very marginally speed up access in the array with some bitwise logic
38 		 *
39 		 * I strongly suspect that any gains from this with our current string count
40 		 * (~500) will be un-noticable
41 		 *
42 		 * TODO: Consider removal for ease of maintanence
43 		 */
44 		private static int CurrentInterfaceStringIndex = 0;
45 
46 
47 		/// <summary>Fetches a translated user interface string</summary>
48 		/// <param name="Name">The name of the string to fetch</param>
49 		/// <returns>The translated string</returns>
GetInterfaceString(string Name)50 		public static string GetInterfaceString(string Name)
51 		{
52 			List<string> FallbackLanguages = new List<string>();
53 			//First, we need to find the default langauge file
54 			for (int i = 0; i < AvailableLanguages.Count; i++)
55 			{
56 				if (AvailableLanguages[i].LanguageCode == CurrentLanguageCode)
57 				{
58 					//Set the fallback languages
59 					FallbackLanguages = AvailableLanguages[i].FallbackCodes;
60 					int n = Name.Length;
61 					for (int k = 0; k < AvailableLanguages[i].InterfaceStringCount; k++)
62 					{
63 						int t;
64 						if ((k & 1) == 0)
65 						{
66 							t = (CurrentInterfaceStringIndex + (k >> 1) + AvailableLanguages[i].InterfaceStringCount) % AvailableLanguages[i].InterfaceStringCount;
67 						}
68 						else
69 						{
70 							t = (CurrentInterfaceStringIndex - (k + 1 >> 1) + AvailableLanguages[i].InterfaceStringCount) % AvailableLanguages[i].InterfaceStringCount;
71 						}
72 						if (AvailableLanguages[i].InterfaceStrings[t].Name.Length == n)
73 						{
74 							if (AvailableLanguages[i].InterfaceStrings[t].Name == Name)
75 							{
76 								CurrentInterfaceStringIndex = (t + 1) % AvailableLanguages[i].InterfaceStringCount;
77 								return AvailableLanguages[i].InterfaceStrings[t].Text;
78 							}
79 						}
80 					}
81 				}
82 			}
83 			//OK, so that didn't work- Try the fallback languages
84 			if (FallbackLanguages == null)
85 			{
86 				return Name;
87 			}
88 			for (int m = 0; m < FallbackLanguages.Count; m++)
89 			{
90 				for (int i = 0; i < AvailableLanguages.Count; i++)
91 				{
92 					if (AvailableLanguages[i].LanguageCode == FallbackLanguages[m])
93 					{
94 						int j = Name.Length;
95 						for (int k = 0; k < AvailableLanguages[i].InterfaceStringCount; k++)
96 						{
97 							int l;
98 							if ((k & 1) == 0)
99 							{
100 								l = (CurrentInterfaceStringIndex + (k >> 1) + AvailableLanguages[i].InterfaceStringCount) % AvailableLanguages[i].InterfaceStringCount;
101 							}
102 							else
103 							{
104 								l = (CurrentInterfaceStringIndex - (k + 1 >> 1) + AvailableLanguages[i].InterfaceStringCount) % AvailableLanguages[i].InterfaceStringCount;
105 							}
106 							if (AvailableLanguages[i].InterfaceStrings[l].Name.Length == j)
107 							{
108 								if (AvailableLanguages[i].InterfaceStrings[l].Name == Name)
109 								{
110 									CurrentInterfaceStringIndex = (l + 1) % AvailableLanguages[i].InterfaceStringCount;
111 									//We found the string in a fallback language, so let's return it!
112 									return AvailableLanguages[i].InterfaceStrings[l].Text;
113 								}
114 							}
115 						}
116 					}
117 				}
118 			}
119 
120 			//Default return type-
121 			//If the string does not exist in the current language or any of the fallback options, return the search string
122 			return Name;
123 		}
124 		/// <summary>The quick-reference strings displayed in-game</summary>
125 		public struct InterfaceQuickReference
126 		{
127 			/// <summary>Reverser Forwards</summary>
128 			public string HandleForward;
129 			/// <summary>Reverser Neutral</summary>
130 			public string HandleNeutral;
131 			/// <summary>Reverser Reverse</summary>
132 			public string HandleBackward;
133 			/// <summary>Power P(n)</summary>
134 			public string HandlePower;
135 			/// <summary>Power Neutral</summary>
136 			public string HandlePowerNull;
137 			/// <summary>Brake B(n)</summary>
138 			public string HandleBrake;
139 			/// <summary>LocoBrake B(n)</summary>
140 			public string HandleLocoBrake;
141 			/// <summary>Brake / LocoBrake Neutral</summary>
142 			public string HandleBrakeNull;
143 			/// <summary>Air brake release</summary>
144 			public string HandleRelease;
145 			/// <summary>Air brake lap</summary>
146 			public string HandleLap;
147 			/// <summary>Air brake service</summary>
148 			public string HandleService;
149 			/// <summary>Brake emergency</summary>
150 			public string HandleEmergency;
151 			/// <summary>Hold brake applied</summary>
152 			public string HandleHoldBrake;
153 			/// <summary>Left Doors</summary>
154 			public string DoorsLeft;
155 			/// <summary>Right doors</summary>
156 			public string DoorsRight;
157 			/// <summary>Score (n)</summary>
158 			public string Score;
159 		}
160 		/// <summary>Holds the current set of interface quick reference strings</summary>
161 		public static InterfaceQuickReference QuickReferences;
162 		/// <summary>The number of score events to be displayed</summary>
163 		/// TODO: Appears to remain constant, investigate exact usages and whether we can dump
164 		public static int RatingsCount = 10;
165 
166 	}
167 }
168