/*
文件名:BaseObject.nut
路径:BaseClass/BaseObject/BaseObject.nut
创建日期:2024-05-05	00:18
文件用途:基础对象Class
*/
class CL_BaseObject {

    //父对象
    Parent = null;
    //子对象数组
    Children = null;
    //C指针
    C_Object = null;
    //UpdateCallBack
    UpdateCallBackFunc = null;
    //已存在的时间
    ExistingTime = 0;
    //自身信息存储Map
    Var = null;
    //销毁状态
    DestroyFlag = false;

    constructor(C_Object, ...) {
        Children = {};
        Var = {};
        this.C_Object = C_Object;
        //如果是新创建的对象注册销毁析构
        if (vargv.len() == 0) Register_Destruction(C_Object, this);
    }

    //设置UpdateCallBack
    function SetUpdateFunc(Func) {
        UpdateCallBackFunc = Func;
    }

    //被调用
    function OnUpdate(dt) {
        ExistingTime += dt;

        //如果CallBack函数存在则调用
        if (UpdateCallBackFunc) UpdateCallBackFunc(this, dt);

        //遍历调用所有子对象
        foreach(Id, Object in Children) {
            //如果子对象正常就执行Update
            if (!Object.DestroyFlag) {
                Object.OnUpdate(dt);
            }
            //如果子对象的销毁Flag 存在 则移除并销毁
            else {
                Removechild(Object);
            }
        }
    }

    //鼠标逻辑处理
    function OnMouseEvent(MouseState, Wheel, MousePos_X, MousePos_Y) {
        //遍历调用所有子对象
        foreach(Id, Object in Children) {
            Object.OnMouseEvent(MouseState, Wheel, MousePos_X, MousePos_Y);
        }
    }

    //添加子对象
    function Addchild(Child) {
        Children[Child.GetId()] <- Child;
        Child.Parent = this.weakref();
        //给自己解引用计数

        Child.OnAddchild(this);
        BaseObject_Addchild(this.C_Object, Child.C_Object);
    }
    //移除子对象
    function Removechild(Child) {
        try {
            delete Children[Child.GetId()];
        } catch (exception) {

        }
        Child.Parent = null;
        Child.OnRemove(this);
        BaseObject_Removechild(this.C_Object, Child.C_Object);
    }
    //从父对象移除自己
    function RemoveSelf() {
        DestroyFlag = true;
    }

    //被添加
    function OnAddchild(Parent) {

    }
    //被移除
    function OnRemove(Parent) {

    }

    //设置名字
    function SetName(name) {
        BaseObject_SetName(this.C_Object, name);
    }
    //获取名字
    function GetName() {
        return BaseObject_GetName(this.C_Object);
    }
    //获取Id
    function GetId() {
        return BaseObject_GetId(this.C_Object);
    }
    //获取显示状态
    function IsVisible() {
        return BaseObject_IsVisible(this.C_Object);
    }
    //是否启用级联透明度
    function IsCascadeOpacityEnabled() {
        return BaseObject_IsCascadeOpacityEnabled(this.C_Object);
    }
    //是否启用事件分发
    function IsEventDispatchEnabled() {
        return BaseObject_IsEventDispatchEnabled(this.C_Object);
    }
    //获取名称的 Hash 值
    function GetHashName() {
        return BaseObject_GetHashName(this.C_Object);
    }
    //获取 Z 轴顺序
    function GetZOrder() {
        return BaseObject_GetZOrder(this.C_Object);
    }
    //获取坐标
    function GetPosition() {
        return BaseObject_GetPosition(this.C_Object);
    }
    //获取世界坐标
    function GetWorldPosition() {
        return BaseObject_ConvertToWorld(this.C_Object);
    }
    //获取大小
    function GetSize() {
        return BaseObject_GetSize(this.C_Object);
    }
    //获取缩放后的大小
    function GetScaledSize() {
        return BaseObject_GetScaledSize(this.C_Object);
    }
    //获取锚点
    function GetAnchor() {
        return BaseObject_GetAnchor(this.C_Object);
    }
    //获取透明度
    function GetOpacity() {
        return BaseObject_GetOpacity(this.C_Object);
    }
    //获取显示透明度
    function GetDisplayedOpacity() {
        return BaseObject_GetDisplayedOpacity(this.C_Object);
    }
    //获取旋转角度
    function GetRotation() {
        return BaseObject_GetRotation(this.C_Object);
    }
    //获取缩放比例
    function GetScale() {
        return BaseObject_GetScale(this.C_Object);
    }
    //获取错切角度
    function GetSkew() {
        return BaseObject_GetSkew(this.C_Object);
    }
    //获取变换
    function GetTransform() {
        return BaseObject_GetTransform(this.C_Object);
    }
    //获取父角色
    function GetParent() {
        return Parent;
    }
    //获取所在舞台
    function GetStage() {
        return BaseObject_GetStage(this.C_Object);
    }

