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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import {
  2. CodeControl,
  3. ControlType,
  4. NumberCodeControl,
  5. VectorCodeControl,
  6. } from "types"
  7. import { v4 as uuid } from "uuid"
  8. import Vector from "./vector"
  9. export const controls: Record<string, any> = {}
  10. export const codeControls = new Set<CodeControl>([])
  11. export class Control<T extends CodeControl> {
  12. control: T
  13. constructor(control: Omit<T, "id">) {
  14. this.control = { ...control, id: uuid() } 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() {
  24. codeControls.delete(this.control)
  25. delete controls[this.control.label]
  26. }
  27. }
  28. export class NumberControl extends Control<NumberCodeControl> {
  29. constructor(options: Omit<NumberCodeControl, "id" | "type">) {
  30. const { value = 0, step = 1 } = options
  31. super({
  32. type: ControlType.Number,
  33. ...options,
  34. value,
  35. step,
  36. })
  37. }
  38. }
  39. export class VectorControl extends Control<VectorCodeControl> {
  40. constructor(options: Omit<VectorCodeControl, "id" | "type">) {
  41. const { value = [0, 0], isNormalized = false } = options
  42. super({
  43. type: ControlType.Vector,
  44. ...options,
  45. value,
  46. isNormalized,
  47. })
  48. }
  49. }