Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

control.tsx 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import state, { useSelector } from "state"
  2. import styled from "styles"
  3. import {
  4. ControlType,
  5. NumberCodeControl,
  6. SelectCodeControl,
  7. TextCodeControl,
  8. VectorCodeControl,
  9. } from "types"
  10. export default function Control({ id }: { id: string }) {
  11. const control = useSelector((s) => s.data.codeControls[id])
  12. if (!control) return null
  13. return (
  14. <>
  15. <label>{control.label}</label>
  16. {control.type === ControlType.Number ? (
  17. <NumberControl {...control} />
  18. ) : control.type === ControlType.Vector ? (
  19. <VectorControl {...control} />
  20. ) : control.type === ControlType.Text ? (
  21. <TextControl {...control} />
  22. ) : control.type === ControlType.Select ? (
  23. <SelectControl {...control} />
  24. ) : null}
  25. </>
  26. )
  27. }
  28. function NumberControl({ id, min, max, step, value }: NumberCodeControl) {
  29. return (
  30. <Inputs>
  31. <input
  32. type="range"
  33. min={min}
  34. max={max}
  35. step={step}
  36. value={value}
  37. onChange={(e) =>
  38. state.send("CHANGED_CODE_CONTROL", {
  39. [id]: Number(e.currentTarget.value),
  40. })
  41. }
  42. />
  43. <input
  44. type="number"
  45. min={min}
  46. max={max}
  47. step={step}
  48. value={value}
  49. onChange={(e) =>
  50. state.send("CHANGED_CODE_CONTROL", {
  51. [id]: Number(e.currentTarget.value),
  52. })
  53. }
  54. />
  55. </Inputs>
  56. )
  57. }
  58. function VectorControl({ id, value, isNormalized }: VectorCodeControl) {
  59. return (
  60. <Inputs>
  61. <input
  62. type="range"
  63. min={isNormalized ? -1 : -Infinity}
  64. max={isNormalized ? 1 : Infinity}
  65. step={0.01}
  66. value={value[0]}
  67. onChange={(e) =>
  68. state.send("CHANGED_CODE_CONTROL", {
  69. [id]: [Number(e.currentTarget.value), value[1]],
  70. })
  71. }
  72. />
  73. <input
  74. type="number"
  75. min={isNormalized ? -1 : -Infinity}
  76. max={isNormalized ? 1 : Infinity}
  77. step={0.01}
  78. value={value[0]}
  79. onChange={(e) =>
  80. state.send("CHANGED_CODE_CONTROL", {
  81. [id]: [Number(e.currentTarget.value), value[1]],
  82. })
  83. }
  84. />
  85. <input
  86. type="range"
  87. min={isNormalized ? -1 : -Infinity}
  88. max={isNormalized ? 1 : Infinity}
  89. step={0.01}
  90. value={value[1]}
  91. onChange={(e) =>
  92. state.send("CHANGED_CODE_CONTROL", {
  93. [id]: [value[0], Number(e.currentTarget.value)],
  94. })
  95. }
  96. />
  97. <input
  98. type="number"
  99. min={isNormalized ? -1 : -Infinity}
  100. max={isNormalized ? 1 : Infinity}
  101. step={0.01}
  102. value={value[1]}
  103. onChange={(e) =>
  104. state.send("CHANGED_CODE_CONTROL", {
  105. [id]: [value[0], Number(e.currentTarget.value)],
  106. })
  107. }
  108. />
  109. </Inputs>
  110. )
  111. }
  112. function TextControl({}: TextCodeControl) {
  113. return <></>
  114. }
  115. function SelectControl({}: SelectCodeControl) {
  116. return <></>
  117. }
  118. const Inputs = styled("div", {
  119. display: "flex",
  120. gap: "8px",
  121. height: "100%",
  122. "& input": {
  123. font: "$ui",
  124. width: "64px",
  125. fontSize: "$1",
  126. border: "1px solid $inputBorder",
  127. backgroundColor: "$input",
  128. color: "$text",
  129. height: "100%",
  130. padding: "0px 6px",
  131. },
  132. "& input[type='range']": {
  133. padding: 0,
  134. flexGrow: 2,
  135. },
  136. })