OpenBve.Interface.LoadControls C# (CSharp) Method

LoadControls() static private method

Loads the current controls from the controls.cfg file
static private LoadControls ( string FileOrNull, Control &Controls ) : void
FileOrNull string An absolute path reference to a saved controls.cfg file, or a null reference to check the default locations
Controls Control The current controls array
return void
		internal static void LoadControls(string FileOrNull, out Control[] Controls)
		{
			string File;
			if (FileOrNull == null) {
				File = OpenBveApi.Path.CombineFile(Program.FileSystem.SettingsFolder, "controls.cfg");
				if (!System.IO.File.Exists(File)) {
					//Load the default key assignments if the user settings don't exist
					File = OpenBveApi.Path.CombineFile(Program.FileSystem.GetDataFolder("Controls"), "Default keyboard assignment.controls");
					if (!System.IO.File.Exists(File))
					{
						MessageBox.Show(Interface.GetInterfaceString("errors_warning") + Environment.NewLine + Interface.GetInterfaceString("errors_controls_missing"),
												Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
						Controls = new Control[0];
						return;
					}
				}
			} else {
				File = FileOrNull;
			}
			Controls = new Control[256];
			int Length = 0;
			CultureInfo Culture = CultureInfo.InvariantCulture;
			if (System.IO.File.Exists(File)) {
				string[] Lines = System.IO.File.ReadAllLines(File, new System.Text.UTF8Encoding());
				for (int i = 0; i < Lines.Length; i++) {
					Lines[i] = Lines[i].Trim();
					if (Lines[i].Length != 0 && !Lines[i].StartsWith(";", StringComparison.OrdinalIgnoreCase)) {
						string[] Terms = Lines[i].Split(',');
						for (int j = 0; j < Terms.Length; j++) {
							Terms[j] = Terms[j].Trim();
						}
						if (Terms.Length >= 2) {
							if (Length >= Controls.Length) {
								Array.Resize<Control>(ref Controls, Controls.Length << 1);
							}
							int j;
							for (j = 0; j < CommandInfos.Length; j++) {
								if (string.Compare(CommandInfos[j].Name, Terms[0], StringComparison.OrdinalIgnoreCase) == 0) break;
							}
							if (j == CommandInfos.Length) {
								Controls[Length].Command = Command.None;
								Controls[Length].InheritedType = CommandType.Digital;
								Controls[Length].Method = ControlMethod.Invalid;
								Controls[Length].Device = -1;
								Controls[Length].Component = JoystickComponent.Invalid;
								Controls[Length].Element = -1;
								Controls[Length].Direction = 0;
								Controls[Length].Modifier = KeyboardModifier.None;
							} else {
								Controls[Length].Command = CommandInfos[j].Command;
								Controls[Length].InheritedType = CommandInfos[j].Type;
								string Method = Terms[1].ToLowerInvariant();
								bool Valid = false;
								if (Method == "keyboard" & Terms.Length == 4)
								{
									Key CurrentKey;
									int SDLTest;
									if (int.TryParse(Terms[2], out SDLTest))
									{
										//We've discovered a SDL keybinding is present, so reset the loading process with the default keyconfig & show an appropriate error message
										if (ControlsReset == false)
										{
											MessageBox.Show(Interface.GetInterfaceString("errors_controls_oldversion") + Environment.NewLine + Interface.GetInterfaceString("errors_controls_reset"), Application.ProductName,
												MessageBoxButtons.OK, MessageBoxIcon.Hand);
										}
										var DefaultControls = OpenBveApi.Path.CombineFile(Program.FileSystem.GetDataFolder("Controls"),"Default keyboard assignment.controls");
										if (System.IO.File.Exists(DefaultControls))
										{
											if (ControlsReset == false)
											{
												LoadControls(DefaultControls, out CurrentControls);
												ControlsReset = true;
											}
											else
											{
												MessageBox.Show(Interface.GetInterfaceString("errors_warning") + Environment.NewLine + Interface.GetInterfaceString("errors_controls_default_oldversion"),
												Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
												Controls = new Control[0];
											}
											
										}
										else
										{
											MessageBox.Show(Interface.GetInterfaceString("errors_warning") + Environment.NewLine + Interface.GetInterfaceString("errors_controls_default_missing"),
												Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
												Controls = new Control[0];
										}
										return;
									}
									if (Enum.TryParse(Terms[2], true, out CurrentKey))
									{
										int Modifiers;
										if (int.TryParse(Terms[3], NumberStyles.Integer, Culture, out Modifiers))
										{
											Controls[Length].Method = ControlMethod.Keyboard;
											Controls[Length].Device = -1;
											Controls[Length].Component = JoystickComponent.Invalid;
											Controls[Length].Key = CurrentKey;
											Controls[Length].Direction = 0;
											Controls[Length].Modifier = (KeyboardModifier) Modifiers;
											Valid = true;
										}
									}
								}
								

								 else if (Method == "joystick" & Terms.Length >= 4) {
									int Device;
									if (int.TryParse(Terms[2], NumberStyles.Integer, Culture, out Device)) {
										string Component = Terms[3].ToLowerInvariant();
										if (Component == "axis" & Terms.Length == 6)
										{
											int CurrentAxis;
											if (Int32.TryParse(Terms[4], out CurrentAxis))
											{
												int Direction;
												if (int.TryParse(Terms[5], NumberStyles.Integer, Culture, out Direction))
												{

													Controls[Length].Method = ControlMethod.Joystick;
													Controls[Length].Device = Device;
													Controls[Length].Component = JoystickComponent.Axis;
													Controls[Length].Element = CurrentAxis;
													Controls[Length].Direction = Direction;
													Controls[Length].Modifier = KeyboardModifier.None;
													Valid = true;
												}
											}
										}
										else if (Component == "hat" & Terms.Length == 6)
										{
											int CurrentHat;
											if (Int32.TryParse(Terms[4], out CurrentHat))
											{
												int HatDirection;
												if (Int32.TryParse(Terms[5], out HatDirection))
												{
													Controls[Length].Method = ControlMethod.Joystick;
													Controls[Length].Device = Device;
													Controls[Length].Component = JoystickComponent.Hat;
													Controls[Length].Element = CurrentHat;
													Controls[Length].Direction = HatDirection;
													Controls[Length].Modifier = KeyboardModifier.None;
													Valid = true;
												}

											}
										}
										else if (Component == "button" & Terms.Length == 5)
										{
											int CurrentButton;
											if (Int32.TryParse(Terms[4], out CurrentButton))
											{
												Controls[Length].Method = ControlMethod.Joystick;
												Controls[Length].Device = Device;
												Controls[Length].Component = JoystickComponent.Button;
												Controls[Length].Element = CurrentButton;
												Controls[Length].Direction = 0;
												Controls[Length].Modifier = KeyboardModifier.None;
												Valid = true;
											}
										}

									}
										  
									
										  
								}

								if (!Valid) {
									Controls[Length].Method = ControlMethod.Invalid;
									Controls[Length].Device = -1;
									Controls[Length].Component = JoystickComponent.Invalid;
									Controls[Length].Element = -1;
									Controls[Length].Direction = 0;
									Controls[Length].Modifier = KeyboardModifier.None;
								}
							}
							Length++;
						}
					}
				}
			}
			Array.Resize<Control>(ref Controls, Length);
		}

Usage Example

示例#1
0
        // import
        private void buttonControlsImport_Click(object sender, EventArgs e)
        {
            OpenFileDialog Dialog = new OpenFileDialog();

            Dialog.CheckFileExists = true;
            //Dialog.InitialDirectory = Interface.GetControlsFolder();
            Dialog.Filter = Interface.GetInterfaceString("dialog_controlsfiles") + "|*.controls|" + Interface.GetInterfaceString("dialog_allfiles") + "|*";
            if (Dialog.ShowDialog() == DialogResult.OK)
            {
                try {
                    Interface.LoadControls(Dialog.FileName, out Interface.CurrentControls);
                    for (int i = 0; i < listviewControls.SelectedItems.Count; i++)
                    {
                        listviewControls.SelectedItems[i].Selected = false;
                    }
                    listviewControls.Items.Clear();
                    ListViewItem[] Items = new ListViewItem[Interface.CurrentControls.Length];
                    for (int i = 0; i < Interface.CurrentControls.Length; i++)
                    {
                        Items[i] = new ListViewItem(new string[] { "", "", "", "" });
                        UpdateControlListElement(Items[i], i, false);
                    }
                    listviewControls.Items.AddRange(Items);
                    listviewControls.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
                } catch (Exception ex) {
                    MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
            }
        }
All Usage Examples Of OpenBve.Interface::LoadControls