Example #1
0
        internal void BuildHatFeature(ref InputControlLayout.Builder builder, SDLFeatureDescriptor xFeature, SDLFeatureDescriptor yFeature)
        {
            string xFeatureName = SDLSupport.GetAxisNameFromUsage((SDLAxisUsage)xFeature.usageHint);
            string yFeatureName = SDLSupport.GetAxisNameFromUsage((SDLAxisUsage)yFeature.usageHint);
            var    hat          = HatNumber(xFeature);
            var    hatName      = (hat > 1) ? $"Hat{hat}" : "Hat";

            var control = builder.AddControl(hatName)
                          .WithLayout("Dpad")
                          .WithByteOffset((uint)xFeature.offset)
                          .WithSizeInBits((uint)xFeature.size * 8)
                          .WithUsages(new InternedString[] { CommonUsages.Hatswitch });

            builder.AddControl(hatName + "/up")
            .WithFormat(InputStateBlock.kTypeInt)
            .WithLayout("Button")
            .WithParameters("scale,scaleFactor=2147483647,clamp,clampMin=-1,clampMax=0,invert")
            .WithByteOffset(4)
            .WithSizeInBits((uint)yFeature.size * 8);

            builder.AddControl(hatName + "/down")
            .WithFormat(InputStateBlock.kTypeInt)
            .WithLayout("Button")
            .WithParameters("scale,scaleFactor=2147483647,clamp,clampMin=0,clampMax=1")
            .WithByteOffset(4)
            .WithSizeInBits((uint)yFeature.size * 8);

            builder.AddControl(hatName + "/left")
            .WithFormat(InputStateBlock.kTypeInt)
            .WithLayout("Button")
            .WithParameters("scale,scaleFactor=2147483647,clamp,clampMin=-1,clampMax=0,invert")
            .WithByteOffset(0)
            .WithSizeInBits((uint)xFeature.size * 8);

            builder.AddControl(hatName + "/right")
            .WithFormat(InputStateBlock.kTypeInt)
            .WithLayout("Button")
            .WithParameters("scale,scaleFactor=2147483647,clamp,clampMin=0,clampMax=1")
            .WithByteOffset(0)
            .WithSizeInBits((uint)xFeature.size * 8);

            builder.AddControl(hatName + "/x")
            .WithFormat(InputStateBlock.kTypeInt)
            .WithLayout("Analog")
            .WithParameters("scale,scaleFactor=2147483647,clamp,clampMin=0,clampMax=1")
            .WithByteOffset(0)
            .WithSizeInBits((uint)xFeature.size * 8);

            builder.AddControl(hatName + "/y")
            .WithFormat(InputStateBlock.kTypeInt)
            .WithLayout("Analog")
            .WithParameters("scale,scaleFactor=2147483647,clamp,clampMin=-1,clampMax=1,invert")
            .WithByteOffset(4)
            .WithSizeInBits((uint)yFeature.size * 8);
        }
Example #2
0
        internal InputControlLayout Build()
        {
            var builder = new InputControlLayout.Builder
            {
                stateFormat   = new FourCC('L', 'J', 'O', 'Y'),
                extendsLayout = parentLayout
            };

            for (var i = 0; i < descriptor.controls.Count; i++)
            {
                SDLFeatureDescriptor feature = descriptor.controls[i];
                switch (feature.featureType)
                {
                case JoystickFeatureType.Axis:
                {
                    SDLAxisUsage usage       = (SDLAxisUsage)feature.usageHint;
                    string       featureName = SDLSupport.GetAxisNameFromUsage(usage);
                    string       parameters  = "scale,scaleFactor=65538,clamp,clampMin=-1,clampMax=1";

                    if (IsAxisX(feature) && i + 1 < descriptor.controls.Count)
                    {
                        SDLFeatureDescriptor nextFeature = descriptor.controls[i + 1];
                        if (IsAxisY(nextFeature))
                        {
                            BuildStickFeature(ref builder, feature, nextFeature);
                        }
                    }

                    if (IsAxisY(feature))
                    {
                        parameters += ",invert";
                    }

                    builder.AddControl(featureName)
                    .WithLayout("Analog")
                    .WithByteOffset((uint)feature.offset)
                    .WithFormat(InputStateBlock.kTypeInt)
                    .WithParameters(parameters);
                }
                break;

                case JoystickFeatureType.Ball:
                {
                    //TODO
                }
                break;

                case JoystickFeatureType.Button:
                {
                    SDLButtonUsage usage       = (SDLButtonUsage)feature.usageHint;
                    string         featureName = SDLSupport.GetButtonNameFromUsage(usage);
                    if (featureName != null)
                    {
                        builder.AddControl(featureName)
                        .WithLayout("Button")
                        .WithByteOffset((uint)feature.offset)
                        .WithBitOffset((uint)feature.bit)
                        .WithFormat(InputStateBlock.kTypeBit);
                    }
                }
                break;

                case JoystickFeatureType.Hat:
                {
                    SDLAxisUsage usage       = (SDLAxisUsage)feature.usageHint;
                    string       featureName = SDLSupport.GetAxisNameFromUsage(usage);
                    string       parameters  = "scale,scaleFactor=2147483647,clamp,clampMin=-1,clampMax=1";

                    if (i + 1 < descriptor.controls.Count)
                    {
                        SDLFeatureDescriptor nextFeature = descriptor.controls[i + 1];
                        if (IsHatY(nextFeature) && HatNumber(feature) == HatNumber(nextFeature))
                        {
                            BuildHatFeature(ref builder, feature, nextFeature);
                        }
                    }

                    if (IsHatY(feature))
                    {
                        parameters += ",invert";
                    }

                    builder.AddControl(featureName)
                    .WithLayout("Analog")
                    .WithByteOffset((uint)feature.offset)
                    .WithFormat(InputStateBlock.kTypeInt)
                    .WithParameters(parameters);
                }
                break;

                default:
                {
                    throw new NotImplementedException(String.Format("SDLLayoutBuilder.Build: Trying to build an SDL device with an unknown feature of type {0}.", feature.featureType));
                }
                }
            }

            return(builder.Build());
        }