Esempio n. 1
0
        public void TestKeyboard()
        {
            var keyboard = new SlimDXTWKeyboard();
            var dev      = new SlimDX.DirectInput.DirectInput();
            var kb       = new SlimDX.DirectInput.Keyboard(dev);

            kb.Acquire();

            Application.Idle += delegate
            {
                while (true)
                {
                    keyboard.UpdateKeyboardState(kb.GetCurrentState());
                    if (keyboard.IsKeyReleased(SlimDX.DirectInput.Key.Escape))
                    {
                        break;
                    }
                    Application.DoEvents();
                }
                Application.Exit();
            };
            Application.Run();

            kb.Dispose();
            dev.Dispose();
        }
 protected override void UpdateKeyStates()
 {
     nativeKeyboard.GetCurrentState(ref nativeState);
     for (int i = 0; i < (int)Key.NumberOfKeys; i++)
     {
         UpdateKeyState(i, nativeState.IsPressed(KeyboardKeyMapper.Translate((Key)i)));
     }
 }
Esempio n. 3
0
 public void Update()
 {
     if (keys_new != null)
     {
         keys_old = keys_new;
     }
     keys_new = keyboard.GetCurrentState();
 }
Esempio n. 4
0
        public void TestLineManager3D()
        {
            var keyboard = new SlimDXTWKeyboard();
            var dev      = new SlimDX.DirectInput.DirectInput();
            var kb       = new SlimDX.DirectInput.Keyboard(dev);

            kb.Acquire();

            var mouse = new TWMouse();
            var m     = new SlimDX.DirectInput.Mouse(dev);

            m.Acquire();



            var game = new DX11Form();

            game.InitDirectX();
            var device = game.Device;

            var rasterizerState = RasterizerState.FromDescription(device, new RasterizerStateDescription()
            {
                CullMode = CullMode.None,
                FillMode = FillMode.Solid
            });

            device.ImmediateContext.Rasterizer.State = rasterizerState;


            var cam = new SpectaterCamera();

            var lineManager = new LineManager3D(device);

            game.GameLoopEvent += delegate
            {
                mouse.UpdateMouseState(m.GetCurrentState());
                keyboard.UpdateKeyboardState(kb.GetCurrentState());
                cam.Update(0.001f, keyboard, mouse);

                for (int num = 0; num < 200; num++)
                {
                    lineManager.AddLine(
                        new Vector3(-12.0f + num / 4.0f, 13.0f, 0),
                        new Vector3(-17.0f + num / 4.0f, -13.0f, 0),
                        new Color4((byte)(255 - num) / 255f, 14 / 255f, (byte)num / 255f));
                } // for

                lineManager.DrawGroundShadows = true;
                lineManager.AddCenteredBox(new Vector3(4, 4, 4), 2, new Color4(1, 0, 0));

                lineManager.WorldMatrix =
                    Matrix.Translation(MathHelper.Up * 30);

                for (int num = 0; num < 200; num++)
                {
                    lineManager.AddLine(
                        new Vector3(-12.0f + num / 4.0f, 13.0f, 0),
                        new Vector3(-17.0f + num / 4.0f, -13.0f, 0),
                        new Color4((byte)(255 - num) / 255f, 14 / 255f, (byte)num / 255f));
                } // for



                lineManager.Render(cam);
                if (keyboard.IsKeyDown(Key.Escape))
                {
                    game.Exit();
                }
            };

            game.Run();

            rasterizerState.Dispose();
            kb.Dispose();
            m.Dispose();
            dev.Dispose();
        }
