From a78b18785eb91a3a678475fa5984af14451ff096 Mon Sep 17 00:00:00 2001 From: SijieWang Date: Thu, 7 Oct 2021 13:13:22 +0800 Subject: [PATCH] [Bug] AddLayer does not have the correct supplementary enum data (#9441) --- cocos/core/scene-graph/layers.ts | 191 ++++++++++++++++--------------- 1 file changed, 96 insertions(+), 95 deletions(-) diff --git a/cocos/core/scene-graph/layers.ts b/cocos/core/scene-graph/layers.ts index 723951c32f..112e7dcffa 100644 --- a/cocos/core/scene-graph/layers.ts +++ b/cocos/core/scene-graph/layers.ts @@ -31,6 +31,7 @@ import { BitMask, Enum } from '../value-types'; import { legacyCC } from '../global-exports'; import { log2 } from '../math/bits'; +import { js } from '../utils/js'; // built-in layers, users can use 0~19 bits, 20~31 are system preserve bits. const layerList = { @@ -55,110 +56,110 @@ const layerList = { * Every node can be assigned to multiple layers with different bit masks, you can setup layer with inclusive or exclusive operation. */ export class Layers { - /** - * @en All layers in an Enum - * @zh 以 Enum 形式存在的所有层列表 - */ - public static Enum = Enum(layerList); + /** + * @en All layers in an Enum + * @zh 以 Enum 形式存在的所有层列表 + */ + public static Enum = Enum(layerList); - /** - * @en All layers in [[BitMask]] type - * @zh 包含所有层的 [[BitMask]] - */ - public static BitMask = BitMask({ ...layerList }); + /** + * @en All layers in [[BitMask]] type + * @zh 包含所有层的 [[BitMask]] + */ + public static BitMask = BitMask({ ...layerList }); - /** - * @en - * Make a layer mask accepting nothing but the listed layers - * @zh - * 创建一个包含式层检测器,只接受列表中的层 - * @param includes All accepted layers - * @return A filter which can detect all accepted layers - */ - public static makeMaskInclude (includes: number[]): number { - let mask = 0; - for (const inc of includes) { - mask |= inc; - } - return mask; - } + /** + * @en + * Make a layer mask accepting nothing but the listed layers + * @zh + * 创建一个包含式层检测器,只接受列表中的层 + * @param includes All accepted layers + * @return A filter which can detect all accepted layers + */ + public static makeMaskInclude (includes: number[]): number { + let mask = 0; + for (const inc of includes) { + mask |= inc; + } + return mask; + } - /** - * @en - * Make a layer mask accepting everything but the listed layers - * @zh - * 创建一个排除式层检测器,只拒绝列表中的层 - * @param excludes All excluded layers - * @return A filter which can detect for excluded layers - */ - public static makeMaskExclude (excludes: number[]): number { - return ~Layers.makeMaskInclude(excludes); - } + /** + * @en + * Make a layer mask accepting everything but the listed layers + * @zh + * 创建一个排除式层检测器,只拒绝列表中的层 + * @param excludes All excluded layers + * @return A filter which can detect for excluded layers + */ + public static makeMaskExclude (excludes: number[]): number { + return ~Layers.makeMaskInclude(excludes); + } - /** - * @zh 添加一个新层,用户可编辑 0 - 19 位为用户自定义层 - * @en Add a new layer, user can use layers from bit position 0 to 19, other bits are reserved. - * @param name Layer's name - * @param bitNum Layer's bit position - */ - public static addLayer (name: string, bitNum: number) { - if (bitNum === undefined) { - console.warn('bitNum can\'t be undefined'); - return; - } - if (bitNum > 19 || bitNum < 0) { - console.warn('maximum layers reached.'); - return; - } - Layers.Enum[name] = 1 << bitNum; - Layers.Enum[bitNum] = name; - Layers.BitMask[name] = 1 << bitNum; - Layers.BitMask[bitNum] = name; - } + /** + * @zh 添加一个新层,用户可编辑 0 - 19 位为用户自定义层 + * @en Add a new layer, user can use layers from bit position 0 to 19, other bits are reserved. + * @param name Layer's name + * @param bitNum Layer's bit position + */ + public static addLayer (name: string, bitNum: number) { + if (bitNum === undefined) { + console.warn('bitNum can\'t be undefined'); + return; + } + if (bitNum > 19 || bitNum < 0) { + console.warn('maximum layers reached.'); + return; + } + Layers.Enum[name] = 1 << bitNum; + js.value(Layers.Enum, String(bitNum), name); + Layers.BitMask[name] = 1 << bitNum; + js.value(Layers.BitMask, String(bitNum), name); + } - /** - * @en Remove a layer, user can remove layers from bit position 0 to 19, other bits are reserved. - * @zh 移除一个层,用户可编辑 0 - 19 位为用户自定义层 - * @param bitNum Layer's bit position - */ - public static deleteLayer (bitNum: number) { - if (bitNum > 19 || bitNum < 0) { - console.warn('do not change buildin layers.'); - return; - } - delete Layers.Enum[Layers.Enum[bitNum]]; - delete Layers.Enum[bitNum]; - delete Layers.BitMask[Layers.BitMask[bitNum]]; - delete Layers.BitMask[bitNum]; - } + /** + * @en Remove a layer, user can remove layers from bit position 0 to 19, other bits are reserved. + * @zh 移除一个层,用户可编辑 0 - 19 位为用户自定义层 + * @param bitNum Layer's bit position + */ + public static deleteLayer (bitNum: number) { + if (bitNum > 19 || bitNum < 0) { + console.warn('do not change buildin layers.'); + return; + } + delete Layers.Enum[Layers.Enum[bitNum]]; + delete Layers.Enum[bitNum]; + delete Layers.BitMask[Layers.BitMask[bitNum]]; + delete Layers.BitMask[bitNum]; + } - /** - * @en Given a layer name, returns the layer index as defined by either a Builtin or a User Layer in the Tags and Layers manager. - * @zh 给定层名称,返回由标记和层管理器中的内置层或用户层定义的层索引。 - * @param name layer's name - */ - public static nameToLayer (name: string): number { - if (name === undefined) { - console.warn('name can\'t be undefined'); - return -1; - } + /** + * @en Given a layer name, returns the layer index as defined by either a Builtin or a User Layer in the Tags and Layers manager. + * @zh 给定层名称,返回由标记和层管理器中的内置层或用户层定义的层索引。 + * @param name layer's name + */ + public static nameToLayer (name: string): number { + if (name === undefined) { + console.warn('name can\'t be undefined'); + return -1; + } - return log2(Layers.Enum[name] as number); - } + return log2(Layers.Enum[name] as number); + } - /** - * @en Given a layer number, returns the name of the layer as defined in either a Builtin or a User Layer in the Tags and Layers manager. - * @zh 给定层数,返回在标记和层管理器中的内置层或用户层中定义的层名称。 - * @param bitNum layer's value - */ - public static layerToName (bitNum: number): string { - if (bitNum > 31 || bitNum < 0) { - console.warn('Unable to access unknown layer.'); - return ''; - } + /** + * @en Given a layer number, returns the name of the layer as defined in either a Builtin or a User Layer in the Tags and Layers manager. + * @zh 给定层数,返回在标记和层管理器中的内置层或用户层中定义的层名称。 + * @param bitNum layer's value + */ + public static layerToName (bitNum: number): string { + if (bitNum > 31 || bitNum < 0) { + console.warn('Unable to access unknown layer.'); + return ''; + } - return Layers.Enum[bitNum] as string; - } + return Layers.Enum[bitNum] as string; + } } export declare namespace Layers { export type Enum = EnumAlias;