/*
文件名:1_Select_Character.nut
路径:User/UI/Window/1_Select_Character.nut
创建日期:2024-12-13	15:45
文件用途:选择角色界面窗口
*/
class _Select_Character_SettingBackground_Object_Window extends Yosin_CommonUi {

    //编号
    Idx = 0;

    //蓝色悬停框
    RectMask = null;
    //黄色选中框
    SelectMask = null;
    //选中状态
    SelectFlag = false;


    constructor(x, y, width, height, gIdx) {
        base.constructor(x, y, width, height);
        //编号
        Idx = gIdx;

        //注册绘制
        RegisterDraw();
    }

    function RegisterDraw() {
        //背景
        local Background = CL_SpriteObject("sprite/interface2/selectcharacter_ver2/background/small/background_small_event.img", Idx);
        Background.SetScale(0.7, 0.7);
        Addchild(Background);

        RectMask = CL_SpriteObject("sprite/interface2/selectcharacter_ver2/setup/setup.img", 18);
        RectMask.SetVisible(false);
        Addchild(RectMask);

        SelectMask = CL_SpriteObject("sprite/interface2/selectcharacter_ver2/setup/setup.img", 19);
        SelectMask.SetPosition(-2, -2);
        SelectMask.SetVisible(false);
        Addchild(SelectMask);
    }

    //override
    function Proc(Dt) {
        base.Proc(Dt);
        if (isInRect && !SelectFlag) {
            RectMask.SetVisible(true);
        } else {
            RectMask.SetVisible(false);
        }

    }


    //鼠标左键单击回调
    function OnMouseLbClick(MousePos_X, MousePos_Y) {
        base.OnMouseLbClick(MousePos_X, MousePos_Y);
        if (isInRect) {
            //必须是在框的范围内
            if (MousePos_Y > Parent.Y && MousePos_Y<(Parent.Y + Parent.Height)) {
                //遍历父对象中的所有按钮 还原其他按钮
                foreach(Button in Parent.SettingBackgroundButtonList) {
                    Button.SelectMask.SetVisible(false);
                    Button.SelectFlag = false;
                }
                //设置自身选中状态
                SelectMask.SetVisible(true);
                SelectFlag = true;

                Parent.Parent.ChangeBackground(Idx);
            }
        }
    }
}
class _Select_Character_SettingBackground_Window extends Yosin_Window {

    //是否为独立窗口
    IsIndependent = false;

    //是否可见
    Visible = false;

    //选择背景按钮集合
    SettingBackgroundButtonList = null;

    //背景对象
    Background = null;

    ScrollObject = null;

    constructor(gObjectId, gX, gY, gWidth, gHeight, gTitleH) {
        base.constructor(gObjectId, gX, gY, gWidth, gHeight, gTitleH);
        SettingBackgroundButtonList = [];

        SetClipRect(5, 9, gWidth, gHeight - 9);
    }

    function Init() {
        //注册绘制
        RegisterDraw();
    }

    function RegisterDraw() {
        //背景
        Background = CL_SpriteObject("sprite/interface2/selectcharacter_ver2/setup/setup.img", 17);
        Background.SetPosition(X, Y);
        Background.SetZOrder(-10);
        Background.SetVisible(false);
        //因为裁切原因 所以要添加到父对象中
        Parent.Addchild(Background);

        for (local i = 0; i< 24; i++) {
            local Buf = _Select_Character_SettingBackground_Object_Window(5 + (101 * (i % 2)), 9 + (61 * (i / 2)), 91, 51, i);
            SettingBackgroundButtonList.push(Buf);
            //如果是父对象默认构造的默认是勾选状态
            if (i == Parent.Info.loginImg) {
                Buf.SelectMask.SetVisible(true);
                Buf.SelectFlag = true;
            }
            AddUIChild(Buf);
        }

        ScrollObject = Yosin_ScrollBar(Width - 13, 5, Height - 8, 20);
        ScrollObject.SetChangeCallBack(function(Value) {
            foreach(Pos, Button in SettingBackgroundButtonList) {
                Button.SyncPos(5 + (101 * (Pos % 2)), 9 + (61 * (Pos / 2)) - Value * (61 * 12));
            }
        }.bindenv(this));
        AddUIChild(ScrollObject);
    }


    //override
    //设置是否可见 通过重载设置可见同步Background的可见性
    function SetVisible(Flag) {
        base.SetVisible(Flag);
        if (Flag) {
            if (Background) Background.SetVisible(true);
        } else {
            if (Background) Background.SetVisible(false);
        }
    }

}
//角色
class _Select_Character_Chr extends Yosin_Window {
    //是否为独立窗口
    IsIndependent = false;

    Info = null;

