From faa7094cf86f5ccbf6a00a55da9f1c5f3d17d415 Mon Sep 17 00:00:00 2001 From: "jiaxin.lai" Date: Fri, 26 Jul 2019 17:56:55 +0800 Subject: [PATCH] add some typing to interface of INode (#4984) * add some typing to interface of INode * fix INode childern typing * add Mutable type, for change Readonly type * decouple node with ui relative component * add missed ui component, and decouple node * add more typing to IBaseNode and INode --- @types/globals.d.ts | 23 +- cocos/3d/ui/components/canvas-component.ts | 5 +- .../ui/components/debug-canvas-component.ts | 15 +- .../components/editbox/edit-box-component.ts | 21 +- cocos/3d/ui/components/layout-component.ts | 35 +- cocos/3d/ui/components/mask-component.ts | 41 +- cocos/3d/ui/components/rich-text-component.ts | 23 +- .../3d/ui/components/scroll-view-component.ts | 11 +- cocos/3d/ui/components/sprite-component.ts | 2 +- cocos/3d/ui/components/ui-component.ts | 13 +- cocos/3d/ui/components/ui-render-component.ts | 2 +- cocos/3d/ui/components/widget-component.ts | 2 +- cocos/3d/ui/components/widget-manager.ts | 4 +- .../block-input-events-component.ts | 2 +- cocos/components/component.ts | 2 +- cocos/core/utils/interfaces.ts | 537 ++++++++++++++++-- cocos/scene-graph/base-node.ts | 8 +- cocos/scene-graph/node.ts | 4 +- 18 files changed, 607 insertions(+), 143 deletions(-) diff --git a/@types/globals.d.ts b/@types/globals.d.ts index 62826da6e5..a92526d50b 100644 --- a/@types/globals.d.ts +++ b/@types/globals.d.ts @@ -42,8 +42,15 @@ declare const jsb: any; declare const CC_PHYSICS_CANNON: boolean; declare const CC_PHYSICS_AMMO: boolean; declare const CC_PHYSICS_BUILT_IN: boolean; - interface Window { + + WebGL2RenderingContext: any; + + sharedCanvas: any; + __canvas: any; + canvas: any; + + XMLHttpRequest: any; mozRequestAnimationFrame (callback: any, element?: any): any; oRequestAnimationFrame (callback: any, element?: any): any; msRequestAnimationFrame (callback: any, element?: any): any; @@ -55,14 +62,6 @@ interface Window { msCancelAnimationFrame (callback: any, element?: any): any; mozCancelAnimationFrame (callback: any, element?: any): any; ocancelAnimationFrame (callback: any, element?: any): any; - - WebGL2RenderingContext: any; - - sharedCanvas:any; - __canvas:any; - canvas:any; - - XMLHttpRequest:any; } interface Document{ @@ -76,9 +75,7 @@ interface HTMLElement{ name: any; } -interface ActiveXObject { - new (s: string): any; -} +type ActiveXObject = new (s: string) => any; declare var ActiveXObject: ActiveXObject; declare const cc: { @@ -103,6 +100,8 @@ declare interface IWritableArrayLike { declare type Constructor = new(...args: any[]) => T; +declare type Mutable = { -readonly [P in keyof T]: T[P] }; + declare namespace Editor { function log (message?: any, ...optionalParams: any[]): void; function error (message?: any, ...optionalParams: any[]): void; diff --git a/cocos/3d/ui/components/canvas-component.ts b/cocos/3d/ui/components/canvas-component.ts index f14b5bfd1e..1843e7cf99 100644 --- a/cocos/3d/ui/components/canvas-component.ts +++ b/cocos/3d/ui/components/canvas-component.ts @@ -35,6 +35,7 @@ import { vec3 } from '../../../core/vmath'; import { GFXClearFlag } from '../../../gfx/define'; import { Camera } from '../../../renderer'; import { UITransformComponent } from './ui-transfrom-component'; +import { Node } from '../../../scene-graph'; const _worldPos = new Vec3(); @@ -177,7 +178,7 @@ export class CanvasComponent extends Component { } public __preload () { - const cameraNode = new cc.Node('UICamera_' + this.node.name); + const cameraNode = new Node('UICamera_' + this.node.name); cameraNode.setPosition(0, 0, 999); if (!CC_EDITOR) { this._camera = cc.director.root.ui.renderScene.createCamera({ @@ -264,7 +265,7 @@ export class CanvasComponent extends Component { vec3.set(_worldPos, canvasSize.width * 0.5 + offsetX, canvasSize.height * 0.5 + offsetY, 0); } - if (!this._pos.equals(_worldPos)){ + if (!this._pos.equals(_worldPos)) { this.node.setPosition(_worldPos); } diff --git a/cocos/3d/ui/components/debug-canvas-component.ts b/cocos/3d/ui/components/debug-canvas-component.ts index 6234df867d..e3b1b03c1d 100644 --- a/cocos/3d/ui/components/debug-canvas-component.ts +++ b/cocos/3d/ui/components/debug-canvas-component.ts @@ -34,6 +34,7 @@ import { Vec3 } from '../../../core/value-types'; import { GFXClearFlag } from '../../../gfx/define'; import { CanvasComponent } from './canvas-component'; import { UITransformComponent } from './ui-transfrom-component'; +import { Node } from '../../../scene-graph'; const _worldPos = new Vec3(); @@ -45,13 +46,13 @@ const _worldPos = new Vec3(); @executionOrder(100) @requireComponent(UITransformComponent) export class DebugCanvasComponent extends CanvasComponent { - constructor (){ + constructor () { super(); this._thisOnResized = this.alignWithScreen.bind(this); } - public __preload (){ - const cameraNode = new cc.Node('UICamera_Debug'); + public __preload () { + const cameraNode = new Node('UICamera_Debug'); cameraNode.setPosition(0, 0, 1000); this._camera = cc.director.root.ui.renderScene.createCamera({ @@ -80,11 +81,11 @@ export class DebugCanvasComponent extends CanvasComponent { } // don't remove - public onEnable (){ + public onEnable () { } // don't remove - public onDisable (){ + public onDisable () { } public onDestroy () { @@ -98,7 +99,7 @@ export class DebugCanvasComponent extends CanvasComponent { } } - public alignWithScreen (){ + public alignWithScreen () { const canvasSize = cc.visibleRect; const nodeSize = canvasSize; const designSize = cc.view.getDesignResolutionSize(); @@ -133,7 +134,7 @@ export class DebugCanvasComponent extends CanvasComponent { } // don't remove - public applySettings (){ + public applySettings () { } } diff --git a/cocos/3d/ui/components/editbox/edit-box-component.ts b/cocos/3d/ui/components/editbox/edit-box-component.ts index bd6e780483..b861075501 100644 --- a/cocos/3d/ui/components/editbox/edit-box-component.ts +++ b/cocos/3d/ui/components/editbox/edit-box-component.ts @@ -35,13 +35,14 @@ import { ccclass, executeInEditMode, executionOrder, menu, property } from '../. import { EventTouch } from '../../../../core/platform'; import { SystemEventType } from '../../../../core/platform/event-manager/event-enum'; import { Color, Size } from '../../../../core/value-types'; -import { Node } from '../../../../scene-graph/node'; import { LabelComponent, VerticalTextAlignment } from '../label-component'; import { SpriteComponent } from '../sprite-component'; import { UIRenderComponent } from '../ui-render-component'; import { UITransformComponent } from '../ui-transfrom-component'; -import { EditBoxImpl} from './edit-box-impl'; +import { EditBoxImpl } from './edit-box-impl'; import { InputFlag, InputMode, KeyboardReturnType } from './types'; +import { INode } from '../../../../core/utils/interfaces'; +import { Node } from '../../../../scene-graph'; const LEFT_PADDING = 2; @@ -547,7 +548,7 @@ export class EditBoxComponent extends Component { this._showLabels(); } - if (this._impl){ + if (this._impl) { this._impl.stayOnTop(this.stayOnTop); } } @@ -555,13 +556,13 @@ export class EditBoxComponent extends Component { public _syncSize () { const size = this.node.getContentSize(); - if (this._background){ + if (this._background) { this._background.node.setAnchorPoint(this.node.getAnchorPoint()); this._background.node.setContentSize(size); } this._updateLabelPosition(size); - if (this._impl){ + if (this._impl) { this._impl.setSize(size.width, size.height); } } @@ -603,9 +604,9 @@ export class EditBoxComponent extends Component { this._background!.spriteFrame = this._backgroundImage; // let background = this._background; // if (!background) { - // let node = this.node.getChildByName('BACKGROUND_SPRITE'); + // let node = this.node.getChildByName('BACKGROUND_SPRITE') as INode; // if (!node) { - // node = new cc.Node('BACKGROUND_SPRITE'); + // node = new cc.Node('BACKGROUND_SPRITE') as INode; // } // background = node.getComponent(cc.SpriteComponent); @@ -624,7 +625,7 @@ export class EditBoxComponent extends Component { if (!this._textLabel) { let node = this.node.getChildByName('TEXT_LABEL'); if (!node) { - node = new cc.Node('TEXT_LABEL'); + node = new Node('TEXT_LABEL') as INode; } let textLabel = node!.getComponent(LabelComponent); node!.parent = this.node; @@ -643,9 +644,9 @@ export class EditBoxComponent extends Component { } if (!this._placeholderLabel) { - let node = this.node.getChildByName('PLACEHOLDER_LABEL'); + let node = this.node.getChildByName('PLACEHOLDER_LABEL') as INode; if (!node) { - node = new Node('PLACEHOLDER_LABEL'); + node = new cc.Node('PLACEHOLDER_LABEL') as INode; } let placeholderLabel = node!.getComponent(LabelComponent); if (!placeholderLabel) { diff --git a/cocos/3d/ui/components/layout-component.ts b/cocos/3d/ui/components/layout-component.ts index cdd7218051..849039c02f 100644 --- a/cocos/3d/ui/components/layout-component.ts +++ b/cocos/3d/ui/components/layout-component.ts @@ -32,10 +32,11 @@ import { Component } from '../../../components/component'; import { ccclass, executeInEditMode, executionOrder, menu, property } from '../../../core/data/class-decorator'; import { Rect, Size, Vec2, Vec3 } from '../../../core/value-types'; import { ccenum } from '../../../core/value-types/enum'; -import { vec3 } from '../../../core/vmath'; -import { Node } from '../../../scene-graph/node'; +import { vec3 } from '../../../core/vmath/vec3'; import { UITransformComponent } from './ui-transfrom-component'; -const NodeEvent = Node.EventType; +import { SystemEventType } from '../../../core/platform/event-manager/event-enum'; +import { INode } from '../../../core/utils/interfaces'; +const NodeEvent = SystemEventType; /** * @zh * 布局类型。 @@ -655,12 +656,12 @@ export class LayoutComponent extends Component { if (rowBreak) { const rowBreakBoundary = nextX + rightBoundaryOfChild + sign * (sign > 0 ? this._paddingRight : this._paddingLeft); let leftToRightRowBreak = false; - if (this._horizontalDirection === HorizontalDirection.LEFT_TO_RIGHT && rowBreakBoundary > (1 - layoutAnchor.x) * baseWidth){ + if (this._horizontalDirection === HorizontalDirection.LEFT_TO_RIGHT && rowBreakBoundary > (1 - layoutAnchor.x) * baseWidth) { leftToRightRowBreak = true; } let rightToLeftRowBreak = false; - if (this._horizontalDirection === HorizontalDirection.RIGHT_TO_LEFT && rowBreakBoundary < -layoutAnchor.x * baseWidth){ + if (this._horizontalDirection === HorizontalDirection.RIGHT_TO_LEFT && rowBreakBoundary < -layoutAnchor.x * baseWidth) { rightToLeftRowBreak = true; } @@ -718,7 +719,7 @@ export class LayoutComponent extends Component { private _doLayoutVertically ( baseHeight: number, columnBreak: boolean, - fnPositionX: (node: Node, columnMaxWidth: number, col: number) => number, + fnPositionX: (node: INode, columnMaxWidth: number, col: number) => number, applyChildren: boolean, ) { const layoutAnchor = this.node.getAnchorPoint(); @@ -796,12 +797,12 @@ export class LayoutComponent extends Component { if (columnBreak) { const columnBreakBoundary = nextY + topBoundaryOfChild + sign * (sign > 0 ? this._paddingTop : this._paddingBottom); let bottomToTopColumnBreak = false; - if (this._verticalDirection === VerticalDirection.BOTTOM_TO_TOP && columnBreakBoundary > (1 - layoutAnchor.y) * baseHeight){ + if (this._verticalDirection === VerticalDirection.BOTTOM_TO_TOP && columnBreakBoundary > (1 - layoutAnchor.y) * baseHeight) { bottomToTopColumnBreak = true; } let topToBottomColumnBreak = false; - if (this._verticalDirection === VerticalDirection.TOP_TO_BOTTOM && columnBreakBoundary < -layoutAnchor.y * baseHeight){ + if (this._verticalDirection === VerticalDirection.TOP_TO_BOTTOM && columnBreakBoundary < -layoutAnchor.y * baseHeight) { topToBottomColumnBreak = true; } @@ -860,11 +861,11 @@ export class LayoutComponent extends Component { private _doLayoutBasic () { const children = this.node.children; - let allChildrenBoundingBox: Rect|null = null; + let allChildrenBoundingBox: Rect | null = null; for (const child of children) { const childTransform = child.getComponent(UITransformComponent); - if (!childTransform){ + if (!childTransform) { continue; } @@ -879,7 +880,7 @@ export class LayoutComponent extends Component { if (allChildrenBoundingBox) { const parentTransform = this.node.parent!.getComponent(UITransformComponent); - if (!parentTransform){ + if (!parentTransform) { return; } @@ -925,9 +926,9 @@ export class LayoutComponent extends Component { const self = this; - const fnPositionY = (child: Node, topOffset: number, row: number) => { + const fnPositionY = (child: INode, topOffset: number, row: number) => { return bottomBoundaryOfLayout + - sign * (topOffset + child.anchorY * child.height * self._getUsedScaleValue(child.getScale().y) + paddingY + row * this._spacingY); + sign * (topOffset + child.anchorY * child.height * self._getUsedScaleValue(child.getScale().y) + paddingY + row * this._spacingY); }; let newHeight = 0; @@ -967,9 +968,9 @@ export class LayoutComponent extends Component { } const self = this; - const fnPositionX = (child: Node, leftOffset: number, column: number) => { + const fnPositionX = (child: INode, leftOffset: number, column: number) => { return leftBoundaryOfLayout + - sign * (leftOffset + child.anchorX * child.width * self._getUsedScaleValue(child.getScale().x) + paddingX + column * this._spacingX); + sign * (leftOffset + child.anchorX * child.width * self._getUsedScaleValue(child.getScale().x) + paddingX + column * this._spacingX); }; let newWidth = 0; @@ -1008,7 +1009,7 @@ export class LayoutComponent extends Component { } - private _getHorizontalBaseWidth (children: Node[]) { + private _getHorizontalBaseWidth (children: Readonly) { let newWidth = 0; let activeChildCount = 0; if (this._resizeMode === ResizeMode.CONTAINER) { @@ -1026,7 +1027,7 @@ export class LayoutComponent extends Component { return newWidth; } - private _getVerticalBaseHeight (children: Node[]) { + private _getVerticalBaseHeight (children: Readonly) { let newHeight = 0; let activeChildCount = 0; if (this._resizeMode === ResizeMode.CONTAINER) { diff --git a/cocos/3d/ui/components/mask-component.ts b/cocos/3d/ui/components/mask-component.ts index 826c81ccf4..ff31b66253 100644 --- a/cocos/3d/ui/components/mask-component.ts +++ b/cocos/3d/ui/components/mask-component.ts @@ -29,15 +29,16 @@ */ import { SpriteFrame } from '../../../assets'; -import { ccclass, executionOrder, menu, property} from '../../../core/data/class-decorator'; -import { SystemEventType } from '../../../core/platform'; +import { ccclass, executionOrder, menu, property } from '../../../core/data/class-decorator'; +import { SystemEventType } from '../../../core/platform/event-manager/event-enum'; import { Color, Mat4, Size, Vec2, Vec3 } from '../../../core/value-types'; import { ccenum } from '../../../core/value-types/enum'; import * as vmath from '../../../core/vmath'; import { UI } from '../../../renderer/ui/ui'; -import { Node } from '../../../scene-graph'; import { GraphicsComponent } from './graphics-component'; import { InstanceMaterialType, UIRenderComponent } from './ui-render-component'; +import { INode } from '../../../core/utils/interfaces'; +import { Node } from '../../../scene-graph'; const _worldMatrix = new Mat4(); const _vec2_temp = new Vec2(); @@ -104,7 +105,7 @@ export class MaskComponent extends UIRenderComponent { } set type (value: MaskType) { - if (this._type === value){ + if (this._type === value) { return; } @@ -191,11 +192,11 @@ export class MaskComponent extends UIRenderComponent { this._updateGraphics(); } - get graphics (){ + get graphics () { return this._graphics; } - get clearGraphics (){ + get clearGraphics () { return this._clearGraphics; } @@ -203,7 +204,7 @@ export class MaskComponent extends UIRenderComponent { visible: false, override: true, }) - get dstBlendFactor (){ + get dstBlendFactor () { return this._dstBlendFactor; } @@ -242,7 +243,7 @@ export class MaskComponent extends UIRenderComponent { return this._color; } - set color (value){ + set color (value) { if (this._color === value) { return; } @@ -271,12 +272,12 @@ export class MaskComponent extends UIRenderComponent { private _graphics: GraphicsComponent | null = null; private _clearGraphics: GraphicsComponent | null = null; - constructor (){ + constructor () { super(); this._instanceMaterialType = InstanceMaterialType.ADDCOLOR; } - public onLoad (){ + public onLoad () { this._createGraphics(); } @@ -307,7 +308,7 @@ export class MaskComponent extends UIRenderComponent { // } // } // else { - if (this._clearGraphics){ + if (this._clearGraphics) { this._clearGraphics.onEnable(); } this._updateGraphics(); @@ -354,7 +355,7 @@ export class MaskComponent extends UIRenderComponent { * * @param cameraPt 屏幕点转换到相机坐标系下的点。 */ - public isHit (cameraPt: Vec2){ + public isHit (cameraPt: Vec2) { const node = this.node; const size = node.getContentSize(); const w = size.width; @@ -396,7 +397,7 @@ export class MaskComponent extends UIRenderComponent { } protected _nodeStateChange (type: SystemEventType) { - if (type === SystemEventType.POSITION_PART){ + if (type === SystemEventType.POSITION_PART) { return; } @@ -405,7 +406,7 @@ export class MaskComponent extends UIRenderComponent { this._updateGraphics(); } - protected _resolutionChanged (){ + protected _resolutionChanged () { this._updateClearGraphics(); } @@ -417,7 +418,7 @@ export class MaskComponent extends UIRenderComponent { return this._clearGraphics !== null && this._graphics !== null && this._renderPermit; } - protected _flushAssembler (){ + protected _flushAssembler () { const assembler = MaskComponent.Assembler!.getAssembler(this); const posAssembler = MaskComponent.PostAssembler!.getAssembler(this); @@ -496,12 +497,12 @@ export class MaskComponent extends UIRenderComponent { graphics.lineWidth = 0; const color = Color.WHITE; color.a = 0; - graphics.fillColor = color; + graphics.fillColor = color; } } - private _updateClearGraphics (){ - if (!this._clearGraphics){ + private _updateClearGraphics () { + if (!this._clearGraphics) { return; } console.log('resolution changed'); @@ -513,7 +514,7 @@ export class MaskComponent extends UIRenderComponent { } private _updateGraphics () { - if (!this._graphics){ + if (!this._graphics) { return; } @@ -548,7 +549,7 @@ export class MaskComponent extends UIRenderComponent { graphics.fill(); } - private _disableGraphics (){ + private _disableGraphics () { if (this._graphics) { this._graphics.onDisable(); } diff --git a/cocos/3d/ui/components/rich-text-component.ts b/cocos/3d/ui/components/rich-text-component.ts index 96fb14e10e..38ac83b508 100644 --- a/cocos/3d/ui/components/rich-text-component.ts +++ b/cocos/3d/ui/components/rich-text-component.ts @@ -41,6 +41,7 @@ import { SpriteComponent } from './sprite-component'; import { UIComponent } from './ui-component'; import { UIRenderComponent } from './ui-render-component'; import { UITransformComponent } from './ui-transfrom-component'; +import { INode } from '../../../core/utils/interfaces'; const _htmlTextParser = new HtmlTextParser(); const RichTextChildName = 'RICHTEXT_CHILD'; @@ -92,7 +93,7 @@ const pool = new Pool((labelSeg: ILabelSegment) => { // @ts-ignore pool.get = function (str: string, richtext: RichTextComponent) { let labelSeg = this._get(); - if (!labelSeg){ + if (!labelSeg) { labelSeg = { node: new PrivateNode(RichTextChildName), comp: null, @@ -150,7 +151,7 @@ pool.get = function (str: string, richtext: RichTextComponent) { return labelObj; }; -interface ILabelSegment{ +interface ILabelSegment { node: PrivateNode; comp: UIRenderComponent | null; clickHandler: ''; @@ -180,7 +181,7 @@ export class RichTextComponent extends UIComponent { return this._string; } set string (value) { - if (this._string === value){ + if (this._string === value) { return; } @@ -315,7 +316,7 @@ export class RichTextComponent extends UIComponent { * 选中此选项后,RichText 将阻止节点边界框中的所有输入事件(鼠标和触摸),从而防止输入事件穿透到底层节点。 */ @property - get handleTouchEvent (){ + get handleTouchEvent () { return this._handleTouchEvent; } @@ -393,7 +394,7 @@ export class RichTextComponent extends UIComponent { } public onRestore () { - if (!CC_EDITOR){ + if (!CC_EDITOR) { return; } @@ -499,7 +500,7 @@ export class RichTextComponent extends UIComponent { private _containsTouchLocation (label: ILabelSegment, point: Vec2) { const comp = label.node.getComponent(UITransformComponent); - if (!comp){ + if (!comp) { return false; } @@ -508,9 +509,9 @@ export class RichTextComponent extends UIComponent { } private _resetState () { - const children = this.node.children; + const children = this.node.children as Mutable; - for (let i = children.length - 1; i >= 0; i--){ + for (let i = children.length - 1; i >= 0; i--) { const child = children[i]; if (child.name === RichTextChildName || child.name === RichTextChildImageName) { if (child.parent === this.node) { @@ -526,7 +527,7 @@ export class RichTextComponent extends UIComponent { return seg.node === child; }); - if (index !== -1){ + if (index !== -1) { pool.put(this._labelSegments[index]); } } @@ -688,7 +689,7 @@ export class RichTextComponent extends UIComponent { } private _addRichTextImageElement (richTextElement: IHtmlTextParserResultObj) { - if (!richTextElement.style){ + if (!richTextElement.style) { return; } @@ -784,7 +785,7 @@ export class RichTextComponent extends UIComponent { for (let i = 0; i < this._textArray.length; ++i) { const richTextElement = this._textArray[i]; const text = richTextElement.text; - if (text === undefined){ + if (text === undefined) { continue; } diff --git a/cocos/3d/ui/components/scroll-view-component.ts b/cocos/3d/ui/components/scroll-view-component.ts index 9b2b6ebb96..34bdaf8e62 100644 --- a/cocos/3d/ui/components/scroll-view-component.ts +++ b/cocos/3d/ui/components/scroll-view-component.ts @@ -31,18 +31,17 @@ import { EventHandler as ComponentEventHandler } from '../../../components'; import { ccclass, executionOrder, menu, property } from '../../../core/data/class-decorator'; import { Event } from '../../../core/event'; -import { EventMouse, EventTouch } from '../../../core/platform'; +import { EventMouse, EventTouch, SystemEventType } from '../../../core/platform'; import Touch from '../../../core/platform/event-manager/CCTouch'; import { Size, Vec2, Vec3 } from '../../../core/value-types'; import { ccenum } from '../../../core/value-types/enum'; import { vec3 } from '../../../core/vmath'; -import { Node } from '../../../scene-graph/node'; import { LayoutComponent } from './layout-component'; import { ScrollBarComponent } from './scroll-bar-component'; import { ViewGroupComponent } from './view-group-component'; import { INode } from '../../../core/utils/interfaces'; -const NodeEvent = Node.EventType; +const NodeEvent = SystemEventType; const NUMBER_OF_GATHERED_TOUCHES_FOR_MOVE_SPEED = 5; const OUT_OF_BOUNDARY_BREAKING_FACTOR = 0.05; @@ -903,7 +902,7 @@ export class ScrollViewComponent extends ViewGroupComponent { cancelEvent.type = NodeEvent.TOUCH_CANCEL; cancelEvent.touch = event.touch; cancelEvent.simulate = true; - (event.target as Node)!.dispatchEvent(cancelEvent); + (event.target as INode)!.dispatchEvent(cancelEvent); this._touchMoved = true; } } @@ -977,10 +976,10 @@ export class ScrollViewComponent extends ViewGroupComponent { if (captureListeners) { // captureListeners are arranged from child to parent for (const listerner of captureListeners) { - const item = listerner as Node; + const item = listerner as INode; if (this.node === item) { - if (event.target && (event.target as Node).getComponent(ViewGroupComponent)) { + if (event.target && (event.target as INode).getComponent(ViewGroupComponent)) { return true; } return false; diff --git a/cocos/3d/ui/components/sprite-component.ts b/cocos/3d/ui/components/sprite-component.ts index 2ce1d6c25a..139701c49d 100644 --- a/cocos/3d/ui/components/sprite-component.ts +++ b/cocos/3d/ui/components/sprite-component.ts @@ -30,7 +30,7 @@ import { SpriteAtlas, SpriteFrame } from '../../../assets'; import { ccclass, executionOrder, menu, property } from '../../../core/data/class-decorator'; -import { SystemEventType } from '../../../core/platform'; +import { SystemEventType } from '../../../core/platform/event-manager/event-enum'; import { Vec2 } from '../../../core/value-types'; import { ccenum } from '../../../core/value-types/enum'; import { clamp } from '../../../core/vmath/utils'; diff --git a/cocos/3d/ui/components/ui-component.ts b/cocos/3d/ui/components/ui-component.ts index 5cb1ea41b6..e6c1e96ba9 100644 --- a/cocos/3d/ui/components/ui-component.ts +++ b/cocos/3d/ui/components/ui-component.ts @@ -30,8 +30,8 @@ import { Component } from '../../../components'; import { ccclass, disallowMultiple, executeInEditMode, executionOrder, property } from '../../../core/data/class-decorator'; import { SystemEventType } from '../../../core/platform/event-manager/event-enum'; +import { INode } from '../../../core/utils/interfaces'; import { UI } from '../../../renderer/ui/ui'; -import { Node } from '../../../scene-graph'; import { CanvasComponent } from './canvas-component'; /** @@ -75,7 +75,7 @@ export class UIComponent extends Component { protected _visibility = -1; - private _lastParent: Node | null = null; + private _lastParent: INode | null = null; public onEnable () { this._lastParent = this.node.parent; this._updateVisibility(); @@ -107,7 +107,7 @@ export class UIComponent extends Component { this._visibility = value; } - protected _parentChanged (node: Node){ + protected _parentChanged (node: INode) { if (node === this.node) { this._updateVisibility(); this._cancelEventFromParent(); @@ -120,9 +120,10 @@ export class UIComponent extends Component { } private _sortSiblings () { - let siblings = this.node.parent && this.node.parent.children; + + const siblings = this.node.parent && this.node.parent.children as Mutable; if (siblings) { - siblings.sort((a, b) => { + siblings.sort((a: INode, b: INode) => { const aComp = a._uiComp; const bComp = b._uiComp; const ca = aComp ? aComp.priority : 0; @@ -149,7 +150,7 @@ export class UIComponent extends Component { } } - private _cancelEventFromParent (){ + private _cancelEventFromParent () { if (this._lastParent) { this._lastParent.off(SystemEventType.CHILD_REMOVED, this._parentChanged, this); this._lastParent = null; diff --git a/cocos/3d/ui/components/ui-render-component.ts b/cocos/3d/ui/components/ui-render-component.ts index 8f54160d31..5f33826c23 100644 --- a/cocos/3d/ui/components/ui-render-component.ts +++ b/cocos/3d/ui/components/ui-render-component.ts @@ -34,7 +34,7 @@ import { property, requireComponent, } from '../../../core/data/class-decorator'; -import { SystemEventType } from '../../../core/platform'; +import { SystemEventType } from '../../../core/platform/event-manager/event-enum'; import { Color } from '../../../core/value-types'; import { ccenum } from '../../../core/value-types/enum'; import { GFXBlendFactor } from '../../../gfx/define'; diff --git a/cocos/3d/ui/components/widget-component.ts b/cocos/3d/ui/components/widget-component.ts index f74470b182..cdeea38777 100644 --- a/cocos/3d/ui/components/widget-component.ts +++ b/cocos/3d/ui/components/widget-component.ts @@ -30,7 +30,7 @@ import { Component} from '../../../components'; import { ccclass, executeInEditMode, executionOrder, menu, property, requireComponent } from '../../../core/data/class-decorator'; -import { SystemEventType } from '../../../core/platform'; +import { SystemEventType } from '../../../core/platform/event-manager/event-enum'; import { Size, Vec3 } from '../../../core/value-types'; import { ccenum } from '../../../core/value-types/enum'; import { INode } from '../../../core/utils/interfaces'; diff --git a/cocos/3d/ui/components/widget-manager.ts b/cocos/3d/ui/components/widget-manager.ts index 76936da256..3769b90452 100644 --- a/cocos/3d/ui/components/widget-manager.ts +++ b/cocos/3d/ui/components/widget-manager.ts @@ -28,10 +28,10 @@ * @hidden */ -import { SystemEventType } from '../../../core/platform'; +import { SystemEventType } from '../../../core/platform/event-manager/event-enum'; import { array } from '../../../core/utils/js'; import { Vec2, Vec3 } from '../../../core/value-types'; -import { vec3 } from '../../../core/vmath'; +import { vec3 } from '../../../core/vmath/vec3'; import { CanvasComponent } from './canvas-component'; import { UIRenderComponent } from './ui-render-component'; import { AlignFlags, AlignMode, WidgetComponent } from './widget-component'; diff --git a/cocos/components/block-input-events-component.ts b/cocos/components/block-input-events-component.ts index ac6e605337..2dd39bb0e3 100644 --- a/cocos/components/block-input-events-component.ts +++ b/cocos/components/block-input-events-component.ts @@ -42,7 +42,7 @@ import { ccclass, menu } from '../core/data/class-decorator'; import { Component } from './component'; import { Event } from '../core/event'; -import { SystemEventType } from '../core/platform'; +import { SystemEventType } from '../core/platform/event-manager/event-enum'; const BlockEvents = [SystemEventType.TOUCH_START, SystemEventType.TOUCH_END, SystemEventType.TOUCH_MOVE, SystemEventType.MOUSE_DOWN, SystemEventType.MOUSE_MOVE, SystemEventType.MOUSE_UP, diff --git a/cocos/components/component.ts b/cocos/components/component.ts index 1050f65c85..bab61413b9 100644 --- a/cocos/components/component.ts +++ b/cocos/components/component.ts @@ -242,7 +242,7 @@ class Component extends CCObject { if (this._sceneGetter) { return this._sceneGetter(); } else { - return this.node.scene._renderScene!; + return this.node.scene!._renderScene!; } } diff --git a/cocos/core/utils/interfaces.ts b/cocos/core/utils/interfaces.ts index 31ed803d65..e0cbbd7c17 100644 --- a/cocos/core/utils/interfaces.ts +++ b/cocos/core/utils/interfaces.ts @@ -25,63 +25,516 @@ import { SystemEventType } from '../platform/event-manager/event-enum'; import { Mat4, Quat, Size, Vec2, Vec3 } from '../value-types'; +import { Scene } from '../../scene-graph/scene'; +import { NodeEventProcessor } from '../../scene-graph/node-event-processor'; +import { Component } from '../../components/component'; +import { Event } from '../event'; -export interface INode { +export interface IBaseNode { + + /** + * @zh + * 是否为常驻节点,与节点的自动释放有关 + */ _persistNode: boolean; + /** + * @en Name of node. + * @zh 该节点名称。 + * @property name + * @type {String} + * @example + * ``` + * node.name = "New Node"; + * cc.log("Node Name: " + node.name); + * ``` + */ name: string; - uuid: string; - scene; - layer; - parent; - children; - childrenCount: number; - hasChanged: boolean; + + /** + * @en The uuid for editor, will be stripped before building project. + * @zh 主要用于编辑器的 uuid,在编辑器下可用于持久化存储,在项目构建之后将变成自增的 id。 + * @property uuid + * @type {String} + * @readOnly + * @example + * ``` + * cc.log("Node Uuid: " + node.uuid); + * ``` + */ + uuid: Readonly; + + /** + * @zh + * 父节点 + */ + parent: this | null; + + /** + * @zh + * 孩子节点数组 + */ + children: Readonly; + + /** + * @en All children nodes. + * @zh 节点的子节点数量。 + * @property childrenCount + * @type {Number} + * @readOnly + * @example + * ``` + * var count = node.childrenCount; + * cc.log("Node Children Count: " + count); + * ``` + */ + childrenCount: Readonly; + + /** + * @en + * The local active state of this node.
+ * Note that a Node may be inactive because a parent is not active, even if this returns true.
+ * Use {{#crossLink "Node/activeInHierarchy:property"}}{{/crossLink}} + * if you want to check if the Node is actually treated as active in the scene. + * @zh + * 当前节点的自身激活状态。
+ * 值得注意的是,一个节点的父节点如果不被激活,那么即使它自身设为激活,它仍然无法激活。
+ * 如果你想检查节点在场景中实际的激活状态可以使用 {{#crossLink "Node/activeInHierarchy:property"}}{{/crossLink}}。 + * @property active + * @type {Boolean} + * @default true + * @example + * ``` + * node.active = false; + * ``` + */ active: boolean; + + /** + * @en Indicates whether this node is active in the scene. + * @zh 表示此节点是否在场景中激活。 + * @property activeInHierarchy + * @type {Boolean} + * @example + * ``` + * cc.log("activeInHierarchy: " + node.activeInHierarchy); + */ activeInHierarchy: boolean; - eventProcessor; - worldScale: Vec3; - worldPosition: Vec3; - worldMatrix: Mat4; - width: number; - height: number; - anchorX: number; - anchorY: number; - _uiComp; - uiTransfromComp; + /** + * @en which scene this node belongs to. + * @zh 此节点属于哪个场景。 + * @type {cc.Scene}} + */ + scene: Readonly | null; + /** + * @en + * Gets all components attached to this node. + * @zh + * 获取此节点上所有的组件 + */ + components: ReadonlyArray; + + /** + * @en Is this node a child of the given node? + * @zh 是否是指定节点的子节点? + * @return True if this node is a child, deep child or identical to the given node. + * @example + * ``` + * node.isChildOf(newNode); + * ``` + */ isChildOf (parent: this): boolean; - addChild (child: this); - getChildByName (name: string); + /** + * @zh 增加一个孩子节点 + * @param child 孩子节点 + */ + addChild (child: this): void; + + /** + * @en Returns a child from the container given its name. + * @zh 通过名称获取节点的子节点。 + * @param name - A name to find the child node. + * @return a CCNode object whose name equals to the input parameter + * @example + * ``` + * var child = node.getChildByName("Test Node"); + * ``` + */ + getChildByName (name: string): this | null; + + /** + * @en Adds a component class to the node. You can also add component to node by passing in the name of the script. + * @zh 向节点添加一个指定类型的组件类,传入参数可以是一个组件类型或 ccclass 的注册名称,也可以是已经获得的组件引用。 + * @example + * ``` + * + * let sprite = node.addComponent(SpriteComponent); + * + * var sprite = node.addComponent(cc.SpriteComponent); + * + * var sprite = node.addComponent("cc.SpriteComponent"); + * + * ``` + */ + addComponent (classConstructor: Constructor): T | null; + addComponent (className: string): Component | null; addComponent (typeOrClassName: string | Function); - _removeComponent (component); + + /** + * @en + * Removes a component identified by the given name or removes the component object given. + * You can also use component.destroy() if you already have the reference. + * @zh + * 删除节点上的指定组件,传入参数可以是一个组件类型或 ccclass 的注册名称,也可以是已经获得的组件引用。 + * 如果你已经获得组件引用,你也可以直接调用 component.destroy() + * @example + * ``` + * + * node.removeComponent(SpriteComponent); + * + * node.removeComponent(cc.SpriteComponent); + * + * node.removeComponent("cc.SpriteComponent"); + * + * ``` + */ + removeComponent (classConstructor: Constructor): void; + removeComponent (classNameOrInstance: string | Component): void; + removeComponent (component: any): void; + + _removeComponent (component: Component): void; + + /** + * @en + * Returns the component of supplied type if the node has one attached, null if it doesn't.
+ * You can also get component in the node by passing in the name of the script. + * @zh + * 获取节点上指定类型的组件,如果节点有附加指定类型的组件,则返回,如果没有则为空。
+ * 传入参数也可以是脚本的名称。 + * @example + * ``` + * + * let sprite = node.getComponent(SpriteComponent); + * + * var sprite = node.getComponent(cc.SpriteComponent); + * + * var sprite = node.getComponent("cc.SpriteComponent"); + * + * ``` + */ + getComponent (classConstructor: Constructor): T | null; + getComponent (className: string): Component | null; getComponent (typeOrClassName: string | Function); + + getComponents (classConstructor: Constructor): T[]; + getComponents (className: string): Component[]; getComponents (typeOrClassName: string | Function); + + /** + * @en Returns all components of supplied type in the node. + * @zh 返回节点上指定类型的所有组件。 + * @example + * ``` + * + * let sprite = node.getComponentInChildren(SpriteComponent); + * + * var sprite = node.getComponentInChildren(cc.SpriteComponent); + * + * var sprite = node.getComponentInChildren("cc.SpriteComponent"); + * + * ``` + */ + getComponentInChildren (classConstructor: Constructor): T | null; + getComponentInChildren (className: string): Component | null; getComponentInChildren (typeOrClassName: string | Function); + + /** + * @en Returns all components of supplied type in self or any of its children. + * @zh 递归查找自身或所有子节点中指定类型的组件 + * @example + * ``` + * + * let sprites = node.getComponentsInChildren(SpriteComponent); + * + * var sprites = node.getComponentsInChildren(cc.SpriteComponent); + * + * var sprites = node.getComponentsInChildren("cc.SpriteComponent"); + * + * + * ``` + */ + getComponentsInChildren (classConstructor: Constructor): T[]; + getComponentsInChildren (className: string): Component[]; getComponentsInChildren (typeOrClassName: string | Function); - getWorldMatrix (out?: Mat4): Mat4; - getPosition (out?: Vec3): Vec3; - setPosition (val: Vec3 | number, y?: number, z?: number); - getWorldPosition (out?: Vec3): Vec3; - setWorldPosition (val: Vec3 | number, y?: number, z?: number); - getRotation (out?: Quat): Quat; - setWorldRotation (val: Quat | number, y?: number, z?: number, w?: number); - getWorldRotation (out?: Quat): Quat; - getWorldRT (out?: Mat4): Mat4; - getScale (out?: Vec3): Vec3; - setScale (val: Vec3 | number, y?: number, z?: number); - updateWorldTransform (); - updateWorldTransformFull (); - getContentSize (out?: Size): Size; - setContentSize (size: Size | number, height?: number); - getAnchorPoint (out?: Vec2): Vec2; - setAnchorPoint (point: Vec2 | number, y?: number); + /** + * @zh + * 销毁实例,实际销毁操作会延迟到当前帧渲染前执行。 + * @returns true 代表销毁成功 + */ + destroy (): boolean; - on (type: string | SystemEventType, callback: Function, target?: Object, useCapture?: any); - off (type: string, callback?: Function, target?: Object, useCapture?: any); - emit (type: string, ...args: any[]); - dispatchEvent (event); + /** + * @en + * Destroy all children from the node, and release all their own references to other objects.
+ * Actual destruct operation will delayed until before rendering. + * @zh + * 销毁所有子节点,并释放所有它们对其它对象的引用。
+ * 实际销毁操作会延迟到当前帧渲染前执行。 + * @example + * ``` + * node.destroyAllChildren(); + * ``` + */ + destroyAllChildren (): void; +} + +export interface INode extends IBaseNode { + + /** + * @zh + * 节点所属层,主要影响射线检测、物理碰撞等,参考 [[Layers]] + */ + layer: number; + + + /** + * @zh + * 这个节点的空间变换信息在当前帧内是否有变过? + */ + hasChanged: Readonly; + + + /** + * @zh + * 节点事件相关的处理器 + */ + eventProcessor: Readonly; + + /** + * @zh + * 本地坐标 + */ + position: Readonly; + + /** + * @zh + * 世界坐标 + */ + worldPosition: Readonly; + + /** + * @zh + * 本地缩放 + */ + scale: Readonly; + + /** + * @zh + * 世界旋转 + */ + worldScale: Readonly; + + /** + * @zh + * 以欧拉角表示的本地旋转值 + */ + eulerAngles: Readonly; + + /** + * @zh + * 本地旋转四元数 + */ + rotation: Readonly; + + /** + * @zh + * 世界旋转四元数 + */ + worldRotation: Readonly; + + /** + * @zh + * 世界变换矩阵 + */ + worldMatrix: Readonly; + + width: number; + + height: number; + + anchorX: number; + + anchorY: number; + + _uiComp; + + uiTransfromComp; + + /** + * @zh + * 获取本地坐标 + * @param out 输出到此目标 vector + */ + getPosition (out?: Vec3): Vec3; + + /** + * @zh + * 设置本地坐标 + * @param position 目标本地坐标 + * @overload + * @param x 目标本地坐标的 X 分量 + * @param y 目标本地坐标的 Y 分量 + * @param z 目标本地坐标的 Z 分量 + * @param w 目标本地坐标的 W 分量 + */ + setPosition (position: Vec3): void; + setPosition (x: number, y: number, z: number): void; + setPosition (val: Vec3 | number, y?: number, z?: number): void; + + /** + * @zh + * 获取世界坐标 + * @param out 输出到此目标 vector + */ + getWorldPosition (out?: Vec3): Vec3; + + /** + * @zh + * 设置世界坐标 + * @param position 目标世界坐标 + * @overload + * @param x 目标世界坐标的 X 分量 + * @param y 目标世界坐标的 Y 分量 + * @param z 目标世界坐标的 Z 分量 + * @param w 目标世界坐标的 W 分量 + */ + setWorldPosition (position: Vec3): void; + setWorldPosition (x: number, y: number, z: number): void; + setWorldPosition (val: Vec3 | number, y?: number, z?: number): void; + + /** + * @zh + * 获取本地旋转 + * @param out 输出到此目标 quaternion + */ + getRotation (out?: Quat): Quat; + + /** + * @zh + * 设置本地旋转 + * @param rotation 目标本地旋转 + * @override + * @param x 目标本地旋转的 X 分量 + * @param y 目标本地旋转的 Y 分量 + * @param z 目标本地旋转的 Z 分量 + * @param w 目标本地旋转的 W 分量 + */ + setRotation (rotation: Quat): void; + setRotation (x: number, y: number, z: number, w: number): void; + setRotation (val: Quat | number, y?: number, z?: number, w?: number): void; + + /** + * @zh + * 获取世界旋转 + * @param out 输出到此目标 quaternion + */ + getWorldRotation (out?: Quat): Quat; + + /** + * @zh + * 设置世界旋转 + * @param rotation 目标世界旋转 + * @override + * @param x 目标世界旋转的 X 分量 + * @param y 目标世界旋转的 Y 分量 + * @param z 目标世界旋转的 Z 分量 + * @param w 目标世界旋转的 W 分量 + */ + setWorldRotation (rotation: Quat): void; + setWorldRotation (x: number, y: number, z: number, w: number): void; + setWorldRotation (val: Quat | number, y?: number, z?: number, w?: number): void; + + /** + * @zh + * 获取本地缩放 + * @param out 输出到此目标 vector + */ + getScale (out?: Vec3): Vec3; + + setScale (scale: Vec3): void; + setScale (x: number, y: number, z: number): void; + setScale (val: Vec3 | number, y?: number, z?: number): void; + + /** + * @zh + * 获取世界缩放 + * @param out 输出到此目标 vector + */ + getWorldScale (out?: Vec3): Vec3; + + /** + * @zh + * 设置世界缩放 + * @param scale 目标世界缩放 + * @override + * @param x 目标世界缩放的 X 分量 + * @param y 目标世界缩放的 Y 分量 + * @param z 目标世界缩放的 Z 分量 + */ + setWorldScale (scale: Vec3): void; + setWorldScale (x: number, y: number, z: number): void; + setWorldScale (val: Vec3 | number, y?: number, z?: number): void; + + /** + * @zh + * 获取世界变换矩阵 + * @param out 输出到此目标矩阵 + */ + getWorldMatrix (out?: Mat4): Mat4; + + /** + * @zh + * 获取只包含坐标和旋转的世界变换矩阵 + * @param out 输出到此目标矩阵 + */ + getWorldRT (out?: Mat4): Mat4; + + /** + * @en + * update the world transform information if outdated + * here we assume all nodes are children of a scene node, + * which is always not dirty, has an identity transform and no parent. + * @zh + * 更新节点的世界变换信息 + */ + updateWorldTransform (): void; + + /** + * @zh + * 更新节点的完整世界变换信息 + */ + updateWorldTransformFull (): void; + + getContentSize (out?: Size): Size; + setContentSize (size: Size | number, height?: number): void; + getAnchorPoint (out?: Vec2): Vec2; + setAnchorPoint (point: Vec2 | number, y?: number): void; + + /** + * @zh + * 节点事件API,注:未来可能会移除 + */ + on (type: string | SystemEventType, callback: Function, target?: Object, useCapture?: boolean): void; + off (type: string, callback?: Function, target?: Object, useCapture?: boolean): void; + once (type: string, callback: Function, target?: Object, useCapture?: boolean): void; + emit (type: string, ...args: any[]): void; + dispatchEvent (event: Event): void; + hasEventListener (type: string): void; + targetOff (target: string | Object): void; + pauseSystemEvents (recursive: boolean): void; + resumeSystemEvents (recursive: boolean): void; } diff --git a/cocos/scene-graph/base-node.ts b/cocos/scene-graph/base-node.ts index 2695daceb9..c69e6df1db 100644 --- a/cocos/scene-graph/base-node.ts +++ b/cocos/scene-graph/base-node.ts @@ -36,6 +36,7 @@ import * as js from '../core/utils/js'; import { baseNodePolyfill } from './base-node-dev'; import { Scene } from './scene'; import { CCClass } from '../core/data'; +import { IBaseNode } from '../core/utils/interfaces'; /** * @@ -84,7 +85,7 @@ function getConstructor (typeOrClassName: string | Function): Function | null { * @protected */ @ccclass('cc._BaseNode') -export class BaseNode extends CCObject { +export class BaseNode extends CCObject implements IBaseNode { /** * Gets all components attached to this node. */ @@ -246,6 +247,11 @@ export class BaseNode extends CCObject { this.setParent(value); } + /** + * @en which scene this node belongs to. + * @zh 此节点属于哪个场景。 + * @type {cc.Scene}} + */ get scene () { return this._scene; } diff --git a/cocos/scene-graph/node.ts b/cocos/scene-graph/node.ts index c6d23a1f6a..ae808638f6 100644 --- a/cocos/scene-graph/node.ts +++ b/cocos/scene-graph/node.ts @@ -32,12 +32,12 @@ import { ccclass, property } from '../core/data/class-decorator'; import Event from '../core/event/event'; import { eventManager } from '../core/platform/event-manager'; import { SystemEventType } from '../core/platform/event-manager/event-enum'; -import { Mat4, Quat, Vec3, Size, Vec2 } from '../core/value-types'; +import { INode } from '../core/utils/interfaces'; +import { Mat4, Quat, Size, Vec2, Vec3 } from '../core/value-types'; import { mat4, quat, vec3 } from '../core/vmath'; import { BaseNode } from './base-node'; import { Layers } from './layers'; import { NodeEventProcessor } from './node-event-processor'; -import { INode } from '../core/utils/interfaces'; const v3_a = new Vec3(); const q_a = new Quat();