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      * @constructor
 46      * @example
 47      * // example
 48      * var uiImageView = new ccui.ImageView;
 49      */
 50     ctor: function (imageFileName, texType) {
 51         this._capInsets = cc.rect(0,0,0,0);
 52         this._imageTextureSize = cc.size(this._capInsets.width, this._capInsets.height);
 53         ccui.Widget.prototype.ctor.call(this);
 54 
 55         texType && this.init(imageFileName, texType);
 56     },
 57 
 58     init: function(imageFileName, texType){
 59         if(ccui.Widget.prototype.init.call(this)){
 60             if(imageFileName === undefined)
 61                 this._imageTexType = ccui.Widget.LOCAL_TEXTURE;
 62             else
 63                 this.loadTexture(imageFileName, texType);
 64             return true;
 65         }
 66         return false;
 67     },
 68 
 69     _initRenderer: function () {
 70         this._imageRenderer = cc.Sprite.create();
 71         this.addProtectedChild(this._imageRenderer, ccui.ImageView.RENDERER_ZORDER, -1);
 72     },
 73 
 74     /**
 75      * Load textures for button.
 76      * @param {String} fileName
 77      * @param {ccui.Widget.LOCAL_TEXTURE|ccui.Widget.PLIST_TEXTURE} texType
 78      */
 79     loadTexture: function (fileName, texType) {
 80         if (!fileName) {
 81             return;
 82         }
 83         var self = this;
 84         texType = texType || ccui.Widget.LOCAL_TEXTURE;
 85         this._textureFile = fileName;
 86         this._imageTexType = texType;
 87         var imageRenderer = self._imageRenderer;
 88 
 89         switch (self._imageTexType) {
 90             case ccui.Widget.LOCAL_TEXTURE:
 91                 if(self._scale9Enabled){
 92                     imageRenderer.initWithFile(fileName);
 93                     imageRenderer.setCapInsets(self._capInsets);
 94                 }else{
 95                     //SetTexture cannot load resource
 96                     imageRenderer.initWithFile(fileName);
 97                 }
 98                 break;
 99             case ccui.Widget.PLIST_TEXTURE:
100                 if(self._scale9Enabled){
101                     imageRenderer.initWithSpriteFrameName(fileName);
102                     imageRenderer.setCapInsets(self._capInsets);
103                 }else{
104                     //SetTexture cannot load resource
105                     imageRenderer.initWithSpriteFrameName(fileName);
106                 }
107                 break;
108             default:
109                 break;
110         }
111 
112         if(!imageRenderer.texture || !imageRenderer.texture.isLoaded()){
113             imageRenderer.addLoadedEventListener(function(){
114                 self._findLayout();
115 
116                 self._imageTextureSize = imageRenderer.getContentSize();
117                 self._updateFlippedX();
118                 self._updateFlippedY();
119                 self._updateContentSizeWithTextureSize(self._imageTextureSize);
120                 self._imageRendererAdaptDirty = true;
121             });
122         }
123 
124         self._imageTextureSize = imageRenderer.getContentSize();
125         self._updateFlippedX();
126         self._updateFlippedY();
127 
128         self._updateContentSizeWithTextureSize(self._imageTextureSize);
129         self._imageRendererAdaptDirty = true;
130     },
131 
132     /**
133      * set texture rect
134      * @param {cc.Rect} rect
135      */
136     setTextureRect: function (rect) {
137         if (!this._scale9Enabled)
138             this._imageRenderer.setTextureRect(rect);
139     },
140 
141     _updateFlippedX: function () {
142         if (this._scale9Enabled)
143             this._imageRenderer.setScaleX(this._flippedX ? -1 : 1);
144         else
145             this._imageRenderer.setFlippedX(this._flippedX);
146     },
147 
148     _updateFlippedY: function () {
149         if (this._scale9Enabled)
150             this._imageRenderer.setScaleY(this._flippedY ? -1 : 1);
151         else
152             this._imageRenderer.setFlippedY(this._flippedY);
153     },
154 
155     /**
156      * Sets if button is using scale9 renderer.
157      * @param {Boolean} able
158      */
159     setScale9Enabled: function (able) {
160         if (this._scale9Enabled == able)
161             return;
162 
163         this._scale9Enabled = able;
164         this.removeProtectedChild(this._imageRenderer);
165         this._imageRenderer = null;
166         if (this._scale9Enabled) {
167             this._imageRenderer = cc.Scale9Sprite.create();
168         } else {
169             this._imageRenderer = cc.Sprite.create();
170         }
171         this.loadTexture(this._textureFile, this._imageTexType);
172         this.addProtectedChild(this._imageRenderer, ccui.ImageView.RENDERER_ZORDER, -1);
173         if (this._scale9Enabled) {
174             var ignoreBefore = this._ignoreSize;
175             this.ignoreContentAdaptWithSize(false);
176             this._prevIgnoreSize = ignoreBefore;
177         } else
178             this.ignoreContentAdaptWithSize(this._prevIgnoreSize);
179         this.setCapInsets(this._capInsets);
180     },
181 
182     /**
183      * Get button is using scale9 renderer or not.
184      * @returns {Boolean}
185      */
186     isScale9Enabled:function(){
187         return this._scale9Enabled;
188     },
189 
190     /**
191      * ignoreContentAdaptWithSize
192      * @param {Boolean} ignore
193      */
194     ignoreContentAdaptWithSize: function (ignore) {
195         if (!this._scale9Enabled || (this._scale9Enabled && !ignore)) {
196             ccui.Widget.prototype.ignoreContentAdaptWithSize.call(this, ignore);
197             this._prevIgnoreSize = ignore;
198         }
199     },
200 
201     /**
202      * Sets capinsets for button, if button is using scale9 renderer.
203      * @param {cc.Rect} capInsets
204      */
205     setCapInsets: function (capInsets) {
206         this._capInsets = capInsets;
207         if (!this._scale9Enabled)
208             return;
209         this._imageRenderer.setCapInsets(capInsets);
210     },
211 
212     /**
213      * Get cap insets.
214      * @returns {cc.Rect}
215      */
216     getCapInsets:function(){
217         return this._capInsets;
218     },
219 
220     _onSizeChanged: function () {
221         ccui.Widget.prototype._onSizeChanged.call(this);
222         this._imageRendererAdaptDirty = true;
223     },
224 
225     _adaptRenderers: function(){
226         if (this._imageRendererAdaptDirty){
227             this._imageTextureScaleChangedWithSize();
228             this._imageRendererAdaptDirty = false;
229         }
230     },
231 
232     getVirtualRendererSize: function(){
233         return cc.size(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         } else {
249             if (this._scale9Enabled)
250                 this._imageRenderer.setPreferredSize(this._contentSize);
251             else {
252                 var textureSize = this._imageRenderer.getContentSize();
253                 if (textureSize.width <= 0.0 || textureSize.height <= 0.0) {
254                     this._imageRenderer.setScale(1.0);
255                     return;
256                 }
257                 this._imageRenderer.setScaleX(this._contentSize.width / textureSize.width);
258                 this._imageRenderer.setScaleY(this._contentSize.height / textureSize.height);
259             }
260         }
261         this._imageRenderer.setPosition(this._contentSize.width / 2.0, this._contentSize.height / 2.0);
262     },
263 
264     /**
265      * Returns the "class name" of widget.
266      * @returns {string}
267      */
268     getDescription: function () {
269         return "ImageView";
270     },
271 
272     _createCloneInstance:function(){
273         return ccui.ImageView.create();
274     },
275 
276     _copySpecialProperties: function (imageView) {
277         if(imageView instanceof ccui.ImageView){
278             this._prevIgnoreSize = imageView._prevIgnoreSize;
279             this.setScale9Enabled(imageView._scale9Enabled);
280             this.loadTexture(imageView._textureFile, imageView._imageTexType);
281             this.setCapInsets(imageView._capInsets);
282         }
283     }
284 
285 });
286 
287 /**
288  * allocates and initializes a UIImageView.
289  * @deprecated
290  * @param {string} imageFileName
291  * @param {Number} texType
292  * @return {ccui.ImageView}
293  * @example
294  * // example
295  * var uiImageView = ccui.ImageView.create();
296  */
297 ccui.ImageView.create = function (imageFileName, texType) {
298     return new ccui.ImageView(imageFileName, texType);
299 };
300 
301 // Constants
302 ccui.ImageView.RENDERER_ZORDER = -1;