From 6b171d2be4ee7fc6958ef511b5619c2bd84cf9db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leslie=20Leigh=20=28=E6=9D=8E=E7=9A=84=E5=BA=8F=29?= Date: Thu, 15 Jun 2023 11:17:47 +0800 Subject: [PATCH] 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 --- cocos/core/data/class.ts | 8 +++-- cocos/core/data/utils/attribute-internal.ts | 36 +++++++++++++++++++++ cocos/core/internal-index.ts | 2 ++ cocos/core/value-types/enum.ts | 2 ++ cocos/dragon-bones/ArmatureDisplay.ts | 18 ++++------- cocos/particle/animator/curve-range.ts | 6 ++-- cocos/spine/skeleton.ts | 12 +++---- 7 files changed, 60 insertions(+), 24 deletions(-) create mode 100644 cocos/core/data/utils/attribute-internal.ts diff --git a/cocos/core/data/class.ts b/cocos/core/data/class.ts index e10f10fa83..bc66b0149f 100644 --- a/cocos/core/data/class.ts +++ b/cocos/core/data/class.ts @@ -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); diff --git a/cocos/core/data/utils/attribute-internal.ts b/cocos/core/data/utils/attribute-internal.ts new file mode 100644 index 0000000000..306019d084 --- /dev/null +++ b/cocos/core/data/utils/attribute-internal.ts @@ -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, propertyName: string, enumType: EnumType): void { + attrs[`${propertyName}${DELIMETER}type`] = 'Enum'; + attrs[`${propertyName}${DELIMETER}enumList`] = Enum.getList(enumType); +} diff --git a/cocos/core/internal-index.ts b/cocos/core/internal-index.ts index 48ab60cf19..25bfb414cf 100644 --- a/cocos/core/internal-index.ts +++ b/cocos/core/internal-index.ts @@ -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'; diff --git a/cocos/core/value-types/enum.ts b/cocos/core/value-types/enum.ts index 6e4e5aa169..60b8264a1d 100644 --- a/cocos/core/value-types/enum.ts +++ b/cocos/core/value-types/enum.ts @@ -29,6 +29,8 @@ import { legacyCC } from '../global-exports'; import { errorID } from '../platform/debug'; import { assertIsTrue } from '../data/utils/asserts'; +export type EnumType = Record; + /** * @en * Define an enum type.
diff --git a/cocos/dragon-bones/ArmatureDisplay.ts b/cocos/dragon-bones/ArmatureDisplay.ts index 20899375b9..2e70e6d966 100644 --- a/cocos/dragon-bones/ArmatureDisplay.ts +++ b/cocos/dragon-bones/ArmatureDisplay.ts @@ -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. diff --git a/cocos/particle/animator/curve-range.ts b/cocos/particle/animator/curve-range.ts index acffa79b81..9930acd2a2 100644 --- a/cocos/particle/animator/curve-range.ts +++ b/cocos/particle/animator/curve-range.ts @@ -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); diff --git a/cocos/spine/skeleton.ts b/cocos/spine/skeleton.ts index 8d7e93570c..8a33cd5646 100644 --- a/cocos/spine/skeleton.ts +++ b/cocos/spine/skeleton.ts @@ -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 {