/*
文件名:InputBox.nut
路径:User/UI/Widget/InputBox.nut
创建日期:2024-12-02	09:55
文件用途:输入框
*/
//基础按钮
class Yosin_InputBox extends Yosin_CommonUi {
    //状态
    State = 0;
    //是否焦点
    IsFocus = false;
    //内容
    str = "";
    sliceCode = "|";

    InputController = null;

    //宽度
    DWidth = 0;

    //文本对象
    Text_Obj = null;
    //Path
    Path = "sprite/interface/lenheartwindowcommon.img";

    constructor(X, Y, W) {
        this.DWidth = W;

        base.constructor(X, Y, W, 20);
        local FillCount = DWidth;

        local HeaderSp = CL_SpriteObject(Path, 63);
        Addchild(HeaderSp);
        for (local i = 0; i< FillCount; i++) {
            local MiddleSp = CL_SpriteObject(Path, 64);
            MiddleSp.SetPosition(i + 3, 0);
            Addchild(MiddleSp);
        }
        local TailSp = CL_SpriteObject(Path, 65);
        TailSp.SetPosition(FillCount + 3, 0);
        Addchild(TailSp);

        Text_Obj = TextActor(0, {
            color = 0xFFFFFFFF
        });
        Text_Obj.SetPosition(4, 3);
        Addchild(Text_Obj);

        _Imm_Input_Func_.rawset(C_Object, Imm_Input.bindenv(this));
    }

    //闪烁时间
    FlushTime = 0;

    function SliceCodeFlicker(Dt) {
        FlushTime += Dt;
        if (FlushTime >= 500) {
            FlushTime = 0;
            if (sliceCode.len() > 0) sliceCode = "";
            else if (sliceCode.len() == 0) sliceCode = "|";
        }
    }

    //判断是否中文字符
    function IsChineseChar(code) {
        return (code & 0x80) != 0;
    }

    //接收文本数据
    function Imm_Input(str) {
        if (!IsFocus) return;
        //退格键
        if (str == "\b") {
            if (this.str.len() > 0) {
                this.str = Sq_RemoveStringLast(this.str);
            }
            return;
        }
        this.str += str;
    }

    //同步文本对象数据
    function SyncText() {
        Text_Obj.SetText(str + sliceCode);
    }

    function Proc(Dt) {
        if (IsFocus) SliceCodeFlicker(Dt);
        else sliceCode = "";

        SyncText();
    }

    //鼠标左键单击回调
    function OnMouseLbClick(MousePos_X, MousePos_Y) {
        local Pos = GetWorldPosition();
        if (Math.IsIntersectRect(MousePos_X, MousePos_Y, 1, 1, Pos.x, Pos.y, Width, Height)) {
            if (OnClick) OnClick(this);
            IsFocus = true;
            sliceCode = "|";
            Sq_SetImmEnabled(true);
        } else {
            IsFocus = false;
            sliceCode = "";
            Sq_SetImmEnabled(false);
        }
    }
}