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

control.ts 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import {
  2. CodeControl,
  3. ControlType,
  4. NumberCodeControl,
  5. VectorCodeControl,
  6. } from 'types'
  7. import { uniqueId } from 'utils'
  8. export const controls: Record<string, any> = {}
  9. export const codeControls = new Set<CodeControl>([])
  10. /* ----------------- Start Copy Here ---------------- */
  11. export class Control<T extends CodeControl> {
  12. _control: T
  13. constructor(control: T) {
  14. this._control = { ...control }
  15. codeControls.add(this._control)
  16. // Could there be a better way to prevent this?
  17. // When updating, constructor should just bind to
  18. // the existing control rather than creating a new one?
  19. if (!(window as any).isUpdatingCode) {
  20. controls[this._control.label] = this._control.value
  21. }
  22. }
  23. destroy(): void {
  24. codeControls.delete(this.control)
  25. delete controls[this.control.label]
  26. }
  27. get control(): T {
  28. return this._control
  29. }
  30. get id(): string {
  31. return this.control.id
  32. }
  33. get value(): T['value'] {
  34. return this.control.value
  35. }
  36. }
  37. type ControlProps<T extends CodeControl> = Omit<Partial<T>, 'type'>
  38. export class NumberControl extends Control<NumberCodeControl> {
  39. constructor(options: ControlProps<NumberCodeControl>) {
  40. const { id = uniqueId(), label = 'Number', value = 0, step = 1 } = options
  41. super({
  42. type: ControlType.Number,
  43. ...options,
  44. label,
  45. value,
  46. step,
  47. id,
  48. })
  49. }
  50. }
  51. export class VectorControl extends Control<VectorCodeControl> {
  52. constructor(options: ControlProps<VectorCodeControl>) {
  53. const {
  54. id = uniqueId(),
  55. label = 'Vector',
  56. value = [0, 0],
  57. isNormalized = false,
  58. } = options
  59. super({
  60. type: ControlType.Vector,
  61. ...options,
  62. label,
  63. value,
  64. isNormalized,
  65. id,
  66. })
  67. }
  68. }