#include "PauseMenuCursor.h"
//玩家1（键盘）
#include "MenuTextUp_Player1Keyboard.h"
#include "MenuTextDown_Player1Keyboard.h"
#include "MenuTextLeft_Player1Keyboard.h"
#include "MenuTextRight_Player1Keyboard.h"
#include "MenuTextOk_Player1Keyboard.h"
#include "MenuTextCancel_Player1Keyboard.h"
#include "MenuTextJump_Player1Keyboard.h"
#include "MenuTextAtk_Player1Keyboard.h"
#include "MenuTextMenu_Player1Keyboard.h"
#include "MenuTextInventory_Player1Keyboard.h"
#include "MenuTextMap_Player1Keyboard.h"
#include "MenuTextPagePrev_Player1Keyboard.h"
#include "MenuTextPageNext_Player1Keyboard.h"
#include "MenuTextSk1_Player1Keyboard.h"
#include "MenuTextSk2_Player1Keyboard.h"
#include "MenuTextSk3_Player1Keyboard.h"
#include "MenuTextSk4_Player1Keyboard.h"
#include "MenuTextSk5_Player1Keyboard.h"
#include "MenuTextSk6_Player1Keyboard.h"
#include "MenuTextSwitchMc_Player1Keyboard.h"
//玩家1（手柄）
#include "MenuTextUp_Player1Joystick.h"
#include "MenuTextDown_Player1Joystick.h"
#include "MenuTextLeft_Player1Joystick.h"
#include "MenuTextRight_Player1Joystick.h"
#include "MenuTextOk_Player1Joystick.h"
#include "MenuTextCancel_Player1Joystick.h"
#include "MenuTextJump_Player1Joystick.h"
#include "MenuTextAtk_Player1Joystick.h"
#include "MenuTextMenu_Player1Joystick.h"
#include "MenuTextInventory_Player1Joystick.h"
#include "MenuTextMap_Player1Joystick.h"
#include "MenuTextPagePrev_Player1Joystick.h"
#include "MenuTextPageNext_Player1Joystick.h"
#include "MenuTextSk1_Player1Joystick.h"
#include "MenuTextSk2_Player1Joystick.h"
#include "MenuTextSk3_Player1Joystick.h"
#include "MenuTextSk4_Player1Joystick.h"
#include "MenuTextSk5_Player1Joystick.h"
#include "MenuTextSk6_Player1Joystick.h"
#include "MenuTextSwitchMc_Player1Joystick.h"
//玩家2（键盘）
#include "MenuTextUp_Player2Keyboard.h"
#include "MenuTextDown_Player2Keyboard.h"
#include "MenuTextLeft_Player2Keyboard.h"
#include "MenuTextRight_Player2Keyboard.h"
#include "MenuTextOk_Player2Keyboard.h"
#include "MenuTextCancel_Player2Keyboard.h"
#include "MenuTextJump_Player2Keyboard.h"
#include "MenuTextAtk_Player2Keyboard.h"
#include "MenuTextMenu_Player2Keyboard.h"
#include "MenuTextInventory_Player2Keyboard.h"
#include "MenuTextMap_Player2Keyboard.h"
#include "MenuTextPagePrev_Player2Keyboard.h"
#include "MenuTextPageNext_Player2Keyboard.h"
#include "MenuTextSk1_Player2Keyboard.h"
#include "MenuTextSk2_Player2Keyboard.h"
#include "MenuTextSk3_Player2Keyboard.h"
#include "MenuTextSk4_Player2Keyboard.h"
#include "MenuTextSk5_Player2Keyboard.h"
#include "MenuTextSk6_Player2Keyboard.h"
#include "MenuTextSwitchMc_Player2Keyboard.h"
//玩家2（手柄）
#include "MenuTextUp_Player2Joystick.h"
#include "MenuTextDown_Player2Joystick.h"
#include "MenuTextLeft_Player2Joystick.h"
#include "MenuTextRight_Player2Joystick.h"
#include "MenuTextOk_Player2Joystick.h"
#include "MenuTextCancel_Player2Joystick.h"
#include "MenuTextJump_Player2Joystick.h"
#include "MenuTextAtk_Player2Joystick.h"
#include "MenuTextMenu_Player2Joystick.h"
#include "MenuTextInventory_Player2Joystick.h"
#include "MenuTextMap_Player2Joystick.h"
#include "MenuTextPagePrev_Player2Joystick.h"
#include "MenuTextPageNext_Player2Joystick.h"
#include "MenuTextSk1_Player2Joystick.h"
#include "MenuTextSk2_Player2Joystick.h"
#include "MenuTextSk3_Player2Joystick.h"
#include "MenuTextSk4_Player2Joystick.h"
#include "MenuTextSk5_Player2Joystick.h"
#include "MenuTextSk6_Player2Joystick.h"
#include "MenuTextSwitchMc_Player2Joystick.h"
#include "MenuTextMsgPlayerPress.h"
#include "MenuTextP1Ball.h"
#include "MenuTextP2Ball.h"
PauseMenuCursor::PauseMenuCursor()
{
    ani.png = L"CS_CaveStoryMod";
    ani.sprite_index = L"mc_kanpa_move_r";
    ani.image_counter_speed = 0.5f;
}

void PauseMenuCursor::Step()
{
    if (!runCursor)
    {
        BlockFirstOkOp();
        //执行运动
        Entity::Step();
        return;
    }
    //开始
    do
    {
        bOnce = false;
        switch (menu)
        {
        case 0://主菜单
            MainMenu();
            break;
        case 1://选项菜单
            OpMenu();
            break;
        case 2://图像设置
            GraphMenu();
            break;
        case 3://音频设置
            AudioMenu();
            break;
        case 4://控制设置
            ControlMenu();
            break;
        case 5://玩家1
            Player1Menu();
            break;
        case 6://玩家1键盘
            Player1KeyboardMenu();
            break;
        case 7://玩家1手柄
            Player1JoystickMenu();
            break;
        case 8://玩家2
            Player2Menu();
            break;
        case 9://玩家2键盘
            Player2KeyboardMenu();
            break;
        case 10://玩家2手柄
            Player2JoystickMenu();
            break;
        case 11://分配玩家控制器
            SelectControllerMenu();
            break;
        }
    }while (bOnce);
    //执行运动
    Entity::Step();
}

void PauseMenuCursor::BlockFirstOkOp()
{
    x = 18 * 16.0f + 8;
    y = (10 + index + (index > 0 ? 1 : 0)) * 16.0f + 8;
    if (gameFunc->GetAnyKeyPress(nullptr) == false &&
        gameFunc->GetAnyGameControllerKeyPress(gameFunc->GetGameControllerFromIndex(0), nullptr) == false &&
        gameFunc->GetAnyGameControllerKeyPress(gameFunc->GetGameControllerFromIndex(1), nullptr) == false)
    {
        //当所有键都放开
        runCursor = true;
        gameFunc->ResetKeyboard();
        std::weak_ptr<Entity> players[4];
        players[0] = gameFunc->GetMyChar_Player1(gameFunc->entityRes);
        players[1] = gameFunc->GetMyChar_Player1_menuKeyboard(gameFunc->entityRes);
        players[2] = gameFunc->GetMyChar_Player2(gameFunc->entityRes);
        players[3] = gameFunc->GetMyChar_Player2_menuKeyboard(gameFunc->entityRes);
        for (auto i = 0; i < _countof(players); i++)
        {
            auto wplayer = players[i].lock();
            if (wplayer)
            {
                wplayer->inputLock.SetMenu(true);
                wplayer->input.SetMenu(false);
            }
        }
    }
}

void PauseMenuCursor::MainMenu()
{
    ClearOp();
    GetOp();
    if (opU)
    {
        if (index > 0)
            index--;
        else
            index = 2;
        gameFunc->PlaySound(L"MenuOk");
    }
    else if (opD)
    {
        if (index < 2)
            index++;
        else
            index = 0;
        gameFunc->PlaySound(L"MenuOk");
    }
    if (opOk)
    {
        bOnce = true;
        switch (index)
        {
        case 0://返回游戏
            gameFunc->PlaySound(L"MenuCancel");
            gameFunc->SetPauseMenuBackGame(true);
            break;
        case 1://选项
            gameFunc->PlaySound(L"MenuOk");
            ShowOpMenu();
            index = 0;
            menu = 1;
            break;
        case 2://退出到标题画面
            gameFunc->PlaySound(L"MenuOk");
            gameFunc->SetPauseMenuBackTitle(true);
            break;
        }
        return;
    }
    else if (opCancel || opMenu)
    {
        backGame = true;
    }
    //防止反复开菜单
    if (backGame)
    {
        if (gameFunc->GetAnyKeyPress(nullptr) == false &&
            gameFunc->GetAnyGameControllerKeyPress(gameFunc->GetGameControllerFromIndex(0), nullptr) == false &&
            gameFunc->GetAnyGameControllerKeyPress(gameFunc->GetGameControllerFromIndex(1), nullptr) == false)
        {
            //当所有键都放开
            gameFunc->PlaySound(L"MenuCancel");
            gameFunc->SetPauseMenuBackGame(true);
        }
    }
    x = 18 * 16.0f + 8;
    y = (10 + index + (index > 0 ? 1 : 0)) * 16.0f + 8;
}

