#include "MenuCursor.h"
#include "MenuTextSlotEmpty.h"
#include "MenuTextSlotRoomName.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"
MenuCursor::MenuCursor()
{
    ani.png = L"CS_CaveStoryMod";
    ani.sprite_index = L"mc_kanpa_move_r";
    ani.image_counter_speed = 0.5f;
}

void MenuCursor::Step()
{
    if (blockControl > 0)
    {
        x = 18 * 16.0f + 8;
        y = (14 + index) * 16.0f + 8;
        blockControl--;
        //执行运动
        Entity::Step();
        return;
    }
    if (gameFunc && bInitFindEntity)
    {
        auto loadedEntity = gameFunc->GetLoadedEntity(gameFunc->entityRes);
        for (auto& p : *loadedEntity)
        {
            for (auto& e : p.second.Ls)
            {
                if (e->el.eventName == L"slotPageNum")
                {
                    e_slotPageNum = e;
                }
                else if (e->el.name == MENUTEXTSLOTEMPTY_CREATENAME)
                {
                    if (e->el.eventName == L"slotA")
                    {
                        e_slotA = e;
                    }
                    else if (e->el.eventName == L"slotB")
                    {
                        e_slotB = e;
                    }
                    else if (e->el.eventName == L"slotC")
                    {
                        e_slotC = e;
                    }
                }
                else if (e->el.name == MENUTEXTSLOTROOMNAME_CREATENAME)
                {
                    if (e->el.eventName == L"slotA")
                    {
                        e_slotA_RoomName = e;
                    }
                    else if (e->el.eventName == L"slotB")
                    {
                        e_slotB_RoomName = e;
                    }
                    else if (e->el.eventName == L"slotC")
                    {
                        e_slotC_RoomName = e;
                    }
                }
                else if (e->el.eventName == L"SlotAPlayer1Icon")
                {
                    e_slotA_p1Icon = e;
                }
                else if (e->el.eventName == L"SlotAPlayer2Icon")
                {
                    e_slotA_p2Icon = e;
                }
                else if (e->el.eventName == L"SlotBPlayer1Icon")
                {
                    e_slotB_p1Icon = e;
                }
                else if (e->el.eventName == L"SlotBPlayer2Icon")
                {
                    e_slotB_p2Icon = e;
                }
                else if (e->el.eventName == L"SlotCPlayer1Icon")
                {
                    e_slotC_p1Icon = e;
                }
                else if (e->el.eventName == L"SlotCPlayer2Icon")
                {
                    e_slotC_p2Icon = e;
                }
            }
        }
        gameFunc->GetLastPlaySlot(&slotPage, &index2);
        if (gameFunc->SlotPageReset)
            gameFunc->SlotPageReset(gameFunc->slotDrawer);
        UpdateSlotPage(false);
        bInitFindEntity = false;
    }
    if (!runCursor)
    {
        BlockFirstOkOp();
        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;
        case 12://语言
            LanguageMenu();
            break;
        case 13://存档界面
            SlotMenu();
            break;
        case 14://选择模式界面
            SelectModeMenu();
            break;
        }
    }while (bOnce);
    //执行运动
    Entity::Step();
}

void MenuCursor::SetState(int stateID, int lr)
{
    if (stateID == 1)
    {
        bInitFindEntity = true;
    }
}

void MenuCursor::InitLanguageMenu()
{
    if (!bInitLanguage)
        return;
    std::vector<std::wstring> languageList;
    gameFunc->GetAllLanguage(&languageList);
    languageNum = (int)languageList.size();
    auto loadedEntity = gameFunc->GetLoadedEntity(gameFunc->entityRes);
    std::shared_ptr<Entity> wlayerE;
    for (auto& p : *loadedEntity)
    {
        for (auto& e : p.second.Ls)
        {
            if (e->el.eventName == L"title_language")
            {
                wlayerE = e;
                goto MenuCursor_InitLanguageMenu_Find;
            }
        }
    }
MenuCursor_InitLanguageMenu_Find:
    if (wlayerE)
    {
        float offX = 19 * 16 + 8;
        float offY = 11 * 16 + 8;
        for (auto& lang : languageList)
        {
            auto we = std::make_shared<Entity>();
            we->x = offX;
            we->y = offY;
            we->el.flag.text = true;
            we->el.eid = lang.c_str();
            gameFunc->SetEntity(wlayerE, we);
            offY += 16;
        }
    }
    bInitLanguage = false;
}

