ui.three.js 8.5 KB

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