Parameters.js 7.4 KB

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