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