Script.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. import { UIElement, UIPanel, UIText } from './libs/ui.js';
  2. import { SetScriptValueCommand } from './commands/SetScriptValueCommand.js';
  3. import { SetMaterialValueCommand } from './commands/SetMaterialValueCommand.js';
  4. function Script( editor ) {
  5. var signals = editor.signals;
  6. var container = new UIPanel();
  7. container.setId( 'script' );
  8. container.setPosition( 'absolute' );
  9. container.setBackgroundColor( '#272822' );
  10. container.setDisplay( 'none' );
  11. var header = new UIPanel();
  12. header.setPadding( '10px' );
  13. container.add( header );
  14. var title = new UIText().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 UIElement( 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 renderer;
  36. signals.rendererChanged.add( function ( newRenderer ) {
  37. renderer = newRenderer;
  38. } );
  39. var delay;
  40. var currentMode;
  41. var currentScript;
  42. var currentObject;
  43. var codemirror = CodeMirror( container.dom, {
  44. value: '',
  45. lineNumbers: true,
  46. matchBrackets: true,
  47. indentWithTabs: true,
  48. tabSize: 4,
  49. indentUnit: 4,
  50. hintOptions: {
  51. completeSingle: false
  52. }
  53. } );
  54. codemirror.setOption( 'theme', 'monokai' );
  55. codemirror.on( 'change', function () {
  56. if ( codemirror.state.focused === false ) return;
  57. clearTimeout( delay );
  58. delay = setTimeout( function () {
  59. var value = codemirror.getValue();
  60. if ( ! validate( value ) ) return;
  61. if ( typeof ( currentScript ) === 'object' ) {
  62. if ( value !== currentScript.source ) {
  63. editor.execute( new SetScriptValueCommand( editor, currentObject, currentScript, 'source', value ) );
  64. }
  65. return;
  66. }
  67. if ( currentScript !== 'programInfo' ) return;
  68. var json = JSON.parse( value );
  69. if ( JSON.stringify( currentObject.material.defines ) !== JSON.stringify( json.defines ) ) {
  70. var cmd = new SetMaterialValueCommand( editor, currentObject, 'defines', json.defines );
  71. cmd.updatable = false;
  72. editor.execute( cmd );
  73. }
  74. if ( JSON.stringify( currentObject.material.uniforms ) !== JSON.stringify( json.uniforms ) ) {
  75. var cmd = new SetMaterialValueCommand( editor, currentObject, 'uniforms', json.uniforms );
  76. cmd.updatable = false;
  77. editor.execute( cmd );
  78. }
  79. if ( JSON.stringify( currentObject.material.attributes ) !== JSON.stringify( json.attributes ) ) {
  80. var cmd = new SetMaterialValueCommand( editor, currentObject, 'attributes', json.attributes );
  81. cmd.updatable = false;
  82. editor.execute( cmd );
  83. }
  84. }, 300 );
  85. } );
  86. // prevent backspace from deleting objects
  87. var wrapper = codemirror.getWrapperElement();
  88. wrapper.addEventListener( 'keydown', function ( event ) {
  89. event.stopPropagation();
  90. } );
  91. // validate
  92. var errorLines = [];
  93. var widgets = [];
  94. var validate = function ( string ) {
  95. var valid;
  96. var errors = [];
  97. return codemirror.operation( function () {
  98. while ( errorLines.length > 0 ) {
  99. codemirror.removeLineClass( errorLines.shift(), 'background', 'errorLine' );
  100. }
  101. while ( widgets.length > 0 ) {
  102. codemirror.removeLineWidget( widgets.shift() );
  103. }
  104. //
  105. switch ( currentMode ) {
  106. case 'javascript':
  107. try {
  108. var syntax = esprima.parse( string, { tolerant: true } );
  109. errors = syntax.errors;
  110. } catch ( error ) {
  111. errors.push( {
  112. lineNumber: error.lineNumber - 1,
  113. message: error.message
  114. } );
  115. }
  116. for ( var i = 0; i < errors.length; i ++ ) {
  117. var error = errors[ i ];
  118. error.message = error.message.replace( /Line [0-9]+: /, '' );
  119. }
  120. break;
  121. case 'json':
  122. errors = [];
  123. jsonlint.parseError = function ( message, info ) {
  124. message = message.split( '\n' )[ 3 ];
  125. errors.push( {
  126. lineNumber: info.loc.first_line - 1,
  127. message: message
  128. } );
  129. };
  130. try {
  131. jsonlint.parse( string );
  132. } catch ( error ) {
  133. // ignore failed error recovery
  134. }
  135. break;
  136. case 'glsl':
  137. currentObject.material[ currentScript ] = string;
  138. currentObject.material.needsUpdate = true;
  139. signals.materialChanged.dispatch( currentObject.material );
  140. var programs = renderer.info.programs;
  141. valid = true;
  142. var parseMessage = /^(?:ERROR|WARNING): \d+:(\d+): (.*)/g;
  143. for ( var i = 0, n = programs.length; i !== n; ++ i ) {
  144. var diagnostics = programs[ i ].diagnostics;
  145. if ( diagnostics === undefined ||
  146. diagnostics.material !== currentObject.material ) continue;
  147. if ( ! diagnostics.runnable ) valid = false;
  148. var shaderInfo = diagnostics[ currentScript ];
  149. var lineOffset = shaderInfo.prefix.split( /\r\n|\r|\n/ ).length;
  150. while ( true ) {
  151. var parseResult = parseMessage.exec( shaderInfo.log );
  152. if ( parseResult === null ) break;
  153. errors.push( {
  154. lineNumber: parseResult[ 1 ] - lineOffset,
  155. message: parseResult[ 2 ]
  156. } );
  157. } // messages
  158. break;
  159. } // programs
  160. } // mode switch
  161. for ( var i = 0; i < errors.length; i ++ ) {
  162. var error = errors[ i ];
  163. var message = document.createElement( 'div' );
  164. message.className = 'esprima-error';
  165. message.textContent = error.message;
  166. var lineNumber = Math.max( error.lineNumber, 0 );
  167. errorLines.push( lineNumber );
  168. codemirror.addLineClass( lineNumber, 'background', 'errorLine' );
  169. var widget = codemirror.addLineWidget( lineNumber, message );
  170. widgets.push( widget );
  171. }
  172. return valid !== undefined ? valid : errors.length === 0;
  173. } );
  174. };
  175. // tern js autocomplete
  176. var server = new CodeMirror.TernServer( {
  177. caseInsensitive: true,
  178. plugins: { threejs: null }
  179. } );
  180. codemirror.setOption( 'extraKeys', {
  181. 'Ctrl-Space': function ( cm ) {
  182. server.complete( cm );
  183. },
  184. 'Ctrl-I': function ( cm ) {
  185. server.showType( cm );
  186. },
  187. 'Ctrl-O': function ( cm ) {
  188. server.showDocs( cm );
  189. },
  190. 'Alt-.': function ( cm ) {
  191. server.jumpToDef( cm );
  192. },
  193. 'Alt-,': function ( cm ) {
  194. server.jumpBack( cm );
  195. },
  196. 'Ctrl-Q': function ( cm ) {
  197. server.rename( cm );
  198. },
  199. 'Ctrl-.': function ( cm ) {
  200. server.selectName( cm );
  201. }
  202. } );
  203. codemirror.on( 'cursorActivity', function ( cm ) {
  204. if ( currentMode !== 'javascript' ) return;
  205. server.updateArgHints( cm );
  206. } );
  207. codemirror.on( 'keypress', function ( cm, kb ) {
  208. if ( currentMode !== 'javascript' ) return;
  209. var typed = String.fromCharCode( kb.which || kb.keyCode );
  210. if ( /[\w\.]/.exec( typed ) ) {
  211. server.complete( cm );
  212. }
  213. } );
  214. //
  215. signals.editorCleared.add( function () {
  216. container.setDisplay( 'none' );
  217. } );
  218. signals.editScript.add( function ( object, script ) {
  219. var mode, name, source;
  220. if ( typeof ( script ) === 'object' ) {
  221. mode = 'javascript';
  222. name = script.name;
  223. source = script.source;
  224. title.setValue( object.name + ' / ' + name );
  225. } else {
  226. switch ( script ) {
  227. case 'vertexShader':
  228. mode = 'glsl';
  229. name = 'Vertex Shader';
  230. source = object.material.vertexShader || '';
  231. break;
  232. case 'fragmentShader':
  233. mode = 'glsl';
  234. name = 'Fragment Shader';
  235. source = object.material.fragmentShader || '';
  236. break;
  237. case 'programInfo':
  238. mode = 'json';
  239. name = 'Program Properties';
  240. var json = {
  241. defines: object.material.defines,
  242. uniforms: object.material.uniforms,
  243. attributes: object.material.attributes
  244. };
  245. source = JSON.stringify( json, null, '\t' );
  246. }
  247. title.setValue( object.material.name + ' / ' + name );
  248. }
  249. currentMode = mode;
  250. currentScript = script;
  251. currentObject = object;
  252. container.setDisplay( '' );
  253. codemirror.setValue( source );
  254. codemirror.clearHistory();
  255. if ( mode === 'json' ) mode = { name: 'javascript', json: true };
  256. codemirror.setOption( 'mode', mode );
  257. } );
  258. signals.scriptRemoved.add( function ( script ) {
  259. if ( currentScript === script ) {
  260. container.setDisplay( 'none' );
  261. }
  262. } );
  263. return container;
  264. }
  265. export { Script };