void MenuCursor::BlockFirstOkOp()
{
    x = 18 * 16.0f + 8;
    y = (13 + index) * 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.SetOk(true);
                wplayer->input.SetOk(false);
            }
        }
    }
}

void MenuCursor::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"MenuOk");
            ShowSlotMenu();
            index = 1;
            menu = 13;
            index4 = 5;
            break;
        case 1://选项
            gameFunc->PlaySound(L"MenuOk");
            ShowOpMenu();
            index = 0;
            menu = 1;
            break;
        case 2://退出
            gameFunc->PlaySound(L"MenuOk");
            gameFunc->QuitGame();
            break;
        }
        return;
    }
    x = 18 * 16.0f + 8;
    y = (14 + index) * 16.0f + 8;
}

void MenuCursor::OpMenu()
{
    //初始化语言菜单
    InitLanguageMenu();
    //
    ClearOp();
    GetOp();
    if (opU)
    {
        if (index > 0)
            index--;
        else
            index = 4;
        gameFunc->PlaySound(L"MenuOk");
    }
    else if (opD)
    {
        if (index < 4)
            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;
        case 4://语言
            gameFunc->PlaySound(L"MenuOk");
            ShowLanguageMenu();
            index = 0;
            menu = 12;
            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 MenuCursor::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 MenuCursor::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 MenuCursor::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 MenuCursor::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 MenuCursor::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 MenuCursor::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_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);
                    }
                }
            }
            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 MenuCursor::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 MenuCursor::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 MenuCursor::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 MenuCursor::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;
}

void MenuCursor::LanguageMenu()
{
    ClearOp();
    GetOp();
    if (opU)
    {
        if (index > 0)
            index--;
        else
            index = languageNum;
        gameFunc->PlaySound(L"MenuOk");
    }
    else if (opD)
    {
        if (index < languageNum)
            index++;
        else
            index = 0;
        gameFunc->PlaySound(L"MenuOk");
    }
    if (opOk)
    {
        bOnce = true;
        if (index == 0)
        {
            //返回
            gameFunc->PlaySound(L"MenuCancel");
            ShowOpMenu();
            index = 4;
            menu = 1;
        }
        else
        {
            //设置为其它语言
            std::vector<std::wstring> languageList;
            gameFunc->GetAllLanguage(&languageList);
            gameFunc->SetLanguage(languageList.at(index - 1).c_str());
            gameFunc->PlaySound(L"MenuOk");
            gameFunc->SaveConfig();
        }
    }
    else if (opCancel || opMenu)
    {
        //返回
        gameFunc->PlaySound(L"MenuCancel");
        ShowOpMenu();
        index = 4;
        menu = 1;
        bOnce = true;
        return;
    }
    x = 18 * 16.0f + 8;
    y = (9 + index + (index > 0 ? 1 : 0)) * 16.0f + 8;
}

