Hello everyone. I'm new to JMonkey.
So, currently i'm studying HelloMousePick example.
In example everything works fine, but after copy&paste it brakes into dust.
Here's the code:
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import org.apache.log4j.Logger;
import org.interspace.datamodel.generator.DefaultGalaxyGenerator;
import org.interspace.datamodel.stellar.Galaxy;
import org.interspace.datamodel.stellar.StarSystem;
import org.interspace.gui.shape.OrtedShape;
import org.interspace.gui.shape.StarConnection;
import org.interspace.gui.shape.OrtedShape.OrtType;
import com.jme.app.AbstractGame;
import com.jme.app.BaseSimpleGame;
import com.jme.bounding.BoundingBox;
import com.jme.image.Texture;
import com.jme.input.AbsoluteMouse;
import com.jme.input.FirstPersonHandler;
import com.jme.input.KeyBindingManager;
import com.jme.input.KeyInput;
import com.jme.input.MouseInput;
import com.jme.intersection.BoundingPickResults;
import com.jme.intersection.PickResults;
import com.jme.intersection.TrianglePickResults;
import com.jme.math.Ray;
import com.jme.math.Vector2f;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.renderer.Renderer;
import com.jme.scene.Line;
import com.jme.scene.Node;
import com.jme.scene.Spatial.LightCombineMode;
import com.jme.scene.shape.Box;
import com.jme.scene.shape.Sphere;
import com.jme.util.geom.Debugger;
public class GuiContext extends AbstractGuiContext {
private static Logger logger = Logger.getLogger(GuiContext.class);
private static GuiContext instance;
private boolean drawOrts;
private boolean drawGrid;
private AbsoluteMouse am;
PickResults pr;
public static GuiContext getInstance() {
if (instance == null) {
instance = new GuiContext();
}
return GuiContext.instance;
}
/**
* Called every frame to update scene information.
*
* @param interpolation
* unused in this implementation
* @see BaseSimpleGame#update(float interpolation)
*/
protected final void update(float interpolation) {
super.update(interpolation);
if ( !pause ) {
/** Call simpleUpdate in any derived classes of SimpleGame. */
simpleUpdate();
/** Update controllers/render states/transforms/bounds for rootNode. */
rootNode.updateGeometricState(tpf, true);
statNode.updateGeometricState(tpf, true);
}
}
/**
* This is called every frame in BaseGame.start(), after update()
*
* @param interpolation
* unused in this implementation
* @see AbstractGame#render(float interpolation)
*/
protected final void render(float interpolation) {
super.render(interpolation);
Renderer r = display.getRenderer();
/** Draw the rootNode and all its children. */
r.draw(rootNode);
/** Call simpleRender() in any derived classes. */
simpleRender();
/** Draw the stats node to show our stat charts. */
r.draw(statNode);
doDebug(r);
}
@Override
protected void doDebug(Renderer r) {
super.doDebug(r);
if (showDepth) {
r.renderQueue();
Debugger.drawBuffer(Texture.RenderToTextureType.Depth, Debugger.NORTHEAST, r);
}
}
@Override
protected void simpleInitGame() {
// Create a new mouse. Restrict its movements to the display screen.
am = new AbsoluteMouse("The Mouse", display.getWidth(), display
.getHeight());
// Get the mouse input device and assign it to the AbsoluteMouse
// Move the mouse to the middle of the screen to start with
am.setLocalTranslation(new Vector3f(display.getWidth() / 2, display
.getHeight() / 2, 0));
// Assign the mouse to an input handler
am.registerWithInputHandler(input);
// Create the box in the middle. Give it a bounds
rootNode.attachChild(am);
// Remove all the lightstates so we can see the per-vertex colors
//lightState.detachAll();
pr = new BoundingPickResults();
MouseInput.get().setCursorVisible(true);
((FirstPersonHandler) input).getMouseLookHandler().setEnabled(false);
addXZGrid();
DefaultGalaxyGenerator generator = new DefaultGalaxyGenerator(20, 20, 20);
Galaxy galaxy = generator.generate();
addStarSystemsToScene(galaxy);
}
private void addStarSystemsToScene(Galaxy galaxy) {
Node galaxyNode = new Node("GalaxyNode");
Iterator<StarSystem> it = galaxy.getSystems().iterator();
MouseInput.get().setCursorVisible(true);
while(it.hasNext()) {
Sphere sphere = new Sphere("sphere", 30, 30, 0.25f);
StarSystem system = it.next();
sphere.updateGeometry(new Vector3f(system.getX(), system.getY(), system.getZ()), sphere.getZSamples(), sphere.getRadialSamples(), sphere.getRadius());
sphere.setLightCombineMode(LightCombineMode.Off);
sphere.updateModelBound();
// OrtedShape shape = new OrtedShape(sphere, sphere.getCenter(), OrtType.XZ_ORT);
// shape.updateModelBound();
// shape.setLightCombineMode(LightCombineMode.Off);
// galaxyNode.attachChild(shape);
galaxyNode.attachChild(sphere);
}
rootNode.attachChild(galaxyNode);
}
private void addXZGrid() {
ColorRGBA[] color = new ColorRGBA[2];
color[0] = new ColorRGBA(1f, 1f, 0, 1f);
color[1] = new ColorRGBA(1f, 1f, 0, 1f);
for (int i = 0; i < 101; i++) {
Vector3f[] v1 = new Vector3f[2];
v1[0] = new Vector3f(i, 0, 0);
v1[1] = new Vector3f(i, 0, 100);
Line l1 = new Line("Line1", v1, null, color, null);
rootNode.attachChild(l1);
}
for (int i = 0; i < 101; i++) {
Vector3f[] v1 = new Vector3f[2];
v1[0] = new Vector3f(0, 0, i);
v1[1] = new Vector3f(100, 0, i);
Line l1 = new Line("Line1", v1, null, color, null);
rootNode.attachChild(l1);
}
}
@Override
public void registerKeyBindings() {
super.registerKeyBindings();
//Toggle orts
KeyBindingManager.getKeyBindingManager().set( KeyBinderCommandConstants.TOGGLE_ORTS,
KeyInput.KEY_O );
//Draw grid
KeyBindingManager.getKeyBindingManager().set( KeyBinderCommandConstants.TOGGLE_GRID,
KeyInput.KEY_O );
}
@Override
public void checkKeyBindings() {
super.checkKeyBindings();
/** If toggle_orts is a valid command (via key o), shows orts. */
if ( KeyBindingManager.getKeyBindingManager().isValidCommand(
KeyBinderCommandConstants.TOGGLE_GRID, false ) ) {
drawGrid = !drawGrid;
}
if ( KeyBindingManager.getKeyBindingManager().isValidCommand(
KeyBinderCommandConstants.TOGGLE_ORTS, false ) ) {
drawOrts = !drawOrts;
}
if (MouseInput.get().isButtonDown(0)) {
pickObject();
}
//releases mouse
//((FirstPersonHandler) input).getMouseLookHandler().setEnabled(false);
}
private void pickObject() {
Vector2f screenPos = new Vector2f();
// Get the position that the mouse is pointing to
screenPos.set(am.getHotSpotPosition().x, am.getHotSpotPosition().y);
// Get the world location of that X,Y value
Vector3f worldCoords = display.getWorldCoordinates(screenPos, 0);
Vector3f worldCoords2 = display.getWorldCoordinates(screenPos, 1);
// Create a ray starting from the camera, and going in the direction
// of the mouse's location
Ray mouseRay = new Ray(worldCoords, worldCoords2.subtractLocal(worldCoords).normalizeLocal());
pr.clear();
rootNode.findPick(mouseRay, pr);
for (int i = 0; i < pr.getNumber(); i++) {
pr.getPickData(i).getTargetMesh().setRandomColors();
logger.debug(pr.getPickData(i).getTargetMesh().getName());
}
}
public void setDrawOrts(boolean drawOrts) {
this.drawOrts = drawOrts;
}
public boolean isDrawOrts() {
return drawOrts;
}
}
AbstractGuiContext is copy&paste BaseSimpleGame
What I did wrong?