    constructor(gObjectId, gX, gY, gWidth, gHeight, gTitleH) {
        base.constructor(gObjectId, gX, gY, gWidth, gHeight, gTitleH);
        RegisterDraw();
    }

    function Init(Info) {
        this.Info = Info;
        RegisterCharac();
    }

    function RegisterDraw() {
        local Magic = CL_SpriteObject("sprite/interface2/selectcharacter_ver2/selectcharacter.img", 10);
        Addchild(Magic);
    }

    function RegisterCharac() {
        local Charc = GameObject.CreateCharacter(Info.job, Info.equip);
        Charc.SetAnimation("RestAni");
        Charc.SetPosition(48, 12, 0);
        Addchild(Charc);
    }

}
class _Select_Character_Window extends Yosin_Window {
    //调试模式
    // DeBugMode = true;

    //不是窗口
    // NoWindow = true;

    //是否可见
    // Visible = false;

    //背景音乐
    BackGroundMusic = null

    //设置背景图片窗口
    SettingBackgroundWindow = null;
    //背景图片
    BackGround = null;
    //角色遮罩栏
    CharacterMaskBox = null;

    //信息
    Info = null;


    constructor(gObjectId, gX, gY, gWidth, gHeight, gTitleH) {
        base.constructor(gObjectId, gX, gY, gWidth, gHeight, gTitleH);
    }

    function Init(gInfo) {
        //接收参数信息
        Info = gInfo;

        //注册绘制
        RegisterDraw();
        //注册控件
        // for (local i = 0; i< 50; i++) {
        //     RegisterWidget();
        // }
        RegisterWidget();
        //注册窗口
        RegisterWindow();
        // OpenDeBug();

        //播放音乐
        PlayBackgroundMusic();
    }

    function PlayBackgroundMusic() {
        // BackGroundMusic = Sound("SoundPacks/Loop.ogg");
        // BackGroundMusic.Play();
    }

    function MusicLogic() {
        if (BackGroundMusic == null) return;
        if (!BackGroundMusic.IsPlaying()) BackGroundMusic.Play();
    }

    function RegisterWindow() {
        SettingBackgroundWindow = _Select_Character_SettingBackground_Window("选择角色_设置背景图片窗口", 850, 28, 212, 129, 0);
        AddUIChild(SettingBackgroundWindow);
        //先添加为子对象 因为子对象需要用到父对象
        SettingBackgroundWindow.Init();

        //角色对象
        for (local i = 0; i< 5; i++) {
            local Buf = _Select_Character_Chr("选择角色角色对象" + i, 190 + (i * 144), ((i % 2) ? 15 : 0) + 225, 126, 208, 0);
            if (i< Info.charac.len()) Buf.Init(Info.charac[i]);
            AddUIChild(Buf);
        }
    }

    function RegisterWidget() {
        //背景设置按钮
        local SettingButton = Yosin_BaseButton(984, 6, 77, 24 "sprite/interface2/ui/community/guild_2019/guildsetting/guildsetting.img", 29);
        //点击事件回调
        SettingButton.OnClick = function(Button) {
            SettingBackgroundWindow.SetVisible(!SettingBackgroundWindow.Visible)
        }.bindenv(this);
        AddUIChild(SettingButton);
        //登录按钮文本
        local LoginTextActor = FontAssetManager.GenerateNormal("背景设置", true, {
            color = sq_RGBA(221, 197, 147, 255)
        });
        LoginTextActor.SetPosition(23, 3);
        LoginTextActor.SetUpdateFunc(function(Text, Dt) {
            if (Text.Parent.State == 1 || Text.Parent.State == 2) {
                Text.SetFillColor(sq_RGBA(255, 255, 184, 255));
            } else if (Text.Parent.State == 0) {
                Text.SetFillColor(sq_RGBA(221, 197, 147, 255));
            }
        })
        SettingButton.Addchild(LoginTextActor);

    }

    //切换背景
    function ChangeBackground(Idx) {
        if (BackGround) {
            BackGround.RemoveSelf();
            BackGround = null;
        }
        BackGround = CL_SpriteObject("sprite/interface2/selectcharacter_ver2/background/large/background_large_event.img", Idx);
        BackGround.SetZOrder(-100);
        Addchild(BackGround);
    }

    function RegisterDraw() {
        //大背景 根据玩家的设定背景决定
        ChangeBackground(Info.loginImg);

        //角色遮罩栏
        CharacterMaskBox = Yosin_NineBoxStretch(-4, 330, 1074, 680, "sprite/interface/lenheartwindowcommon.img", 0);
        AddUIChild(CharacterMaskBox);


    }


    //逻辑入口
    function Proc(Dt) {
        MusicLogic();

        SyncPos(X, Y);
        base.Proc(Dt);
    }

}