Parameters.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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.paramList = new Item( name );
  10. this.paramList.setCollapsible( true );
  11. this.objects = [];
  12. }
  13. close() {
  14. this.paramList.close();
  15. return this;
  16. }
  17. name( name ) {
  18. this.paramList.setValue( 0, name );
  19. return this;
  20. }
  21. show() {
  22. this.paramList.show();
  23. return this;
  24. }
  25. hide() {
  26. this.paramList.hide();
  27. return this;
  28. }
  29. add( object, property, ...params ) {
  30. const value = object[ property ];
  31. const type = typeof value;
  32. let item = null;
  33. if ( typeof params[ 0 ] === 'object' ) {
  34. item = this.addSelect( object, property, params[ 0 ] );
  35. } else if ( type === 'number' ) {
  36. if ( params.length >= 2 ) {
  37. item = this.addSlider( object, property, ...params );
  38. } else {
  39. item = this.addNumber( object, property, ...params );
  40. }
  41. } else if ( type === 'boolean' ) {
  42. item = this.addBoolean( object, property );
  43. } else if ( type === 'string' ) {
  44. item = this.addString( object, property );
  45. } else if ( type === 'function' ) {
  46. item = this.addButton( object, property, ...params );
  47. }
  48. return item;
  49. }
  50. _addInfo( editor, itemNode ) {
  51. editor.info = ( text ) => {
  52. info( itemNode, text );
  53. return editor;
  54. };
  55. }
  56. _addParameter( object, property, editor, subItem ) {
  57. editor.name = ( name ) => {
  58. if ( subItem.data[ 0 ].childNodes.length > 0 && subItem.data[ 0 ].firstChild.nodeType === 3 /* Node.TEXT_NODE */ ) {
  59. subItem.data[ 0 ].firstChild.textContent = name;
  60. } else {
  61. subItem.data[ 0 ].insertBefore( document.createTextNode( name ), subItem.data[ 0 ].firstChild );
  62. }
  63. return editor;
  64. };
  65. this._addInfo( editor, subItem.data[ 0 ] );
  66. editor.listen = () => {
  67. const update = () => {
  68. const value = editor.getValue();
  69. const propertyValue = object[ property ];
  70. if ( value !== propertyValue ) {
  71. editor.setValue( propertyValue );
  72. }
  73. requestAnimationFrame( update );
  74. };
  75. requestAnimationFrame( update );
  76. return editor;
  77. };
  78. this._registerParameter( object, property, editor, subItem );
  79. }
  80. _registerParameter( object, property, editor, subItem ) {
  81. this.objects.push( { object: object, key: property, editor: editor, subItem: subItem } );
  82. editor.addEventListener( 'show', () => subItem.show() );
  83. editor.addEventListener( 'hide', () => subItem.hide() );
  84. }
  85. addString( object, property ) {
  86. const value = object[ property ];
  87. const editor = new ValueString( { 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. this._addParameter( object, property, editor, subItem );
  99. return editor;
  100. }
  101. addFolder( name ) {
  102. const group = new ParametersGroup( this.parameters, name );
  103. this.paramList.add( group.paramList );
  104. return group;
  105. }
  106. addBoolean( object, property ) {
  107. const value = object[ property ];
  108. const editor = new ValueCheckbox( { 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. // extends logic to toggle checkbox when clicking on the row
  117. const itemRow = subItem.domElement.firstChild;
  118. itemRow.classList.add( 'actionable' );
  119. itemRow.addEventListener( 'click', ( e ) => {
  120. if ( e.target.closest( 'label' ) ) return;
  121. const checkbox = itemRow.querySelector( 'input[type="checkbox"]' );
  122. if ( checkbox ) {
  123. checkbox.checked = ! checkbox.checked;
  124. checkbox.dispatchEvent( new Event( 'change' ) );
  125. }
  126. } );
  127. // extend object property
  128. this._addParameter( object, property, editor, subItem );
  129. return editor;
  130. }
  131. addSelect( object, property, options ) {
  132. const value = object[ property ];
  133. const editor = new ValueSelect( { options, value } );
  134. editor.addEventListener( 'change', ( { value } ) => {
  135. object[ property ] = value;
  136. } );
  137. const description = createValueSpan();
  138. description.textContent = property;
  139. const subItem = new Item( description, editor.domElement );
  140. this.paramList.add( subItem );
  141. const itemRow = subItem.domElement.firstChild;
  142. itemRow.classList.add( 'actionable' );
  143. // extend object property
  144. this._addParameter( object, property, editor, subItem );
  145. return editor;
  146. }
  147. addColor( object, property ) {
  148. const value = object[ property ];
  149. const editor = new ValueColor( { value } );
  150. editor.addEventListener( 'change', ( { value } ) => {
  151. object[ property ] = value;
  152. } );
  153. const description = createValueSpan();
  154. description.textContent = property;
  155. const subItem = new Item( description, editor.domElement );
  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. addSlider( object, property, min = 0, max = 1, step = 0.01 ) {
  164. const value = object[ property ];
  165. const editor = new ValueSlider( { value, min, max, step } );
  166. editor.addEventListener( 'change', ( { value } ) => {
  167. object[ property ] = value;
  168. } );
  169. const description = createValueSpan();
  170. description.textContent = property;
  171. const subItem = new Item( description, editor.domElement );
  172. this.paramList.add( subItem );
  173. const itemRow = subItem.domElement.firstChild;
  174. itemRow.classList.add( 'actionable' );
  175. // extend object property
  176. this._addParameter( object, property, editor, subItem );
  177. return editor;
  178. }
  179. addNumber( object, property, ...params ) {
  180. const value = object[ property ];
  181. const [ min, max ] = params;
  182. const editor = new ValueNumber( { value, min, max } );
  183. editor.addEventListener( 'change', ( { value } ) => {
  184. object[ property ] = value;
  185. } );
  186. const description = createValueSpan();
  187. description.textContent = property;
  188. const subItem = new Item( description, editor.domElement );
  189. this.paramList.add( subItem );
  190. const itemRow = subItem.domElement.firstChild;
  191. itemRow.classList.add( 'actionable' );
  192. // extend object property
  193. this._addParameter( object, property, editor, subItem );
  194. return editor;
  195. }
  196. addButton( object, property ) {
  197. const value = object[ property ];
  198. const editor = new ValueButton( { text: property, value } );
  199. editor.addEventListener( 'change', ( { value } ) => {
  200. object[ property ] = value;
  201. } );
  202. const subItem = new Item( editor.domElement );
  203. subItem.itemRow.childNodes[ 0 ].style.gridColumn = '1 / -1';
  204. this.paramList.add( subItem );
  205. const itemRow = subItem.domElement.firstChild;
  206. itemRow.classList.add( 'actionable' );
  207. // extend object property
  208. editor.name = ( name ) => {
  209. const buttonNode = editor.domElement.childNodes[ 0 ];
  210. if ( buttonNode.childNodes.length > 0 && buttonNode.firstChild.nodeType === 3 /* Node.TEXT_NODE */ ) {
  211. buttonNode.firstChild.textContent = name;
  212. } else {
  213. buttonNode.insertBefore( document.createTextNode( name ), buttonNode.firstChild );
  214. }
  215. return editor;
  216. };
  217. this._addInfo( editor, editor.domElement.childNodes[ 0 ] );
  218. this._registerParameter( object, property, editor, subItem );
  219. return editor;
  220. }
  221. }
  222. class Parameters extends Tab {
  223. constructor( options = {} ) {
  224. super( options.name || 'Parameters', options );
  225. const paramList = new List( 'Property', 'Value' );
  226. paramList.domElement.classList.add( 'parameters' );
  227. paramList.setGridStyle( '.5fr 1fr' );
  228. paramList.domElement.style.minWidth = '300px';
  229. const scrollWrapper = document.createElement( 'div' );
  230. scrollWrapper.className = 'list-scroll-wrapper';
  231. scrollWrapper.appendChild( paramList.domElement );
  232. this.content.appendChild( scrollWrapper );
  233. this.paramList = paramList;
  234. this.groups = [];
  235. }
  236. createGroup( name ) {
  237. const group = new ParametersGroup( this, name );
  238. this.paramList.add( group.paramList );
  239. this.groups.push( group );
  240. return group;
  241. }
  242. }
  243. export { Parameters };
粤ICP备19079148号