From 0ac050b843273810a8739800a66a6a28bee1a9d3 Mon Sep 17 00:00:00 2001 From: Leslie Date: Tue, 15 Jan 2019 11:11:52 +0800 Subject: [PATCH 1/2] renderable-component.js: rename to ts, tslint autofix --- ...e-component.js => renderable-component.ts} | 38 +++++++++---------- 1 file changed, 19 insertions(+), 19 deletions(-) rename cocos/3d/framework/{renderable-component.js => renderable-component.ts} (78%) diff --git a/cocos/3d/framework/renderable-component.js b/cocos/3d/framework/renderable-component.ts similarity index 78% rename from cocos/3d/framework/renderable-component.js rename to cocos/3d/framework/renderable-component.ts index 533c00e606..24131cf972 100644 --- a/cocos/3d/framework/renderable-component.js +++ b/cocos/3d/framework/renderable-component.ts @@ -1,7 +1,7 @@ -//@ts-check +// @ts-check +import { Component } from '../../components/component'; import { _decorator } from '../../core/data/index'; -import { Material } from "../assets/material"; -import { Component } from "../../components/component"; +import { Material } from '../assets/material'; const { ccclass, property } = _decorator; @ccclass('cc.RenderableComponent') @@ -10,21 +10,21 @@ export default class RenderableComponent extends Component { * @type {Material[]} */ @property([Material]) - _materials = []; + public _materials = []; - constructor() { + constructor () { super(); } @property({ type: [Material], - displayName: 'Materials' + displayName: 'Materials', }) - get sharedMaterials() { + get sharedMaterials () { return this._materials; } - set sharedMaterials(val) { + set sharedMaterials (val) { // 因为现在编辑器的做法是,当这个val传进来时,内部的materials已经被修改了。 // 我们没办法做到上面的最优化,只能先清理ModelComponent里所有Submodel的材质,再重新关联。 this._clearMaterials(); @@ -37,14 +37,14 @@ export default class RenderableComponent extends Component { * !#ch 模型材质 * @type {Material[]} */ - get materials() { + get materials () { for (let i = 0; i < this._materials.length; i++) { this._materials[i] = this.getMaterial(i); } return this._materials; } - set materials(val) { + set materials (val) { const dLen = val.length - this._materials.length; if (dLen > 0) { this._materials = this._materials.concat(new Array(dLen).fill(null)); @@ -62,12 +62,12 @@ export default class RenderableComponent extends Component { * !#ch 返回相对应序号的材质 * @param {Number} idx - Look for the material list number */ - getMaterial(idx) { + public getMaterial (idx) { if (idx < 0 || idx >= this._materials.length || this._materials[idx] == null) { return null; } - let instantiated = Material.getInstantiatedMaterial(this._materials[idx], this); + const instantiated = Material.getInstantiatedMaterial(this._materials[idx], this); if (instantiated !== this._materials[idx]) { this.setMaterial(instantiated, idx); } @@ -75,38 +75,38 @@ export default class RenderableComponent extends Component { return this._materials[idx]; } - getSharedMaterial(idx) { + public getSharedMaterial (idx) { if (idx < 0 || idx >= this._materials.length) { return null; } return this._materials[idx]; } - get material() { + get material () { return this.getMaterial(0); } - get sharedMaterial() { + get sharedMaterial () { return this.getSharedMaterial(0); } - set material(val) { + set material (val) { if (this._materials.length === 1 && this._materials[0] === val) { return; } this.setMaterial(val, 0); } - setMaterial(material, index) { + public setMaterial (material, index) { this._materials[index] = material; this._onMaterialModified(index, material); } - _onMaterialModified(index, material) { + public _onMaterialModified (index, material) { } - _clearMaterials() { + public _clearMaterials () { } } From 437755c5a2017e2e06cb0e06c5242d8da5bd8edd Mon Sep 17 00:00:00 2001 From: Leslie Date: Tue, 15 Jan 2019 11:13:24 +0800 Subject: [PATCH 2/2] fix ts errors --- cocos/3d/framework/renderable-component.ts | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/cocos/3d/framework/renderable-component.ts b/cocos/3d/framework/renderable-component.ts index 24131cf972..4d0055ed0e 100644 --- a/cocos/3d/framework/renderable-component.ts +++ b/cocos/3d/framework/renderable-component.ts @@ -6,11 +6,8 @@ const { ccclass, property } = _decorator; @ccclass('cc.RenderableComponent') export default class RenderableComponent extends Component { - /** - * @type {Material[]} - */ @property([Material]) - public _materials = []; + private _materials: Material[] = []; constructor () { super(); @@ -39,7 +36,7 @@ export default class RenderableComponent extends Component { */ get materials () { for (let i = 0; i < this._materials.length; i++) { - this._materials[i] = this.getMaterial(i); + this._materials[i] = this.getMaterial(i)!; } return this._materials; } @@ -86,10 +83,6 @@ export default class RenderableComponent extends Component { return this.getMaterial(0); } - get sharedMaterial () { - return this.getSharedMaterial(0); - } - set material (val) { if (this._materials.length === 1 && this._materials[0] === val) { return; @@ -97,6 +90,10 @@ export default class RenderableComponent extends Component { this.setMaterial(val, 0); } + get sharedMaterial () { + return this.getSharedMaterial(0); + } + public setMaterial (material, index) { this._materials[index] = material; this._onMaterialModified(index, material);