JMButton.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /**
  2. * 嘉米自定义按钮
  3. */
  4. cc.Class({
  5. extends: cc.Component,
  6. editor: {
  7. executeInEditMode: true,
  8. menu: '嘉米公用/JMButton'
  9. },
  10. properties: {
  11. spriteNameFormat: 'btn_label_a1_%s',
  12. button: {
  13. default: undefined,
  14. type: cc.Button,
  15. tooltip: '按钮句柄'
  16. },
  17. sprite: {
  18. default: undefined,
  19. type: cc.Sprite,
  20. tooltip: '按钮背景'
  21. },
  22. label: {
  23. default: undefined,
  24. type: cc.RichText,
  25. tooltip: '按钮标题富文本'
  26. },
  27. spriteAtlas: {
  28. default: undefined,
  29. type: cc.SpriteAtlas,
  30. tooltip: '背景所在的 atlas:unt_btn'
  31. },
  32. buttonType: {
  33. type: JMC.BUTTON_TYPE,
  34. get () {
  35. return this._buttonType;
  36. },
  37. set (value) {
  38. if (this._buttonType !== value) {
  39. this._buttonType = value;
  40. if (CC_EDITOR) {
  41. this._debouncedUpdateSprite();
  42. } else {
  43. this._updateSprite();
  44. }
  45. }
  46. },
  47. tooltip: '按钮类型。修改时,自动刷新按钮背景的 SpriteFrame'
  48. },
  49. _buttonType: {
  50. default: JMC.BUTTON_TYPE.GREEN,
  51. type: JMC.BUTTON_TYPE
  52. },
  53. interactable: {
  54. type: cc.Boolean,
  55. get () {
  56. return this._interactable;
  57. },
  58. set (value) {
  59. if (this.button) {
  60. this.button.interactable = value;
  61. }
  62. if (this._interactable !== value) {
  63. this._interactable = value;
  64. if (CC_EDITOR) {
  65. this._debouncedUpdateLabel();
  66. } else {
  67. this._updateLabel();
  68. }
  69. }
  70. },
  71. tooltip: 'Interactabel将同步Button的Interactabel值,同时影响richText的文案展示。注意:请不要直接操作Button的Interactabel值'
  72. },
  73. _interactable: true,
  74. title: {
  75. type: cc.String,
  76. get () {
  77. return this._title;
  78. },
  79. set (value) {
  80. if (this._title !== value) {
  81. this._title = value;
  82. if (CC_EDITOR) {
  83. this._debouncedUpdateLabel();
  84. } else {
  85. this._updateLabel();
  86. }
  87. }
  88. },
  89. tooltip: 'Interactabel是true时,richText展示的文案。但是,如果Disabled Title是空,则还是展示Title',
  90. multiline: true
  91. },
  92. _title: '按钮',
  93. disabledTitle: {
  94. type: cc.String,
  95. get () {
  96. return this._disabledTitle;
  97. },
  98. set (value) {
  99. if (this._disabledTitle !== value) {
  100. this._disabledTitle = value;
  101. if (CC_EDITOR) {
  102. this._debouncedUpdateLabel();
  103. } else {
  104. this._updateLabel();
  105. }
  106. }
  107. },
  108. tooltip: 'Interactabel是false时,richText展示的文案。但是,如果是空,则展示Title',
  109. multiline: true
  110. },
  111. _disabledTitle: ''
  112. },
  113. onLoad () {
  114. if (CC_EDITOR) {
  115. this._debouncedUpdateSprite = G.ViewUtils.debounce(this._updateSprite, 200);
  116. this._debouncedUpdateLabel = G.ViewUtils.debounce(this._updateLabel, 200);
  117. }
  118. this._updateSprite();
  119. this._updateLabel();
  120. },
  121. /**
  122. * 添加按钮点击事件回调
  123. *
  124. * @author Pyden
  125. * @date 2019-03-21
  126. * @param {cc.Component.EventHandler} clickEventHandler
  127. */
  128. pushClickEvent (clickEventHandler) {
  129. if (!this.button) {
  130. return;
  131. }
  132. this.button.clickEvents.push(clickEventHandler);
  133. },
  134. // ---------------- 私有方法分割线 ----------------
  135. /**
  136. * 刷新按钮背景
  137. *
  138. * @author Pyden
  139. * @date 2019-03-21
  140. */
  141. _updateSprite () {
  142. if (!this.sprite) {
  143. return;
  144. }
  145. if (!this.spriteAtlas) {
  146. return;
  147. }
  148. let spriteName = this._getSpriteName(this.buttonType);
  149. this.sprite.spriteFrame = this.spriteAtlas.getSpriteFrame(spriteName);
  150. },
  151. /**
  152. * 刷新标题文案
  153. *
  154. * @author Pyden
  155. * @date 2019-03-21
  156. */
  157. _updateLabel () {
  158. if (!this.label) {
  159. return;
  160. }
  161. this.button.interactable = this._interactable;
  162. if (this.disabledTitle.length > 0 && !this.interactable) {
  163. this.label.string = this.disabledTitle;
  164. } else {
  165. this.label.string = this.title;
  166. }
  167. },
  168. /**
  169. * 各个按钮类型映射spriteFrameName的字典
  170. *
  171. * @author Pyden
  172. * @date 2019-03-21
  173. * @param {JMC.BUTTON_TYPE} buttonType 按钮类型
  174. * @returns {string} 精灵名称
  175. */
  176. _getSpriteName (buttonType) {
  177. let map = this._getSpriteNameMap();
  178. let spriteName = map[buttonType];
  179. spriteName = cc.js.formatStr(this.spriteNameFormat, spriteName);
  180. return spriteName;
  181. },
  182. /**
  183. * 各个按钮类型映射spriteFrameName的字典
  184. *
  185. * @author Pyden
  186. * @date 2019-03-21
  187. * @returns {map}
  188. */
  189. _getSpriteNameMap () {
  190. return {
  191. [JMC.BUTTON_TYPE.GREEN]: 'green',
  192. [JMC.BUTTON_TYPE.JU_HUANG]: 'juhuang',
  193. [JMC.BUTTON_TYPE.YELLOW]: 'yellow'
  194. };
  195. }
  196. });