Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

inputs.tsx 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import React from 'react'
  2. import { PointerInfo } from 'types'
  3. import vec from 'utils/vec'
  4. import { isDarwin, getPoint } from 'utils/utils'
  5. const DOUBLE_CLICK_DURATION = 250
  6. class Inputs {
  7. activePointerId?: number
  8. pointerUpTime = 0
  9. points: Record<string, PointerInfo> = {}
  10. pointer: PointerInfo
  11. touchStart(e: TouchEvent | React.TouchEvent, target: string) {
  12. const { shiftKey, ctrlKey, metaKey, altKey } = e
  13. e.preventDefault()
  14. const touch = e.changedTouches[0]
  15. const info = {
  16. target,
  17. pointerId: touch.identifier,
  18. origin: getPoint(touch),
  19. point: getPoint(touch),
  20. pressure: 0.5,
  21. shiftKey,
  22. ctrlKey,
  23. metaKey: isDarwin() ? metaKey : ctrlKey,
  24. altKey,
  25. }
  26. this.points[touch.identifier] = info
  27. this.activePointerId = touch.identifier
  28. this.pointer = info
  29. return info
  30. }
  31. touchMove(e: TouchEvent | React.TouchEvent) {
  32. const { shiftKey, ctrlKey, metaKey, altKey } = e
  33. e.preventDefault()
  34. const touch = e.changedTouches[0]
  35. const prev = this.points[touch.identifier]
  36. const info = {
  37. ...prev,
  38. pointerId: touch.identifier,
  39. point: getPoint(touch),
  40. pressure: 0.5,
  41. shiftKey,
  42. ctrlKey,
  43. metaKey: isDarwin() ? metaKey : ctrlKey,
  44. altKey,
  45. }
  46. if (this.points[touch.identifier]) {
  47. this.points[touch.identifier] = info
  48. }
  49. this.pointer = info
  50. return info
  51. }
  52. pointerDown(e: PointerEvent | React.PointerEvent, target: string) {
  53. const { shiftKey, ctrlKey, metaKey, altKey } = e
  54. const info = {
  55. target,
  56. pointerId: e.pointerId,
  57. origin: getPoint(e),
  58. point: getPoint(e),
  59. pressure: e.pressure || 0.5,
  60. shiftKey,
  61. ctrlKey,
  62. metaKey: isDarwin() ? metaKey : ctrlKey,
  63. altKey,
  64. }
  65. this.points[e.pointerId] = info
  66. this.activePointerId = e.pointerId
  67. this.pointer = info
  68. return info
  69. }
  70. pointerEnter(e: PointerEvent | React.PointerEvent, target: string) {
  71. const { shiftKey, ctrlKey, metaKey, altKey } = e
  72. const info = {
  73. target,
  74. pointerId: e.pointerId,
  75. origin: getPoint(e),
  76. point: getPoint(e),
  77. pressure: e.pressure || 0.5,
  78. shiftKey,
  79. ctrlKey,
  80. metaKey: isDarwin() ? metaKey : ctrlKey,
  81. altKey,
  82. }
  83. this.pointer = info
  84. return info
  85. }
  86. pointerMove(e: PointerEvent | React.PointerEvent, target = '') {
  87. const { shiftKey, ctrlKey, metaKey, altKey } = e
  88. const prev = this.points[e.pointerId]
  89. const info = {
  90. ...prev,
  91. target,
  92. pointerId: e.pointerId,
  93. point: getPoint(e),
  94. pressure: e.pressure || 0.5,
  95. shiftKey,
  96. ctrlKey,
  97. metaKey: isDarwin() ? metaKey : ctrlKey,
  98. altKey,
  99. }
  100. if (this.points[e.pointerId]) {
  101. this.points[e.pointerId] = info
  102. }
  103. this.pointer = info
  104. return info
  105. }
  106. pointerUp = (e: PointerEvent | React.PointerEvent, target = '') => {
  107. const { shiftKey, ctrlKey, metaKey, altKey } = e
  108. const prev = this.points[e.pointerId]
  109. const info = {
  110. ...prev,
  111. target,
  112. origin: prev?.origin || getPoint(e),
  113. point: getPoint(e),
  114. pressure: e.pressure || 0.5,
  115. shiftKey,
  116. ctrlKey,
  117. metaKey: isDarwin() ? metaKey : ctrlKey,
  118. altKey,
  119. }
  120. delete this.points[e.pointerId]
  121. delete this.activePointerId
  122. if (vec.dist(info.origin, info.point) < 8) {
  123. this.pointerUpTime = Date.now()
  124. }
  125. this.pointer = info
  126. return info
  127. }
  128. wheel = (e: WheelEvent) => {
  129. const { shiftKey, ctrlKey, metaKey, altKey } = e
  130. return { point: getPoint(e), shiftKey, ctrlKey, metaKey, altKey }
  131. }
  132. canAccept = (pointerId: PointerEvent['pointerId']) => {
  133. return (
  134. this.activePointerId === undefined || this.activePointerId === pointerId
  135. )
  136. }
  137. isDoubleClick() {
  138. if (!this.pointer) return
  139. const { origin, point } = this.pointer
  140. return (
  141. Date.now() - this.pointerUpTime < DOUBLE_CLICK_DURATION &&
  142. vec.dist(origin, point) < 8
  143. )
  144. }
  145. clear() {
  146. this.activePointerId = undefined
  147. this.pointer = undefined
  148. this.points = {}
  149. }
  150. resetDoubleClick() {
  151. this.pointerUpTime = 0
  152. }
  153. }
  154. export default new Inputs()