Parameters.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. _addInfo( editor, itemNode ) {
  39. editor.info = ( text ) => {
  40. let infoIcon = itemNode.querySelector( '.info-icon' );
  41. if ( ! infoIcon ) {
  42. infoIcon = document.createElement( 'span' );
  43. infoIcon.className = 'info-icon';
  44. infoIcon.textContent = 'i';
  45. itemNode.appendChild( infoIcon );
  46. infoIcon.addEventListener( 'mouseenter', () => {
  47. const container = infoIcon.closest( '.three-inspector' ) || document.body;
  48. let tooltip = container.querySelector( '.three-inspector-info-tooltip' );
  49. if ( ! tooltip ) {
  50. tooltip = document.createElement( 'div' );
  51. tooltip.className = 'info-tooltip three-inspector-info-tooltip';
  52. container.appendChild( tooltip );
  53. }
  54. const html = text.trim().replace( /### (.*?)(?:\r?\n|$)/g, '<h3>$1</h3>' )
  55. .replace( /\*\*(.*?)\*\*/g, '<strong>$1</strong>' )
  56. .replace( /\n/g, '<br/>' );
  57. tooltip.innerHTML = html;
  58. const rect = infoIcon.getBoundingClientRect();
  59. tooltip.style.left = ( rect.left + rect.width / 2 ) + 'px';
  60. tooltip.style.top = ( rect.top - 8 ) + 'px';
  61. tooltip.style.opacity = '1';
  62. tooltip.style.visibility = 'visible';
  63. } );
  64. infoIcon.addEventListener( 'mouseleave', () => {
  65. const container = infoIcon.closest( '.three-inspector' ) || document.body;
  66. const tooltip = container.querySelector( '.three-inspector-info-tooltip' );
  67. if ( tooltip ) {
  68. tooltip.style.opacity = '0';
  69. tooltip.style.visibility = 'hidden';
  70. }
  71. } );
  72. }
  73. return editor;
  74. };
  75. }
  76. _addParameter( object, property, editor, subItem ) {
  77. editor.name = ( name ) => {
  78. if ( subItem.data[ 0 ].childNodes.length > 0 && subItem.data[ 0 ].firstChild.nodeType === 3 /* Node.TEXT_NODE */ ) {
  79. subItem.data[ 0 ].firstChild.textContent = name;
  80. } else {
  81. subItem.data[ 0 ].insertBefore( document.createTextNode( name ), subItem.data[ 0 ].firstChild );
  82. }
  83. return editor;
  84. };
  85. this._addInfo( editor, subItem.data[ 0 ] );
  86. editor.listen = () => {
  87. const update = () => {
  88. const value = editor.getValue();
  89. const propertyValue = object[ property ];
  90. if ( value !== propertyValue ) {
  91. editor.setValue( propertyValue );
  92. }
  93. requestAnimationFrame( update );
  94. };
  95. requestAnimationFrame( update );
  96. return editor;
  97. };
  98. this._registerParameter( object, property, editor, subItem );
  99. }
  100. _registerParameter( object, property, editor, subItem ) {
  101. this.objects.push( { object: object, key: property, editor: editor, subItem: subItem } );
  102. }
  103. addString( object, property ) {
  104. const value = object[ property ];
  105. const editor = new ValueString( { value } );
  106. editor.addEventListener( 'change', ( { value } ) => {
  107. object[ property ] = value;
  108. } );
  109. const description = createValueSpan();
  110. description.textContent = property;
  111. const subItem = new Item( description, editor.domElement );
  112. this.paramList.add( subItem );
  113. const itemRow = subItem.domElement.firstChild;
  114. itemRow.classList.add( 'actionable' );
  115. // extend object property
  116. this._addParameter( object, property, editor, subItem );
  117. return editor;
  118. }
  119. addFolder( name ) {
  120. const group = new ParametersGroup( this.parameters, name );
  121. this.paramList.add( group.paramList );
  122. return group;
  123. }
  124. addBoolean( object, property ) {
  125. const value = object[ property ];
  126. const editor = new ValueCheckbox( { value } );
  127. editor.addEventListener( 'change', ( { value } ) => {
  128. object[ property ] = value;
  129. } );
  130. const description = createValueSpan();
  131. description.textContent = property;
  132. const subItem = new Item( description, editor.domElement );
  133. this.paramList.add( subItem );
  134. // extends logic to toggle checkbox when clicking on the row
  135. const itemRow = subItem.domElement.firstChild;
  136. itemRow.classList.add( 'actionable' );
  137. itemRow.addEventListener( 'click', ( e ) => {
  138. if ( e.target.closest( 'label' ) ) return;
  139. const checkbox = itemRow.querySelector( 'input[type="checkbox"]' );
  140. if ( checkbox ) {
  141. checkbox.checked = ! checkbox.checked;
  142. checkbox.dispatchEvent( new Event( 'change' ) );
  143. }
  144. } );
  145. // extend object property
  146. this._addParameter( object, property, editor, subItem );
  147. return editor;
  148. }
  149. addSelect( object, property, options ) {
  150. const value = object[ property ];
  151. const editor = new ValueSelect( { options, value } );
  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. addColor( object, property ) {
  166. const value = object[ property ];
  167. const editor = new ValueColor( { value } );
  168. editor.addEventListener( 'change', ( { value } ) => {
  169. object[ property ] = value;
  170. } );
  171. const description = createValueSpan();
  172. description.textContent = property;
  173. const subItem = new Item( description, editor.domElement );
  174. this.paramList.add( subItem );
  175. const itemRow = subItem.domElement.firstChild;
  176. itemRow.classList.add( 'actionable' );
  177. // extend object property
  178. this._addParameter( object, property, editor, subItem );
  179. return editor;
  180. }
  181. addSlider( object, property, min = 0, max = 1, step = 0.01 ) {
  182. const value = object[ property ];
  183. const editor = new ValueSlider( { value, min, max, step } );
  184. editor.addEventListener( 'change', ( { value } ) => {
  185. object[ property ] = value;
  186. } );
  187. const description = createValueSpan();
  188. description.textContent = property;
  189. const subItem = new Item( description, editor.domElement );
  190. this.paramList.add( subItem );
  191. const itemRow = subItem.domElement.firstChild;
  192. itemRow.classList.add( 'actionable' );
  193. // extend object property
  194. this._addParameter( object, property, editor, subItem );
  195. return editor;
  196. }
  197. addNumber( object, property, ...params ) {
  198. const value = object[ property ];
  199. const [ min, max ] = params;
  200. const editor = new ValueNumber( { value, min, max } );
  201. editor.addEventListener( 'change', ( { value } ) => {
  202. object[ property ] = value;
  203. } );
  204. const description = createValueSpan();
  205. description.textContent = property;
  206. const subItem = new Item( description, editor.domElement );
  207. this.paramList.add( subItem );
  208. const itemRow = subItem.domElement.firstChild;
  209. itemRow.classList.add( 'actionable' );
  210. // extend object property
  211. this._addParameter( object, property, editor, subItem );
  212. return editor;
  213. }
  214. addButton( object, property ) {
  215. const value = object[ property ];
  216. const editor = new ValueButton( { text: property, value } );
  217. editor.addEventListener( 'change', ( { value } ) => {
  218. object[ property ] = value;
  219. } );
  220. const subItem = new Item( editor.domElement );
  221. subItem.itemRow.childNodes[ 0 ].style.gridColumn = '1 / -1';
  222. this.paramList.add( subItem );
  223. const itemRow = subItem.domElement.firstChild;
  224. itemRow.classList.add( 'actionable' );
  225. // extend object property
  226. editor.name = ( name ) => {
  227. const buttonNode = editor.domElement.childNodes[ 0 ];
  228. if ( buttonNode.childNodes.length > 0 && buttonNode.firstChild.nodeType === 3 /* Node.TEXT_NODE */ ) {
  229. buttonNode.firstChild.textContent = name;
  230. } else {
  231. buttonNode.insertBefore( document.createTextNode( name ), buttonNode.firstChild );
  232. }
  233. return editor;
  234. };
  235. this._addInfo( editor, editor.domElement.childNodes[ 0 ] );
  236. this._registerParameter( object, property, editor, subItem );
  237. return editor;
  238. }
  239. }
  240. class Parameters extends Tab {
  241. constructor( options = {} ) {
  242. super( options.name || 'Parameters', options );
  243. const paramList = new List( 'Property', 'Value' );
  244. paramList.domElement.classList.add( 'parameters' );
  245. paramList.setGridStyle( '.5fr 1fr' );
  246. paramList.domElement.style.minWidth = '300px';
  247. const scrollWrapper = document.createElement( 'div' );
  248. scrollWrapper.className = 'list-scroll-wrapper';
  249. scrollWrapper.appendChild( paramList.domElement );
  250. this.content.appendChild( scrollWrapper );
  251. this.paramList = paramList;
  252. this.groups = [];
  253. }
  254. createGroup( name ) {
  255. const group = new ParametersGroup( this, name );
  256. this.paramList.add( group.paramList );
  257. this.groups.push( group );
  258. return group;
  259. }
  260. }
  261. export { Parameters };
粤ICP备19079148号