void MenuCursor::SlotMenu()
{
    if (SlotMenuSlideControl())
        return;
    ClearOp();
    GetOp();
    if (opL)
    {
        //向左功能区移动
        if (index > 0)
            index--;
    }
    else if (opR)
    {
        //向右功能区移动
        if (index < 2)
            index++;
    }
    switch (index)
    {
    case 0://翻页功能区
        if (opU)
        {
            if (index4 > 0)
                index4--;
            else
                index4 = 5;
            gameFunc->PlaySound(L"MenuOk");
        }
        else if (opD)
        {
            if (index4 < 5)
                index4++;
            else
                index4 = 0;
            gameFunc->PlaySound(L"MenuOk");
        }
        x = 1 * 16.0f + 8;
        y = (14 + index4 + 
            (index4 > 0 ? 1 : 0) +
            (index4 > 4 ? 1 : 0)
            ) * 16.0f + 8;
        break;
    case 1://存档选择功能区
    case 2://存档删除功能区
        if (opU)
        {
            if (index2 > 0)
            {
                index2--;
                gameFunc->PlaySound(L"MenuOk");
            }
            else
            {
                index2 = 2;
                opPageUp = true;
            }   
           
        }
        else if (opD)
        {
            if (index2 < 2)
            {
                index2++;
                gameFunc->PlaySound(L"MenuOk");
            } 
            else
            {
                index2 = 0;
                opPageDown = true;
            }
        }
        if (index == 1)
            x = 8 * 16.0f + 8;
        else
            x = 31 * 16.0f + 8;
        y = (8 + index2 * 5) * 16.0f;
        break;
    }
    if (opPageUp)
    {
        //上一页
        if (slotPage > 0)
            slotPage--;
        else
            slotPage = 99;
        gameFunc->PlaySound(L"MenuOk");
    }
    if (opPageDown)
    {
        //下一页
        if (slotPage < 99)
            slotPage++;
        else
            slotPage = 0;
        gameFunc->PlaySound(L"MenuOk");
    }
    if (opOk)
    {
        bOnce = true;
        switch (index)
        {
        case 0://翻页功能区
            switch (index4)
            {
            case 0://滑动（滑动不支持点击）
                break;
            case 1://首页
                slotPage = 0;
                gameFunc->PlaySound(L"MenuOk");
                break;
            case 2://上一页
                if (slotPage > 0)
                    slotPage--;
                else
                    slotPage = 99;
                gameFunc->PlaySound(L"MenuOk");
                break;
            case 3://下一页
                if (slotPage < 99)
                    slotPage++;
                else
                    slotPage = 0;
                gameFunc->PlaySound(L"MenuOk");
                break;
            case 4://尾页
                slotPage = 99;
                gameFunc->PlaySound(L"MenuOk");
                break;
            case 5://返回
                gameFunc->PlaySound(L"MenuCancel");
                ShowMainMenu();
                index = 0;
                menu = 0;
                bOnce = true;
                break;
            }
            break;
        case 1://存档选择功能区
            //载入游戏存档（设置游戏存档名）
            switch (index2)
            {
            case 0://SlotA
                {
                    auto we_slotA = e_slotA.lock();
                    if (we_slotA)
                    {
                        if (we_slotA->ani.sprite_index == L"slotempty")
                        {
                            //进入选择模式菜单
                            ShowSelectModeMenu();
                            difficulty = 1;
                            gameName = L"MimigaMissingEvents";
                            menu = 14;
                            index = 0;
                            //记录存档名
                            saveName = we_slotA->el.eventName.c_str();
                            gameFunc->PlaySound(L"MenuOk");
                        }
                        else
                        {
                            //直接载入游戏
                            gameFunc->SetSaveName(we_slotA->el.eventName.c_str());
                            gameFunc->SetLastPlaySlot(slotPage, index2);
                            gameFunc->SaveConfig();
                            gameFunc->PlaySound(L"MenuOk");
                        }
                    }
                }
                break;
            case 1://SlotB
                {
                    auto we_slotB = e_slotB.lock();
                    if (we_slotB)
                    {
                        if (we_slotB->ani.sprite_index == L"slotempty")
                        {
                            //进入选择模式菜单
                            ShowSelectModeMenu();
                            difficulty = 1;
                            gameName = L"MimigaMissingEvents";
                            menu = 14;
                            index = 0;
                            //记录存档名
                            saveName = we_slotB->el.eventName.c_str();
                            gameFunc->PlaySound(L"MenuOk");
                        }
                        else
                        {
                            //直接载入游戏
                            gameFunc->SetSaveName(we_slotB->el.eventName.c_str());
                            gameFunc->SetLastPlaySlot(slotPage, index2);
                            gameFunc->SaveConfig();
                            gameFunc->PlaySound(L"MenuOk");
                        }
                    }
                }
                break;
            case 2://SlotC
                {
                    auto we_slotC = e_slotC.lock();
                    if (we_slotC)
                    {
                        if (we_slotC->ani.sprite_index == L"slotempty")
                        {
                            //进入选择模式菜单
                            ShowSelectModeMenu();
                            difficulty = 1;
                            gameName = L"MimigaMissingEvents";
                            menu = 14;
                            index = 0;
                            //记录存档名
                            saveName = we_slotC->el.eventName.c_str();
                            gameFunc->PlaySound(L"MenuOk");
                        }
                        else
                        {
                            //直接载入游戏
                            gameFunc->SetSaveName(we_slotC->el.eventName.c_str());
                            gameFunc->SetLastPlaySlot(slotPage, index2);
                            gameFunc->SaveConfig();
                            gameFunc->PlaySound(L"MenuOk");
                        }
                    }
                }
                break;
            }
            break;
        case 2://存档删除功能区
            switch (index2)
            {
            case 0://SlotA
                {
                    auto we_slotA = e_slotA.lock();
                    if (we_slotA)
                    {
                        gameFunc->DelSave(we_slotA->el.eventName.c_str());
                        we_slotA->el.eventName = L"";
                    }
                    auto we_slotA_RoomName = e_slotA_RoomName.lock();
                    if (we_slotA_RoomName)
                        we_slotA_RoomName->el.eventName = L"";
                }
                break;
            case 1://SlotB
                {
                    auto we_slotB = e_slotB.lock();
                    if (we_slotB)
                    {
                        gameFunc->DelSave(we_slotB->el.eventName.c_str());
                        we_slotB->el.eventName = L"";
                    }
                    auto we_slotB_RoomName = e_slotB_RoomName.lock();
                    if (we_slotB_RoomName)
                        we_slotB_RoomName->el.eventName = L"";
                }
                break;
            case 2://SlotC
                {
                    auto we_slotC = e_slotC.lock();
                    if (we_slotC)
                    {
                        gameFunc->DelSave(we_slotC->el.eventName.c_str());
                        we_slotC->el.eventName = L"";
                    }
                    auto we_slotC_RoomName = e_slotC_RoomName.lock();
                    if (we_slotC_RoomName)
                        we_slotC_RoomName->el.eventName = L"";
                }
                break;
            }
            gameFunc->PlaySound(L"MenuCancel");
            gameFunc->SlotPageReset(gameFunc->slotDrawer);
            UpdateSlotPage(false);
            break;
        }
    }
    if (opCancel || opMenu)
    {
        gameFunc->PlaySound(L"MenuCancel");
        ShowMainMenu();
        index = 0;
        menu = 0;
        bOnce = true;
        return;
    }
    UpdateSlotPage(true);
}

