diff --git a/cocos2d/labels/CCLabelAtlas.js b/cocos2d/labels/CCLabelAtlas.js index 2b8c06178f..6f2bce2e29 100644 --- a/cocos2d/labels/CCLabelAtlas.js +++ b/cocos2d/labels/CCLabelAtlas.js @@ -27,7 +27,7 @@ /** * using image file to print text label on the screen, might be a bit slower than cc.Label, similar to cc.LabelBMFont * @class - * @extends cc.AtlasNode + * @extends cc.LabelBMFont * * @property {String} string - Content string of label * @@ -43,14 +43,7 @@ * //creates the cc.LabelAtlas with a string, a fnt file * var myLabel = new cc.LabelAtlas('Text to display', 'CharMapFile.plist‘); */ -cc.LabelAtlas = cc.AtlasNode.extend(/** @lends cc.LabelAtlas# */{ - //property String is Getter and Setter - // string to render - _string: null, - // the first char in the charmap - _mapStartChar: null, - - _textureLoaded: false, +cc.LabelAtlas = cc.LabelBMFont.extend(/** @lends cc.LabelBMFont# */{ _className: "LabelAtlas", /** @@ -68,35 +61,47 @@ cc.LabelAtlas = cc.AtlasNode.extend(/** @lends cc.LabelAtlas# */{ * @param {Number} [startCharMap=""] */ ctor: function (strText, charMapFile, itemWidth, itemHeight, startCharMap) { - cc.AtlasNode.prototype.ctor.call(this); + cc.SpriteBatchNode.prototype.ctor.call(this); + this._imageOffset = cc.p(0, 0); + this._cascadeColorEnabled = true; + this._cascadeOpacityEnabled = true; - this._renderCmd.setCascade(); charMapFile && cc.LabelAtlas.prototype.initWithString.call(this, strText, charMapFile, itemWidth, itemHeight, startCharMap); }, - _createRenderCmd: function(){ - if(cc._renderType === cc.game.RENDER_TYPE_WEBGL) - return new cc.LabelAtlas.WebGLRenderCmd(this); + _createRenderCmd: function () { + if (cc._renderType === cc.game.RENDER_TYPE_WEBGL) + return new cc.LabelBMFont.WebGLRenderCmd(this); else - return new cc.LabelAtlas.CanvasRenderCmd(this); + return new cc.LabelBMFont.CanvasRenderCmd(this); }, - /** - * Return texture is loaded. - * @returns {boolean} - */ - textureLoaded: function () { - return this._textureLoaded; - }, + _createFntConfig: function (texture, itemWidth, itemHeight, startCharMap) { + var fnt = {}; + fnt.commonHeight = itemHeight; + + var fontDefDictionary = fnt.fontDefDictionary = {}; + + var textureWidth = texture.pixelsWidth; + var textureHeight = texture.pixelsHeight; + + var startCharCode = startCharMap.charCodeAt(0); + var i = 0; + for (var col = itemHeight; col <= textureHeight; col += itemHeight) { + for (var row = 0; row < textureWidth; row += itemWidth) { + fontDefDictionary[startCharCode+i] = { + rect: {x: row, y: col - itemHeight, width:itemWidth, height: itemHeight }, + xOffset: 0, + yOffset: 0, + xAdvance: itemWidth + }; + ++i; + } + } - /** - * Add texture loaded event listener. - * @param {Function} callback - * @param {cc.Node} target - * @deprecated since 3.1, please use addEventListener instead - */ - addLoadedEventListener: function (callback, target) { - this.addEventListener("load", callback, target); + fnt.kerningDict = {}; + + return fnt; }, /** @@ -116,6 +121,10 @@ cc.LabelAtlas = cc.AtlasNode.extend(/** @lends cc.LabelAtlas# */{ */ initWithString: function (strText, charMapFile, itemWidth, itemHeight, startCharMap) { var label = strText + "", textureFilename, width, height, startChar; + var self = this, theString = label || ""; + this._initialString = theString; + self._string = theString; + if (itemWidth === undefined) { var dict = cc.loader.getRes(charMapFile); if (parseInt(dict["version"], 10) !== 1) { @@ -135,88 +144,60 @@ cc.LabelAtlas = cc.AtlasNode.extend(/** @lends cc.LabelAtlas# */{ startChar = startCharMap || " "; } - var texture = null; - if (textureFilename instanceof cc.Texture2D) - texture = textureFilename; - else - texture = cc.textureCache.addImage(textureFilename); - var locLoaded = texture.isLoaded(); - this._textureLoaded = locLoaded; - if (!locLoaded) { - this._string = label; - texture.addEventListener("load", function (sender) { - this.initWithTexture(texture, width, height, label.length); - this.string = this._string; - this.setColor(this._renderCmd._displayedColor); - this.dispatchEvent("load"); - }, this); - } - if (this.initWithTexture(texture, width, height, label.length)) { - this._mapStartChar = startChar; - this.string = label; - return true; + var texture; + if (charMapFile) { + self._fntFile = "dummy_fnt_file:" + textureFilename; + var spriteFrameBaseName = textureFilename; + var spriteFrame = cc.spriteFrameCache.getSpriteFrame(spriteFrameBaseName) || cc.spriteFrameCache.getSpriteFrame(cc.path.basename(spriteFrameBaseName)); + if(spriteFrame) { + texture = spriteFrame.getTexture(); + this._spriteFrame = spriteFrame; + } else { + texture = cc.textureCache.addImage(textureFilename); + } + + var newConf = this._createFntConfig(texture, width, height, startChar); + newConf.atlasName = textureFilename; + self._config = newConf; + + var locIsLoaded = texture.isLoaded(); + self._textureLoaded = locIsLoaded; + if (!locIsLoaded) { + texture.addEventListener("load", function () { + var self1 = this; + self1._textureLoaded = true; + //reset the LabelBMFont + self1.setString(self1._initialString, true); + self1.dispatchEvent("load"); + }, self); + } + } else { + texture = new cc.Texture2D(); + var image = new Image(); + texture.initWithElement(image); + self._textureLoaded = false; } - return false; - }, + this._texture = texture; - /** - * Set the color. - * @param {cc.Color} color3 - */ - setColor: function (color3) { - cc.AtlasNode.prototype.setColor.call(this, color3); - this._renderCmd.updateAtlasValues(); - }, + self._alignment = cc.TEXT_ALIGNMENT_LEFT; + self._imageOffset = cc.p(0, 0); + self._width = -1; - /** - * return the text of this label - * @return {String} - */ - getString: function () { - return this._string; - }, + self._realOpacity = 255; + self._realColor = cc.color(255, 255, 255, 255); - addChild: function(child, localZOrder, tag){ - this._renderCmd._addChild(child); - cc.Node.prototype.addChild.call(this, child, localZOrder, tag); - }, + self._contentSize.width = 0; + self._contentSize.height = 0; - /** - * Atlas generation - * @function - */ - updateAtlasValues: function(){ - this._renderCmd.updateAtlasValues(); + self.setString(theString, true); + return true; }, - /** - * set the display string - * @function - * @param {String} label - */ - setString: function(label){ - label = String(label); - var len = label.length; - this._string = label; - this.setContentSize(len * this._itemWidth, this._itemHeight); - this._renderCmd.setString(label); - - this._renderCmd.updateAtlasValues(); - this.quadsToDraw = len; + setFntFile: function () { + cc.warn("setFntFile doesn't support with LabelAtlas."); } -}); -(function(){ - var proto = cc.LabelAtlas.prototype; - // Override properties - cc.defineGetterSetter(proto, "opacity", proto.getOpacity, proto.setOpacity); - cc.defineGetterSetter(proto, "color", proto.getColor, proto.setColor); - - // Extended properties - /** @expose */ - proto.string; - cc.defineGetterSetter(proto, "string", proto.getString, proto.setString); -})(); +}); /** *

diff --git a/cocos2d/labels/CCLabelBMFont.js b/cocos2d/labels/CCLabelBMFont.js index 7c261a14b7..26a5154db4 100644 --- a/cocos2d/labels/CCLabelBMFont.js +++ b/cocos2d/labels/CCLabelBMFont.js @@ -80,7 +80,7 @@ * // Example 03 * var label3 = new cc.LabelBMFont("This is a \n test case", "test.fnt", 200, cc.TEXT_ALIGNMENT_LEFT, cc.p(0,0)); */ -cc.LabelBMFont = cc.SpriteBatchNode.extend(/** @lends cc.LabelBMFont# */{ +cc.LabelBMFont = cc.Node.extend(/** @lends cc.LabelBMFont# */{ //property string is Getter and Setter. //property textAlign is Getter and Setter. //property boundingWidth is Getter and Setter. @@ -119,6 +119,7 @@ cc.LabelBMFont = cc.SpriteBatchNode.extend(/** @lends cc.LabelBMFont# */{ } else { this._initialString = newString; } + var locChildren = this._children; if (locChildren) { for (var i = 0; i < locChildren.length; i++) { @@ -226,13 +227,23 @@ cc.LabelBMFont = cc.SpriteBatchNode.extend(/** @lends cc.LabelBMFont# */{ if (fntFile) { var newConf = cc.loader.getRes(fntFile); if (!newConf) { - cc.log("cc.LabelBMFont.initWithString(): Impossible to create font. Please check file"); - return false; + newConf = cc.FntFrameCache[fntFile] || cc.FntFrameCache[cc.path.basename(fntFile)]; + if(!newConf) { + cc.log("cc.LabelBMFont.initWithString(): Impossible to create font. Please check file"); + return false; + } } self._config = newConf; self._fntFile = fntFile; - texture = cc.textureCache.addImage(newConf.atlasName); + var spriteFrameBaseName = newConf.atlasName; + var spriteFrame = cc.spriteFrameCache.getSpriteFrame(spriteFrameBaseName) || cc.spriteFrameCache.getSpriteFrame(cc.path.basename(spriteFrameBaseName)); + if(spriteFrame) { + texture = spriteFrame.getTexture(); + this._spriteFrame = spriteFrame; + } else { + texture = cc.textureCache.addImage(newConf.atlasName); + } var locIsLoaded = texture.isLoaded(); self._textureLoaded = locIsLoaded; if (!locIsLoaded) { @@ -240,7 +251,6 @@ cc.LabelBMFont = cc.SpriteBatchNode.extend(/** @lends cc.LabelBMFont# */{ var self1 = this; self1._textureLoaded = true; //reset the LabelBMFont - self1.initWithTexture(sender, self1._initialString.length); self1.setString(self1._initialString, true); self1.dispatchEvent("load"); }, self); @@ -251,24 +261,22 @@ cc.LabelBMFont = cc.SpriteBatchNode.extend(/** @lends cc.LabelBMFont# */{ texture.initWithElement(image); self._textureLoaded = false; } + this._texture = texture; - if (self.initWithTexture(texture, theString.length)) { - self._alignment = alignment || cc.TEXT_ALIGNMENT_LEFT; - self._imageOffset = imageOffset || cc.p(0, 0); - self._width = (width === undefined) ? -1 : width; + self._alignment = alignment || cc.TEXT_ALIGNMENT_LEFT; + self._imageOffset = imageOffset || cc.p(0, 0); + self._width = (width === undefined) ? -1 : width; - self._realOpacity = 255; - self._realColor = cc.color(255, 255, 255, 255); + self._realOpacity = 255; + self._realColor = cc.color(255, 255, 255, 255); - self._contentSize.width = 0; - self._contentSize.height = 0; + self._contentSize.width = 0; + self._contentSize.height = 0; - self.setAnchorPoint(0.5, 0.5); + self.setAnchorPoint(0.5, 0.5); - self.setString(theString, true); - return true; - } - return false; + self.setString(theString, true); + return true; }, /** @@ -336,15 +344,30 @@ cc.LabelBMFont = cc.SpriteBatchNode.extend(/** @lends cc.LabelBMFont# */{ rect.x += self._imageOffset.x; rect.y += self._imageOffset.y; + var isRotated = false; + if(this._spriteFrame) { + var textureWidth = locTexture.width; + var spriteFrameRect = this._spriteFrame._rect; + if(!this._spriteFrame._rotated) { + rect.x = rect.x + spriteFrameRect.x; + rect.y = rect.y + spriteFrameRect.y; + } else { + isRotated = true; + var originalX = rect.x; + rect.x = rect.y + spriteFrameRect.x; + rect.y = originalX + spriteFrameRect.y; + } + } + var fontChar = self.getChildByTag(i); if (!fontChar) { fontChar = new cc.Sprite(); - fontChar.initWithTexture(locTexture, rect, false); + fontChar.initWithTexture(locTexture, rect, isRotated); fontChar._newTextureWhenChangeColor = true; this.addChild(fontChar, 0, i); } else { - cmd._updateCharTexture(fontChar, rect, key); + cmd._updateCharTexture(fontChar, rect, key, isRotated); } // Apply label properties @@ -542,6 +565,7 @@ cc.LabelBMFont = cc.SpriteBatchNode.extend(/** @lends cc.LabelBMFont# */{ updateLabel: function () { var self = this; self.string = self._initialString; + var i, j, characterSprite; // process string // Step 1: Make multiline @@ -564,7 +588,6 @@ cc.LabelBMFont = cc.SpriteBatchNode.extend(/** @lends cc.LabelBMFont# */{ wrapString = wrapString + String.fromCharCode(0); self._setString(wrapString, false); } - // Step 2: Make alignment if (self._alignment !== cc.TEXT_ALIGNMENT_LEFT) { i = 0; @@ -797,9 +820,9 @@ cc.LabelBMFont = cc.SpriteBatchNode.extend(/** @lends cc.LabelBMFont# */{ //Checking whether the character is a whitespace _isspace_unicode: function (ch) { ch = ch.charCodeAt(0); - return ((ch >= 9 && ch <= 13) || ch === 32 || ch === 133 || ch === 160 || ch === 5760 - || (ch >= 8192 && ch <= 8202) || ch === 8232 || ch === 8233 || ch === 8239 - || ch === 8287 || ch === 12288) + return ((ch >= 9 && ch <= 13) || ch === 32 || ch === 133 || ch === 160 || ch === 5760 + || (ch >= 8192 && ch <= 8202) || ch === 8232 || ch === 8233 || ch === 8239 + || ch === 8287 || ch === 12288); }, _utf8_trim_ws: function (str) { @@ -866,7 +889,11 @@ cc.LabelBMFont.create = function (str, fntFile, width, alignment, imageOffset) { return new cc.LabelBMFont(str, fntFile, width, alignment, imageOffset); }; +cc.FntFrameCache = {}; + var _fntLoader = { + FNT_HEAD: /fntframes [^\n]*(\n|$)/gi, + FNT_FRAME_NAME: /fntframe [^\n]*(\n|$)/gi, INFO_EXP: /info [^\n]*(\n|$)/gi, COMMON_EXP: /common [^\n]*(\n|$)/gi, PAGE_EXP: /page [^\n]*(\n|$)/gi, @@ -892,24 +919,8 @@ var _fntLoader = { return obj; }, - /** - * Parse Fnt string. - * @param fntStr - * @param url - * @returns {{}} - */ - parseFnt: function (fntStr, url) { - var self = this, fnt = {}; - //padding - var infoObj = self._parseStrToObj(fntStr.match(self.INFO_EXP)[0]); - var paddingArr = infoObj["padding"].split(","); - var padding = { - left: parseInt(paddingArr[0]), - top: parseInt(paddingArr[1]), - right: parseInt(paddingArr[2]), - bottom: parseInt(paddingArr[3]) - }; - + _parseFntContent: function (fnt, fntStr, url, useAtlas) { + var self = this; //common var commonObj = self._parseStrToObj(fntStr.match(self.COMMON_EXP)[0]); fnt.commonHeight = commonObj["lineHeight"]; @@ -923,7 +934,11 @@ var _fntLoader = { //page var pageObj = self._parseStrToObj(fntStr.match(self.PAGE_EXP)[0]); if (pageObj["id"] !== 0) cc.log("cc.LabelBMFont._parseImageFileName() : file could not be found"); - fnt.atlasName = cc.path.changeBasename(url, pageObj["file"]); + if(!useAtlas) { + fnt.atlasName = cc.path.changeBasename(url, pageObj["file"]); + } else { + fnt.atlasName = cc.path.join(cc.path.dirname(useAtlas.path) + pageObj["file"]); + } //char var charLines = fntStr.match(self.CHAR_EXP); @@ -948,6 +963,40 @@ var _fntLoader = { kerningDict[(kerningObj["first"] << 16) | (kerningObj["second"] & 0xffff)] = kerningObj["amount"]; } } + + return fnt; + }, + + /** + * Parse Fnt string. + * @param fntStr + * @param url + * @returns {{}} + */ + parseFnt: function (fntStr, url) { + var self = this, fnt = {}; + var headString = fntStr.match(self.FNT_HEAD); + if(headString) { + var headObj = self._parseStrToObj(headString[0]); + if(headObj && headObj.count) { + fntStr = fntStr.substr(headString[0].length); + var fntFrames = fntStr.split("----"); + for(var i = 0; i < headObj.count; ++i) { + var contentString = fntFrames[i]; + var frameNameStr = contentString.match(self.FNT_FRAME_NAME); + if(frameNameStr) { + var frameName = self._parseStrToObj(frameNameStr[0]); + if(frameName && frameName.name) { + fnt = {}; + var realFntPathKey = cc.path.join(cc.path.dirname(url), frameName.name); + cc.FntFrameCache[realFntPathKey] = this._parseFntContent(fnt, contentString.substr(frameNameStr[0].length), url, {path: frameName.name}); + } + } + } + } + } else { + fnt = this._parseFntContent(fnt, fntStr, url); + } return fnt; }, diff --git a/cocos2d/labels/CCLabelBMFontWebGLRenderCmd.js b/cocos2d/labels/CCLabelBMFontWebGLRenderCmd.js index 7e468e37d7..09e18a5f6b 100644 --- a/cocos2d/labels/CCLabelBMFontWebGLRenderCmd.js +++ b/cocos2d/labels/CCLabelBMFontWebGLRenderCmd.js @@ -42,9 +42,9 @@ this._node.setOpacityModifyRGB(this._node._texture.hasPremultipliedAlpha()); }; - proto._updateCharTexture = function (fontChar, rect, key) { + proto._updateCharTexture = function(fontChar, rect, key, isRotated){ // updating previous sprite - fontChar.setTextureRect(rect, false); + fontChar.setTextureRect(rect, isRotated); // restore to default in case they were modified fontChar.visible = true; }; diff --git a/extensions/ccui/uiwidgets/UIButton.js b/extensions/ccui/uiwidgets/UIButton.js index fc65d2e6dd..348177fee5 100644 --- a/extensions/ccui/uiwidgets/UIButton.js +++ b/extensions/ccui/uiwidgets/UIButton.js @@ -1,6 +1,7 @@ /**************************************************************************** Copyright (c) 2011-2012 cocos2d-x.org Copyright (c) 2013-2014 Chukong Technologies Inc. + Copyright (c) 2015-2016 zilongshanren http://www.cocos2d-x.org @@ -36,9 +37,10 @@ * @property {Boolean} pressedActionEnabled - Indicate whether button has zoom effect when clicked */ ccui.Button = ccui.Widget.extend(/** @lends ccui.Button# */{ - _buttonNormalRenderer: null, - _buttonClickedRenderer: null, - _buttonDisableRenderer: null, + _buttonScale9Renderer: null, + _buttonNormalSpriteFrame: null, + _buttonClickedSpriteFrame: null, + _buttonDisableSpriteFrame: null, _titleRenderer: null, _normalFileName: "", @@ -49,23 +51,15 @@ ccui.Button = ccui.Widget.extend(/** @lends ccui.Button# */{ _scale9Enabled: false, _capInsetsNormal: null, - _capInsetsPressed: null, - _capInsetsDisabled: null, _normalTexType: ccui.Widget.LOCAL_TEXTURE, _pressedTexType: ccui.Widget.LOCAL_TEXTURE, _disabledTexType: ccui.Widget.LOCAL_TEXTURE, _normalTextureSize: null, - _pressedTextureSize: null, - _disabledTextureSize: null, pressedActionEnabled: false, _titleColor: null, - _normalTextureScaleXInSize: 1, - _normalTextureScaleYInSize: 1, - _pressedTextureScaleXInSize: 1, - _pressedTextureScaleYInSize: 1, _zoomScale: 0.1, @@ -75,8 +69,6 @@ ccui.Button = ccui.Widget.extend(/** @lends ccui.Button# */{ _className: "Button", _normalTextureAdaptDirty: true, - _pressedTextureAdaptDirty: true, - _disabledTextureAdaptDirty: true, _fontName: "Thonburi", _fontSize: 12, @@ -95,33 +87,35 @@ ccui.Button = ccui.Widget.extend(/** @lends ccui.Button# */{ */ ctor: function (normalImage, selectedImage, disableImage, texType) { this._capInsetsNormal = cc.rect(0, 0, 0, 0); - this._capInsetsPressed = cc.rect(0, 0, 0, 0); - this._capInsetsDisabled = cc.rect(0, 0, 0, 0); this._normalTextureSize = cc.size(0, 0); - this._pressedTextureSize = cc.size(0, 0); - this._disabledTextureSize = cc.size(0, 0); - this._titleColor = cc.color.WHITE; ccui.Widget.prototype.ctor.call(this); this.setTouchEnabled(true); + this._normalLoader = new cc.Sprite.LoadManager(); + this._clickedLoader = new cc.Sprite.LoadManager(); + this._disabledLoader = new cc.Sprite.LoadManager(); + if (normalImage) { this.loadTextures(normalImage, selectedImage,disableImage, texType); } }, + _createTitleRendererIfNeeded: function ( ) { + if(!this._titleRenderer) { + this._titleRenderer = new cc.LabelTTF(""); + this._titleRenderer.setAnchorPoint(0.5, 0.5); + this._titleColor = cc.color.WHITE; + this._titleRenderer.setVerticalAlignment(cc.VERTICAL_TEXT_ALIGNMENT_CENTER); + this.addProtectedChild(this._titleRenderer, ccui.Button.TITLE_RENDERER_ZORDER, -1); + } + }, + _initRenderer: function () { - //todo create Scale9Sprite - this._buttonNormalRenderer = new cc.Sprite(); - this._buttonClickedRenderer = new cc.Sprite(); - this._buttonDisableRenderer = new cc.Sprite(); - this._titleRenderer = new cc.LabelTTF(""); - this._titleRenderer.setAnchorPoint(0.5, 0.5); - this._titleRenderer.setVerticalAlignment(cc.VERTICAL_TEXT_ALIGNMENT_CENTER); + this._buttonScale9Renderer = new ccui.Scale9Sprite(); + + this._buttonScale9Renderer.setRenderingType(ccui.Scale9Sprite.RenderingType.SIMPLE); - this.addProtectedChild(this._buttonNormalRenderer, ccui.Button.NORMAL_RENDERER_ZORDER, -1); - this.addProtectedChild(this._buttonClickedRenderer, ccui.Button.PRESSED_RENDERER_ZORDER, -1); - this.addProtectedChild(this._buttonDisableRenderer, ccui.Button.DISABLED_RENDERER_ZORDER, -1); - this.addProtectedChild(this._titleRenderer, ccui.Button.TITLE_RENDERER_ZORDER, -1); + this.addProtectedChild(this._buttonScale9Renderer, ccui.Button.DISABLED_RENDERER_ZORDER, -1); }, /** @@ -129,37 +123,18 @@ ccui.Button = ccui.Widget.extend(/** @lends ccui.Button# */{ * @param {Boolean} able true that using scale9 renderer, false otherwise. */ setScale9Enabled: function (able) { - //todo create Scale9Sprite if (this._scale9Enabled === able) return; this._brightStyle = ccui.Widget.BRIGHT_STYLE_NONE; this._scale9Enabled = able; - this.removeProtectedChild(this._buttonNormalRenderer); - this.removeProtectedChild(this._buttonClickedRenderer); - this.removeProtectedChild(this._buttonDisableRenderer); - if (this._scale9Enabled) { - this._buttonNormalRenderer = new ccui.Scale9Sprite(); - this._buttonClickedRenderer = new ccui.Scale9Sprite(); - this._buttonDisableRenderer = new ccui.Scale9Sprite(); + this._buttonScale9Renderer.setRenderingType(ccui.Scale9Sprite.RenderingType.SLICED); } else { - this._buttonNormalRenderer = new cc.Sprite(); - this._buttonClickedRenderer = new cc.Sprite(); - this._buttonDisableRenderer = new cc.Sprite(); + this._buttonScale9Renderer.setRenderingType(ccui.Scale9Sprite.RenderingType.SIMPLE); } - this._buttonClickedRenderer.setVisible(false); - this._buttonDisableRenderer.setVisible(false); - - this.loadTextureNormal(this._normalFileName, this._normalTexType); - this.loadTexturePressed(this._clickedFileName, this._pressedTexType); - this.loadTextureDisabled(this._disabledFileName, this._disabledTexType); - - this.addProtectedChild(this._buttonNormalRenderer, ccui.Button.NORMAL_RENDERER_ZORDER, -1); - this.addProtectedChild(this._buttonClickedRenderer, ccui.Button.PRESSED_RENDERER_ZORDER, -1); - this.addProtectedChild(this._buttonDisableRenderer, ccui.Button.DISABLED_RENDERER_ZORDER, -1); if (this._scale9Enabled) { var ignoreBefore = this._ignoreSize; this.ignoreContentAdaptWithSize(false); @@ -167,14 +142,11 @@ ccui.Button = ccui.Widget.extend(/** @lends ccui.Button# */{ } else { this.ignoreContentAdaptWithSize(this._prevIgnoreSize); } - this.setCapInsetsNormalRenderer(this._capInsetsNormal); - this.setCapInsetsPressedRenderer(this._capInsetsPressed); - this.setCapInsetsDisabledRenderer(this._capInsetsDisabled); + this.setCapInsets(this._capInsetsNormal); + this.setBright(this._bright); this._normalTextureAdaptDirty = true; - this._pressedTextureAdaptDirty = true; - this._disabledTextureAdaptDirty = true; }, /** @@ -209,8 +181,10 @@ ccui.Button = ccui.Widget.extend(/** @lends ccui.Button# */{ if (this._unifySize) return this._getNormalSize(); - if (!this._normalTextureLoaded && this._titleRenderer.getString().length > 0) { - return this._titleRenderer.getContentSize(); + if (!this._normalTextureLoaded ) { + if(this._titleRenderer && this._titleRenderer.getString().length > 0) { + return this._titleRenderer.getContentSize(); + } } return cc.size(this._normalTextureSize); }, @@ -228,52 +202,91 @@ ccui.Button = ccui.Widget.extend(/** @lends ccui.Button# */{ this.loadTextureDisabled(disabled, texType); }, + _createSpriteFrameWithFile: function (file) { + var texture = cc.textureCache.getTextureForKey(file); + if (!texture) { + texture = cc.textureCache.addImage(file); + } + if(!texture._textureLoaded) { + return texture; + } + + var textureSize = texture.getContentSize(); + var rect = cc.rect(0, 0, textureSize.width, textureSize.height); + return new cc.SpriteFrame(texture, rect); + }, + + _createSpriteFrameWithName: function (name) { + var frame = cc.spriteFrameCache.getSpriteFrame(name); + if (frame == null) { + cc.log("ccui.Scale9Sprite.initWithSpriteFrameName(): can't find the sprite frame by spriteFrameName"); + return null; + } + + return frame; + }, + /** * Load normal state texture for button. * @param {String} normal normal state of texture's filename. * @param {ccui.Widget.LOCAL_TEXTURE|ccui.Widget.PLIST_TEXTURE} texType */ loadTextureNormal: function (normal, texType) { - if (!normal) - return; + if (!normal) return; + texType = texType || ccui.Widget.LOCAL_TEXTURE; this._normalFileName = normal; this._normalTexType = texType; - var self = this; - var normalRenderer = this._buttonNormalRenderer; - if(!normalRenderer._textureLoaded){ - normalRenderer.addEventListener("load", function(){ - self.loadTextureNormal(self._normalFileName, self._normalTexType); - }); - } + var normalSpriteFrame; switch (this._normalTexType){ case ccui.Widget.LOCAL_TEXTURE: - //SetTexture cannot load resource - normalRenderer.initWithFile(normal); - break; + normalSpriteFrame = this._createSpriteFrameWithFile(normal); + break; case ccui.Widget.PLIST_TEXTURE: - //SetTexture cannot load resource - if (normal[0] === "#") { - normal = normal.substr(1, normal.length - 1); - } - normalRenderer.initWithSpriteFrameName(normal); - break; - default: - break; + if (normal[0] === "#") { + normal = normal.substr(1, normal.length - 1); + } + normalSpriteFrame = this._createSpriteFrameWithName(normal); + break; + default: + break; + } + + if(!normalSpriteFrame) { + return; } - this._normalTextureLoaded = normalRenderer._textureLoaded; + if(!normalSpriteFrame._textureLoaded) { + this._normalLoader.clear(); + this._normalLoader.once(normalSpriteFrame, function () { + this.loadTextureNormal(this._normalFileName, this._normalTexType); + }, this); + return; + } + + this._normalTextureLoaded = normalSpriteFrame._textureLoaded; + this._buttonNormalSpriteFrame = normalSpriteFrame; + this._buttonScale9Renderer.setSpriteFrame(normalSpriteFrame); + if (this._scale9Enabled){ + this._buttonScale9Renderer.setCapInsets(this._capInsetsNormal); + } - this._normalTextureSize = this._buttonNormalRenderer.getContentSize(); + // FIXME: https://github.com/cocos2d/cocos2d-x/issues/12249 + if(!this._ignoreSize && cc.sizeEqualToSize(this._customSize, cc.size(0, 0))) { + this._customSize = this._buttonScale9Renderer.getContentSize(); + } + + this._normalTextureSize = this._buttonScale9Renderer.getContentSize(); this._updateChildrenDisplayedRGBA(); if (this._unifySize){ if (this._scale9Enabled){ - normalRenderer.setCapInsets(this._capInsetsNormal); + this._buttonScale9Renderer.setCapInsets(this._capInsetsNormal); this._updateContentSizeWithTextureSize(this._getNormalSize()); } - }else + }else { this._updateContentSizeWithTextureSize(this._normalTextureSize); + } this._normalTextureAdaptDirty = true; this._findLayout(); @@ -291,39 +304,35 @@ ccui.Button = ccui.Widget.extend(/** @lends ccui.Button# */{ this._clickedFileName = selected; this._pressedTexType = texType; - var self = this; - var clickedRenderer = this._buttonClickedRenderer; - if(!clickedRenderer._textureLoaded){ - clickedRenderer.addEventListener("load", function(){ - self.loadTexturePressed(self._clickedFileName, self._pressedTexType); - }); - } - + var clickedSpriteFrame; switch (this._pressedTexType) { - case ccui.Widget.LOCAL_TEXTURE: - //SetTexture cannot load resource - clickedRenderer.initWithFile(selected); - break; - case ccui.Widget.PLIST_TEXTURE: - //SetTexture cannot load resource - if (selected[0] === "#") { - selected = selected.substr(1, selected.length - 1); - } - clickedRenderer.initWithSpriteFrameName(selected); - break; - default: - break; + case ccui.Widget.LOCAL_TEXTURE: + clickedSpriteFrame = this._createSpriteFrameWithFile(selected); + break; + case ccui.Widget.PLIST_TEXTURE: + if (selected[0] === "#") { + selected = selected.substr(1, selected.length - 1); + } + clickedSpriteFrame = this._createSpriteFrameWithName(selected); + break; + default: + break; } - if (this._scale9Enabled) - clickedRenderer.setCapInsets(this._capInsetsPressed); + if(!clickedSpriteFrame) return; - this._pressedTextureSize = this._buttonClickedRenderer.getContentSize(); + if(!clickedSpriteFrame._textureLoaded) { + this._clickedLoader.clear(); + this._clickedLoader.once(clickedSpriteFrame, function () { + this.loadTexturePressed(this._clickedFileName, this._pressedTexType); + }, this); + return; + } + + this._buttonClickedSpriteFrame = clickedSpriteFrame; this._updateChildrenDisplayedRGBA(); this._pressedTextureLoaded = true; - this._pressedTextureAdaptDirty = true; - this._findLayout(); }, /** @@ -339,38 +348,35 @@ ccui.Button = ccui.Widget.extend(/** @lends ccui.Button# */{ this._disabledFileName = disabled; this._disabledTexType = texType; - var self = this; - var disabledRenderer = this._buttonDisableRenderer; - if(!disabledRenderer._textureLoaded){ - disabledRenderer.addEventListener("load", function() { - self.loadTextureDisabled(self._disabledFileName, self._disabledTexType); - }); - } - + var disabledSpriteframe; switch (this._disabledTexType) { - case ccui.Widget.LOCAL_TEXTURE: - //SetTexture cannot load resource - disabledRenderer.initWithFile(disabled); - break; - case ccui.Widget.PLIST_TEXTURE: - //SetTexture cannot load resource - if (disabled[0] === "#") { - disabled = disabled.substr(1, disabled.length - 1); - } - disabledRenderer.initWithSpriteFrameName(disabled); - break; - default: - break; + case ccui.Widget.LOCAL_TEXTURE: + disabledSpriteframe = this._createSpriteFrameWithFile(disabled); + break; + case ccui.Widget.PLIST_TEXTURE: + if (disabled[0] === "#") { + disabled = disabled.substr(1, disabled.length - 1); + } + disabledSpriteframe = this._createSpriteFrameWithName(disabled); + break; + default: + break; } - if (this._scale9Enabled) - disabledRenderer.setCapInsets(this._capInsetsDisabled); + if(!disabledSpriteframe) return; - this._disabledTextureSize = this._buttonDisableRenderer.getContentSize(); + if(!disabledSpriteframe._textureLoaded) { + this._disabledLoader.clear(); + this._disabledLoader.once(disabledSpriteframe, function () { + this.loadTextureDisabled(this._disabledFileName, this._disabledTexType); + }, this); + return; + } + + this._buttonDisableSpriteFrame = disabledSpriteframe; this._updateChildrenDisplayedRGBA(); this._disabledTextureLoaded = true; - this._disabledTextureAdaptDirty = true; this._findLayout(); }, @@ -380,8 +386,6 @@ ccui.Button = ccui.Widget.extend(/** @lends ccui.Button# */{ */ setCapInsets: function (capInsets) { this.setCapInsetsNormalRenderer(capInsets); - this.setCapInsetsPressedRenderer(capInsets); - this.setCapInsetsDisabledRenderer(capInsets); }, /** @@ -389,7 +393,7 @@ ccui.Button = ccui.Widget.extend(/** @lends ccui.Button# */{ * @param {cc.Rect} capInsets */ setCapInsetsNormalRenderer: function (capInsets) { - if(!capInsets) + if(!capInsets || !this._scale9Enabled) return; var x = capInsets.x, y = capInsets.y; @@ -409,9 +413,8 @@ ccui.Button = ccui.Widget.extend(/** @lends ccui.Button# */{ locInsets.width = width; locInsets.height = height; - if (!this._scale9Enabled) - return; - this._buttonNormalRenderer.setCapInsets(locInsets); + this._capInsetsNormal = locInsets; + this._buttonScale9Renderer.setCapInsets(locInsets); }, /** @@ -427,28 +430,7 @@ ccui.Button = ccui.Widget.extend(/** @lends ccui.Button# */{ * @param {cc.Rect} capInsets */ setCapInsetsPressedRenderer: function (capInsets) { - if(!capInsets || !this._scale9Enabled) - return; - - var x = capInsets.x, y = capInsets.y; - var width = capInsets.width, height = capInsets.height; - - if (this._pressedTextureSize.width < width) { - x = 0; - width = 0; - } - if (this._pressedTextureSize.height < height) { - y = 0; - height = 0; - } - - var locInsets = this._capInsetsPressed; - locInsets.x = x; - locInsets.y = y; - locInsets.width = width; - locInsets.height = height; - - this._buttonClickedRenderer.setCapInsets(locInsets); + this.setCapInsetsNormalRenderer(capInsets); }, /** @@ -456,7 +438,7 @@ ccui.Button = ccui.Widget.extend(/** @lends ccui.Button# */{ * @returns {cc.Rect} */ getCapInsetsPressedRenderer: function () { - return cc.rect(this._capInsetsPressed); + return cc.rect(this._capInsetsNormal); }, /** @@ -464,28 +446,7 @@ ccui.Button = ccui.Widget.extend(/** @lends ccui.Button# */{ * @param {cc.Rect} capInsets */ setCapInsetsDisabledRenderer: function (capInsets) { - if(!capInsets || !this._scale9Enabled) - return; - - var x = capInsets.x, y = capInsets.y; - var width = capInsets.width, height = capInsets.height; - - if (this._disabledTextureSize.width < width) { - x = 0; - width = 0; - } - if (this._disabledTextureSize.height < height) { - y = 0; - height = 0; - } - - var locInsets = this._capInsetsDisabled; - locInsets.x = x; - locInsets.y = y; - locInsets.width = width; - locInsets.height = height; - - this._buttonDisableRenderer.setCapInsets(locInsets); + this.setCapInsetsNormalRenderer(capInsets); }, /** @@ -493,93 +454,99 @@ ccui.Button = ccui.Widget.extend(/** @lends ccui.Button# */{ * @returns {cc.Rect} */ getCapInsetsDisabledRenderer: function () { - return cc.rect(this._capInsetsDisabled); + return cc.rect(this._capInsetsNormal); }, _onPressStateChangedToNormal: function () { - this._buttonNormalRenderer.setVisible(true); - this._buttonClickedRenderer.setVisible(false); - this._buttonDisableRenderer.setVisible(false); - if (this._scale9Enabled) - this._buttonNormalRenderer.setState( ccui.Scale9Sprite.state.NORMAL); + this._buttonScale9Renderer.setSpriteFrame(this._buttonNormalSpriteFrame); + + if (this._scale9Enabled) { + this._buttonScale9Renderer.setState( ccui.Scale9Sprite.state.NORMAL); + } + if (this._pressedTextureLoaded) { if (this.pressedActionEnabled){ - this._buttonNormalRenderer.stopAllActions(); - this._buttonClickedRenderer.stopAllActions(); - //var zoomAction = cc.scaleTo(ccui.Button.ZOOM_ACTION_TIME_STEP, this._normalTextureScaleXInSize, this._normalTextureScaleYInSize); - //fixme: the zoomAction will run in the next frame which will cause the _buttonNormalRenderer to a wrong scale - //this._buttonNormalRenderer.runAction(zoomAction); - this._buttonNormalRenderer.setScale(this._normalTextureScaleXInSize, this._normalTextureScaleYInSize); - this._buttonClickedRenderer.setScale(this._pressedTextureScaleXInSize, this._pressedTextureScaleYInSize); - - this._titleRenderer.stopAllActions(); - if (this._unifySize){ - var zoomTitleAction = cc.scaleTo(ccui.Button.ZOOM_ACTION_TIME_STEP, 1, 1); - this._titleRenderer.runAction(zoomTitleAction); - }else{ - this._titleRenderer.setScaleX(1); - this._titleRenderer.setScaleY(1); + this._buttonScale9Renderer.stopAllActions(); + this._buttonScale9Renderer.setScale(1.0); + + if(this._titleRenderer) { + this._titleRenderer.stopAllActions(); + + if (this._unifySize){ + var zoomTitleAction = cc.scaleTo(ccui.Button.ZOOM_ACTION_TIME_STEP, 1, 1); + this._titleRenderer.runAction(zoomTitleAction); + }else{ + this._titleRenderer.setScaleX(1); + this._titleRenderer.setScaleY(1); + } } + } } else { - this._buttonNormalRenderer.stopAllActions(); - this._buttonNormalRenderer.setScale(this._normalTextureScaleXInSize, this._normalTextureScaleYInSize); + this._buttonScale9Renderer.stopAllActions(); + this._buttonScale9Renderer.setScale(1.0); - this._titleRenderer.stopAllActions(); - if (this._scale9Enabled) - this._buttonNormalRenderer.setColor(cc.color.WHITE); + if (this._scale9Enabled) { + this._buttonScale9Renderer.setColor(cc.color.WHITE); + } + + if(this._titleRenderer) { + this._titleRenderer.stopAllActions(); - this._titleRenderer.setScaleX(1); - this._titleRenderer.setScaleY(1); + this._titleRenderer.setScaleX(1); + this._titleRenderer.setScaleY(1); + } } }, _onPressStateChangedToPressed: function () { - var locNormalRenderer = this._buttonNormalRenderer; - if (this._scale9Enabled) - locNormalRenderer.setState(ccui.Scale9Sprite.state.NORMAL); + if (this._scale9Enabled) { + this._buttonScale9Renderer.setState(ccui.Scale9Sprite.state.NORMAL); + } if (this._pressedTextureLoaded) { - locNormalRenderer.setVisible(false); - this._buttonClickedRenderer.setVisible(true); - this._buttonDisableRenderer.setVisible(false); - if (this.pressedActionEnabled) { - locNormalRenderer.stopAllActions(); - this._buttonClickedRenderer.stopAllActions(); - var zoomAction = cc.scaleTo(ccui.Button.ZOOM_ACTION_TIME_STEP, this._pressedTextureScaleXInSize + this._zoomScale, - this._pressedTextureScaleYInSize + this._zoomScale); - this._buttonClickedRenderer.runAction(zoomAction); - locNormalRenderer.setScale(this._pressedTextureScaleXInSize + this._zoomScale, this._pressedTextureScaleYInSize + this._zoomScale); + this._buttonScale9Renderer.setSpriteFrame(this._buttonClickedSpriteFrame); - this._titleRenderer.stopAllActions(); - this._titleRenderer.runAction(cc.scaleTo(ccui.Button.ZOOM_ACTION_TIME_STEP, 1 + this._zoomScale, 1 + this._zoomScale)); + if (this.pressedActionEnabled) { + this._buttonScale9Renderer.stopAllActions(); + + var zoomAction = cc.scaleTo(ccui.Button.ZOOM_ACTION_TIME_STEP, + 1.0 + this._zoomScale, + 1.0 + this._zoomScale); + this._buttonScale9Renderer.runAction(zoomAction); + + if(this._titleRenderer) { + this._titleRenderer.stopAllActions(); + this._titleRenderer.runAction(cc.scaleTo(ccui.Button.ZOOM_ACTION_TIME_STEP, + 1 + this._zoomScale, + 1 + this._zoomScale)); + } } } else { - locNormalRenderer.setVisible(true); - this._buttonClickedRenderer.setVisible(true); - this._buttonDisableRenderer.setVisible(false); - locNormalRenderer.stopAllActions(); - locNormalRenderer.setScale(this._normalTextureScaleXInSize + this._zoomScale, this._normalTextureScaleYInSize + this._zoomScale); + this._buttonScale9Renderer.setSpriteFrame(this._buttonClickedSpriteFrame); - this._titleRenderer.stopAllActions(); - this._titleRenderer.setScaleX(1 + this._zoomScale); - this._titleRenderer.setScaleY(1 + this._zoomScale); + this._buttonScale9Renderer.stopAllActions(); + this._buttonScale9Renderer.setScale(1.0 + this._zoomScale, 1.0 + this._zoomScale); + + if (this._titleRenderer) { + this._titleRenderer.stopAllActions(); + this._titleRenderer.setScaleX(1 + this._zoomScale); + this._titleRenderer.setScaleY(1 + this._zoomScale); + } } }, _onPressStateChangedToDisabled: function () { //if disable resource is null if (!this._disabledTextureLoaded){ - if (this._normalTextureLoaded && this._scale9Enabled) - this._buttonNormalRenderer.setState(ccui.Scale9Sprite.state.GRAY); + if (this._normalTextureLoaded && this._scale9Enabled) { + this._buttonScale9Renderer.setState(ccui.Scale9Sprite.state.GRAY); + } }else{ - this._buttonNormalRenderer.setVisible(false); - this._buttonDisableRenderer.setVisible(true); + this._buttonScale9Renderer.setSpriteFrame(this._buttonDisableSpriteFrame); } - this._buttonClickedRenderer.setVisible(false); - this._buttonNormalRenderer.setScale(this._normalTextureScaleXInSize, this._normalTextureScaleYInSize); - this._buttonClickedRenderer.setScale(this._pressedTextureScaleXInSize, this._pressedTextureScaleYInSize); + this._buttonScale9Renderer.setScale(1.0); }, _updateContentSize: function(){ @@ -600,10 +567,10 @@ ccui.Button = ccui.Widget.extend(/** @lends ccui.Button# */{ _onSizeChanged: function () { ccui.Widget.prototype._onSizeChanged.call(this); - this._updateTitleLocation(); + if(this._titleRenderer) { + this._updateTitleLocation(); + } this._normalTextureAdaptDirty = true; - this._pressedTextureAdaptDirty = true; - this._disabledTextureAdaptDirty = true; }, /** @@ -611,97 +578,12 @@ ccui.Button = ccui.Widget.extend(/** @lends ccui.Button# */{ * @returns {cc.Node} */ getVirtualRenderer: function () { - if (this._bright) { - switch (this._brightStyle) { - case ccui.Widget.BRIGHT_STYLE_NORMAL: - return this._buttonNormalRenderer; - case ccui.Widget.BRIGHT_STYLE_HIGH_LIGHT: - return this._buttonClickedRenderer; - default: - return null; - } - } else - return this._buttonDisableRenderer; + return this._buttonScale9Renderer; }, _normalTextureScaleChangedWithSize: function () { - if(this._ignoreSize && !this._unifySize){ - if(!this._scale9Enabled){ - this._buttonNormalRenderer.setScale(1); - this._normalTextureScaleXInSize = this._normalTextureScaleYInSize = 1; - } - }else{ - if (this._scale9Enabled){ - this._buttonNormalRenderer.setPreferredSize(this._contentSize); - this._normalTextureScaleXInSize = this._normalTextureScaleYInSize = 1; - this._buttonNormalRenderer.setScale(this._normalTextureScaleXInSize, this._normalTextureScaleYInSize); - }else{ - var textureSize = this._normalTextureSize; - if (textureSize.width <= 0 || textureSize.height <= 0) - { - this._buttonNormalRenderer.setScale(1); - return; - } - var scaleX = this._contentSize.width / textureSize.width; - var scaleY = this._contentSize.height / textureSize.height; - this._buttonNormalRenderer.setScaleX(scaleX); - this._buttonNormalRenderer.setScaleY(scaleY); - this._normalTextureScaleXInSize = scaleX; - this._normalTextureScaleYInSize = scaleY; - } - } - this._buttonNormalRenderer.setPosition(this._contentSize.width / 2, this._contentSize.height / 2); - }, - - _pressedTextureScaleChangedWithSize: function () { - if (this._ignoreSize && !this._unifySize) { - if (!this._scale9Enabled) { - this._buttonClickedRenderer.setScale(1); - this._pressedTextureScaleXInSize = this._pressedTextureScaleYInSize = 1; - } - } else { - if (this._scale9Enabled) { - this._buttonClickedRenderer.setPreferredSize(this._contentSize); - this._pressedTextureScaleXInSize = this._pressedTextureScaleYInSize = 1; - this._buttonClickedRenderer.setScale(this._pressedTextureScaleXInSize, this._pressedTextureScaleYInSize); - } else { - var textureSize = this._pressedTextureSize; - if (textureSize.width <= 0 || textureSize.height <= 0) { - this._buttonClickedRenderer.setScale(1); - return; - } - var scaleX = this._contentSize.width / textureSize.width; - var scaleY = this._contentSize.height / textureSize.height; - this._buttonClickedRenderer.setScaleX(scaleX); - this._buttonClickedRenderer.setScaleY(scaleY); - this._pressedTextureScaleXInSize = scaleX; - this._pressedTextureScaleYInSize = scaleY; - } - } - this._buttonClickedRenderer.setPosition(this._contentSize.width / 2, this._contentSize.height / 2); - }, - - _disabledTextureScaleChangedWithSize: function () { - if(this._ignoreSize && !this._unifySize){ - if (this._scale9Enabled) - this._buttonDisableRenderer.setScale(1); - }else { - if (this._scale9Enabled){ - this._buttonDisableRenderer.setScale(1); - this._buttonDisableRenderer.setPreferredSize(this._contentSize); - }else{ - var textureSize = this._disabledTextureSize; - if (textureSize.width <= 0 || textureSize.height <= 0) { - this._buttonDisableRenderer.setScale(1); - return; - } - var scaleX = this._contentSize.width / textureSize.width; - var scaleY = this._contentSize.height / textureSize.height; - this._buttonDisableRenderer.setScaleX(scaleX); - this._buttonDisableRenderer.setScaleY(scaleY); - } - } - this._buttonDisableRenderer.setPosition(this._contentSize.width / 2, this._contentSize.height / 2); + this._buttonScale9Renderer.setContentSize(this._contentSize); + this._buttonScale9Renderer.setPosition(this._contentSize.width / 2, this._contentSize.height / 2); }, _adaptRenderers: function(){ @@ -709,14 +591,6 @@ ccui.Button = ccui.Widget.extend(/** @lends ccui.Button# */{ this._normalTextureScaleChangedWithSize(); this._normalTextureAdaptDirty = false; } - if (this._pressedTextureAdaptDirty) { - this._pressedTextureScaleChangedWithSize(); - this._pressedTextureAdaptDirty = false; - } - if (this._disabledTextureAdaptDirty) { - this._disabledTextureScaleChangedWithSize(); - this._disabledTextureAdaptDirty = false; - } }, _updateTitleLocation: function(){ @@ -736,8 +610,10 @@ ccui.Button = ccui.Widget.extend(/** @lends ccui.Button# */{ * @param {String} text */ setTitleText: function (text) { - if(text === this.getTitleText()) - return; + if(text === this.getTitleText()) return; + + this._createTitleRendererIfNeeded(); + this._titleRenderer.setString(text); if (this._ignoreSize){ var s = this.getVirtualRendererSize(); @@ -752,7 +628,10 @@ ccui.Button = ccui.Widget.extend(/** @lends ccui.Button# */{ * @returns {String} text */ getTitleText: function () { - return this._titleRenderer.getString(); + if(this._titleRenderer) { + return this._titleRenderer.getString(); + } + return ""; }, /** @@ -760,6 +639,7 @@ ccui.Button = ccui.Widget.extend(/** @lends ccui.Button# */{ * @param {cc.Color} color */ setTitleColor: function (color) { + this._createTitleRendererIfNeeded(); this._titleRenderer.setFontFillColor(color); }, @@ -768,7 +648,10 @@ ccui.Button = ccui.Widget.extend(/** @lends ccui.Button# */{ * @returns {cc.Color} */ getTitleColor: function () { - return this._titleRenderer._getFillStyle(); + if (this._titleRenderer) { + return this._titleRenderer._getFillStyle(); + } + return cc.color.WHITE; }, /** @@ -776,6 +659,8 @@ ccui.Button = ccui.Widget.extend(/** @lends ccui.Button# */{ * @param {cc.Size} size */ setTitleFontSize: function (size) { + this._createTitleRendererIfNeeded(); + this._titleRenderer.setFontSize(size); this._fontSize = size; }, @@ -785,7 +670,10 @@ ccui.Button = ccui.Widget.extend(/** @lends ccui.Button# */{ * @returns {Number} */ getTitleFontSize: function () { - return this._titleRenderer.getFontSize(); + if (this._titleRenderer) { + return this._titleRenderer.getFontSize(); + } + return this._fontSize; }, /** @@ -821,6 +709,8 @@ ccui.Button = ccui.Widget.extend(/** @lends ccui.Button# */{ * @param {String} fontName */ setTitleFontName: function (fontName) { + this._createTitleRendererIfNeeded(); + this._titleRenderer.setFontName(fontName); this._fontName = fontName; }, @@ -839,7 +729,10 @@ ccui.Button = ccui.Widget.extend(/** @lends ccui.Button# */{ * @returns {String} */ getTitleFontName: function () { - return this._titleRenderer.getFontName(); + if(this._titleRenderer) { + return this._titleRenderer.getFontName(); + } + return this._fontName; }, _setTitleFont: function (font) { @@ -864,17 +757,19 @@ ccui.Button = ccui.Widget.extend(/** @lends ccui.Button# */{ _copySpecialProperties: function (uiButton) { this._prevIgnoreSize = uiButton._prevIgnoreSize; + this._capInsetsNormal = uiButton._capInsetsNormal; this.setScale9Enabled(uiButton._scale9Enabled); + this.loadTextureNormal(uiButton._normalFileName, uiButton._normalTexType); this.loadTexturePressed(uiButton._clickedFileName, uiButton._pressedTexType); this.loadTextureDisabled(uiButton._disabledFileName, uiButton._disabledTexType); - this.setCapInsetsNormalRenderer(uiButton._capInsetsNormal); - this.setCapInsetsPressedRenderer(uiButton._capInsetsPressed); - this.setCapInsetsDisabledRenderer(uiButton._capInsetsDisabled); - this.setTitleText(uiButton.getTitleText()); - this.setTitleFontName(uiButton.getTitleFontName()); - this.setTitleFontSize(uiButton.getTitleFontSize()); - this.setTitleColor(uiButton.getTitleColor()); + + if(uiButton._titleRenderer && uiButton._titleRenderer._string) { + this.setTitleText(uiButton.getTitleText()); + this.setTitleFontName(uiButton.getTitleFontName()); + this.setTitleFontSize(uiButton.getTitleFontSize()); + this.setTitleColor(uiButton.getTitleColor()); + } this.setPressedActionEnabled(uiButton.pressedActionEnabled); this.setZoomScale(uiButton._zoomScale); }, @@ -884,9 +779,7 @@ ccui.Button = ccui.Widget.extend(/** @lends ccui.Button# */{ if (this._titleRenderer !== null) titleSize = this._titleRenderer.getContentSize(); - var imageSize; - if (this._buttonNormalRenderer !== null) - imageSize = this._buttonNormalRenderer.getContentSize(); + var imageSize = this._buttonScale9Renderer.getContentSize(); var width = titleSize.width > imageSize.width ? titleSize.width : imageSize.width; var height = titleSize.height > imageSize.height ? titleSize.height : imageSize.height; diff --git a/extensions/ccui/uiwidgets/UIImageView.js b/extensions/ccui/uiwidgets/UIImageView.js index b7e6511bf9..970d015160 100644 --- a/extensions/ccui/uiwidgets/UIImageView.js +++ b/extensions/ccui/uiwidgets/UIImageView.js @@ -63,8 +63,8 @@ ccui.ImageView = ccui.Widget.extend(/** @lends ccui.ImageView# */{ }, _initRenderer: function () { - //todo create Scale9Sprite and setScale9Enabled(false) - this._imageRenderer = new cc.Sprite(); + this._imageRenderer = new ccui.Scale9Sprite(); + this._imageRenderer.setRenderingType(ccui.Scale9Sprite.RenderingType.SIMPLE); this.addProtectedChild(this._imageRenderer, ccui.ImageView.RENDERER_ZORDER, -1); }, @@ -74,9 +74,7 @@ ccui.ImageView = ccui.Widget.extend(/** @lends ccui.ImageView# */{ * @param {ccui.Widget.LOCAL_TEXTURE|ccui.Widget.PLIST_TEXTURE} texType */ loadTexture: function (fileName, texType) { - //todo use this code when _initRenderer use Scale9Sprite - //if (!fileName || (this._textureFile == fileName && this._imageTexType == texType)) { - if (!fileName) { + if (!fileName || (this._textureFile == fileName && this._imageTexType == texType)) { return; } var self = this; @@ -87,7 +85,15 @@ ccui.ImageView = ccui.Widget.extend(/** @lends ccui.ImageView# */{ if(!imageRenderer._textureLoaded){ imageRenderer.addEventListener("load", function(){ - self.loadTexture(self._textureFile, self._imageTexType); + if(!self._ignoreSize && cc.sizeEqualToSize(self._customSize, cc.size(0, 0))) { + self._customSize = self._imageRenderer.getContentSize(); + } + + self._imageTextureSize = imageRenderer.getContentSize(); + + self._updateChildrenDisplayedRGBA(); + + self._updateContentSizeWithTextureSize(self._imageTextureSize); }); } @@ -114,6 +120,10 @@ ccui.ImageView = ccui.Widget.extend(/** @lends ccui.ImageView# */{ break; } + if(!this._ignoreSize && cc.sizeEqualToSize(this._customSize, cc.size(0, 0))) { + this._customSize = this._imageRenderer.getContentSize(); + } + self._imageTextureSize = imageRenderer.getContentSize(); this._updateChildrenDisplayedRGBA(); @@ -128,9 +138,8 @@ ccui.ImageView = ccui.Widget.extend(/** @lends ccui.ImageView# */{ * Sets texture rect * @param {cc.Rect} rect */ - setTextureRect: function (rect) { - if (!this._scale9Enabled) - this._imageRenderer.setTextureRect(rect); + setTextureRect: function () { + cc.warn('ImageView.setTextureRect is deprecated!'); }, /** @@ -138,20 +147,17 @@ ccui.ImageView = ccui.Widget.extend(/** @lends ccui.ImageView# */{ * @param {Boolean} able */ setScale9Enabled: function (able) { - //todo setScale9Enabled if (this._scale9Enabled === able) return; this._scale9Enabled = able; - this.removeProtectedChild(this._imageRenderer); - this._imageRenderer = null; + if (this._scale9Enabled) { - this._imageRenderer = new ccui.Scale9Sprite(); + this._imageRenderer.setRenderingType(ccui.Scale9Sprite.RenderingType.SLICED); } else { - this._imageRenderer = new cc.Sprite(); + this._imageRenderer.setRenderingType(ccui.Scale9Sprite.RenderingType.SIMPLE); } - this.loadTexture(this._textureFile, this._imageTexType); - this.addProtectedChild(this._imageRenderer, ccui.ImageView.RENDERER_ZORDER, -1); + if (this._scale9Enabled) { var ignoreBefore = this._ignoreSize; this.ignoreContentAdaptWithSize(false); @@ -187,16 +193,15 @@ ccui.ImageView = ccui.Widget.extend(/** @lends ccui.ImageView# */{ * @param {cc.Rect} capInsets */ setCapInsets: function (capInsets) { - if(!capInsets) - return; + if(!capInsets) return; + var locInsets = this._capInsets; locInsets.x = capInsets.x; locInsets.y = capInsets.y; locInsets.width = capInsets.width; locInsets.height = capInsets.height; - if (!this._scale9Enabled) - return; + if (!this._scale9Enabled) return; this._imageRenderer.setCapInsets(capInsets); }, @@ -238,23 +243,7 @@ ccui.ImageView = ccui.Widget.extend(/** @lends ccui.ImageView# */{ }, _imageTextureScaleChangedWithSize: function () { - if (this._ignoreSize) { - if (!this._scale9Enabled) - this._imageRenderer.setScale(1.0); - } else { - if (this._scale9Enabled){ - this._imageRenderer.setPreferredSize(this._contentSize); - this._imageRenderer.setScale(1); - } else { - var textureSize = this._imageTextureSize; - if (textureSize.width <= 0.0 || textureSize.height <= 0.0) { - this._imageRenderer.setScale(1.0); - return; - } - this._imageRenderer.setScaleX(this._contentSize.width / textureSize.width); - this._imageRenderer.setScaleY(this._contentSize.height / textureSize.height); - } - } + this._imageRenderer.setContentSize(this._contentSize); this._imageRenderer.setPosition(this._contentSize.width / 2.0, this._contentSize.height / 2.0); }, @@ -274,9 +263,9 @@ ccui.ImageView = ccui.Widget.extend(/** @lends ccui.ImageView# */{ _copySpecialProperties: function (imageView) { if(imageView instanceof ccui.ImageView){ this._prevIgnoreSize = imageView._prevIgnoreSize; - this.setScale9Enabled(imageView._scale9Enabled); + this._capInsets = imageView._capInsets; this.loadTexture(imageView._textureFile, imageView._imageTexType); - this.setCapInsets(imageView._capInsets); + this.setScale9Enabled(imageView._scale9Enabled); } }, /** @@ -287,17 +276,12 @@ ccui.ImageView = ccui.Widget.extend(/** @lends ccui.ImageView# */{ * @override */ setContentSize: function(contentSize, height){ - if(height != null) + if (height) { contentSize = cc.size(contentSize, height); - ccui.Widget.prototype.setContentSize.call(this, contentSize); - if(!this._scale9Enabled){ - var iContentSize = this._imageRenderer.getContentSize(); - this._imageRenderer.setScaleX(contentSize.width / iContentSize.width); - this._imageRenderer.setScaleY(contentSize.height / iContentSize.height); - }else{ - this._imageRenderer.setContentSize(contentSize); } + ccui.Widget.prototype.setContentSize.call(this, contentSize); + this._imageRenderer.setContentSize(contentSize); } }); diff --git a/moduleConfig.json b/moduleConfig.json index 99e383e35f..b94181331b 100644 --- a/moduleConfig.json +++ b/moduleConfig.json @@ -153,13 +153,12 @@ ], "labels" : [ "core", - - "cocos2d/labels/CCLabelAtlas.js", - "cocos2d/labels/CCLabelAtlasCanvasRenderCmd.js", - "cocos2d/labels/CCLabelAtlasWebGLRenderCmd.js", "cocos2d/labels/CCLabelBMFont.js", "cocos2d/labels/CCLabelBMFontCanvasRenderCmd.js", - "cocos2d/labels/CCLabelBMFontWebGLRenderCmd.js" + "cocos2d/labels/CCLabelBMFontWebGLRenderCmd.js", + "cocos2d/labels/CCLabelAtlas.js", + "cocos2d/labels/CCLabelAtlasCanvasRenderCmd.js", + "cocos2d/labels/CCLabelAtlasWebGLRenderCmd.js" ], "menus" : [ "core", "actions",