ui.three.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. import * as THREE from '../../../build/three.module.js';
  5. import { TGALoader } from '../../../examples/jsm/loaders/TGALoader.js';
  6. import { UIElement, UISpan, UIDiv, UIRow, UIButton, UICheckbox, UIText, UINumber } from './ui.js';
  7. import { MoveObjectCommand } from '../commands/MoveObjectCommand.js';
  8. /**
  9. * @author mrdoob / http://mrdoob.com/
  10. */
  11. var UITexture = function ( mapping ) {
  12. UIElement.call( this );
  13. var scope = this;
  14. var dom = document.createElement( 'span' );
  15. var form = document.createElement( 'form' );
  16. var input = document.createElement( 'input' );
  17. input.type = 'file';
  18. input.addEventListener( 'change', function ( event ) {
  19. loadFile( event.target.files[ 0 ] );
  20. } );
  21. form.appendChild( input );
  22. var canvas = document.createElement( 'canvas' );
  23. canvas.width = 32;
  24. canvas.height = 16;
  25. canvas.style.cursor = 'pointer';
  26. canvas.style.marginRight = '5px';
  27. canvas.style.border = '1px solid #888';
  28. canvas.addEventListener( 'click', function () {
  29. input.click();
  30. }, false );
  31. canvas.addEventListener( 'drop', function () {
  32. event.preventDefault();
  33. event.stopPropagation();
  34. loadFile( event.dataTransfer.files[ 0 ] );
  35. }, false );
  36. dom.appendChild( canvas );
  37. function loadFile( file ) {
  38. if ( file.type.match( 'image.*' ) ) {
  39. var reader = new FileReader();
  40. if ( file.type === 'image/targa' ) {
  41. reader.addEventListener( 'load', function ( event ) {
  42. var canvas = new TGALoader().parse( event.target.result );
  43. var texture = new THREE.CanvasTexture( canvas, mapping );
  44. texture.sourceFile = file.name;
  45. scope.setValue( texture );
  46. if ( scope.onChangeCallback ) scope.onChangeCallback( texture );
  47. }, false );
  48. reader.readAsArrayBuffer( file );
  49. } else {
  50. reader.addEventListener( 'load', function ( event ) {
  51. var image = document.createElement( 'img' );
  52. image.addEventListener( 'load', function () {
  53. var texture = new THREE.Texture( this, mapping );
  54. texture.sourceFile = file.name;
  55. texture.format = file.type === 'image/jpeg' ? THREE.RGBFormat : THREE.RGBAFormat;
  56. texture.needsUpdate = true;
  57. scope.setValue( texture );
  58. if ( scope.onChangeCallback ) scope.onChangeCallback( texture );
  59. }, false );
  60. image.src = event.target.result;
  61. }, false );
  62. reader.readAsDataURL( file );
  63. }
  64. }
  65. form.reset();
  66. }
  67. this.dom = dom;
  68. this.texture = null;
  69. this.onChangeCallback = null;
  70. return this;
  71. };
  72. UITexture.prototype = Object.create( UIElement.prototype );
  73. UITexture.prototype.constructor = UITexture;
  74. UITexture.prototype.getValue = function () {
  75. return this.texture;
  76. };
  77. UITexture.prototype.setValue = function ( texture ) {
  78. var canvas = this.dom.children[ 0 ];
  79. var context = canvas.getContext( '2d' );
  80. if ( texture !== null ) {
  81. var image = texture.image;
  82. if ( image !== undefined && image.width > 0 ) {
  83. canvas.title = texture.sourceFile;
  84. var scale = canvas.width / image.width;
  85. context.drawImage( image, 0, 0, image.width * scale, image.height * scale );
  86. } else {
  87. canvas.title = texture.sourceFile + ' (error)';
  88. context.clearRect( 0, 0, canvas.width, canvas.height );
  89. }
  90. } else {
  91. canvas.title = 'empty';
  92. if ( context !== null ) {
  93. // Seems like context can be null if the canvas is not visible
  94. context.clearRect( 0, 0, canvas.width, canvas.height );
  95. }
  96. }
  97. this.texture = texture;
  98. };
  99. UITexture.prototype.setEncoding = function ( encoding ) {
  100. var texture = this.getValue();
  101. if ( texture !== null ) {
  102. texture.encoding = encoding;
  103. }
  104. return this;
  105. };
  106. UITexture.prototype.onChange = function ( callback ) {
  107. this.onChangeCallback = callback;
  108. return this;
  109. };
  110. // UIOutliner
  111. var UIOutliner = function ( editor ) {
  112. UIElement.call( this );
  113. var scope = this;
  114. var dom = document.createElement( 'div' );
  115. dom.className = 'Outliner';
  116. dom.tabIndex = 0; // keyup event is ignored without setting tabIndex
  117. // hack
  118. this.scene = editor.scene;
  119. // Prevent native scroll behavior
  120. dom.addEventListener( 'keydown', function ( event ) {
  121. switch ( event.keyCode ) {
  122. case 38: // up
  123. case 40: // down
  124. event.preventDefault();
  125. event.stopPropagation();
  126. break;
  127. }
  128. }, false );
  129. // Keybindings to support arrow navigation
  130. dom.addEventListener( 'keyup', function ( event ) {
  131. switch ( event.keyCode ) {
  132. case 38: // up
  133. scope.selectIndex( scope.selectedIndex - 1 );
  134. break;
  135. case 40: // down
  136. scope.selectIndex( scope.selectedIndex + 1 );
  137. break;
  138. }
  139. }, false );
  140. this.dom = dom;
  141. this.editor = editor;
  142. this.options = [];
  143. this.selectedIndex = - 1;
  144. this.selectedValue = null;
  145. return this;
  146. };
  147. UIOutliner.prototype = Object.create( UIElement.prototype );
  148. UIOutliner.prototype.constructor = UIOutliner;
  149. UIOutliner.prototype.selectIndex = function ( index ) {
  150. if ( index >= 0 && index < this.options.length ) {
  151. this.setValue( this.options[ index ].value );
  152. var changeEvent = document.createEvent( 'HTMLEvents' );
  153. changeEvent.initEvent( 'change', true, true );
  154. this.dom.dispatchEvent( changeEvent );
  155. }
  156. };
  157. UIOutliner.prototype.setOptions = function ( options ) {
  158. var scope = this;
  159. while ( scope.dom.children.length > 0 ) {
  160. scope.dom.removeChild( scope.dom.firstChild );
  161. }
  162. function onClick() {
  163. scope.setValue( this.value );
  164. var changeEvent = document.createEvent( 'HTMLEvents' );
  165. changeEvent.initEvent( 'change', true, true );
  166. scope.dom.dispatchEvent( changeEvent );
  167. }
  168. // Drag
  169. var currentDrag;
  170. function onDrag() {
  171. currentDrag = this;
  172. }
  173. function onDragStart( event ) {
  174. event.dataTransfer.setData( 'text', 'foo' );
  175. }
  176. function onDragOver( event ) {
  177. if ( this === currentDrag ) return;
  178. var area = event.offsetY / this.clientHeight;
  179. if ( area < 0.25 ) {
  180. this.className = 'option dragTop';
  181. } else if ( area > 0.75 ) {
  182. this.className = 'option dragBottom';
  183. } else {
  184. this.className = 'option drag';
  185. }
  186. }
  187. function onDragLeave() {
  188. if ( this === currentDrag ) return;
  189. this.className = 'option';
  190. }
  191. function onDrop( event ) {
  192. if ( this === currentDrag ) return;
  193. this.className = 'option';
  194. var scene = scope.scene;
  195. var object = scene.getObjectById( currentDrag.value );
  196. var area = event.offsetY / this.clientHeight;
  197. if ( area < 0.25 ) {
  198. var nextObject = scene.getObjectById( this.value );
  199. moveObject( object, nextObject.parent, nextObject );
  200. } else if ( area > 0.75 ) {
  201. var nextObject = scene.getObjectById( this.nextSibling.value );
  202. moveObject( object, nextObject.parent, nextObject );
  203. } else {
  204. var parentObject = scene.getObjectById( this.value );
  205. moveObject( object, parentObject );
  206. }
  207. }
  208. function moveObject( object, newParent, nextObject ) {
  209. if ( nextObject === null ) nextObject = undefined;
  210. var newParentIsChild = false;
  211. object.traverse( function ( child ) {
  212. if ( child === newParent ) newParentIsChild = true;
  213. } );
  214. if ( newParentIsChild ) return;
  215. var editor = scope.editor;
  216. editor.execute( new MoveObjectCommand( editor, object, newParent, nextObject ) );
  217. var changeEvent = document.createEvent( 'HTMLEvents' );
  218. changeEvent.initEvent( 'change', true, true );
  219. scope.dom.dispatchEvent( changeEvent );
  220. }
  221. //
  222. scope.options = [];
  223. for ( var i = 0; i < options.length; i ++ ) {
  224. var div = options[ i ];
  225. div.className = 'option';
  226. scope.dom.appendChild( div );
  227. scope.options.push( div );
  228. div.addEventListener( 'click', onClick, false );
  229. if ( div.draggable === true ) {
  230. div.addEventListener( 'drag', onDrag, false );
  231. div.addEventListener( 'dragstart', onDragStart, false ); // Firefox needs this
  232. div.addEventListener( 'dragover', onDragOver, false );
  233. div.addEventListener( 'dragleave', onDragLeave, false );
  234. div.addEventListener( 'drop', onDrop, false );
  235. }
  236. }
  237. return scope;
  238. };
  239. UIOutliner.prototype.getValue = function () {
  240. return this.selectedValue;
  241. };
  242. UIOutliner.prototype.setValue = function ( value ) {
  243. for ( var i = 0; i < this.options.length; i ++ ) {
  244. var element = this.options[ i ];
  245. if ( element.value === value ) {
  246. element.classList.add( 'active' );
  247. // scroll into view
  248. var y = element.offsetTop - this.dom.offsetTop;
  249. var bottomY = y + element.offsetHeight;
  250. var minScroll = bottomY - this.dom.offsetHeight;
  251. if ( this.dom.scrollTop > y ) {
  252. this.dom.scrollTop = y;
  253. } else if ( this.dom.scrollTop < minScroll ) {
  254. this.dom.scrollTop = minScroll;
  255. }
  256. this.selectedIndex = i;
  257. } else {
  258. element.classList.remove( 'active' );
  259. }
  260. }
  261. this.selectedValue = value;
  262. return this;
  263. };
  264. var UIPoints = function ( onAddClicked ) {
  265. UIElement.call( this );
  266. var span = new UISpan().setDisplay( 'inline-block' );
  267. this.pointsList = new UIDiv();
  268. span.add( this.pointsList );
  269. var row = new UIRow();
  270. span.add( row );
  271. var addPointButton = new UIButton( '+' ).onClick( onAddClicked );
  272. row.add( addPointButton );
  273. this.update = function () {
  274. if ( this.onChangeCallback !== null ) {
  275. this.onChangeCallback();
  276. }
  277. }.bind( this );
  278. this.dom = span.dom;
  279. this.pointsUI = [];
  280. this.lastPointIdx = 0;
  281. this.onChangeCallback = null;
  282. return this;
  283. };
  284. UIPoints.prototype = Object.create( UIElement.prototype );
  285. UIPoints.prototype.constructor = UIPoints;
  286. UIPoints.prototype.onChange = function ( callback ) {
  287. this.onChangeCallback = callback;
  288. return this;
  289. };
  290. UIPoints.prototype.clear = function () {
  291. for ( var i = 0; i < this.pointsUI.length; ++ i ) {
  292. if ( this.pointsUI[ i ] ) {
  293. this.deletePointRow( i, true );
  294. }
  295. }
  296. this.lastPointIdx = 0;
  297. };
  298. UIPoints.prototype.deletePointRow = function ( idx, dontUpdate ) {
  299. if ( ! this.pointsUI[ idx ] ) return;
  300. this.pointsList.remove( this.pointsUI[ idx ].row );
  301. this.pointsUI[ idx ] = null;
  302. if ( dontUpdate !== true ) {
  303. this.update();
  304. }
  305. };
  306. var UIPoints2 = function () {
  307. UIPoints.call( this, UIPoints2.addRow.bind( this ) );
  308. return this;
  309. };
  310. UIPoints2.prototype = Object.create( UIPoints.prototype );
  311. UIPoints2.prototype.constructor = UIPoints2;
  312. UIPoints2.addRow = function () {
  313. if ( this.pointsUI.length === 0 ) {
  314. this.pointsList.add( this.createPointRow( 0, 0 ) );
  315. } else {
  316. var point = this.pointsUI[ this.pointsUI.length - 1 ];
  317. this.pointsList.add( this.createPointRow( point.x.getValue(), point.y.getValue() ) );
  318. }
  319. this.update();
  320. };
  321. UIPoints2.prototype.getValue = function () {
  322. var points = [];
  323. var count = 0;
  324. for ( var i = 0; i < this.pointsUI.length; i ++ ) {
  325. var pointUI = this.pointsUI[ i ];
  326. if ( ! pointUI ) continue;
  327. points.push( new THREE.Vector2( pointUI.x.getValue(), pointUI.y.getValue() ) );
  328. ++ count;
  329. pointUI.lbl.setValue( count );
  330. }
  331. return points;
  332. };
  333. UIPoints2.prototype.setValue = function ( points ) {
  334. this.clear();
  335. for ( var i = 0; i < points.length; i ++ ) {
  336. var point = points[ i ];
  337. this.pointsList.add( this.createPointRow( point.x, point.y ) );
  338. }
  339. this.update();
  340. return this;
  341. };
  342. UIPoints2.prototype.createPointRow = function ( x, y ) {
  343. var pointRow = new UIDiv();
  344. var lbl = new UIText( this.lastPointIdx + 1 ).setWidth( '20px' );
  345. var txtX = new UINumber( x ).setWidth( '30px' ).onChange( this.update );
  346. var txtY = new UINumber( y ).setWidth( '30px' ).onChange( this.update );
  347. var idx = this.lastPointIdx;
  348. var scope = this;
  349. var btn = new UIButton( '-' ).onClick( function () {
  350. if ( scope.isEditing ) return;
  351. scope.deletePointRow( idx );
  352. } );
  353. this.pointsUI.push( { row: pointRow, lbl: lbl, x: txtX, y: txtY } );
  354. ++ this.lastPointIdx;
  355. pointRow.add( lbl, txtX, txtY, btn );
  356. return pointRow;
  357. };
  358. var UIPoints3 = function () {
  359. UIPoints.call( this, UIPoints3.addRow.bind( this ) );
  360. return this;
  361. };
  362. UIPoints3.prototype = Object.create( UIPoints.prototype );
  363. UIPoints3.prototype.constructor = UIPoints3;
  364. UIPoints3.addRow = function () {
  365. if ( this.pointsUI.length === 0 ) {
  366. this.pointsList.add( this.createPointRow( 0, 0, 0 ) );
  367. } else {
  368. var point = this.pointsUI[ this.pointsUI.length - 1 ];
  369. this.pointsList.add( this.createPointRow( point.x.getValue(), point.y.getValue(), point.z.getValue() ) );
  370. }
  371. this.update();
  372. };
  373. UIPoints3.prototype.getValue = function () {
  374. var points = [];
  375. var count = 0;
  376. for ( var i = 0; i < this.pointsUI.length; i ++ ) {
  377. var pointUI = this.pointsUI[ i ];
  378. if ( ! pointUI ) continue;
  379. points.push( new THREE.Vector3( pointUI.x.getValue(), pointUI.y.getValue(), pointUI.z.getValue() ) );
  380. ++ count;
  381. pointUI.lbl.setValue( count );
  382. }
  383. return points;
  384. };
  385. UIPoints3.prototype.setValue = function ( points ) {
  386. this.clear();
  387. for ( var i = 0; i < points.length; i ++ ) {
  388. var point = points[ i ];
  389. this.pointsList.add( this.createPointRow( point.x, point.y, point.z ) );
  390. }
  391. this.update();
  392. return this;
  393. };
  394. UIPoints3.prototype.createPointRow = function ( x, y, z ) {
  395. var pointRow = new UIDiv();
  396. var lbl = new UIText( this.lastPointIdx + 1 ).setWidth( '20px' );
  397. var txtX = new UINumber( x ).setWidth( '30px' ).onChange( this.update );
  398. var txtY = new UINumber( y ).setWidth( '30px' ).onChange( this.update );
  399. var txtZ = new UINumber( z ).setWidth( '30px' ).onChange( this.update );
  400. var idx = this.lastPointIdx;
  401. var scope = this;
  402. var btn = new UIButton( '-' ).onClick( function () {
  403. if ( scope.isEditing ) return;
  404. scope.deletePointRow( idx );
  405. } );
  406. this.pointsUI.push( { row: pointRow, lbl: lbl, x: txtX, y: txtY, z: txtZ } );
  407. ++ this.lastPointIdx;
  408. pointRow.add( lbl, txtX, txtY, txtZ, btn );
  409. return pointRow;
  410. };
  411. var UIBoolean = function ( boolean, text ) {
  412. UISpan.call( this );
  413. this.setMarginRight( '10px' );
  414. this.checkbox = new UICheckbox( boolean );
  415. this.text = new UIText( text ).setMarginLeft( '3px' );
  416. this.add( this.checkbox );
  417. this.add( this.text );
  418. };
  419. UIBoolean.prototype = Object.create( UISpan.prototype );
  420. UIBoolean.prototype.constructor = UIBoolean;
  421. UIBoolean.prototype.getValue = function () {
  422. return this.checkbox.getValue();
  423. };
  424. UIBoolean.prototype.setValue = function ( value ) {
  425. return this.checkbox.setValue( value );
  426. };
  427. export { UITexture, UIOutliner, UIPoints, UIPoints2, UIPoints3, UIBoolean };