Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

control.ts 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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: Omit<T, 'id'>) {
  14. this.control = { ...control, id: uniqueId() } as T
  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 value(): T['value'] {
  28. return this.control.value
  29. }
  30. set value(value: T['value']) {
  31. this.control.value = value
  32. }
  33. }
  34. type ControlProps<T extends CodeControl> = Omit<Partial<T>, 'id' | 'type'>
  35. export class NumberControl extends Control<NumberCodeControl> {
  36. constructor(options: ControlProps<NumberCodeControl>) {
  37. const { label = 'Number', value = 0, step = 1 } = options
  38. super({
  39. type: ControlType.Number,
  40. ...options,
  41. label,
  42. value,
  43. step,
  44. })
  45. }
  46. }
  47. export class VectorControl extends Control<VectorCodeControl> {
  48. constructor(options: ControlProps<VectorCodeControl>) {
  49. const { label = 'Vector', value = [0, 0], isNormalized = false } = options
  50. super({
  51. type: ControlType.Vector,
  52. ...options,
  53. label,
  54. value,
  55. isNormalized,
  56. })
  57. }
  58. }