1 /**************************************************************************** 2 Copyright (c) 2010-2012 cocos2d-x.org 3 4 http://www.cocos2d-x.org 5 6 Permission is hereby granted, free of charge, to any person obtaining a copy 7 of this software and associated documentation files (the "Software"), to deal 8 in the Software without restriction, including without limitation the rights 9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 copies of the Software, and to permit persons to whom the Software is 11 furnished to do so, subject to the following conditions: 12 13 The above copyright notice and this permission notice shall be included in 14 all copies or substantial portions of the Software. 15 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 THE SOFTWARE. 23 ****************************************************************************/ 24 ccs.IMAGERENDERERZ = -1; 25 /** 26 * Base class for ccs.Button 27 * @class 28 * @extends ccs.Widget 29 */ 30 ccs.ImageView = ccs.Widget.extend(/** @lends ccs.ImageView# */{ 31 _scale9Enabled: false, 32 _prevIgnoreSize: true, 33 _capInsets: null, 34 _imageRenderer: null, 35 _textureFile: "", 36 _imageTexType: null, 37 _imageTextureSize: null, 38 ctor: function () { 39 ccs.Widget.prototype.ctor.call(this); 40 this._scale9Enabled = false; 41 this._prevIgnoreSize = true; 42 this._capInsets = cc.rect(0,0,0,0); 43 this._imageRenderer = null; 44 this._textureFile = ""; 45 this._imageTexType = ccs.TextureResType.local; 46 this._imageTextureSize = cc.size(this._size.width, this._size.height); 47 }, 48 49 initRenderer: function () { 50 this._imageRenderer = cc.Sprite.create(); 51 cc.Node.prototype.addChild.call(this, this._imageRenderer, ccs.IMAGERENDERERZ, -1); 52 }, 53 54 /** 55 * Load textures for button. 56 * @param {String} fileName 57 * @param {ccs.TextureResType} texType 58 */ 59 loadTexture: function (fileName, texType) { 60 if (!fileName) { 61 return; 62 } 63 texType = texType || ccs.TextureResType.local; 64 this._textureFile = fileName; 65 this._imageTexType = texType; 66 var imageRenderer = this._imageRenderer 67 switch (this._imageTexType) { 68 case ccs.TextureResType.local: 69 imageRenderer.initWithFile(fileName); 70 break; 71 case ccs.TextureResType.plist: 72 imageRenderer.initWithSpriteFrameName(fileName); 73 break; 74 default: 75 break; 76 } 77 78 var locRendererSize = imageRenderer.getContentSize(); 79 if(imageRenderer.textureLoaded()){ 80 this._imageTextureSize.width = locRendererSize.width; 81 this._imageTextureSize.height = locRendererSize.height; 82 }else{ 83 imageRenderer.addLoadedEventListener(function(){ 84 var locSize = imageRenderer.getContentSize(); 85 this._imageTextureSize.width = locSize.width; 86 this._imageTextureSize.height = locSize.height; 87 if (imageRenderer.setCapInsets) { 88 imageRenderer.setCapInsets(this._capInsets); 89 } 90 this.imageTextureScaleChangedWithSize(); 91 },this); 92 this._imageTextureSize.width = this._customSize.width; 93 this._imageTextureSize.height = this._customSize.height; 94 } 95 96 if (this._scale9Enabled) { 97 imageRenderer.setCapInsets(this._capInsets); 98 } 99 100 this.updateRGBAToRenderer(imageRenderer); 101 this.updateAnchorPoint(); 102 this.updateFlippedX(); 103 this.updateFlippedY(); 104 this.imageTextureScaleChangedWithSize(); 105 }, 106 107 /** 108 * set texture rect 109 * @param {cc.Rect} rect 110 */ 111 setTextureRect: function (rect) { 112 if (!this._scale9Enabled){ 113 this._imageRenderer.setTextureRect(rect); 114 var locRendererSize = this._imageRenderer.getContentSize(); 115 this._imageTextureSize.width = locRendererSize.width; 116 this._imageTextureSize.height = locRendererSize.height; 117 this.imageTextureScaleChangedWithSize(); 118 } 119 }, 120 121 updateFlippedX: function () { 122 if (this._scale9Enabled) { 123 this._imageRenderer.setScaleX(this._flippedX ? -1 : 1); 124 } else { 125 this._imageRenderer.setFlippedX(this._flippedX); 126 } 127 }, 128 129 updateFlippedY: function () { 130 if (this._scale9Enabled) { 131 this._imageRenderer.setScaleY(this._flippedY ? -1 : 1); 132 } else { 133 this._imageRenderer.setFlippedY(this._flippedY); 134 } 135 }, 136 137 /** 138 * Sets if button is using scale9 renderer. 139 * @param {Boolean} able 140 */ 141 setScale9Enabled: function (able) { 142 if (this._scale9Enabled == able) { 143 return; 144 } 145 146 147 this._scale9Enabled = able; 148 cc.Node.prototype.removeChild.call(this, this._imageRenderer, true); 149 this._imageRenderer = null; 150 if (this._scale9Enabled) { 151 this._imageRenderer = cc.Scale9Sprite.create(); 152 } 153 else { 154 this._imageRenderer = cc.Sprite.create(); 155 } 156 this.loadTexture(this._textureFile, this._imageTexType); 157 cc.Node.prototype.addChild.call(this, this._imageRenderer, ccs.IMAGERENDERERZ, -1); 158 if (this._scale9Enabled) { 159 var ignoreBefore = this._ignoreSize; 160 this.ignoreContentAdaptWithSize(false); 161 this._prevIgnoreSize = ignoreBefore; 162 } 163 else { 164 this.ignoreContentAdaptWithSize(this._prevIgnoreSize); 165 } 166 this.setCapInsets(this._capInsets); 167 }, 168 169 /** 170 * Get button is using scale9 renderer or not. 171 * @returns {Boolean} 172 */ 173 isScale9Enabled:function(){ 174 return this._scale9Enabled; 175 }, 176 177 /** 178 * ignoreContentAdaptWithSize 179 * @param {Boolean} ignore 180 */ 181 ignoreContentAdaptWithSize: function (ignore) { 182 if (!this._scale9Enabled || (this._scale9Enabled && !ignore)) { 183 ccs.Widget.prototype.ignoreContentAdaptWithSize.call(this, ignore); 184 this._prevIgnoreSize = ignore; 185 } 186 }, 187 188 /** 189 * Sets capinsets for imageView, if button is using scale9 renderer. 190 * @param {cc.Rect} capInsets 191 */ 192 setCapInsets: function (capInsets) { 193 this._capInsets = capInsets; 194 if (!this._scale9Enabled) { 195 return; 196 } 197 this._imageRenderer.setCapInsets(capInsets); 198 }, 199 200 /** 201 * Get cap insets. 202 * @returns {cc.Rect} 203 */ 204 getCapInsets:function(){ 205 return this._capInsets; 206 }, 207 208 /** 209 * override "setAnchorPoint" of widget. 210 * @param {cc.Point|Number} point The anchor point of UIImageView or The anchor point.x of UIImageView. 211 * @param {Number} [y] The anchor point.y of UIImageView. 212 */ 213 setAnchorPoint: function (point, y) { 214 if(y === undefined){ 215 ccs.Widget.prototype.setAnchorPoint.call(this, point); 216 this._imageRenderer.setAnchorPoint(point); 217 } else { 218 ccs.Widget.prototype.setAnchorPoint.call(this, point, y); 219 this._imageRenderer.setAnchorPoint(point, y); 220 } 221 }, 222 223 onSizeChanged: function () { 224 ccs.Widget.prototype.onSizeChanged.call(this); 225 this.imageTextureScaleChangedWithSize(); 226 }, 227 228 /** 229 * override "getContentSize" method of widget. 230 * @returns {cc.Size} 231 */ 232 getContentSize: function () { 233 return this._imageTextureSize; 234 }, 235 236 /** 237 * override "getVirtualRenderer" method of widget. 238 * @returns {cc.Node} 239 */ 240 getVirtualRenderer: function () { 241 return this._imageRenderer; 242 }, 243 244 imageTextureScaleChangedWithSize: function () { 245 if (this._ignoreSize) { 246 if (!this._scale9Enabled) { 247 this._imageRenderer.setScale(1.0); 248 this._size = this._imageTextureSize; 249 } 250 } 251 else { 252 if (this._scale9Enabled) { 253 this._imageRenderer.setPreferredSize(this._size); 254 } 255 else { 256 var textureSize = this._imageRenderer.getContentSize(); 257 if (textureSize.width <= 0.0 || textureSize.height <= 0.0) { 258 this._imageRenderer.setScale(1.0); 259 return; 260 } 261 var scaleX = this._size.width / textureSize.width; 262 var scaleY = this._size.height / textureSize.height; 263 this._imageRenderer.setScaleX(scaleX); 264 this._imageRenderer.setScaleY(scaleY); 265 } 266 } 267 }, 268 269 updateTextureColor: function () { 270 this.updateColorToRenderer(this._imageRenderer); 271 }, 272 273 updateTextureOpacity: function () { 274 this.updateOpacityToRenderer(this._imageRenderer); 275 }, 276 277 updateTextureRGBA: function () { 278 this.updateRGBAToRenderer(this._imageRenderer); 279 }, 280 281 /** 282 * Returns the "class name" of widget. 283 * @returns {string} 284 */ 285 getDescription: function () { 286 return "ImageView"; 287 }, 288 289 createCloneInstance:function(){ 290 return ccs.ImageView.create(); 291 }, 292 293 copySpecialProperties: function (imageView) { 294 this._prevIgnoreSize = imageView._prevIgnoreSize; 295 this.setScale9Enabled(imageView._scale9Enabled); 296 this.loadTexture(imageView._textureFile, imageView._imageTexType); 297 this.setCapInsets(imageView._capInsets); 298 } 299 300 }); 301 /** 302 * allocates and initializes a UIImageView. 303 * @constructs 304 * @return {ccs.ImageView} 305 * @example 306 * // example 307 * var uiImageView = ccs.ImageView.create(); 308 */ 309 ccs.ImageView.create = function () { 310 var uiImageView = new ccs.ImageView(); 311 if (uiImageView && uiImageView.init()) { 312 return uiImageView; 313 } 314 return null; 315 }; 316