UserButton.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /** @format */
  2. import UserButtonData from './UserButtonData'
  3. import {ccUtils} from '../utils/ccUtils'
  4. import {Mgr} from '../GameControl'
  5. import {SOUND} from '../enums/Sound'
  6. const {ccclass, requireComponent, executeInEditMode, menu, inspector} = cc._decorator
  7. @ccclass
  8. @menu('自定义组件/UserButton')
  9. @executeInEditMode()
  10. @requireComponent(UserButtonData)
  11. @inspector('packages://inspector/inspectors/comps/button.js')
  12. export default class UserButton extends cc.Button {
  13. public buttonData: UserButtonData
  14. public openContinuous = true
  15. public continuousTime: number = 0.2
  16. public continuous: boolean = false
  17. public _continuousTimer: any = -1
  18. public multiTouch: boolean = true
  19. public onEnable() {
  20. this.buttonData = this.node.getComponent(UserButtonData)
  21. if (this.buttonData) {
  22. this.openContinuous = this.buttonData.openContinuous
  23. this.continuousTime = this.buttonData.continuousTime
  24. this.multiTouch = this.buttonData.multiTouch
  25. }
  26. this.continuous = false
  27. // this.transition = cc.Button.Transition.SCALE;
  28. super.onEnable()
  29. }
  30. public onDisable() {
  31. if (this._continuousTimer >= 0) {
  32. clearTimeout(this._continuousTimer)
  33. this._continuousTimer = -1
  34. }
  35. super.onDisable()
  36. }
  37. // tslint:disable-next-line: function-name
  38. public _onTouchBegan(event) {
  39. if (!this.interactable || !this.enabledInHierarchy) {
  40. return
  41. }
  42. if (!this.multiTouch && ccUtils.cannotTouch()) {
  43. return
  44. }
  45. this['_pressed'] = true
  46. this['_updateState']()
  47. Mgr.audio.playSFX(SOUND.clickBtn)
  48. event.stopPropagation()
  49. }
  50. // tslint:disable-next-line: function-name
  51. public _onTouchEnded(event) {
  52. if (!this.interactable || !this.enabledInHierarchy) {
  53. return
  54. }
  55. if (this['_pressed'] && !this.continuous) {
  56. this.continuous = !!this.openContinuous
  57. cc.Component.EventHandler.emitEvents(this.clickEvents, event)
  58. this.node.emit('click', this)
  59. if (this.openContinuous) {
  60. this._continuousTimer = setTimeout(() => {
  61. this.continuous = false
  62. }, this.continuousTime * 1000)
  63. }
  64. }
  65. this['_pressed'] = false
  66. this['_updateState']()
  67. event.stopPropagation()
  68. }
  69. }