
import { _decorator, Component, Node, ccenum, CCInteger, CCFloat, Enum, director, UI, UIRenderer } from 'cc';
import { DEFAULT_SORTING_PRIORITY, ORDER_IN_LAYER_MAX, SortingLayer } from './sorting-define';
const { ccclass, property, type, disallowMultiple, requireComponent, executeInEditMode } = _decorator;

@ccclass('lcc-ui/SortingGroup')
@requireComponent(UIRenderer)
@disallowMultiple(true)
@executeInEditMode(true)
export class SortingGroup extends Component {
    /**
     * 排序层
     */
    @type(Enum(SortingLayer))
    sortingLayer:SortingLayer = SortingLayer.DEFAULT;

    /**
     * 排序值
     */
    @property({ type:CCFloat, min: 0, max : ORDER_IN_LAYER_MAX })
    orderInLayer:number = 0;

    /**
     * UI渲染器
     */
    private _uiRenderer:UIRenderer = null;

    onLoad(){
        this._uiRenderer = this.getComponent(UIRenderer);
    }

    onEnable(){
        this._uiRenderer.sortingPriority = Math.sign(this.sortingLayer) * (Math.abs(this.sortingLayer) * ORDER_IN_LAYER_MAX + this.orderInLayer);
    }

    onDisable(){
        this._uiRenderer.sortingPriority = DEFAULT_SORTING_PRIORITY;
    }
}
