Ejemplo n.º 1
0
            public void AddInputWithSampleMouseAxisMappings(out InputComponent inputComponent, out AxisMapping lookRight, out AxisMapping lookUp)
            {
                lookRight = new AxisMapping {
                    AxisName = nameof(lookRight)
                };
                lookRight.HardwareAxes.Add(new HardwareAxis
                {
                    HardwareInputVariant = HardwareInputVariant.CreateMouseVariant(HardwareInputVariant.MouseVariant.AxisX),
                    Scale = 1.0
                });

                lookUp = new AxisMapping {
                    AxisName = nameof(lookUp)
                };
                lookUp.HardwareAxes.Add(new HardwareAxis
                {
                    HardwareInputVariant = HardwareInputVariant.CreateMouseVariant(HardwareInputVariant.MouseVariant.AxisY),
                    Scale = 1.0
                });

                var inputMapping = new InputMapping();

                inputMapping.AxisMappings.Add(lookUp);
                inputMapping.AxisMappings.Add(lookRight);

                inputComponent = new InputComponent {
                    InputMapping = inputMapping
                };

                var entity = new Entity();

                entity.AddComponent(inputComponent);

                _scene.AddEntity(entity);
            }
        public void AsKeyboard_ShouldThrowException_WhenVariantIsNotKeyboard()
        {
            // Arrange
            var hardwareInputVariant = HardwareInputVariant.CreateMouseVariant(HardwareInputVariant.MouseVariant.LeftButton);

            // Act
            // Assert
            Assert.That(() => hardwareInputVariant.AsKeyboard(), Throws.TypeOf <InvalidOperationForCurrentVariantException>());
        }
Ejemplo n.º 3
0
            public void AddInputWithSampleMouseActionMappings(out InputComponent inputComponent, out ActionMapping fire, out ActionMapping zoom,
                                                              out ActionMapping altFire, out ActionMapping melee)
            {
                fire = new ActionMapping {
                    ActionName = nameof(fire)
                };
                fire.HardwareActions.Add(new HardwareAction
                {
                    HardwareInputVariant = HardwareInputVariant.CreateMouseVariant(HardwareInputVariant.MouseVariant.LeftButton)
                });

                zoom = new ActionMapping {
                    ActionName = nameof(zoom)
                };
                zoom.HardwareActions.Add(new HardwareAction
                {
                    HardwareInputVariant = HardwareInputVariant.CreateMouseVariant(HardwareInputVariant.MouseVariant.MiddleButton)
                });

                altFire = new ActionMapping {
                    ActionName = nameof(altFire)
                };
                altFire.HardwareActions.Add(new HardwareAction
                {
                    HardwareInputVariant = HardwareInputVariant.CreateMouseVariant(HardwareInputVariant.MouseVariant.RightButton)
                });

                melee = new ActionMapping {
                    ActionName = nameof(melee)
                };
                melee.HardwareActions.Add(new HardwareAction
                {
                    HardwareInputVariant = HardwareInputVariant.CreateMouseVariant(HardwareInputVariant.MouseVariant.XButton1)
                });
                melee.HardwareActions.Add(new HardwareAction
                {
                    HardwareInputVariant = HardwareInputVariant.CreateMouseVariant(HardwareInputVariant.MouseVariant.XButton2)
                });

                var inputMapping = new InputMapping();

                inputMapping.ActionMappings.Add(fire);
                inputMapping.ActionMappings.Add(zoom);
                inputMapping.ActionMappings.Add(altFire);
                inputMapping.ActionMappings.Add(melee);

                inputComponent = new InputComponent {
                    InputMapping = inputMapping
                };

                var entity = new Entity();

                entity.AddComponent(inputComponent);

                _scene.AddEntity(entity);
            }
        public void AsMouse_ShouldReturnMouseVariant_WhenVariantIsMouse()
        {
            // Arrange
            const HardwareInputVariant.MouseVariant mouseVariant = HardwareInputVariant.MouseVariant.LeftButton;
            var hardwareInputVariant = HardwareInputVariant.CreateMouseVariant(mouseVariant);

            // Act
            // Assert
            Assert.That(hardwareInputVariant.AsMouse(), Is.EqualTo(mouseVariant));
        }
        public void CreateMouseVariant_ShouldSetCurrentVariantToMouse()
        {
            // Arrange

            // Act
            var hardwareInputVariant = HardwareInputVariant.CreateMouseVariant(HardwareInputVariant.MouseVariant.LeftButton);

            // Assert
            Assert.That(hardwareInputVariant.CurrentVariant, Is.EqualTo(HardwareInputVariant.Variant.Mouse));
        }
