GUI – GamePad Thumb Stick Controls

At some point in your game development you might find yourself needing thumbstick controls in the GUI (start screen/main menu). Thumb stick control is normally easy to setup using .txscenes and components, but in the GUI its a bit tricky.

This example is for basic up down menu control from the left thumbstick (up & left = Move Up; Down & Right = Move Down). Place this code right after “public GuiMainMenu() {….}” or whatever GUI your using.

        public override bool OnInputEvent(ref TorqueInputDevice.InputEventData data)
        {
            if (base.OnInputEvent(ref data))
            {
                return true;
            }

            // look for events from the gamepad
            if (data.DeviceTypeId == TorqueInputDevice.GamePadId)
            {
                // look for Left Thumb input events on the X axis
                if (data.ObjectId == (int)XGamePadDevice.GamePadObjects.LeftThumbX)
                {
                    if (_moveX)
                    {
                        if (data.Value < -.6f)
                        {
                            MoveUp();
                            _moveX = false;
                            return true;
                        }
                        else if (data.Value > .6f)
                        {
                            MoveDown();
                            _moveX = false;
                            return true;
                        }
                    }
                    if (data.Value < .2f && data.Value > -.2f)
                    {
                        _moveX = true;
                        return true;
                    }

                }

                // look for Left Thumb input events on the Y axis
                if (data.ObjectId == (int)XGamePadDevice.GamePadObjects.LeftThumbY)
                {
                    if (_moveY)
                    {
                        if (data.Value < -.6f)
                        {
                            MoveDown();
                            _moveY = false;
                            return true;
                        }
                        else if (data.Value > .6f)
                        {
                            MoveUp();
                            _moveY = false;
                            return true;
                        }
                    }
                    if (data.Value < .2f && data.Value > -.2f)
                    {
                        _moveY = true;
                        return true;
                    }
                }
            }
            return false;
        }

Make sure to define _moveX and _moveY

        bool _moveX = true;
        bool _moveY = true;
Powered by WordPress | Designed by: Download Free WordPress Themes | Thanks to wordpress themes 2012, Themes Gallery and Best Free WordPress Themes