2
0

ui.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  1. var UI = {};
  2. UI.Element = function () {};
  3. UI.Element.prototype = {
  4. setId: function ( id ) {
  5. this.dom.id = id;
  6. return this;
  7. },
  8. setClass: function ( name ) {
  9. this.dom.className = name;
  10. return this;
  11. },
  12. setStyle: function ( style, array ) {
  13. for ( var i = 0; i < array.length; i ++ ) {
  14. this.dom.style[ style ] = array[ i ];
  15. }
  16. },
  17. setDisabled: function ( value ) {
  18. this.dom.disabled = value;
  19. return this;
  20. },
  21. setTextContent: function ( value ) {
  22. this.dom.textContent = value;
  23. return this;
  24. }
  25. }
  26. // properties
  27. var properties = [ 'position', 'left', 'top', 'right', 'bottom', 'width', 'height', 'border', 'borderLeft',
  28. 'borderTop', 'borderRight', 'borderBottom', 'borderColor', 'display', 'overflow', 'margin', 'marginLeft', 'marginTop', 'marginRight', 'marginBottom', 'padding', 'paddingLeft', 'paddingTop', 'paddingRight', 'paddingBottom', 'color',
  29. 'backgroundColor', 'opacity', 'fontSize', 'fontWeight', 'textTransform', 'cursor' ];
  30. properties.forEach( function ( property ) {
  31. var method = 'set' + property.substr( 0, 1 ).toUpperCase() + property.substr( 1, property.length );
  32. UI.Element.prototype[ method ] = function () {
  33. this.setStyle( property, arguments );
  34. return this;
  35. };
  36. } );
  37. // events
  38. var events = [ 'KeyUp', 'KeyDown', 'MouseOver', 'MouseOut', 'Click', 'Change' ];
  39. events.forEach( function ( event ) {
  40. var method = 'on' + event;
  41. UI.Element.prototype[ method ] = function ( callback ) {
  42. this.dom.addEventListener( event.toLowerCase(), callback.bind( this ), false );
  43. return this;
  44. };
  45. } );
  46. // Panel
  47. UI.Panel = function () {
  48. UI.Element.call( this );
  49. var dom = document.createElement( 'div' );
  50. dom.className = 'Panel';
  51. this.dom = dom;
  52. return this;
  53. };
  54. UI.Panel.prototype = Object.create( UI.Element.prototype );
  55. UI.Panel.prototype.add = function () {
  56. for ( var i = 0; i < arguments.length; i ++ ) {
  57. this.dom.appendChild( arguments[ i ].dom );
  58. }
  59. return this;
  60. };
  61. UI.Panel.prototype.remove = function () {
  62. for ( var i = 0; i < arguments.length; i ++ ) {
  63. this.dom.removeChild( arguments[ i ].dom );
  64. }
  65. return this;
  66. };
  67. UI.Panel.prototype.clear = function () {
  68. while ( this.dom.children.length ) {
  69. this.dom.removeChild( this.dom.lastChild );
  70. }
  71. };
  72. // Text
  73. UI.Text = function ( text ) {
  74. UI.Element.call( this );
  75. var dom = document.createElement( 'span' );
  76. dom.className = 'Text';
  77. dom.style.cursor = 'default';
  78. dom.style.display = 'inline-block';
  79. dom.style.verticalAlign = 'middle';
  80. this.dom = dom;
  81. this.setValue( text );
  82. return this;
  83. };
  84. UI.Text.prototype = Object.create( UI.Element.prototype );
  85. UI.Text.prototype.setValue = function ( value ) {
  86. if ( value !== undefined ) {
  87. this.dom.textContent = value;
  88. }
  89. return this;
  90. };
  91. // Input
  92. UI.Input = function () {
  93. UI.Element.call( this );
  94. var scope = this;
  95. var dom = document.createElement( 'input' );
  96. dom.className = 'Input';
  97. dom.style.padding = '2px';
  98. dom.style.border = '1px solid #ccc';
  99. dom.addEventListener( 'keydown', function ( event ) {
  100. event.stopPropagation();
  101. }, false );
  102. this.dom = dom;
  103. return this;
  104. };
  105. UI.Input.prototype = Object.create( UI.Element.prototype );
  106. UI.Input.prototype.getValue = function () {
  107. return this.dom.value;
  108. };
  109. UI.Input.prototype.setValue = function ( value ) {
  110. this.dom.value = value;
  111. return this;
  112. };
  113. // TextArea
  114. UI.TextArea = function () {
  115. UI.Element.call( this );
  116. var scope = this;
  117. var dom = document.createElement( 'textarea' );
  118. dom.className = 'TextArea';
  119. dom.style.padding = '2px';
  120. dom.style.border = '1px solid #ccc';
  121. dom.addEventListener( 'keydown', function ( event ) {
  122. event.stopPropagation();
  123. }, false );
  124. this.dom = dom;
  125. return this;
  126. };
  127. UI.TextArea.prototype = Object.create( UI.Element.prototype );
  128. UI.TextArea.prototype.getValue = function () {
  129. return this.dom.value;
  130. };
  131. UI.TextArea.prototype.setValue = function ( value ) {
  132. this.dom.value = value;
  133. return this;
  134. };
  135. // Select
  136. UI.Select = function () {
  137. UI.Element.call( this );
  138. var scope = this;
  139. var dom = document.createElement( 'select' );
  140. dom.className = 'Select';
  141. dom.style.width = '64px';
  142. dom.style.height = '16px';
  143. dom.style.border = '0px';
  144. dom.style.padding = '0px';
  145. this.dom = dom;
  146. return this;
  147. };
  148. UI.Select.prototype = Object.create( UI.Element.prototype );
  149. UI.Select.prototype.setMultiple = function ( boolean ) {
  150. this.dom.multiple = boolean;
  151. return this;
  152. };
  153. UI.Select.prototype.setOptions = function ( options ) {
  154. var selected = this.dom.value;
  155. while ( this.dom.children.length > 0 ) {
  156. this.dom.removeChild( this.dom.firstChild );
  157. }
  158. for ( var key in options ) {
  159. var option = document.createElement( 'option' );
  160. option.value = key;
  161. option.innerHTML = options[ key ];
  162. this.dom.appendChild( option );
  163. }
  164. this.dom.value = selected;
  165. return this;
  166. };
  167. UI.Select.prototype.getValue = function () {
  168. return this.dom.value;
  169. };
  170. UI.Select.prototype.setValue = function ( value ) {
  171. this.dom.value = value;
  172. return this;
  173. };
  174. // FancySelect
  175. UI.FancySelect = function () {
  176. UI.Element.call( this );
  177. var scope = this;
  178. var dom = document.createElement( 'div' );
  179. dom.className = 'FancySelect';
  180. dom.tabIndex = 0; // keyup event is ignored without setting tabIndex
  181. // Broadcast for object selection after arrow navigation
  182. var changeEvent = document.createEvent('HTMLEvents');
  183. changeEvent.initEvent( 'change', true, true );
  184. // Prevent native scroll behavior
  185. dom.addEventListener( 'keydown', function (event) {
  186. switch ( event.keyCode ) {
  187. case 38: // up
  188. case 40: // down
  189. event.preventDefault();
  190. event.stopPropagation();
  191. break;
  192. }
  193. }, false);
  194. // Keybindings to support arrow navigation
  195. dom.addEventListener( 'keyup', function (event) {
  196. switch ( event.keyCode ) {
  197. case 38: // up
  198. case 40: // down
  199. scope.selectedIndex += ( event.keyCode == 38 ) ? -1 : 1;
  200. if ( scope.selectedIndex >= 0 && scope.selectedIndex < scope.options.length ) {
  201. // Highlight selected dom elem and scroll parent if needed
  202. scope.setValue( scope.options[ scope.selectedIndex ].value );
  203. // Invoke object/helper/mesh selection logic
  204. scope.dom.dispatchEvent( changeEvent );
  205. }
  206. break;
  207. }
  208. }, false);
  209. this.dom = dom;
  210. this.options = [];
  211. this.selectedIndex = -1;
  212. this.selectedValue = null;
  213. return this;
  214. };
  215. UI.FancySelect.prototype = Object.create( UI.Element.prototype );
  216. UI.FancySelect.prototype.setOptions = function ( options ) {
  217. var scope = this;
  218. var changeEvent = document.createEvent( 'HTMLEvents' );
  219. changeEvent.initEvent( 'change', true, true );
  220. while ( scope.dom.children.length > 0 ) {
  221. scope.dom.removeChild( scope.dom.firstChild );
  222. }
  223. scope.options = [];
  224. for ( var key in options ) {
  225. var option = document.createElement('div');
  226. option.className = 'option';
  227. option.innerHTML = options[ key ];
  228. option.value = key;
  229. scope.dom.appendChild( option );
  230. scope.options.push( option );
  231. option.addEventListener( 'click', function ( event ) {
  232. scope.setValue( this.value );
  233. scope.dom.dispatchEvent( changeEvent );
  234. }, false );
  235. }
  236. return scope;
  237. };
  238. UI.FancySelect.prototype.getValue = function () {
  239. return this.selectedValue;
  240. };
  241. UI.FancySelect.prototype.setValue = function ( value ) {
  242. if ( typeof value === 'number' ) value = value.toString();
  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. // Checkbox
  265. UI.Checkbox = function ( boolean ) {
  266. UI.Element.call( this );
  267. var scope = this;
  268. var dom = document.createElement( 'input' );
  269. dom.className = 'Checkbox';
  270. dom.type = 'checkbox';
  271. this.dom = dom;
  272. this.setValue( boolean );
  273. return this;
  274. };
  275. UI.Checkbox.prototype = Object.create( UI.Element.prototype );
  276. UI.Checkbox.prototype.getValue = function () {
  277. return this.dom.checked;
  278. };
  279. UI.Checkbox.prototype.setValue = function ( value ) {
  280. if ( value !== undefined ) {
  281. this.dom.checked = value;
  282. }
  283. return this;
  284. };
  285. // Color
  286. UI.Color = function () {
  287. UI.Element.call( this );
  288. var scope = this;
  289. var dom = document.createElement( 'input' );
  290. dom.className = 'Color';
  291. dom.style.width = '64px';
  292. dom.style.height = '16px';
  293. dom.style.border = '0px';
  294. dom.style.padding = '0px';
  295. dom.style.backgroundColor = 'transparent';
  296. try {
  297. dom.type = 'color';
  298. dom.value = '#ffffff';
  299. } catch ( exception ) {}
  300. this.dom = dom;
  301. return this;
  302. };
  303. UI.Color.prototype = Object.create( UI.Element.prototype );
  304. UI.Color.prototype.getValue = function () {
  305. return this.dom.value;
  306. };
  307. UI.Color.prototype.getHexValue = function () {
  308. return parseInt( this.dom.value.substr( 1 ), 16 );
  309. };
  310. UI.Color.prototype.setValue = function ( value ) {
  311. this.dom.value = value;
  312. return this;
  313. };
  314. UI.Color.prototype.setHexValue = function ( hex ) {
  315. this.dom.value = "#" + ( '000000' + hex.toString( 16 ) ).slice( -6 );
  316. return this;
  317. };
  318. // Number
  319. UI.Number = function ( number ) {
  320. UI.Element.call( this );
  321. var scope = this;
  322. var dom = document.createElement( 'input' );
  323. dom.className = 'Number';
  324. dom.value = '0.00';
  325. dom.addEventListener( 'keydown', function ( event ) {
  326. event.stopPropagation();
  327. if ( event.keyCode === 13 ) dom.blur();
  328. }, false );
  329. this.min = - Infinity;
  330. this.max = Infinity;
  331. this.precision = 2;
  332. this.step = 1;
  333. this.dom = dom;
  334. this.setValue( number );
  335. var changeEvent = document.createEvent( 'HTMLEvents' );
  336. changeEvent.initEvent( 'change', true, true );
  337. var distance = 0;
  338. var onMouseDownValue = 0;
  339. var pointer = new THREE.Vector2();
  340. var prevPointer = new THREE.Vector2();
  341. var onMouseDown = function ( event ) {
  342. event.preventDefault();
  343. distance = 0;
  344. onMouseDownValue = parseFloat( dom.value );
  345. prevPointer.set( event.clientX, event.clientY );
  346. document.addEventListener( 'mousemove', onMouseMove, false );
  347. document.addEventListener( 'mouseup', onMouseUp, false );
  348. };
  349. var onMouseMove = function ( event ) {
  350. var currentValue = dom.value;
  351. pointer.set( event.clientX, event.clientY );
  352. distance += ( pointer.x - prevPointer.x ) - ( pointer.y - prevPointer.y );
  353. var number = onMouseDownValue + ( distance / ( event.shiftKey ? 5 : 50 ) ) * scope.step;
  354. dom.value = Math.min( scope.max, Math.max( scope.min, number ) ).toFixed( scope.precision );
  355. if ( currentValue !== dom.value ) dom.dispatchEvent( changeEvent );
  356. prevPointer.set( event.clientX, event.clientY );
  357. };
  358. var onMouseUp = function ( event ) {
  359. document.removeEventListener( 'mousemove', onMouseMove, false );
  360. document.removeEventListener( 'mouseup', onMouseUp, false );
  361. if ( Math.abs( distance ) < 2 ) {
  362. dom.focus();
  363. dom.select();
  364. }
  365. };
  366. var onChange = function ( event ) {
  367. var number = parseFloat( dom.value );
  368. dom.value = isNaN( number ) === false ? number : 0;
  369. };
  370. var onFocus = function ( event ) {
  371. dom.style.backgroundColor = '';
  372. dom.style.borderColor = '#ccc';
  373. dom.style.cursor = '';
  374. };
  375. var onBlur = function ( event ) {
  376. dom.style.backgroundColor = 'transparent';
  377. dom.style.borderColor = 'transparent';
  378. dom.style.cursor = 'col-resize';
  379. };
  380. dom.addEventListener( 'mousedown', onMouseDown, false );
  381. dom.addEventListener( 'change', onChange, false );
  382. dom.addEventListener( 'focus', onFocus, false );
  383. dom.addEventListener( 'blur', onBlur, false );
  384. return this;
  385. };
  386. UI.Number.prototype = Object.create( UI.Element.prototype );
  387. UI.Number.prototype.getValue = function () {
  388. return parseFloat( this.dom.value );
  389. };
  390. UI.Number.prototype.setValue = function ( value ) {
  391. if ( value !== undefined ) {
  392. this.dom.value = value.toFixed( this.precision );
  393. }
  394. return this;
  395. };
  396. UI.Number.prototype.setRange = function ( min, max ) {
  397. this.min = min;
  398. this.max = max;
  399. return this;
  400. };
  401. UI.Number.prototype.setPrecision = function ( precision ) {
  402. this.precision = precision;
  403. return this;
  404. };
  405. // Integer
  406. UI.Integer = function ( number ) {
  407. UI.Element.call( this );
  408. var scope = this;
  409. var dom = document.createElement( 'input' );
  410. dom.className = 'Number';
  411. dom.value = '0.00';
  412. dom.addEventListener( 'keydown', function ( event ) {
  413. event.stopPropagation();
  414. }, false );
  415. this.min = - Infinity;
  416. this.max = Infinity;
  417. this.step = 1;
  418. this.dom = dom;
  419. this.setValue( number );
  420. var changeEvent = document.createEvent( 'HTMLEvents' );
  421. changeEvent.initEvent( 'change', true, true );
  422. var distance = 0;
  423. var onMouseDownValue = 0;
  424. var pointer = new THREE.Vector2();
  425. var prevPointer = new THREE.Vector2();
  426. var onMouseDown = function ( event ) {
  427. event.preventDefault();
  428. distance = 0;
  429. onMouseDownValue = parseFloat( dom.value );
  430. prevPointer.set( event.clientX, event.clientY );
  431. document.addEventListener( 'mousemove', onMouseMove, false );
  432. document.addEventListener( 'mouseup', onMouseUp, false );
  433. };
  434. var onMouseMove = function ( event ) {
  435. var currentValue = dom.value;
  436. pointer.set( event.clientX, event.clientY );
  437. distance += ( pointer.x - prevPointer.x ) - ( pointer.y - prevPointer.y );
  438. var number = onMouseDownValue + ( distance / ( event.shiftKey ? 5 : 50 ) ) * scope.step;
  439. dom.value = Math.min( scope.max, Math.max( scope.min, number ) ) | 0;
  440. if ( currentValue !== dom.value ) dom.dispatchEvent( changeEvent );
  441. prevPointer.set( event.clientX, event.clientY );
  442. };
  443. var onMouseUp = function ( event ) {
  444. document.removeEventListener( 'mousemove', onMouseMove, false );
  445. document.removeEventListener( 'mouseup', onMouseUp, false );
  446. if ( Math.abs( distance ) < 2 ) {
  447. dom.focus();
  448. dom.select();
  449. }
  450. };
  451. var onChange = function ( event ) {
  452. var number = parseInt( dom.value );
  453. if ( isNaN( number ) === false ) {
  454. dom.value = number;
  455. }
  456. };
  457. var onFocus = function ( event ) {
  458. dom.style.backgroundColor = '';
  459. dom.style.borderColor = '#ccc';
  460. dom.style.cursor = '';
  461. };
  462. var onBlur = function ( event ) {
  463. dom.style.backgroundColor = 'transparent';
  464. dom.style.borderColor = 'transparent';
  465. dom.style.cursor = 'col-resize';
  466. };
  467. dom.addEventListener( 'mousedown', onMouseDown, false );
  468. dom.addEventListener( 'change', onChange, false );
  469. dom.addEventListener( 'focus', onFocus, false );
  470. dom.addEventListener( 'blur', onBlur, false );
  471. return this;
  472. };
  473. UI.Integer.prototype = Object.create( UI.Element.prototype );
  474. UI.Integer.prototype.getValue = function () {
  475. return parseInt( this.dom.value );
  476. };
  477. UI.Integer.prototype.setValue = function ( value ) {
  478. if ( value !== undefined ) {
  479. this.dom.value = value | 0;
  480. }
  481. return this;
  482. };
  483. UI.Integer.prototype.setRange = function ( min, max ) {
  484. this.min = min;
  485. this.max = max;
  486. return this;
  487. };
  488. // Break
  489. UI.Break = function () {
  490. UI.Element.call( this );
  491. var dom = document.createElement( 'br' );
  492. dom.className = 'Break';
  493. this.dom = dom;
  494. return this;
  495. };
  496. UI.Break.prototype = Object.create( UI.Element.prototype );
  497. // HorizontalRule
  498. UI.HorizontalRule = function () {
  499. UI.Element.call( this );
  500. var dom = document.createElement( 'hr' );
  501. dom.className = 'HorizontalRule';
  502. this.dom = dom;
  503. return this;
  504. };
  505. UI.HorizontalRule.prototype = Object.create( UI.Element.prototype );
  506. // Button
  507. UI.Button = function ( value ) {
  508. UI.Element.call( this );
  509. var scope = this;
  510. var dom = document.createElement( 'button' );
  511. dom.className = 'Button';
  512. this.dom = dom;
  513. this.dom.textContent = value;
  514. return this;
  515. };
  516. UI.Button.prototype = Object.create( UI.Element.prototype );
  517. UI.Button.prototype.setLabel = function ( value ) {
  518. this.dom.textContent = value;
  519. return this;
  520. };