Parameters.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. import { Tab } from '../ui/Tab.js';
  2. import { List } from '../ui/List.js';
  3. import { Item } from '../ui/Item.js';
  4. import { createValueSpan } from '../ui/utils.js';
  5. import { ValueNumber, ValueSlider, ValueSelect, ValueCheckbox, ValueColor } from '../ui/Values.js';
  6. class ParametersGroup {
  7. constructor( parameters, name ) {
  8. this.parameters = parameters;
  9. this.name = name;
  10. this.paramList = new Item( name );
  11. }
  12. close() {
  13. this.paramList.close();
  14. return this;
  15. }
  16. add( object, property, ...params ) {
  17. const value = object[ property ];
  18. const type = typeof value;
  19. let item = null;
  20. if ( typeof params[ 0 ] === 'object' ) {
  21. item = this.addSelect( object, property, params[ 0 ] );
  22. } else if ( type === 'number' ) {
  23. if ( params.length >= 2 ) {
  24. item = this.addSlider( object, property, ...params );
  25. } else {
  26. item = this.addNumber( object, property, ...params );
  27. }
  28. } else if ( type === 'boolean' ) {
  29. item = this.addBoolean( object, property );
  30. }
  31. return item;
  32. }
  33. addFolder( name ) {
  34. const group = new ParametersGroup( this.parameters, name );
  35. this.paramList.add( group.paramList );
  36. return group;
  37. }
  38. addBoolean( object, property ) {
  39. const value = object[ property ];
  40. const editor = new ValueCheckbox( { value } );
  41. editor.addEventListener( 'change', ( { value } ) => {
  42. object[ property ] = value;
  43. } );
  44. const description = createValueSpan();
  45. description.textContent = property;
  46. const subItem = new Item( description, editor.domElement );
  47. this.paramList.add( subItem );
  48. // extends logic to toggle checkbox when clicking on the row
  49. const itemRow = subItem.domElement.firstChild;
  50. itemRow.classList.add( 'actionable' );
  51. itemRow.addEventListener( 'click', ( e ) => {
  52. if ( e.target.closest( 'label' ) ) return;
  53. const checkbox = itemRow.querySelector( 'input[type="checkbox"]' );
  54. if ( checkbox ) {
  55. checkbox.checked = ! checkbox.checked;
  56. checkbox.dispatchEvent( new Event( 'change' ) );
  57. }
  58. } );
  59. // extend object property
  60. editor.name = ( name ) => {
  61. description.textContent = name;
  62. return editor;
  63. };
  64. return editor;
  65. }
  66. addSelect( object, property, options ) {
  67. const value = object[ property ];
  68. const editor = new ValueSelect( { options, value } );
  69. editor.addEventListener( 'change', ( { value } ) => {
  70. object[ property ] = value;
  71. } );
  72. const description = createValueSpan();
  73. description.textContent = property;
  74. const subItem = new Item( description, editor.domElement );
  75. this.paramList.add( subItem );
  76. const itemRow = subItem.domElement.firstChild;
  77. itemRow.classList.add( 'actionable' );
  78. // extend object property
  79. editor.name = ( name ) => {
  80. description.textContent = name;
  81. return editor;
  82. };
  83. return editor;
  84. }
  85. addColor( object, property ) {
  86. const value = object[ property ];
  87. const editor = new ValueColor( { value } );
  88. editor.addEventListener( 'change', ( { value } ) => {
  89. object[ property ] = value;
  90. } );
  91. const description = createValueSpan();
  92. description.textContent = property;
  93. const subItem = new Item( description, editor.domElement );
  94. this.paramList.add( subItem );
  95. const itemRow = subItem.domElement.firstChild;
  96. itemRow.classList.add( 'actionable' );
  97. // extend object property
  98. editor.name = ( name ) => {
  99. description.textContent = name;
  100. return editor;
  101. };
  102. return editor;
  103. }
  104. addSlider( object, property, min = 0, max = 1, step = 0.01 ) {
  105. const value = object[ property ];
  106. const editor = new ValueSlider( { value, min, max, step } );
  107. editor.addEventListener( 'change', ( { value } ) => {
  108. object[ property ] = value;
  109. } );
  110. const description = createValueSpan();
  111. description.textContent = property;
  112. const subItem = new Item( description, editor.domElement );
  113. this.paramList.add( subItem );
  114. const itemRow = subItem.domElement.firstChild;
  115. itemRow.classList.add( 'actionable' );
  116. // extend object property
  117. editor.name = ( name ) => {
  118. description.textContent = name;
  119. return editor;
  120. };
  121. return editor;
  122. }
  123. addNumber( object, property, ...params ) {
  124. const value = object[ property ];
  125. const [ min, max ] = params;
  126. const editor = new ValueNumber( { value, min, max } );
  127. editor.addEventListener( 'change', ( { value } ) => {
  128. object[ property ] = value;
  129. } );
  130. const description = createValueSpan();
  131. description.textContent = property;
  132. const subItem = new Item( description, editor.domElement );
  133. this.paramList.add( subItem );
  134. const itemRow = subItem.domElement.firstChild;
  135. itemRow.classList.add( 'actionable' );
  136. // extend object property
  137. editor.name = ( name ) => {
  138. description.textContent = name;
  139. return editor;
  140. };
  141. return editor;
  142. }
  143. }
  144. class Parameters extends Tab {
  145. constructor() {
  146. super( 'Parameters' );
  147. const paramList = new List( 'Property', 'Value' );
  148. paramList.domElement.classList.add( 'parameters' );
  149. paramList.setGridStyle( '.5fr 1fr' );
  150. paramList.domElement.style.minWidth = '300px';
  151. const scrollWrapper = document.createElement( 'div' );
  152. scrollWrapper.className = 'list-scroll-wrapper';
  153. scrollWrapper.appendChild( paramList.domElement );
  154. this.content.appendChild( scrollWrapper );
  155. this.paramList = paramList;
  156. }
  157. createGroup( name ) {
  158. const group = new ParametersGroup( this, name );
  159. this.paramList.add( group.paramList );
  160. return group;
  161. }
  162. }
  163. export { Parameters };
粤ICP备19079148号