Ejemplo n.º 6
0
        public void GetAsset_ShouldLoadAndReturn_InputMapping()
        {
            // Arrange
            var assetsDirectoryPath = Utils.GetPathUnderTestDirectory("Assets");

            SystemUnderTest.AssetStore.RegisterAssets(assetsDirectoryPath);

            // Act
            var inputMapping = SystemUnderTest.AssetStore.GetAsset <InputMapping>(AssetsIds.TestInputMapping);

            // Assert
            Assert.That(inputMapping, Is.Not.Null);

            Assert.That(inputMapping.ActionMappings.Count, Is.EqualTo(2));
            // Action mapping
            Assert.That(inputMapping.ActionMappings[0].ActionName, Is.EqualTo("Jump"));
            Assert.That(inputMapping.ActionMappings[0].HardwareActions.Count, Is.EqualTo(1));
            Assert.That(inputMapping.ActionMappings[0].HardwareActions[0].HardwareInputVariant,
                        Is.EqualTo(HardwareInputVariant.CreateKeyboardVariant(Key.Space)));

            Assert.That(inputMapping.ActionMappings[1].ActionName, Is.EqualTo("Fire"));
            Assert.That(inputMapping.ActionMappings[1].HardwareActions.Count, Is.EqualTo(1));
            Assert.That(inputMapping.ActionMappings[1].HardwareActions[0].HardwareInputVariant,
                        Is.EqualTo(HardwareInputVariant.CreateMouseVariant(HardwareInputVariant.MouseVariant.LeftButton)));

            Assert.That(inputMapping.AxisMappings.Count, Is.EqualTo(4));
            // Axis mapping 1
            Assert.That(inputMapping.AxisMappings[0].AxisName, Is.EqualTo("MoveForward"));
            Assert.That(inputMapping.AxisMappings[0].HardwareAxes.Count, Is.EqualTo(2));
            Assert.That(inputMapping.AxisMappings[0].HardwareAxes[0].HardwareInputVariant, Is.EqualTo(HardwareInputVariant.CreateKeyboardVariant(Key.W)));
            Assert.That(inputMapping.AxisMappings[0].HardwareAxes[0].Scale, Is.EqualTo(1));
            Assert.That(inputMapping.AxisMappings[0].HardwareAxes[1].HardwareInputVariant, Is.EqualTo(HardwareInputVariant.CreateKeyboardVariant(Key.S)));
            Assert.That(inputMapping.AxisMappings[0].HardwareAxes[1].Scale, Is.EqualTo(-1));
            // Axis mapping 2
            Assert.That(inputMapping.AxisMappings[1].AxisName, Is.EqualTo("MoveRight"));
            Assert.That(inputMapping.AxisMappings[1].HardwareAxes.Count, Is.EqualTo(2));
            Assert.That(inputMapping.AxisMappings[1].HardwareAxes[0].HardwareInputVariant, Is.EqualTo(HardwareInputVariant.CreateKeyboardVariant(Key.D)));
            Assert.That(inputMapping.AxisMappings[1].HardwareAxes[0].Scale, Is.EqualTo(1));
            Assert.That(inputMapping.AxisMappings[1].HardwareAxes[1].HardwareInputVariant, Is.EqualTo(HardwareInputVariant.CreateKeyboardVariant(Key.A)));
            Assert.That(inputMapping.AxisMappings[1].HardwareAxes[1].Scale, Is.EqualTo(-1));
            // Axis mapping 3
            Assert.That(inputMapping.AxisMappings[2].AxisName, Is.EqualTo("LookUp"));
            Assert.That(inputMapping.AxisMappings[2].HardwareAxes.Count, Is.EqualTo(1));
            Assert.That(inputMapping.AxisMappings[2].HardwareAxes[0].HardwareInputVariant,
                        Is.EqualTo(HardwareInputVariant.CreateMouseVariant(HardwareInputVariant.MouseVariant.AxisX)));
            Assert.That(inputMapping.AxisMappings[2].HardwareAxes[0].Scale, Is.EqualTo(1));
            // Axis mapping 4
            Assert.That(inputMapping.AxisMappings[3].AxisName, Is.EqualTo("LookRight"));
            Assert.That(inputMapping.AxisMappings[3].HardwareAxes.Count, Is.EqualTo(1));
            Assert.That(inputMapping.AxisMappings[3].HardwareAxes[0].HardwareInputVariant,
                        Is.EqualTo(HardwareInputVariant.CreateMouseVariant(HardwareInputVariant.MouseVariant.AxisY)));
            Assert.That(inputMapping.AxisMappings[3].HardwareAxes[0].Scale, Is.EqualTo(1));
        }