void PauseMenuCursor::OpMenu()
{
    ClearOp();
    GetOp();
    if (opU)
    {
        if (index > 0)
            index--;
        else
            index = 3;
        gameFunc->PlaySound(L"MenuOk");
    }
    else if (opD)
    {
        if (index < 3)
            index++;
        else
            index = 0;
        gameFunc->PlaySound(L"MenuOk");
    }
    if (opOk)
    {
        bOnce = true;
        switch (index)
        {
        case 0://返回
            gameFunc->PlaySound(L"MenuCancel");
            ShowMainMenu();
            index = 1;
            menu = 0;
            break;
        case 1://图像设置
            gameFunc->PlaySound(L"MenuOk");
            ShowGraphMenu();
            index = 0;
            menu = 2;
            break;
        case 2://音频设置
            gameFunc->PlaySound(L"MenuOk");
            ShowAudioMenu();
            index = 0;
            menu = 3;
            break;
        case 3://控制设置
            gameFunc->PlaySound(L"MenuOk");
            ShowControlMenu();
            index = 0;
            menu = 4;
            break;
        }
        return;
    }
    else if (opCancel || opMenu)
    {
        gameFunc->PlaySound(L"MenuCancel");
        ShowMainMenu();
        index = 1;
        menu = 0;
        bOnce = true;
        return;
    }
    x = 18 * 16.0f + 8;
    y = (11 + index + (index > 0 ? 1 : 0)) * 16.0f + 8;
}

void PauseMenuCursor::GraphMenu()
{
    ClearOp();
    GetOp();
    if (opU)
    {
        if (index > 0)
            index--;
        else
            index = 5;
        gameFunc->PlaySound(L"MenuOk");
    }
    else if (opD)
    {
        if (index < 5)
            index++;
        else
            index = 0;
        gameFunc->PlaySound(L"MenuOk");
    }
    if (opOk)
    {
        bOnce = true;
        switch (index)
        {
        case 0://返回
            gameFunc->PlaySound(L"MenuCancel");
            ShowOpMenu();
            index = 1;
            menu = 1;
            break;
        case 1://显示模式
            gameFunc->PlaySound(L"MenuOk");
            gameFunc->SetFullScreen(!gameFunc->GetFullScreen());
            gameFunc->SaveConfig();
            break;
        case 2://缩小窗口
            gameFunc->PlaySound(L"MenuOk");
            gameFunc->ReduceWindow();
            gameFunc->SaveConfig();
            break;
        case 3://放大窗口
            gameFunc->PlaySound(L"MenuOk");
            gameFunc->EnlargeWindow();
            gameFunc->SaveConfig();
            break;
        case 4://截图
            gameFunc->ScreenShot();
            break;
        case 5://调试信息
            gameFunc->PlaySound(L"MenuOk");
            gameFunc->SetShowDebug(!gameFunc->GetShowDebug());
            gameFunc->SaveConfig();
            break;
        }
        return;
    }
    else if (opCancel || opMenu)
    {
        gameFunc->PlaySound(L"MenuCancel");
        ShowOpMenu();
        index = 1;
        menu = 1;
        bOnce = true;
        return;
    }
    x = 15 * 16.0f + 8;
    y = (10 + index + (index > 0 ? 1 : 0)) * 16.0f + 8;
}

void PauseMenuCursor::AudioMenu()
{
    ClearOp();
    GetOp();
    if (opU)
    {
        if (index > 0)
            index--;
        else
            index = 2;
        gameFunc->PlaySound(L"MenuOk");
    }
    else if (opD)
    {
        if (index < 2)
            index++;
        else
            index = 0;
        gameFunc->PlaySound(L"MenuOk");
    }
    if (opL)
    {
        if (index == 1)
        {
            //缩小音乐音量
            if (gameFunc->GetMusicVolume() > 0)
            {
                gameFunc->SetMusicVolume(gameFunc->GetMusicVolume() - 1);
                gameFunc->PlaySound(L"MenuOk");
                gameFunc->SaveConfig();
            }
        }
        else if (index == 2)
        {
            //缩小音效音量
            if (gameFunc->GetSoundVolume() > 0)
            {
                gameFunc->SetSoundVolume(gameFunc->GetSoundVolume() - 1);
                gameFunc->PlaySound(L"MenuOk");
                gameFunc->SaveConfig();
            }
        }
    }
    else if (opR)
    {
        if (index == 1)
        {
            //放大音乐音量
            if (gameFunc->GetMusicVolume() < 10)
            {
                gameFunc->SetMusicVolume(gameFunc->GetMusicVolume() + 1);
                gameFunc->PlaySound(L"MenuOk");
                gameFunc->SaveConfig();
            }
        }
        else if (index == 2)
        {
            //放大音效音量
            if (gameFunc->GetSoundVolume() < 10)
            {
                gameFunc->SetSoundVolume(gameFunc->GetSoundVolume() + 1);
                gameFunc->PlaySound(L"MenuOk");
                gameFunc->SaveConfig();
            }
        }
    }
    if (opOk)
    {
        bOnce = true;
        switch (index)
        {
        case 0://返回
            gameFunc->PlaySound(L"MenuCancel");
            ShowOpMenu();
            index = 2;
            menu = 1;
            break;
        }
        return;
    }
    else if (opCancel || opMenu)
    {
        gameFunc->PlaySound(L"MenuCancel");
        ShowOpMenu();
        index = 2;
        menu = 1;
        bOnce = true;
        return;
    }
    x = 15 * 16.0f + 8;
    y = (12 + index + (index > 0 ? 1 : 0)) * 16.0f + 8;
}

void PauseMenuCursor::ControlMenu()
{
    ClearOp();
    GetOp();
    if (opU)
    {
        if (index > 0)
            index--;
        else
            index = 3;
        gameFunc->PlaySound(L"MenuOk");
    }
    else if (opD)
    {
        if (index < 3)
            index++;
        else
            index = 0;
        gameFunc->PlaySound(L"MenuOk");
    }
    if (opOk)
    {
        bOnce = true;
        switch (index)
        {
        case 0://返回
            gameFunc->PlaySound(L"MenuCancel");
            ShowOpMenu();
            index = 3;
            menu = 1;
            break;
        case 1://玩家1
            gameFunc->PlaySound(L"MenuOk");
            ShowPlayer1Menu();
            index = 0;
            menu = 5;
            break;
        case 2://玩家2
            gameFunc->PlaySound(L"MenuOk");
            ShowPlayer2Menu();
            index = 0;
            menu = 8;
            break;
        case 3://选择控制器
            gameFunc->PlaySound(L"MenuOk");
            gameFunc->OpenGameController();
            ShowSelectControllerMenu();
            index = 0;
            menu = 11;
            break;
        }
        return;
    }
    else if (opCancel || opMenu)
    {
        gameFunc->PlaySound(L"MenuCancel");
        ShowOpMenu();
        index = 3;
        menu = 1;
        bOnce = true;
        return;
    }
    x = 18 * 16.0f + 8;
    y = (12 + index + (index > 0 ? 1 : 0)) * 16.0f + 8;
}

void PauseMenuCursor::Player1Menu()
{
    ClearOp();
    GetOp();
    if (opU)
    {
        if (index > 0)
            index--;
        else
            index = 2;
        gameFunc->PlaySound(L"MenuOk");
    }
    else if (opD)
    {
        if (index < 2)
            index++;
        else
            index = 0;
        gameFunc->PlaySound(L"MenuOk");
    }
    if (opOk)
    {
        bOnce = true;
        switch (index)
        {
        case 0://返回
            gameFunc->PlaySound(L"MenuCancel");
            ShowControlMenu();
            index = 1;
            menu = 4;
            break;
        case 1://键盘
            gameFunc->PlaySound(L"MenuOk");
            ShowPlayer1KeyboardMenu();
            index = 0;
            menu = 6;
            break;
        case 2://手柄
            gameFunc->PlaySound(L"MenuOk");
            ShowPlayer1JoystickMenu();
            index = 0;
            menu = 7;
            break;
        }
        return;
    }
    else if (opCancel || opMenu)
    {
        gameFunc->PlaySound(L"MenuCancel");
        ShowControlMenu();
        index = 1;
        menu = 4;
        bOnce = true;
        return;
    }
    x = 18 * 16.0f + 8;
    y = (12 + index + (index > 0 ? 1 : 0)) * 16.0f + 8;
}

