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.

control.ts 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import {
  2. CodeControl,
  3. ControlType,
  4. NumberCodeControl,
  5. VectorCodeControl,
  6. } from "types"
  7. import { v4 as uuid } from "uuid"
  8. export const controls: Record<string, any> = {}
  9. export const codeControls = new Set<CodeControl>([])
  10. export class Control<T extends CodeControl> {
  11. control: T
  12. constructor(control: Omit<T, "id">) {
  13. this.control = { ...control, id: uuid() } as T
  14. codeControls.add(this.control)
  15. // Could there be a better way to prevent this?
  16. // When updating, constructor should just bind to
  17. // the existing control rather than creating a new one?
  18. if (!(window as any).isUpdatingCode) {
  19. controls[this.control.label] = this.control.value
  20. }
  21. }
  22. destroy() {
  23. codeControls.delete(this.control)
  24. delete controls[this.control.label]
  25. }
  26. }
  27. export class NumberControl extends Control<NumberCodeControl> {
  28. constructor(options: Omit<NumberCodeControl, "id" | "type">) {
  29. const { value = 0, step = 1 } = options
  30. super({
  31. type: ControlType.Number,
  32. ...options,
  33. value,
  34. step,
  35. })
  36. }
  37. }
  38. export class VectorControl extends Control<VectorCodeControl> {
  39. constructor(options: Omit<VectorCodeControl, "id" | "type">) {
  40. const { value = [0, 0], isNormalized = false } = options
  41. super({
  42. type: ControlType.Vector,
  43. ...options,
  44. value,
  45. isNormalized,
  46. })
  47. }
  48. }