Exemple #1
0
        protected override void OnLoad(EventArgs e)
        {
            Program.FileSystem.AppendToLogFile("Game window initialised successfully.");
            //Initialise the loader thread queues
            jobs  = new Queue <ThreadStart>(10);
            locks = new Queue <object>(10);
            Program.Renderer.Initialize(Program.CurrentHost, Interface.CurrentOptions);
            Program.Renderer.DetermineMaxAFLevel();
            HUD.LoadHUD();
            Program.Renderer.Loading.InitLoading(Program.FileSystem.GetDataFolder("In-game"), typeof(NewRenderer).Assembly.GetName().Version.ToString());
            Program.Renderer.UpdateViewport(ViewportChangeMode.NoChange);
            Program.Renderer.MotionBlur.Initialize(Interface.CurrentOptions.MotionBlur);
            Loading.LoadAsynchronously(MainLoop.currentResult.RouteFile, MainLoop.currentResult.RouteEncoding, MainLoop.currentResult.TrainFolder, MainLoop.currentResult.TrainEncoding);
            LoadingScreenLoop();
            //Add event handler hooks for keyboard and mouse buttons
            //Do this after the renderer has init and the loop has started to prevent timing issues
            KeyDown    += MainLoop.keyDownEvent;
            KeyUp      += MainLoop.keyUpEvent;
            MouseDown  += MainLoop.mouseDownEvent;
            MouseUp    += MainLoop.mouseUpEvent;
            MouseMove  += MainLoop.mouseMoveEvent;
            MouseWheel += MainLoop.mouseWheelEvent;

            for (int i = 0; i < InputDevicePlugin.AvailablePluginInfos.Count; i++)
            {
                if (InputDevicePlugin.AvailablePluginInfos[i].Status == InputDevicePlugin.PluginInfo.PluginStatus.Enable)
                {
                    int       AddControlsLength = InputDevicePlugin.AvailablePlugins[i].Controls.Length;
                    Control[] AddControls       = new Control[AddControlsLength];
                    for (int j = 0; j < AddControlsLength; j++)
                    {
                        AddControls[j].Command = InputDevicePlugin.AvailablePlugins[i].Controls[j].Command;
                        AddControls[j].Method  = ControlMethod.InputDevicePlugin;
                        AddControls[j].Option  = InputDevicePlugin.AvailablePlugins[i].Controls[j].Option;
                    }
                    Interface.CurrentControls = Interface.CurrentControls.Concat(AddControls).ToArray();
                    foreach (var Train in Program.TrainManager.Trains)
                    {
                        if (Train.State != TrainState.Bogus)
                        {
                            if (Train.IsPlayerTrain)
                            {
                                InputDevicePlugin.AvailablePlugins[i].SetMaxNotch(Train.Handles.Power.MaximumDriverNotch, Train.Handles.Brake.MaximumDriverNotch);
                            }
                        }
                    }
                    InputDevicePlugin.AvailablePlugins[i].KeyDown += MainLoop.InputDevicePluginKeyDown;
                    InputDevicePlugin.AvailablePlugins[i].KeyUp   += MainLoop.InputDevicePluginKeyUp;
                }
            }
        }
 // control down
 private void buttonControlDown_Click(object sender, EventArgs e)
 {
     if (listviewControls.SelectedIndices.Count == 1)
     {
         int j = listviewControls.SelectedIndices[0];
         if (j < Interface.CurrentControls.Length - 1)
         {
             Control c = Interface.CurrentControls[j];
             Interface.CurrentControls[j]     = Interface.CurrentControls[j + 1];
             Interface.CurrentControls[j + 1] = c;
             ListViewItem v = listviewControls.Items[j];
             listviewControls.Items.RemoveAt(j);
             listviewControls.Items.Insert(j + 1, v);
         }
     }
 }
