Values.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. import { EventDispatcher } from 'three';
  2. class Value extends EventDispatcher {
  3. constructor() {
  4. super();
  5. this.domElement = document.createElement( 'div' );
  6. this.domElement.className = 'param-control';
  7. this._onChangeFunction = null;
  8. this._changeTimeout = null;
  9. this._debounceTime = 0;
  10. this.addEventListener( 'change', ( e ) => {
  11. // defer to avoid issues when changing multiple values in the same call stack
  12. clearTimeout( this._changeTimeout );
  13. this._changeTimeout = setTimeout( () => {
  14. if ( this._onChangeFunction ) this._onChangeFunction( e.value );
  15. }, this._debounceTime );
  16. } );
  17. }
  18. setValue( /*val*/ ) {
  19. this.dispatchChange();
  20. return this;
  21. }
  22. getValue() {
  23. return null;
  24. }
  25. dispatchChange() {
  26. this.dispatchEvent( { type: 'change', value: this.getValue() } );
  27. }
  28. onChange( callback ) {
  29. this._onChangeFunction = callback;
  30. return this;
  31. }
  32. debounce( time ) {
  33. this._debounceTime = time;
  34. return this;
  35. }
  36. show() {
  37. this.dispatchEvent( { type: 'show' } );
  38. return this;
  39. }
  40. hide() {
  41. this.dispatchEvent( { type: 'hide' } );
  42. return this;
  43. }
  44. }
  45. class ValueNumber extends Value {
  46. constructor( { value = 0, step = 0.1, min = - Infinity, max = Infinity } ) {
  47. super();
  48. this.input = document.createElement( 'input' );
  49. this.input.type = 'number';
  50. this.input.value = value;
  51. this.input.step = step;
  52. this.input.min = min;
  53. this.input.max = max;
  54. this.input.addEventListener( 'change', this._onChangeValue.bind( this ) );
  55. this.domElement.appendChild( this.input );
  56. this.addDragHandler();
  57. }
  58. _onChangeValue() {
  59. const value = parseFloat( this.input.value );
  60. const min = parseFloat( this.input.min );
  61. const max = parseFloat( this.input.max );
  62. if ( value > max ) {
  63. this.input.value = max;
  64. } else if ( value < min ) {
  65. this.input.value = min;
  66. } else if ( isNaN( value ) ) {
  67. this.input.value = min;
  68. }
  69. this.dispatchChange();
  70. }
  71. addDragHandler() {
  72. let isDragging = false;
  73. let startY, startValue;
  74. this.input.style.touchAction = 'none';
  75. this.input.addEventListener( 'pointerdown', ( e ) => {
  76. isDragging = true;
  77. startY = e.clientY;
  78. startValue = parseFloat( this.input.value );
  79. document.body.style.cursor = 'ns-resize';
  80. } );
  81. document.addEventListener( 'pointermove', ( e ) => {
  82. if ( isDragging ) {
  83. const deltaY = startY - e.clientY;
  84. const step = parseFloat( this.input.step ) || 1;
  85. const min = parseFloat( this.input.min );
  86. const max = parseFloat( this.input.max );
  87. let stepSize = step;
  88. if ( ! isNaN( max ) && isFinite( min ) ) {
  89. stepSize = ( max - min ) / 100;
  90. }
  91. const change = deltaY * stepSize;
  92. let newValue = startValue + change;
  93. newValue = Math.max( min, Math.min( newValue, max ) );
  94. const precision = ( String( step ).split( '.' )[ 1 ] || [] ).length;
  95. this.input.value = newValue.toFixed( precision );
  96. this.input.dispatchEvent( new Event( 'input' ) );
  97. this.dispatchChange();
  98. }
  99. } );
  100. document.addEventListener( 'pointerup', () => {
  101. if ( isDragging ) {
  102. isDragging = false;
  103. document.body.style.cursor = 'default';
  104. }
  105. } );
  106. }
  107. setValue( val ) {
  108. this.input.value = val;
  109. return super.setValue( val );
  110. }
  111. getValue() {
  112. return parseFloat( this.input.value );
  113. }
  114. }
  115. class ValueCheckbox extends Value {
  116. constructor( { value = false } ) {
  117. super();
  118. const label = document.createElement( 'label' );
  119. label.className = 'custom-checkbox';
  120. const checkbox = document.createElement( 'input' );
  121. checkbox.type = 'checkbox';
  122. checkbox.checked = value;
  123. this.checkbox = checkbox;
  124. const checkmark = document.createElement( 'span' );
  125. checkmark.className = 'checkmark';
  126. label.appendChild( checkbox );
  127. label.appendChild( checkmark );
  128. this.domElement.appendChild( label );
  129. checkbox.addEventListener( 'change', () => {
  130. this.dispatchChange();
  131. } );
  132. }
  133. setValue( val ) {
  134. this.checkbox.checked = val;
  135. return super.setValue( val );
  136. }
  137. getValue() {
  138. return this.checkbox.checked;
  139. }
  140. }
  141. class ValueSlider extends Value {
  142. constructor( { value = 0, min = 0, max = 1, step = 0.01 } ) {
  143. super();
  144. this.slider = document.createElement( 'input' );
  145. this.slider.type = 'range';
  146. this.slider.min = min;
  147. this.slider.max = max;
  148. this.slider.step = step;
  149. const numberValue = new ValueNumber( { value, min, max, step } );
  150. this.numberInput = numberValue.input;
  151. this.numberInput.style.flexBasis = '80px';
  152. this.numberInput.style.flexShrink = '0';
  153. this.slider.value = value;
  154. this.domElement.append( this.slider, this.numberInput );
  155. this.slider.addEventListener( 'input', () => {
  156. this.numberInput.value = this.slider.value;
  157. this.dispatchChange();
  158. } );
  159. numberValue.addEventListener( 'change', () => {
  160. this.slider.value = parseFloat( this.numberInput.value );
  161. this.dispatchChange();
  162. } );
  163. }
  164. setValue( val ) {
  165. this.slider.value = val;
  166. this.numberInput.value = val;
  167. return super.setValue( val );
  168. }
  169. getValue() {
  170. return parseFloat( this.slider.value );
  171. }
  172. step( value ) {
  173. this.slider.step = value;
  174. this.numberInput.step = value;
  175. this.slider.value = parseFloat( this.numberInput.value );
  176. return this;
  177. }
  178. }
  179. class ValueSelect extends Value {
  180. constructor( { options = [], value = '' } ) {
  181. super();
  182. const select = document.createElement( 'select' );
  183. const createOption = ( name, optionValue ) => {
  184. const optionEl = document.createElement( 'option' );
  185. optionEl.value = name;
  186. optionEl.textContent = name;
  187. if ( optionValue == value ) optionEl.selected = true;
  188. select.appendChild( optionEl );
  189. return optionEl;
  190. };
  191. if ( Array.isArray( options ) ) {
  192. options.forEach( opt => createOption( opt, opt ) );
  193. } else {
  194. Object.entries( options ).forEach( ( [ key, value ] ) => createOption( key, value ) );
  195. }
  196. this.domElement.appendChild( select );
  197. //
  198. select.addEventListener( 'change', () => {
  199. this.dispatchChange();
  200. } );
  201. this.options = options;
  202. this.select = select;
  203. }
  204. setValue( val ) {
  205. if ( Array.isArray( this.options ) ) {
  206. this.select.value = val;
  207. } else {
  208. const entry = Object.entries( this.options ).find( ( [ , v ] ) => v === val );
  209. if ( entry ) {
  210. this.select.value = entry[ 0 ];
  211. } else {
  212. this.select.value = val;
  213. }
  214. }
  215. return super.setValue( val );
  216. }
  217. getValue() {
  218. const options = this.options;
  219. if ( Array.isArray( options ) ) {
  220. return options[ this.select.selectedIndex ];
  221. } else {
  222. return options[ this.select.value ];
  223. }
  224. }
  225. }
  226. class ValueColor extends Value {
  227. constructor( { value = '#ffffff' } ) {
  228. super();
  229. const colorInput = document.createElement( 'input' );
  230. colorInput.type = 'color';
  231. colorInput.value = this._getColorHex( value );
  232. this.colorInput = colorInput;
  233. this._value = value;
  234. colorInput.addEventListener( 'input', () => {
  235. const colorValue = colorInput.value;
  236. if ( this._value.isColor ) {
  237. this._value.setHex( parseInt( colorValue.slice( 1 ), 16 ) );
  238. } else {
  239. this._value = colorValue;
  240. }
  241. this.dispatchChange();
  242. } );
  243. this.domElement.appendChild( colorInput );
  244. }
  245. setValue( val ) {
  246. const colorHex = this._getColorHex( val );
  247. this.colorInput.value = colorHex;
  248. if ( this._value && this._value.isColor ) {
  249. this._value.setHex( parseInt( colorHex.slice( 1 ), 16 ) );
  250. } else {
  251. this._value = val;
  252. }
  253. return super.setValue( val );
  254. }
  255. _getColorHex( color ) {
  256. if ( color && color.isColor ) {
  257. color = color.getHex();
  258. }
  259. if ( typeof color === 'number' ) {
  260. color = '#' + color.toString( 16 ).padStart( 6, '0' );
  261. } else if ( typeof color === 'string' && color[ 0 ] !== '#' ) {
  262. color = '#' + color;
  263. }
  264. return color;
  265. }
  266. getValue() {
  267. let value = this._value;
  268. if ( typeof value === 'string' ) {
  269. value = parseInt( value.slice( 1 ), 16 );
  270. }
  271. return value;
  272. }
  273. }
  274. class ValueButton extends Value {
  275. constructor( { text = 'Button', value = () => {} } ) {
  276. super();
  277. const button = document.createElement( 'button' );
  278. button.textContent = text;
  279. button.onclick = value;
  280. this.domElement.appendChild( button );
  281. }
  282. }
  283. class ValueString extends Value {
  284. constructor( { value = '' } ) {
  285. super();
  286. const input = document.createElement( 'input' );
  287. input.type = 'text';
  288. input.value = value;
  289. this.input = input;
  290. input.addEventListener( 'input', () => {
  291. this.dispatchChange();
  292. } );
  293. this.domElement.appendChild( input );
  294. }
  295. setValue( val ) {
  296. this.input.value = val;
  297. return super.setValue( val );
  298. }
  299. getValue() {
  300. return this.input.value;
  301. }
  302. }
  303. export { Value, ValueNumber, ValueString, ValueCheckbox, ValueSlider, ValueSelect, ValueColor, ValueButton };
粤ICP备19079148号