You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

inputs.tsx 4.8KB

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