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 
 25 /**
 26  * Base class for ccui.Button
 27  * @class
 28  * @extends ccui.Widget
 29  */
 30 ccui.ImageView = ccui.Widget.extend(/** @lends ccui.ImageView# */{
 31     _scale9Enabled: false,
 32     _prevIgnoreSize: true,
 33     _capInsets: null,
 34     _imageRenderer: null,
 35     _textureFile: "",
 36     _imageTexType: null,
 37     _imageTextureSize: null,
 38     _className:"ImageView",
 39     ctor: function () {
 40         ccui.Widget.prototype.ctor.call(this);
 41         this._scale9Enabled = false;
 42         this._prevIgnoreSize = true;
 43         this._capInsets = cc.rect(0,0,0,0);
 44         this._imageRenderer = null;
 45         this._textureFile = "";
 46         this._imageTexType = ccui.Widget.LOCAL_TEXTURE;
 47         this._imageTextureSize = cc.size(this._size.width, this._size.height);
 48     },
 49 
 50     initRenderer: function () {
 51         this._imageRenderer = cc.Sprite.create();
 52         cc.Node.prototype.addChild.call(this, this._imageRenderer, ccui.ImageView.RENDERER_ZORDER, -1);
 53     },
 54 
 55     /**
 56      * Load textures for button.
 57      * @param {String} fileName
 58      * @param {ccui.Widget.LOCAL_TEXTURE|ccui.Widget.PLIST_TEXTURE} texType
 59      */
 60     loadTexture: function (fileName, texType) {
 61         if (!fileName) {
 62             return;
 63         }
 64         texType = texType || ccui.Widget.LOCAL_TEXTURE;
 65         this._textureFile = fileName;
 66         this._imageTexType = texType;
 67         var imageRenderer = this._imageRenderer
 68         switch (this._imageTexType) {
 69             case ccui.Widget.LOCAL_TEXTURE:
 70                 imageRenderer.initWithFile(fileName);
 71                 break;
 72             case ccui.Widget.PLIST_TEXTURE:
 73                 imageRenderer.initWithSpriteFrameName(fileName);
 74                 break;
 75             default:
 76                 break;
 77         }
 78 
 79         var locRendererSize = imageRenderer.getContentSize();
 80         if(imageRenderer.textureLoaded()){
 81             this._imageTextureSize.width = locRendererSize.width;
 82             this._imageTextureSize.height = locRendererSize.height;
 83         }else{
 84             imageRenderer.addLoadedEventListener(function(){
 85                 var locSize = imageRenderer.getContentSize();
 86                 this._imageTextureSize.width = locSize.width;
 87                 this._imageTextureSize.height = locSize.height;
 88                 if (imageRenderer.setCapInsets) {
 89                     imageRenderer.setCapInsets(this._capInsets);
 90                 }
 91                 this.imageTextureScaleChangedWithSize();
 92             },this);
 93             this._imageTextureSize.width = this._customSize.width;
 94             this._imageTextureSize.height = this._customSize.height;
 95         }
 96 
 97         if (this._scale9Enabled) {
 98             imageRenderer.setCapInsets(this._capInsets);
 99         }
100 
101         this.updateColorToRenderer(imageRenderer);
102         this.updateAnchorPoint();
103         this.updateFlippedX();
104         this.updateFlippedY();
105         this.imageTextureScaleChangedWithSize();
106     },
107 
108     /**
109      * set texture rect
110      * @param {cc.Rect} rect
111      */
112     setTextureRect: function (rect) {
113         if (!this._scale9Enabled){
114             this._imageRenderer.setTextureRect(rect);
115             var locRendererSize = this._imageRenderer.getContentSize();
116             this._imageTextureSize.width = locRendererSize.width;
117             this._imageTextureSize.height = locRendererSize.height;
118             this.imageTextureScaleChangedWithSize();
119         }
120     },
121 
122     updateFlippedX: function () {
123         if (this._scale9Enabled) {
124             this._imageRenderer.setScaleX(this._flippedX ? -1 : 1);
125         } else {
126             this._imageRenderer.setFlippedX(this._flippedX);
127         }
128     },
129 
130     updateFlippedY: function () {
131         if (this._scale9Enabled) {
132             this._imageRenderer.setScaleY(this._flippedY ? -1 : 1);
133         } else {
134             this._imageRenderer.setFlippedY(this._flippedY);
135         }
136     },
137 
138     /**
139      * Sets if button is using scale9 renderer.
140      * @param {Boolean} able
141      */
142     setScale9Enabled: function (able) {
143         if (this._scale9Enabled == able) {
144             return;
145         }
146 
147 
148         this._scale9Enabled = able;
149         cc.Node.prototype.removeChild.call(this, this._imageRenderer, true);
150         this._imageRenderer = null;
151         if (this._scale9Enabled) {
152             this._imageRenderer = cc.Scale9Sprite.create();
153         }
154         else {
155             this._imageRenderer = cc.Sprite.create();
156         }
157         this.loadTexture(this._textureFile, this._imageTexType);
158         cc.Node.prototype.addChild.call(this, this._imageRenderer, ccui.ImageView.RENDERER_ZORDER, -1);
159         if (this._scale9Enabled) {
160             var ignoreBefore = this._ignoreSize;
161             this.ignoreContentAdaptWithSize(false);
162             this._prevIgnoreSize = ignoreBefore;
163         }
164         else {
165             this.ignoreContentAdaptWithSize(this._prevIgnoreSize);
166         }
167         this.setCapInsets(this._capInsets);
168     },
169 
170     /**
171      * Get  button is using scale9 renderer or not.
172      * @returns {Boolean}
173      */
174     isScale9Enabled:function(){
175         return this._scale9Enabled;
176     },
177 
178     /**
179      * ignoreContentAdaptWithSize
180      * @param {Boolean} ignore
181      */
182     ignoreContentAdaptWithSize: function (ignore) {
183         if (!this._scale9Enabled || (this._scale9Enabled && !ignore)) {
184             ccui.Widget.prototype.ignoreContentAdaptWithSize.call(this, ignore);
185             this._prevIgnoreSize = ignore;
186         }
187     },
188 
189     /**
190      * Sets capinsets for button, if button is using scale9 renderer.
191      * @param {cc.Rect} capInsets
192      */
193     setCapInsets: function (capInsets) {
194         this._capInsets = capInsets;
195         if (!this._scale9Enabled) {
196             return;
197         }
198         this._imageRenderer.setCapInsets(capInsets);
199     },
200 
201     /**
202      * Get cap insets.
203      * @returns {cc.Rect}
204      */
205     getCapInsets:function(){
206         return this._capInsets;
207     },
208 
209     /**
210      * override "setAnchorPoint" of widget.
211      * @param {cc.Point|Number} point The anchor point of UIImageView or The anchor point.x of UIImageView.
212      * @param {Number} [y] The anchor point.y of UIImageView.
213      */
214     setAnchorPoint: function (point, y) {
215         if(y === undefined){
216 	        ccui.Widget.prototype.setAnchorPoint.call(this, point);
217 	        this._imageRenderer.setAnchorPoint(point);
218         } else {
219 	        ccui.Widget.prototype.setAnchorPoint.call(this, point, y);
220 	        this._imageRenderer.setAnchorPoint(point, y);
221         }
222     },
223 	_setAnchorX: function (value) {
224 		ccui.Widget.prototype._setAnchorX.call(this, value);
225 		this._imageRenderer._setAnchorX(value);
226 	},
227 	_setAnchorY: function (value) {
228 		ccui.Widget.prototype._setAnchorY.call(this, value);
229 		this._imageRenderer._setAnchorY(value);
230 	},
231 
232 
233     onSizeChanged: function () {
234         ccui.Widget.prototype.onSizeChanged.call(this);
235         this.imageTextureScaleChangedWithSize();
236     },
237 
238     /**
239      * override "getContentSize" method of widget.
240      * @returns {cc.Size}
241      */
242     getContentSize: function () {
243         return this._imageTextureSize;
244     },
245 	_getWidth: function () {
246 		return this._imageTextureSize.width;
247 	},
248 	_getHeight: function () {
249 		return this._imageTextureSize.height;
250 	},
251 
252     /**
253      * override "getVirtualRenderer" method of widget.
254      * @returns {cc.Node}
255      */
256     getVirtualRenderer: function () {
257         return this._imageRenderer;
258     },
259 
260     imageTextureScaleChangedWithSize: function () {
261         if (this._ignoreSize) {
262             if (!this._scale9Enabled) {
263                 this._imageRenderer.setScale(1.0);
264                 this._size = this._imageTextureSize;
265             }
266         }
267         else {
268             if (this._scale9Enabled) {
269                 this._imageRenderer.setPreferredSize(this._size);
270             }
271             else {
272                 var textureSize = this._imageRenderer.getContentSize();
273                 if (textureSize.width <= 0.0 || textureSize.height <= 0.0) {
274                     this._imageRenderer.setScale(1.0);
275                     return;
276                 }
277                 var scaleX = this._size.width / textureSize.width; var scaleY = this._size.height / textureSize.height;
278                 this._imageRenderer.setScaleX(scaleX);
279                 this._imageRenderer.setScaleY(scaleY);
280             }
281         }
282     },
283 
284     updateTextureColor: function () {
285         this.updateColorToRenderer(this._imageRenderer);
286     },
287 
288     updateTextureOpacity: function () {
289         this.updateOpacityToRenderer(this._imageRenderer);
290     },
291 
292     /**
293      * Returns the "class name" of widget.
294      * @returns {string}
295      */
296     getDescription: function () {
297         return "ImageView";
298     },
299 
300     createCloneInstance:function(){
301         return ccui.ImageView.create();
302     },
303 
304     copySpecialProperties: function (imageView) {
305         this._prevIgnoreSize = imageView._prevIgnoreSize;
306         this.setScale9Enabled(imageView._scale9Enabled);
307         this.loadTexture(imageView._textureFile, imageView._imageTexType);
308         this.setCapInsets(imageView._capInsets);
309     }
310 
311 });
312 
313 /**
314  * allocates and initializes a UIImageView.
315  * @constructs
316  * @return {ccui.ImageView}
317  * @example
318  * // example
319  * var uiImageView = ccui.ImageView.create();
320  */
321 ccui.ImageView.create = function () {
322     var uiImageView = new ccui.ImageView();
323     if (uiImageView && uiImageView.init()) {
324         return uiImageView;
325     }
326     return null;
327 };
328 
329 // Constants
330 ccui.ImageView.RENDERER_ZORDER = -1;