void PauseMenuCursor::Player1KeyboardMenu()
{
    ClearOp();
    GetOp();
    if (opU)
    {
        if (index3 == 0)
        {
            if (index > 0)
                index--;
            else
            {
                index3 = 1;
                index = 10;
            }
                
        }
        else
        {
            if (index > 1)
                index--;
            else
            {
                index3 = 0;
                index = 10;
            }  
        }
        gameFunc->PlaySound(L"MenuOk");
    }
    else if (opD)
    {
        if (index3 == 0)
        {
            if (index < 10)
                index++;
            else
            {
                index3 = 1;
                index = 1;
            } 
        }
        else
        {
            if (index < 10)
                index++;
            else
            {
                index3 = 0;
                index = 0;
            }
        }
        gameFunc->PlaySound(L"MenuOk");
    }
    if (opL)
    {
        if (index3 > 0)
            index3--;
        else
            index3 = 1;
        gameFunc->PlaySound(L"MenuOk");
        if (index3 == 1)
        {
            if (index == 0)
                index = 1;
        }
    }
    else if (opR)
    {
        if (index3 < 1)
            index3++;
        else
            index3 = 0;
        gameFunc->PlaySound(L"MenuOk");
        if (index3 == 1)
        {
            if (index == 0)
                index = 1;
        }
    }
    if (opOk)
    {
        bOnce = true;
        std::weak_ptr<Entity> e;
        switch (index)
        {
        case 0://返回
            gameFunc->PlaySound(L"MenuCancel");
            ShowPlayer1Menu();
            index = 1;
            menu = 5;
            {
                //修复bug
                std::weak_ptr<Entity> eArr[] = {
                    GetEntityByType(MENUTEXTUP_PLAYER1KEYBOARD_CREATENAME),
                    GetEntityByType(MENUTEXTMAP_PLAYER1KEYBOARD_CREATENAME),
                    GetEntityByType(MENUTEXTDOWN_PLAYER1KEYBOARD_CREATENAME),
                    GetEntityByType(MENUTEXTJUMP_PLAYER1KEYBOARD_CREATENAME),
                    GetEntityByType(MENUTEXTLEFT_PLAYER1KEYBOARD_CREATENAME),
                    GetEntityByType(MENUTEXTATK_PLAYER1KEYBOARD_CREATENAME),
                    GetEntityByType(MENUTEXTRIGHT_PLAYER1KEYBOARD_CREATENAME),
                    GetEntityByType(MENUTEXTSK1_PLAYER1KEYBOARD_CREATENAME),
                    GetEntityByType(MENUTEXTOK_PLAYER1KEYBOARD_CREATENAME),
                    GetEntityByType(MENUTEXTSK2_PLAYER1KEYBOARD_CREATENAME),
                    GetEntityByType(MENUTEXTCANCEL_PLAYER1KEYBOARD_CREATENAME),
                    GetEntityByType(MENUTEXTSK3_PLAYER1KEYBOARD_CREATENAME),
                    GetEntityByType(MENUTEXTMENU_PLAYER1KEYBOARD_CREATENAME),
                    GetEntityByType(MENUTEXTSK4_PLAYER1KEYBOARD_CREATENAME),
                    GetEntityByType(MENUTEXTINVENTORY_PLAYER1KEYBOARD_CREATENAME),
                    GetEntityByType(MENUTEXTSK5_PLAYER1KEYBOARD_CREATENAME),
                    GetEntityByType(MENUTEXTPAGEPREV_PLAYER1KEYBOARD_CREATENAME),
                    GetEntityByType(MENUTEXTSK6_PLAYER1KEYBOARD_CREATENAME),
                    GetEntityByType(MENUTEXTPAGENEXT_PLAYER1KEYBOARD_CREATENAME),
                    GetEntityByType(MENUTEXTSWITCHMC_PLAYER1KEYBOARD_CREATENAME),
                };
                for (int i = 0; i < _countof(eArr); i++)
                {
                    auto we = eArr[i].lock();
                    if (we)
                    {
                        we->ani.png = we->ani.sprite_index = L"";
                        we->el.flag.text = true;
                        we->SetState(0, 0);
                    }
                }
            }
            break;
        case 1:
            if (index3 == 0)//上
                e = GetEntityByType(MENUTEXTUP_PLAYER1KEYBOARD_CREATENAME);
            else//地图
                e = GetEntityByType(MENUTEXTMAP_PLAYER1KEYBOARD_CREATENAME);
            break;
        case 2:
            if (index3 == 0)//下
                e = GetEntityByType(MENUTEXTDOWN_PLAYER1KEYBOARD_CREATENAME);
            else//跳跃
                e = GetEntityByType(MENUTEXTJUMP_PLAYER1KEYBOARD_CREATENAME);
            break;
        case 3:
            if (index3 == 0)//左
                e = GetEntityByType(MENUTEXTLEFT_PLAYER1KEYBOARD_CREATENAME);
            else//攻击
                e = GetEntityByType(MENUTEXTATK_PLAYER1KEYBOARD_CREATENAME);
            break;
        case 4:
            if (index3 == 0)//右
                e = GetEntityByType(MENUTEXTRIGHT_PLAYER1KEYBOARD_CREATENAME);
            else//技能1
                e = GetEntityByType(MENUTEXTSK1_PLAYER1KEYBOARD_CREATENAME);
            break;
        case 5:
            if (index3 == 0)//确认
                e = GetEntityByType(MENUTEXTOK_PLAYER1KEYBOARD_CREATENAME);
            else//技能2
                e = GetEntityByType(MENUTEXTSK2_PLAYER1KEYBOARD_CREATENAME);
            break;
        case 6:
            if (index3 == 0)//取消
                e = GetEntityByType(MENUTEXTCANCEL_PLAYER1KEYBOARD_CREATENAME);
            else//技能3
                e = GetEntityByType(MENUTEXTSK3_PLAYER1KEYBOARD_CREATENAME);
            break;
        case 7:
            if (index3 == 0)//暂停菜单
                e = GetEntityByType(MENUTEXTMENU_PLAYER1KEYBOARD_CREATENAME);
            else//技能4
                e = GetEntityByType(MENUTEXTSK4_PLAYER1KEYBOARD_CREATENAME);
            break;
        case 8:
            if (index3 == 0)//面板
                e = GetEntityByType(MENUTEXTINVENTORY_PLAYER1KEYBOARD_CREATENAME);
            else//技能5
                e = GetEntityByType(MENUTEXTSK5_PLAYER1KEYBOARD_CREATENAME);
            break;
        case 9:
            if (index3 == 0)//面板 上一页
                e = GetEntityByType(MENUTEXTPAGEPREV_PLAYER1KEYBOARD_CREATENAME);
            else//技能6
                e = GetEntityByType(MENUTEXTSK6_PLAYER1KEYBOARD_CREATENAME);
            break;
        case 10:
            if (index3 == 0)//面板 下一页
                e = GetEntityByType(MENUTEXTPAGENEXT_PLAYER1KEYBOARD_CREATENAME);
            else//切换角色
                e = GetEntityByType(MENUTEXTSWITCHMC_PLAYER1KEYBOARD_CREATENAME);
            break;
        }
        auto we = e.lock();
        if (we)
        {
            we->SetState(1, 0);
            gameFunc->PlaySound(L"MenuOk");
            gameFunc->ResetKeyboard();
        }
        return;
    }
     else if (opCancel)
     {
         bOnce = true;
         gameFunc->PlaySound(L"MenuCancel");
         ShowPlayer1Menu();
         index = 1;
         menu = 5;
         {
             //修复bug
             std::weak_ptr<Entity> eArr[] = {
                 GetEntityByType(MENUTEXTUP_PLAYER1KEYBOARD_CREATENAME),
                 GetEntityByType(MENUTEXTMAP_PLAYER1KEYBOARD_CREATENAME),
                 GetEntityByType(MENUTEXTDOWN_PLAYER1KEYBOARD_CREATENAME),
                 GetEntityByType(MENUTEXTJUMP_PLAYER1KEYBOARD_CREATENAME),
                 GetEntityByType(MENUTEXTLEFT_PLAYER1KEYBOARD_CREATENAME),
                 GetEntityByType(MENUTEXTATK_PLAYER1KEYBOARD_CREATENAME),
                 GetEntityByType(MENUTEXTRIGHT_PLAYER1KEYBOARD_CREATENAME),
                 GetEntityByType(MENUTEXTSK1_PLAYER1KEYBOARD_CREATENAME),
                 GetEntityByType(MENUTEXTOK_PLAYER1KEYBOARD_CREATENAME),
                 GetEntityByType(MENUTEXTSK2_PLAYER1KEYBOARD_CREATENAME),
                 GetEntityByType(MENUTEXTCANCEL_PLAYER1KEYBOARD_CREATENAME),
                 GetEntityByType(MENUTEXTSK3_PLAYER1KEYBOARD_CREATENAME),
                 GetEntityByType(MENUTEXTMENU_PLAYER1KEYBOARD_CREATENAME),
                 GetEntityByType(MENUTEXTSK4_PLAYER1KEYBOARD_CREATENAME),
                 GetEntityByType(MENUTEXTINVENTORY_PLAYER1KEYBOARD_CREATENAME),
                 GetEntityByType(MENUTEXTSK5_PLAYER1KEYBOARD_CREATENAME),
                 GetEntityByType(MENUTEXTPAGEPREV_PLAYER1KEYBOARD_CREATENAME),
                 GetEntityByType(MENUTEXTSK6_PLAYER1KEYBOARD_CREATENAME),
                 GetEntityByType(MENUTEXTPAGENEXT_PLAYER1KEYBOARD_CREATENAME),
                 GetEntityByType(MENUTEXTSWITCHMC_PLAYER1KEYBOARD_CREATENAME),
             };
             for (int i = 0; i < _countof(eArr); i++)
             {
                 auto we = eArr[i].lock();
                 if (we)
                 {
                     we->ani.png = we->ani.sprite_index = L"";
                     we->el.flag.text = true;
                     we->SetState(0, 0);
                 }
             }
         }
    }
    x = (2 + index3 * 19) * 16.0f + 8;
    y = (6 + index + (index > 0 ? 1 : 0)) * 16.0f + 8;
}

