Пример #1
0
        private static bool ActionTest()
        {
            Action act = new("test", new InputMap(InputDevice.Joystick, InputType.Button, "0", "1"));

            if (!Input.Manager.Actions.Add(act))
            {
                return(Logger.LogReturn("Unable to add valid action to action set.", false, LogType.Error));
            }

            Action a = Input.Manager.Actions["test"];

            if (a is null)
            {
                return(Logger.LogReturn("Unable to retrieve previously added action from the action set.", false, LogType.Error));
            }

            {
                bool con = Logger.LogToConsole,
                     fil = Logger.LogToFile;

                Logger.LogToConsole = false;
                Logger.LogToFile    = false;

                bool result = Input.Manager.Actions.Add(a, false);

                Logger.LogToConsole = con;
                Logger.LogToFile    = fil;

                if (result)
                {
                    return(Logger.LogReturn("Input manager allowed adding an action that already exists when replace is false.", false, LogType.Error));
                }
            }

            if (!Input.Manager.SaveToFile(InputPath, true))
            {
                return(Logger.LogReturn("Input manager failed saving to file.", false, LogType.Error));
            }

            if (!Input.Manager.LoadFromFile(InputPath))
            {
                return(Logger.LogReturn("Input manager failed loading from file.", false, LogType.Error));
            }

            if (!Input.Manager.Actions.Contains(a))
            {
                return(Logger.LogReturn("Lost mapped input after loading from file.", false, LogType.Error));
            }
            if (Input.Manager.Actions["test"].Name != act.Name)
            {
                return(Logger.LogReturn("Input manager did not load from file correctly.", false, LogType.Error));
            }

            try
            {
                File.Delete(InputPath);
            }
            catch
            { }

            return(true);
        }
Пример #2
0
        public static int RunExample()
        {
            int  exitVal = 0;
            bool running = true;

            using (RenderWindow window = new(new VideoMode(640, 480), "MiInput", Styles.Close) )
            {
                window.Closed += OnClose;

                while (running)
                {
                    // Create action.
                    {
                        Action hor = new("horizontal",
                                         new InputMap(InputDevice.Joystick, InputType.Axis, "LeftStickX"),
                                         new InputMap(InputDevice.Keyboard, InputType.Button, "D", "A"));

                        // Add action to action set, replacing an already existing action with the same ID.
                        if (!Input.Manager.Actions.Add(hor, true))
                        {
                            Console.WriteLine("Failed adding action to set (is the action valid?)");
                            exitVal = -1;
                            running = false;
                        }
                    }

                    // Retrieve assigned action.
                    {
                        Action a = Input.Manager.Actions["horizontal"];

                        if (a is null)
                        {
                            Console.WriteLine("Unable to retrieve previously added action from the action set.");
                            exitVal = -2;
                            running = false;
                        }
                    }

                    // Save action set to file.
                    if (!Input.Manager.SaveToFile(FilePath, true))
                    {
                        Console.WriteLine("Unable to write action set to file.");
                        exitVal = -3;
                        running = false;
                    }

                    // Load action set from file.
                    if (!Input.Manager.LoadFromFile(FilePath))
                    {
                        Console.WriteLine("Unable to load action set from file.");
                        exitVal = -4;
                        running = false;
                    }

                    // Access assigned action.
                    {
                        Action horizontal = Input.Manager.Actions["horizontal"];

                        if (horizontal == null)
                        {
                            Console.WriteLine("horizontal action does not exist.");
                            exitVal = -5;
                            running = false;
                        }
                    }

                    // Break out on success.
                    break;
                }

                // For holding the current and previous values of the horizontal action we created before.
                float thisX = 0.0f,
                      lastX = 0.0f;

                while (running && window.IsOpen)
                {
                    window.DispatchEvents();

                    // Update input managers. This must be called every frame before polling for input.
                    Input.Manager.Update();

                    lastX = thisX;
                    thisX = Input.Manager.Actions["horizontal"].Value;

                    // Only print horizontal action value if it changed.
                    if (thisX != lastX)
                    {
                        Console.WriteLine("Horizontal: " + thisX.ToString() + ".");
                    }

                    if (Input.Manager.JustPressed(InputDevice.Keyboard, "Space"))
                    {
                        Console.WriteLine("Space just pressed.");
                    }

                    if (Input.Manager.JustPressed(InputDevice.Mouse, "Left"))
                    {
                        Console.WriteLine("Left mouse button just clicked.");
                    }
                    if (Input.Manager.JustReleased(InputDevice.Mouse, "Left"))
                    {
                        Console.WriteLine("Left mouse button just released.");
                    }

                    if (Input.Manager.JustPressed(InputDevice.Joystick, "0"))
                    {
                        Console.WriteLine("Joystick button 0 just pressed.");
                    }

                    window.Clear();
                    window.Display();
                }
            }

            return(exitVal);
        }