Uniform the way to set a property's type as enum (#15440)

* Uniform the way to set a property's type as enum

* Update

* Revert "Update"

* License
This commit is contained in:
Leslie Leigh (李的序)
2023-06-15 11:17:47 +08:00
committed by GitHub
parent 7e86eb0f95
commit 6b171d2be4
7 changed files with 60 additions and 24 deletions
+6 -2
View File
@@ -36,6 +36,7 @@ import * as RF from './utils/requiring-frame';
import { legacyCC } from '../global-exports';
import { PropertyStash, PropertyStashInternalFlag } from './class-stash';
import { setPropertyEnumTypeOnAttrs } from './utils/attribute-internal';
const DELIMETER = attributeUtils.DELIMETER;
const CCCLASS_TAG = '__ctors__'; // Still use this historical name to avoid unsynchronized version issue
@@ -455,8 +456,11 @@ function parseAttributes (constructor: Function, attributes: PropertyStash, clas
// }
else if (typeof type === 'object') {
if (Enum.isEnum(type)) {
(attrs || initAttrs())[`${propertyNamePrefix}type`] = ENUM_TAG;
attrs![`${propertyNamePrefix}enumList`] = Enum.getList(type);
setPropertyEnumTypeOnAttrs(
attrs || initAttrs(),
propertyName,
type,
);
} else if (BitMask.isBitMask(type)) {
(attrs || initAttrs())[`${propertyNamePrefix}type`] = BITMASK_TAG;
attrs![`${propertyNamePrefix}bitmaskList`] = BitMask.getList(type);
@@ -0,0 +1,36 @@
/*
Copyright (c) 2023 Xiamen Yaji Software Co., Ltd.
http://www.cocos.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import { Enum, EnumType } from '../../value-types/enum';
import { getClassAttrs, DELIMETER } from './attribute';
// eslint-disable-next-line @typescript-eslint/ban-types
export function setPropertyEnumType (objectOrConstructor: object, propertyName: string, enumType: EnumType): void {
setPropertyEnumTypeOnAttrs(getClassAttrs(objectOrConstructor), propertyName, enumType);
}
export function setPropertyEnumTypeOnAttrs (attrs: Record<string, any>, propertyName: string, enumType: EnumType): void {
attrs[`${propertyName}${DELIMETER}type`] = 'Enum';
attrs[`${propertyName}${DELIMETER}enumList`] = Enum.getList(enumType);
}
+2
View File
@@ -41,6 +41,8 @@ export type { DeserializationContext } from './data/custom-serializable';
export * from './data/utils/asserts';
export * from './data/utils/compiler';
export { setPropertyEnumType, setPropertyEnumTypeOnAttrs } from './data/utils/attribute-internal';
export { ENUM_TAG, BITMASK_TAG } from './data/class';
export { isCCObject, isValid } from './data/object';
+2
View File
@@ -29,6 +29,8 @@ import { legacyCC } from '../global-exports';
import { errorID } from '../platform/debug';
import { assertIsTrue } from '../data/utils/asserts';
export type EnumType = Record<string, string | number>;
/**
* @en
* Define an enum type. <br/>
+7 -11
View File
@@ -25,7 +25,7 @@
import { EDITOR_NOT_IN_PREVIEW } from 'internal:constants';
import { Armature, Bone, EventObject, AnimationState } from '@cocos/dragonbones-js';
import { UIRenderer } from '../2d/framework/ui-renderer';
import { CCClass, Color, Enum, ccenum, errorID, RecyclePool, js, CCObject, EventTarget, cclegacy, _decorator } from '../core';
import { Color, Enum, ccenum, errorID, RecyclePool, js, CCObject, EventTarget, cclegacy, _decorator } from '../core';
import { BlendFactor } from '../gfx';
import { AnimationCache, ArmatureCache, ArmatureFrame } from './ArmatureCache';
import { AttachUtil } from './AttachUtil';
@@ -42,6 +42,7 @@ import { RenderDrawInfo } from '../2d/renderer/render-draw-info';
import { Material, Texture2D } from '../asset/assets';
import { Node } from '../scene-graph';
import { builtinResMgr } from '../asset/asset-manager';
import { setPropertyEnumType } from '../core/internal-index';
enum DefaultArmaturesEnum {
default = -1,
@@ -94,11 +95,6 @@ ccenum(AnimationCacheMode);
const { ccclass, serializable, editable, type, help, menu, tooltip, visible, displayName, override, displayOrder, executeInEditMode } = _decorator;
function setEnumAttr (obj, propName, enumDef): void {
CCClass.Attr.setClassAttr(obj, propName, 'type', 'Enum');
CCClass.Attr.setClassAttr(obj, propName, 'enumList', Enum.getList(enumDef));
}
/**
* @en Struct that can store rendering data-related information.
* @zh 用于存储渲染数据相关信息的结构体。
@@ -616,8 +612,8 @@ export class ArmatureDisplay extends UIRenderer {
this._inited = false;
this.attachUtil = new AttachUtil();
this.initFactory();
setEnumAttr(this, '_animationIndex', this._enumAnimations);
setEnumAttr(this, '_defaultArmatureIndex', this._enumArmatures);
setPropertyEnumType(this, '_animationIndex', this._enumAnimations);
setPropertyEnumType(this, '_defaultArmatureIndex', this._enumArmatures);
this._useVertexOpacity = true;
}
/**
@@ -1157,7 +1153,7 @@ export class ArmatureDisplay extends UIRenderer {
} else {
Object.assign(this._cacheModeEnum, DefaultCacheMode);
}
setEnumAttr(this, '_defaultCacheMode', this._cacheModeEnum);
setPropertyEnumType(this, '_defaultCacheMode', this._cacheModeEnum);
}
// update animation list for editor
@@ -1176,7 +1172,7 @@ export class ArmatureDisplay extends UIRenderer {
Object.assign(this._enumAnimations, animEnum || DefaultAnimsEnum);
Enum.update(this._enumAnimations);
// change enum
setEnumAttr(this, '_animationIndex', this._enumAnimations);
setPropertyEnumType(this, '_animationIndex', this._enumAnimations);
}
// update armature list for editor
@@ -1195,7 +1191,7 @@ export class ArmatureDisplay extends UIRenderer {
Object.assign(this._enumArmatures, armatureEnum || DefaultArmaturesEnum);
Enum.update(this._enumArmatures);
// change enum
setEnumAttr(this, '_defaultArmatureIndex', this._enumArmatures);
setPropertyEnumType(this, '_defaultArmatureIndex', this._enumArmatures);
}
/**
* @engineInternal Since v3.7.2 this is an engine private function.
+3 -3
View File
@@ -27,6 +27,7 @@ import { EDITOR } from 'internal:constants';
import { lerp, RealCurve, CCClass, geometry, Enum, approx, EPSILON } from '../../core';
import { PixelFormat, Filter, WrapMode } from '../../asset/assets/asset-enum';
import { Texture2D, ImageAsset } from '../../asset/assets';
import { setPropertyEnumType } from '../../core/internal-index';
const setClassAttr = CCClass.Attr.setClassAttr;
@@ -262,7 +263,7 @@ export default class CurveRange {
/**
* @deprecated since v3.5.0, this is an engine private interface that will be removed in the future.
*/
public _onBeforeSerialize (props): readonly ["mode", "constant", "multiplier"] | readonly ["mode", "spline", "multiplier"] | readonly ["mode", "splineMin", "splineMax", "multiplier"] | readonly ["mode", "constantMin", "constantMax", "multiplier"] {
public _onBeforeSerialize (props): readonly ['mode', 'constant', 'multiplier'] | readonly ['mode', 'spline', 'multiplier'] | readonly ['mode', 'splineMin', 'splineMax', 'multiplier'] | readonly ['mode', 'constantMin', 'constantMax', 'multiplier'] {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return SerializableTable[this._mode];
}
@@ -287,8 +288,7 @@ setClassAttr(CurveRange, 'multiplier', 'visible', true);
setClassAttr(CurveRange, 'constantMax', 'visible', true);
setClassAttr(CurveRange, 'constantMin', 'visible', true);
setClassAttr(CurveRange, 'constant', 'visible', true);
setClassAttr(CurveRange, 'mode', 'type', 'Enum');
setClassAttr(CurveRange, 'mode', 'enumList', Enum.getList(Mode));
setPropertyEnumType(CurveRange, 'mode', Mode);
setClassAttr(CurveRange, 'mode', 'visible', true);
setClassAttr(CurveRange, 'splineMax', 'type', 'Object');
setClassAttr(CurveRange, 'splineMax', 'ctor', RealCurve);
+4 -8
View File
@@ -27,7 +27,7 @@ import { Material, Texture2D } from '../asset/assets';
import { errorID, warn } from '../core/platform/debug';
import { Enum, ccenum } from '../core/value-types/enum';
import { Component, Node } from '../scene-graph';
import { CCBoolean, CCClass, CCFloat, CCObject, Color, Mat4, RecyclePool, js } from '../core';
import { CCBoolean, CCFloat, CCObject, Color, Mat4, RecyclePool, js } from '../core';
import { SkeletonData } from './skeleton-data';
import { UIRenderer, UITransform } from '../2d';
import { Batcher2D } from '../2d/renderer/batcher-2d';
@@ -44,6 +44,7 @@ import spine from './lib/spine-core.js';
import { VertexEffectDelegate } from './vertex-effect-delegate';
import SkeletonCache, { AnimationCache, AnimationFrame } from './skeleton-cache';
import { TrackEntryListeners } from './track-entry-listeners';
import { setPropertyEnumType } from '../core/internal-index';
const spineTag = SPINE_WASM;
const CachedFrameTime = 1 / 60;
@@ -120,11 +121,6 @@ export interface SkeletonDrawData {
indexCount: number;
}
function setEnumAttr (obj, propName, enumDef): void {
CCClass.Attr.setClassAttr(obj, propName, 'type', 'Enum');
CCClass.Attr.setClassAttr(obj, propName, 'enumList', Enum.getList(enumDef));
}
/**
* @en
* The Sockets attached to bones, synchronous transform with spine animation.
@@ -898,7 +894,7 @@ export class Skeleton extends UIRenderer {
this._enumAnimations = Enum({});
Object.assign(this._enumAnimations, animEnum);
Enum.update(this._enumAnimations);
setEnumAttr(this, '_animationIndex', this._enumAnimations);
setPropertyEnumType(this, '_animationIndex', this._enumAnimations);
}
// update skin list for editor
protected _updateSkinEnum (): void {
@@ -912,7 +908,7 @@ export class Skeleton extends UIRenderer {
this._enumSkins = Enum({});
Object.assign(this._enumSkins, skinEnum);
Enum.update(this._enumSkins);
setEnumAttr(this, '_defaultSkinIndex', this._enumSkins);
setPropertyEnumType(this, '_defaultSkinIndex', this._enumSkins);
}
protected _refreshInspector (): void {