void MenuCursor::SelectModeMenu()
{
    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;
        std::wstring bootName = gameName;
        switch (index)
        {
        case 0://单人
            gameFunc->SetNewNameOption(bootName.c_str(), 1, difficulty);
            gameFunc->SetSaveName(saveName.c_str());
            gameFunc->SetLastPlaySlot(slotPage, index2);
            gameFunc->SaveConfig();
            break;
        case 1://双人
            bootName.append(L"_2Player");
            gameFunc->SetNewNameOption(bootName.c_str(), 2, difficulty);
            gameFunc->SetSaveName(saveName.c_str());
            gameFunc->SetLastPlaySlot(slotPage, index2);
            gameFunc->SaveConfig();
            break;
        case 2://返回
            gameFunc->PlaySound(L"MenuCancel");
            ShowSlotMenu();
            menu = 13;
            index = 1;
            bOnce = true;
            break;
        }
    }
    if (opCancel || opMenu)
    {
        gameFunc->PlaySound(L"MenuCancel");
        ShowSlotMenu();
        menu = 13;
        index = 1;
        bOnce = true;
        return;
    }
    x = 1 * 16.0f + 8;
    y = (6 + index +
        (index > 1 ? 13 : 0)) * 16.0f + 8;
}

std::weak_ptr<Entity> MenuCursor::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 MenuCursor::HideAllMenuLayer()
{
    static const std::wstring layerNameList[] = 
    {
        L"title",
        L"option",
        L"graphics",
        L"audio",
        L"control",
        L"player1",
        L"player1Keyboard",
        L"player1Joystick",
        L"player2",
        L"player2Keyboard",
        L"player2Joystick",
        L"selectController",
        L"language",
        L"slotMenu",
        L"selectMode",
    };
    for (auto& layerName : layerNameList)
        gameFunc->GetMapLayer(gameFunc->gameMap, layerName.c_str())->visible = false;
}

