Parameters.js 8.2 KB

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