@author normenhansen
*/
public class AA_GUIDemo_Inventory extends SimpleApplication implements RawInputListener, ActionListener {
VideoRecorderAppState vrAppState;
private Screen screen;
private int winCount = 0;
private float iconSize = 40;
Vector2f dim = new Vector2f();
Vector4f windowPadding = new Vector4f();
float dragBarHeight;
private Node obj1Node, obj2Node, obj3Node;
Vector2f click2d = new Vector2f(), tempV2 = new Vector2f();
Vector3f click3d = new Vector3f(), pickDir = new Vector3f(), tempV3 = new Vector3f();
Ray pickRay = new Ray();
CollisionResults rayResults = new CollisionResults();
CollisionResult closest;
public static void main(String[] args) {
AA_GUIDemo_Inventory app = new AA_GUIDemo_Inventory();
app.start();
}
@Override
public void simpleInitApp() {
vrAppState = new VideoRecorderAppState();
vrAppState.setQuality(0.35f);
setupKeys();
setupLights();
setupCamera();
createGUIScreen();
layoutGUI();
addSceneObjects();
flyCam.setMoveSpeed(30);
flyCam.setDragToRotate(true);
inputManager.deleteMapping("FLYCAM_RotateDrag");
inputManager.addMapping("FLYCAM_RotateDrag", new MouseButtonTrigger(MouseInput.BUTTON_RIGHT));
inputManager.setCursorVisible(true);
}
private void setupKeys() {
inputManager.addRawInputListener(this);
inputManager.addMapping(“F9”, new KeyTrigger(KeyInput.KEY_F9));
inputManager.addListener(this, “F9”);
}
private void setupLights() {
AmbientLight al = new AmbientLight();
al.setColor(new ColorRGBA(1f, 1f, 1f, 1f));
rootNode.addLight(al);
DirectionalLight sun = new DirectionalLight();
sun.setDirection(new Vector3f(0f,-.25f,0f).normalizeLocal());
sun.setColor(new ColorRGBA(1f,1f,1f,1f));
rootNode.addLight(sun);
}
private void setupCamera() { }
private void addSceneObjects() {
Box obj1 = new Box(1,1,1);
Material mat1 = new Material(assetManager, “Common/MatDefs/Light/Lighting.j3md”);
mat1.setColor(“Diffuse”, ColorRGBA.Blue);
mat1.setColor(“Ambient”, new ColorRGBA(0,0,0.25f,1));
mat1.setBoolean(“UseMaterialColors”, true);
Geometry obj1Geom = new Geometry();
obj1Geom.setMesh(obj1);
obj1Node = new Node();
obj1Node.attachChild(obj1Geom);
obj1Node.setMaterial(mat1);
obj1Node.setLocalTranslation(-3,0,0);
rootNode.attachChild(obj1Node);
Sphere obj2 = new Sphere(8,8,1f);
Material mat2 = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
mat2.setColor("Diffuse", ColorRGBA.Red);
mat2.setColor("Ambient", new ColorRGBA(0.25f,0,0,1));
mat2.setBoolean("UseMaterialColors", true);
Geometry obj2Geom = new Geometry();
obj2Geom.setMesh(obj2);
obj2Node = new Node();
obj2Node.attachChild(obj2Geom);
obj2Node.setMaterial(mat2);
obj2Node.setLocalTranslation(0,0,0);
rootNode.attachChild(obj2Node);
Torus obj3 = new Torus(8,8,0.5f,1f);
Material mat3 = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
mat3.setColor("Diffuse", ColorRGBA.Green);
mat3.setColor("Ambient", new ColorRGBA(0,0.25f,0,1));
mat3.setBoolean("UseMaterialColors", true);
Geometry obj3Geom = new Geometry();
obj3Geom.setMesh(obj3);
obj3Node = new Node();
obj3Node.attachChild(obj3Geom);
obj3Node.setMaterial(mat3);
obj3Node.setLocalTranslation(3,0,0);
rootNode.attachChild(obj3Node);
}
private void createGUIScreen() {
screen = new Screen(this);
screen.setUseUIAudio(true);
screen.setUIAudioVolume(1f);
guiNode.addControl(screen);
}
private void layoutGUI() {
windowPadding.set(screen.getStyle(“Window#Dragbar”).getVector4f(“indents”));
dragBarHeight = screen.getStyle(“Window#Dragbar”).getFloat(“defaultControlSize”);
Element innerContent = new Element(
screen,
UIDUtil.getUID(),
new Vector2f(
windowPadding.x,
(windowPadding.y*2)+dragBarHeight
),
Vector2f.ZERO,
Vector4f.ZERO,
null
);
innerContent.setAsContainerOnly();
for (int i = 0; i < 10; i++) {
float x = i*iconSize;
x += 5;
Element e = createInventorySlot(i, x, 0);
innerContent.addChild(e);
}
innerContent.sizeToContent();
dim.set(
innerContent.getWidth()+(windowPadding.x*2),
innerContent.getHeight()+(windowPadding.y*3)+dragBarHeight
);
Window win = new Window(screen, Vector2f.ZERO, dim);
win.addChild(innerContent);
win.setlockToParentBounds(true);
win.setIsResizable(false);
screen.addElement(win);
}
private Element createInventorySlot(int index, float x, float y) {
Element slot = new Element(
screen,
“InvSlot” + index,
new Vector2f(x,y),
new Vector2f(iconSize,iconSize),
screen.getStyle(“CheckBox”).getVector4f(“resizeBorders”),
screen.getStyle(“CheckBox”).getString(“defaultImg”)
);
slot.setIsDragDropDropElement(true);
return slot;
}
private DragElement createNewDragElement() {
DragElement e = new DragElement(
screen,
new Vector2f(
screen.getMouseXY().x-(iconSize/2),
screen.getHeight()-(screen.getMouseXY().y+(iconSize/2))
),
new Vector2f(iconSize,iconSize),
Vector4f.ZERO,
"tonegod/gui/style/def/Common/Particles/spark.png"
) {
@Override
public void onDragStart(MouseButtonEvent evt) {
}
@Override
public boolean onDragEnd(MouseButtonEvent evt, Element dropElement) {
if (dropElement != null) {
setlockToParentBounds(false);
return true;
} else {
Node n = getUserData("worldObject");
rootNode.attachChild(n);
screen.removeElement(this);
return false;
}
}
};
e.setlockToParentBounds(true);
e.setUseLockToDropElementCenter(true);
e.setUseSpringBack(true);
screen.addElement(e);
return e;
}
@Override
public void simpleUpdate(float tpf) {
}
@Override
public void simpleRender(RenderManager rm) {
//TODO: add render code
}
public void onAction(String binding, boolean value, float tpf) {
if (binding.equals(“F9”)) {
if (!value) {
if (stateManager.hasState(vrAppState)) {
System.out.println(“Stopping video recorder”);
stateManager.detach(vrAppState);
} else {
System.out.println(“Starting video recorder”);
stateManager.attach(vrAppState);
}
}
}
}
public void beginInput() { }
public void endInput() { }
public void onJoyAxisEvent(JoyAxisEvent evt) { }
public void onJoyButtonEvent(JoyButtonEvent evt) { }
public void onMouseMotionEvent(MouseMotionEvent evt) { }
public void onMouseButtonEvent(MouseButtonEvent evt) {
if (evt.getButtonIndex() == 0 && evt.isPressed()) {
click2d.set(inputManager.getCursorPosition());
tempV2.set(click2d);
click3d.set(cam.getWorldCoordinates(tempV2, 0f));
pickDir.set(cam.getWorldCoordinates(tempV2, 1f).subtractLocal(click3d).normalizeLocal());
pickRay.setOrigin(click3d);
pickRay.setDirection(pickDir);
rayResults.clear();
// Check for targeting collision
rootNode.collideWith(pickRay, rayResults);
closest = rayResults.getClosestCollision();
if (closest != null) {
Geometry geom = closest.getGeometry();
Node parent = geom.getParent();
DragElement de = createNewDragElement();
de.setUserData("worldObject", parent);
parent.removeFromParent();
// inputManager.setCursorVisible(true);
}
}
}
public void onKeyEvent(KeyInputEvent evt) { }
public void onTouchEvent(TouchEvent evt) { }
}
[/java]