    //设置角色是否可见
    function SetVisible(Value) {
        BaseObject_SetVisible(this.C_Object, Value);
    }
    //设置坐标
    function SetPosition(Value, ...) {
        if (vargv.len() == 0)
            BaseObject_SetPosition(this.C_Object, Value);
        else if (vargv.len() == 1)
            BaseObject_SetPosition(this.C_Object, Value, vargv[0]);
    }
    //移动坐标
    function MoveTo(Value, ...) {
        if (vargv.len() == 0)
            BaseObject_MoveTo(this.C_Object, Value);
        else if (vargv.len() == 1)
            BaseObject_MoveTo(this.C_Object, Value, vargv[0]);
    }
    //移动相对坐标
    function MoveBy(Value, ...) {
        if (vargv.len() == 0)
            BaseObject_MoveBy(this.C_Object, Value);
        else if (vargv.len() == 1)
            BaseObject_MoveBy(this.C_Object, Value, vargv[0]);
    }
    //设置缩放比例
    function SetScale(Value, ...) {
        if (vargv.len() == 0)
            BaseObject_SetScale(this.C_Object, Value);
        else if (vargv.len() == 1)
            BaseObject_SetScale(this.C_Object, Value, vargv[0]);
    }
    //设置错切角度
    function SetSkew(Value, ...) {
        if (vargv.len() == 0)
            BaseObject_SetSkew(this.C_Object, Value);
        else if (vargv.len() == 1)
            BaseObject_SetSkew(this.C_Object, Value, vargv[0]);
    }
    //设置锚点位置
    function SetAnchor(Value, ...) {
        if (vargv.len() == 0)
            BaseObject_SetAnchor(this.C_Object, Value);
        else if (vargv.len() == 1)
            BaseObject_SetAnchor(this.C_Object, Value, vargv[0]);
    }
    //修改大小
    function SetSize(Value, ...) {
        if (vargv.len() == 0)
            BaseObject_SetSize(this.C_Object, Value);
        else if (vargv.len() == 1)
            BaseObject_SetSize(this.C_Object, Value, vargv[0]);
    }
    //设置旋转角度
    function SetRotation(Value) {
        BaseObject_SetRotation(this.C_Object, Value);
    }
    //设置透明度
    function SetOpacity(Value) {
        BaseObject_SetOpacity(this.C_Object, Value);
    }
    //启用或禁用级联透明度
    function SetCascadeOpacityEnabled(Value) {
        BaseObject_SetCascadeOpacityEnabled(this.C_Object, Value);
    }
    //设置 Z 轴顺序
    function SetZOrder(Value) {
        BaseObject_SetZOrder(this.C_Object, Value);
    }
    //判断点是否在角色内
    function IsContainsPoint(Value) {
        if (vargv.len() == 0)
            BaseObject_IsContainsPoint(this.C_Object, Value);
        else if (vargv.len() == 1)
            BaseObject_IsContainsPoint(this.C_Object, Value, vargv[0]);
    }
    //设置渲染角色边界
    function ShowBorder(Value) {
        BaseObject_ShowBorder(this.C_Object, Value);
    }
    //设置渲染角色边界
    function SetRotate(Duration, Rotation) {
        BaseObject_SetRotate(this.C_Object, Duration, Rotation);
    }
}