void PauseMenuCursor::Player1JoystickMenu()
{
    ClearOp();
    GetOp();
    if (opU)
    {
        if (index3 == 0)
        {
            if (index > 0)
                index--;
            else
            {
                index3 = 1;
                index = 10;
            }

        }
        else
        {
            if (index > 1)
                index--;
            else
            {
                index3 = 0;
                index = 10;
            }
        }
        gameFunc->PlaySound(L"MenuOk");
    }
    else if (opD)
    {
        if (index3 == 0)
        {
            if (index < 10)
                index++;
            else
            {
                index3 = 1;
                index = 1;
            }
        }
        else
        {
            if (index < 10)
                index++;
            else
            {
                index3 = 0;
                index = 0;
            }
        }
        gameFunc->PlaySound(L"MenuOk");
    }
    if (opL)
    {
        if (index3 > 0)
            index3--;
        else
            index3 = 1;
        gameFunc->PlaySound(L"MenuOk");
        if (index3 == 1)
        {
            if (index == 0)
                index = 1;
        }
    }
    else if (opR)
    {
        if (index3 < 1)
            index3++;
        else
            index3 = 0;
        gameFunc->PlaySound(L"MenuOk");
        if (index3 == 1)
        {
            if (index == 0)
                index = 1;
        }
    }
    if (opOk)
    {
        bOnce = true;
        std::weak_ptr<Entity> e;
        switch (index)
        {
        case 0://返回
            gameFunc->PlaySound(L"MenuCancel");
            ShowPlayer1Menu();
            index = 2;
            menu = 5;
            {
                //修复bug
                std::weak_ptr<Entity> eArr[] = {
                    GetEntityByType(MENUTEXTUP_PLAYER1JOYSTICK_CREATENAME),
                    GetEntityByType(MENUTEXTMAP_PLAYER1JOYSTICK_CREATENAME),
                    GetEntityByType(MENUTEXTDOWN_PLAYER1JOYSTICK_CREATENAME),
                    GetEntityByType(MENUTEXTJUMP_PLAYER1JOYSTICK_CREATENAME),
                    GetEntityByType(MENUTEXTLEFT_PLAYER1JOYSTICK_CREATENAME),
                    GetEntityByType(MENUTEXTATK_PLAYER1JOYSTICK_CREATENAME),
                    GetEntityByType(MENUTEXTRIGHT_PLAYER1JOYSTICK_CREATENAME),
                    GetEntityByType(MENUTEXTSK1_PLAYER1JOYSTICK_CREATENAME),
                    GetEntityByType(MENUTEXTOK_PLAYER1JOYSTICK_CREATENAME),
                    GetEntityByType(MENUTEXTSK2_PLAYER1JOYSTICK_CREATENAME),
                    GetEntityByType(MENUTEXTCANCEL_PLAYER1JOYSTICK_CREATENAME),
                    GetEntityByType(MENUTEXTSK3_PLAYER1JOYSTICK_CREATENAME),
                    GetEntityByType(MENUTEXTMENU_PLAYER1JOYSTICK_CREATENAME),
                    GetEntityByType(MENUTEXTSK4_PLAYER1JOYSTICK_CREATENAME),
                    GetEntityByType(MENUTEXTINVENTORY_PLAYER1JOYSTICK_CREATENAME),
                    GetEntityByType(MENUTEXTSK5_PLAYER1JOYSTICK_CREATENAME),
                    GetEntityByType(MENUTEXTPAGEPREV_PLAYER1JOYSTICK_CREATENAME),
                    GetEntityByType(MENUTEXTSK6_PLAYER1JOYSTICK_CREATENAME),
                    GetEntityByType(MENUTEXTPAGENEXT_PLAYER1JOYSTICK_CREATENAME),
                    GetEntityByType(MENUTEXTSWITCHMC_PLAYER1KEYBOARD_CREATENAME),
                };
                for (int i = 0; i < _countof(eArr); i++)
                {
                    auto we = eArr[i].lock();
                    if (we)
                    {
                        we->ani.png = we->ani.sprite_index = L"";
                        we->el.flag.text = true;
                        we->SetState(0, 0);
                    }
                }
            }
            break;
        case 1:
            if (index3 == 0)//上
                e = GetEntityByType(MENUTEXTUP_PLAYER1JOYSTICK_CREATENAME);
            else//地图
                e = GetEntityByType(MENUTEXTMAP_PLAYER1JOYSTICK_CREATENAME);
            break;
        case 2:
            if (index3 == 0)//下
                e = GetEntityByType(MENUTEXTDOWN_PLAYER1JOYSTICK_CREATENAME);
            else//跳跃
                e = GetEntityByType(MENUTEXTJUMP_PLAYER1JOYSTICK_CREATENAME);
            break;
        case 3:
            if (index3 == 0)//左
                e = GetEntityByType(MENUTEXTLEFT_PLAYER1JOYSTICK_CREATENAME);
            else//攻击
                e = GetEntityByType(MENUTEXTATK_PLAYER1JOYSTICK_CREATENAME);
            break;
        case 4:
            if (index3 == 0)//右
                e = GetEntityByType(MENUTEXTRIGHT_PLAYER1JOYSTICK_CREATENAME);
            else//技能1
                e = GetEntityByType(MENUTEXTSK1_PLAYER1JOYSTICK_CREATENAME);
            break;
        case 5:
            if (index3 == 0)//确认
                e = GetEntityByType(MENUTEXTOK_PLAYER1JOYSTICK_CREATENAME);
            else//技能2
                e = GetEntityByType(MENUTEXTSK2_PLAYER1JOYSTICK_CREATENAME);
            break;
        case 6:
            if (index3 == 0)//取消
                e = GetEntityByType(MENUTEXTCANCEL_PLAYER1JOYSTICK_CREATENAME);
            else//技能3
                e = GetEntityByType(MENUTEXTSK3_PLAYER1JOYSTICK_CREATENAME);
            break;
        case 7:
            if (index3 == 0)//暂停菜单
                e = GetEntityByType(MENUTEXTMENU_PLAYER1JOYSTICK_CREATENAME);
            else//技能4
                e = GetEntityByType(MENUTEXTSK4_PLAYER1JOYSTICK_CREATENAME);
            break;
        case 8:
            if (index3 == 0)//面板
                e = GetEntityByType(MENUTEXTINVENTORY_PLAYER1JOYSTICK_CREATENAME);
            else//技能5
                e = GetEntityByType(MENUTEXTSK5_PLAYER1JOYSTICK_CREATENAME);
            break;
        case 9:
            if (index3 == 0)//面板 上一页
                e = GetEntityByType(MENUTEXTPAGEPREV_PLAYER1JOYSTICK_CREATENAME);
            else//技能6
                e = GetEntityByType(MENUTEXTSK6_PLAYER1JOYSTICK_CREATENAME);
            break;
        case 10:
            if (index3 == 0)//面板 下一页
                e = GetEntityByType(MENUTEXTPAGENEXT_PLAYER1JOYSTICK_CREATENAME);
            else//切换角色
                e = GetEntityByType(MENUTEXTSWITCHMC_PLAYER1JOYSTICK_CREATENAME);
            break;
        }
        auto we = e.lock();
        if (we)
        {
            we->SetState(1, 0);
            gameFunc->PlaySound(L"MenuOk");
        }
        return;
    }
     else if (opCancel)
     {
         bOnce = true;
         gameFunc->PlaySound(L"MenuCancel");
         ShowPlayer1Menu();
         index = 2;
         menu = 5;
         {
             //修复bug
             std::weak_ptr<Entity> eArr[] = {
                 GetEntityByType(MENUTEXTUP_PLAYER1JOYSTICK_CREATENAME),
                 GetEntityByType(MENUTEXTMAP_PLAYER1JOYSTICK_CREATENAME),
                 GetEntityByType(MENUTEXTDOWN_PLAYER1JOYSTICK_CREATENAME),
                 GetEntityByType(MENUTEXTJUMP_PLAYER1JOYSTICK_CREATENAME),
                 GetEntityByType(MENUTEXTLEFT_PLAYER1JOYSTICK_CREATENAME),
                 GetEntityByType(MENUTEXTATK_PLAYER1JOYSTICK_CREATENAME),
                 GetEntityByType(MENUTEXTRIGHT_PLAYER1JOYSTICK_CREATENAME),
                 GetEntityByType(MENUTEXTSK1_PLAYER1JOYSTICK_CREATENAME),
                 GetEntityByType(MENUTEXTOK_PLAYER1JOYSTICK_CREATENAME),
                 GetEntityByType(MENUTEXTSK2_PLAYER1JOYSTICK_CREATENAME),
                 GetEntityByType(MENUTEXTCANCEL_PLAYER1JOYSTICK_CREATENAME),
                 GetEntityByType(MENUTEXTSK3_PLAYER1JOYSTICK_CREATENAME),
                 GetEntityByType(MENUTEXTMENU_PLAYER1JOYSTICK_CREATENAME),
                 GetEntityByType(MENUTEXTSK4_PLAYER1JOYSTICK_CREATENAME),
                 GetEntityByType(MENUTEXTINVENTORY_PLAYER1JOYSTICK_CREATENAME),
                 GetEntityByType(MENUTEXTSK5_PLAYER1JOYSTICK_CREATENAME),
                 GetEntityByType(MENUTEXTPAGEPREV_PLAYER1JOYSTICK_CREATENAME),
                 GetEntityByType(MENUTEXTSK6_PLAYER1JOYSTICK_CREATENAME),
                 GetEntityByType(MENUTEXTPAGENEXT_PLAYER1JOYSTICK_CREATENAME),
                 GetEntityByType(MENUTEXTSWITCHMC_PLAYER1JOYSTICK_CREATENAME),
             };
             for (int i = 0; i < _countof(eArr); i++)
             {
                 auto we = eArr[i].lock();
                 if (we)
                 {
                     we->ani.png = we->ani.sprite_index = L"";
                     we->el.flag.text = true;
                     we->SetState(0, 0);
                 }
             }
         }
    }
    x = (2 + index3 * 19) * 16.0f + 8;
    y = (6 + index + (index > 0 ? 1 : 0)) * 16.0f + 8;
}

