示例#1
0
        /// <summary>
        /// Gets the state of the Zune input allowing for flicks and taps.
        /// </summary>
        /// <param name="gameTime">The current time snapshot</param>
        /// <returns>The new ZunePadState object.</returns>
        public static ZunePadState GetState(GameTime gameTime)
        {
            GamePadState gps    = GamePad.GetState(PlayerIndex.One, GamePadDeadZone.None);
            Vector2      flick  = Vector2.Zero;
            bool         tapped = false;

            if (gps.Buttons.LeftStick == ButtonState.Pressed && !zps.IsTouched)
            {
                flickStart     = gps.ThumbSticks.Left;
                flickStartTime = gameTime.TotalGameTime;
            }
            else if (gps.Buttons.LeftStick == ButtonState.Released && zps.IsTouched)
            {
                flick = zps.TouchPosition - flickStart;
                TimeSpan elapsed = gameTime.TotalGameTime - flickStartTime;

                //scale the flick based on how long it took
                flick /= (float)elapsed.TotalSeconds;

                //adjust the .5 and .3 to fit your sensitivity needs. .5 and .3 seem
                //to be pretty decent, but they might need tweaking for some situations
                tapped = (flick.Length() < .5f && elapsed.TotalSeconds < .3f);

                flickStart = Vector2.Zero;
            }

            zps = new ZunePadState(gps, flick, tapped);

            return(zps);
        }
示例#2
0
        /// <summary>
        /// Gets the state of the Zune input without flicks or taps.
        /// </summary>
        /// <returns>The new ZunePadState object.</returns>
        public static ZunePadState GetState()
        {
            GamePadState gps = GamePad.GetState(PlayerIndex.One, GamePadDeadZone.None);

            zps = new ZunePadState(gps, Vector2.Zero, false);

            return(zps);
        }
示例#3
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // get the current gamepad state
            ZunePadState zunePadState = ZunePad.GetState(gameTime);

            // see what was pressed before and what the new state is, toggling states accordingly
            bool backKey   = (oldZunePadState.BackButton == ButtonState.Pressed) && (zunePadState.BackButton == ButtonState.Released);
            bool playKey   = (oldZunePadState.PlayButton == ButtonState.Pressed) && (zunePadState.PlayButton == ButtonState.Released);
            bool selectKey = (oldZunePadState.SelectButton == ButtonState.Pressed) && (zunePadState.SelectButton == ButtonState.Released);
            bool upKey     = (oldZunePadState.DPad.Up == ButtonState.Pressed) && (zunePadState.DPad.Up == ButtonState.Released);
            bool downKey   = (oldZunePadState.DPad.Down == ButtonState.Pressed) && (zunePadState.DPad.Down == ButtonState.Released);
            bool rightKey  = (oldZunePadState.DPad.Right == ButtonState.Pressed) && (zunePadState.DPad.Right == ButtonState.Released);
            bool leftKey   = (oldZunePadState.DPad.Left == ButtonState.Pressed) && (zunePadState.DPad.Left == ButtonState.Released);

            // capture the current state and store it for the next loop
            oldZunePadState = zunePadState;

            // stop the player and exit the app
            if (backKey)
            {
                MediaPlayer.Stop();
                this.Exit();
            }

            // pressing play will toggle the music (and set a flag to show it was a manual selected state)
            // if a track is currently playing, pause it
            // if it was paused, allow it to resume
            // if the player was stopped, play the collection
            else if (playKey)
            {
                if (MediaPlayer.State == MediaState.Playing)
                {
                    MediaPlayer.Pause();
                    wasPausePressed = true;
                }
                else if (MediaPlayer.State == MediaState.Paused)
                {
                    MediaPlayer.Resume();
                    wasPausePressed = false;
                }
                else
                {
                    if (songCollection == null)
                    {
                        SetNextAlbum();
                    }
                    MediaPlayer.Play(songCollection);
                }
            }

            // select simply moves to the next disc
            else if (selectKey)
            {
                StopStartPlay();
            }

            // adjust the current app's volume (not that device)
            else if ((upKey) || (zunePadState.Flick.Y > FlickLength))
            {
                MediaPlayer.Volume = MediaPlayer.Volume + 0.02f;
            }

            // adjust the current app's volume (not that device)
            else if ((downKey) || (zunePadState.Flick.Y < -FlickLength))
            {
                MediaPlayer.Volume = MediaPlayer.Volume - 0.02f;
            }

            // if there IS another track on the current album go to it
            // if there isn't, jump to the next track
            else if ((rightKey) || (zunePadState.Flick.X > FlickLength))
            {
                if (MediaPlayer.State != MediaState.Stopped)
                {
                    if (IsLastSong())
                    {
                        StopStartPlay();
                    }
                    else
                    {
                        MediaPlayer.MoveNext();
                    }
                }
            }

            // if less than five seconds on the current track has been played, move back a the track
            // more than five seconds, restart the track
            else if ((leftKey) || (zunePadState.Flick.X < -FlickLength))
            {
                if (MediaPlayer.State != MediaState.Stopped)
                {
                    if (MediaPlayer.PlayPosition.TotalSeconds <= 5)
                    {
                        MediaPlayer.MovePrevious();
                    }
                    else
                    {
                        // Restart this song
                        int index = MediaPlayer.Queue.ActiveSongIndex;
                        MediaPlayer.Stop();
                        MediaPlayer.Play(songCollection, index);
                    }
                }
            }

            // update all of the visual fields
            TimeSpan remainingTime;

            switch (MediaPlayer.State)
            {
            case MediaState.Playing:
                currentState         = "";
                currentElapsedTime   = String.Format("{0,2:00}:{1,2:00}", MediaPlayer.PlayPosition.Minutes, MediaPlayer.PlayPosition.Seconds);
                remainingTime        = MediaPlayer.Queue.ActiveSong.Duration - MediaPlayer.PlayPosition;
                currentRemianingTime = String.Format("-{0,2:00}:{1,2:00}", remainingTime.Minutes, remainingTime.Seconds);
                currentSong          = MediaPlayer.Queue.ActiveSong.Name;
                currentTrack         = String.Format("Track: {0,2:00} of {1,2:00}", MediaPlayer.Queue.ActiveSongIndex + 1, MediaPlayer.Queue.Count);
                break;

            case MediaState.Paused:
                if ((!wasPausePressed) && (MediaPlayer.PlayPosition.TotalSeconds == 0))
                {
                    StopStartPlay();
                    break;
                }
                currentState         = "-- paused --";
                currentElapsedTime   = String.Format("{0,2:00}:{1,2:00}", MediaPlayer.PlayPosition.Minutes, MediaPlayer.PlayPosition.Seconds);
                remainingTime        = MediaPlayer.Queue.ActiveSong.Duration - MediaPlayer.PlayPosition;
                currentRemianingTime = String.Format("-{0,2:00}:{1,2:00}", remainingTime.Minutes, remainingTime.Seconds);

                currentSong  = MediaPlayer.Queue.ActiveSong.Name;
                currentTrack = String.Format("Track: {0,2:00} of {1,2:00}", MediaPlayer.Queue.ActiveSongIndex + 1, MediaPlayer.Queue.Count);
                break;

            default:
                currentState         = "-- stopped --";
                currentElapsedTime   = "00:00";
                currentRemianingTime = "-00:00";
                break;
            }
            currentVolume = String.Format("Vol: {0,2:00.0}", MediaPlayer.Volume * 10.0f);

            // required call
            base.Update(gameTime);
        }