ui.three.js 13 KB

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