Ejemplo n.º 7
0
        public object LoadAsset(AssetInfo assetInfo, IAssetStore assetStore)
        {
            using var fileStream = _fileSystem.GetFile(assetInfo.AssetFilePath).OpenRead();
            var assetData = AssetData.Load(fileStream);
            var inputMappingAssetContent = assetData.ReadJsonContent <InputMappingAssetContent>();

            if (inputMappingAssetContent.ActionMappings == null)
            {
                throw new InvalidOperationException($"{nameof(InputMappingAssetContent)}.{nameof(InputMappingAssetContent.ActionMappings)} cannot be null.");
            }
            if (inputMappingAssetContent.AxisMappings == null)
            {
                throw new InvalidOperationException($"{nameof(InputMappingAssetContent)}.{nameof(InputMappingAssetContent.AxisMappings)} cannot be null.");
            }

            var inputMapping = new InputMapping();

            foreach (var(actionName, serializableHardwareActions) in inputMappingAssetContent.ActionMappings)
            {
                var actionMapping = new ActionMapping
                {
                    ActionName = actionName
                };

                foreach (var serializableHardwareAction in serializableHardwareActions)
                {
                    if (serializableHardwareAction.Key != null && serializableHardwareAction.MouseButton == null)
                    {
                        actionMapping.HardwareActions.Add(new HardwareAction
                        {
                            HardwareInputVariant = HardwareInputVariant.CreateKeyboardVariant(serializableHardwareAction.Key.Value)
                        });
                    }
                    else if (serializableHardwareAction.Key == null && serializableHardwareAction.MouseButton != null)
                    {
                        var mouseVariant = serializableHardwareAction.MouseButton switch
                        {
                            MouseButton.LeftButton => HardwareInputVariant.MouseVariant.LeftButton,
                            MouseButton.MiddleButton => HardwareInputVariant.MouseVariant.MiddleButton,
                            MouseButton.RightButton => HardwareInputVariant.MouseVariant.RightButton,
                            MouseButton.XButton1 => HardwareInputVariant.MouseVariant.XButton1,
                            MouseButton.XButton2 => HardwareInputVariant.MouseVariant.XButton2,
                            _ => throw new ArgumentOutOfRangeException(nameof(SerializableHardwareAction.MouseButton), serializableHardwareAction.MouseButton,
                                                                       "Unsupported mouse button found in input mapping.")
                        };

                        actionMapping.HardwareActions.Add(new HardwareAction
                        {
                            HardwareInputVariant = HardwareInputVariant.CreateMouseVariant(mouseVariant)
                        });
                    }
                    else
                    {
                        throw InvalidInputMappingAssetContentException.CreateForInvalidHardwareAction(inputMappingAssetContent);
                    }
                }

                inputMapping.ActionMappings.Add(actionMapping);
            }

            foreach (var(axisName, serializableHardwareAxes) in inputMappingAssetContent.AxisMappings)
            {
                var axisMapping = new AxisMapping
                {
                    AxisName = axisName
                };

                foreach (var serializableHardwareAxis in serializableHardwareAxes)
                {
                    if (serializableHardwareAxis.Key != null && serializableHardwareAxis.MouseAxis == null)
                    {
                        axisMapping.HardwareAxes.Add(new HardwareAxis
                        {
                            HardwareInputVariant = HardwareInputVariant.CreateKeyboardVariant(serializableHardwareAxis.Key.Value),
                            Scale = serializableHardwareAxis.Scale
                        });
                    }
                    else if (serializableHardwareAxis.Key == null && serializableHardwareAxis.MouseAxis != null)
                    {
                        var mouseVariant = serializableHardwareAxis.MouseAxis switch
                        {
                            MouseAxis.AxisX => HardwareInputVariant.MouseVariant.AxisX,
                            MouseAxis.AxisY => HardwareInputVariant.MouseVariant.AxisY,
                            _ => throw new ArgumentOutOfRangeException(nameof(SerializableHardwareAxis.MouseAxis), serializableHardwareAxis.MouseAxis,
                                                                       "Unsupported mouse axis found in input mapping.")
                        };

                        axisMapping.HardwareAxes.Add(new HardwareAxis
                        {
                            HardwareInputVariant = HardwareInputVariant.CreateMouseVariant(mouseVariant),
                            Scale = serializableHardwareAxis.Scale
                        });
                    }
                    else
                    {
                        throw InvalidInputMappingAssetContentException.CreateForInvalidHardwareAxis(inputMappingAssetContent);
                    }
                }

                inputMapping.AxisMappings.Add(axisMapping);
            }

            return(inputMapping);
        }