void PauseMenuCursor::Player2Menu()
{
    ClearOp();
    GetOp();
    if (opU)
    {
        if (index > 0)
            index--;
        else
            index = 2;
        gameFunc->PlaySound(L"MenuOk");
    }
    else if (opD)
    {
        if (index < 2)
            index++;
        else
            index = 0;
        gameFunc->PlaySound(L"MenuOk");
    }
    if (opOk)
    {
        bOnce = true;
        switch (index)
        {
        case 0://返回
            gameFunc->PlaySound(L"MenuCancel");
            ShowControlMenu();
            index = 2;
            menu = 4;
            break;
        case 1://键盘
            gameFunc->PlaySound(L"MenuOk");
            ShowPlayer2KeyboardMenu();
            index = 0;
            menu = 9;
            break;
        case 2://手柄
            gameFunc->PlaySound(L"MenuOk");
            ShowPlayer2JoystickMenu();
            index = 0;
            menu = 10;
            break;
        }
        return;
    }
    else if (opCancel || opMenu)
    {
        gameFunc->PlaySound(L"MenuCancel");
        ShowControlMenu();
        index = 2;
        menu = 4;
        bOnce = true;
        return;
    }
    x = 18 * 16.0f + 8;
    y = (12 + index + (index > 0 ? 1 : 0)) * 16.0f + 8;
}

void PauseMenuCursor::Player2KeyboardMenu()
{
    ClearOp();
    GetOp();
    if (opU)
    {
        if (index3 == 0)
        {
            if (index > 0)
                index--;
            else
            {
                index3 = 1;
                index = 10;
            }

        }
        else
        {
            if (index > 1)
                index--;
            else
            {
                index3 = 0;
                index = 10;
            }
        }
        gameFunc->PlaySound(L"MenuOk");
    }
    else if (opD)
    {
        if (index3 == 0)
        {
            if (index < 10)
                index++;
            else
            {
                index3 = 1;
                index = 1;
            }
        }
        else
        {
            if (index < 10)
                index++;
            else
            {
                index3 = 0;
                index = 0;
            }
        }
        gameFunc->PlaySound(L"MenuOk");
    }
    if (opL)
    {
        if (index3 > 0)
            index3--;
        else
            index3 = 1;
        gameFunc->PlaySound(L"MenuOk");
        if (index3 == 1)
        {
            if (index == 0)
                index = 1;
        }
    }
    else if (opR)
    {
        if (index3 < 1)
            index3++;
        else
            index3 = 0;
        gameFunc->PlaySound(L"MenuOk");
        if (index3 == 1)
        {
            if (index == 0)
                index = 1;
        }
    }
    if (opOk)
    {
        bOnce = true;
        std::weak_ptr<Entity> e;
        switch (index)
        {
        case 0://返回
            gameFunc->PlaySound(L"MenuCancel");
            ShowPlayer2Menu();
            index = 1;
            menu = 8;
            {
                //修复bug
                std::weak_ptr<Entity> eArr[] = {
                    GetEntityByType(MENUTEXTUP_PLAYER2KEYBOARD_CREATENAME),
                    GetEntityByType(MENUTEXTMAP_PLAYER2KEYBOARD_CREATENAME),
                    GetEntityByType(MENUTEXTDOWN_PLAYER2KEYBOARD_CREATENAME),
                    GetEntityByType(MENUTEXTJUMP_PLAYER2KEYBOARD_CREATENAME),
                    GetEntityByType(MENUTEXTLEFT_PLAYER2KEYBOARD_CREATENAME),
                    GetEntityByType(MENUTEXTATK_PLAYER2KEYBOARD_CREATENAME),
                    GetEntityByType(MENUTEXTRIGHT_PLAYER2KEYBOARD_CREATENAME),
                    GetEntityByType(MENUTEXTSK1_PLAYER2KEYBOARD_CREATENAME),
                    GetEntityByType(MENUTEXTOK_PLAYER2KEYBOARD_CREATENAME),
                    GetEntityByType(MENUTEXTSK2_PLAYER2KEYBOARD_CREATENAME),
                    GetEntityByType(MENUTEXTCANCEL_PLAYER2KEYBOARD_CREATENAME),
                    GetEntityByType(MENUTEXTSK3_PLAYER2KEYBOARD_CREATENAME),
                    GetEntityByType(MENUTEXTMENU_PLAYER2KEYBOARD_CREATENAME),
                    GetEntityByType(MENUTEXTSK4_PLAYER2KEYBOARD_CREATENAME),
                    GetEntityByType(MENUTEXTINVENTORY_PLAYER2KEYBOARD_CREATENAME),
                    GetEntityByType(MENUTEXTSK5_PLAYER2KEYBOARD_CREATENAME),
                    GetEntityByType(MENUTEXTPAGEPREV_PLAYER2KEYBOARD_CREATENAME),
                    GetEntityByType(MENUTEXTSK6_PLAYER2KEYBOARD_CREATENAME),
                    GetEntityByType(MENUTEXTPAGENEXT_PLAYER2KEYBOARD_CREATENAME),
                    GetEntityByType(MENUTEXTSWITCHMC_PLAYER2KEYBOARD_CREATENAME),
                };
                for (int i = 0; i < _countof(eArr); i++)
                {
                    auto we = eArr[i].lock();
                    if (we)
                    {
                        we->ani.png = we->ani.sprite_index = L"";
                        we->el.flag.text = true;
                        we->SetState(0, 0);
                    }
                }
            }
            break;
        case 1:
            if (index3 == 0)//上
                e = GetEntityByType(MENUTEXTUP_PLAYER2KEYBOARD_CREATENAME);
            else//地图
                e = GetEntityByType(MENUTEXTMAP_PLAYER2KEYBOARD_CREATENAME);
            break;
        case 2:
            if (index3 == 0)//下
                e = GetEntityByType(MENUTEXTDOWN_PLAYER2KEYBOARD_CREATENAME);
            else//跳跃
                e = GetEntityByType(MENUTEXTJUMP_PLAYER2KEYBOARD_CREATENAME);
            break;
        case 3:
            if (index3 == 0)//左
                e = GetEntityByType(MENUTEXTLEFT_PLAYER2KEYBOARD_CREATENAME);
            else//攻击
                e = GetEntityByType(MENUTEXTATK_PLAYER2KEYBOARD_CREATENAME);
            break;
        case 4:
            if (index3 == 0)//右
                e = GetEntityByType(MENUTEXTRIGHT_PLAYER2KEYBOARD_CREATENAME);
            else//技能1
                e = GetEntityByType(MENUTEXTSK1_PLAYER2KEYBOARD_CREATENAME);
            break;
        case 5:
            if (index3 == 0)//确认
                e = GetEntityByType(MENUTEXTOK_PLAYER2KEYBOARD_CREATENAME);
            else//技能2
                e = GetEntityByType(MENUTEXTSK2_PLAYER2KEYBOARD_CREATENAME);
            break;
        case 6:
            if (index3 == 0)//取消
                e = GetEntityByType(MENUTEXTCANCEL_PLAYER2KEYBOARD_CREATENAME);
            else//技能3
                e = GetEntityByType(MENUTEXTSK3_PLAYER2KEYBOARD_CREATENAME);
            break;
        case 7:
            if (index3 == 0)//暂停菜单
                e = GetEntityByType(MENUTEXTMENU_PLAYER2KEYBOARD_CREATENAME);
            else//技能4
                e = GetEntityByType(MENUTEXTSK4_PLAYER2KEYBOARD_CREATENAME);
            break;
        case 8:
            if (index3 == 0)//面板
                e = GetEntityByType(MENUTEXTINVENTORY_PLAYER2KEYBOARD_CREATENAME);
            else//技能5
                e = GetEntityByType(MENUTEXTSK5_PLAYER2KEYBOARD_CREATENAME);
            break;
        case 9:
            if (index3 == 0)//面板 上一页
                e = GetEntityByType(MENUTEXTPAGEPREV_PLAYER2KEYBOARD_CREATENAME);
            else//技能6
                e = GetEntityByType(MENUTEXTSK6_PLAYER2KEYBOARD_CREATENAME);
            break;
        case 10:
            if (index3 == 0)//面板 下一页
                e = GetEntityByType(MENUTEXTPAGENEXT_PLAYER2KEYBOARD_CREATENAME);
            else//切换角色
                e = GetEntityByType(MENUTEXTSWITCHMC_PLAYER2KEYBOARD_CREATENAME);
            break;
        }
        auto we = e.lock();
        if (we)
        {
            we->SetState(1, 0);
            gameFunc->PlaySound(L"MenuOk");
            gameFunc->ResetKeyboard();
        }
        return;
    }
     else if (opCancel)
     {
         bOnce = true;
         gameFunc->PlaySound(L"MenuCancel");
         ShowPlayer2Menu();
         index = 1;
         menu = 8;
         {
             //修复bug
             std::weak_ptr<Entity> eArr[] = {
                 GetEntityByType(MENUTEXTUP_PLAYER2KEYBOARD_CREATENAME),
                 GetEntityByType(MENUTEXTMAP_PLAYER2KEYBOARD_CREATENAME),
                 GetEntityByType(MENUTEXTDOWN_PLAYER2KEYBOARD_CREATENAME),
                 GetEntityByType(MENUTEXTJUMP_PLAYER2KEYBOARD_CREATENAME),
                 GetEntityByType(MENUTEXTLEFT_PLAYER2KEYBOARD_CREATENAME),
                 GetEntityByType(MENUTEXTATK_PLAYER2KEYBOARD_CREATENAME),
                 GetEntityByType(MENUTEXTRIGHT_PLAYER2KEYBOARD_CREATENAME),
                 GetEntityByType(MENUTEXTSK1_PLAYER2KEYBOARD_CREATENAME),
                 GetEntityByType(MENUTEXTOK_PLAYER2KEYBOARD_CREATENAME),
                 GetEntityByType(MENUTEXTSK2_PLAYER2KEYBOARD_CREATENAME),
                 GetEntityByType(MENUTEXTCANCEL_PLAYER2KEYBOARD_CREATENAME),
                 GetEntityByType(MENUTEXTSK3_PLAYER2KEYBOARD_CREATENAME),
                 GetEntityByType(MENUTEXTMENU_PLAYER2KEYBOARD_CREATENAME),
                 GetEntityByType(MENUTEXTSK4_PLAYER2KEYBOARD_CREATENAME),
                 GetEntityByType(MENUTEXTINVENTORY_PLAYER2KEYBOARD_CREATENAME),
                 GetEntityByType(MENUTEXTSK5_PLAYER2KEYBOARD_CREATENAME),
                 GetEntityByType(MENUTEXTPAGEPREV_PLAYER2KEYBOARD_CREATENAME),
                 GetEntityByType(MENUTEXTSK6_PLAYER2KEYBOARD_CREATENAME),
                 GetEntityByType(MENUTEXTPAGENEXT_PLAYER2KEYBOARD_CREATENAME),
                 GetEntityByType(MENUTEXTSWITCHMC_PLAYER2KEYBOARD_CREATENAME),
             };
             for (int i = 0; i < _countof(eArr); i++)
             {
                 auto we = eArr[i].lock();
                 if (we)
                 {
                     we->ani.png = we->ani.sprite_index = L"";
                     we->el.flag.text = true;
                     we->SetState(0, 0);
                 }
             }
         }
    }
    x = (2 + index3 * 19) * 16.0f + 8;
    y = (6 + index + (index > 0 ? 1 : 0)) * 16.0f + 8;
}

