Skip to main content

problems receiving key and mouse events

7 replies [Last post]
miechus
Offline
Joined: 2007-05-08
Points: 0

Hello,

Last night I created a custom cell (TestCellGLO, TestCellSetup and TestCell classes). Here's how my TestCell class looks like:

public class TestCell extends Cell implements MouseListener, KeyListener {

...

public void setup(CellSetup setupData) {

float scale = 5f;
float HEIGHT = (float)WonderlandUniverseFactory.getUniverse().getViewer().getCanvas3D().getScreen3D().getPhysicalScreenHeight()*scale;
float WIDTH = (float)WonderlandUniverseFactory.getUniverse().getViewer().getCanvas3D().getScreen3D().getPhysicalScreenWidth()*scale;

QuadArray quad;
quad = new QuadArray(4, QuadArray.COORDINATES | QuadArray.NORMALS | QuadArray.COLOR_3 | QuadArray.TEXTURE_COORDINATE_2);
quad.setCoordinates(0, new float[] {
WIDTH/2f, HEIGHT/2f, 0f,
-WIDTH/2f, HEIGHT/2f, 0f,
-WIDTH/2f, -HEIGHT/2f, 0f,
WIDTH/2f, -HEIGHT/2f, 0f,
});

Vector3f n = new Vector3f(0f,0f,1f);
quad.setNormals(0, new Vector3f[] {
n,n,n,n
});

Color3f color = new Color3f(1f,1f,1f);
quad.setColors(0, new Color3f[] {
color, color, color, color
});

quad.setCapability(QuadArray.ALLOW_COLOR_WRITE);

Appearance app = new Appearance();
Shape3D shape = new Shape3D( quad, app );

BranchGroup bg = new BranchGroup();
bg.addChild(shape);

Text2D text = new Text2D("This is my test", color, "Courier New", 36, Font.BOLD);
bg.addChild(text);

cellLocal.addChild(bg);

}

public void mouseEntered(MouseEvent e) {
System.out.println("Mouse entered: " + e.getX() + " - " + e.getY());
}

public void mouseExited(MouseEvent e) {
System.out.println("Mouse exited: " + e.getX() + " - " + e.getY());
}

public void mousePressed(MouseEvent e) {
System.out.println("Mouse pressed: " + e.getX() + " - " + e.getY());
}

public void mouseReleased(MouseEvent e) {
System.out.println("Mouse released: " + e.getX() + " - " + e.getY());
}

public void keyTyped(KeyEvent e) {
System.out.println("Key typed: " + e.getKeyCode());
}

public void keyReleased(KeyEvent e) {
System.out.println("Key released: " + e.getKeyCode());
}

public void keyPressed(KeyEvent e) {
System.out.println("Key pressed: " + e.getKeyCode());
}

}

I modified the root GLO to show the cell in the world and I am able to see the rectangle with the text. The problem is with the mouse and key events, when clicking or trying to type something I do not get the notifications.

1) Is there anything else I have to enable for a cell to react to mouse or key events? What is the proper way to propagate a keyboard event in the Wonderland client to an object/cell?

2) Is there an easier way (than the one above) to create a simple 3D object (that is not an exported model) in the world?

Thanks

Mac

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
miechus
Offline
Joined: 2007-05-08
Points: 0

I found a great tutorial on Java3D here:

http://java.sun.com/developer/onlineTraining/java3d/

I also found the MouseClickedEventAdapter adapter class from lg3d for mouse events not supported by Java3D. I'm not sure how I can use the lg3d classes with the cellLocal object being a Java3D BranchGroup class.

Is there a way to somehow use lg3d with Java3D BranchGroup (or such) objects? They inherit from a different Node object and therefore cannot be used together.

Thanks

Mac

paulby
Offline
Joined: 2003-06-13
Points: 0

Wonderland uses Java3D so you can use any Java3D class.

You best bet is probably to use the 3D event system code, did the example I posted not work for you ?

miechus
Offline
Joined: 2007-05-08
Points: 0

Paul,

Which example are you referring to? I looked at the Tutorial2.java class in the lg3d-demo-apps folder but it uses the lg3d classes to react on events. When I try to add a Component3D object to cellLocal I get an error due to the problem mentioned above - the Component3D object inherits from lg3d ThreadGroup and cellLocal from Java3D ThreadGroup object.

Is there a wonderland-specific tutorial you posted?

Thanks

Mac

paulby
Offline
Joined: 2003-06-13
Points: 0
miechus
Offline
Joined: 2007-05-08
Points: 0

Thanks, I just looked at my initial post and the link was there. Sorry for the confusion. I'll add it to my test cell and will let you know.

deronj
Offline
Joined: 2003-09-02
Points: 0

I didn't see any place in the code you sent us which registers the listeners. The listeners
don't get registered automatically; you must do this explicitly. Check out the code in
appshare/AppWindow.java for an example of how to register the listeners. It's in the
initEventHandling function. Note that in order for you to receive events you will need to
create the branch group specially--specifically you must create an instance of Lg3dBranchGroup
and add this to the parent of the screen graph add your other nodes to the Lg3dBranchGroup.
Then you can use the method in InitEventHandling to register the listeners.

Hopefully this extra bit of funkiness will go away when Paul unwraps the LG stuff from Java3D.

miechus
Offline
Joined: 2007-05-08
Points: 0

Thanks for the info, the tutorial that Paul posted has been of great help. I was able to get the objects to answer the mouse events.