From ca1a0f4b63d6afa92924a4e9f1c7f874bb20a278 Mon Sep 17 00:00:00 2001 From: xingsenma Date: Fri, 7 Mar 2014 14:38:33 +0800 Subject: [PATCH 1/6] issue #4221 add richText --- extensions/ccui/uiwidgets/UIRichText.js | 462 +++++++++++++++++++ extensions/cocostudio/action/CCActionNode.js | 2 +- moduleConfig.json | 1 + 3 files changed, 464 insertions(+), 1 deletion(-) create mode 100644 extensions/ccui/uiwidgets/UIRichText.js diff --git a/extensions/ccui/uiwidgets/UIRichText.js b/extensions/ccui/uiwidgets/UIRichText.js new file mode 100644 index 0000000000..1e7e93bde4 --- /dev/null +++ b/extensions/ccui/uiwidgets/UIRichText.js @@ -0,0 +1,462 @@ +/**************************************************************************** + Copyright (c) 2010-2012 cocos2d-x.org + + http://www.cocos2d-x.org + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + ****************************************************************************/ +//Rich element type +ccui.RICH_ELEMENT_TYPE_TEXT = 0; +ccui.RICH_ELEMENT_TYPE_IMAGE = 1; +ccui.RICH_ELEMENT_TYPE_CUSTOM = 2; + +/** + * Base class for ccui.RichElement + * @class + * @extends ccui.Class + */ +ccui.RichElement = ccui.Class.extend(/** @lends ccui.RichElement# */{ + type: 0, + tag: 0, + color: null, + ctor: function () { + this.tag = 0; + this.color = cc.color(255, 255, 255, 255); + }, + init: function (tag, color, opacity) { + this.tag = tag; + this.color.r = color.r; + this.color.g = color.g; + this.color.b = color.b; + this.color.a = opacity; + } +}); + +/** + * Base class for ccui.RichElementText + * @class + * @extends ccui.RichElement + */ +ccui.RichElementText = ccui.RichElement.extend(/** @lends ccui.RichElementText# */{ + text: "", + fontName: "", + fontSize: 0, + ctor: function () { + ccui.RichElement.prototype.ctor.call(this); + this.type = ccui.RICH_ELEMENT_TYPE_TEXT; + this.text = ""; + this.fontName = ""; + this.fontSize = 0; + }, + init: function (tag, color, opacity, text, fontName, fontSize) { + ccui.RichElement.prototype.init.call(this, tag, color, opacity); + this.text = text; + this.fontName = fontName; + this.fontSize = fontSize; + } +}); + +/** + * Create a richElementText + * @param {Number} tag + * @param {cc.Color} color + * @param {Number} opacity + * @param {String} text + * @param {String} fontName + * @param {Number} fontSize + * @returns {ccui.RichElementText} + */ +ccui.RichElementText.create = function (tag, color, opacity, text, fontName, fontSize) { + var element = new ccui.RichElementText(); + element.init(tag, color, opacity, text, fontName, fontSize); + return element; +}; + +/** + * Base class for ccui.RichElementImage + * @class + * @extends ccui.RichElement + */ +ccui.RichElementImage = ccui.RichElement.extend(/** @lends ccui.RichElementImage# */{ + filePath: "", + textureRect: null, + textureType: 0, + ctor: function () { + ccui.RichElement.prototype.ctor.call(this); + this.type = ccui.RICH_ELEMENT_TYPE_IMAGE; + this.filePath = ""; + this.textureRect = cc.rect(0, 0, 0, 0); + this.textureType = 0; + }, + init: function (tag, color, opacity, filePath) { + ccui.RichElement.prototype.init.call(this, tag, color, opacity); + this.filePath = filePath; + } +}); + +/** + * Create a richElementImage + * @param {Number} tag + * @param {cc.Color} color + * @param {Number} opacity + * @param {String} filePath + * @returns {ccui.RichElementText} + */ +ccui.RichElementImage.create = function (tag, color, opacity, filePath) { + var element = new ccui.RichElementImage(); + element.init(tag, color, opacity, filePath); + return element; +}; + +/** + * Base class for ccui.RichElementCustomNode + * @class + * @extends ccui.RichElement + */ +ccui.RichElementCustomNode = ccui.RichElement.extend(/** @lends ccui.RichElementCustomNode# */{ + customNode: null, + ctor: function () { + ccui.RichElement.prototype.ctor.call(this); + this.type = ccui.RICH_ELEMENT_TYPE_CUSTOM; + this.customNode = null; + }, + init: function (tag, color, opacity, customNode) { + ccui.RichElement.prototype.init.call(this, tag, color, opacity); + this.customNode = customNode; + } +}); + +/** + * Create a richElementCustomNode + * @param {Number} tag + * @param {Number} color + * @param {Number} opacity + * @param {cc.Node} customNode + * @returns {RichElementText} + */ +ccui.RichElementCustomNode.create = function (tag, color, opacity, customNode) { + var element = new ccui.RichElementCustomNode(); + element.init(tag, color, opacity, customNode); + return element; +}; + +/** + * Base class for ccui.RichText + * @class + * @extends ccui.Widget + */ +ccui.RichText = ccui.Widget.extend(/** @lends ccui.RichText# */{ + _formatTextDirty: false, + _richElements: null, + _elementRenders: null, + _leftSpaceWidth: 0, + _verticalSpace: 0, + _elementRenderersContainer: null, + + ctor: function () { + ccui.Widget.prototype.ctor.call(this); + this._formatTextDirty = false; + this._richElements = []; + this._elementRenders = []; + this._leftSpaceWidth = 0; + this._verticalSpace = 0; + this._elementRenderersContainer = null; + }, + + initRenderer: function () { + this._elementRenderersContainer = cc.Node.create(); + this._elementRenderersContainer.setAnchorPoint(cc.p(0.5, 0.5)); + cc.Node.prototype.addChild.call(this,this._elementRenderersContainer, 0, -1); + }, + + /** + * Insert a element + * @param {ccui.RichElement} element + * @param {Number} index + */ + insertElement: function (element, index) { + this._richElements.splice(index, 0, element); + this._formatTextDirty = true; + }, + + /** + * Push a element + * @param {ccui.RichElement} element + */ + pushBackElement: function (element) { + this._richElements.push(element); + this._formatTextDirty = true; + }, + + /** + * Remove element + * @param {ccui.RichElement} element + */ + removeElement: function (element) { + if (typeof element === "number") { + this._richElements.splice(element, 1); + } else { + cc.arrayRemoveObject(this._richElements, element); + } + this._formatTextDirty = true; + }, + + formatText: function () { + if (this._formatTextDirty) { + this._elementRenderersContainer.removeAllChildren(); + this._elementRenders.length = 0; + if (this._ignoreSize) { + this.addNewLine(); + for (var i = 0; i < this._richElements.length; i++) { + var element = this._richElements[i]; + var elementRenderer = null; + switch (element.type) { + case ccui.RICH_ELEMENT_TYPE_TEXT: + elementRenderer = cc.LabelTTF.create(element.text, element.fontName, element.fontSize); + break; + case ccui.RICH_ELEMENT_TYPE_IMAGE: + elementRenderer = cc.Sprite.create(element.filePath); + break; + case ccui.RICH_ELEMENT_TYPE_CUSTOM: + elementRenderer = element.customNode; + break; + default: + break; + } + elementRenderer.setColor(element.color); + this.pushToContainer(elementRenderer); + } + } + else { + this.addNewLine(); + for (var i = 0; i < this._richElements.length; i++) { + var element = this._richElements[i]; + switch (element.type) { + case ccui.RICH_ELEMENT_TYPE_TEXT: + this.handleTextRenderer(element.text, element.fontName, element.fontSize, element.color); + break; + case ccui.RICH_ELEMENT_TYPE_IMAGE: + this.handleImageRenderer(element.filePath, element.color); + break; + case ccui.RICH_ELEMENT_TYPE_CUSTOM: + this.handleCustomRenderer(element.customNode); + break; + default: + break; + } + } + } + this.formatRenderers(); + this._formatTextDirty = false; + } + }, + + /** + * Handle text renderer + * @param {String} text + * @param {String} fontName + * @param {Number} fontSize + * @param {cc.Color} color + */ + handleTextRenderer: function (text, fontName, fontSize, color) { + var textRenderer = cc.LabelTTF.create(text, fontName, fontSize); + var textRendererWidth = textRenderer.getContentSize().width; + this._leftSpaceWidth -= textRendererWidth; + if (this._leftSpaceWidth < 0) { + var overstepPercent = (-this._leftSpaceWidth) / textRendererWidth; + var curText = text; + var stringLength = curText.length; + var leftLength = stringLength * (1 - overstepPercent); + var leftWords = curText.substr(0, leftLength); + var cutWords = curText.substr(leftLength, curText.length - 1); + if (leftLength > 0) { + var leftRenderer = cc.LabelTTF.create(leftWords.substr(0, leftLength), fontName, fontSize); + leftRenderer.setColor(color); + this.pushToContainer(leftRenderer); + } + + this.addNewLine(); + this.handleTextRenderer(cutWords, fontName, fontSize, color); + } + else { + textRenderer.setColor(color); + this.pushToContainer(textRenderer); + } + }, + + /** + * Handle image renderer + * @param {String} filePath + * @param {cc.Color} color + * @param {Number} opacity + */ + handleImageRenderer: function (filePath, color, opacity) { + var imageRenderer = cc.Sprite.create(filePath); + this.handleCustomRenderer(imageRenderer); + }, + + /** + * Handle custom renderer + * @param {cc.Node} renderer + */ + handleCustomRenderer: function (renderer) { + var imgSize = renderer.getContentSize(); + this._leftSpaceWidth -= imgSize.width; + if (this._leftSpaceWidth < 0) { + this.addNewLine(); + this.pushToContainer(renderer); + this._leftSpaceWidth -= imgSize.width; + } + else { + this.pushToContainer(renderer); + } + }, + + addNewLine: function () { + this._leftSpaceWidth = this._customSize.width; + this._elementRenders.push([]); + }, + + formatRenderers: function () { + if (this._ignoreSize) { + var newContentSizeWidth = 0; + var newContentSizeHeight = 0; + + var row = this._elementRenders[0]; + var nextPosX = 0; + for (var j = 0; j < row.length; j++) { + var l = row[j]; + l.setAnchorPoint(cc.p(0, 0)); + l.setPosition(cc.p(nextPosX, 0)); + this._elementRenderersContainer.addChild(l, 1, j); + var iSize = l.getContentSize(); + newContentSizeWidth += iSize.width; + newContentSizeHeight = Math.max(newContentSizeHeight, iSize.height); + nextPosX += iSize.width; + } + this._elementRenderersContainer.setContentSize(cc.size(newContentSizeWidth, newContentSizeHeight)); + } + else { + var newContentSizeHeight = 0; + var maxHeights = []; + + for (var i = 0; i < this._elementRenders.length; i++) { + var row = this._elementRenders[i]; + var maxHeight = 0; + for (var j = 0; j < row.length; j++) { + var l = row[j]; + maxHeight = Math.max(l.getContentSize().height, maxHeight); + } + maxHeights[i] = maxHeight; + newContentSizeHeight += maxHeights[i]; + } + + + var nextPosY = this._customSize.height; + for (var i = 0; i < this._elementRenders.length; i++) { + var row = this._elementRenders[i]; + var nextPosX = 0; + nextPosY -= (maxHeights[i] + this._verticalSpace); + + for (var j = 0; j < row.length; j++) { + var l = row[j]; + l.setAnchorPoint(cc.p(0, 0)); + l.setPosition(cc.p(nextPosX, nextPosY)); + this._elementRenderersContainer.addChild(l, 1, i * 10 + j); + nextPosX += l.getContentSize().width; + } + } + this._elementRenderersContainer.setContentSize(this._size); + } + this._elementRenders.length = 0; + if (this._ignoreSize) { + var s = this.getContentSize(); + this._size.width = s.width; + this._size.height = s.height; + } + else { + this._size.width = this._customSize.width; + this._size.height = this._customSize.height; + } + }, + + /** + * Push renderer to container + * @param {cc.Node} renderer + */ + pushToContainer: function (renderer) { + if (this._elementRenders.length <= 0) { + return; + } + this._elementRenders[this._elementRenders.length - 1].push(renderer); + }, + + visit: function (ctx) { + if (this._enabled) { + this.formatText(); + ccui.Widget.prototype.visit.call(this, ctx); + } + }, + + /** + * Set vertical space + * @param {Number} space + */ + setVerticalSpace: function (space) { + this._verticalSpace = space; + }, + + /** + * Set anchor point + * @param {cc.Point} pt + */ + setAnchorPoint: function (pt) { + ccui.Widget.prototype.setAnchorPoint.call(this, pt); + this._elementRenderersContainer.setAnchorPoint(pt); + }, + + /** + * Get content size + * @returns {cc.Size} + */ + getContentSize: function () { + return this._elementRenderersContainer.getContentSize(); + }, + + /** + * Ignore content adapt with size + * @param {Boolean} ignore + */ + ignoreContentAdaptWithSize: function (ignore) { + if (this._ignoreSize != ignore) { + this._formatTextDirty = true; + ccui.Widget.prototype.ignoreContentAdaptWithSize.call(this, ignore); + } + } +}); + +/** + * create a rich text + * @returns {RichText} + */ +ccui.RichText.create = function(){ + var richText = new ccui.RichText(); + richText.init(); + return richText; +}; \ No newline at end of file diff --git a/extensions/cocostudio/action/CCActionNode.js b/extensions/cocostudio/action/CCActionNode.js index 52a7ea5d58..292576b6d8 100644 --- a/extensions/cocostudio/action/CCActionNode.js +++ b/extensions/cocostudio/action/CCActionNode.js @@ -200,7 +200,7 @@ ccs.ActionNode = ccs.Class.extend({ } var frameType = frame.frameType; var array = this._frameArray[frameType]; - array[index] = frame; + array.splice(index, 0, frame); }, /** diff --git a/moduleConfig.json b/moduleConfig.json index 825fa5fb17..5c301f4cbf 100644 --- a/moduleConfig.json +++ b/moduleConfig.json @@ -233,6 +233,7 @@ "extensions/ccui/uiwidgets/UITextAtlas.js", "extensions/ccui/uiwidgets/UITextBMFont.js", "extensions/ccui/uiwidgets/UITextField.js", + "extensions/ccui/uiwidgets/UIRichText.js", "extensions/ccui/uiwidgets/scroll-widget/UIScrollView.js", "extensions/ccui/uiwidgets/scroll-widget/UIListView.js", "extensions/ccui/uiwidgets/scroll-widget/UIPageView.js" From 5b77a72342b2ee247c9da056e0f577b5ff94de00 Mon Sep 17 00:00:00 2001 From: xingsenma Date: Fri, 7 Mar 2014 15:46:38 +0800 Subject: [PATCH 2/6] issue #4221 Refactor flipped --- extensions/ccui/base-classes/UIWidget.js | 37 ++++++++++++++- extensions/ccui/uiwidgets/UIButton.js | 58 +++++++++++++++--------- extensions/ccui/uiwidgets/UICheckBox.js | 44 +++++++++--------- extensions/ccui/uiwidgets/UIImageView.js | 48 +++++--------------- extensions/ccui/uiwidgets/UIText.js | 57 ++--------------------- 5 files changed, 110 insertions(+), 134 deletions(-) diff --git a/extensions/ccui/base-classes/UIWidget.js b/extensions/ccui/base-classes/UIWidget.js index 273e7e99ae..cd0502a963 100644 --- a/extensions/ccui/base-classes/UIWidget.js +++ b/extensions/ccui/base-classes/UIWidget.js @@ -106,6 +106,8 @@ ccui.Widget = ccui.Node.extend(/** @lends ccui.Widget# */{ _nodes: null, _touchListener : null, _color:null, + _flippedX: false, + _flippedY: false, ctor: function () { cc.Node.prototype.ctor.call(this); this._enabled = true; @@ -138,6 +140,8 @@ ccui.Widget = ccui.Node.extend(/** @lends ccui.Widget# */{ this._nodes = []; this._color = cc.color(255,255,255,255); this._touchListener = null; + this._flippedX = false; + this._flippedY = false; }, /** @@ -1151,17 +1155,46 @@ ccui.Widget = ccui.Node.extend(/** @lends ccui.Widget# */{ return this.positionType; }, + /** + * Set flipped x + * @param {Boolean} flipX + */ setFlippedX: function (flipX) { + this._flippedX = flipX; + this.updateFlippedX(); }, + /** + * Get flipped x + * @returns {Boolean} + */ isFlippedX: function () { - return false; + return this._flippedX; }, + /** + * Set flipped y + * @param {Boolean} flipY + */ setFlippedY: function (flipY) { + this._flippedY = flipY; + this.updateFlippedY(); }, + + /** + * Get flipped y + * @returns {Boolean} + */ isFlippedY: function () { - return false; + return this._flippedY; + }, + + updateFlippedX:function(){ + + }, + + updateFlippedY:function(){ + }, /** diff --git a/extensions/ccui/uiwidgets/UIButton.js b/extensions/ccui/uiwidgets/UIButton.js index cc5975e022..b7a76fdd6c 100644 --- a/extensions/ccui/uiwidgets/UIButton.js +++ b/extensions/ccui/uiwidgets/UIButton.js @@ -240,7 +240,8 @@ ccui.Button = ccui.Widget.extend(/** @lends ccui.Button# */{ this.updateColorToRenderer(buttonNormalRenderer); this.updateAnchorPoint(); - + this.updateFlippedX(); + this.updateFlippedY(); this.normalTextureScaleChangedWithSize(); this._normalTextureLoaded = true; }, @@ -288,6 +289,8 @@ ccui.Button = ccui.Widget.extend(/** @lends ccui.Button# */{ } this.updateColorToRenderer(clickedRenderer); this.updateAnchorPoint(); + this.updateFlippedX(); + this.updateFlippedY(); this.pressedTextureScaleChangedWithSize(); this._pressedTextureLoaded = true; }, @@ -335,6 +338,8 @@ ccui.Button = ccui.Widget.extend(/** @lends ccui.Button# */{ } this.updateColorToRenderer(disableRenderer); this.updateAnchorPoint(); + this.updateFlippedX(); + this.updateFlippedY(); this.disabledTextureScaleChangedWithSize(); this._disabledTextureLoaded = true; }, @@ -464,32 +469,43 @@ ccui.Button = ccui.Widget.extend(/** @lends ccui.Button# */{ this._buttonClickedRenderer.setScale(this._pressedTextureScaleXInSize, this._pressedTextureScaleYInSize); }, - /** - * override "setFlippedX" of widget. - * @param {Boolean} flipX - */ - setFlippedX: function (flipX) { - this._titleRenderer.setFlippedX(flipX); + updateFlippedX: function () { + this._titleRenderer.setFlippedX(this._flippedX); if (this._scale9Enabled) { - return; + if (this._flippedX) { + this._buttonNormalRenderer.setScaleX(-1); + this._buttonClickedRenderer.setScaleX(-1); + this._buttonDisableRenderer.setScaleX(-1); + } + else { + this._buttonNormalRenderer.setScaleX(1); + this._buttonClickedRenderer.setScaleX(1); + this._buttonDisableRenderer.setScaleX(1); + } + } else { + this._buttonNormalRenderer.setFlippedX(this._flippedX); + this._buttonClickedRenderer.setFlippedX(this._flippedX); + this._buttonDisableRenderer.setFlippedX(this._flippedX); } - this._buttonNormalRenderer.setFlippedX(flipX); - this._buttonClickedRenderer.setFlippedX(flipX); - this._buttonDisableRenderer.setFlippedX(flipX); }, - /** - * override "setFlippedY" of widget. - * @param {Boolean} flipY - */ - setFlippedY: function (flipY) { - this._titleRenderer.setFlippedY(flipY); + updateFlippedY: function () { + this._titleRenderer.setFlippedY(this._flippedY); if (this._scale9Enabled) { - return; + if (this._flippedX) { + this._buttonNormalRenderer.setScaleY(-1); + this._buttonClickedRenderer.setScaleX(-1); + this._buttonDisableRenderer.setScaleX(-1); + } + else { + this._buttonNormalRenderer.setScaleY(1); + this._buttonClickedRenderer.setScaleY(1); + this._buttonDisableRenderer.setScaleY(1); + } } - this._buttonNormalRenderer.setFlippedY(flipY); - this._buttonClickedRenderer.setFlippedY(flipY); - this._buttonDisableRenderer.setFlippedY(flipY); + this._buttonNormalRenderer.setFlippedY(this._flippedY); + this._buttonClickedRenderer.setFlippedY(this._flippedY); + this._buttonDisableRenderer.setFlippedY(this._flippedY); }, /** diff --git a/extensions/ccui/uiwidgets/UICheckBox.js b/extensions/ccui/uiwidgets/UICheckBox.js index e429d40732..81c0c82f3b 100644 --- a/extensions/ccui/uiwidgets/UICheckBox.js +++ b/extensions/ccui/uiwidgets/UICheckBox.js @@ -143,7 +143,8 @@ ccui.CheckBox = ccui.Widget.extend(/** @lends ccui.CheckBox# */{ this.updateColorToRenderer(bgBoxRenderer); this.updateAnchorPoint(); - + this.updateFlippedX(); + this.updateFlippedY(); if(!bgBoxRenderer.textureLoaded()){ this._backGroundBoxRenderer.setContentSize(this._customSize); bgBoxRenderer.addLoadedEventListener(function(){ @@ -176,7 +177,8 @@ ccui.CheckBox = ccui.Widget.extend(/** @lends ccui.CheckBox# */{ } this.updateColorToRenderer(this._backGroundSelectedBoxRenderer); this.updateAnchorPoint(); - + this.updateFlippedX(); + this.updateFlippedY(); this.backGroundSelectedTextureScaleChangedWithSize(); }, @@ -204,6 +206,8 @@ ccui.CheckBox = ccui.Widget.extend(/** @lends ccui.CheckBox# */{ } this.updateColorToRenderer(this._frontCrossRenderer); this.updateAnchorPoint(); + this.updateFlippedX(); + this.updateFlippedY(); this.frontCrossTextureScaleChangedWithSize(); }, @@ -231,6 +235,8 @@ ccui.CheckBox = ccui.Widget.extend(/** @lends ccui.CheckBox# */{ } this.updateColorToRenderer(this._backGroundBoxDisabledRenderer); this.updateAnchorPoint(); + this.updateFlippedX(); + this.updateFlippedY(); this.backGroundDisabledTextureScaleChangedWithSize(); }, @@ -258,6 +264,8 @@ ccui.CheckBox = ccui.Widget.extend(/** @lends ccui.CheckBox# */{ } this.updateColorToRenderer(this._frontCrossDisabledRenderer); this.updateAnchorPoint(); + this.updateFlippedX(); + this.updateFlippedY(); this.frontCrossDisabledTextureScaleChangedWithSize(); }, @@ -341,28 +349,20 @@ ccui.CheckBox = ccui.Widget.extend(/** @lends ccui.CheckBox# */{ this._checkBoxEventListener = target; }, - /** - * Sets whether the widget should be flipped horizontally or not. - * @param {Boolean} flipX - */ - setFlippedX: function (flipX) { - this._backGroundBoxRenderer.setFlippedX(flipX); - this._backGroundSelectedBoxRenderer.setFlippedX(flipX); - this._frontCrossRenderer.setFlippedX(flipX); - this._backGroundBoxDisabledRenderer.setFlippedX(flipX); - this._frontCrossDisabledRenderer.setFlippedX(flipX); + updateFlippedX: function () { + this._backGroundBoxRenderer.setFlippedX(this._flippedX); + this._backGroundSelectedBoxRenderer.setFlippedX(this._flippedX); + this._frontCrossRenderer.setFlippedX(this._flippedX); + this._backGroundBoxDisabledRenderer.setFlippedX(this._flippedX); + this._frontCrossDisabledRenderer.setFlippedX(this._flippedX); }, - /** - * override "setFlippedY" of widget. - * @param {Boolean} flipY - */ - setFlippedY: function (flipY) { - this._backGroundBoxRenderer.setFlippedY(flipY); - this._backGroundSelectedBoxRenderer.setFlippedY(flipY); - this._frontCrossRenderer.setFlippedY(flipY); - this._backGroundBoxDisabledRenderer.setFlippedY(flipY); - this._frontCrossDisabledRenderer.setFlippedY(flipY); + updateFlippedY: function () { + this._backGroundBoxRenderer.setFlippedY(this._flippedY); + this._backGroundSelectedBoxRenderer.setFlippedY(this._flippedY); + this._frontCrossRenderer.setFlippedY(this._flippedY); + this._backGroundBoxDisabledRenderer.setFlippedY(this._flippedY); + this._frontCrossDisabledRenderer.setFlippedY(this._flippedY); }, /** diff --git a/extensions/ccui/uiwidgets/UIImageView.js b/extensions/ccui/uiwidgets/UIImageView.js index 61888fc058..18f63a32f3 100644 --- a/extensions/ccui/uiwidgets/UIImageView.js +++ b/extensions/ccui/uiwidgets/UIImageView.js @@ -99,6 +99,8 @@ ccui.ImageView = ccui.Widget.extend(/** @lends ccui.ImageView# */{ this.updateColorToRenderer(imageRenderer); this.updateAnchorPoint(); + this.updateFlippedX(); + this.updateFlippedY(); this.imageTextureScaleChangedWithSize(); }, @@ -116,48 +118,22 @@ ccui.ImageView = ccui.Widget.extend(/** @lends ccui.ImageView# */{ } }, - /** - * Sets whether the widget should be flipped horizontally or not. - * @param {Boolean} flipX - */ - setFlippedX: function (flipX) { - if (!this._scale9Enabled) { - this._imageRenderer.setFlippedX(flipX); + updateFlippedX: function () { + if (this._scale9Enabled) { + this._imageRenderer.setScaleX(this._flippedX ? -1 : 1); + } else { + this._imageRenderer.setFlippedX(this._flippedX); } }, - /** - * override "setFlippedY" of widget. - * @param {Boolean} flipY - */ - setFlippedY: function (flipY) { - if (!this._scale9Enabled) { - this._imageRenderer.setFlippedY(flipY); + updateFlippedY: function () { + if (this._scale9Enabled) { + this._imageRenderer.setScaleY(this._flippedY ? -1 : 1); + } else { + this._imageRenderer.setFlippedY(this._flippedY); } }, - /** - * override "isFlippedX" of widget. - * @returns {Boolean} - */ - isFlippedX: function () { - if (this._scale9Enabled) - return false; - else - return this._imageRenderer.isFlippedX(); - }, - - /** - * override "isFlippedY" of widget. - * @returns {Boolean} - */ - isFlippedY: function () { - if (this._scale9Enabled) - return false; - else - return this._imageRenderer.isFlippedY(); - }, - /** * Sets if button is using scale9 renderer. * @param {Boolean} able diff --git a/extensions/ccui/uiwidgets/UIText.js b/extensions/ccui/uiwidgets/UIText.js index d4529a6b29..3e5827bca9 100644 --- a/extensions/ccui/uiwidgets/UIText.js +++ b/extensions/ccui/uiwidgets/UIText.js @@ -258,62 +258,13 @@ ccui.Text = ccui.Widget.extend(/** @lends ccui.Text# */{ }, - /** - * set scale - * @param {Number} scale - */ - setScale: function (scale) { - ccui.Widget.prototype.setScale.call(this, scale); - }, - - /** - * set scaleX - * @param {Number} scaleX - */ - setScaleX: function (scaleX) { - ccui.Widget.prototype.setScaleX.call(this, scaleX); - this._normalScaleValueX = scaleX; - }, - /** - * set scaleY - * @param {Number} scaleY - */ - setScaleY: function (scaleY) { - ccui.Widget.prototype.setScaleY.call(this, scaleY); - this._normalScaleValueY = scaleY; - }, - - /** - * override "setFlippedX" of widget. - * @param {Boolean} flipX - */ - setFlippedX: function (flipX) { - this._labelRenderer.setFlippedX(flipX); + updateFlippedX: function () { + this._labelRenderer.setFlippedX(this._flippedX); }, - /** - * override "setFlippedY" of widget. - * @param {Boolean} flipY - */ - setFlippedY: function (flipY) { - this._labelRenderer.setFlippedY(flipY); - }, - - /** - * override "isFlippedX" of widget. - * @returns {Boolean} - */ - isFlippedX: function () { - return this._labelRenderer.isFlippedX(); - }, - - /** - * override "isFlippedY" of widget. - * @returns {Boolean} - */ - isFlippedY: function () { - return this._labelRenderer.isFlippedY(); + updateFlippedY: function () { + this._labelRenderer.setFlippedY(this._flippedY); }, /** From 5f29a2b3a5c39a1aef7105c89cf7dccbcfa666e2 Mon Sep 17 00:00:00 2001 From: xingsenma Date: Fri, 7 Mar 2014 16:01:08 +0800 Subject: [PATCH 3/6] issue #4221 Refactor setBackGroundImageColor for Layout --- extensions/ccui/layouts/UILayout.js | 50 +++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/extensions/ccui/layouts/UILayout.js b/extensions/ccui/layouts/UILayout.js index 449a513b87..503fd7cd9c 100644 --- a/extensions/ccui/layouts/UILayout.js +++ b/extensions/ccui/layouts/UILayout.js @@ -74,6 +74,7 @@ ccui.Layout = ccui.Widget.extend(/** @lends ccui.Layout# */{ _scissorRectDirty: false, _clippingRect: null, _clippingParent: null, + _backGroundImageColor:null, ctor: function () { ccui.Widget.prototype.ctor.call(this); this._clippingEnabled = false; @@ -101,6 +102,7 @@ ccui.Layout = ccui.Widget.extend(/** @lends ccui.Layout# */{ this._scissorRectDirty = false; this._clippingRect = cc.rect(0, 0, 0, 0); this._clippingParent = null; + this._backGroundImageColor = cc.color(255, 255, 255, 255); }, init: function () { if (cc.Node.prototype.init.call(this)){ @@ -678,10 +680,9 @@ ccui.Layout = ccui.Widget.extend(/** @lends ccui.Layout# */{ if (this._backGroundScale9Enabled) { this._backGroundImage.setPreferredSize(this._size); } - this._backGroundImage.setColor(this.getColor()); - this._backGroundImage.setOpacity(this.getOpacity()); this._backGroundImageTextureSize = this._backGroundImage.getContentSize(); this._backGroundImage.setPosition(this._size.width / 2.0, this._size.height / 2.0); + this.updateBackGroundImageRGBA(); }, /** @@ -923,6 +924,51 @@ ccui.Layout = ccui.Widget.extend(/** @lends ccui.Layout# */{ return this._alongVector; }, + /** + * Set backGround image color + * @param {cc.Color} color + */ + setBackGroundImageColor: function (color) { + this._backGroundImageColor.r = color.r; + this._backGroundImageColor.g = color.g; + this._backGroundImageColor.b = color.b; + + this.updateBackGroundImageColor(); + if (color.a !== undefined && !color.a_undefined) { + this.setBackGroundImageOpacity(color.a); + } + }, + + /** + * Get backGround image color + * @param {Number} opacity + */ + setBackGroundImageOpacity: function (opacity) { + this._backGroundImageColor.a = color.a; + this.getBackGroundImageColor(); + }, + + /** + * Get backGround image color + * @returns {cc.Color} + */ + getBackGroundImageColor: function () { + var color = this._backGroundImageColor; + return cc.color(color.r, color.g, color.b, color.a); + }, + + /** + * Get backGround image opacity + * @returns {Number} + */ + getBackGroundImageOpacity: function () { + return this._backGroundImageColor.a; + }, + + updateBackGroundImageColor: function () { + this._backGroundImage.setColor(this._backGroundImageColor); + }, + /** * Gets background image texture size. * @returns {cc.Size} From 645564b8f85d962806056c33e43d94e79ca4942c Mon Sep 17 00:00:00 2001 From: xingsenma Date: Fri, 7 Mar 2014 16:03:56 +0800 Subject: [PATCH 4/6] issue #4221 add getCustomSize method for widget --- extensions/ccui/base-classes/UIWidget.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/extensions/ccui/base-classes/UIWidget.js b/extensions/ccui/base-classes/UIWidget.js index cd0502a963..605d2e9109 100644 --- a/extensions/ccui/base-classes/UIWidget.js +++ b/extensions/ccui/base-classes/UIWidget.js @@ -650,6 +650,14 @@ ccui.Widget = ccui.Node.extend(/** @lends ccui.Widget# */{ return this._size; }, + /** + * Get custom size + * @returns {cc.Size} + */ + getCustomSize:function(){ + return this._customSize + }, + /** * Returns size percent of widget * @returns {cc.Point} From 2b814adf74aa85e63305343b41dcf10257408727 Mon Sep 17 00:00:00 2001 From: xingsenma Date: Fri, 7 Mar 2014 17:14:53 +0800 Subject: [PATCH 5/6] issue #4221 Set default enabled touch for widget --- extensions/ccui/uiwidgets/UIButton.js | 4 +++- extensions/ccui/uiwidgets/UICheckBox.js | 1 + extensions/ccui/uiwidgets/UISlider.js | 8 ++++++++ extensions/ccui/uiwidgets/UITextField.js | 7 +++++++ 4 files changed, 19 insertions(+), 1 deletion(-) diff --git a/extensions/ccui/uiwidgets/UIButton.js b/extensions/ccui/uiwidgets/UIButton.js index b7a76fdd6c..207fd920ff 100644 --- a/extensions/ccui/uiwidgets/UIButton.js +++ b/extensions/ccui/uiwidgets/UIButton.js @@ -101,8 +101,10 @@ ccui.Button = ccui.Widget.extend(/** @lends ccui.Button# */{ }, init: function () { - if (ccui.Widget.prototype.init.call(this)) + if (ccui.Widget.prototype.init.call(this)){ + this.setTouchEnabled(true); return true; + } return false; }, diff --git a/extensions/ccui/uiwidgets/UICheckBox.js b/extensions/ccui/uiwidgets/UICheckBox.js index 81c0c82f3b..afaad94c69 100644 --- a/extensions/ccui/uiwidgets/UICheckBox.js +++ b/extensions/ccui/uiwidgets/UICheckBox.js @@ -81,6 +81,7 @@ ccui.CheckBox = ccui.Widget.extend(/** @lends ccui.CheckBox# */{ }, init: function () { if (ccui.Widget.prototype.init.call(this)) { + this.setTouchEnabled(true); this.setSelectedState(false); return true; } diff --git a/extensions/ccui/uiwidgets/UISlider.js b/extensions/ccui/uiwidgets/UISlider.js index f8799b360b..f281020e67 100644 --- a/extensions/ccui/uiwidgets/UISlider.js +++ b/extensions/ccui/uiwidgets/UISlider.js @@ -94,6 +94,14 @@ ccui.Slider = ccui.Widget.extend(/** @lends ccui.Slider# */{ this._isTextureLoaded = false; }, + init:function(){ + if(ccui.Widget.prototype.init.call(this)){ + this.setTouchEnabled(true); + return true; + } + return false; + }, + initRenderer: function () { this._barRenderer = cc.Sprite.create(); this._progressBarRenderer = cc.Sprite.create(); diff --git a/extensions/ccui/uiwidgets/UITextField.js b/extensions/ccui/uiwidgets/UITextField.js index 3a10bbf63a..ac5a6e68a6 100644 --- a/extensions/ccui/uiwidgets/UITextField.js +++ b/extensions/ccui/uiwidgets/UITextField.js @@ -58,6 +58,13 @@ ccui.UICCTextField = cc.TextFieldTTF.extend({ this._insertText = false; this._deleteBackward = false; }, + init:function(){ + if(ccui.Widget.prototype.init.call(this)){ + this.setTouchEnabled(true); + return true; + } + return false; + }, onEnter: function () { cc.TextFieldTTF.prototype.onEnter.call(this); cc.TextFieldTTF.prototype.setDelegate.call(this,this); From 5e16a83dc9dc4f726d0b68232141782ba8012805 Mon Sep 17 00:00:00 2001 From: xingsenma Date: Fri, 7 Mar 2014 18:09:57 +0800 Subject: [PATCH 6/6] issue #4221 Fixed some bug --- extensions/ccui/layouts/UILayout.js | 2 +- extensions/ccui/uiwidgets/UIButton.js | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/extensions/ccui/layouts/UILayout.js b/extensions/ccui/layouts/UILayout.js index 503fd7cd9c..1490eb8543 100644 --- a/extensions/ccui/layouts/UILayout.js +++ b/extensions/ccui/layouts/UILayout.js @@ -682,7 +682,7 @@ ccui.Layout = ccui.Widget.extend(/** @lends ccui.Layout# */{ } this._backGroundImageTextureSize = this._backGroundImage.getContentSize(); this._backGroundImage.setPosition(this._size.width / 2.0, this._size.height / 2.0); - this.updateBackGroundImageRGBA(); + this.updateBackGroundImageColor(); }, /** diff --git a/extensions/ccui/uiwidgets/UIButton.js b/extensions/ccui/uiwidgets/UIButton.js index 207fd920ff..4bb7dbd56e 100644 --- a/extensions/ccui/uiwidgets/UIButton.js +++ b/extensions/ccui/uiwidgets/UIButton.js @@ -504,10 +504,11 @@ ccui.Button = ccui.Widget.extend(/** @lends ccui.Button# */{ this._buttonClickedRenderer.setScaleY(1); this._buttonDisableRenderer.setScaleY(1); } + }else{ + this._buttonNormalRenderer.setFlippedY(this._flippedY); + this._buttonClickedRenderer.setFlippedY(this._flippedY); + this._buttonDisableRenderer.setFlippedY(this._flippedY); } - this._buttonNormalRenderer.setFlippedY(this._flippedY); - this._buttonClickedRenderer.setFlippedY(this._flippedY); - this._buttonDisableRenderer.setFlippedY(this._flippedY); }, /**