Skip to content

Commit

Permalink
ccclass: fix default value when using cc.String (cocos#4689)
Browse files Browse the repository at this point in the history
  • Loading branch information
jareguo authored Jun 24, 2019
1 parent d97719b commit fa51126
Show file tree
Hide file tree
Showing 11 changed files with 178 additions and 33 deletions.
12 changes: 6 additions & 6 deletions EngineErrorMap.md
Original file line number Diff line number Diff line change
Expand Up @@ -1355,24 +1355,24 @@ Default value must be initialized at their declaration:
type: cc.Integer
default: 0 // <--
})
value;
myProp;
// After:
@property({
type: cc.Integer
})
value = 0; // <--
myProp = 0; // <--
```

### 3654

Please specifiy a default value for "%s" property at its declaration:
Please specifiy a default value for "%s.%s" at its declaration:
```
// Before:
@property(...)
value;
myProp;
// After:
@property(...)
value = 0
myProp = 0;
```

### 3655
Expand Down Expand Up @@ -1912,7 +1912,7 @@ The 'type' attribute of '%s.%s' must be child class of cc.Asset, otherwise you s

### 5510

The 'type' attribute of '%s.%s' can not be 'Number', use 'Float' or 'Integer' instead please.
The 'type' attribute of '%s.%s' can not be 'Number', use cc.Float or cc.Integer instead please.

### 5511

Expand Down
2 changes: 1 addition & 1 deletion cocos2d/animation/animation-clip.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var AnimationClip = cc.Class({
properties: {
_duration: {
default: 0,
type: 'Float',
type: cc.Float,
},

/**
Expand Down
2 changes: 1 addition & 1 deletion cocos2d/core/components/CCProgressBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ var ProgressBar = cc.Class({
*/
progress: {
default: 1,
type: 'Float',
type: cc.Float,
range: [0, 1, 0.1],
slide: true,
tooltip: CC_DEV && 'i18n:COMPONENT.progress.progress',
Expand Down
2 changes: 1 addition & 1 deletion cocos2d/core/components/CCScrollView.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ let ScrollView = cc.Class({
*/
brake: {
default: 0.5,
type: 'Float',
type: cc.Float,
range: [0, 1, 0.1],
tooltip: CC_DEV && 'i18n:COMPONENT.scrollview.brake',
},
Expand Down
2 changes: 1 addition & 1 deletion cocos2d/core/components/CCStudioComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ var StudioComponent = cc.Class({
sliderProgress: {
default: 0.5,
readonly: true,
type: 'Float',
type: cc.Float,
range: [0, 1, 0.1]
},

Expand Down
2 changes: 1 addition & 1 deletion cocos2d/core/platform/CCClass.js
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,7 @@ function declareProperties (cls, className, properties, baseClass, mixins, es6)
properties {
width: {
default: 128,
type: 'Integer',
type: cc.Integer,
tooltip: 'The width of sprite'
},
height: 128,
Expand Down
33 changes: 28 additions & 5 deletions cocos2d/core/platform/attribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,12 @@ function setClassAttr (ctor, propName, key, value) {
* })
* member = [];
*/
cc.Integer = 'Integer';
cc.Integer = {
default: 0,
toString () {
return 'Integer';
}
};

/**
* Indicates that the elements in array should be type double.
Expand All @@ -182,7 +187,12 @@ cc.Integer = 'Integer';
* })
* member = [];
*/
cc.Float = 'Float';
cc.Float = {
default: 0,
toString () {
return 'Float';
}
};

if (CC_EDITOR) {
js.get(cc, 'Number', function () {
Expand All @@ -207,7 +217,12 @@ if (CC_EDITOR) {
* })
* member = [];
*/
cc.Boolean = 'Boolean';
cc.Boolean = {
default: false,
toString () {
return 'Boolean';
}
};

/**
* Indicates that the elements in array should be type string.
Expand All @@ -225,7 +240,12 @@ cc.Boolean = 'Boolean';
* })
* member = [];
*/
cc.String = 'String';
cc.String = {
default: '',
toString () {
return 'String';
}
};

/*
BuiltinAttributes: {
Expand All @@ -244,7 +264,7 @@ Callbacks: {
*/

function getTypeChecker (type, attrName) {
if (CC_DEV) {
if (CC_EDITOR || CC_TEST) {
return function (constructor, mainPropName) {
var propInfo = '"' + js.getClassName(constructor) + '.' + mainPropName + '"';
var mainPropAttrs = attr(constructor, mainPropName);
Expand All @@ -253,6 +273,9 @@ function getTypeChecker (type, attrName) {
if (mainPropAttrsType === cc.Integer || mainPropAttrsType === cc.Float) {
mainPropAttrsType = 'Number';
}
else if (mainPropAttrsType === cc.String || mainPropAttrsType === cc.Boolean) {
mainPropAttrsType = '' + mainPropAttrsType;
}
if (mainPropAttrsType !== type) {
cc.warnID(3604, propInfo);
return;
Expand Down
32 changes: 27 additions & 5 deletions cocos2d/core/platform/preprocess-class.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,27 @@ function parseType (val, type, className, propName) {
js.getClassName(type));
}
}
else if (type === 'Number') {
cc.warnID(5510, className, propName);
}
else if (type == null) {
cc.warnID(5511, className, propName);
else {
switch (type) {
case 'Number':
cc.warnID(5510, className, propName);
break;
case 'String':
cc.warn(`The type of "${className}.${propName}" must be cc.String, not "String".`);
break;
case 'Boolean':
cc.warn(`The type of "${className}.${propName}" must be cc.Boolean, not "Boolean".`);
break;
case 'Float':
cc.warn(`The type of "${className}.${propName}" must be cc.Float, not "Float".`);
break;
case 'Integer':
cc.warn(`The type of "${className}.${propName}" must be cc.Integer, not "Integer".`);
break;
case null:
cc.warnID(5511, className, propName);
break;
}
}
}
}
Expand Down Expand Up @@ -297,6 +313,12 @@ exports.getFullFormOfProperty = function (options, propname_dev, classname_dev)
_short: true
};
}
else if (options === cc.String || options === cc.Integer || options === cc.Float || options === cc.Boolean) {
return {
default: options.default,
_short: true
};
}
else {
return {
default: options,
Expand Down
4 changes: 2 additions & 2 deletions test/qunit/unit-es5/_init.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var TestTexture = cc.Class({
*/
width: {
default: 0,
type: 'Integer',
type: cc.Integer,
readonly: true
},

Expand All @@ -49,7 +49,7 @@ var TestTexture = cc.Class({
*/
height: {
default: 0,
type: 'Integer',
type: cc.Integer,
readonly: true
},
}
Expand Down
62 changes: 53 additions & 9 deletions test/qunit/unit-es5/test-class.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ test('test', function () {
serializable: false
},
weight10: {
type: 'Integer',
type: cc.Integer,
set: function (value) {
this.weight = Math.floor(value / 10);
},
Expand All @@ -33,7 +33,7 @@ test('test', function () {
}
},
weight5x: {
type: 'Integer',
type: cc.Integer,
get: function () {
return this.weight * 5;
},
Expand Down Expand Up @@ -722,10 +722,6 @@ test('simplified properties define', function () {
var ArrayType = cc.Class({
properties: {
empty: [],
bool: [cc.Boolean],
string: [cc.String],
float: [cc.Float],
int: [cc.Integer],
valueType: [cc.Vec2],
node: [cc.Node],
rawAsset: [cc.RawAsset],
Expand All @@ -738,7 +734,6 @@ test('simplified properties define', function () {

var arrayObj = new ArrayType();

strictEqual(cc.Class.attr(ArrayType, 'bool').type, cc.Boolean, 'checking array of bool type');
strictEqual(cc.Class.attr(ArrayType, 'valueType').type, 'Object', 'checking array of vec2 type');
strictEqual(cc.Class.attr(ArrayType, 'valueType').ctor, cc.Vec2, 'checking array of vec2 ctor');
strictEqual(cc.Class.attr(ArrayType, 'node').type, 'Object', 'checking array of node type');
Expand All @@ -747,14 +742,63 @@ test('simplified properties define', function () {
strictEqual(cc.Class.attr(ArrayType, 'rawAsset').ctor, cc.RawAsset, 'checking array of raw asset ctor');

deepEqual(arrayObj.empty, [], 'checking array of empty');
deepEqual(arrayObj.bool, [], 'checking array of bool');
deepEqual(arrayObj.string, [], 'checking array of string');
deepEqual(arrayObj.valueType, [], 'checking array of valueType');
deepEqual(arrayObj.node, [], 'checking array of node');
deepEqual(arrayObj.rawAsset, [], 'checking array of rawAsset');
deepEqual(arrayObj.asset, [], 'checking array of asset');
});

test('simplified properties define using cc.xxxType', function () {
var Type = cc.Class({
properties: {
string: cc.String,
bool: cc.Boolean,
float: cc.Float,
int: cc.Integer,
}
});
var ArrayType = cc.Class({
properties: {
string: [cc.String],
bool: [cc.Boolean],
float: [cc.Float],
int: [cc.Integer],
}
});

strictEqual(cc.Class.attr(Type, 'string').type, undefined, 'checking string type');
strictEqual(cc.Class.attr(Type, 'string').ctor, undefined, 'checking string ctor');
strictEqual(cc.Class.attr(Type, 'bool').type, undefined, 'checking bool type');
strictEqual(cc.Class.attr(Type, 'bool').ctor, undefined, 'checking bool ctor');
strictEqual(cc.Class.attr(Type, 'float').type, undefined, 'checking float type');
strictEqual(cc.Class.attr(Type, 'float').ctor, undefined, 'checking float ctor');
strictEqual(cc.Class.attr(Type, 'int').type, undefined, 'checking int type');
strictEqual(cc.Class.attr(Type, 'int').ctor, undefined, 'checking int ctor');

strictEqual(cc.Class.attr(ArrayType, 'string').type, cc.String, 'checking array of string type');
strictEqual(cc.Class.attr(ArrayType, 'string').ctor, undefined, 'checking array of string ctor');
strictEqual(cc.Class.attr(ArrayType, 'bool').type, cc.Boolean, 'checking array of bool type');
strictEqual(cc.Class.attr(ArrayType, 'bool').ctor, undefined, 'checking array of bool ctor');
strictEqual(cc.Class.attr(ArrayType, 'float').type, cc.Float, 'checking array of float type');
strictEqual(cc.Class.attr(ArrayType, 'float').ctor, undefined, 'checking array of float ctor');
strictEqual(cc.Class.attr(ArrayType, 'int').type, cc.Integer, 'checking array of int type');
strictEqual(cc.Class.attr(ArrayType, 'int').ctor, undefined, 'checking array of int ctor');

var obj = new Type();
var arrayObj = new ArrayType();

strictEqual(obj.string, '', 'checking default value of string');
strictEqual(obj.bool, false, 'checking default value of bool');
strictEqual(obj.float, 0, 'checking default value of float');
strictEqual(obj.int, 0, 'checking default value of int');

deepEqual(arrayObj.bool, [], 'checking array of bool');
deepEqual(arrayObj.string, [], 'checking array of string');
deepEqual(arrayObj.float, [], 'checking array of float');
deepEqual(arrayObj.int, [], 'checking array of int');
});


// test('call CCClass', function () {
// var Husky = cc.Class({
// properties: {
Expand Down
58 changes: 57 additions & 1 deletion test/qunit/unit/test-class.js
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,62 @@ largeModule('Class ES6');
deepEqual(arrayObj.asset, [], 'checking array of asset');
});

test('simplified properties define using cc.xxxType', function () {
@ccclass
class Type {
@property(cc.String)
string = '';
@property(cc.Boolean)
bool = false;
@property(cc.Float)
float = 0;
@property(cc.Integer)
int = 0;
}
@ccclass
class ArrayType {
@property([cc.String])
string = [];
@property([cc.Boolean])
bool = [];
@property([cc.Float])
float = [];
@property([cc.Integer])
int = [];
}

strictEqual(cc.Class.attr(Type, 'string').type, undefined, 'checking string type');
strictEqual(cc.Class.attr(Type, 'string').ctor, undefined, 'checking string ctor');
strictEqual(cc.Class.attr(Type, 'bool').type, undefined, 'checking bool type');
strictEqual(cc.Class.attr(Type, 'bool').ctor, undefined, 'checking bool ctor');
strictEqual(cc.Class.attr(Type, 'float').type, undefined, 'checking float type');
strictEqual(cc.Class.attr(Type, 'float').ctor, undefined, 'checking float ctor');
strictEqual(cc.Class.attr(Type, 'int').type, undefined, 'checking int type');
strictEqual(cc.Class.attr(Type, 'int').ctor, undefined, 'checking int ctor');

strictEqual(cc.Class.attr(ArrayType, 'string').type, cc.String, 'checking array of string type');
strictEqual(cc.Class.attr(ArrayType, 'string').ctor, undefined, 'checking array of string ctor');
strictEqual(cc.Class.attr(ArrayType, 'bool').type, cc.Boolean, 'checking array of bool type');
strictEqual(cc.Class.attr(ArrayType, 'bool').ctor, undefined, 'checking array of bool ctor');
strictEqual(cc.Class.attr(ArrayType, 'float').type, cc.Float, 'checking array of float type');
strictEqual(cc.Class.attr(ArrayType, 'float').ctor, undefined, 'checking array of float ctor');
strictEqual(cc.Class.attr(ArrayType, 'int').type, cc.Integer, 'checking array of int type');
strictEqual(cc.Class.attr(ArrayType, 'int').ctor, undefined, 'checking array of int ctor');

var obj = new Type();
var arrayObj = new ArrayType();

strictEqual(obj.string, '', 'checking default value of string');
strictEqual(obj.bool, false, 'checking default value of bool');
strictEqual(obj.float, 0, 'checking default value of float');
strictEqual(obj.int, 0, 'checking default value of int');

deepEqual(arrayObj.bool, [], 'checking array of bool');
deepEqual(arrayObj.string, [], 'checking array of string');
deepEqual(arrayObj.float, [], 'checking array of float');
deepEqual(arrayObj.int, [], 'checking array of int');
});

// test('property', function () {
// @ccclass
// class Foo {
Expand All @@ -850,7 +906,7 @@ largeModule('Class ES6');
// return this.bar;
// };
//
// // @property('Integer')
// // @property(cc.Integer)
// set bbb (value) {
// this.bar = value;
// }
Expand Down

0 comments on commit fa51126

Please sign in to comment.