From 2ec607663813cc63c0e5ad85e5382b721315ab18 Mon Sep 17 00:00:00 2001 From: ChiaNing Date: Thu, 14 Sep 2023 14:02:40 +0800 Subject: [PATCH] name change & use assert --- cocos/2d/assembler/label/ttfUtils.ts | 4 +-- cocos/2d/components/label-outline.ts | 44 +++++++++-------------- cocos/2d/components/label-shadow.ts | 53 +++++++++++----------------- cocos/2d/components/label.ts | 38 ++++++++++---------- editor/i18n/en/localization.js | 4 +-- editor/i18n/zh/localization.js | 4 +-- 6 files changed, 62 insertions(+), 85 deletions(-) diff --git a/cocos/2d/assembler/label/ttfUtils.ts b/cocos/2d/assembler/label/ttfUtils.ts index 95acfeca9e5..73f448e7ef2 100644 --- a/cocos/2d/assembler/label/ttfUtils.ts +++ b/cocos/2d/assembler/label/ttfUtils.ts @@ -68,7 +68,7 @@ export const ttfUtils = { style.underlineHeight = comp.underlineHeight; // outline// both - const isOutlined = comp.outlineUsed && comp.outlineWidth > 0; + const isOutlined = comp.enableOutline && comp.outlineWidth > 0; if (isOutlined) { style.isOutlined = true; style.outlineColor.set(comp.outlineColor); @@ -78,7 +78,7 @@ export const ttfUtils = { } // shadow// both - const isShadow = comp.shadowUsed; + const isShadow = comp.enableShadow && (comp.shadowBlur > 0 || comp.shadowOffset.x !== 0 || comp.shadowOffset.y !== 0); if (isShadow) { style.hasShadow = true; style.shadowColor.set(comp.shadowColor); diff --git a/cocos/2d/components/label-outline.ts b/cocos/2d/components/label-outline.ts index cd4115b3a4c..8aacfb29c57 100644 --- a/cocos/2d/components/label-outline.ts +++ b/cocos/2d/components/label-outline.ts @@ -25,7 +25,7 @@ import { ccclass, help, executionOrder, menu, tooltip, requireComponent, executeInEditMode, serializable } from 'cc.decorator'; import { Component } from '../../scene-graph/component'; -import { Color, cclegacy, warn } from '../../core'; +import { Color, assertIsTrue, cclegacy } from '../../core'; import { Label } from './label'; /** @@ -59,21 +59,16 @@ export class LabelOutline extends Component { * @deprecated since v3.8.2, please use [[Label.outlineColor]] instead. */ @tooltip('i18n:labelOutline.color') - // @constget get color (): Readonly { const label = this.node.getComponent(Label); - if (!label) { - warn('Required Label component to work, please add Label component.'); - return this._color; - } + assertIsTrue(label, 'Required Label component to work, please add Label component.'); return label.outlineColor; } set color (value) { const label = this.node.getComponent(Label); - if (label) { - label.outlineColor = value; - } + assertIsTrue(label, 'Required Label component to work, please add Label component.'); + label.outlineColor = value; } /** @@ -88,47 +83,42 @@ export class LabelOutline extends Component { @tooltip('i18n:labelOutline.width') get width (): number { const label = this.node.getComponent(Label); - if (!label) { - warn('Required Label component to work, please add Label component.'); - return this._width; - } + assertIsTrue(label, 'Required Label component to work, please add Label component.'); return label.outlineWidth; } set width (value) { const label = this.node.getComponent(Label); - if (label) { - label.outlineWidth = value; - } + assertIsTrue(label, 'Required Label component to work, please add Label component.'); + label.outlineWidth = value; } /** - * @deprecated since v3.8.2, please use [[Label.outlineUsed]] instead. + * @deprecated since v3.8.2, please use [[Label.enableOutline]] instead. */ public onEnable (): void { const label = this.node.getComponent(Label); - if (label) { - label.outlineUsed = true; - } + assertIsTrue(label, 'Required Label component to work, please add Label component.'); + label.enableOutline = true; } /** - * @deprecated since v3.8.2, please use [[Label.outlineUsed]] instead. + * @deprecated since v3.8.2, please use [[Label.enableOutline]] instead. */ public onDisable (): void { const label = this.node.getComponent(Label); - if (label) { - label.outlineUsed = false; - } + assertIsTrue(label, 'Required Label component to work, please add Label component.'); + label.enableOutline = false; } /** - * @deprecated since v3.8.2, please use [[Label.outlineUsed]] instead. + * @deprecated since v3.8.2, please use [[Label.enableOutline]] instead. */ public onLoad (): void { const label = this.node.getComponent(Label); - if (label && this.enabledInHierarchy) { - label.outlineUsed = true; + assertIsTrue(label, 'Required Label component to work, please add Label component.'); + if (this.enabledInHierarchy) { + label.enableOutline = true; } } } diff --git a/cocos/2d/components/label-shadow.ts b/cocos/2d/components/label-shadow.ts index 886b308f348..4e9d1078310 100644 --- a/cocos/2d/components/label-shadow.ts +++ b/cocos/2d/components/label-shadow.ts @@ -25,7 +25,7 @@ import { ccclass, help, executionOrder, menu, tooltip, requireComponent, executeInEditMode, serializable } from 'cc.decorator'; import { Component } from '../../scene-graph/component'; -import { Color, Vec2, warn } from '../../core'; +import { Color, Vec2, assertIsTrue } from '../../core'; import { Label } from './label'; /** @@ -60,18 +60,14 @@ export class LabelShadow extends Component { @tooltip('i18n:labelShadow.color') get color (): Readonly { const label = this.node.getComponent(Label); - if (!label) { - warn('Required Label component to work, please add Label component.'); - return this._color; - } + assertIsTrue(label, 'Required Label component to work, please add Label component.'); return label.shadowColor; } set color (value) { const label = this.node.getComponent(Label); - if (label) { - label.shadowColor = value; - } + assertIsTrue(label, 'Required Label component to work, please add Label component.'); + label.shadowColor = value; } /** @@ -86,18 +82,14 @@ export class LabelShadow extends Component { @tooltip('i18n:labelShadow.offset') get offset (): Vec2 { const label = this.node.getComponent(Label); - if (!label) { - warn('Required Label component to work, please add Label component.'); - return this._offset; - } + assertIsTrue(label, 'Required Label component to work, please add Label component.'); return label.shadowOffset; } set offset (value) { const label = this.node.getComponent(Label); - if (label) { - label.shadowOffset = value; - } + assertIsTrue(label, 'Required Label component to work, please add Label component.'); + label.shadowOffset = value; } /** @@ -112,47 +104,42 @@ export class LabelShadow extends Component { @tooltip('i18n:labelShadow.blur') get blur (): number { const label = this.node.getComponent(Label); - if (!label) { - warn('Required Label component to work, please add Label component.'); - return this._blur; - } + assertIsTrue(label, 'Required Label component to work, please add Label component.'); return label.shadowBlur; } set blur (value) { const label = this.node.getComponent(Label); - if (label) { - label.shadowBlur = value; - } + assertIsTrue(label, 'Required Label component to work, please add Label component.'); + label.shadowBlur = value; } /** - * @deprecated since v3.8.2, please use [[Label.shadowUsed]] instead. + * @deprecated since v3.8.2, please use [[Label.enableShadow]] instead. */ public onEnable (): void { const label = this.node.getComponent(Label); - if (label) { - label.shadowUsed = true; - } + assertIsTrue(label, 'Required Label component to work, please add Label component.'); + label.enableShadow = true; } /** - * @deprecated since v3.8.2, please use [[Label.shadowUsed]] instead. + * @deprecated since v3.8.2, please use [[Label.enableShadow]] instead. */ public onDisable (): void { const label = this.node.getComponent(Label); - if (label) { - label.shadowUsed = false; - } + assertIsTrue(label, 'Required Label component to work, please add Label component.'); + label.enableShadow = false; } /** - * @deprecated since v3.8.2, please use [[Label.outlineUsed]] instead. + * @deprecated since v3.8.2, please use [[Label.enableShadow]] instead. */ public onLoad (): void { const label = this.node.getComponent(Label); - if (label && this.enabledInHierarchy) { - label.shadowUsed = true; + assertIsTrue(label, 'Required Label component to work, please add Label component.'); + if (this.enabledInHierarchy) { + label.enableShadow = true; } } } diff --git a/cocos/2d/components/label.ts b/cocos/2d/components/label.ts index 103bf498c40..5e5008a4d88 100644 --- a/cocos/2d/components/label.ts +++ b/cocos/2d/components/label.ts @@ -612,13 +612,13 @@ export class Label extends UIRenderer { **/ @editable @displayOrder(19) - @tooltip('i18n:label.outline_used') - get outlineUsed (): boolean { - return this._isOutline; + @tooltip('i18n:label.outline_enable') + get enableOutline (): boolean { + return this._enableOutline; } - set outlineUsed (value) { - if (this._isOutline === value) return; - this._isOutline = value; + set enableOutline (value) { + if (this._enableOutline === value) return; + this._enableOutline = value; this.markForUpdateRenderData(); } @@ -630,7 +630,7 @@ export class Label extends UIRenderer { * 改变描边的颜色。 */ @editable - @visible(function (this: Label) { return this._isOutline; }) + @visible(function (this: Label) { return this._enableOutline; }) @displayOrder(20) @tooltip('i18n:label.outline_color') get outlineColor (): Color { @@ -650,7 +650,7 @@ export class Label extends UIRenderer { * 改变描边的宽度。 */ @editable - @visible(function (this: Label) { return this._isOutline; }) + @visible(function (this: Label) { return this._enableOutline; }) @displayOrder(21) @tooltip('i18n:label.outline_width') get outlineWidth (): number { @@ -668,13 +668,13 @@ export class Label extends UIRenderer { */ @editable @displayOrder(22) - @tooltip('i18n:label.shadow_used') - get shadowUsed (): boolean { - return this._isShadow; + @tooltip('i18n:label.shadow_enable') + get enableShadow (): boolean { + return this._enableShadow; } - set shadowUsed (value) { - if (this._isShadow === value) return; - this._isShadow = value; + set enableShadow (value) { + if (this._enableShadow === value) return; + this._enableShadow = value; this.markForUpdateRenderData(); } @@ -686,7 +686,7 @@ export class Label extends UIRenderer { * 阴影的颜色。 */ @editable - @visible(function (this: Label) { return this._isShadow; }) + @visible(function (this: Label) { return this._enableShadow; }) @displayOrder(23) @tooltip('i18n:label.shadow_color') get shadowColor (): Color { @@ -706,7 +706,7 @@ export class Label extends UIRenderer { * 字体与阴影的偏移。 */ @editable - @visible(function (this: Label) { return this._isShadow; }) + @visible(function (this: Label) { return this._enableShadow; }) @displayOrder(24) @tooltip('i18n:label.shadow_offset') get shadowOffset (): Vec2 { @@ -726,7 +726,7 @@ export class Label extends UIRenderer { * 阴影的模糊程度。 */ @editable - @visible(function (this: Label) { return this._isShadow; }) + @visible(function (this: Label) { return this._enableShadow; }) @displayOrder(25) @tooltip('i18n:label.shadow_blur') get shadowBlur (): number { @@ -841,13 +841,13 @@ export class Label extends UIRenderer { @serializable protected _cacheMode = CacheMode.NONE; @serializable - protected _isOutline = false; + protected _enableOutline = false; @serializable protected _outlineColor = new Color(0, 0, 0, 255); @serializable protected _outlineWidth = 2; @serializable - protected _isShadow = false; + protected _enableShadow = false; @serializable protected _shadowColor = new Color(0, 0, 0, 255); @serializable diff --git a/editor/i18n/en/localization.js b/editor/i18n/en/localization.js index 25fdd4add63..69d2bef94f8 100755 --- a/editor/i18n/en/localization.js +++ b/editor/i18n/en/localization.js @@ -408,10 +408,10 @@ module.exports = link(mixin({ font_underline: 'Font underlined', spacing_x: 'The spacing between text characters, only available in BMFont', underline_height: 'The height of underline', - outline_used: 'Whether outline is enabled', + outline_enable: 'Whether outline is enabled', outline_width: 'The width of outline', outline_color: 'The color of outline', - shadow_used: 'Whether shadow is enabled', + shadow_enable: 'Whether shadow is enabled', shadow_color: 'The color of shadow', shadow_offset: 'Offset between font and shadow', shadow_blur: 'A non-negative float specifying the level of shadow blur', diff --git a/editor/i18n/zh/localization.js b/editor/i18n/zh/localization.js index 8916789929c..bca21c95c5c 100755 --- a/editor/i18n/zh/localization.js +++ b/editor/i18n/zh/localization.js @@ -397,10 +397,10 @@ module.exports = link(mixin({ font_underline: '字体加下划线', spacing_x: '文本字符之间的间距。仅在使用 BMFont 位图字体时生效', underline_height: '下划线高度', - outline_used: '是否启用描边', + outline_enable: '是否启用描边', outline_width: '描边宽度', outline_color: '描边颜色', - shadow_used: '是否启用阴影', + shadow_enable: '是否启用阴影', shadow_color: '阴影颜色', shadow_offset: '阴影偏移量', shadow_blur: '阴影模糊程度',