Exemple #3
0
        /// <summary>Loads the current controls from the controls.cfg file</summary>
        /// <param name="FileOrNull">An absolute path reference to a saved controls.cfg file, or a null reference to check the default locations</param>
        /// <param name="Controls">The current controls array</param>
        internal static void LoadControls(string FileOrNull, out Control[] Controls)
        {
            string File;

            string[] Lines = {};
            try
            {
                //Don't crash horribly if the embedded default controls file is missing (makefile.....)
                Lines = GetLines(Assembly.GetExecutingAssembly().GetManifestResourceStream("OpenBve.Default.controls"));
            }
            catch
            {
            }

            if (FileOrNull == null)
            {
                File = OpenBveApi.Path.CombineFile(Program.FileSystem.SettingsFolder, "1.5.0/controls.cfg");
                if (!System.IO.File.Exists(File))
                {
                    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.controls");
                    if (!System.IO.File.Exists(File))
                    {
                        MessageBox.Show(Translations.GetInterfaceString("errors_warning") + Environment.NewLine + Translations.GetInterfaceString("errors_controls_missing"),
                                        Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    }
                }
            }
            else
            {
                File = FileOrNull;
            }

            Controls = new Control[256];
            int         Length  = 0;
            CultureInfo Culture = CultureInfo.InvariantCulture;

            if (System.IO.File.Exists(File))
            {
                Lines = System.IO.File.ReadAllLines(File, new System.Text.UTF8Encoding());
            }

            for (int i = 0; i < Lines.Length; i++)
            {
                Lines[i] = Lines[i].Trim(new char[] { });
                if (Lines[i].Length != 0 && !Lines[i].StartsWith(";", StringComparison.OrdinalIgnoreCase))
                {
                    string[] Terms = Lines[i].Split(new[] { ',' });
                    for (int j = 0; j < Terms.Length; j++)
                    {
                        Terms[j] = Terms[j].Trim(new char[] { });
                    }

                    if (Terms.Length >= 2)
                    {
                        if (Length >= Controls.Length)
                        {
                            Array.Resize(ref Controls, Controls.Length << 1);
                        }

                        int j;
                        for (j = 0; j < Translations.CommandInfos.Length; j++)
                        {
                            if (string.Compare(Translations.CommandInfos[j].Name, Terms[0], StringComparison.OrdinalIgnoreCase) == 0)
                            {
                                break;
                            }
                        }

                        if (j == Translations.CommandInfos.Length)
                        {
                            Controls[Length].Command       = Translations.Command.None;
                            Controls[Length].InheritedType = Translations.CommandType.Digital;
                            Controls[Length].Method        = ControlMethod.Invalid;
                            Controls[Length].Device        = new Guid();
                            Controls[Length].Component     = JoystickComponent.Invalid;
                            Controls[Length].Element       = -1;
                            Controls[Length].Direction     = 0;
                            Controls[Length].Modifier      = KeyboardModifier.None;
                            Controls[Length].Option        = 0;
                        }
                        else
                        {
                            Controls[Length].Command       = Translations.CommandInfos[j].Command;
                            Controls[Length].InheritedType = Translations.CommandInfos[j].Type;
                            string Method = Terms[1].ToLowerInvariant();
                            bool   Valid  = false;
                            if (Method == "keyboard" & Terms.Length >= 4)
                            {
                                Key CurrentKey;
                                // ReSharper disable once NotAccessedVariable
                                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(Translations.GetInterfaceString("errors_controls_oldversion") + Environment.NewLine + Translations.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)
                                        {
                                            ControlsReset = true;
                                            LoadControls(DefaultControls, out CurrentControls);
                                        }
                                        else
                                        {
                                            MessageBox.Show(Translations.GetInterfaceString("errors_warning") + Environment.NewLine + Translations.GetInterfaceString("errors_controls_default_oldversion"),
                                                            Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                                            i     = 0;
                                            Lines = GetLines(Assembly.GetExecutingAssembly().GetManifestResourceStream("OpenBve.Default.controls"));
                                            continue;
                                        }
                                    }
                                    else
                                    {
                                        MessageBox.Show(Translations.GetInterfaceString("errors_warning") + Environment.NewLine + Translations.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    = new Guid();                                      //will create invalid all zero GUID
                                        Controls[Length].Component = JoystickComponent.Invalid;
                                        Controls[Length].Key       = (OpenBveApi.Input.Key)CurrentKey;
                                        Controls[Length].Direction = 0;
                                        Controls[Length].Modifier  = (KeyboardModifier)Modifiers;
                                        int Option;
                                        if (Terms.Length >= 5 && int.TryParse(Terms[4], NumberStyles.Integer, Culture, out Option))
                                        {
                                            Controls[Length].Option = Option;
                                        }

                                        Valid = true;
                                    }
                                }
                            }


                            else if (Method == "joystick" & Terms.Length >= 4)
                            {
                                int  oldDevice;
                                Guid Device = new Guid();
                                if (int.TryParse(Terms[2], NumberStyles.Integer, Culture, out oldDevice))
                                {
                                    Device = OpenTK.Input.Joystick.GetGuid(oldDevice);
                                }

                                if (Device != new Guid() || Guid.TryParse(Terms[2], 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;
                                                int Option;
                                                if (Terms.Length >= 7 && int.TryParse(Terms[6], NumberStyles.Integer, Culture, out Option))
                                                {
                                                    Controls[Length].Option = Option;
                                                }

                                                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;
                                                int Option;
                                                if (Terms.Length >= 7 && int.TryParse(Terms[6], NumberStyles.Integer, Culture, out Option))
                                                {
                                                    Controls[Length].Option = Option;
                                                }

                                                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;
                                            int Option;
                                            if (Terms.Length >= 6 && int.TryParse(Terms[5], NumberStyles.Integer, Culture, out Option))
                                            {
                                                Controls[Length].Option = Option;
                                            }

                                            Valid = true;
                                        }
                                    }
                                }
                            }
                            else if (Method == "raildriver" & Terms.Length >= 4)
                            {
                                int  oldDevice;
                                Guid Device = new Guid();
                                if (int.TryParse(Terms[2], NumberStyles.Integer, Culture, out oldDevice))
                                {
                                    Device = OpenTK.Input.Joystick.GetGuid(oldDevice);
                                }

                                if (Device != new Guid() || Guid.TryParse(Terms[2], 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.RailDriver;
                                                Controls[Length].Device    = Device;
                                                Controls[Length].Component = JoystickComponent.Axis;
                                                Controls[Length].Element   = CurrentAxis;
                                                Controls[Length].Direction = Direction;
                                                Controls[Length].Modifier  = KeyboardModifier.None;
                                                int Option;
                                                if (Terms.Length >= 7 && int.TryParse(Terms[6], NumberStyles.Integer, Culture, out Option))
                                                {
                                                    Controls[Length].Option = Option;
                                                }

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

                                            Valid = true;
                                        }
                                    }
                                }
                            }

                            if (!Valid)
                            {
                                Controls[Length].Method    = ControlMethod.Invalid;
                                Controls[Length].Device    = new Guid();                              //Invalid all zero GUID
                                Controls[Length].Component = JoystickComponent.Invalid;
                                Controls[Length].Element   = -1;
                                Controls[Length].Direction = 0;
                                Controls[Length].Modifier  = KeyboardModifier.None;
                                Controls[Length].Option    = 0;
                            }
                        }

                        Length++;
                    }
                }
            }
            Array.Resize(ref Controls, Length);
        }
