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 ccs.UIButton
 27  * @class
 28  * @extends ccs.UIWidget
 29  */
 30 ccs.UIImageView = ccs.UIWidget.extend(/** @lends ccs.UIImageView# */{
 31     _clickCount: 0,
 32     _clickTimeInterval: 0,
 33     _startCheckDoubleClick: false,
 34     _touchRelease: false,
 35     _doubleClickEnabled: false,
 36     _scale9Enabled: false,
 37     _prevIgnoreSize: true,
 38     _capInsets: null,
 39     _imageRenderer: null,
 40     _textureFile: "",
 41     _imageTexType: null,
 42     _imageTextureSize: null,
 43     ctor: function () {
 44         ccs.UIWidget.prototype.ctor.call(this);
 45         this._clickCount = 0;
 46         this._clickTimeInterval = 0;
 47         this._startCheckDoubleClick = false;
 48         this._touchRelease = false;
 49         this._doubleClickEnabled = false;
 50         this._scale9Enabled = false;
 51         this._prevIgnoreSize = true;
 52         this._capInsets = cc.rect(0,0,0,0);
 53         this._imageRenderer = null;
 54         this._textureFile = "";
 55         this._imageTexType = ccs.TextureResType.local;
 56         this._imageTextureSize = this._size;
 57     },
 58 
 59     initRenderer: function () {
 60         ccs.UIWidget.prototype.initRenderer.call(this);
 61         this._imageRenderer = cc.Sprite.create();
 62         this._renderer.addChild(this._imageRenderer);
 63     },
 64 
 65     /**
 66      * Load textures for button.
 67      * @param {String} fileName
 68      * @param {ccs.TextureResType} texType
 69      */
 70     loadTexture: function (fileName, texType) {
 71         if (!fileName) {
 72             return;
 73         }
 74         texType = texType || ccs.TextureResType.local;
 75         this._textureFile = fileName;
 76         this._imageTexType = texType;
 77         switch (this._imageTexType) {
 78             case ccs.TextureResType.local:
 79                 if (this._scale9Enabled) {
 80                     this._imageRenderer.initWithFile(fileName);
 81                     this._imageRenderer.setColor(this.getColor());
 82                     this._imageRenderer.setOpacity(this.getOpacity());
 83                     this._imageRenderer.setCapInsets(this._capInsets);
 84                 }
 85                 else {
 86                     this._imageRenderer.initWithFile(fileName);
 87                     this._imageRenderer.setColor(this.getColor());
 88                     this._imageRenderer.setOpacity(this.getOpacity());
 89                 }
 90                 break;
 91             case ccs.TextureResType.plist:
 92                 if (this._scale9Enabled) {
 93                     this._imageRenderer.initWithSpriteFrameName(fileName);
 94                     this._imageRenderer.setColor(this.getColor());
 95                     this._imageRenderer.setOpacity(this.getOpacity());
 96                     this._imageRenderer.setCapInsets(this._capInsets);
 97                 }
 98                 else {
 99                     this._imageRenderer.initWithSpriteFrameName(fileName);
100                     this._imageRenderer.setColor(this.getColor());
101                     this._imageRenderer.setOpacity(this.getOpacity());
102                 }
103                 break;
104             default:
105                 break;
106         }
107         this._imageTextureSize = this._imageRenderer.getContentSize();
108         this.updateAnchorPoint();
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         }
120     },
121 
122     onTouchBegan: function (touchPoint) {
123         this.setFocused(true);
124         this._touchStartPos.x = touchPoint.x;
125         this._touchStartPos.y = touchPoint.y;
126         this._widgetParent.checkChildInfo(0, this, touchPoint);
127         this.pushDownEvent();
128 
129         if (this._doubleClickEnabled) {
130             this._clickTimeInterval = 0;
131             this._startCheckDoubleClick = true;
132             this._clickCount++;
133             this._touchRelease = false;
134         }
135         return this._touchPassedEnabled;
136     },
137 
138     onTouchEnded: function (touchPoint) {
139         if (this._doubleClickEnabled) {
140             if (this._clickCount >= 2) {
141                 this.doubleClickEvent();
142                 this._clickCount = 0;
143                 this._startCheckDoubleClick = false;
144             }
145             else {
146                 this._touchRelease = true;
147             }
148         }
149         else {
150             ccs.UIWidget.prototype.onTouchEnded.call(this, touchPoint);
151         }
152     },
153 
154     doubleClickEvent: function () {
155 
156     },
157 
158     checkDoubleClick: function (dt) {
159         if (this._startCheckDoubleClick) {
160             this._clickTimeInterval += dt;
161             if (this._clickTimeInterval >= 200 && this._clickCount > 0) {
162                 this._clickTimeInterval = 0;
163                 this._clickCount--;
164                 this._startCheckDoubleClick = false;
165             }
166         }
167         else {
168             if (this._clickCount <= 1) {
169                 if (this._touchRelease) {
170                     this.releaseUpEvent();
171                     this._clickTimeInterval = 0;
172                     this._clickCount = 0;
173                     this._touchRelease = false;
174                 }
175             }
176         }
177     },
178 
179     setDoubleClickEnabled: function (bool) {
180         if (bool == this._doubleClickEnabled) {
181             return;
182         }
183         this._doubleClickEnabled = bool;
184     },
185 
186     /**
187      * Sets whether the widget should be flipped horizontally or not.
188      * @param {Boolean} flipX
189      */
190     setFlippedX: function (flipX) {
191         if (!this._scale9Enabled) {
192             this._imageRenderer.setFlippedX(flipX);
193         }
194     },
195 
196     /**
197      * override "setFlippedY" of widget.
198      * @param {Boolean} flipY
199      */
200     setFlippedY: function (flipY) {
201         if (!this._scale9Enabled) {
202             this._imageRenderer.setFlippedY(flipY);
203         }
204     },
205 
206     /**
207      * override "isFlippedX" of widget.
208      * @returns {Boolean}
209      */
210     isFlippedX: function () {
211         if (this._scale9Enabled)
212             return false;
213         else
214             return this._imageRenderer.isFlippedX();
215     },
216 
217     /**
218      * override "isFlippedY" of widget.
219      * @returns {Boolean}
220      */
221     isFlippedY: function () {
222         if (this._scale9Enabled)
223             return false;
224         else
225             return this._imageRenderer.isFlippedY();
226     },
227 
228     /**
229      * Sets if button is using scale9 renderer.
230      * @param {Boolean} able
231      */
232     setScale9Enabled: function (able) {
233         if (this._scale9Enabled == able) {
234             return;
235         }
236 
237 
238         this._scale9Enabled = able;
239         this._renderer.removeChild(this._imageRenderer, true);
240         this._imageRenderer = null;
241         if (this._scale9Enabled) {
242             this._imageRenderer = cc.Scale9Sprite.create();
243         }
244         else {
245             this._imageRenderer = cc.Sprite.create();
246         }
247         this.loadTexture(this._textureFile, this._imageTexType);
248         this._renderer.addChild(this._imageRenderer);
249         if (this._scale9Enabled) {
250             var ignoreBefore = this._ignoreSize;
251             this.ignoreContentAdaptWithSize(false);
252             this._prevIgnoreSize = ignoreBefore;
253         }
254         else {
255             this.ignoreContentAdaptWithSize(this._prevIgnoreSize);
256         }
257         this.setCapInsets(this._capInsets);
258     },
259 
260     /**
261      * ignoreContentAdaptWithSize
262      * @param {Boolean} ignore
263      */
264     ignoreContentAdaptWithSize: function (ignore) {
265         if (!this._scale9Enabled || (this._scale9Enabled && !ignore)) {
266             ccs.UIWidget.prototype.ignoreContentAdaptWithSize.call(this, ignore);
267             this._prevIgnoreSize = ignore;
268         }
269     },
270 
271     /**
272      * Sets capinsets for button, if button is using scale9 renderer.
273      * @param {cc.Rect} capInsets
274      */
275     setCapInsets: function (capInsets) {
276         this._capInsets = capInsets;
277         if (!this._scale9Enabled) {
278             return;
279         }
280         this._imageRenderer.setCapInsets(capInsets);
281     },
282 
283     /**
284      * override "setAnchorPoint" of widget.
285      * @param {cc.Point} pt
286      */
287     setAnchorPoint: function (pt) {
288         ccs.UIWidget.prototype.setAnchorPoint.call(this, pt);
289         this._imageRenderer.setAnchorPoint(pt);
290     },
291 
292     onSizeChanged: function () {
293         this.imageTextureScaleChangedWithSize();
294     },
295 
296     /**
297      * override "getContentSize" method of widget.
298      * @returns {cc.Size}
299      */
300     getContentSize: function () {
301         return this._imageTextureSize;
302     },
303 
304     /**
305      * override "getVirtualRenderer" method of widget.
306      * @returns {cc.Node}
307      */
308     getVirtualRenderer: function () {
309         return this._imageRenderer;
310     },
311 
312     imageTextureScaleChangedWithSize: function () {
313         if (this._ignoreSize) {
314             if (!this._scale9Enabled) {
315                 this._imageRenderer.setScale(1.0);
316                 this._size = this._imageTextureSize;
317             }
318         }
319         else {
320             if (this._scale9Enabled) {
321                 this._imageRenderer.setPreferredSize(this._size);
322             }
323             else {
324                 var textureSize = this._imageRenderer.getContentSize();
325                 if (textureSize.width <= 0.0 || textureSize.height <= 0.0) {
326                     this._imageRenderer.setScale(1.0);
327                     return;
328                 }
329                 var scaleX = this._size.width / textureSize.width;
330                 var scaleY = this._size.height / textureSize.height;
331                 this._imageRenderer.setScaleX(scaleX);
332                 this._imageRenderer.setScaleY(scaleY);
333             }
334         }
335     },
336 
337     /**
338      * Returns the "class name" of widget.
339      * @returns {string}
340      */
341     getDescription: function () {
342         return "ImageView";
343     },
344 
345     createCloneInstance:function(){
346         return ccs.UIImageView.create();
347     },
348 
349     copySpecialProperties: function (imageView) {
350         this._prevIgnoreSize = imageView._prevIgnoreSize;
351         this.setScale9Enabled(imageView._scale9Enabled);
352         this.loadTexture(imageView._textureFile, imageView._imageTexType);
353         this.setCapInsets(imageView._capInsets);
354     }
355 
356 });
357 /**
358  * allocates and initializes a UIImageView.
359  * @constructs
360  * @return {ccs.UIImageView}
361  * @example
362  * // example
363  * var uiImageView = ccs.UIImageView.create();
364  */
365 ccs.UIImageView.create = function () {
366     var uiImageView = new ccs.UIImageView();
367     if (uiImageView && uiImageView.init()) {
368         return uiImageView;
369     }
370     return null;
371 };
372