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