Skip to content

Commit b4d2d00

Browse files
author
Mark Henderson
committed
Fix handling of contentScaleFactor != 1
fixes for Sprite, scale9Sprite, and LabelTTF.
1 parent 8b51a1b commit b4d2d00

File tree

7 files changed

+45
-20
lines changed

7 files changed

+45
-20
lines changed

cocos2d/cocoa/CCGeometry.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
THE SOFTWARE.
2525
****************************************************************************/
2626

27+
/**
28+
* @namespace
29+
*/
30+
var cc = cc || {};
31+
2732
//--------------------------------------------------------
2833
//
2934
// POINT

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);
@@ -362,6 +362,7 @@ cc.LabelTTFCanvas = cc.Sprite.extend(/** @lends cc.LabelTTFCanvas# */{
362362
this._fontSize = textDefinition.fontSize;
363363
this._fontStyleStr = this._fontSize + "px '" + this._fontName + "'";
364364

365+
365366
// shadow
366367
if ( textDefinition.shadowEnabled)
367368
this.enableShadow(textDefinition.shadowOffset, textDefinition.shadowOpacity, textDefinition.shadowBlur, false);
@@ -598,8 +599,9 @@ cc.LabelTTFCanvas = cc.Sprite.extend(/** @lends cc.LabelTTFCanvas# */{
598599
}
599600

600601
var locVAlignment = this._vAlignment, locHAlignment = this._hAlignment,
601-
locContentSizeWidth = this._contentSize.width, locContentSizeHeight = this._contentSize.height;
602-
var locFontHeight = this._fontClientHeight;
602+
locContentSizeWidth = this._contentSize.width* cc.CONTENT_SCALE_FACTOR(),
603+
locContentSizeHeight = this._contentSize.height* cc.CONTENT_SCALE_FACTOR();
604+
var locFontHeight = this._fontClientHeight* cc.CONTENT_SCALE_FACTOR();
603605

604606
context.textBaseline = cc.LabelTTF._textBaseline[locVAlignment];
605607
context.textAlign = cc.LabelTTF._textAlign[locHAlignment];
@@ -702,6 +704,7 @@ cc.LabelTTFWebGL = cc.Sprite.extend(/** @lends cc.LabelTTFWebGL# */{
702704
_string:"",
703705
_isMultiLine:false,
704706
_fontStyleStr:null,
707+
_scaledFontStyleStr:null,
705708
_colorStyleStr:null,
706709

707710
// font shadow
@@ -734,6 +737,7 @@ cc.LabelTTFWebGL = cc.Sprite.extend(/** @lends cc.LabelTTFWebGL# */{
734737
this._vAlignment = cc.VERTICAL_TEXT_ALIGNMENT_TOP;
735738
this._opacityModifyRGB = false;
736739
this._fontStyleStr = "";
740+
this._scaledFontStyleStr = "";
737741
this._colorStyleStr = "";
738742
this._fontName = "Arial";
739743
this._opacity = 255;
@@ -868,8 +872,9 @@ cc.LabelTTFWebGL = cc.Sprite.extend(/** @lends cc.LabelTTFWebGL# */{
868872
this._fontName = fontName;
869873
this._hAlignment = hAlignment;
870874
this._vAlignment = vAlignment;
871-
this._fontSize = fontSize * cc.CONTENT_SCALE_FACTOR();
875+
this._fontSize = fontSize;
872876
this._fontStyleStr = this._fontSize + "px '" + this._fontName + "'";
877+
this._scaledFontStyleStr = this._fontSize * cc.CONTENT_SCALE_FACTOR() + "px '" + this._fontName + "'";
873878
this._fontClientHeight = cc.LabelTTF.__getFontHeightByDiv(this._fontName,this._fontSize);
874879
this.setString(strInfo);
875880
this._updateTexture();
@@ -1039,6 +1044,7 @@ cc.LabelTTFWebGL = cc.Sprite.extend(/** @lends cc.LabelTTFWebGL# */{
10391044
this._fontName = textDefinition.fontName;
10401045
this._fontSize = textDefinition.fontSize || 12;
10411046
this._fontStyleStr = this._fontSize + "px '" + this._fontName + "'";
1047+
this._scaledFontStyleStr = this._fontSize * cc.CONTENT_SCALE_FACTOR() + "px '" + this._fontName + "'";
10421048
this._fontClientHeight = cc.LabelTTF.__getFontHeightByDiv(this._fontName,this._fontSize);
10431049

10441050
// shadow
@@ -1059,6 +1065,7 @@ cc.LabelTTFWebGL = cc.Sprite.extend(/** @lends cc.LabelTTFWebGL# */{
10591065
_prepareTextDefinition:function(adjustForResolution){
10601066
var texDef = new cc.FontDefinition();
10611067

1068+
//Do these reference to CONTENT_SCALE_FACTOR need to be removed ?
10621069
if (adjustForResolution){
10631070
texDef.fontSize = this._fontSize * cc.CONTENT_SCALE_FACTOR();
10641071
texDef.fontDimensions = cc.SIZE_POINTS_TO_PIXELS(this._dimensions);
@@ -1160,6 +1167,7 @@ cc.LabelTTFWebGL = cc.Sprite.extend(/** @lends cc.LabelTTFWebGL# */{
11601167
if (this._fontSize != fontSize) {
11611168
this._fontSize = fontSize;
11621169
this._fontStyleStr = fontSize + "px '" + this._fontName + "'";
1170+
this._scaledFontStyleStr = this._fontSize * cc.CONTENT_SCALE_FACTOR() + "px '" + this._fontName + "'";
11631171
this._fontClientHeight = cc.LabelTTF.__getFontHeightByDiv(this._fontName,fontSize);
11641172
// Force update
11651173
this._needUpdateTexture = true;
@@ -1174,6 +1182,7 @@ cc.LabelTTFWebGL = cc.Sprite.extend(/** @lends cc.LabelTTFWebGL# */{
11741182
if (this._fontName && this._fontName != fontName) {
11751183
this._fontName = fontName;
11761184
this._fontStyleStr = this._fontSize + "px '" + fontName + "'";
1185+
this._scaledFontStyleStr = this._fontSize * cc.CONTENT_SCALE_FACTOR() + "px '" + this._fontName + "'";
11771186
this._fontClientHeight = cc.LabelTTF.__getFontHeightByDiv(fontName,this._fontSize);
11781187
// Force update
11791188
this._needUpdateTexture = true;
@@ -1189,8 +1198,8 @@ cc.LabelTTFWebGL = cc.Sprite.extend(/** @lends cc.LabelTTFWebGL# */{
11891198

11901199
context.setTransform(1, 0, 0, 1, 0, locContentSizeHeight);
11911200
//this is fillText for canvas
1192-
if (context.font != this._fontStyleStr)
1193-
context.font = this._fontStyleStr;
1201+
if (context.font != this._scaledFontStyleStr)
1202+
context.font = this._scaledFontStyleStr;
11941203
context.fillStyle = this._fillColorStr;
11951204

11961205
//stroke style setup
@@ -1388,8 +1397,8 @@ cc.LabelTTFWebGL = cc.Sprite.extend(/** @lends cc.LabelTTFWebGL# */{
13881397
this._labelContext.font = this._fontStyleStr;
13891398
this._updateTTF();
13901399
var width = this._contentSize.width, height = this._contentSize.height;
1391-
this._labelCanvas.width = width;
1392-
this._labelCanvas.height = height;
1400+
this._labelCanvas.width = width * cc.CONTENT_SCALE_FACTOR();
1401+
this._labelCanvas.height = height * cc.CONTENT_SCALE_FACTOR();;
13931402

13941403
//draw text to labelCanvas
13951404
this._drawTTFInCanvasForWebGL(this._labelContext);

cocos2d/platform/jsloader.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
****************************************************************************/
2727
(function () {
2828
var engine = [
29+
'cocoa/CCGeometry.js',
30+
'platform/Sys.js',
31+
'platform/CCConfig.js',
2932
'platform/CCClass.js',
3033
'platform/miniFramework.js',
3134
'platform/CCCommon.js',
@@ -38,9 +41,6 @@
3841
'platform/CCAccelerometer.js',
3942
'platform/zlib.min.js',
4043
'platform/CCEGLView.js',
41-
'cocoa/CCGeometry.js',
42-
'platform/Sys.js',
43-
'platform/CCConfig.js',
4444
'platform/CCImage.js',
4545
'kazmath/utility.js',
4646
'kazmath/vec2.js',

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
}
@@ -1238,6 +1246,7 @@ cc.SpriteCanvas = cc.NodeRGBA.extend(/** @lends cc.SpriteCanvas# */{
12381246

12391247
context.globalAlpha = this._realOpacity / 255;
12401248
var locRect = this._rect;
1249+
var scaledRect = cc.RECT_POINTS_TO_PIXELS(locRect);
12411250
var flipXOffset = 0 | (this._offsetPosition.x), flipYOffset = -this._offsetPosition.y - locRect.height;
12421251
if (this._flipX) {
12431252
flipXOffset = -this._offsetPosition.x - locRect.width;
@@ -1250,11 +1259,11 @@ cc.SpriteCanvas = cc.NodeRGBA.extend(/** @lends cc.SpriteCanvas# */{
12501259
if (this._texture) {
12511260
if (this._colorized) {
12521261
context.drawImage(this._texture,
1253-
0, 0, locRect.width, locRect.height,
1262+
0, 0, scaledRect.width, scaledRect.height,
12541263
flipXOffset, flipYOffset, locRect.width, locRect.height);
12551264
} else {
12561265
context.drawImage(this._texture,
1257-
locRect.x, locRect.y, locRect.width, locRect.height,
1266+
scaledRect.x, scaledRect.y, scaledRect.width, scaledRect.height,
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)