[Bug] AddLayer does not have the correct supplementary enum data (#9441)

This commit is contained in:
SijieWang
2021-10-07 13:13:22 +08:00
committed by GitHub
parent bad357d864
commit a78b18785e
+96 -95
View File
@@ -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<typeof Layers.Enum>;