Parameters.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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, ValueButton } 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. } else if ( type === 'function' ) {
  31. item = this.addButton( object, property, ...params );
  32. }
  33. return item;
  34. }
  35. _addParameter( object, property, editor, subItem ) {
  36. editor.name = ( name ) => {
  37. subItem.data[ 0 ].textContent = name;
  38. return editor;
  39. };
  40. editor.listen = () => {
  41. const update = () => {
  42. const value = editor.getValue();
  43. const propertyValue = object[ property ];
  44. if ( value !== propertyValue ) {
  45. editor.setValue( propertyValue );
  46. }
  47. requestAnimationFrame( update );
  48. };
  49. requestAnimationFrame( update );
  50. return editor;
  51. };
  52. }
  53. addFolder( name ) {
  54. const group = new ParametersGroup( this.parameters, name );
  55. this.paramList.add( group.paramList );
  56. return group;
  57. }
  58. addBoolean( object, property ) {
  59. const value = object[ property ];
  60. const editor = new ValueCheckbox( { value } );
  61. editor.addEventListener( 'change', ( { value } ) => {
  62. object[ property ] = value;
  63. } );
  64. const description = createValueSpan();
  65. description.textContent = property;
  66. const subItem = new Item( description, editor.domElement );
  67. this.paramList.add( subItem );
  68. // extends logic to toggle checkbox when clicking on the row
  69. const itemRow = subItem.domElement.firstChild;
  70. itemRow.classList.add( 'actionable' );
  71. itemRow.addEventListener( 'click', ( e ) => {
  72. if ( e.target.closest( 'label' ) ) return;
  73. const checkbox = itemRow.querySelector( 'input[type="checkbox"]' );
  74. if ( checkbox ) {
  75. checkbox.checked = ! checkbox.checked;
  76. checkbox.dispatchEvent( new Event( 'change' ) );
  77. }
  78. } );
  79. // extend object property
  80. this._addParameter( object, property, editor, subItem );
  81. return editor;
  82. }
  83. addSelect( object, property, options ) {
  84. const value = object[ property ];
  85. const editor = new ValueSelect( { options, value } );
  86. editor.addEventListener( 'change', ( { value } ) => {
  87. object[ property ] = value;
  88. } );
  89. const description = createValueSpan();
  90. description.textContent = property;
  91. const subItem = new Item( description, editor.domElement );
  92. this.paramList.add( subItem );
  93. const itemRow = subItem.domElement.firstChild;
  94. itemRow.classList.add( 'actionable' );
  95. // extend object property
  96. this._addParameter( object, property, editor, subItem );
  97. return editor;
  98. }
  99. addColor( object, property ) {
  100. const value = object[ property ];
  101. const editor = new ValueColor( { value } );
  102. editor.addEventListener( 'change', ( { value } ) => {
  103. object[ property ] = value;
  104. } );
  105. const description = createValueSpan();
  106. description.textContent = property;
  107. const subItem = new Item( description, editor.domElement );
  108. this.paramList.add( subItem );
  109. const itemRow = subItem.domElement.firstChild;
  110. itemRow.classList.add( 'actionable' );
  111. // extend object property
  112. this._addParameter( object, property, editor, subItem );
  113. return editor;
  114. }
  115. addSlider( object, property, min = 0, max = 1, step = 0.01 ) {
  116. const value = object[ property ];
  117. const editor = new ValueSlider( { value, min, max, step } );
  118. editor.addEventListener( 'change', ( { value } ) => {
  119. object[ property ] = value;
  120. } );
  121. const description = createValueSpan();
  122. description.textContent = property;
  123. const subItem = new Item( description, editor.domElement );
  124. this.paramList.add( subItem );
  125. const itemRow = subItem.domElement.firstChild;
  126. itemRow.classList.add( 'actionable' );
  127. // extend object property
  128. this._addParameter( object, property, editor, subItem );
  129. return editor;
  130. }
  131. addNumber( object, property, ...params ) {
  132. const value = object[ property ];
  133. const [ min, max ] = params;
  134. const editor = new ValueNumber( { value, min, max } );
  135. editor.addEventListener( 'change', ( { value } ) => {
  136. object[ property ] = value;
  137. } );
  138. const description = createValueSpan();
  139. description.textContent = property;
  140. const subItem = new Item( description, editor.domElement );
  141. this.paramList.add( subItem );
  142. const itemRow = subItem.domElement.firstChild;
  143. itemRow.classList.add( 'actionable' );
  144. // extend object property
  145. this._addParameter( object, property, editor, subItem );
  146. return editor;
  147. }
  148. addButton( object, property ) {
  149. const value = object[ property ];
  150. const editor = new ValueButton( { text: property, value } );
  151. editor.addEventListener( 'change', ( { value } ) => {
  152. object[ property ] = value;
  153. } );
  154. const subItem = new Item( editor.domElement );
  155. subItem.itemRow.childNodes[ 0 ].style.gridColumn = '1 / -1';
  156. this.paramList.add( subItem );
  157. const itemRow = subItem.domElement.firstChild;
  158. itemRow.classList.add( 'actionable' );
  159. // extend object property
  160. this._addParameter( object, property, editor, subItem );
  161. return editor;
  162. }
  163. }
  164. class Parameters extends Tab {
  165. constructor() {
  166. super( 'Parameters' );
  167. const paramList = new List( 'Property', 'Value' );
  168. paramList.domElement.classList.add( 'parameters' );
  169. paramList.setGridStyle( '.5fr 1fr' );
  170. paramList.domElement.style.minWidth = '300px';
  171. const scrollWrapper = document.createElement( 'div' );
  172. scrollWrapper.className = 'list-scroll-wrapper';
  173. scrollWrapper.appendChild( paramList.domElement );
  174. this.content.appendChild( scrollWrapper );
  175. this.paramList = paramList;
  176. }
  177. createGroup( name ) {
  178. const group = new ParametersGroup( this, name );
  179. this.paramList.add( group.paramList );
  180. return group;
  181. }
  182. }
  183. export { Parameters };
粤ICP备19079148号