Parameters.js 6.8 KB

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