Skip to content

Fix handling of contentScaleFactor != 1 #1037

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 29, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions cocos2d/label_nodes/CCLabelTTF.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ cc.LabelTTFCanvas = cc.Sprite.extend(/** @lends cc.LabelTTFCanvas# */{
this._fontName = fontName;
this._hAlignment = hAlignment;
this._vAlignment = vAlignment;
this._fontSize = fontSize * cc.CONTENT_SCALE_FACTOR();
this._fontSize = fontSize;
this._fontStyleStr = this._fontSize + "px '" + this._fontName + "'";
this._fontClientHeight = cc.LabelTTF.__getFontHeightByDiv(this._fontName,this._fontSize);
this.setString(strInfo);
Expand Down Expand Up @@ -333,6 +333,7 @@ cc.LabelTTFCanvas = cc.Sprite.extend(/** @lends cc.LabelTTFCanvas# */{
this._fontSize = textDefinition.fontSize;
this._fontStyleStr = this._fontSize + "px '" + this._fontName + "'";


// shadow
if ( textDefinition.shadowEnabled)
this.enableShadow(textDefinition.shadowOffset, textDefinition.shadowOpacity, textDefinition.shadowBlur, false);
Expand Down Expand Up @@ -566,8 +567,9 @@ cc.LabelTTFCanvas = cc.Sprite.extend(/** @lends cc.LabelTTFCanvas# */{
}

var locVAlignment = this._vAlignment, locHAlignment = this._hAlignment,
locContentSizeWidth = this._contentSize.width, locContentSizeHeight = this._contentSize.height;
var locFontHeight = this._fontClientHeight;
locContentSizeWidth = this._contentSize.width* cc.CONTENT_SCALE_FACTOR(),
locContentSizeHeight = this._contentSize.height* cc.CONTENT_SCALE_FACTOR();
var locFontHeight = this._fontClientHeight* cc.CONTENT_SCALE_FACTOR();

context.textBaseline = cc.LabelTTF._textBaseline[locVAlignment];
context.textAlign = cc.LabelTTF._textAlign[locHAlignment];
Expand Down Expand Up @@ -670,6 +672,7 @@ cc.LabelTTFWebGL = cc.Sprite.extend(/** @lends cc.LabelTTFWebGL# */{
_string:"",
_isMultiLine:false,
_fontStyleStr:null,
_scaledFontStyleStr:null,
_colorStyleStr:null,

// font shadow
Expand Down Expand Up @@ -702,6 +705,7 @@ cc.LabelTTFWebGL = cc.Sprite.extend(/** @lends cc.LabelTTFWebGL# */{
this._vAlignment = cc.VERTICAL_TEXT_ALIGNMENT_TOP;
this._opacityModifyRGB = false;
this._fontStyleStr = "";
this._scaledFontStyleStr = "";
this._colorStyleStr = "";
this._fontName = "Arial";
this._opacity = 255;
Expand Down Expand Up @@ -836,8 +840,9 @@ cc.LabelTTFWebGL = cc.Sprite.extend(/** @lends cc.LabelTTFWebGL# */{
this._fontName = fontName;
this._hAlignment = hAlignment;
this._vAlignment = vAlignment;
this._fontSize = fontSize * cc.CONTENT_SCALE_FACTOR();
this._fontSize = fontSize;
this._fontStyleStr = this._fontSize + "px '" + this._fontName + "'";
this._scaledFontStyleStr = this._fontSize * cc.CONTENT_SCALE_FACTOR() + "px '" + this._fontName + "'";
this._fontClientHeight = cc.LabelTTF.__getFontHeightByDiv(this._fontName,this._fontSize);
this.setString(strInfo);
this._updateTexture();
Expand Down Expand Up @@ -986,6 +991,7 @@ cc.LabelTTFWebGL = cc.Sprite.extend(/** @lends cc.LabelTTFWebGL# */{
this._fontName = textDefinition.fontName;
this._fontSize = textDefinition.fontSize || 12;
this._fontStyleStr = this._fontSize + "px '" + this._fontName + "'";
this._scaledFontStyleStr = this._fontSize * cc.CONTENT_SCALE_FACTOR() + "px '" + this._fontName + "'";
this._fontClientHeight = cc.LabelTTF.__getFontHeightByDiv(this._fontName,this._fontSize);

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

//Do these reference to CONTENT_SCALE_FACTOR need to be removed ?
if (adjustForResolution){
texDef.fontSize = this._fontSize * cc.CONTENT_SCALE_FACTOR();
texDef.fontDimensions = cc.SIZE_POINTS_TO_PIXELS(this._dimensions);
Expand Down Expand Up @@ -1107,6 +1114,7 @@ cc.LabelTTFWebGL = cc.Sprite.extend(/** @lends cc.LabelTTFWebGL# */{
if (this._fontSize != fontSize) {
this._fontSize = fontSize;
this._fontStyleStr = fontSize + "px '" + this._fontName + "'";
this._scaledFontStyleStr = this._fontSize * cc.CONTENT_SCALE_FACTOR() + "px '" + this._fontName + "'";
this._fontClientHeight = cc.LabelTTF.__getFontHeightByDiv(this._fontName,fontSize);
// Force update
this._needUpdateTexture = true;
Expand All @@ -1121,6 +1129,7 @@ cc.LabelTTFWebGL = cc.Sprite.extend(/** @lends cc.LabelTTFWebGL# */{
if (this._fontName && this._fontName != fontName) {
this._fontName = fontName;
this._fontStyleStr = this._fontSize + "px '" + fontName + "'";
this._scaledFontStyleStr = this._fontSize * cc.CONTENT_SCALE_FACTOR() + "px '" + this._fontName + "'";
this._fontClientHeight = cc.LabelTTF.__getFontHeightByDiv(fontName,this._fontSize);
// Force update
this._needUpdateTexture = true;
Expand All @@ -1136,8 +1145,8 @@ cc.LabelTTFWebGL = cc.Sprite.extend(/** @lends cc.LabelTTFWebGL# */{

context.setTransform(1, 0, 0, 1, 0, locContentSizeHeight);
//this is fillText for canvas
if (context.font != this._fontStyleStr)
context.font = this._fontStyleStr;
if (context.font != this._scaledFontStyleStr)
context.font = this._scaledFontStyleStr;
context.fillStyle = this._fillColorStr;

//stroke style setup
Expand Down Expand Up @@ -1335,8 +1344,8 @@ cc.LabelTTFWebGL = cc.Sprite.extend(/** @lends cc.LabelTTFWebGL# */{
this._labelContext.font = this._fontStyleStr;
this._updateTTF();
var width = this._contentSize.width, height = this._contentSize.height;
this._labelCanvas.width = width;
this._labelCanvas.height = height;
this._labelCanvas.width = width * cc.CONTENT_SCALE_FACTOR();
this._labelCanvas.height = height * cc.CONTENT_SCALE_FACTOR();;

//draw text to labelCanvas
this._drawTTFInCanvasForWebGL(this._labelContext);
Expand Down
2 changes: 1 addition & 1 deletion cocos2d/platform/jsloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@
'../extensions/PluginX/plugins/SocialQQWeibo.js',
'../extensions/PluginX/plugins/SocialQzone.js',
'../extensions/PluginX/plugins/SocialTwitter.js',
'../extensions/PluginX/plugins/SocialFacebook.js',
'../extensions/PluginX/plugins/SocialFacebook.js'
//'../extensions/PluginX/plugins/AdsGoogle.js'
]);
}
Expand Down
25 changes: 17 additions & 8 deletions cocos2d/sprite_nodes/CCSprite.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ cc.generateTextureCacheForColor.tempCtx = cc.generateTextureCacheForColor.tempCa
cc.generateTintImage2 = function (texture, color, rect) {
if (!rect) {
rect = cc.rect(0, 0, texture.width, texture.height);
rect = cc.RECT_PIXELS_TO_POINTS(rect);
}
var selColor;
if (color instanceof cc.Color4F) {
Expand Down Expand Up @@ -933,6 +934,7 @@ cc.SpriteCanvas = cc.NodeRGBA.extend(/** @lends cc.SpriteCanvas# */{
loadImg.addEventListener("load", function () {
if (!rect) {
rect = cc.rect(0, 0, loadImg.width, loadImg.height);
rect = cc.RECT_PIXELS_TO_POINTS(rect);
}
selfPointer.initWithTexture(loadImg, rect);
cc.TextureCache.getInstance().cacheImage(filename, loadImg);
Expand All @@ -947,6 +949,7 @@ cc.SpriteCanvas = cc.NodeRGBA.extend(/** @lends cc.SpriteCanvas# */{
if (texture) {
if (!rect) {
rect = cc.rect(0, 0, texture.width, texture.height);
rect = cc.RECT_PIXELS_TO_POINTS(rect);
}
return this.initWithTexture(texture, rect);
}
Expand Down Expand Up @@ -996,8 +999,10 @@ cc.SpriteCanvas = cc.NodeRGBA.extend(/** @lends cc.SpriteCanvas# */{

if (!rect) {
rect = cc.rect(0, 0, 0, 0);
if ((texture instanceof HTMLImageElement) || (texture instanceof HTMLCanvasElement))
if ((texture instanceof HTMLImageElement) || (texture instanceof HTMLCanvasElement)) {
rect.size = cc.size(texture.width, texture.height);
rect = cc.RECT_PIXELS_TO_POINTS(rect);
}
}
this._originalTexture = texture;

Expand Down Expand Up @@ -1163,7 +1168,7 @@ cc.SpriteCanvas = cc.NodeRGBA.extend(/** @lends cc.SpriteCanvas# */{
return cc.SpriteFrame._frameWithTextureForCanvas(this._texture,
cc.RECT_POINTS_TO_PIXELS(this._rect),
this._rectRotated,
this._unflippedOffsetPositionFromCenter,
cc.POINT_POINTS_TO_PIXELS(this._unflippedOffsetPositionFromCenter),
cc.SIZE_POINTS_TO_PIXELS(this._contentSize));
},

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

if (this._texture != texture) {
if (texture instanceof HTMLImageElement) {
if (!this._rect || cc.rectEqualToRect(this._rect, cc.RectZero()))
if (!this._rect || cc.rectEqualToRect(this._rect, cc.RectZero())) {
this._rect = cc.rect(0, 0, texture.width, texture.height);
this._rect = cc.RECT_PIXELS_TO_POINTS(this._rect);
}
this._originalTexture = texture;
}
this._texture = texture;
Expand All @@ -1217,10 +1224,11 @@ cc.SpriteCanvas = cc.NodeRGBA.extend(/** @lends cc.SpriteCanvas# */{
if (cacheTextureForColor) {
this._colorized = true;
//generate color texture cache
var rect = cc.RECT_POINTS_TO_PIXELS(this._rect);
if (this._texture instanceof HTMLCanvasElement && !this._rectRotated)
cc.generateTintImage(this.getTexture(), cacheTextureForColor, this._displayedColor, this.getTextureRect(), this._texture);
cc.generateTintImage(this.getTexture(), cacheTextureForColor, this._color, rect, this._texture);
else {
var colorTexture = cc.generateTintImage(this.getTexture(), cacheTextureForColor, this._displayedColor, this.getTextureRect());
var colorTexture = cc.generateTintImage(this.getTexture(), cacheTextureForColor, this._color, rect);
this.setTexture(colorTexture);
}
}
Expand Down Expand Up @@ -1248,13 +1256,14 @@ cc.SpriteCanvas = cc.NodeRGBA.extend(/** @lends cc.SpriteCanvas# */{
context.scale(1, -1);
}
if (this._texture) {
var scaleFactor = cc.CONTENT_SCALE_FACTOR();
if (this._colorized) {
context.drawImage(this._texture,
0, 0, locRect.width, locRect.height,
0, 0, locRect.width * scaleFactor, locRect.height * scaleFactor,
flipXOffset, flipYOffset, locRect.width, locRect.height);
} else {
context.drawImage(this._texture,
locRect.x, locRect.y, locRect.width, locRect.height,
locRect.x * scaleFactor, locRect.y * scaleFactor, locRect.width * scaleFactor, locRect.height * scaleFactor,
flipXOffset, flipYOffset, locRect.width, locRect.height);
}
} else if (this._contentSize.width !== 0) {
Expand Down Expand Up @@ -2454,7 +2463,7 @@ cc.SpriteWebGL = cc.NodeRGBA.extend(/** @lends cc.SpriteWebGL# */{
return cc.SpriteFrame.createWithTexture(this._texture,
cc.RECT_POINTS_TO_PIXELS(this._rect),
this._rectRotated,
this._unflippedOffsetPositionFromCenter,
cc.POINT_POINTS_TO_PIXELS(this._unflippedOffsetPositionFromCenter),
cc.SIZE_POINTS_TO_PIXELS(this._contentSize));
},

Expand Down
1 change: 1 addition & 0 deletions cocos2d/sprite_nodes/CCSpriteFrame.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ cc.SpriteFrame = cc.Class.extend(/** @lends cc.SpriteFrame# */{
*/
setOffset:function (offsets) {
this._offset = offsets;
this._offsetInPixels = cc.POINT_POINTS_TO_PIXELS(offsetInPixels);
},

clone: function(){
Expand Down
2 changes: 1 addition & 1 deletion extensions/CCBReader/CCNodeLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ cc.NodeLoader = cc.Class.extend({
if(texture instanceof cc.Texture2D){
bounds = cc.RectMake(0, 0, texture.getContentSize().width, texture.getContentSize().height);
}else{
bounds = cc.RectMake(0, 0, texture.width, texture.height);
bounds = cc.RECT_PIXELS_TO_POINTS(bounds);
}
spriteFrame = cc.SpriteFrame.createWithTexture(texture, bounds);
} else {
Expand Down
1 change: 1 addition & 0 deletions extensions/GUI/CCControlExtension/CCScale9Sprite.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@ cc.Scale9Sprite = cc.Node.extend(/** @lends cc.Scale9Sprite# */{
rect = cc.RectMake(0, 0, textureSize.width, textureSize.height);
} else {
rect = cc.RectMake(0, 0, selTexture.width, selTexture.height);
rect = cc.RECT_PIXELS_TO_POINTS(rect);
}
}

Expand Down