Skip to content

Commit

Permalink
[Bug] AddLayer does not have the correct supplementary enum data (coc…
Browse files Browse the repository at this point in the history
  • Loading branch information
VisualSJ authored Oct 7, 2021
1 parent bad357d commit a78b187
Showing 1 changed file with 105 additions and 104 deletions.
209 changes: 105 additions & 104 deletions cocos/core/scene-graph/layers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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 [[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 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;
}

/**
* @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;
}

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 '';
}

return Layers.Enum[bitNum] as string;
}
/**
* @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
* 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);
}

/**
* @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 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);
}

/**
* @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;
}
}
export declare namespace Layers {
export type Enum = EnumAlias<typeof Layers.Enum>;
Expand Down

0 comments on commit a78b187

Please sign in to comment.