Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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