Saturday, January 29, 2011

Blog post week #1 - P2PU OWL class

Finished the reading for week one and built the shape cell. I sorta cheated. I worked with the shape cell tutorial back when it was first written. I have included below a short 'tutorial' that I wrote a while back about the shape cell and adding a scripting command interface. Thought it might be interesting for anyone interested in scripting. I can't guarantee that everything matches up exactly with the current shape cell because there have been some minor changes. I have it in pdf format also.


Shape Cell Scripting Mod

This is an addition to Jordan's shape cell tutorial that enables scripting control of the shape change. The changes in this mini-tutorial could be applied to any type of cell in order to easily add automation. The changes are in only the cell.java and the cellMO.java code.
Functionally what is changed is that a scripting component is attached and a command stack mechanism is added that will allow the component to call the commands in the command stack by an arbitrary command name. The command stack for the shape cell only implements one command but the process of adding other commands is exactly the same. Then you will create a script that calls 

MyClass.executeAction(“shapeChange”);


Shape Cell

Add the following imports:

import org.jdesktop.wonderland.modules.scriptingComponent.client.ScriptingActionClass; 
import org.jdesktop.wonderland.modules.scriptingComponent.client.ScriptingComponent; 
import org.jdesktop.wonderland.modules.scriptingComponent.client.ScriptingRunnable;

Add the following two lines lines in bold and italics near the top of the code in the location as below:

@UsesCellComponent private ContextMenuComponent contextComp = null; 
private ContextMenuFactorySPI menuFactory = null; 

@UsesCellComponent 
private ScriptingComponent scriptingComponent;

public ShapeCell(CellID cellID, CellCache cellCache) { 

Add the two following methods with the other methods in the file:

public void shapeChangeShape() 
{
shapeType = (shapeType.equals("BOX") == true) ? "SPHERE" : "BOX"; 
renderer.updateShape();

ShapeCellChangeMessage msg = new ShapeCellChangeMessage(getCellID(), shapeType); 
sendCellMessage(msg); 
}

ScriptingRunnable shapeChangeShapeRun = new ScriptingRunnable() 
{
@Override 
public void run()
shapeChangeShape(); 
System.out.println("ScriptingActionClass - enter shapeChange"); 
}
};


Add the following four lines in bold and italics in the setStatus method in the spot as below:

contextComp.addContextMenuFactory(menuFactory);

ScriptingActionClass sac = new ScriptingActionClass(); 
sac.setName("Shape"); 
sac.insertCmdMap("shapeChange", shapeChangeShapeRun); 
scriptingComponent.putActionObject(sac);

Shape Cell MO

Add the following import:

import org.jdesktop.wonderland.modules.scriptingComponent.server.ScriptingComponentMO;

Add the following line in the constructor as below. public ShapeCellMO() 
{
addComponent(new ScriptingComponentMO(this), ScriptingComponentMO.class);
}

Scripting based cell creation

The scripting component allows for creation of cells under scripting control. Basically all that is required is to put into a script a command like:

MyClass.createCellInstance("org.jdesktop.wonderland.modules.shape.common.S hapeCellServerState", 3.0, 0.0, 3.0, "shape");

The parameters are the cell server state class name, x, y, z and the cell name to be used for this cell instance.

In order for this to work best I changed one line in the ShapeCellServerState.java to be like this:

private String textureURI = "wla://shape-tutorial-module/MountainPicture.png";

This change puts a default value for the textureURI. Creating a cell this way seems to require that all values be defaulted in the server state class.

2 comments:

  1. Hey Morris, this looks very interesting.

    Am I correct in thinking that it would have the same effect as dragging and object in-world and adding the scripting component manually?

    In that case sounds cool to create scripting enabled components, with the scripts already in them, to add functionality that would be more difficult to write in pure Java, but without having to do it all in-world.

    ReplyDelete
  2. While the shape cell with the command stack as in this tutorial has the scripting component already attached, the idea here is that adding the command capability to a cell allows the cell (shape cell) respond to script commands. All of the cell types that I have been working with, eg, shape, npc, scriptingPoster, scriptingImager, bugX, clockX, etc, all have the scripting component already attached. I always insert them in world with insert->object from the client menus. I believe there is a capability in core to allow every inserted module to have the same component attached. That may do what you are talking about.

    ReplyDelete