Switch.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /** @format */
  2. import {LANGUAGE_TYPE} from '../enums/Enum'
  3. import {ccUtils} from '../utils/ccUtils'
  4. const {ccclass, property, executeInEditMode} = cc._decorator
  5. @ccclass
  6. @executeInEditMode
  7. export default class Switch extends cc.Component {
  8. @property({displayName: '是否启用'})
  9. interact: boolean = false
  10. @property({type: cc.Boolean})
  11. get isChecked(): boolean {
  12. return this._isChecked
  13. }
  14. set isChecked(value: boolean) {
  15. if (this.interact) {
  16. this._isChecked = value
  17. this.updateTogglePosition(this.isClicked)
  18. if (this.isClicked) this.emitCheckEvents()
  19. this.isClicked = false
  20. }
  21. }
  22. @property({type: cc.Node, displayName: '开启滑块'})
  23. switchOnNode: cc.Node = null
  24. @property({type: cc.Node, displayName: '关闭滑块'})
  25. switchOffNode: cc.Node = null
  26. @property({type: cc.Node, displayName: '选择背景'})
  27. background: cc.Node = null
  28. @property({displayName: '选择背景开启颜色'})
  29. bgOnColor: cc.Color = cc.Color.WHITE.fromHEX('#FAC73E')
  30. @property({displayName: '选择背景关闭颜色'})
  31. bgOffColor: cc.Color = cc.Color.WHITE.fromHEX('#5B5B5B')
  32. @property({displayName: '滑动起点'})
  33. onPosition: cc.Vec2 = cc.v2(50, 0)
  34. @property({displayName: '滑动终点'})
  35. offPosition: cc.Vec2 = cc.v2(-50, 0)
  36. @property()
  37. private _isChecked: boolean = false
  38. isClicked: boolean = false
  39. // 定义一个事件
  40. @property({type: [cc.Component.EventHandler]})
  41. checkEvents: cc.Component.EventHandler[] = []
  42. onLoad() {
  43. this.updateTogglePosition(false)
  44. this.node.on(cc.Node.EventType.TOUCH_END, this.onToggle, this)
  45. }
  46. onDestroy() {
  47. this.node.off(cc.Node.EventType.TOUCH_END, this.onToggle, this)
  48. }
  49. onToggle() {
  50. this.isClicked = true
  51. this.isChecked = !this.isChecked
  52. }
  53. updateChecked(value: boolean) {
  54. this._isChecked = value
  55. this.updateTogglePosition(false)
  56. }
  57. updateTogglePosition(animated: boolean) {
  58. if (!this.switchOnNode || !this.switchOffNode) return
  59. this.switchOnNode.active = this.isChecked
  60. this.switchOffNode.active = !this.isChecked
  61. let switchNode = this.isChecked ? this.switchOnNode : this.switchOffNode
  62. let position = this.isChecked ? this.onPosition : this.offPosition
  63. let opacity = this.isChecked ? 255 : 0
  64. if (animated) {
  65. switchNode.setPosition(this.isChecked ? this.offPosition : this.onPosition)
  66. cc.tween(switchNode)
  67. .to(0.2, {position: cc.v3(position)})
  68. .start()
  69. if (this.background) cc.tween(this.background).to(0.2, {opacity: opacity}).start()
  70. } else {
  71. switchNode.setPosition(position)
  72. }
  73. if (this.background) this.background.color = this.isChecked ? this.bgOnColor : this.bgOffColor
  74. }
  75. emitCheckEvents() {
  76. cc.Component.EventHandler.emitEvents(this.checkEvents, this.isChecked)
  77. }
  78. }