private void SwitchWeapon(ref IMyHandheldGunObject<MyDeviceBase> weapon, ref MyVRWeaponInfo weaponInfo, List<MyVRWeaponInfo> weapons, ControllerRole role, Vector2? touchpadPos)
        {
            int currentIndex = -1;

            if (weapon != null)
            {
                for (int i = 0; i < weapons.Count; ++i)
                {
                    var info = weapons[i];
                    if (info.DefinitionId == weapon.DefinitionId)
                    {
                        currentIndex = i;
                        break;
                    }
                }
            }

            int nextIndex = 0;
            if (touchpadPos != null)
            {
                Vector2 pos = touchpadPos.Value;
                if (touchpadPos != Vector2.Zero)
                {
                    pos.Normalize();
                    float anglePerWeaponSector = 360f / weapons.Count;
                    float dot = Vector2.Dot(Vector2.UnitX, pos);
                    float angle = (float)Math.Acos(Math.Abs(dot));
                    if (pos.Y >= 0)
                    {
                        if (dot < 0)
                            angle = 180 - angle;
                    }
                    else
                    {
                        if (dot < 0)
                            angle = 180 + angle;
                        else
                            angle = 360 - angle;
                    }

                    nextIndex = (int)Math.Floor(angle / anglePerWeaponSector);
                    Debug.Assert(nextIndex < weapons.Count);
                }
            }

            if (nextIndex == currentIndex)
                return;

            if (weapon != null)
            {
                weapon.OnControlReleased();
                MyEntity weaponEntity = (MyEntity)weapon;
                weaponEntity.Close();
                weapon = null;
            }

            {
                weaponInfo = weapons[nextIndex];
                weapon = CreateWeapon(weaponInfo.DefinitionId, weaponInfo.Reference);
                weapon.OnControlAcquired(null);
                MyEntity weaponEntity = (MyEntity)weapon;
            }

            var gunBase = weapon.GunBase as MyGunBase;
            Matrix holdingMatrix = gunBase != null ? gunBase.m_holdingDummyMatrix : Matrix.Identity;
            MyOpenVR.LMUAdd(holdingMatrix, m_worldMatrixOriginal, role, weaponInfo.Reference);
        }
        void HandleButtons(ref IMyHandheldGunObject<MyDeviceBase> weapon, ref MyVRWeaponInfo weaponInfo, bool secondController, List<MyVRWeaponInfo> weapons)
        {
            if (weapon == null)
                return;

            MyWeaponSharedActionsComponentBase sharedWeaponActionsComponent = null;
            ((MyEntity)weapon).Components.TryGet<MyWeaponSharedActionsComponentBase>(out sharedWeaponActionsComponent);
            if (sharedWeaponActionsComponent != null)
                sharedWeaponActionsComponent.Update();

            if (MyOpenVR.GetControllerState(secondController).IsButtonPressed(EVRButtonId.k_EButton_SteamVR_Trigger))
            {//holding the trigger
                var gunBase = weapon.GunBase as MyGunBase;
                MyGunStatusEnum status;
                weapon.CanShoot(MyShootActionEnum.PrimaryAction, this.EntityId, out status);
                if (status != MyGunStatusEnum.Cooldown && status != MyGunStatusEnum.BurstLimit)
                {
                    weapon.Shoot(MyShootActionEnum.PrimaryAction, gunBase != null ? (Vector3)gunBase.GetMuzzleWorldMatrix().Forward : Vector3.Forward, null);
                    if (sharedWeaponActionsComponent != null)
                        sharedWeaponActionsComponent.Shoot(MyShootActionEnum.PrimaryAction);
                }
            }

            if (MyOpenVR.GetControllerState(secondController).WasButtonReleased(EVRButtonId.k_EButton_SteamVR_Trigger))
            {
                weapon.EndShoot(MyShootActionEnum.PrimaryAction);
                if (sharedWeaponActionsComponent != null)
                    sharedWeaponActionsComponent.EndShoot(MyShootActionEnum.PrimaryAction);
            }

            if (MyOpenVR.GetControllerState(secondController).WasButtonPressed(EVRButtonId.k_EButton_Grip))
            {
                var gunBase = weapon.GunBase as MyGunBase;
                weapon.Shoot(MyShootActionEnum.SecondaryAction, gunBase != null ? (Vector3)gunBase.GetMuzzleWorldMatrix().Forward : Vector3.Forward, null);
                if (sharedWeaponActionsComponent != null)
                    sharedWeaponActionsComponent.Shoot(MyShootActionEnum.SecondaryAction);
            }
            if (MyOpenVR.GetControllerState(secondController).WasButtonReleased(EVRButtonId.k_EButton_Grip))
            {
                weapon.EndShoot(MyShootActionEnum.SecondaryAction);
                if (sharedWeaponActionsComponent != null)
                    sharedWeaponActionsComponent.EndShoot(MyShootActionEnum.SecondaryAction);
            }

            if (MyOpenVR.GetControllerState(secondController).WasButtonPressed(EVRButtonId.k_EButton_ApplicationMenu))
            {
                var gunBase = weapon.GunBase as MyGunBase;
                weapon.Shoot(MyShootActionEnum.TertiaryAction, gunBase != null ? (Vector3)gunBase.GetMuzzleWorldMatrix().Forward : Vector3.Forward, null);
                if (sharedWeaponActionsComponent != null)
                    sharedWeaponActionsComponent.Shoot(MyShootActionEnum.TertiaryAction);
            }
            if (MyOpenVR.GetControllerState(secondController).WasButtonReleased(EVRButtonId.k_EButton_ApplicationMenu))
            {
                weapon.EndShoot(MyShootActionEnum.TertiaryAction);
                if (sharedWeaponActionsComponent != null)
                    sharedWeaponActionsComponent.EndShoot(MyShootActionEnum.TertiaryAction);
            }

            Vector2 touchpadPos = Vector2.Zero;
            bool validTouchpadPos = false;
            validTouchpadPos = MyOpenVR.GetControllerState(secondController).GetTouchpadXY(ref touchpadPos);

            if (MyOpenVR.GetControllerState(secondController).WasButtonPressed(EVRButtonId.k_EButton_SteamVR_Touchpad))
            {
                SwitchWeapon(ref weapon, ref weaponInfo, weapons, secondController ? ControllerRole.rightHand : ControllerRole.leftHand, validTouchpadPos ? touchpadPos : (Vector2?)null);
            }
            else
            {
                if (validTouchpadPos)
                {
                    if (weapon is ITouchPadListener)
                        (weapon as ITouchPadListener).TouchPadChanged(touchpadPos);
                }
            }
        }