UIButton.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  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 button controls of Cocos UI.
  23. * @class
  24. * @extends ccui.Widget
  25. *
  26. * @property {String} titleText - The content string of the button title
  27. * @property {String} titleFont - The content string font of the button title
  28. * @property {Number} titleFontSize - The content string font size of the button title
  29. * @property {String} titleFontName - The content string font name of the button title
  30. * @property {cc.Color} titleFontColor - The content string font color of the button title
  31. * @property {Boolean} pressedActionEnabled - Indicate whether button has zoom effect when clicked
  32. */
  33. ccui.Button = ccui.Widget.extend(/** @lends ccui.Button# */{
  34. _buttonNormalRenderer: null,
  35. _buttonClickedRenderer: null,
  36. _buttonDisableRenderer: null,
  37. _titleRenderer: null,
  38. _normalFileName: "",
  39. _clickedFileName: "",
  40. _disabledFileName: "",
  41. _prevIgnoreSize: true,
  42. _scale9Enabled: false,
  43. _capInsetsNormal: null,
  44. _capInsetsPressed: null,
  45. _capInsetsDisabled: null,
  46. _normalTexType: ccui.Widget.LOCAL_TEXTURE,
  47. _pressedTexType: ccui.Widget.LOCAL_TEXTURE,
  48. _disabledTexType: ccui.Widget.LOCAL_TEXTURE,
  49. _normalTextureSize: null,
  50. _pressedTextureSize: null,
  51. _disabledTextureSize: null,
  52. pressedActionEnabled: false,
  53. _titleColor: null,
  54. _normalTextureScaleXInSize: 1,
  55. _normalTextureScaleYInSize: 1,
  56. _pressedTextureScaleXInSize: 1,
  57. _pressedTextureScaleYInSize: 1,
  58. _normalTextureLoaded: false,
  59. _pressedTextureLoaded: false,
  60. _disabledTextureLoaded: false,
  61. _className: "Button",
  62. _normalTextureAdaptDirty: true,
  63. _pressedTextureAdaptDirty: true,
  64. _disabledTextureAdaptDirty: true,
  65. _fontName: "Thonburi",
  66. _fontSize: 12,
  67. _type: 0,
  68. /**
  69. * Allocates and initializes a UIButton.
  70. * Constructor of ccui.Button. override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
  71. * @param {String} normalImage
  72. * @param {String} [selectedImage=""]
  73. * @param {String} [disableImage=""]
  74. * @param {Number} [texType=ccui.Widget.LOCAL_TEXTURE]
  75. * @example
  76. * // example
  77. * var uiButton = new ccui.Button();
  78. */
  79. ctor: function (normalImage, selectedImage, disableImage, texType) {
  80. this._capInsetsNormal = cc.rect(0, 0, 0, 0);
  81. this._capInsetsPressed = cc.rect(0, 0, 0, 0);
  82. this._capInsetsDisabled = cc.rect(0, 0, 0, 0);
  83. this._normalTextureSize = cc.size(0, 0);
  84. this._pressedTextureSize = cc.size(0, 0);
  85. this._disabledTextureSize = cc.size(0, 0);
  86. this._titleColor = cc.color.WHITE;
  87. ccui.Widget.prototype.ctor.call(this);
  88. this.setTouchEnabled(true);
  89. texType && this.init(normalImage, selectedImage, disableImage, texType);
  90. },
  91. /**
  92. * Initializes a button. please do not call this function by yourself, you should pass the parameters to constructor to initialize it.
  93. * @param {String} normalImage
  94. * @param {String} [selectedImage=""]
  95. * @param {String} [disableImage=""]
  96. * @param {Number} [texType=ccui.Widget.LOCAL_TEXTURE]
  97. * @returns {boolean}
  98. * @override
  99. */
  100. init: function (normalImage, selectedImage,disableImage, texType) {
  101. if (ccui.Widget.prototype.init.call(this)) {
  102. if(normalImage === undefined)
  103. return true;
  104. this.loadTextures(normalImage, selectedImage,disableImage, texType);
  105. }
  106. return false;
  107. },
  108. _initRenderer: function () {
  109. this._buttonNormalRenderer = cc.Sprite.create();
  110. this._buttonClickedRenderer = cc.Sprite.create();
  111. this._buttonDisableRenderer = cc.Sprite.create();
  112. this._titleRenderer = new cc.LabelTTF("");
  113. this._titleRenderer.setAnchorPoint(0.5, 0.5);
  114. this.addProtectedChild(this._buttonNormalRenderer, ccui.Button.NORMAL_RENDERER_ZORDER, -1);
  115. this.addProtectedChild(this._buttonClickedRenderer, ccui.Button.PRESSED_RENDERER_ZORDER, -1);
  116. this.addProtectedChild(this._buttonDisableRenderer, ccui.Button.DISABLED_RENDERER_ZORDER, -1);
  117. this.addProtectedChild(this._titleRenderer, ccui.Button.TITLE_RENDERER_ZORDER, -1);
  118. },
  119. /**
  120. * Sets if button is using scale9 renderer.
  121. * @param {Boolean} able true that using scale9 renderer, false otherwise.
  122. */
  123. setScale9Enabled: function (able) {
  124. if (this._scale9Enabled == able)
  125. return;
  126. this._brightStyle = ccui.Widget.BRIGHT_STYLE_NONE;
  127. this._scale9Enabled = able;
  128. this.removeProtectedChild(this._buttonNormalRenderer);
  129. this.removeProtectedChild(this._buttonClickedRenderer);
  130. this.removeProtectedChild(this._buttonDisableRenderer);
  131. if (this._scale9Enabled) {
  132. this._buttonNormalRenderer = new ccui.Scale9Sprite();
  133. this._buttonClickedRenderer = new ccui.Scale9Sprite();
  134. this._buttonDisableRenderer = new ccui.Scale9Sprite();
  135. } else {
  136. this._buttonNormalRenderer = cc.Sprite.create();
  137. this._buttonClickedRenderer = cc.Sprite.create();
  138. this._buttonDisableRenderer = cc.Sprite.create();
  139. }
  140. this.loadTextureNormal(this._normalFileName, this._normalTexType);
  141. this.loadTexturePressed(this._clickedFileName, this._pressedTexType);
  142. this.loadTextureDisabled(this._disabledFileName, this._disabledTexType);
  143. this.addProtectedChild(this._buttonNormalRenderer, ccui.Button.NORMAL_RENDERER_ZORDER, -1);
  144. this.addProtectedChild(this._buttonClickedRenderer, ccui.Button.PRESSED_RENDERER_ZORDER, -1);
  145. this.addProtectedChild(this._buttonDisableRenderer, ccui.Button.DISABLED_RENDERER_ZORDER, -1);
  146. if (this._scale9Enabled) {
  147. var ignoreBefore = this._ignoreSize;
  148. this.ignoreContentAdaptWithSize(false);
  149. this._prevIgnoreSize = ignoreBefore;
  150. } else {
  151. this.ignoreContentAdaptWithSize(this._prevIgnoreSize);
  152. }
  153. this.setCapInsetsNormalRenderer(this._capInsetsNormal);
  154. this.setCapInsetsPressedRenderer(this._capInsetsPressed);
  155. this.setCapInsetsDisabledRenderer(this._capInsetsDisabled);
  156. this.setBright(this._bright);
  157. },
  158. /**
  159. * Returns button is using scale9 renderer or not.
  160. * @returns {Boolean}
  161. */
  162. isScale9Enabled: function () {
  163. return this._scale9Enabled;
  164. },
  165. /**
  166. * Sets whether ignore the widget size
  167. * @param {Boolean} ignore true that widget will ignore it's size, use texture size, false otherwise. Default value is true.
  168. * @override
  169. */
  170. ignoreContentAdaptWithSize: function (ignore) {
  171. if (!this._scale9Enabled || (this._scale9Enabled && !ignore)) {
  172. ccui.Widget.prototype.ignoreContentAdaptWithSize.call(this, ignore);
  173. this._prevIgnoreSize = ignore;
  174. }
  175. },
  176. /**
  177. * Returns the renderer size.
  178. * @returns {cc.Size}
  179. */
  180. getVirtualRendererSize: function(){
  181. return cc.size(this._normalTextureSize);
  182. },
  183. /**
  184. * Load textures for button.
  185. * @param {String} normal normal state of texture's filename.
  186. * @param {String} selected selected state of texture's filename.
  187. * @param {String} disabled disabled state of texture's filename.
  188. * @param {ccui.Widget.LOCAL_TEXTURE|ccui.Widget.PLIST_TEXTURE} texType
  189. */
  190. loadTextures: function (normal, selected, disabled, texType) {
  191. this.loadTextureNormal(normal, texType);
  192. this.loadTexturePressed(selected, texType);
  193. this.loadTextureDisabled(disabled, texType);
  194. },
  195. /**
  196. * Load normal state texture for button.
  197. * @param {String} normal normal state of texture's filename.
  198. * @param {ccui.Widget.LOCAL_TEXTURE|ccui.Widget.PLIST_TEXTURE} texType
  199. */
  200. loadTextureNormal: function (normal, texType) {
  201. if (!normal)
  202. return;
  203. texType = texType || ccui.Widget.LOCAL_TEXTURE;
  204. this._normalFileName = normal;
  205. this._normalTexType = texType;
  206. var self = this;
  207. if(!this._buttonNormalRenderer.texture || !this._buttonNormalRenderer.texture.isLoaded()){
  208. this._buttonNormalRenderer.addLoadedEventListener(function(){
  209. self._findLayout();
  210. self._normalTextureSize = self._buttonNormalRenderer.getContentSize();
  211. self._updateFlippedX();
  212. self._updateFlippedY();
  213. self._updateChildrenDisplayedRGBA();
  214. self._buttonNormalRenderer.setColor(self.getColor());
  215. self._buttonNormalRenderer.setOpacity(self.getOpacity());
  216. self._updateContentSizeWithTextureSize(self._normalTextureSize);
  217. self._normalTextureLoaded = true;
  218. self._normalTextureAdaptDirty = true;
  219. });
  220. }
  221. if (this._scale9Enabled) {
  222. var normalRendererScale9 = this._buttonNormalRenderer;
  223. switch (this._normalTexType){
  224. case ccui.Widget.LOCAL_TEXTURE:
  225. normalRendererScale9.initWithFile(normal);
  226. break;
  227. case ccui.Widget.PLIST_TEXTURE:
  228. normalRendererScale9.initWithSpriteFrameName(normal);
  229. break;
  230. default:
  231. break;
  232. }
  233. normalRendererScale9.setCapInsets(this._capInsetsNormal);
  234. } else {
  235. var normalRenderer = this._buttonNormalRenderer;
  236. switch (this._normalTexType){
  237. case ccui.Widget.LOCAL_TEXTURE:
  238. //SetTexture cannot load resource
  239. normalRenderer.initWithFile(normal);
  240. break;
  241. case ccui.Widget.PLIST_TEXTURE:
  242. //SetTexture cannot load resource
  243. normalRenderer.initWithSpriteFrameName(normal);
  244. break;
  245. default:
  246. break;
  247. }
  248. }
  249. this._normalTextureSize = this._buttonNormalRenderer.getContentSize();
  250. this._updateFlippedX();
  251. this._updateFlippedY();
  252. this._updateChildrenDisplayedRGBA();
  253. this._updateContentSizeWithTextureSize(this._normalTextureSize);
  254. this._normalTextureLoaded = true;
  255. this._normalTextureAdaptDirty = true;
  256. },
  257. /**
  258. * Load selected state texture for button.
  259. * @param {String} selected selected state of texture's filename.
  260. * @param {ccui.Widget.LOCAL_TEXTURE|ccui.Widget.PLIST_TEXTURE} texType
  261. */
  262. loadTexturePressed: function (selected, texType) {
  263. if (!selected)
  264. return;
  265. texType = texType || ccui.Widget.LOCAL_TEXTURE;
  266. this._clickedFileName = selected;
  267. this._pressedTexType = texType;
  268. var self = this;
  269. if(!this._buttonClickedRenderer.texture || !this._buttonClickedRenderer.texture.isLoaded()){
  270. this._buttonClickedRenderer.addLoadedEventListener(function(){
  271. self._findLayout();
  272. self._pressedTextureSize = self._buttonClickedRenderer.getContentSize();
  273. self._updateFlippedX();
  274. self._updateFlippedY();
  275. self._updateChildrenDisplayedRGBA();
  276. self._pressedTextureLoaded = true;
  277. self._pressedTextureAdaptDirty = true;
  278. });
  279. }
  280. if (this._scale9Enabled) {
  281. var clickedRendererScale9 = this._buttonClickedRenderer;
  282. switch (this._pressedTexType) {
  283. case ccui.Widget.LOCAL_TEXTURE:
  284. clickedRendererScale9.initWithFile(selected);
  285. break;
  286. case ccui.Widget.PLIST_TEXTURE:
  287. clickedRendererScale9.initWithSpriteFrameName(selected);
  288. break;
  289. default:
  290. break;
  291. }
  292. clickedRendererScale9.setCapInsets(this._capInsetsPressed);
  293. } else {
  294. var clickedRenderer = this._buttonClickedRenderer;
  295. switch (this._pressedTexType) {
  296. case ccui.Widget.LOCAL_TEXTURE:
  297. //SetTexture cannot load resource
  298. clickedRenderer.initWithFile(selected);
  299. break;
  300. case ccui.Widget.PLIST_TEXTURE:
  301. //SetTexture cannot load resource
  302. clickedRenderer.initWithSpriteFrameName(selected);
  303. break;
  304. default:
  305. break;
  306. }
  307. }
  308. this._pressedTextureSize = this._buttonClickedRenderer.getContentSize();
  309. this._updateFlippedX();
  310. this._updateFlippedY();
  311. this._updateChildrenDisplayedRGBA();
  312. this._pressedTextureLoaded = true;
  313. this._pressedTextureAdaptDirty = true;
  314. },
  315. /**
  316. * Load dark state texture for button.
  317. * @param {String} disabled disabled state of texture's filename.
  318. * @param {ccui.Widget.LOCAL_TEXTURE|ccui.Widget.PLIST_TEXTURE} texType
  319. */
  320. loadTextureDisabled: function (disabled, texType) {
  321. if (!disabled)
  322. return;
  323. texType = texType || ccui.Widget.LOCAL_TEXTURE;
  324. this._disabledFileName = disabled;
  325. this._disabledTexType = texType;
  326. var self = this;
  327. if(!this._buttonDisableRenderer.texture || !this._buttonDisableRenderer.texture.isLoaded()){
  328. this._buttonDisableRenderer.addLoadedEventListener(function() {
  329. self._findLayout();
  330. self._disabledTextureSize = self._buttonDisableRenderer.getContentSize();
  331. self._updateFlippedX();
  332. self._updateFlippedY();
  333. self._updateChildrenDisplayedRGBA();
  334. self._disabledTextureLoaded = true;
  335. self._disabledTextureAdaptDirty = true;
  336. });
  337. }
  338. if (this._scale9Enabled) {
  339. var disabledScale9 = this._buttonDisableRenderer;
  340. switch (this._disabledTexType) {
  341. case ccui.Widget.LOCAL_TEXTURE:
  342. disabledScale9.initWithFile(disabled);
  343. break;
  344. case ccui.Widget.PLIST_TEXTURE:
  345. disabledScale9.initWithSpriteFrameName(disabled);
  346. break;
  347. default:
  348. break;
  349. }
  350. disabledScale9.setCapInsets(this._capInsetsDisabled);
  351. } else {
  352. var disabledRenderer = this._buttonDisableRenderer;
  353. switch (this._disabledTexType) {
  354. case ccui.Widget.LOCAL_TEXTURE:
  355. //SetTexture cannot load resource
  356. disabledRenderer.initWithFile(disabled);
  357. break;
  358. case ccui.Widget.PLIST_TEXTURE:
  359. //SetTexture cannot load resource
  360. disabledRenderer.initWithSpriteFrameName(disabled);
  361. break;
  362. default:
  363. break;
  364. }
  365. }
  366. this._disabledTextureSize = this._buttonDisableRenderer.getContentSize();
  367. this._updateFlippedX();
  368. this._updateFlippedY();
  369. this._updateChildrenDisplayedRGBA();
  370. this._disabledTextureLoaded = true;
  371. this._disabledTextureAdaptDirty = true;
  372. },
  373. /**
  374. * Sets capinsets for button, if button is using scale9 renderer.
  375. * @param {cc.Rect} capInsets
  376. */
  377. setCapInsets: function (capInsets) {
  378. this.setCapInsetsNormalRenderer(capInsets);
  379. this.setCapInsetsPressedRenderer(capInsets);
  380. this.setCapInsetsDisabledRenderer(capInsets);
  381. },
  382. /**
  383. * Sets capinsets for button, if button is using scale9 renderer.
  384. * @param {cc.Rect} capInsets
  385. */
  386. setCapInsetsNormalRenderer: function (capInsets) {
  387. if(!capInsets)
  388. return;
  389. var locInsets = this._capInsetsNormal;
  390. locInsets.x = capInsets.x;
  391. locInsets.y = capInsets.y;
  392. locInsets.width = capInsets.width;
  393. locInsets.height = capInsets.height;
  394. if (!this._scale9Enabled)
  395. return;
  396. this._buttonNormalRenderer.setCapInsets(capInsets);
  397. },
  398. /**
  399. * Returns normal renderer cap insets.
  400. * @returns {cc.Rect}
  401. */
  402. getCapInsetsNormalRenderer:function(){
  403. return cc.rect(this._capInsetsNormal);
  404. },
  405. /**
  406. * Sets capinsets for button, if button is using scale9 renderer.
  407. * @param {cc.Rect} capInsets
  408. */
  409. setCapInsetsPressedRenderer: function (capInsets) {
  410. if(!capInsets)
  411. return;
  412. var locInsets = this._capInsetsPressed;
  413. locInsets.x = capInsets.x;
  414. locInsets.y = capInsets.y;
  415. locInsets.width = capInsets.width;
  416. locInsets.height = capInsets.height;
  417. if (!this._scale9Enabled)
  418. return;
  419. this._buttonClickedRenderer.setCapInsets(capInsets);
  420. },
  421. /**
  422. * Returns pressed renderer cap insets.
  423. * @returns {cc.Rect}
  424. */
  425. getCapInsetsPressedRenderer: function () {
  426. return cc.rect(this._capInsetsPressed);
  427. },
  428. /**
  429. * Sets capinsets for button, if button is using scale9 renderer.
  430. * @param {cc.Rect} capInsets
  431. */
  432. setCapInsetsDisabledRenderer: function (capInsets) {
  433. if(!capInsets)
  434. return;
  435. var locInsets = this._capInsetsDisabled;
  436. locInsets.x = capInsets.x;
  437. locInsets.y = capInsets.y;
  438. locInsets.width = capInsets.width;
  439. locInsets.height = capInsets.height;
  440. if (!this._scale9Enabled)
  441. return;
  442. this._buttonDisableRenderer.setCapInsets(capInsets);
  443. },
  444. /**
  445. * Returns disable renderer cap insets.
  446. * @returns {cc.Rect}
  447. */
  448. getCapInsetsDisabledRenderer: function () {
  449. return cc.rect(this._capInsetsDisabled);
  450. },
  451. _onPressStateChangedToNormal: function () {
  452. this._buttonNormalRenderer.setVisible(true);
  453. this._buttonClickedRenderer.setVisible(false);
  454. this._buttonDisableRenderer.setVisible(false);
  455. if (this._pressedTextureLoaded) {
  456. if (this.pressedActionEnabled){
  457. this._buttonNormalRenderer.stopAllActions();
  458. this._buttonClickedRenderer.stopAllActions();
  459. var zoomAction = cc.scaleTo(0.05, this._normalTextureScaleXInSize, this._normalTextureScaleYInSize);
  460. this._buttonNormalRenderer.runAction(zoomAction);
  461. this._buttonClickedRenderer.setScale(this._pressedTextureScaleXInSize, this._pressedTextureScaleYInSize);
  462. }
  463. } else {
  464. if (this._scale9Enabled)
  465. this._updateTexturesRGBA();
  466. else {
  467. this._buttonNormalRenderer.stopAllActions();
  468. this._buttonNormalRenderer.setScale(this._normalTextureScaleXInSize, this._normalTextureScaleYInSize);
  469. }
  470. }
  471. },
  472. _onPressStateChangedToPressed: function () {
  473. var locNormalRenderer = this._buttonNormalRenderer;
  474. if (this._pressedTextureLoaded) {
  475. locNormalRenderer.setVisible(false);
  476. this._buttonClickedRenderer.setVisible(true);
  477. this._buttonDisableRenderer.setVisible(false);
  478. if (this.pressedActionEnabled) {
  479. locNormalRenderer.stopAllActions();
  480. this._buttonClickedRenderer.stopAllActions();
  481. var zoomAction = cc.scaleTo(0.05, this._pressedTextureScaleXInSize + 0.1,this._pressedTextureScaleYInSize + 0.1);
  482. this._buttonClickedRenderer.runAction(zoomAction);
  483. locNormalRenderer.setScale(this._pressedTextureScaleXInSize + 0.1, this._pressedTextureScaleYInSize + 0.1);
  484. }
  485. } else {
  486. locNormalRenderer.setVisible(true);
  487. this._buttonClickedRenderer.setVisible(true);
  488. this._buttonDisableRenderer.setVisible(false);
  489. if (this._scale9Enabled)
  490. locNormalRenderer.setColor(cc.color.GRAY);
  491. else {
  492. locNormalRenderer.stopAllActions();
  493. locNormalRenderer.setScale(this._normalTextureScaleXInSize + 0.1, this._normalTextureScaleYInSize + 0.1);
  494. }
  495. }
  496. },
  497. _onPressStateChangedToDisabled: function () {
  498. this._buttonNormalRenderer.setVisible(false);
  499. this._buttonClickedRenderer.setVisible(false);
  500. this._buttonDisableRenderer.setVisible(true);
  501. this._buttonNormalRenderer.setScale(this._normalTextureScaleXInSize, this._normalTextureScaleYInSize);
  502. this._buttonClickedRenderer.setScale(this._pressedTextureScaleXInSize, this._pressedTextureScaleYInSize);
  503. },
  504. _updateFlippedX: function () {
  505. var flip = this._flippedX ? -1.0 : 1.0;
  506. this._titleRenderer.setScaleX(flip);
  507. if (this._scale9Enabled) {
  508. this._buttonNormalRenderer.setScaleX(flip);
  509. this._buttonClickedRenderer.setScaleX(flip);
  510. this._buttonDisableRenderer.setScaleX(flip);
  511. } else {
  512. this._buttonNormalRenderer.setFlippedX(this._flippedX);
  513. this._buttonClickedRenderer.setFlippedX(this._flippedX);
  514. this._buttonDisableRenderer.setFlippedX(this._flippedX);
  515. }
  516. },
  517. _updateFlippedY: function () {
  518. var flip = this._flippedY ? -1.0 : 1.0;
  519. this._titleRenderer.setScaleY(flip);
  520. if (this._scale9Enabled) {
  521. this._buttonNormalRenderer.setScaleY(flip);
  522. this._buttonClickedRenderer.setScaleY(flip);
  523. this._buttonDisableRenderer.setScaleY(flip);
  524. } else {
  525. this._buttonNormalRenderer.setFlippedY(this._flippedY);
  526. this._buttonClickedRenderer.setFlippedY(this._flippedY);
  527. this._buttonDisableRenderer.setFlippedY(this._flippedY);
  528. }
  529. },
  530. _updateTexturesRGBA: function(){
  531. this._buttonNormalRenderer.setColor(this.getColor());
  532. this._buttonClickedRenderer.setColor(this.getColor());
  533. this._buttonDisableRenderer.setColor(this.getColor());
  534. this._buttonNormalRenderer.setOpacity(this.getOpacity());
  535. this._buttonClickedRenderer.setOpacity(this.getOpacity());
  536. this._buttonDisableRenderer.setOpacity(this.getOpacity());
  537. },
  538. _onSizeChanged: function () {
  539. ccui.Widget.prototype._onSizeChanged.call(this);
  540. this._updateTitleLocation();
  541. this._normalTextureAdaptDirty = true;
  542. this._pressedTextureAdaptDirty = true;
  543. this._disabledTextureAdaptDirty = true;
  544. },
  545. /**
  546. * Gets the Virtual Renderer of widget.
  547. * @returns {cc.Node}
  548. */
  549. getVirtualRenderer: function () {
  550. if (this._bright) {
  551. switch (this._brightStyle) {
  552. case ccui.Widget.BRIGHT_STYLE_NORMAL:
  553. return this._buttonNormalRenderer;
  554. case ccui.Widget.BRIGHT_STYLE_HIGH_LIGHT:
  555. return this._buttonClickedRenderer;
  556. default:
  557. return null;
  558. }
  559. } else
  560. return this._buttonDisableRenderer;
  561. },
  562. _normalTextureScaleChangedWithSize: function () {
  563. if (this._ignoreSize) {
  564. if (!this._scale9Enabled) {
  565. this._buttonNormalRenderer.setScale(1.0);
  566. this._normalTextureScaleXInSize = this._normalTextureScaleYInSize = 1;
  567. }
  568. } else {
  569. if (this._scale9Enabled) {
  570. this._buttonNormalRenderer.setPreferredSize(this._contentSize);
  571. this._normalTextureScaleXInSize = this._normalTextureScaleYInSize = 1;
  572. } else {
  573. var textureSize = this._normalTextureSize;
  574. if (textureSize.width <= 0.0 || textureSize.height <= 0.0) {
  575. this._buttonNormalRenderer.setScale(1.0);
  576. return;
  577. }
  578. var scaleX = this._contentSize.width / textureSize.width;
  579. var scaleY = this._contentSize.height / textureSize.height;
  580. this._buttonNormalRenderer.setScaleX(scaleX);
  581. this._buttonNormalRenderer.setScaleY(scaleY);
  582. this._normalTextureScaleXInSize = scaleX;
  583. this._normalTextureScaleYInSize = scaleY;
  584. }
  585. }
  586. this._buttonNormalRenderer.setPosition(this._contentSize.width / 2.0, this._contentSize.height / 2.0);
  587. },
  588. _pressedTextureScaleChangedWithSize: function () {
  589. if (this._ignoreSize) {
  590. if (!this._scale9Enabled) {
  591. this._buttonClickedRenderer.setScale(1.0);
  592. this._pressedTextureScaleXInSize = this._pressedTextureScaleYInSize = 1;
  593. }
  594. } else {
  595. if (this._scale9Enabled) {
  596. this._buttonClickedRenderer.setPreferredSize(this._contentSize);
  597. this._pressedTextureScaleXInSize = this._pressedTextureScaleYInSize = 1;
  598. } else {
  599. var textureSize = this._pressedTextureSize;
  600. if (textureSize.width <= 0.0 || textureSize.height <= 0.0) {
  601. this._buttonClickedRenderer.setScale(1.0);
  602. return;
  603. }
  604. var scaleX = this._contentSize.width / textureSize.width;
  605. var scaleY = this._contentSize.height / textureSize.height;
  606. this._buttonClickedRenderer.setScaleX(scaleX);
  607. this._buttonClickedRenderer.setScaleY(scaleY);
  608. this._pressedTextureScaleXInSize = scaleX;
  609. this._pressedTextureScaleYInSize = scaleY;
  610. }
  611. }
  612. this._buttonClickedRenderer.setPosition(this._contentSize.width / 2.0, this._contentSize.height / 2.0);
  613. },
  614. _disabledTextureScaleChangedWithSize: function () {
  615. if (this._ignoreSize) {
  616. if (!this._scale9Enabled)
  617. this._buttonDisableRenderer.setScale(1.0);
  618. } else {
  619. if (this._scale9Enabled)
  620. this._buttonDisableRenderer.setPreferredSize(this._contentSize);
  621. else {
  622. var textureSize = this._disabledTextureSize;
  623. if (textureSize.width <= 0.0 || textureSize.height <= 0.0) {
  624. this._buttonDisableRenderer.setScale(1.0);
  625. return;
  626. }
  627. var scaleX = this._contentSize.width / textureSize.width;
  628. var scaleY = this._contentSize.height / textureSize.height;
  629. this._buttonDisableRenderer.setScaleX(scaleX);
  630. this._buttonDisableRenderer.setScaleY(scaleY);
  631. }
  632. }
  633. this._buttonDisableRenderer.setPosition(this._contentSize.width / 2.0, this._contentSize.height / 2.0);
  634. },
  635. _adaptRenderers: function(){
  636. if (this._normalTextureAdaptDirty) {
  637. this._normalTextureScaleChangedWithSize();
  638. this._normalTextureAdaptDirty = false;
  639. }
  640. if (this._pressedTextureAdaptDirty) {
  641. this._pressedTextureScaleChangedWithSize();
  642. this._pressedTextureAdaptDirty = false;
  643. }
  644. if (this._disabledTextureAdaptDirty) {
  645. this._disabledTextureScaleChangedWithSize();
  646. this._disabledTextureAdaptDirty = false;
  647. }
  648. },
  649. _updateTitleLocation: function(){
  650. this._titleRenderer.setPosition(this._contentSize.width * 0.5, this._contentSize.height * 0.5);
  651. },
  652. /**
  653. * Changes if button can be clicked zoom effect.
  654. * @param {Boolean} enabled
  655. */
  656. setPressedActionEnabled: function (enabled) {
  657. this.pressedActionEnabled = enabled;
  658. },
  659. /**
  660. * Sets title text to ccui.Button
  661. * @param {String} text
  662. */
  663. setTitleText: function (text) {
  664. this._titleRenderer.setString(text);
  665. },
  666. /**
  667. * Returns title text of ccui.Button
  668. * @returns {String} text
  669. */
  670. getTitleText: function () {
  671. return this._titleRenderer.getString();
  672. },
  673. /**
  674. * Sets title color to ccui.Button.
  675. * @param {cc.Color} color
  676. */
  677. setTitleColor: function (color) {
  678. this._titleColor.r = color.r;
  679. this._titleColor.g = color.g;
  680. this._titleColor.b = color.b;
  681. this._titleRenderer.updateDisplayedColor(color);
  682. },
  683. /**
  684. * Returns title color of ccui.Button
  685. * @returns {cc.Color}
  686. */
  687. getTitleColor: function () {
  688. return this._titleRenderer.getColor();
  689. },
  690. /**
  691. * Sets title fontSize to ccui.Button
  692. * @param {cc.Size} size
  693. */
  694. setTitleFontSize: function (size) {
  695. this._titleRenderer.setFontSize(size);
  696. },
  697. /**
  698. * Returns title fontSize of ccui.Button.
  699. * @returns {cc.Size}
  700. */
  701. getTitleFontSize: function () {
  702. return this._titleRenderer.getFontSize();
  703. },
  704. /**
  705. * Sets title fontName to ccui.Button.
  706. * @param {String} fontName
  707. */
  708. setTitleFontName: function (fontName) {
  709. this._titleRenderer.setFontName(fontName);
  710. this._fontName = fontName;
  711. },
  712. /**
  713. * Gets title fontName of ccui.Button.
  714. * @returns {String}
  715. */
  716. getTitleFontName: function () {
  717. return this._titleRenderer.getFontName();
  718. },
  719. _setTitleFont: function (font) {
  720. this._titleRenderer.font = font;
  721. },
  722. _getTitleFont: function () {
  723. return this._titleRenderer.font;
  724. },
  725. /**
  726. * Returns the "class name" of widget.
  727. * @override
  728. * @returns {string}
  729. */
  730. getDescription: function () {
  731. return "Button";
  732. },
  733. _createCloneInstance: function () {
  734. return ccui.Button.create();
  735. },
  736. _copySpecialProperties: function (uiButton) {
  737. this._prevIgnoreSize = uiButton._prevIgnoreSize;
  738. this.setScale9Enabled(uiButton._scale9Enabled);
  739. this.loadTextureNormal(uiButton._normalFileName, uiButton._normalTexType);
  740. this.loadTexturePressed(uiButton._clickedFileName, uiButton._pressedTexType);
  741. this.loadTextureDisabled(uiButton._disabledFileName, uiButton._disabledTexType);
  742. this.setCapInsetsNormalRenderer(uiButton._capInsetsNormal);
  743. this.setCapInsetsPressedRenderer(uiButton._capInsetsPressed);
  744. this.setCapInsetsDisabledRenderer(uiButton._capInsetsDisabled);
  745. this.setTitleText(uiButton.getTitleText());
  746. this.setTitleFontName(uiButton.getTitleFontName());
  747. this.setTitleFontSize(uiButton.getTitleFontSize());
  748. this.setTitleColor(uiButton.getTitleColor());
  749. this.setPressedActionEnabled(uiButton.pressedActionEnabled);
  750. }
  751. });
  752. var _p = ccui.Button.prototype;
  753. // Extended properties
  754. /** @expose */
  755. _p.titleText;
  756. cc.defineGetterSetter(_p, "titleText", _p.getTitleText, _p.setTitleText);
  757. /** @expose */
  758. _p.titleFont;
  759. cc.defineGetterSetter(_p, "titleFont", _p._getTitleFont, _p._setTitleFont);
  760. /** @expose */
  761. _p.titleFontSize;
  762. cc.defineGetterSetter(_p, "titleFontSize", _p.getTitleFontSize, _p.setTitleFontSize);
  763. /** @expose */
  764. _p.titleFontName;
  765. cc.defineGetterSetter(_p, "titleFontName", _p.getTitleFontName, _p.setTitleFontName);
  766. /** @expose */
  767. _p.titleColor;
  768. cc.defineGetterSetter(_p, "titleColor", _p.getTitleColor, _p.setTitleColor);
  769. _p = null;
  770. /**
  771. * allocates and initializes a UIButton.
  772. * @deprecated since v3.0, please use new ccui.Button() instead.
  773. * @param {string} [normalImage] normal state texture name
  774. * @param {string} [selectedImage] selected state texture name
  775. * @param {string} [disableImage] disabled state texture name
  776. * @param {string} [texType]
  777. * @return {ccui.Button}
  778. * @example
  779. * // example
  780. * var uiButton = ccui.Button.create();
  781. */
  782. ccui.Button.create = function (normalImage, selectedImage, disableImage, texType) {
  783. return new ccui.Button(normalImage, selectedImage, disableImage, texType);
  784. };
  785. // Constants
  786. /**
  787. * The normal renderer's zOrder value.
  788. * @constant
  789. * @type {number}
  790. */
  791. ccui.Button.NORMAL_RENDERER_ZORDER = -2;
  792. /**
  793. * The pressed renderer's zOrder value.
  794. * @constant
  795. * @type {number}
  796. */
  797. ccui.Button.PRESSED_RENDERER_ZORDER = -2;
  798. /**
  799. * The disabled renderer's zOrder value.
  800. * @constant
  801. * @type {number}
  802. */
  803. ccui.Button.DISABLED_RENDERER_ZORDER = -2;
  804. /**
  805. * The title renderer's zOrder value.
  806. * @constant
  807. * @type {number}
  808. */
  809. ccui.Button.TITLE_RENDERER_ZORDER = -1;
  810. /**
  811. * @ignore
  812. */
  813. ccui.Button.SYSTEM = 0;
  814. ccui.Button.TTF = 1;