Jump to Main Content
Jump to Main Content

BlinkScript Mouse Handler Code Example



Description


This code example shows how to capture mouse events. In particular it will display information about the sub mesh that you clicked on. Left click on the different areas of the Ogre head, tusks, ear ring, eyes and green skin. All the code and assets are in the projects Scripts directory. You can download the project using the link above.

PostWorld.js


// Turn on debug so that Chat.printDebug() and Chat.dump() work
Chat.debugOutput = true;

// Display the stats panel in the bottom left corner of screen
Application.statsVisible = true;

// Turn on collisions, but allow the user to turn it off
Actor.setCollisionSetting( true, false );

// Turn on gravity, but allow the user to turn it off
Actor.setGravitySetting( true, false );

// Hide the avatar
Actor.showAvatar  = false;

// Show the chat panel
Application.chatPaneVisible = true;

// Set ambient light for the scene
Scene.ambientLightColor = Color(0.5, 0.5, 0.5, 0);

// Create a point light over the oger head to give meanacing look
var oPointLight = Scene.createPointLight( "SceneLight" );
oPointLight.position = Vector( 0, 600, -500 );

// Create the floor
var oGround = Scene.createGround( "MainFloor", "RustySteel", 3000.0, 3000.0, true );

// Create a Skydome
oSkyDome = Scene.createSkyDome();
oSkyDome.materialName = "CloudySky";
oSkyDome.curvature = 5;
oSkyDome.tiling = 8;
oSkyDome.distance = 4000;
oSkyDome.enable = true;

// Add the Ogre head
oHeadEntity = Scene.createEntity( "OgreHead", "ogrehead.mesh" );
oHeadNode = Scene.createSceneNode( "OgreHeadNode" );
oHeadNode.attachObject( oHeadEntity );
oHeadNode.position = Vector( 0, 175, -500 );

// Create the mouse event listener
this.oMouseListener = new Object();
this.oMouseListener.onClick = function( dMouseRelX, mouseRelY, dMouseRelZ, oObject, iSubMesh )
{
	 // If the user clicked on the ogre head, say where they clicked when they release the mouse button
	 if ( oObject != null && oObject.name == "OgreHead" && !Application.mouseButtonDown )
		  Chat.print( "onClick(). You clicked on: ", oObject.name, " Sub Mesh: ", iSubMesh );
}

// These are commented out as the create a lot of output.
// Feel free to uncomment them and try it
/*
this.oMouseListener.onOver = function( dMouseRelX, dMouseRelY, dMouseRelZ, oObject, iSubMesh )
{
	if ( oObject != null  )
		Chat.print( "oMouseListener Called onOver(). Mouse is over: ", oObject.name, " Sub Mesh: ", iSubMesh );
}

this.oMouseListener.onOut = function( dMouseRelX, dMouseRelY, dMouseRelZ )
{
	Chat.print( "oMouseListener Called onOut" );
}

this.oMouseListener.onEvent = function( dMouseRelX, dMouseRelY, dMouseRelZ, oObject, iSubMesh )
{
	if ( oObject != null  )
		Chat.print( "oMouseListener Called onEvent(). Mouse is over: ", oObject.name, " Sub Mesh: ", iSubMesh );

	if ( dMouseRelZ != 0 )
		Chat.print( "oMouseListener Called onEvent Relative dMouseRelZ: "+dMouseRelZ );
}

this.oMouseListener.onMove = function( dMouseRelX, dMouseRelY, dMouseRelZ, oObject, iSubMesh )
{
	if ( oObject != null  )
		Chat.print( "oMouseListener Called onMove(). Mouse is over: ", oObject.name, " Sub Mesh: ", iSubMesh );

	Chat.print( "oMouseListener Called onMove Relative: "+dMouseRelX+","+dMouseRelY+", Absolute: "+vuView.mouseX+","+vuView.mouseY );
}
*/

// Add the mouse listener
Mouse.addListener( this.oMouseListener, oHeadEntity );

Chat.print( "Click on the different parts of the Ogre head, eyes, tusks, etc." );


Related BlinkScript Examples
?????? ?????? ?????? ??????