/*
文件名:Drag_Button.nut
路径:User/UI/Widget/Drag_Button.nut
创建日期:2024-12-14	09:40
文件用途:拖动按钮
*/
class Yosin_DragButton extends Yosin_CommonUi {
    //路径
    Path = "";
    //索引
    Idx = 0;
    //方向
    Direction = true;

    //按钮
    Button = null;

    //变动位置回调函数
    OnChange = null;

    //鼠标相对位置
    M_Xpos = null;
    M_Ypos = null;
    //未移动时的初始坐标
    BasePos = null;
    //移动Flag
    MoveFlag = false;
    //最大移动值
    Max_Move_Value = 0;
    //移动值
    Move_Value = 0;
    //侦测值
    Detect_Value = 0;

    constructor(X, Y, W, H, Path, Idx, Direction = true, UnavailableFlag = true) {
        this.Path = Path;
        this.Idx = Idx;
        this.Direction = Direction;
        base.constructor(X, Y, W, H);

        Button = Yosin_SplicingButton(0, 0, W, H, Path, Idx, Direction, UnavailableFlag);
        Button.DownSimulateOffset = false;
        AddUIChild(Button);

        BasePos = {
            x = X,
            y = Y
        }

    }

    //override
    //鼠标左键按下回调
    function OnMouseLbDown(MousePos_X, MousePos_Y) {
        base.OnMouseLbDown(MousePos_X, MousePos_Y);

        if (isInRect) {
            MoveFlag = true;
            M_Xpos = MousePos_X; //原始鼠标位置数据
            M_Ypos = MousePos_Y;
            B_X = X; //原始窗口位置
            B_Y = Y;
        }
    }

    //override
    function OnMouseProc(MousePos_X, MousePos_Y) {
        //调用原生方法
        base.OnMouseProc(MousePos_X, MousePos_Y);

        //移动
        if (MoveFlag) {
            //左键拖动
            if (Direction) {
                Move_Value = B_X + (MousePos_X - M_Xpos);
                if (!(Move_Value >= (0 + BasePos.x) && Move_Value <= (Max_Move_Value - Width + BasePos.x))) {
                    X = Move_Value;
                    DragLogic((Move_Value - Localtion_X).tofloat() / (Max_Move_Value - Width + BasePos.x - Localtion_X).tofloat());
                }
            } else {
                Move_Value = B_Y + (MousePos_Y - M_Ypos);
                if (Move_Value >= (0 + BasePos.y) && Move_Value <= (Max_Move_Value - Height + BasePos.y)) {
                    Y = (Move_Value);
                    DragLogic((Move_Value - Localtion_Y).tofloat() / (Max_Move_Value - Height + BasePos.y - Localtion_Y).tofloat());
                }
            }

            SyncPos(X, Y);
        }
    }

    //拖动逻辑
    function DragLogic(Value) {
        Detect_Value = Value;
        if (OnChange) OnChange(Value);
    }

    //鼠标左键弹起回调
    function OnMouseLbUp(MousePos_X, MousePos_Y) {
        base.OnMouseLbUp(MousePos_X, MousePos_Y);
        MoveFlag = false;
    }

    //设置最大移动值
    function SetMaxMoveValue(Value) {
        Max_Move_Value = Value;
    }
}