Esempio n. 5
0
        public void TestDirectX11SpecaterCamera()
        {
            var keyboard = new SlimDXTWKeyboard();
            var dev      = new SlimDX.DirectInput.DirectInput();
            var kb       = new SlimDX.DirectInput.Keyboard(dev);

            kb.Acquire();

            var mouse = new TWMouse();
            var m     = new SlimDX.DirectInput.Mouse(dev);

            m.Acquire();



            var game = new DX11Form();

            game.InitDirectX();
            var device    = game.Device;
            var bytecode  = ShaderBytecode.CompileFromFile("../../Common.Core/Shaders/MiniTri.fx", "fx_5_0", ShaderFlags.None, EffectFlags.None);
            var effect    = new Effect(device, bytecode);
            var technique = effect.GetTechniqueByName("RenderTransform");
            var pass      = technique.GetPassByIndex(0);
            var layout    = new InputLayout(device, pass.Description.Signature,
                                            new[] {
                new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0),
                new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 16, 0),
                new InputElement("TEXCOORD", 0, Format.R32G32_Float, 32, 0)
            });

            var vertexStride = (16 + 16 + 8);
            var stream       = new DataStream(3 * vertexStride, true, true);

            stream.WriteRange(new[] {
                new VertexCustom(new Vector4(-1.0f, 0, 0, 1.0f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f), new Vector2(0.5f, 0)),
                new VertexCustom(new Vector4(0f, 1f, 0, 1.0f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f), new Vector2(0f, 1f)),
                new VertexCustom(new Vector4(1f, 0f, 0, 1.0f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f), new Vector2(1f, 1f))
            });
            stream.Position = 0;



            var vertices = new SlimDX.Direct3D11.Buffer(device, stream, new BufferDescription()
            {
                BindFlags      = BindFlags.VertexBuffer,
                CpuAccessFlags = CpuAccessFlags.None,
                OptionFlags    = ResourceOptionFlags.None,
                SizeInBytes    = 3 * vertexStride,
                Usage          = ResourceUsage.Default
            });

            stream.Dispose();

            //var world = Matrix.Translation(MathHelper.Forward);

            /*var viewProjection = Matrix.LookAtRH(Vector3.UnitZ * 5, -Vector3.UnitZ, MathHelper.Up)
             * Matrix.PerspectiveFovRH(MathHelper.PiOver4, 4f / 3f, 0.1f, 1000f);*/


            var diffuseShaderVariable = effect.GetVariableByName("txDiffuse").AsResource();
            var worldParam            = effect.GetVariableByName("world").AsMatrix();
            //worldParam.AsMatrix().SetMatrix(world);


            var viewProjParam = effect.GetVariableBySemantic("viewprojection").AsMatrix();

            /*viewProjParam.SetMatrix(
             *  viewProjection);*/

            //worldParam.SetMatrix(Matrix.Identity);
            //effect.GetVariableBySemantic("viewprojection").AsMatrix().SetMatrix(Matrix.Identity);

            var texturePath = Wallpaper001_png;

            var diffuseTexture = Texture2D.FromFile(device, texturePath);

            var diffuseTextureRv = new ShaderResourceView(device, diffuseTexture);


            diffuseShaderVariable.SetResource(diffuseTextureRv);


            var rasterizerState = RasterizerState.FromDescription(device, new RasterizerStateDescription()
            {
                CullMode = CullMode.None,
                FillMode = FillMode.Solid
            });

            device.ImmediateContext.Rasterizer.State = rasterizerState;


            var cam = new SpectaterCamera();

            game.GameLoopEvent += delegate
            {
                mouse.UpdateMouseState(m.GetCurrentState());
                keyboard.UpdateKeyboardState(kb.GetCurrentState());
                cam.Update(0.001f, keyboard, mouse);

                device.ImmediateContext.InputAssembler.InputLayout       = layout;
                device.ImmediateContext.InputAssembler.PrimitiveTopology =
                    PrimitiveTopology.TriangleList;
                device.ImmediateContext.InputAssembler.SetVertexBuffers(0,
                                                                        new VertexBufferBinding
                                                                            (vertices,
                                                                            vertexStride, 0));

                for (int i = 0; i < technique.Description.PassCount; ++i)
                {
                    pass.Apply(device.ImmediateContext);
                    device.ImmediateContext.Draw(3, 0);
                }

                viewProjParam.SetMatrix(cam.ViewProjection);
                worldParam.SetMatrix(Matrix.Identity);
                if (keyboard.IsKeyDown(Key.Escape))
                {
                    game.Exit();
                }
            };

            game.Run();

            bytecode.Dispose();
            effect.Dispose();
            layout.Dispose();
            vertices.Dispose();
            diffuseTexture.Dispose();
            diffuseTextureRv.Dispose();
            rasterizerState.Dispose();
            kb.Dispose();
            m.Dispose();
            dev.Dispose();
        }