void PauseMenuCursor::Player2JoystickMenu()
{
    ClearOp();
    GetOp();
    if (opU)
    {
        if (index3 == 0)
        {
            if (index > 0)
                index--;
            else
            {
                index3 = 1;
                index = 10;
            }

        }
        else
        {
            if (index > 1)
                index--;
            else
            {
                index3 = 0;
                index = 10;
            }
        }
        gameFunc->PlaySound(L"MenuOk");
    }
    else if (opD)
    {
        if (index3 == 0)
        {
            if (index < 10)
                index++;
            else
            {
                index3 = 1;
                index = 1;
            }
        }
        else
        {
            if (index < 10)
                index++;
            else
            {
                index3 = 0;
                index = 0;
            }
        }
        gameFunc->PlaySound(L"MenuOk");
    }
    if (opL)
    {
        if (index3 > 0)
            index3--;
        else
            index3 = 1;
        gameFunc->PlaySound(L"MenuOk");
        if (index3 == 1)
        {
            if (index == 0)
                index = 1;
        }
    }
    else if (opR)
    {
        if (index3 < 1)
            index3++;
        else
            index3 = 0;
        gameFunc->PlaySound(L"MenuOk");
        if (index3 == 1)
        {
            if (index == 0)
                index = 1;
        }
    }
    if (opOk)
    {
        bOnce = true;
        std::weak_ptr<Entity> e;
        switch (index)
        {
        case 0://返回
            gameFunc->PlaySound(L"MenuCancel");
            ShowPlayer2Menu();
            index = 2;
            menu = 8;
            {
                //修复bug
                std::weak_ptr<Entity> eArr[] = {
                    GetEntityByType(MENUTEXTUP_PLAYER2JOYSTICK_CREATENAME),
                    GetEntityByType(MENUTEXTMAP_PLAYER2JOYSTICK_CREATENAME),
                    GetEntityByType(MENUTEXTDOWN_PLAYER2JOYSTICK_CREATENAME),
                    GetEntityByType(MENUTEXTJUMP_PLAYER2JOYSTICK_CREATENAME),
                    GetEntityByType(MENUTEXTLEFT_PLAYER2JOYSTICK_CREATENAME),
                    GetEntityByType(MENUTEXTATK_PLAYER2JOYSTICK_CREATENAME),
                    GetEntityByType(MENUTEXTRIGHT_PLAYER2JOYSTICK_CREATENAME),
                    GetEntityByType(MENUTEXTSK1_PLAYER2JOYSTICK_CREATENAME),
                    GetEntityByType(MENUTEXTOK_PLAYER2JOYSTICK_CREATENAME),
                    GetEntityByType(MENUTEXTSK2_PLAYER2JOYSTICK_CREATENAME),
                    GetEntityByType(MENUTEXTCANCEL_PLAYER2JOYSTICK_CREATENAME),
                    GetEntityByType(MENUTEXTSK3_PLAYER2JOYSTICK_CREATENAME),
                    GetEntityByType(MENUTEXTMENU_PLAYER2JOYSTICK_CREATENAME),
                    GetEntityByType(MENUTEXTSK4_PLAYER2JOYSTICK_CREATENAME),
                    GetEntityByType(MENUTEXTINVENTORY_PLAYER2JOYSTICK_CREATENAME),
                    GetEntityByType(MENUTEXTSK5_PLAYER2JOYSTICK_CREATENAME),
                    GetEntityByType(MENUTEXTPAGEPREV_PLAYER2JOYSTICK_CREATENAME),
                    GetEntityByType(MENUTEXTSK6_PLAYER2JOYSTICK_CREATENAME),
                    GetEntityByType(MENUTEXTPAGENEXT_PLAYER2JOYSTICK_CREATENAME),
                    GetEntityByType(MENUTEXTSWITCHMC_PLAYER2JOYSTICK_CREATENAME),
                };
                for (int i = 0; i < _countof(eArr); i++)
                {
                    auto we = eArr[i].lock();
                    if (we)
                    {
                        we->ani.png = we->ani.sprite_index = L"";
                        we->el.flag.text = true;
                        we->SetState(0, 0);
                    }
                }
            }
            break;
        case 1:
            if (index3 == 0)//上
                e = GetEntityByType(MENUTEXTUP_PLAYER2JOYSTICK_CREATENAME);
            else//地图
                e = GetEntityByType(MENUTEXTMAP_PLAYER2JOYSTICK_CREATENAME);
            break;
        case 2:
            if (index3 == 0)//下
                e = GetEntityByType(MENUTEXTDOWN_PLAYER2JOYSTICK_CREATENAME);
            else//跳跃
                e = GetEntityByType(MENUTEXTJUMP_PLAYER2JOYSTICK_CREATENAME);
            break;
        case 3:
            if (index3 == 0)//左
                e = GetEntityByType(MENUTEXTLEFT_PLAYER2JOYSTICK_CREATENAME);
            else//攻击
                e = GetEntityByType(MENUTEXTATK_PLAYER2JOYSTICK_CREATENAME);
            break;
        case 4:
            if (index3 == 0)//右
                e = GetEntityByType(MENUTEXTRIGHT_PLAYER2JOYSTICK_CREATENAME);
            else//技能1
                e = GetEntityByType(MENUTEXTSK1_PLAYER2JOYSTICK_CREATENAME);
            break;
        case 5:
            if (index3 == 0)//确认
                e = GetEntityByType(MENUTEXTOK_PLAYER2JOYSTICK_CREATENAME);
            else//技能2
                e = GetEntityByType(MENUTEXTSK2_PLAYER2JOYSTICK_CREATENAME);
            break;
        case 6:
            if (index3 == 0)//取消
                e = GetEntityByType(MENUTEXTCANCEL_PLAYER2JOYSTICK_CREATENAME);
            else//技能3
                e = GetEntityByType(MENUTEXTSK3_PLAYER2JOYSTICK_CREATENAME);
            break;
        case 7:
            if (index3 == 0)//暂停菜单
                e = GetEntityByType(MENUTEXTMENU_PLAYER2JOYSTICK_CREATENAME);
            else//技能4
                e = GetEntityByType(MENUTEXTSK4_PLAYER2JOYSTICK_CREATENAME);
            break;
        case 8:
            if (index3 == 0)//面板
                e = GetEntityByType(MENUTEXTINVENTORY_PLAYER2JOYSTICK_CREATENAME);
            else//技能5
                e = GetEntityByType(MENUTEXTSK5_PLAYER2JOYSTICK_CREATENAME);
            break;
        case 9:
            if (index3 == 0)//面板 上一页
                e = GetEntityByType(MENUTEXTPAGEPREV_PLAYER2JOYSTICK_CREATENAME);
            else//技能6
                e = GetEntityByType(MENUTEXTSK6_PLAYER2JOYSTICK_CREATENAME);
            break;
        case 10:
            if (index3 == 0)//面板 下一页
                e = GetEntityByType(MENUTEXTPAGENEXT_PLAYER2JOYSTICK_CREATENAME);
            else//切换角色
                e = GetEntityByType(MENUTEXTSWITCHMC_PLAYER2JOYSTICK_CREATENAME);
            break;
        }
        auto we = e.lock();
        if (we)
        {
            we->SetState(1, 0);
            gameFunc->PlaySound(L"MenuOk");
        }
        return;
    }
     else if (opCancel)
     {
         bOnce = true;
         gameFunc->PlaySound(L"MenuCancel");
         ShowPlayer2Menu();
         index = 2;
         menu = 8;
         {
             //修复bug
             std::weak_ptr<Entity> eArr[] = {
                 GetEntityByType(MENUTEXTUP_PLAYER2JOYSTICK_CREATENAME),
                 GetEntityByType(MENUTEXTMAP_PLAYER2JOYSTICK_CREATENAME),
                 GetEntityByType(MENUTEXTDOWN_PLAYER2JOYSTICK_CREATENAME),
                 GetEntityByType(MENUTEXTJUMP_PLAYER2JOYSTICK_CREATENAME),
                 GetEntityByType(MENUTEXTLEFT_PLAYER2JOYSTICK_CREATENAME),
                 GetEntityByType(MENUTEXTATK_PLAYER2JOYSTICK_CREATENAME),
                 GetEntityByType(MENUTEXTRIGHT_PLAYER2JOYSTICK_CREATENAME),
                 GetEntityByType(MENUTEXTSK1_PLAYER2JOYSTICK_CREATENAME),
                 GetEntityByType(MENUTEXTOK_PLAYER2JOYSTICK_CREATENAME),
                 GetEntityByType(MENUTEXTSK2_PLAYER2JOYSTICK_CREATENAME),
                 GetEntityByType(MENUTEXTCANCEL_PLAYER2JOYSTICK_CREATENAME),
                 GetEntityByType(MENUTEXTSK3_PLAYER2JOYSTICK_CREATENAME),
                 GetEntityByType(MENUTEXTMENU_PLAYER2JOYSTICK_CREATENAME),
                 GetEntityByType(MENUTEXTSK4_PLAYER2JOYSTICK_CREATENAME),
                 GetEntityByType(MENUTEXTINVENTORY_PLAYER2JOYSTICK_CREATENAME),
                 GetEntityByType(MENUTEXTSK5_PLAYER2JOYSTICK_CREATENAME),
                 GetEntityByType(MENUTEXTPAGEPREV_PLAYER2JOYSTICK_CREATENAME),
                 GetEntityByType(MENUTEXTSK6_PLAYER2JOYSTICK_CREATENAME),
                 GetEntityByType(MENUTEXTPAGENEXT_PLAYER2JOYSTICK_CREATENAME),
                 GetEntityByType(MENUTEXTSWITCHMC_PLAYER2JOYSTICK_CREATENAME),
             };
             for (int i = 0; i < _countof(eArr); i++)
             {
                 auto we = eArr[i].lock();
                 if (we)
                 {
                     we->ani.png = we->ani.sprite_index = L"";
                     we->el.flag.text = true;
                     we->SetState(0, 0);
                 }
             }
         }
    }
    x = (2 + index3 * 19) * 16.0f + 8;
    y = (6 + index + (index > 0 ? 1 : 0)) * 16.0f + 8;
}

