/// <summary>
        /// Create a new player object,it will be initialized
        /// </summary>
        public Player(PlayerIndex playerIndex, Device playerDevice)
            : base(new Vector2(0, TGPAContext.Instance.ScreenHeight / 2),
            Rectangle.Empty,
            new Vector2(400.0f, 400.0f),
            new Vector2(0.35f, 0.35f),
            0.0f,
            InfiniteTimeToLive)
        {
            this.Index = playerIndex;
            this.Name = "";

            switch (Index)
            {
                case PlayerIndex.One:
                    sRect = new Rectangle(0, 0, 368, 292);
                    break;

                case PlayerIndex.Two:
                    sRect = new Rectangle(38, 358, 309, 255);
                    break;

            }

            dRect = ComputeDstRect(sRect);

            Rumble = new Rumble[4];
            for (int i = 0; i < Rumble.Length; i++)
            {
                Rumble[i] = new Rumble(0);
            }
            this.Device = playerDevice;
            RumbleAssignation(playerDevice);

            Initialize();
        }
 /// <summary>
 /// For a given gamepad, this function determines if there is a profile logged on it
 /// </summary>
 /// <param name="gamepadIndex"></param>
 /// <returns></returns>
 public static bool DeviceHasProfile(Device device)
 {
     #if XBOX
     return DeviceProfile(device) != null;
     #else
     return false;
     #endif
 }
        public OptionsData()
        {
            //version = TGPAContext.Instance.Version;

            showScores = false;
            showMusic = true;
            debugMode = false;
            difficulty = Difficulty.Normal;

            #if WINDOWS
            screenWidth = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width; //Use best resolution
            screenHeight = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height;
            Player1Name = "windowsPlayer";
            Player1PlayingDevice = new Device();
            fullScreen = true;
            log = true;
            #endif

            musicVolume = 0.9f;
            soundVolume = 0.5f;
            rumble = true;
        }
        /// <summary>
        /// Update menu/title screen (buttons)
        /// </summary>
        /// <param name="gameTime"></param>
        public void Update(GameTime gameTime)
        {
            this.fadeInOut -= 0.05f;
            if (this.fadeInOut < 0) { this.fadeInOut = 0f; }

            #region Titlescreen
            if (mode == MenuMode.Titlescreen)
            {

                if (startPressed == false)
                {
                    startPressAlpha += startPressAlphaDelta;
                    if (startPressAlpha > 1f || startPressAlpha < 0f) startPressAlphaDelta = -startPressAlphaDelta;

                    //Wait for a player to press start
                    if (deviceOkP1 == false)
                    {
                        for (PlayerIndex index = PlayerIndex.One; index <= PlayerIndex.Four; index++)
                        {
                            if (GamePad.GetState(index).Buttons.Start == ButtonState.Pressed)
                            {
                                //It will become Player 1
                                player1device = new Device(DeviceType.Gamepad, (int)index);
                                deviceOkP1 = true;
                                break;
                            }
                        }
                    }
                    else
                    {
                        if (TGPAContext.Instance.Player1 == null)
                        {
                            if (deviceOkP1)
                            {
                                //Ask for sign in
                                if (Device.DeviceHasProfile(player1device) == false)
                                {
                                    if (Guide.IsVisible == false)
                                    {
                                        signInRequested = true;
                                        Guide.ShowSignIn(1, false);
                                    }
                                    else
                                    {
                                        if (signInRequested)
                                        {
                                            signInRequested = false;
                                            deviceOkP1 = false;
                                            player1device = null;
                                        }
                                    }
                                }

                                else
                                {
                                    TGPAContext.Instance.Player1 = new Player(PlayerIndex.One, player1device);
            #if XBOX
                                    TGPAContext.Instance.Player1.Name = TGPAContext.Instance.Player1.XboxProfile.Gamertag;
            #endif
                                }
                            }
                        }
                        else
                        {
                            //Assign eventually a new controller for P1
                            if (deviceOkP1)
                            {
                                TGPAContext.Instance.Player1.Device = player1device;
                            }

                            startPressed = true;
                        }
                    }
                }
                else
                {
                    if (TGPAContext.Instance.Player1 == null)
                        throw new Exception("No player WTF");

                    if (TGPAContext.Instance.InputManager.PlayerPressButtonBack(TGPAContext.Instance.Player1))
                    {
                        TGPAContext.Instance.CurrentGameState = GameState.Credits;
                    }
                    else
                    {
                        //Buy from title screen
                        if (TGPAContext.Instance.IsTrialMode)
                        {
                            if (TGPAContext.Instance.InputManager.PlayerPressYButton(TGPAContext.Instance.Player1))
                            {
                                if (Guide.IsVisible == false)
                                {
                                    //Player need rights
                                    SignedInGamer xboxProfile = Device.DeviceProfile(TGPAContext.Instance.Player1.Device);

                                    if (xboxProfile.Privileges.AllowPurchaseContent)
                                    {
                                        Guide.ShowMarketplace((PlayerIndex)TGPAContext.Instance.Player1.Device.Index);
                                    }
                                    else
                                    {
                                        Guide.BeginShowMessageBox(LocalizedStrings.GetString("BuyFailTitle"), LocalizedStrings.GetString("BuyFailContent"), new List<string> { "OK" }, 0, MessageBoxIcon.Warning, null, null);
                                    }
                                }
                            }
                        }

                    }

                    KeyboardState keyboard = Keyboard.GetState();

                    //Mouse and menus
                    Rectangle startRect = new Rectangle(
                        StartButton.X, StartButton.Y,
                        StartButton.X + StartButton.Width,
                        StartButton.Y + StartButton.Height);

                    Rectangle optionsRect = new Rectangle(
                         OptionsButton.X, OptionsButton.Y,
                         OptionsButton.X + OptionsButton.Width,
                         OptionsButton.Y + OptionsButton.Height);

                    Rectangle exitRect = new Rectangle(
                                         QuitButton.X, QuitButton.Y,
                         QuitButton.X + QuitButton.Width,
                         QuitButton.Y + QuitButton.Height);

                    //PC Management
                    #region Mouse input
                    if (TGPAContext.Instance.Player1.IsPlayingOnWindows() && TGPAContext.Instance.IsActive)
                    {
                        if (buttonGoDst.Contains(TGPAContext.Instance.MousePoint))
                        {
                            Focus = MenuButtons.Play;

                            if (TGPAContext.Instance.InputManager.PlayerPressButtonConfirm(TGPAContext.Instance.Player1))
                                TGPAContext.Instance.CurrentGameState = GameState.LevelSelectionScreen;

                        }
                        else if (buttonOptionsDst.Contains(TGPAContext.Instance.MousePoint))
                        {
                            Focus = MenuButtons.Options;

                            if (TGPAContext.Instance.InputManager.PlayerPressButtonConfirm(TGPAContext.Instance.Player1))
                            {
                                changeModeAfterAlphaBlending(MenuMode.Options);
                                InitializeOptionsValues(false);
                            }

                        }
                        else if (buttonExitDst.Contains(TGPAContext.Instance.MousePoint))
                        {
                            Focus = MenuButtons.Exit;

                            if ((InputManager.IsClic(previousMouseState, Mouse.GetState())) || (TGPAContext.Instance.InputManager.PlayerPressButtonConfirm(TGPAContext.Instance.Player1)))
                                TGPAContext.Instance.CurrentGameState = GameState.Exit;

                        }
                        else
                        {
                            Focus = MenuButtons.None;
                        }
                    }
                    #endregion

                    switch (focus)
                    {
                        case MenuButtons.Play:

                            if ((TGPAContext.Instance.InputManager.PlayerPressButtonConfirm(TGPAContext.Instance.Player1)))
                                TGPAContext.Instance.CurrentGameState = GameState.LevelSelectionScreen;

                            if (TGPAContext.Instance.InputManager.PlayerPressDown(TGPAContext.Instance.Player1))
                                focus = MenuButtons.Options;

                            if (TGPAContext.Instance.InputManager.PlayerPressUp(TGPAContext.Instance.Player1))
                                focus = MenuButtons.Exit;

                            break;

                        case MenuButtons.Options:

                            if ((TGPAContext.Instance.InputManager.PlayerPressButtonConfirm(TGPAContext.Instance.Player1)))
                            {
                                changeModeAfterAlphaBlending(MenuMode.Options);
                                InitializeOptionsValues(false);
                            }

                            if (TGPAContext.Instance.InputManager.PlayerPressDown(TGPAContext.Instance.Player1))
                                focus = MenuButtons.Exit;

                            if (TGPAContext.Instance.InputManager.PlayerPressUp(TGPAContext.Instance.Player1))
                                focus = MenuButtons.Play;

                            break;

                        case MenuButtons.Exit:

                            if ((TGPAContext.Instance.InputManager.PlayerPressButtonConfirm(TGPAContext.Instance.Player1)))
                                TGPAContext.Instance.CurrentGameState = GameState.Exit;

                            if (TGPAContext.Instance.InputManager.PlayerPressDown(TGPAContext.Instance.Player1))
                                focus = MenuButtons.Play;

                            if (TGPAContext.Instance.InputManager.PlayerPressUp(TGPAContext.Instance.Player1))
                                focus = MenuButtons.Options;

                            break;
            #if XBOX
                    case MenuButtons.None:
                        focus = MenuButtons.Play;
                        break;
            #endif
                    }
                    previousMouseState = Mouse.GetState();
                }

            }
            #endregion
            #region Option screen
            else if (mode == MenuMode.Options)
            {
                this.optionsSection.Update(gameTime);

                //Set focus
                if (TGPAContext.Instance.Player1.IsPlayingOnWindows() == false)
                {
                    #region Pad Management

                    TGPAControl control = null;

                    if (TGPAContext.Instance.InputManager.PlayerPressDown(TGPAContext.Instance.Player1))
                    {
                        if (optionsSection.CurrentLine + 1 < optionsSection.Lines)
                        {
                            control = optionsSection.GetControl(optionsSection.CurrentLine + 1, optionsSection.CurrentColumn);
                        }

                        if (control == null)
                        {
                            control = optionsSection.GetControl(0, optionsSection.CurrentColumn);
                        }

                        if (control != null)
                        {
                            optionsSection.UnsetFocus();
                            optionsSection.SetFocus(control);
                        }
                    }
                    else if (TGPAContext.Instance.InputManager.PlayerPressUp(TGPAContext.Instance.Player1))
                    {
                        if (optionsSection.CurrentLine - 1 < optionsSection.Lines)
                        {
                            control = optionsSection.GetControl(optionsSection.CurrentLine - 1, optionsSection.CurrentColumn);
                        }

                        if (control == null)
                        {
                            control = optionsSection.GetControl(optionsSection.Lines - 1, optionsSection.CurrentColumn);
                        }

                        if (control != null)
                        {
                            optionsSection.UnsetFocus();
                            optionsSection.SetFocus(control);
                        }
                    }
                    #endregion
                }
            #if WINDOWS
                else
                {
                    bool leave = false;

                    for (int i = 0; i < optionsSection.Lines; i++)
                    {
                        for (int j = 0; j < optionsSection.Columns; j++)
                        {
                            TGPAControl control = optionsSection.GetControl(i, j);

                            if (control != null)
                            {
                                if (control.DstRect.Intersects(TGPAContext.Instance.MouseDst))
                                {
                                    if (control != optionsSection.FocusedControl)
                                    {
                                        optionsSection.UnsetFocus();
                                        optionsSection.SetFocus(control);
                                    }
                                    leave = true;
                                    break;
                                }
                            }

                            if (leave) break; //As beautiful as a GOTO
                        }
                        if (leave) break;
                    }
                }
                if (TGPAContext.Instance.MouseDst.Intersects(buttonOptionsBackDst))
                {
                    buttonOptionsBackSrc.Y = 50;
                    bool exit = TGPAContext.Instance.InputManager.PlayerPressButtonConfirm(TGPAContext.Instance.Player1);

                    if (exit)
                    {
                        changeModeAfterAlphaBlending(MenuMode.Titlescreen);
                        TGPAContext.Instance.Saver.Save();
                    }
                }
                else
                {
                    buttonOptionsBackSrc.Y = 0;
                }
            #endif
                if (TGPAContext.Instance.InputManager.PlayerPressButtonBack(TGPAContext.Instance.Player1))
                {
                    changeModeAfterAlphaBlending(MenuMode.Titlescreen);
                    TGPAContext.Instance.Saver.Save();
                }

            }
            #endregion

            #region Animation

            stuffTimer += gameTime.ElapsedGameTime.Milliseconds;

            //Update stuff
            List<FlyingStuff> fsToDelete = new List<FlyingStuff>();
            foreach (FlyingStuff fs in flyingStuff)
            {
                fs.Update(gameTime);

                if (fs.KillMe)
                {
                    fsToDelete.Add(fs);
                }
            }

            foreach (FlyingStuff deadfs in fsToDelete)
            {
                flyingStuff.Remove(deadfs);
            }

            if (stuffTimer - stuffCooldown > 0f)
            {
                flyingStuff.Add(new FlyingStuff());
                stuffTimer = 0f;
                stuffCooldown = RandomMachine.GetRandomFloat(750, 6000);
            }

            for (int i = 0; i < 2; i++)
            {
                Vector2 loc = new Vector2(610, 625);
                loc.X += RandomMachine.GetRandomInt(-35, 35);
                loc.Y += RandomMachine.GetRandomInt(-35, 35);

                loc.Y = (loc.Y / 768) * TGPAContext.Instance.ScreenHeight;

                Smoke s = new Smoke(loc, RandomMachine.GetRandomVector2(30f, 150f, -40f, -120f), 0.03f, 0.04f, 0.14f, 1,
                                1f, RandomMachine.GetRandomInt(0, 4));

                pmanager.AddParticle(s, true);
            }

            //anim
            elapsed += gameTime.ElapsedGameTime.Milliseconds;

            if (elapsed > time)
            {
                anim = (anim == 0) ? 1 : 0;
                foreSrc.Y = anim * 440;
                elapsed = 0;
                time = 200 + (float)(RandomMachine.GetRandomFloat(-100, 300));
            }

            pmanager.UpdateParticles((float)gameTime.ElapsedGameTime.TotalSeconds);

            #endregion

            this.magicAlpha -= magicAlphaDelta;

            if (this.magicAlpha < 0) {
                this.magicAlpha = 0f;
                this.magicAlphaDelta = -magicAlphaDelta;

                //Change mode
                if (nextMode != mode)
                {
                    mode = nextMode;
                }
            }
            if (this.magicAlpha > 1) { this.magicAlpha = 1f; }
        }
 /// <summary>
 /// For a given gamepad, this function return the logged profile on it
 /// </summary>
 /// <param name="gamepadIndex"></param>
 /// <returns></returns>
 public static SignedInGamer DeviceProfile(Device device)
 {
     #if XBOX
     if (device.Type == DeviceType.Gamepad)
     {
         foreach (SignedInGamer gamer in SignedInGamer.SignedInGamers)
         {
             if ((int)gamer.PlayerIndex == device.Index)
             {
                 return gamer;
             }
         }
     }
     #endif
     return null;
 }
        public void RumbleAssignation(Device playerDevice)
        {
            if (playerDevice.Type != DeviceType.Gamepad) return;

            for (int i = 0; i < Rumble.Length; i++)
            {
                Rumble[i].GamePadIndex = Device.Index;
            }
        }
        /// <summary>
        /// Determine if player 2 joined the game, and with wich device
        /// </summary>
        /// <param name="player1"></param>
        /// <returns></returns>
        public Device Player2JoinedTheGame(Player player1)
        {
            for (PlayerIndex index = PlayerIndex.One; index <= PlayerIndex.Four; index++)
            {
                bool pressed = false;
                switch (index)
                {
                    case PlayerIndex.One:
                        pressed = HasBeenPressed(ppad1.Buttons.Start, pad1.Buttons.Start);
                        break;

                    case PlayerIndex.Two:
                        pressed = HasBeenPressed(ppad2.Buttons.Start, pad2.Buttons.Start);
                        break;

                    case PlayerIndex.Three:
                        pressed = HasBeenPressed(ppad3.Buttons.Start, pad3.Buttons.Start);
                        break;

                    case PlayerIndex.Four:
                        pressed = HasBeenPressed(ppad4.Buttons.Start, pad4.Buttons.Start);
                        break;
                }

                if (pressed)
                {
                    Device playerDevice = new Device(DeviceType.Gamepad, (int)index);

                    //Not player 1 ?
                    if (player1.Device.Type != DeviceType.KeyboardMouse)
                    {
                        if (player1.Device.Type == playerDevice.Type)
                        {
                            if (player1.Device.Index == playerDevice.Index) return null;
                        }
                    }

                    return playerDevice;
                }
            }

            if (player1.Device.Type != DeviceType.KeyboardMouse)
            {
                if (pkeyboard.IsKeyDown(Keys.Enter) && keyboard.IsKeyUp(Keys.Enter)) //On PC Player 2 has to hit Enter touch to join
                {
                    return new Device();
                }
            }

            //Joystick join
            //HACK : Disabled cause this cause me too many problems
            //#if WINDOWS
            //            for (int i = 0; i < JoystickManager.MaxJoystickNumber; i++)
            //            {
            //                if ((player1.Device.Type == DeviceType.Joystick) && (player1.Device.Index == i)) continue;

            //                if (JoystickManager.ConnectedJoystick[i])
            //                {
            //                    foreach (bool b in joyState[i].Buttons)
            //                    {
            //                        if (b == true)
            //                        {
            //                            return new Device(DeviceType.Joystick,i);
            //                        }
            //                    }
            //                }
            //            }
            //#endif

            return null;
        }