UIImageView.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /****************************************************************************
  2. Copyright (c) 2011-2012 cocos2d-x.org
  3. Copyright (c) 2013-2014 Chukong Technologies Inc.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. /**
  22. * The ImageView control of Cocos GUI
  23. * @class
  24. * @extends ccui.Widget
  25. */
  26. ccui.ImageView = ccui.Widget.extend(/** @lends ccui.ImageView# */{
  27. _scale9Enabled: false,
  28. _prevIgnoreSize: true,
  29. _capInsets: null,
  30. _imageRenderer: null,
  31. _textureFile: "",
  32. _imageTexType: ccui.Widget.LOCAL_TEXTURE,
  33. _imageTextureSize: null,
  34. _className:"ImageView",
  35. _imageRendererAdaptDirty: true,
  36. /**
  37. * allocates and initializes a ccui.ImageView.
  38. * Constructor of ccui.ImageView, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
  39. * @param {String} imageFileName
  40. * @param {Number} [texType==ccui.Widget.LOCAL_TEXTURE]
  41. * @example
  42. * // example
  43. * var uiImageView = new ccui.ImageView;
  44. */
  45. ctor: function (imageFileName, texType) {
  46. this._capInsets = cc.rect(0,0,0,0);
  47. this._imageTextureSize = cc.size(this._capInsets.width, this._capInsets.height);
  48. ccui.Widget.prototype.ctor.call(this);
  49. texType && this.init(imageFileName, texType);
  50. },
  51. /**
  52. * Initializes an imageView. please do not call this function by yourself, you should pass the parameters to constructor to initialize it.
  53. * @param {String} imageFileName
  54. * @param {Number} [texType==ccui.Widget.LOCAL_TEXTURE]
  55. * @returns {boolean}
  56. */
  57. init: function(imageFileName, texType){
  58. if(ccui.Widget.prototype.init.call(this)){
  59. if(imageFileName === undefined)
  60. this._imageTexType = ccui.Widget.LOCAL_TEXTURE;
  61. else
  62. this.loadTexture(imageFileName, texType);
  63. return true;
  64. }
  65. return false;
  66. },
  67. _initRenderer: function () {
  68. this._imageRenderer = cc.Sprite.create();
  69. this.addProtectedChild(this._imageRenderer, ccui.ImageView.RENDERER_ZORDER, -1);
  70. },
  71. /**
  72. * Loads textures for button.
  73. * @param {String} fileName
  74. * @param {ccui.Widget.LOCAL_TEXTURE|ccui.Widget.PLIST_TEXTURE} texType
  75. */
  76. loadTexture: function (fileName, texType) {
  77. if (!fileName) {
  78. return;
  79. }
  80. var self = this;
  81. texType = texType || ccui.Widget.LOCAL_TEXTURE;
  82. this._textureFile = fileName;
  83. this._imageTexType = texType;
  84. var imageRenderer = self._imageRenderer;
  85. switch (self._imageTexType) {
  86. case ccui.Widget.LOCAL_TEXTURE:
  87. if(self._scale9Enabled){
  88. imageRenderer.initWithFile(fileName);
  89. imageRenderer.setCapInsets(self._capInsets);
  90. }else{
  91. //SetTexture cannot load resource
  92. imageRenderer.initWithFile(fileName);
  93. }
  94. break;
  95. case ccui.Widget.PLIST_TEXTURE:
  96. if(self._scale9Enabled){
  97. imageRenderer.initWithSpriteFrameName(fileName);
  98. imageRenderer.setCapInsets(self._capInsets);
  99. }else{
  100. //SetTexture cannot load resource
  101. imageRenderer.initWithSpriteFrameName(fileName);
  102. }
  103. break;
  104. default:
  105. break;
  106. }
  107. if(!imageRenderer.texture || !imageRenderer.texture.isLoaded()){
  108. imageRenderer.addLoadedEventListener(function(){
  109. self._findLayout();
  110. self._imageTextureSize = imageRenderer.getContentSize();
  111. self._updateFlippedX();
  112. self._updateFlippedY();
  113. self._updateChildrenDisplayedRGBA();
  114. self._updateContentSizeWithTextureSize(self._imageTextureSize);
  115. self._imageRendererAdaptDirty = true;
  116. });
  117. }
  118. self._imageTextureSize = imageRenderer.getContentSize();
  119. self._updateFlippedX();
  120. self._updateFlippedY();
  121. this._updateChildrenDisplayedRGBA();
  122. self._updateContentSizeWithTextureSize(self._imageTextureSize);
  123. self._imageRendererAdaptDirty = true;
  124. },
  125. /**
  126. * Sets texture rect
  127. * @param {cc.Rect} rect
  128. */
  129. setTextureRect: function (rect) {
  130. if (!this._scale9Enabled)
  131. this._imageRenderer.setTextureRect(rect);
  132. },
  133. _updateFlippedX: function () {
  134. if (this._scale9Enabled)
  135. this._imageRenderer.setScaleX(this._flippedX ? -1 : 1);
  136. else
  137. this._imageRenderer.setFlippedX(this._flippedX);
  138. },
  139. _updateFlippedY: function () {
  140. if (this._scale9Enabled)
  141. this._imageRenderer.setScaleY(this._flippedY ? -1 : 1);
  142. else
  143. this._imageRenderer.setFlippedY(this._flippedY);
  144. },
  145. /**
  146. * Sets if button is using scale9 renderer.
  147. * @param {Boolean} able
  148. */
  149. setScale9Enabled: function (able) {
  150. if (this._scale9Enabled == able)
  151. return;
  152. this._scale9Enabled = able;
  153. this.removeProtectedChild(this._imageRenderer);
  154. this._imageRenderer = null;
  155. if (this._scale9Enabled) {
  156. this._imageRenderer = new ccui.Scale9Sprite();
  157. } else {
  158. this._imageRenderer = cc.Sprite.create();
  159. }
  160. this.loadTexture(this._textureFile, this._imageTexType);
  161. this.addProtectedChild(this._imageRenderer, ccui.ImageView.RENDERER_ZORDER, -1);
  162. if (this._scale9Enabled) {
  163. var ignoreBefore = this._ignoreSize;
  164. this.ignoreContentAdaptWithSize(false);
  165. this._prevIgnoreSize = ignoreBefore;
  166. } else
  167. this.ignoreContentAdaptWithSize(this._prevIgnoreSize);
  168. this.setCapInsets(this._capInsets);
  169. },
  170. /**
  171. * Returns ImageView is using scale9 renderer or not.
  172. * @returns {Boolean}
  173. */
  174. isScale9Enabled:function(){
  175. return this._scale9Enabled;
  176. },
  177. /**
  178. * Ignore the imageView's custom size, true that imageView will ignore it's custom size, use renderer's content size, false otherwise.
  179. * @override
  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. * Sets capinsets for button, if button is using scale9 renderer.
  190. * @param {cc.Rect} capInsets
  191. */
  192. setCapInsets: function (capInsets) {
  193. if(!capInsets)
  194. return;
  195. var locInsets = this._capInsets;
  196. locInsets.x = capInsets.x;
  197. locInsets.y = capInsets.y;
  198. locInsets.width = capInsets.width;
  199. locInsets.height = capInsets.height;
  200. if (!this._scale9Enabled)
  201. return;
  202. this._imageRenderer.setCapInsets(capInsets);
  203. },
  204. /**
  205. * Returns cap insets of ccui.ImageView.
  206. * @returns {cc.Rect}
  207. */
  208. getCapInsets:function(){
  209. return cc.rect(this._capInsets);
  210. },
  211. _onSizeChanged: function () {
  212. ccui.Widget.prototype._onSizeChanged.call(this);
  213. this._imageRendererAdaptDirty = true;
  214. },
  215. _adaptRenderers: function(){
  216. if (this._imageRendererAdaptDirty){
  217. this._imageTextureScaleChangedWithSize();
  218. this._imageRendererAdaptDirty = false;
  219. }
  220. },
  221. /**
  222. * Returns the image's texture size.
  223. * @returns {cc.Size}
  224. */
  225. getVirtualRendererSize: function(){
  226. return cc.size(this._imageTextureSize);
  227. },
  228. /**
  229. * Returns the renderer of ccui.ImageView
  230. * @override
  231. * @returns {cc.Node}
  232. */
  233. getVirtualRenderer: function () {
  234. return this._imageRenderer;
  235. },
  236. _imageTextureScaleChangedWithSize: function () {
  237. if (this._ignoreSize) {
  238. if (!this._scale9Enabled)
  239. this._imageRenderer.setScale(1.0);
  240. } else {
  241. if (this._scale9Enabled)
  242. this._imageRenderer.setPreferredSize(this._contentSize);
  243. else {
  244. var textureSize = this._imageRenderer.getContentSize();
  245. if (textureSize.width <= 0.0 || textureSize.height <= 0.0) {
  246. this._imageRenderer.setScale(1.0);
  247. return;
  248. }
  249. this._imageRenderer.setScaleX(this._contentSize.width / textureSize.width);
  250. this._imageRenderer.setScaleY(this._contentSize.height / textureSize.height);
  251. }
  252. }
  253. this._imageRenderer.setPosition(this._contentSize.width / 2.0, this._contentSize.height / 2.0);
  254. },
  255. /**
  256. * Returns the "class name" of ccui.ImageView.
  257. * @override
  258. * @returns {string}
  259. */
  260. getDescription: function () {
  261. return "ImageView";
  262. },
  263. _createCloneInstance:function(){
  264. return ccui.ImageView.create();
  265. },
  266. _copySpecialProperties: function (imageView) {
  267. if(imageView instanceof ccui.ImageView){
  268. this._prevIgnoreSize = imageView._prevIgnoreSize;
  269. this.setScale9Enabled(imageView._scale9Enabled);
  270. this.loadTexture(imageView._textureFile, imageView._imageTexType);
  271. this.setCapInsets(imageView._capInsets);
  272. }
  273. }
  274. });
  275. /**
  276. * Allocates and initializes a UIImageView.
  277. * @deprecated since v3.0, please use new ccui.ImageView() instead.
  278. * @param {string} imageFileName
  279. * @param {Number} texType
  280. * @return {ccui.ImageView}
  281. * @example
  282. * // example
  283. * var uiImageView = ccui.ImageView.create();
  284. */
  285. ccui.ImageView.create = function (imageFileName, texType) {
  286. return new ccui.ImageView(imageFileName, texType);
  287. };
  288. // Constants
  289. /**
  290. * The zOrder value of ccui.ImageView's renderer.
  291. * @constant
  292. * @type {number}
  293. */
  294. ccui.ImageView.RENDERER_ZORDER = -1;