Ejemplo n.º 1
0
 public void ProcessHostGamepads(Action <string?, bool, InputFocus> handleButton, Action <string?, float> handleAxis)
 {
     foreach (var pad in OTK_GamePad.EnumerateDevices())
     {
         foreach (var but in pad.buttonObjects)
         {
             handleButton(pad.InputNamePrefix + but.ButtonName, but.ButtonAction(), InputFocus.Pad);
         }
         foreach (var(axisID, f) in pad.GetAxes())
         {
             handleAxis($"{pad.InputNamePrefix}{axisID} Axis", f);
         }
     }
 }
Ejemplo n.º 2
0
        private const int MAX_JOYSTICKS = 4;         //They don't have a way to query this for some reason. 4 is the minimum promised.

        public static void Initialize()
        {
            Devices = new List <OTK_GamePad>();

            for (int i = 0; i < MAX_JOYSTICKS; i++)
            {
                JoystickState jss = Joystick.GetState(i);
                if (jss.IsConnected)
                {
                    Console.WriteLine($"joydevice index: {i}");                     //OpenTK doesn't expose the GUID, even though it stores it internally...

                    OTK_GamePad ogp = new OTK_GamePad(i);
                    Devices.Add(ogp);
                }
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Initialization is only called once when MainForm loads
        /// </summary>
        public static void Initialize()
        {
            Devices.Clear();

            int playerCount = 0;

            for (int i = 0; i < MAX_GAMEPADS; i++)
            {
                if (OpenTK.Input.GamePad.GetState(i).IsConnected || Joystick.GetState(i).IsConnected)
                {
                    Console.WriteLine(string.Format("OTK GamePad/Joystick index: {0}", i));
                    OTK_GamePad ogp = new OTK_GamePad(i, ++playerCount);
                    Devices.Add(ogp);
                }
            }
        }
Ejemplo n.º 4
0
 public static void Initialize(Control parent)
 {
     if (OSTailoredCode.IsUnixHost)
     {
         OTK_Keyboard.Initialize();
         OTK_GamePad.Initialize();
     }
     else
     {
         KeyInput.Initialize(parent);
         IPCKeyInput.Initialize();
         GamePad.Initialize(parent);
         GamePad360.Initialize();
     }
     Instance = new Input();
 }
Ejemplo n.º 5
0
 public static void Initialize()
 {
     if (OSTailoredCode.IsWindows())
     {
         KeyInput.Initialize();
         IPCKeyInput.Initialize();
         GamePad.Initialize();
         GamePad360.Initialize();
     }
     else
     {
         OTK_Keyboard.Initialize();
         OTK_GamePad.Initialize();
     }
     Instance = new Input();
 }
Ejemplo n.º 6
0
 public static void Initialize()
 {
     if (PlatformLinkedLibSingleton.RunningOnUnix)
     {
         OTK_Keyboard.Initialize();
         OTK_GamePad.Initialize();
     }
     else
     {
         KeyInput.Initialize();
         IPCKeyInput.Initialize();
         GamePad.Initialize();
         GamePad360.Initialize();
         //OTK_GamePad.Initialize();
     }
     Instance = new Input();
 }
Ejemplo n.º 7
0
        private void UpdateThreadProc()
        {
            while (true)
            {
#if true
                var keyEvents = OTK_Keyboard.Update();
                OTK_GamePad.UpdateAll();
#else
                var keyEvents = OSTailoredCode.IsUnixHost
                                        ? OTK_Keyboard.Update()
                                        : KeyInput.Update().Concat(IPCKeyInput.Update());
                if (OSTailoredCode.IsUnixHost)
                {
                    OTK_GamePad.UpdateAll();
                }
                else
                {
                    GamePad.UpdateAll();
                    GamePad360.UpdateAll();
                }
#endif

                //this block is going to massively modify data structures that the binding method uses, so we have to lock it all
                lock (this)
                {
                    _newEvents.Clear();

                    //analyze keys
                    foreach (var ke in keyEvents)
                    {
                        HandleButton(ke.Key.ToString(), ke.Pressed, InputFocus.Keyboard);
                    }

                    lock (_floatValues)
                    {
                        //FloatValues.Clear();

                        // analyze OpenTK xinput (or is it libinput?)
                        foreach (var pad in OTK_GamePad.EnumerateDevices())
                        {
                            foreach (var but in pad.buttonObjects)
                            {
                                HandleButton(pad.InputNamePrefix + but.ButtonName, but.ButtonAction(), InputFocus.Pad);
                            }
                            foreach (var sv in pad.GetFloats())
                            {
                                var n = $"{pad.InputNamePrefix}{sv.Item1} Axis";
                                var f = sv.Item2;
                                if (_trackDeltas)
                                {
                                    _floatDeltas[n] += Math.Abs(f - _floatValues[n]);
                                }
                                _floatValues[n] = f;
                            }
                        }

#if false
                        // analyze xinput
                        foreach (var pad in GamePad360.EnumerateDevices())
                        {
                            string xName = $"X{pad.PlayerNumber} ";
                            for (int b = 0; b < pad.NumButtons; b++)
                            {
                                HandleButton(xName + pad.ButtonName(b), pad.Pressed(b), InputFocus.Pad);
                            }
                            foreach (var sv in pad.GetFloats())
                            {
                                string n = xName + sv.Item1;
                                float  f = sv.Item2;
                                if (_trackDeltas)
                                {
                                    _floatDeltas[n] += Math.Abs(f - _floatValues[n]);
                                }
                                _floatValues[n] = f;
                            }
                        }

                        // analyze joysticks
                        foreach (var pad in GamePad.EnumerateDevices())
                        {
                            string jName = $"J{pad.PlayerNumber} ";
                            for (int b = 0; b < pad.NumButtons; b++)
                            {
                                HandleButton(jName + pad.ButtonName(b), pad.Pressed(b), InputFocus.Pad);
                            }
                            foreach (var sv in pad.GetFloats())
                            {
                                string n = jName + sv.Item1;
                                float  f = sv.Item2;
                                //if (n == "J5 RotationZ")
                                //	System.Diagnostics.Debugger.Break();
                                if (_trackDeltas)
                                {
                                    _floatDeltas[n] += Math.Abs(f - _floatValues[n]);
                                }
                                _floatValues[n] = f;
                            }
                        }
#endif

                        // analyze moose
                        // other sorts of mouse api (raw input) could easily be added as a separate listing under a different class
                        if (_wantingMouseFocus.Contains(Form.ActiveForm))
                        {
                            var mousePos = Control.MousePosition;
                            if (_trackDeltas)
                            {
                                // these are relative to screen coordinates, but that's not terribly important
                                _floatDeltas["WMouse X"] += Math.Abs(mousePos.X - _floatValues["WMouse X"]) * 50;
                                _floatDeltas["WMouse Y"] += Math.Abs(mousePos.Y - _floatValues["WMouse Y"]) * 50;
                            }
                            // coordinate translation happens later
                            _floatValues["WMouse X"] = mousePos.X;
                            _floatValues["WMouse Y"] = mousePos.Y;

                            var mouseBtns = Control.MouseButtons;
                            HandleButton("WMouse L", (mouseBtns & MouseButtons.Left) != 0, InputFocus.Mouse);
                            HandleButton("WMouse C", (mouseBtns & MouseButtons.Middle) != 0, InputFocus.Mouse);
                            HandleButton("WMouse R", (mouseBtns & MouseButtons.Right) != 0, InputFocus.Mouse);
                            HandleButton("WMouse 1", (mouseBtns & MouseButtons.XButton1) != 0, InputFocus.Mouse);
                            HandleButton("WMouse 2", (mouseBtns & MouseButtons.XButton2) != 0, InputFocus.Mouse);
                        }
                        else
                        {
#if false // don't do this: for now, it will interfere with the virtualpad. don't do something similar for the mouse position either
                            // unpress all buttons
                            HandleButton("WMouse L", false, InputFocus.Mouse);
                            HandleButton("WMouse C", false, InputFocus.Mouse);
                            HandleButton("WMouse R", false, InputFocus.Mouse);
                            HandleButton("WMouse 1", false, InputFocus.Mouse);
                            HandleButton("WMouse 2", false, InputFocus.Mouse);
#endif
                        }
                    }

                    if (_newEvents.Count != 0)
                    {
                        //WHAT!? WE SHOULD NOT BE SO NAIVELY TOUCHING MAINFORM FROM THE INPUTTHREAD. ITS BUSY RUNNING.
                        AllowInput allowInput = GlobalWin.MainForm.AllowInput(false);

                        foreach (var ie in _newEvents)
                        {
                            //events are swallowed in some cases:
                            if (ie.LogicalButton.Alt && ShouldSwallow(GlobalWin.MainForm.AllowInput(true), ie))
                            {
                                continue;
                            }
                            if (ie.EventType == InputEventType.Press && ShouldSwallow(allowInput, ie))
                            {
                                continue;
                            }

                            EnqueueEvent(ie);
                        }
                    }

                    _ignoreEventsNextPoll = false;
                }                 //lock(this)

                //arbitrary selection of polling frequency:
                Thread.Sleep(10);
            }
        }
Ejemplo n.º 8
0
 public void FirstInitAll(IntPtr mainFormHandle)
 {
     OTK_Keyboard.Initialize();
     OTK_GamePad.Initialize();
 }
Ejemplo n.º 9
0
 public void PreprocessHostGamepads() => OTK_GamePad.UpdateAll();
Ejemplo n.º 10
0
        void UpdateThreadProc()
        {
            while (true)
            {
                var keyEvents = PlatformLinkedLibSingleton.RunningOnUnix
                                        ? OTK_Keyboard.Update()
                                        : KeyInput.Update().Concat(IPCKeyInput.Update());
                if (PlatformLinkedLibSingleton.RunningOnUnix)
                {
                    OTK_GamePad.UpdateAll();
                }
                else
                {
                    GamePad.UpdateAll();
                    GamePad360.UpdateAll();
                    //OTK_GamePad.UpdateAll();
                }

                //this block is going to massively modify data structures that the binding method uses, so we have to lock it all
                lock (this)
                {
                    _NewEvents.Clear();

                    //analyze keys
                    foreach (var ke in keyEvents)
                    {
                        HandleButton(ke.Key.ToString(), ke.Pressed);
                    }

                    lock (FloatValues)
                    {
                        //FloatValues.Clear();

                        //analyze OTK
                        foreach (var pad in OTK_GamePad.EnumerateDevices())
                        {
                            string cType = pad.MappedGamePad ? "X" : "J";
                            string xname = cType + pad.ID + " ";

                            foreach (var but in pad.buttonObjects)
                            {
                                HandleButton(xname + but.ButtonName, but.ButtonAction());
                            }

                            foreach (var sv in pad.GetFloats())
                            {
                                string n = xname + sv.Item1 + " Axis";
                                float  f = sv.Item2;
                                if (trackdeltas)
                                {
                                    FloatDeltas[n] += Math.Abs(f - FloatValues[n]);
                                }
                                FloatValues[n] = f;
                            }
                        }

                        //analyze xinput
                        foreach (var pad in GamePad360.EnumerateDevices())
                        {
                            string xname = "X" + pad.PlayerNumber + " ";
                            for (int b = 0; b < pad.NumButtons; b++)
                            {
                                HandleButton(xname + pad.ButtonName(b), pad.Pressed(b));
                            }
                            foreach (var sv in pad.GetFloats())
                            {
                                string n = xname + sv.Item1;
                                float  f = sv.Item2;
                                if (trackdeltas)
                                {
                                    FloatDeltas[n] += Math.Abs(f - FloatValues[n]);
                                }
                                FloatValues[n] = f;
                            }
                        }

                        //analyze joysticks
                        foreach (var pad in GamePad.EnumerateDevices())
                        {
                            string jname = "J" + pad.PlayerNumber + " ";
                            for (int b = 0; b < pad.NumButtons; b++)
                            {
                                HandleButton(jname + pad.ButtonName(b), pad.Pressed(b));
                            }
                            foreach (var sv in pad.GetFloats())
                            {
                                string n = jname + sv.Item1;
                                float  f = sv.Item2;
                                //if (n == "J5 RotationZ")
                                //	System.Diagnostics.Debugger.Break();
                                if (trackdeltas)
                                {
                                    FloatDeltas[n] += Math.Abs(f - FloatValues[n]);
                                }
                                FloatValues[n] = f;
                            }
                        }

                        // analyse moose
                        // other sorts of mouse api (raw input) could easily be added as a separate listing under a different class
                        if (WantingMouseFocus.Contains(System.Windows.Forms.Form.ActiveForm))
                        {
                            var P = System.Windows.Forms.Control.MousePosition;
                            if (trackdeltas)
                            {
                                // these are relative to screen coordinates, but that's not terribly important
                                FloatDeltas["WMouse X"] += Math.Abs(P.X - FloatValues["WMouse X"]) * 50;
                                FloatDeltas["WMouse Y"] += Math.Abs(P.Y - FloatValues["WMouse Y"]) * 50;
                            }
                            // coordinate translation happens later
                            FloatValues["WMouse X"] = P.X;
                            FloatValues["WMouse Y"] = P.Y;

                            var B = System.Windows.Forms.Control.MouseButtons;
                            HandleButton("WMouse L", (B & System.Windows.Forms.MouseButtons.Left) != 0);
                            HandleButton("WMouse C", (B & System.Windows.Forms.MouseButtons.Middle) != 0);
                            HandleButton("WMouse R", (B & System.Windows.Forms.MouseButtons.Right) != 0);
                            HandleButton("WMouse 1", (B & System.Windows.Forms.MouseButtons.XButton1) != 0);
                            HandleButton("WMouse 2", (B & System.Windows.Forms.MouseButtons.XButton2) != 0);
                        }
                        else
                        {
                            //dont do this: for now, it will interfere with the virtualpad. dont do something similar for the mouse position either
                            //unpress all buttons
                            //HandleButton("WMouse L", false);
                            //HandleButton("WMouse C", false);
                            //HandleButton("WMouse R", false);
                            //HandleButton("WMouse 1", false);
                            //HandleButton("WMouse 2", false);
                        }
                    }

                    //WHAT!? WE SHOULD NOT BE SO NAIVELY TOUCHING MAINFORM FROM THE INPUTTHREAD. ITS BUSY RUNNING.
                    bool swallow = !GlobalWin.MainForm.AllowInput(false);

                    foreach (var ie in _NewEvents)
                    {
                        //events are swallowed in some cases:
                        if (ie.LogicalButton.Alt && !GlobalWin.MainForm.AllowInput(true))
                        {
                        }
                        else if (ie.EventType == InputEventType.Press && swallow)
                        {
                        }
                        else
                        {
                            EnqueueEvent(ie);
                        }
                    }
                }                 //lock(this)

                //arbitrary selection of polling frequency:
                Thread.Sleep(10);
            }
        }