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.NodeRGBA.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.updateDisplayedColor(this.getColor());
101         this.updateDisplayedOpacity(this.getOpacity());
102         this.updateAnchorPoint();
103         this.imageTextureScaleChangedWithSize();
104     },
105 
106     /**
107      * set texture rect
108      * @param {cc.Rect} rect
109      */
110     setTextureRect: function (rect) {
111         if (!this._scale9Enabled){
112             this._imageRenderer.setTextureRect(rect);
113         }
114     },
115 
116     /**
117      * Sets whether the widget should be flipped horizontally or not.
118      * @param {Boolean} flipX
119      */
120     setFlippedX: function (flipX) {
121         if (!this._scale9Enabled) {
122             this._imageRenderer.setFlippedX(flipX);
123         }
124     },
125 
126     /**
127      * override "setFlippedY" of widget.
128      * @param {Boolean} flipY
129      */
130     setFlippedY: function (flipY) {
131         if (!this._scale9Enabled) {
132             this._imageRenderer.setFlippedY(flipY);
133         }
134     },
135 
136     /**
137      * override "isFlippedX" of widget.
138      * @returns {Boolean}
139      */
140     isFlippedX: function () {
141         if (this._scale9Enabled)
142             return false;
143         else
144             return this._imageRenderer.isFlippedX();
145     },
146 
147     /**
148      * override "isFlippedY" of widget.
149      * @returns {Boolean}
150      */
151     isFlippedY: function () {
152         if (this._scale9Enabled)
153             return false;
154         else
155             return this._imageRenderer.isFlippedY();
156     },
157 
158     /**
159      * Sets if button is using scale9 renderer.
160      * @param {Boolean} able
161      */
162     setScale9Enabled: function (able) {
163         if (this._scale9Enabled == able) {
164             return;
165         }
166 
167 
168         this._scale9Enabled = able;
169         cc.NodeRGBA.prototype.removeChild.call(this, this._imageRenderer, true);
170         this._imageRenderer = null;
171         if (this._scale9Enabled) {
172             this._imageRenderer = cc.Scale9Sprite.create();
173         }
174         else {
175             this._imageRenderer = cc.Sprite.create();
176         }
177         this.loadTexture(this._textureFile, this._imageTexType);
178         cc.NodeRGBA.prototype.addChild.call(this, this._imageRenderer, ccs.IMAGERENDERERZ, -1);
179         if (this._scale9Enabled) {
180             var ignoreBefore = this._ignoreSize;
181             this.ignoreContentAdaptWithSize(false);
182             this._prevIgnoreSize = ignoreBefore;
183         }
184         else {
185             this.ignoreContentAdaptWithSize(this._prevIgnoreSize);
186         }
187         this.setCapInsets(this._capInsets);
188     },
189 
190     /**
191      * ignoreContentAdaptWithSize
192      * @param {Boolean} ignore
193      */
194     ignoreContentAdaptWithSize: function (ignore) {
195         if (!this._scale9Enabled || (this._scale9Enabled && !ignore)) {
196             ccs.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         }
210         this._imageRenderer.setCapInsets(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(arguments.length === 2){
220             ccs.Widget.prototype.setAnchorPoint.call(this, point, y);
221             this._imageRenderer.setAnchorPoint(point, y);
222         } else {
223             ccs.Widget.prototype.setAnchorPoint.call(this, point);
224             this._imageRenderer.setAnchorPoint(point);
225         }
226     },
227 
228     onSizeChanged: function () {
229         ccs.Widget.prototype.onSizeChanged.call(this);
230         this.imageTextureScaleChangedWithSize();
231     },
232 
233     /**
234      * override "getContentSize" method of widget.
235      * @returns {cc.Size}
236      */
237     getContentSize: function () {
238         return this._imageTextureSize;
239     },
240 
241     /**
242      * override "getVirtualRenderer" method of widget.
243      * @returns {cc.Node}
244      */
245     getVirtualRenderer: function () {
246         return this._imageRenderer;
247     },
248 
249     imageTextureScaleChangedWithSize: function () {
250         if (this._ignoreSize) {
251             if (!this._scale9Enabled) {
252                 this._imageRenderer.setScale(1.0);
253                 this._size = this._imageTextureSize;
254             }
255         }
256         else {
257             if (this._scale9Enabled) {
258                 this._imageRenderer.setPreferredSize(this._size);
259             }
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                 var scaleX = this._size.width / textureSize.width;
267                 var scaleY = this._size.height / textureSize.height;
268                 this._imageRenderer.setScaleX(scaleX);
269                 this._imageRenderer.setScaleY(scaleY);
270             }
271         }
272     },
273 
274     /**
275      * Returns the "class name" of widget.
276      * @returns {string}
277      */
278     getDescription: function () {
279         return "ImageView";
280     },
281 
282     createCloneInstance:function(){
283         return ccs.ImageView.create();
284     },
285 
286     copySpecialProperties: function (imageView) {
287         this._prevIgnoreSize = imageView._prevIgnoreSize;
288         this.setScale9Enabled(imageView._scale9Enabled);
289         this.loadTexture(imageView._textureFile, imageView._imageTexType);
290         this.setCapInsets(imageView._capInsets);
291     }
292 
293 });
294 /**
295  * allocates and initializes a UIImageView.
296  * @constructs
297  * @return {ccs.ImageView}
298  * @example
299  * // example
300  * var uiImageView = ccs.ImageView.create();
301  */
302 ccs.ImageView.create = function () {
303     var uiImageView = new ccs.ImageView();
304     if (uiImageView && uiImageView.init()) {
305         return uiImageView;
306     }
307     return null;
308 };
309