Explorar o código

Editor: Hopefully last iteration of the scripting system.

Mr.doob %!s(int64=10) %!d(string=hai) anos
pai
achega
cd916ee722

+ 3 - 3
editor/js/Sidebar.Object3D.js

@@ -40,7 +40,7 @@ Sidebar.Object3D = function ( editor ) {
 	// name
 
 	var objectNameRow = new UI.Panel();
-	var objectName = new UI.Input().setWidth( '150px' ).setColor( '#444' ).setFontSize( '12px' ).onChange( function () {
+	var objectName = new UI.Input().setWidth( '150px' ).setFontSize( '12px' ).onChange( function () {
 
 			editor.setObjectName( editor.selected, objectName.getValue() );
 
@@ -54,7 +54,7 @@ Sidebar.Object3D = function ( editor ) {
 	// parent
 
 	var objectParentRow = new UI.Panel();
-	var objectParent = new UI.Select().setWidth( '150px' ).setColor( '#444' ).setFontSize( '12px' ).onChange( update );
+	var objectParent = new UI.Select().setWidth( '150px' ).setFontSize( '12px' ).onChange( update );
 
 	objectParentRow.add( new UI.Text( 'Parent' ).setWidth( '90px' ) );
 	objectParentRow.add( objectParent );
@@ -200,7 +200,7 @@ Sidebar.Object3D = function ( editor ) {
 	container.add( objectVisibleRow );
 
 	// user data
-	
+
 	var timeout;
 
 	var objectUserDataRow = new UI.Panel();

+ 1 - 1
editor/js/Sidebar.Scene.js

@@ -133,7 +133,7 @@ Sidebar.Scene = function ( editor ) {
 
 		var options = [];
 
-		options.push( { value: camera.id, html: '<span class="type ' + camera.type + '"></span> ' + camera.name } );
+		// options.push( { value: camera.id, html: '<span class="type ' + camera.type + '"></span> ' + camera.name } );
 		options.push( { value: scene.id, html: '<span class="type ' + scene.type + '"></span> ' + scene.name } );
 
 		( function addObjects( objects, pad ) {

+ 9 - 19
editor/js/Sidebar.Script.js

@@ -23,26 +23,12 @@ Sidebar.Script = function ( editor ) {
 	var scriptsContainer = new UI.Panel();
 	container.add( scriptsContainer );
 
-	var eventType = new UI.Select();
-	eventType.setOptions( {
-
-		'init': 'init',
-		'keydown': 'keydown',
-		'keyup': 'keyup',
-		'mousedown': 'mousedown',
-		'mouseup': 'mouseup',
-		'mousemove': 'mousemove',
-		'update': 'update'
-
-	} );
-	container.add( eventType );
-
-	var button = new UI.Button( 'Add' );
-	button.setMarginLeft( '5px' );
-	button.onClick( function () {
+	var newScript = new UI.Button( 'New' );
+	newScript.setMarginLeft( '5px' );
+	newScript.onClick( function () {
 
 		var script = new UI.ScriptEditor();
-		script.setValue( { event: eventType.getValue(), source: '' } );
+		script.setValue( { name: '', source: 'return {\n\tupdate: function ( event ) {}\n};' } );
 		script.onChange( function () {
 
 			signals.scriptChanged.dispatch();
@@ -51,7 +37,11 @@ Sidebar.Script = function ( editor ) {
 		scriptsContainer.add( script );
 
 	} );
-	container.add( button );
+	container.add( newScript );
+
+	var loadScript = new UI.Button( 'Load' );
+	loadScript.setMarginLeft( '4px' );
+	container.add( loadScript );
 
 	// signals
 

+ 15 - 7
editor/js/libs/app.js

@@ -22,7 +22,6 @@ var APP = {
 			scene = loader.parse( json.scene );
 
 			scripts = {
-				init: [],
 				keydown: [],
 				keyup: [],
 				mousedown: [],
@@ -41,16 +40,25 @@ var APP = {
 
 					var script = sources[ i ];
 
-					script.compiled = new Function( 'scene', 'event', script.source ).bind( object );
+					var events = ( new Function( 'scene', script.source ).bind( object ) )();
 
-					scripts[ script.event ].push( script.compiled );
+					for ( var name in events ) {
+
+						if ( scripts[ name ] === undefined ) {
+
+							console.warn( 'APP.Player: event type not supported (', name, ')' );
+							continue;
+
+						}
+
+						scripts[ name ].push( events[ name ] );
+
+					}
 
 				}
 
 			}
 
-			dispatch( scripts.init, {} );
-
 			this.dom = renderer.domElement;
 
 		};
@@ -68,7 +76,7 @@ var APP = {
 
 			for ( var i = 0, l = array.length; i < l; i ++ ) {
 
-				array[ i ]( scene, event );
+				array[ i ]( event );
 
 			}
 
@@ -105,7 +113,7 @@ var APP = {
 			document.removeEventListener( 'mousedown', onDocumentMouseDown );
 			document.removeEventListener( 'mouseup', onDocumentMouseUp );
 			document.removeEventListener( 'mousemove', onDocumentMouseMove );
-			
+
 			cancelAnimationFrame( request );
 
 		};

+ 19 - 8
editor/js/libs/ui.editor.js

@@ -8,10 +8,16 @@ UI.ScriptEditor = function () {
 
 	var scope = this;
 
-	var timeout;
+	var name = new UI.Input().setWidth( '150px' ).setFontSize( '12px' ).onChange( function () {
+
+		if ( scope.onChangeCallback !== undefined ) {
+
+			scope.onChangeCallback();
+
+		}
 
-	var event = new UI.Text( '' );
-	this.add( event );
+	} );
+	this.add( name );
 
 	var remove = new UI.Text( 'x' );
 	remove.setPosition( 'absolute' );
@@ -36,9 +42,11 @@ UI.ScriptEditor = function () {
 
 	this.add( new UI.Break() );
 
+	var timeout;
+
 	var textarea = new UI.TextArea();
 	textarea.setWidth( '100%' );
-	textarea.setHeight( '100px' );
+	textarea.setHeight( '150px' );
 	textarea.setMarginTop( '8px' );
 	textarea.onKeyUp( function () {
 
@@ -51,7 +59,7 @@ UI.ScriptEditor = function () {
 
 			try {
 
-				( new Function( 'scene', 'event', source ).bind( object.clone() ) )( new THREE.Scene(), {} );
+				( new Function( 'scene', source ).bind( object.clone() ) )( new THREE.Scene() );
 
 				textarea.dom.classList.add( 'success' );
 				textarea.dom.classList.remove( 'fail' );
@@ -78,7 +86,7 @@ UI.ScriptEditor = function () {
 	} );
 	this.add( textarea );
 
-	this.event = event;
+	this.name = name;
 	this.textarea = textarea;
 
 };
@@ -88,13 +96,16 @@ UI.ScriptEditor.prototype.constructor = UI.ScriptEditor;
 
 UI.ScriptEditor.prototype.getValue = function () {
 
-	return { event: this.event.getValue(), source: this.textarea.getValue() };
+	return {
+		name: this.name.getValue(),
+		source: this.textarea.getValue()
+	};
 
 };
 
 UI.ScriptEditor.prototype.setValue = function ( value ) {
 
-	this.event.setValue( value.event );
+	this.name.setValue( value.name );
 	this.textarea.setValue( value.source );
 
 	return this;