void MenuCursor::ShowMainMenu()
{
    HideAllMenuLayer();
    gameFunc->GetMapLayer(gameFunc->gameMap, L"title")->visible = true;
}

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

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

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

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

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

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

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

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

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

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

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

void MenuCursor::ShowLanguageMenu()
{
    HideAllMenuLayer();
    gameFunc->GetMapLayer(gameFunc->gameMap, L"language")->visible = true;
}

void MenuCursor::ShowSlotMenu()
{
    HideAllMenuLayer();
    gameFunc->GetMapLayer(gameFunc->gameMap, L"slotMenu")->visible = true;
}

void MenuCursor::ShowSelectModeMenu()
{
    HideAllMenuLayer();
    gameFunc->GetMapLayer(gameFunc->gameMap, L"selectMode")->visible = true;
}

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

void MenuCursor::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;
            }
        }
    }
}

void MenuCursor::UpdateSlotPage(bool bDrawSlot)
{
    auto we_slotPageNum = e_slotPageNum.lock();
    if (we_slotPageNum)
    {
        wchar_t wbuff[32];
        swprintf_s(wbuff, L"<%02d>", slotPage);
        we_slotPageNum->el.eid = wbuff;
        we_slotPageNum->x = 319;
        we_slotPageNum->y = 344;
        if (bDrawSlot && gameFunc->slotDrawer)
            gameFunc->SlotPageDrawClear(gameFunc->slotDrawer);
        auto we_slotA = e_slotA.lock();
        if (we_slotA)
        {
            swprintf_s(wbuff, L"slotA_%02d", slotPage);
            //绘制存档
            if (bDrawSlot && gameFunc->slotDrawer)
                gameFunc->SlotPageAddDraw(gameFunc->slotDrawer, wbuff, e_slotA_p1Icon, e_slotA_p2Icon);
            if (we_slotA->el.eventName != wbuff)
            {
                we_slotA->el.eventName = wbuff;
                we_slotA->SetState(0, 1);//存档检查
                we_slotA->Step();
            }
        }
        auto we_slotB = e_slotB.lock();
        if (we_slotB)
        {
            swprintf_s(wbuff, L"slotB_%02d", slotPage);
            //绘制存档
            if (bDrawSlot && gameFunc->slotDrawer)
                gameFunc->SlotPageAddDraw(gameFunc->slotDrawer, wbuff, e_slotB_p1Icon, e_slotB_p2Icon);
            if(we_slotB->el.eventName != wbuff)
            {
                we_slotB->el.eventName = wbuff;
                we_slotB->SetState(0, 1);//存档检查
                we_slotB->Step();
            }
           
        }
        auto we_slotC = e_slotC.lock();
        if (we_slotC)
        {
            swprintf_s(wbuff, L"slotC_%02d", slotPage);
            //绘制存档
            if (bDrawSlot && gameFunc->slotDrawer)
                gameFunc->SlotPageAddDraw(gameFunc->slotDrawer, wbuff, e_slotC_p1Icon, e_slotC_p2Icon);
            if (we_slotC->el.eventName != wbuff)
            {
                we_slotC->el.eventName = wbuff;
                we_slotC->SetState(0, 1);//存档检查
                we_slotC->Step();
            }
        }
        auto we_slotA_RoomName = e_slotA_RoomName.lock();
        if (we_slotA_RoomName)
        {
            swprintf_s(wbuff, L"slotA_%02d", slotPage);
            if (we_slotA_RoomName->el.eventName != wbuff)
            {
                we_slotA_RoomName->el.eventName = wbuff;
                we_slotA_RoomName->SetState(0, 1);//存档检查
                we_slotA_RoomName->Step();
            }
        }
        auto we_slotB_RoomName = e_slotB_RoomName.lock();
        if (we_slotB_RoomName)
        {
            swprintf_s(wbuff, L"slotB_%02d", slotPage);
            if (we_slotB_RoomName->el.eventName != wbuff)
            {
                we_slotB_RoomName->el.eventName = wbuff;
                we_slotB_RoomName->SetState(0, 1);//存档检查
                we_slotB_RoomName->Step();
            }
        }
        auto we_slotC_RoomName = e_slotC_RoomName.lock();
        if (we_slotC_RoomName)
        {
            swprintf_s(wbuff, L"slotC_%02d", slotPage);
            if (we_slotC_RoomName->el.eventName != wbuff)
            {
                we_slotC_RoomName->el.eventName = wbuff;
                we_slotC_RoomName->SetState(0, 1);//存档检查
                we_slotC_RoomName->Step();
            }
        }
    }
}