void PauseMenuCursor::SelectControllerMenu()
{
    bool bLoop = false;
    do
    {
        switch (index)
        {
        case 0://是否所有的控制器都放开了按键
            if (gameFunc->GetAnyKeyPress(nullptr) == false &&
                gameFunc->GetAnyGameControllerKeyPress(gameFunc->GetGameControllerFromIndex(0), nullptr) == false &&
                gameFunc->GetAnyGameControllerKeyPress(gameFunc->GetGameControllerFromIndex(1), nullptr) == false)
            {
                index = 1;
                bLoop = true;
                continue;
            }
            break;
        case 1://玩家1确认设备
            if (gameFunc->GetAnyKeyPress(nullptr))
            {
                gameFunc->SetPlayer1ControllerType(0);
                gameFunc->GetInputSetting_Player1Joystick()->pGameController = nullptr;
                index = 2;
                gameFunc->PlaySound(L"MenuOk");
                auto we = GetEntityByType(MENUTEXTP1BALL_CREATENAME).lock();
                if (we)
                    we->SetState(1, 0);
                bLoop = true;
                continue;
            }
            else if (gameFunc->GetAnyGameControllerKeyPress(gameFunc->GetGameControllerFromIndex(0), nullptr))
            {
                gameFunc->SetPlayer1ControllerType(1);
                gameFunc->GetInputSetting_Player1Joystick()->pGameController = gameFunc->GetGameControllerFromIndex(0);
                index = 2;
                gameFunc->PlaySound(L"MenuOk");
                auto we = GetEntityByType(MENUTEXTP1BALL_CREATENAME).lock();
                if (we)
                    we->SetState(2, 0);
                bLoop = true;
                continue;
            }
            else if (gameFunc->GetAnyGameControllerKeyPress(gameFunc->GetGameControllerFromIndex(1), nullptr))
            {
                gameFunc->SetPlayer1ControllerType(1);
                gameFunc->GetInputSetting_Player1Joystick()->pGameController = gameFunc->GetGameControllerFromIndex(1);
                index = 2;
                gameFunc->PlaySound(L"MenuOk");
                auto we = GetEntityByType(MENUTEXTP1BALL_CREATENAME).lock();
                if (we)
                    we->SetState(3, 0);
                bLoop = true;
                continue;
            }
            break;
        case 2://是否所有的控制器都放开了按键
            if (gameFunc->GetAnyKeyPress(nullptr) == false &&
                gameFunc->GetAnyGameControllerKeyPress(gameFunc->GetGameControllerFromIndex(0), nullptr) == false &&
                gameFunc->GetAnyGameControllerKeyPress(gameFunc->GetGameControllerFromIndex(1), nullptr) == false)
            {
                index = 3;
                bLoop = true;
                auto we = GetEntityByType(MENUTEXT_MSG_PLAYERPRESS_CREATENAME).lock();
                if (we)
                    we->SetState(1, 0);
                continue;
            }
            break;
        case 3://玩家2确认设备
            if (gameFunc->GetAnyKeyPress(nullptr))
            {
                gameFunc->SetPlayer2ControllerType(0);
                gameFunc->GetInputSetting_Player2Joystick()->pGameController = nullptr;
                index = 4;
                index3 = 60;
                gameFunc->PlaySound(L"MenuOk");
                auto we = GetEntityByType(MENUTEXTP2BALL_CREATENAME).lock();
                if (we)
                    we->SetState(1, 0);
                bLoop = true;
                continue;
            }
            else if (gameFunc->GetAnyGameControllerKeyPress(gameFunc->GetGameControllerFromIndex(0), nullptr))
            {
                gameFunc->SetPlayer2ControllerType(1);
                gameFunc->GetInputSetting_Player2Joystick()->pGameController = gameFunc->GetGameControllerFromIndex(0);
                index = 4;
                index3 = 60;
                gameFunc->PlaySound(L"MenuOk");
                auto we = GetEntityByType(MENUTEXTP2BALL_CREATENAME).lock();
                if (we)
                    we->SetState(2, 0);
                bLoop = true;
                continue;
            }
            else if (gameFunc->GetAnyGameControllerKeyPress(gameFunc->GetGameControllerFromIndex(1), nullptr))
            {
                gameFunc->SetPlayer2ControllerType(1);
                gameFunc->GetInputSetting_Player2Joystick()->pGameController = gameFunc->GetGameControllerFromIndex(1);
                index = 4;
                index3 = 60;
                gameFunc->PlaySound(L"MenuOk");
                auto we = GetEntityByType(MENUTEXTP2BALL_CREATENAME).lock();
                if (we)
                    we->SetState(3, 0);
                bLoop = true;
                continue;
            }
            break;
        case 4://等待1秒
            if (index3 > 0)
                index3--;
            else
            {
                index = 5;
                bLoop = true;
                continue;
            }
            break;
        case 5://返回上级菜单
            gameFunc->PlaySound(L"MenuCancel");
            ShowControlMenu();
            index = 3;
            menu = 4;
            {
                auto we = GetEntityByType(MENUTEXT_MSG_PLAYERPRESS_CREATENAME).lock();
                if (we)
                    we->SetState(0, 0);

                we = GetEntityByType(MENUTEXTP1BALL_CREATENAME).lock();
                if (we)
                {
                    we->SetState(0, 0);
                    auto b = std::static_pointer_cast<MenuTextP1Ball>(we);
                    b->Reset();
                }

                we = GetEntityByType(MENUTEXTP2BALL_CREATENAME).lock();
                if (we)
                {
                    we->SetState(0, 0);
                    auto b = std::static_pointer_cast<MenuTextP2Ball>(we);
                    b->Reset();
                }
                    
            }
            break;
        }
        bLoop = false;
    } while (bLoop);
    
    x = (10 ) * 16.0f + 8;
    y = (3 ) * 16.0f + 8;
}

