ui.three.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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.needsUpdate = true;
  54. scope.setValue( texture );
  55. if ( scope.onChangeCallback ) scope.onChangeCallback();
  56. }, false );
  57. image.src = event.target.result;
  58. }, false );
  59. reader.readAsDataURL( file );
  60. }
  61. }
  62. form.reset();
  63. }
  64. this.dom = dom;
  65. this.texture = null;
  66. this.onChangeCallback = null;
  67. return this;
  68. };
  69. UI.Texture.prototype = Object.create( UI.Element.prototype );
  70. UI.Texture.prototype.constructor = UI.Texture;
  71. UI.Texture.prototype.getValue = function () {
  72. return this.texture;
  73. };
  74. UI.Texture.prototype.setValue = function ( texture ) {
  75. var canvas = this.dom.children[ 0 ];
  76. var name = this.dom.children[ 1 ];
  77. var context = canvas.getContext( '2d' );
  78. if ( texture !== null ) {
  79. var image = texture.image;
  80. if ( image !== undefined && image.width > 0 ) {
  81. name.value = texture.sourceFile;
  82. var scale = canvas.width / image.width;
  83. context.drawImage( image, 0, 0, image.width * scale, image.height * scale );
  84. } else {
  85. name.value = texture.sourceFile + ' (error)';
  86. context.clearRect( 0, 0, canvas.width, canvas.height );
  87. }
  88. } else {
  89. name.value = '';
  90. if ( context !== null ) {
  91. // Seems like context can be null if the canvas is not visible
  92. context.clearRect( 0, 0, canvas.width, canvas.height );
  93. }
  94. }
  95. this.texture = texture;
  96. };
  97. UI.Texture.prototype.onChange = function ( callback ) {
  98. this.onChangeCallback = callback;
  99. return this;
  100. };
  101. // Outliner
  102. UI.Outliner = function ( editor ) {
  103. UI.Element.call( this );
  104. var scope = this;
  105. var dom = document.createElement( 'div' );
  106. dom.className = 'Outliner';
  107. dom.tabIndex = 0; // keyup event is ignored without setting tabIndex
  108. // hack
  109. this.scene = editor.scene;
  110. // Prevent native scroll behavior
  111. dom.addEventListener( 'keydown', function ( event ) {
  112. switch ( event.keyCode ) {
  113. case 38: // up
  114. case 40: // down
  115. event.preventDefault();
  116. event.stopPropagation();
  117. break;
  118. }
  119. }, false );
  120. // Keybindings to support arrow navigation
  121. dom.addEventListener( 'keyup', function ( event ) {
  122. switch ( event.keyCode ) {
  123. case 38: // up
  124. scope.selectIndex( scope.selectedIndex - 1 );
  125. break;
  126. case 40: // down
  127. scope.selectIndex( scope.selectedIndex + 1 );
  128. break;
  129. }
  130. }, false );
  131. this.dom = dom;
  132. this.options = [];
  133. this.selectedIndex = - 1;
  134. this.selectedValue = null;
  135. return this;
  136. };
  137. UI.Outliner.prototype = Object.create( UI.Element.prototype );
  138. UI.Outliner.prototype.constructor = UI.Outliner;
  139. UI.Outliner.prototype.selectIndex = function ( index ) {
  140. if ( index >= 0 && index < this.options.length ) {
  141. this.setValue( this.options[ index ].value );
  142. var changeEvent = document.createEvent( 'HTMLEvents' );
  143. changeEvent.initEvent( 'change', true, true );
  144. this.dom.dispatchEvent( changeEvent );
  145. }
  146. };
  147. UI.Outliner.prototype.setOptions = function ( options ) {
  148. var scope = this;
  149. while ( scope.dom.children.length > 0 ) {
  150. scope.dom.removeChild( scope.dom.firstChild );
  151. }
  152. function onClick() {
  153. scope.setValue( this.value );
  154. var changeEvent = document.createEvent( 'HTMLEvents' );
  155. changeEvent.initEvent( 'change', true, true );
  156. scope.dom.dispatchEvent( changeEvent );
  157. }
  158. // Drag
  159. var currentDrag;
  160. function onDrag( event ) {
  161. currentDrag = this;
  162. }
  163. function onDragStart( event ) {
  164. event.dataTransfer.setData( 'text', 'foo' );
  165. }
  166. function onDragOver( event ) {
  167. if ( this === currentDrag ) return;
  168. var area = event.offsetY / this.clientHeight;
  169. if ( area < 0.25 ) {
  170. this.className = 'option dragTop';
  171. } else if ( area > 0.75 ) {
  172. this.className = 'option dragBottom';
  173. } else {
  174. this.className = 'option drag';
  175. }
  176. }
  177. function onDragLeave() {
  178. if ( this === currentDrag ) return;
  179. this.className = 'option';
  180. }
  181. function onDrop( event ) {
  182. if ( this === currentDrag ) return;
  183. this.className = 'option';
  184. var scene = scope.scene;
  185. var object = scene.getObjectById( currentDrag.value );
  186. var area = event.offsetY / this.clientHeight;
  187. if ( area < 0.25 ) {
  188. var nextObject = scene.getObjectById( this.value );
  189. moveObject( object, nextObject.parent, nextObject );
  190. } else if ( area > 0.75 ) {
  191. var nextObject = scene.getObjectById( this.nextSibling.value );
  192. moveObject( object, nextObject.parent, nextObject );
  193. } else {
  194. var parentObject = scene.getObjectById( this.value );
  195. moveObject( object, parentObject );
  196. }
  197. }
  198. function moveObject( object, newParent, nextObject ) {
  199. if ( nextObject === null ) nextObject = undefined;
  200. var newParentIsChild = false;
  201. object.traverse( function ( child ) {
  202. if ( child === newParent ) newParentIsChild = true;
  203. } );
  204. if ( newParentIsChild ) return;
  205. editor.execute( new MoveObjectCommand( object, newParent, nextObject ) );
  206. var changeEvent = document.createEvent( 'HTMLEvents' );
  207. changeEvent.initEvent( 'change', true, true );
  208. scope.dom.dispatchEvent( changeEvent );
  209. }
  210. //
  211. scope.options = [];
  212. for ( var i = 0; i < options.length; i ++ ) {
  213. var div = options[ i ];
  214. div.className = 'option';
  215. scope.dom.appendChild( div );
  216. scope.options.push( div );
  217. div.addEventListener( 'click', onClick, false );
  218. if ( div.draggable === true ) {
  219. div.addEventListener( 'drag', onDrag, false );
  220. div.addEventListener( 'dragstart', onDragStart, false ); // Firefox needs this
  221. div.addEventListener( 'dragover', onDragOver, false );
  222. div.addEventListener( 'dragleave', onDragLeave, false );
  223. div.addEventListener( 'drop', onDrop, false );
  224. }
  225. }
  226. return scope;
  227. };
  228. UI.Outliner.prototype.getValue = function () {
  229. return this.selectedValue;
  230. };
  231. UI.Outliner.prototype.setValue = function ( value ) {
  232. for ( var i = 0; i < this.options.length; i ++ ) {
  233. var element = this.options[ i ];
  234. if ( element.value === value ) {
  235. element.classList.add( 'active' );
  236. // scroll into view
  237. var y = element.offsetTop - this.dom.offsetTop;
  238. var bottomY = y + element.offsetHeight;
  239. var minScroll = bottomY - this.dom.offsetHeight;
  240. if ( this.dom.scrollTop > y ) {
  241. this.dom.scrollTop = y;
  242. } else if ( this.dom.scrollTop < minScroll ) {
  243. this.dom.scrollTop = minScroll;
  244. }
  245. this.selectedIndex = i;
  246. } else {
  247. element.classList.remove( 'active' );
  248. }
  249. }
  250. this.selectedValue = value;
  251. return this;
  252. };
  253. UI.THREE = {};
  254. UI.THREE.Boolean = function ( boolean, text ) {
  255. UI.Span.call( this );
  256. this.setMarginRight( '10px' );
  257. this.checkbox = new UI.Checkbox( boolean );
  258. this.text = new UI.Text( text ).setMarginLeft( '3px' );
  259. this.add( this.checkbox );
  260. this.add( this.text );
  261. };
  262. UI.THREE.Boolean.prototype = Object.create( UI.Span.prototype );
  263. UI.THREE.Boolean.prototype.constructor = UI.THREE.Boolean;
  264. UI.THREE.Boolean.prototype.getValue = function () {
  265. return this.checkbox.getValue();
  266. };
  267. UI.THREE.Boolean.prototype.setValue = function ( value ) {
  268. return this.checkbox.setValue( value );
  269. };