bool MenuCursor::SlotMenuSlideControl()
{
    //如果是滑动功能
    if (slideInterval > 0)
    {
        slideInterval--;
        return true;
    }
    if (slideInterval == 0)
    {
        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);
        bool bOk = false;
        bool bL = false;
        bool bR = false;
        for (auto i = 0; i < _countof(players); i++)
        {
            auto we = players[i].lock();
            if (we)
            {
                if (we->input.GetOk())
                    bOk = true;
                if (we->input.GetLeft())
                    bL = true;
                if (we->input.GetRight())
                    bR = true;
            }
        }
        if (index == 0 && index4 == 0)
        {
            if (bL)
            {
                if (slotPage > 0)
                    slotPage--;
                else
                    slotPage = 99;
                gameFunc->PlaySound(L"MenuOk");
                UpdateSlotPage(false);
                if (bOk)
                    return true;
                else
                    slideInterval = 5;
                return true;

            }
            else if (bR)
            {
                if (slotPage < 99)
                    slotPage++;
                else
                    slotPage = 0;
                gameFunc->PlaySound(L"MenuOk");
                UpdateSlotPage(false);
                if (bOk)
                    return true;
                else
                    slideInterval = 5;
                return true;
            }
        }
        x = 1 * 16.0f + 8;
        y = (14 + index4 +
            (index4 > 0 ? 1 : 0) +
            (index4 > 4 ? 1 : 0)
            ) * 16.0f + 8;
    }
    if (index == 0 && index4 == 0)
    {
        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);
        bool oU = false;
        bool oD = false;
        bool oCancel = false;
        bool oMenu = false;
        for (auto i = 0; i < _countof(players); i++)
        {
            auto we = players[i].lock();
            if (we)
            {
                if (we->input.GetUp())
                {
                    oU = true;
                    we->inputLock.SetUp(true);
                }
                else if (we->input.GetDown())
                {
                    oD = true;
                    we->inputLock.SetDown(true);
                }
                if (we->input.GetCancel())
                {
                    we->input.SetCancel(false);
                    we->inputLock.SetCancel(true);
                    oCancel = true;
                }
                if (we->input.GetMenu())
                {
                    we->input.SetMenu(false);
                    we->inputLock.SetMenu(true);
                    oMenu = true;
                }
            }
        }
        if (oU)
        {
            if (index4 > 0)
                index4--;
            else
                index4 = 5;
            gameFunc->PlaySound(L"MenuOk");
        }
        else if (oD)
        {
            if (index4 < 5)
                index4++;
            else
                index4 = 0;
            gameFunc->PlaySound(L"MenuOk");
        }

        if (oCancel || oMenu)
        {
            gameFunc->PlaySound(L"MenuCancel");
            ShowMainMenu();
            index = 0;
            menu = 0;
            bOnce = true;
            return true;
        }
        return true;
    }
    return false;
}

std::shared_ptr<Entity> CreateMenuCursor()
{
    return std::make_shared<MenuCursor>();
}
