Script.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. var Script = function ( editor ) {
  5. var signals = editor.signals;
  6. var container = new UI.Panel();
  7. container.setId( 'script' );
  8. container.setPosition( 'absolute' );
  9. container.setBackgroundColor( '#272822' );
  10. container.setDisplay( 'none' );
  11. var header = new UI.Panel();
  12. header.setPadding( '10px' );
  13. container.add( header );
  14. var title = new UI.Text().setColor( '#fff' );
  15. header.add( title );
  16. var buttonSVG = ( function () {
  17. var svg = document.createElementNS( 'http://www.w3.org/2000/svg', 'svg' );
  18. svg.setAttribute( 'width', 32 );
  19. svg.setAttribute( 'height', 32 );
  20. var path = document.createElementNS( 'http://www.w3.org/2000/svg', 'path' );
  21. path.setAttribute( 'd', 'M 12,12 L 22,22 M 22,12 12,22' );
  22. path.setAttribute( 'stroke', '#fff' );
  23. svg.appendChild( path );
  24. return svg;
  25. } )();
  26. var close = new UI.Element( buttonSVG );
  27. close.setPosition( 'absolute' );
  28. close.setTop( '3px' );
  29. close.setRight( '1px' );
  30. close.setCursor( 'pointer' );
  31. close.onClick( function () {
  32. container.setDisplay( 'none' );
  33. } );
  34. header.add( close );
  35. var delay;
  36. var currentScript;
  37. var codemirror = CodeMirror( container.dom, {
  38. value: '',
  39. lineNumbers: true,
  40. matchBrackets: true,
  41. indentWithTabs: true,
  42. tabSize: 4,
  43. indentUnit: 4
  44. } );
  45. codemirror.setOption( 'theme', 'monokai' );
  46. codemirror.on( 'change', function () {
  47. clearTimeout( delay );
  48. delay = setTimeout( function () {
  49. currentScript.source = codemirror.getValue();
  50. signals.scriptChanged.dispatch( currentScript );
  51. }, 300 );
  52. });
  53. //
  54. signals.editorCleared.add( function () {
  55. container.setDisplay( 'none' );
  56. } );
  57. signals.editScript.add( function ( object, script ) {
  58. container.setDisplay( '' );
  59. currentScript = script;
  60. title.setValue( object.name + ' / ' + script.name );
  61. codemirror.setValue( script.source );
  62. } );
  63. return container;
  64. };