Esempio n. 6
0
        /// <summary>
        /// Callback, получающий информацию об изменениях в состоянии контролов джойстика
        /// </summary>
        private void GetDeviceData()
        {
            while (true)
            {
                try
                {
                    lock (_threadLock)
                    {
                        if (_quitThread)
                        {
                            break;
                        }
                    }
                    if (!_event.WaitOne(30))
                    {
                        continue;
                    }
                    _device.Poll();
                    var state = _device.GetCurrentState();

                    lock (_dumpObjectLocker)
                    {
                        var modifiers = 0;
                        if (state.IsPressed(Key.LeftShift) || state.IsPressed(Key.RightShift))
                        {
                            modifiers |= (int)Modifiers.Shift;
                        }
                        if (state.IsPressed(Key.LeftAlt) || state.IsPressed(Key.RightAlt))
                        {
                            modifiers |= (int)Modifiers.Alt;
                        }
                        if (state.IsPressed(Key.LeftControl) || state.IsPressed(Key.RightControl))
                        {
                            modifiers |= (int)Modifiers.Control;
                        }

                        for (var i = 0; i < _buttons.Length; i++)
                        {
                            if (state.IsPressed((Key)i))
                            {
                                // Если кнопка нажата и модификаторы не изменились и не нужно дампить - не запоминаем
                                if (_buttons[i].IsPressed && modifiers == _buttons[i].ModifiersMask && _dumpMode == false)
                                {
                                    continue;
                                }
                            }
                            else
                            {
                                // Если кнопка нажата и модификаторы не изменились и не нужно дампить - не запоминаем
                                if (!_buttons[i].IsPressed && _dumpMode == false)
                                {
                                    continue;
                                }
                            }
                            _buttons[i].ModifiersMask = modifiers;
                            _buttons[i].IsPressed     = state.IsPressed((Key)i);
                            var ev = new ButtonEvent
                            {
                                Hardware =
                                    new ControlProcessorHardware
                                {
                                    ModuleType    = HardwareModuleType.Button,
                                    MotherBoardId = GetDeviceGuid(),
                                    ModuleId      = (uint)_buttons[i].ModifiersMask,
                                    ControlId     = (uint)i
                                },
                                IsPressed = _buttons[i].IsPressed
                            };
                            _incomingEvents.Enqueue(ev);
                        }
                        _dumpMode = false;
                    }
                }
                catch (Exception ex)
                {
                }
            }
        }
        /// <summary>Initialises the keyboard.</summary>
        /// <param name="control">The control to associate with DirectInput.</param>
        private void InitialiseKeyboard(WF.Control control)
        {
            // From idlibc.c
            _keyToScanCode = new int[256];
            _keyToScanCode[(int) DI.Key.Escape] = 0x01;
            _keyToScanCode[(int) DI.Key.Space] = 0x39;
            _keyToScanCode[(int) DI.Key.Backspace] = 0x0e;
            _keyToScanCode[(int) DI.Key.Tab] = 0x0f;
            _keyToScanCode[(int) DI.Key.LeftControl] = 0x1d;
            _keyToScanCode[(int) DI.Key.LeftShift] = 0x2a;
            _keyToScanCode[(int) DI.Key.CapsLock] = 0x3a;
            _keyToScanCode[(int) DI.Key.F1] = 0x3b;
            _keyToScanCode[(int) DI.Key.F2] = 0x3c;
            _keyToScanCode[(int) DI.Key.F3] = 0x3d;
            _keyToScanCode[(int) DI.Key.F4] = 0x3e;
            _keyToScanCode[(int) DI.Key.F5] = 0x3f;
            _keyToScanCode[(int) DI.Key.F6] = 0x40;
            _keyToScanCode[(int) DI.Key.F7] = 0x41;
            _keyToScanCode[(int) DI.Key.F8] = 0x42;
            _keyToScanCode[(int) DI.Key.F9] = 0x43;
            _keyToScanCode[(int) DI.Key.F10] = 0x44;
            _keyToScanCode[(int) DI.Key.F11] = 0x57;
            _keyToScanCode[(int) DI.Key.F12] = 0x58;
            _keyToScanCode[(int) DI.Key.ScrollLock] = 0x46;
            _keyToScanCode[(int) DI.Key.Return] = 0x1c;
            _keyToScanCode[(int) DI.Key.RightShift] = 0x36;
            _keyToScanCode[(int) DI.Key.PrintScreen] = 0x37;
            _keyToScanCode[(int) DI.Key.LeftAlt] = 0x38;
            _keyToScanCode[(int) DI.Key.Home] = 0x47;
            _keyToScanCode[(int) DI.Key.PageUp] = 0x49;
            _keyToScanCode[(int) DI.Key.End] = 0x4f;
            _keyToScanCode[(int) DI.Key.PageDown] = 0x51;
            _keyToScanCode[(int) DI.Key.Insert] = 0x52;
            _keyToScanCode[(int) DI.Key.Delete] = 0x53;
            _keyToScanCode[(int) DI.Key.NumberLock] = 0x45;
            _keyToScanCode[(int) DI.Key.UpArrow] = 0x48;
            _keyToScanCode[(int) DI.Key.DownArrow] = 0x50;
            _keyToScanCode[(int) DI.Key.LeftArrow] = 0x4b;
            _keyToScanCode[(int) DI.Key.RightArrow] = 0x4d;
            _keyToScanCode[(int) DI.Key.D1] = 0x02;
            _keyToScanCode[(int) DI.Key.D2] = 0x03;
            _keyToScanCode[(int) DI.Key.D3] = 0x04;
            _keyToScanCode[(int) DI.Key.D4] = 0x05;
            _keyToScanCode[(int) DI.Key.D5] = 0x06;
            _keyToScanCode[(int) DI.Key.D6] = 0x07;
            _keyToScanCode[(int) DI.Key.D7] = 0x08;
            _keyToScanCode[(int) DI.Key.D8] = 0x09;
            _keyToScanCode[(int) DI.Key.D9] = 0x0a;
            _keyToScanCode[(int) DI.Key.D0] = 0x0b;
            _keyToScanCode[(int) DI.Key.Minus] = 0x0c;
            _keyToScanCode[(int) DI.Key.Equals] = 0x0d;
            _keyToScanCode[(int) DI.Key.Q] = 0x10;
            _keyToScanCode[(int) DI.Key.W] = 0x11;
            _keyToScanCode[(int) DI.Key.E] = 0x12;
            _keyToScanCode[(int) DI.Key.R] = 0x13;
            _keyToScanCode[(int) DI.Key.T] = 0x14;
            _keyToScanCode[(int) DI.Key.Y] = 0x15;
            _keyToScanCode[(int) DI.Key.U] = 0x16;
            _keyToScanCode[(int) DI.Key.I] = 0x17;
            _keyToScanCode[(int) DI.Key.O] = 0x18;
            _keyToScanCode[(int) DI.Key.P] = 0x19;
            _keyToScanCode[(int) DI.Key.LeftBracket] = 0x1a;
            _keyToScanCode[(int) DI.Key.RightBracket] = 0x1b;
            //_keyToScanCode[(int) DI.Key.OemPipe] = 0x1c; // TODO
            _keyToScanCode[(int) DI.Key.A] = 0x1e;
            _keyToScanCode[(int) DI.Key.S] = 0x1f;
            _keyToScanCode[(int) DI.Key.D] = 0x20;
            _keyToScanCode[(int) DI.Key.F] = 0x21;
            _keyToScanCode[(int) DI.Key.G] = 0x22;
            _keyToScanCode[(int) DI.Key.H] = 0x23;
            _keyToScanCode[(int) DI.Key.J] = 0x24;
            _keyToScanCode[(int) DI.Key.K] = 0x25;
            _keyToScanCode[(int) DI.Key.L] = 0x26;
            _keyToScanCode[(int) DI.Key.Semicolon] = 0x27;
            //_keyToScanCode[(int) DI.Key.OemQuotes] = 0x28; // TODO
            _keyToScanCode[(int) DI.Key.Z] = 0x2c;
            _keyToScanCode[(int) DI.Key.X] = 0x2d;
            _keyToScanCode[(int) DI.Key.C] = 0x2e;
            _keyToScanCode[(int) DI.Key.V] = 0x2f;
            _keyToScanCode[(int) DI.Key.B] = 0x30;
            _keyToScanCode[(int) DI.Key.N] = 0x31;
            _keyToScanCode[(int) DI.Key.M] = 0x32;
            _keyToScanCode[(int) DI.Key.Comma] = 0x33;
            _keyToScanCode[(int) DI.Key.Period] = 0x34;
            // see idlibc.chartable for other keys

            _keyboard = new DI.Keyboard(_directInput);
            _keyboard.SetCooperativeLevel(control, DI.CooperativeLevel.Nonexclusive | DI.CooperativeLevel.Background);
            _keyboard.Acquire();

            _keyboardState = _keyboard.GetCurrentState();
        }