Exemple #4
0
        internal static void RefreshObjects()
        {
            LightingRelative = -1.0;
            Game.Reset();
            formTrain.Instance?.DisableUI();
            for (int i = 0; i < Files.Count; i++)
            {
                try
                {
                    if (String.Compare(System.IO.Path.GetFileName(Files[i]), "extensions.cfg", StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        string currentTrainFolder = System.IO.Path.GetDirectoryName(Files[i]);
                        bool   canLoad            = false;
                        for (int j = 0; j < Program.CurrentHost.Plugins.Length; j++)
                        {
                            if (Program.CurrentHost.Plugins[j].Train != null && Program.CurrentHost.Plugins[j].Train.CanLoadTrain(currentTrainFolder))
                            {
                                Control[] dummyControls = new Control[0];
                                TrainManager.Trains = new[] { new TrainBase(TrainState.Available) };
                                AbstractTrain playerTrain = TrainManager.Trains[0];
                                Program.CurrentHost.Plugins[j].Train.LoadTrain(Encoding.UTF8, currentTrainFolder, ref playerTrain, ref dummyControls);
                                TrainManager.PlayerTrain = TrainManager.Trains[0];
                                canLoad = true;
                                break;
                            }
                        }

                        if (canLoad)
                        {
                            TrainManager.PlayerTrain.Initialize();
                            foreach (var Car in TrainManager.PlayerTrain.Cars)
                            {
                                double length = TrainManager.PlayerTrain.Cars[0].Length;
                                Car.Move(-length);
                                Car.Move(length);
                            }
                            TrainManager.PlayerTrain.PlaceCars(0);
                            for (int j = 0; j < TrainManager.PlayerTrain.Cars.Length; j++)
                            {
                                TrainManager.PlayerTrain.Cars[j].UpdateTrackFollowers(0, true, false);
                                TrainManager.PlayerTrain.Cars[j].UpdateTopplingCantAndSpring(0.0);
                                TrainManager.PlayerTrain.Cars[j].ChangeCarSection(CarSectionType.Exterior);
                                TrainManager.PlayerTrain.Cars[j].FrontBogie.UpdateTopplingCantAndSpring();
                                TrainManager.PlayerTrain.Cars[j].FrontBogie.ChangeSection(0);
                                TrainManager.PlayerTrain.Cars[j].RearBogie.UpdateTopplingCantAndSpring();
                                TrainManager.PlayerTrain.Cars[j].RearBogie.ChangeSection(0);
                                TrainManager.PlayerTrain.Cars[j].Coupler.ChangeSection(0);
                            }
                        }
                        else
                        {
                            //As we now attempt to load the train as a whole, the most likely outcome is that the train.dat file is MIA
                            Interface.AddMessage(MessageType.Critical, false, "No plugin found capable of loading file " + Files[i] + ".");
                        }
                    }
                    else
                    {
                        UnifiedObject o;
                        if (CurrentHost.LoadObject(Files[i], System.Text.Encoding.UTF8, out o))
                        {
                            o.CreateObject(Vector3.Zero, 0.0, 0.0, 0.0);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Interface.AddMessage(MessageType.Critical, false, "Unhandled error (" + ex.Message + ") encountered while processing the file " + Files[i] + ".");
                }
            }

            NearestTrain.UpdateSpecs();
            NearestTrain.Apply();
            formTrain.Instance?.EnableUI();

            Renderer.InitializeVisibility();
            Renderer.UpdateViewingDistances(600);
            Renderer.UpdateVisibility(0.0, true);
            ObjectManager.UpdateAnimatedWorldObjects(0.01, true);
            Program.TrainManager.UpdateTrainObjects(0.0, true);
            Renderer.ApplyBackgroundColor();
        }
Exemple #5
0
        internal static void RefreshObjects()
        {
            LightingRelative = -1.0;
            Game.Reset();
            for (int i = 0; i < Files.Length; i++)
            {
                try
                {
                    if (String.Compare(System.IO.Path.GetFileName(Files[i]), "extensions.cfg", StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        string currentTrainFolder = System.IO.Path.GetDirectoryName(Files[i]);

                        for (int j = 0; j < Program.CurrentHost.Plugins.Length; j++)
                        {
                            if (Program.CurrentHost.Plugins[j].Train != null && Program.CurrentHost.Plugins[j].Train.CanLoadTrain(currentTrainFolder))
                            {
                                TrainManager.Trains = new TrainBase[1];
                                Control[] dummyControls = new Control[0];
                                TrainManager.Trains[0] = new TrainManager.Train();
                                AbstractTrain playerTrain = TrainManager.Trains[0] as AbstractTrain;
                                Program.CurrentHost.Plugins[j].Train.LoadTrain(Encoding.UTF8, currentTrainFolder, ref playerTrain, ref dummyControls);
                                TrainManager.PlayerTrain = TrainManager.Trains[0];
                                break;
                            }
                        }
                        TrainManager.PlayerTrain.Initialize();
                        foreach (var Car in TrainManager.PlayerTrain.Cars)
                        {
                            double length = TrainManager.PlayerTrain.Cars[0].Length;
                            Car.Move(-length);
                            Car.Move(length);
                        }
                        TrainManager.PlayerTrain.PlaceCars(0);
                        for (int j = 0; j < TrainManager.PlayerTrain.Cars.Length; j++)
                        {
                            TrainManager.PlayerTrain.Cars[j].UpdateTrackFollowers(0, true, false);
                            TrainManager.PlayerTrain.Cars[j].ChangeCarSection(CarSectionType.Exterior);
                            TrainManager.PlayerTrain.Cars[j].FrontBogie.ChangeSection(0);
                            TrainManager.PlayerTrain.Cars[j].RearBogie.ChangeSection(0);
                        }
                    }
                    else
                    {
                        UnifiedObject o;
                        Program.CurrentHost.LoadObject(Files[i], System.Text.Encoding.UTF8, out o);
                        o.CreateObject(Vector3.Zero, 0.0, 0.0, 0.0);
                    }
                }
                catch (Exception ex)
                {
                    Interface.AddMessage(MessageType.Critical, false, "Unhandled error (" + ex.Message + ") encountered while processing the file " + Files[i] + ".");
                }
            }

            Renderer.InitializeVisibility();
            Renderer.UpdateViewingDistances(600);
            Renderer.UpdateVisibility(0.0, true);
            ObjectManager.UpdateAnimatedWorldObjects(0.01, true);
            Program.TrainManager.UpdateTrainObjects(0.0, true);
            Renderer.ApplyBackgroundColor();
        }