Skip to content

Commit 530891e

Browse files
author
SeanLin
committed
Merge pull request cocos2d#1037 from dingpinglv/ContentScaleFactorNotEqual1
Fix handling of contentScaleFactor != 1
2 parents 5be5c80 + 4a2f77a commit 530891e

File tree

6 files changed

+38
-18
lines changed

6 files changed

+38
-18
lines changed

cocos2d/label_nodes/CCLabelTTF.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ cc.LabelTTFCanvas = cc.Sprite.extend(/** @lends cc.LabelTTFCanvas# */{
200200
this._fontName = fontName;
201201
this._hAlignment = hAlignment;
202202
this._vAlignment = vAlignment;
203-
this._fontSize = fontSize * cc.CONTENT_SCALE_FACTOR();
203+
this._fontSize = fontSize;
204204
this._fontStyleStr = this._fontSize + "px '" + this._fontName + "'";
205205
this._fontClientHeight = cc.LabelTTF.__getFontHeightByDiv(this._fontName,this._fontSize);
206206
this.setString(strInfo);
@@ -333,6 +333,7 @@ cc.LabelTTFCanvas = cc.Sprite.extend(/** @lends cc.LabelTTFCanvas# */{
333333
this._fontSize = textDefinition.fontSize;
334334
this._fontStyleStr = this._fontSize + "px '" + this._fontName + "'";
335335

336+
336337
// shadow
337338
if ( textDefinition.shadowEnabled)
338339
this.enableShadow(textDefinition.shadowOffset, textDefinition.shadowOpacity, textDefinition.shadowBlur, false);
@@ -566,8 +567,9 @@ cc.LabelTTFCanvas = cc.Sprite.extend(/** @lends cc.LabelTTFCanvas# */{
566567
}
567568

568569
var locVAlignment = this._vAlignment, locHAlignment = this._hAlignment,
569-
locContentSizeWidth = this._contentSize.width, locContentSizeHeight = this._contentSize.height;
570-
var locFontHeight = this._fontClientHeight;
570+
locContentSizeWidth = this._contentSize.width* cc.CONTENT_SCALE_FACTOR(),
571+
locContentSizeHeight = this._contentSize.height* cc.CONTENT_SCALE_FACTOR();
572+
var locFontHeight = this._fontClientHeight* cc.CONTENT_SCALE_FACTOR();
571573

572574
context.textBaseline = cc.LabelTTF._textBaseline[locVAlignment];
573575
context.textAlign = cc.LabelTTF._textAlign[locHAlignment];
@@ -670,6 +672,7 @@ cc.LabelTTFWebGL = cc.Sprite.extend(/** @lends cc.LabelTTFWebGL# */{
670672
_string:"",
671673
_isMultiLine:false,
672674
_fontStyleStr:null,
675+
_scaledFontStyleStr:null,
673676
_colorStyleStr:null,
674677

675678
// font shadow
@@ -702,6 +705,7 @@ cc.LabelTTFWebGL = cc.Sprite.extend(/** @lends cc.LabelTTFWebGL# */{
702705
this._vAlignment = cc.VERTICAL_TEXT_ALIGNMENT_TOP;
703706
this._opacityModifyRGB = false;
704707
this._fontStyleStr = "";
708+
this._scaledFontStyleStr = "";
705709
this._colorStyleStr = "";
706710
this._fontName = "Arial";
707711
this._opacity = 255;
@@ -836,8 +840,9 @@ cc.LabelTTFWebGL = cc.Sprite.extend(/** @lends cc.LabelTTFWebGL# */{
836840
this._fontName = fontName;
837841
this._hAlignment = hAlignment;
838842
this._vAlignment = vAlignment;
839-
this._fontSize = fontSize * cc.CONTENT_SCALE_FACTOR();
843+
this._fontSize = fontSize;
840844
this._fontStyleStr = this._fontSize + "px '" + this._fontName + "'";
845+
this._scaledFontStyleStr = this._fontSize * cc.CONTENT_SCALE_FACTOR() + "px '" + this._fontName + "'";
841846
this._fontClientHeight = cc.LabelTTF.__getFontHeightByDiv(this._fontName,this._fontSize);
842847
this.setString(strInfo);
843848
this._updateTexture();
@@ -986,6 +991,7 @@ cc.LabelTTFWebGL = cc.Sprite.extend(/** @lends cc.LabelTTFWebGL# */{
986991
this._fontName = textDefinition.fontName;
987992
this._fontSize = textDefinition.fontSize || 12;
988993
this._fontStyleStr = this._fontSize + "px '" + this._fontName + "'";
994+
this._scaledFontStyleStr = this._fontSize * cc.CONTENT_SCALE_FACTOR() + "px '" + this._fontName + "'";
989995
this._fontClientHeight = cc.LabelTTF.__getFontHeightByDiv(this._fontName,this._fontSize);
990996

991997
// shadow
@@ -1006,6 +1012,7 @@ cc.LabelTTFWebGL = cc.Sprite.extend(/** @lends cc.LabelTTFWebGL# */{
10061012
_prepareTextDefinition:function(adjustForResolution){
10071013
var texDef = new cc.FontDefinition();
10081014

1015+
//Do these reference to CONTENT_SCALE_FACTOR need to be removed ?
10091016
if (adjustForResolution){
10101017
texDef.fontSize = this._fontSize * cc.CONTENT_SCALE_FACTOR();
10111018
texDef.fontDimensions = cc.SIZE_POINTS_TO_PIXELS(this._dimensions);
@@ -1107,6 +1114,7 @@ cc.LabelTTFWebGL = cc.Sprite.extend(/** @lends cc.LabelTTFWebGL# */{
11071114
if (this._fontSize != fontSize) {
11081115
this._fontSize = fontSize;
11091116
this._fontStyleStr = fontSize + "px '" + this._fontName + "'";
1117+
this._scaledFontStyleStr = this._fontSize * cc.CONTENT_SCALE_FACTOR() + "px '" + this._fontName + "'";
11101118
this._fontClientHeight = cc.LabelTTF.__getFontHeightByDiv(this._fontName,fontSize);
11111119
// Force update
11121120
this._needUpdateTexture = true;
@@ -1121,6 +1129,7 @@ cc.LabelTTFWebGL = cc.Sprite.extend(/** @lends cc.LabelTTFWebGL# */{
11211129
if (this._fontName && this._fontName != fontName) {
11221130
this._fontName = fontName;
11231131
this._fontStyleStr = this._fontSize + "px '" + fontName + "'";
1132+
this._scaledFontStyleStr = this._fontSize * cc.CONTENT_SCALE_FACTOR() + "px '" + this._fontName + "'";
11241133
this._fontClientHeight = cc.LabelTTF.__getFontHeightByDiv(fontName,this._fontSize);
11251134
// Force update
11261135
this._needUpdateTexture = true;
@@ -1136,8 +1145,8 @@ cc.LabelTTFWebGL = cc.Sprite.extend(/** @lends cc.LabelTTFWebGL# */{
11361145

11371146
context.setTransform(1, 0, 0, 1, 0, locContentSizeHeight);
11381147
//this is fillText for canvas
1139-
if (context.font != this._fontStyleStr)
1140-
context.font = this._fontStyleStr;
1148+
if (context.font != this._scaledFontStyleStr)
1149+
context.font = this._scaledFontStyleStr;
11411150
context.fillStyle = this._fillColorStr;
11421151

11431152
//stroke style setup
@@ -1335,8 +1344,8 @@ cc.LabelTTFWebGL = cc.Sprite.extend(/** @lends cc.LabelTTFWebGL# */{
13351344
this._labelContext.font = this._fontStyleStr;
13361345
this._updateTTF();
13371346
var width = this._contentSize.width, height = this._contentSize.height;
1338-
this._labelCanvas.width = width;
1339-
this._labelCanvas.height = height;
1347+
this._labelCanvas.width = width * cc.CONTENT_SCALE_FACTOR();
1348+
this._labelCanvas.height = height * cc.CONTENT_SCALE_FACTOR();;
13401349

13411350
//draw text to labelCanvas
13421351
this._drawTTFInCanvasForWebGL(this._labelContext);

cocos2d/platform/jsloader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@
217217
'../extensions/PluginX/plugins/SocialQQWeibo.js',
218218
'../extensions/PluginX/plugins/SocialQzone.js',
219219
'../extensions/PluginX/plugins/SocialTwitter.js',
220-
'../extensions/PluginX/plugins/SocialFacebook.js',
220+
'../extensions/PluginX/plugins/SocialFacebook.js'
221221
//'../extensions/PluginX/plugins/AdsGoogle.js'
222222
]);
223223
}

cocos2d/sprite_nodes/CCSprite.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ cc.generateTextureCacheForColor.tempCtx = cc.generateTextureCacheForColor.tempCa
122122
cc.generateTintImage2 = function (texture, color, rect) {
123123
if (!rect) {
124124
rect = cc.rect(0, 0, texture.width, texture.height);
125+
rect = cc.RECT_PIXELS_TO_POINTS(rect);
125126
}
126127
var selColor;
127128
if (color instanceof cc.Color4F) {
@@ -933,6 +934,7 @@ cc.SpriteCanvas = cc.NodeRGBA.extend(/** @lends cc.SpriteCanvas# */{
933934
loadImg.addEventListener("load", function () {
934935
if (!rect) {
935936
rect = cc.rect(0, 0, loadImg.width, loadImg.height);
937+
rect = cc.RECT_PIXELS_TO_POINTS(rect);
936938
}
937939
selfPointer.initWithTexture(loadImg, rect);
938940
cc.TextureCache.getInstance().cacheImage(filename, loadImg);
@@ -947,6 +949,7 @@ cc.SpriteCanvas = cc.NodeRGBA.extend(/** @lends cc.SpriteCanvas# */{
947949
if (texture) {
948950
if (!rect) {
949951
rect = cc.rect(0, 0, texture.width, texture.height);
952+
rect = cc.RECT_PIXELS_TO_POINTS(rect);
950953
}
951954
return this.initWithTexture(texture, rect);
952955
}
@@ -996,8 +999,10 @@ cc.SpriteCanvas = cc.NodeRGBA.extend(/** @lends cc.SpriteCanvas# */{
996999

9971000
if (!rect) {
9981001
rect = cc.rect(0, 0, 0, 0);
999-
if ((texture instanceof HTMLImageElement) || (texture instanceof HTMLCanvasElement))
1002+
if ((texture instanceof HTMLImageElement) || (texture instanceof HTMLCanvasElement)) {
10001003
rect.size = cc.size(texture.width, texture.height);
1004+
rect = cc.RECT_PIXELS_TO_POINTS(rect);
1005+
}
10011006
}
10021007
this._originalTexture = texture;
10031008

@@ -1163,7 +1168,7 @@ cc.SpriteCanvas = cc.NodeRGBA.extend(/** @lends cc.SpriteCanvas# */{
11631168
return cc.SpriteFrame._frameWithTextureForCanvas(this._texture,
11641169
cc.RECT_POINTS_TO_PIXELS(this._rect),
11651170
this._rectRotated,
1166-
this._unflippedOffsetPositionFromCenter,
1171+
cc.POINT_POINTS_TO_PIXELS(this._unflippedOffsetPositionFromCenter),
11671172
cc.SIZE_POINTS_TO_PIXELS(this._contentSize));
11681173
},
11691174

@@ -1203,8 +1208,10 @@ cc.SpriteCanvas = cc.NodeRGBA.extend(/** @lends cc.SpriteCanvas# */{
12031208

12041209
if (this._texture != texture) {
12051210
if (texture instanceof HTMLImageElement) {
1206-
if (!this._rect || cc.rectEqualToRect(this._rect, cc.RectZero()))
1211+
if (!this._rect || cc.rectEqualToRect(this._rect, cc.RectZero())) {
12071212
this._rect = cc.rect(0, 0, texture.width, texture.height);
1213+
this._rect = cc.RECT_PIXELS_TO_POINTS(this._rect);
1214+
}
12081215
this._originalTexture = texture;
12091216
}
12101217
this._texture = texture;
@@ -1217,10 +1224,11 @@ cc.SpriteCanvas = cc.NodeRGBA.extend(/** @lends cc.SpriteCanvas# */{
12171224
if (cacheTextureForColor) {
12181225
this._colorized = true;
12191226
//generate color texture cache
1227+
var rect = cc.RECT_POINTS_TO_PIXELS(this._rect);
12201228
if (this._texture instanceof HTMLCanvasElement && !this._rectRotated)
1221-
cc.generateTintImage(this.getTexture(), cacheTextureForColor, this._displayedColor, this.getTextureRect(), this._texture);
1229+
cc.generateTintImage(this.getTexture(), cacheTextureForColor, this._color, rect, this._texture);
12221230
else {
1223-
var colorTexture = cc.generateTintImage(this.getTexture(), cacheTextureForColor, this._displayedColor, this.getTextureRect());
1231+
var colorTexture = cc.generateTintImage(this.getTexture(), cacheTextureForColor, this._color, rect);
12241232
this.setTexture(colorTexture);
12251233
}
12261234
}
@@ -1248,13 +1256,14 @@ cc.SpriteCanvas = cc.NodeRGBA.extend(/** @lends cc.SpriteCanvas# */{
12481256
context.scale(1, -1);
12491257
}
12501258
if (this._texture) {
1259+
var scaleFactor = cc.CONTENT_SCALE_FACTOR();
12511260
if (this._colorized) {
12521261
context.drawImage(this._texture,
1253-
0, 0, locRect.width, locRect.height,
1262+
0, 0, locRect.width * scaleFactor, locRect.height * scaleFactor,
12541263
flipXOffset, flipYOffset, locRect.width, locRect.height);
12551264
} else {
12561265
context.drawImage(this._texture,
1257-
locRect.x, locRect.y, locRect.width, locRect.height,
1266+
locRect.x * scaleFactor, locRect.y * scaleFactor, locRect.width * scaleFactor, locRect.height * scaleFactor,
12581267
flipXOffset, flipYOffset, locRect.width, locRect.height);
12591268
}
12601269
} else if (this._contentSize.width !== 0) {
@@ -2454,7 +2463,7 @@ cc.SpriteWebGL = cc.NodeRGBA.extend(/** @lends cc.SpriteWebGL# */{
24542463
return cc.SpriteFrame.createWithTexture(this._texture,
24552464
cc.RECT_POINTS_TO_PIXELS(this._rect),
24562465
this._rectRotated,
2457-
this._unflippedOffsetPositionFromCenter,
2466+
cc.POINT_POINTS_TO_PIXELS(this._unflippedOffsetPositionFromCenter),
24582467
cc.SIZE_POINTS_TO_PIXELS(this._contentSize));
24592468
},
24602469

cocos2d/sprite_nodes/CCSpriteFrame.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ cc.SpriteFrame = cc.Class.extend(/** @lends cc.SpriteFrame# */{
198198
*/
199199
setOffset:function (offsets) {
200200
this._offset = offsets;
201+
this._offsetInPixels = cc.POINT_POINTS_TO_PIXELS(offsetInPixels);
201202
},
202203

203204
clone: function(){

extensions/CCBReader/CCNodeLoader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ cc.NodeLoader = cc.Class.extend({
493493
if(texture instanceof cc.Texture2D){
494494
bounds = cc.RectMake(0, 0, texture.getContentSize().width, texture.getContentSize().height);
495495
}else{
496-
bounds = cc.RectMake(0, 0, texture.width, texture.height);
496+
bounds = cc.RECT_PIXELS_TO_POINTS(bounds);
497497
}
498498
spriteFrame = cc.SpriteFrame.createWithTexture(texture, bounds);
499499
} else {

extensions/GUI/CCControlExtension/CCScale9Sprite.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,7 @@ cc.Scale9Sprite = cc.Node.extend(/** @lends cc.Scale9Sprite# */{
457457
rect = cc.RectMake(0, 0, textureSize.width, textureSize.height);
458458
} else {
459459
rect = cc.RectMake(0, 0, selTexture.width, selTexture.height);
460+
rect = cc.RECT_PIXELS_TO_POINTS(rect);
460461
}
461462
}
462463

0 commit comments

Comments
 (0)