std::weak_ptr<Entity> PauseMenuCursor::GetEntityByType(const wchar_t* name)
{
    auto loadedEntity = gameFunc->GetLoadedEntity(gameFunc->entityRes);
    for (auto& p : *loadedEntity)
    {
        for (auto& e : p.second.Ls)
        {
            if (e->el.name == name)
                return e;
        }
    }
    return std::weak_ptr<Entity>();
}

void PauseMenuCursor::HideAllMenuLayer()
{
    static const std::wstring layerNameList[] =
    {
        L"pausemenu",
        L"option",
        L"graphics",
        L"audio",
        L"control",
        L"player1",
        L"player1Keyboard",
        L"player1Joystick",
        L"player2",
        L"player2Keyboard",
        L"player2Joystick",
        L"selectController",
    };
    for (auto& layerName : layerNameList)
        gameFunc->GetMapLayer(gameFunc->gameMap, layerName.c_str())->visible = false;
}

void PauseMenuCursor::ShowMainMenu()
{
    HideAllMenuLayer();
    MapLayer* mapLayer = gameFunc->GetMapLayer(gameFunc->gameMap, L"bk");
    mapLayer->clipZone.left = 192;
    mapLayer->clipZone.right = 448;
    mapLayer->clipZone.top = 112;
    mapLayer->clipZone.bottom = 256;
    mapLayer->enableClipZone = true;
    gameFunc->GetMapLayer(gameFunc->gameMap, L"pausemenu")->visible = true;
}

void PauseMenuCursor::ShowOpMenu()
{
    HideAllMenuLayer();
    gameFunc->GetMapLayer(gameFunc->gameMap, L"bk")->enableClipZone = false;
    gameFunc->GetMapLayer(gameFunc->gameMap, L"option")->visible = true;
}

void PauseMenuCursor::ShowGraphMenu()
{
    HideAllMenuLayer();
    gameFunc->GetMapLayer(gameFunc->gameMap, L"bk")->enableClipZone = false;
    gameFunc->GetMapLayer(gameFunc->gameMap, L"graphics")->visible = true;
}

void PauseMenuCursor::ShowAudioMenu()
{
    HideAllMenuLayer();
    gameFunc->GetMapLayer(gameFunc->gameMap, L"bk")->enableClipZone = false;
    gameFunc->GetMapLayer(gameFunc->gameMap, L"audio")->visible = true;
}

void PauseMenuCursor::ShowControlMenu()
{
    HideAllMenuLayer();
    gameFunc->GetMapLayer(gameFunc->gameMap, L"bk")->enableClipZone = false;
    gameFunc->GetMapLayer(gameFunc->gameMap, L"control")->visible = true;
}

void PauseMenuCursor::ShowPlayer1Menu()
{
    HideAllMenuLayer();
    gameFunc->GetMapLayer(gameFunc->gameMap, L"bk")->enableClipZone = false;
    gameFunc->GetMapLayer(gameFunc->gameMap, L"player1")->visible = true;
}

void PauseMenuCursor::ShowPlayer1KeyboardMenu()
{
    HideAllMenuLayer();
    gameFunc->GetMapLayer(gameFunc->gameMap, L"bk")->enableClipZone = false;
    gameFunc->GetMapLayer(gameFunc->gameMap, L"player1Keyboard")->visible = true;
}

void PauseMenuCursor::ShowPlayer1JoystickMenu()
{
    HideAllMenuLayer();
    gameFunc->GetMapLayer(gameFunc->gameMap, L"bk")->enableClipZone = false;
    gameFunc->GetMapLayer(gameFunc->gameMap, L"player1Joystick")->visible = true;
}

void PauseMenuCursor::ShowPlayer2Menu()
{
    HideAllMenuLayer();
    gameFunc->GetMapLayer(gameFunc->gameMap, L"bk")->enableClipZone = false;
    gameFunc->GetMapLayer(gameFunc->gameMap, L"player2")->visible = true;
}

void PauseMenuCursor::ShowPlayer2KeyboardMenu()
{
    HideAllMenuLayer();
    gameFunc->GetMapLayer(gameFunc->gameMap, L"bk")->enableClipZone = false;
    gameFunc->GetMapLayer(gameFunc->gameMap, L"player2Keyboard")->visible = true;
}

void PauseMenuCursor::ShowPlayer2JoystickMenu()
{
    HideAllMenuLayer();
    gameFunc->GetMapLayer(gameFunc->gameMap, L"bk")->enableClipZone = false;
    gameFunc->GetMapLayer(gameFunc->gameMap, L"player2Joystick")->visible = true;
}

void PauseMenuCursor::ShowSelectControllerMenu()
{
    HideAllMenuLayer();
    gameFunc->GetMapLayer(gameFunc->gameMap, L"bk")->enableClipZone = false;
    gameFunc->GetMapLayer(gameFunc->gameMap, L"selectController")->visible = true;
}

void PauseMenuCursor::ClearOp()
{
    opU = opD = opL = opR = opOk = opCancel = opMenu = opPageUp = opPageDown = false;
}

void PauseMenuCursor::GetOp()
{
    std::weak_ptr<Entity> players[4];
    players[0] = gameFunc->GetMyChar_Player1(gameFunc->entityRes);
    players[1] = gameFunc->GetMyChar_Player1_menuKeyboard(gameFunc->entityRes);
    players[2] = gameFunc->GetMyChar_Player2(gameFunc->entityRes);
    players[3] = gameFunc->GetMyChar_Player2_menuKeyboard(gameFunc->entityRes);
    for (auto i = 0; i < _countof(players); i++)
    {
        auto wplayer = players[i].lock();
        if (wplayer)
        {
            if (wplayer->input.GetUp())
            {
                wplayer->input.SetUp(false);
                wplayer->inputLock.SetUp(true);
                opU = true;
            }
            else if (wplayer->input.GetDown())
            {
                wplayer->input.SetDown(false);
                wplayer->inputLock.SetDown(true);
                opD = true;
            }
            if (wplayer->input.GetLeft())
            {
                wplayer->input.SetLeft(false);
                wplayer->inputLock.SetLeft(true);
                opL = true;
            }
            else if (wplayer->input.GetRight())
            {
                wplayer->input.SetRight(false);
                wplayer->inputLock.SetRight(true);
                opR = true;
            }
            if (wplayer->input.GetOk())
            {
                wplayer->input.SetOk(false);
                wplayer->inputLock.SetOk(true);
                opOk = true;
            }
            if (wplayer->input.GetCancel())
            {
                wplayer->input.SetCancel(false);
                wplayer->inputLock.SetCancel(true);
                opCancel = true;
            }
            if (wplayer->input.GetMenu())
            {
                wplayer->input.SetMenu(false);
                wplayer->inputLock.SetMenu(true);
                opMenu = true;
            }
            if (wplayer->input.GetPagePrev())
            {
                wplayer->input.SetPagePrev(false);
                wplayer->inputLock.SetPagePrev(true);
                opPageUp = true;
            }
            if (wplayer->input.GetPageNext())
            {
                wplayer->input.SetPageNext(false);
                wplayer->inputLock.SetPageNext(true);
                opPageDown = true;
            }
        }
    }
}

std::shared_ptr<Entity> CreatePauseMenuCursor()
{
    return std::make_shared<PauseMenuCursor>();
}
