Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package mygame;
- import com.jme3.collision.CollisionResults;
- import com.jme3.export.JmeExporter;
- import com.jme3.export.JmeImporter;
- import com.jme3.input.InputManager;
- import com.jme3.input.KeyInput;
- import com.jme3.input.MouseInput;
- import com.jme3.input.controls.ActionListener;
- import com.jme3.input.controls.AnalogListener;
- import com.jme3.input.controls.KeyTrigger;
- import com.jme3.input.controls.MouseAxisTrigger;
- import com.jme3.input.controls.MouseButtonTrigger;
- import com.jme3.math.Quaternion;
- import com.jme3.math.Ray;
- import com.jme3.math.Vector2f;
- import com.jme3.math.Vector3f;
- import com.jme3.renderer.RenderManager;
- import com.jme3.renderer.ViewPort;
- import com.jme3.scene.Geometry;
- import com.jme3.scene.Node;
- import com.jme3.scene.Spatial;
- import com.jme3.scene.control.Control;
- import java.io.IOException;
- import static mygame.Main.orbitSelection;
- import static mygame.Main.orbit;
- import static mygame.Main.modelNode;
- import static mygame.OrbitCamera.enabled;
- import static mygame.OrbitCamera.canRotate;
- import static mygame.OrbitCamera.rotateCamera;
- import static mygame.OrbitCamera.vRotateCamera;
- import static mygame.OrbitCamera.zoomCamera;
- public class Controls implements ActionListener, AnalogListener, Control
- {
- private InputManager inputManager;
- private Node rootNode, guiNode;
- Controls(InputManager inputManager, Node rootNode, Node guiNode)
- {
- this.inputManager = inputManager;
- this.rootNode = rootNode;
- this.guiNode = guiNode;
- }
- public void setupKeys()
- {
- //mouse control right (continuous running of command until button is released)
- inputManager.addMapping("select", new MouseButtonTrigger(MouseInput.BUTTON_RIGHT));
- //orbit controls
- inputManager.addMapping("toggleRotate", new MouseButtonTrigger(0));
- inputManager.addListener(this, "toggleRotate");
- //movement controls
- inputManager.addMapping("Down", new MouseAxisTrigger(1, true));
- inputManager.addListener(this, "Down");
- inputManager.addMapping("Up", new MouseAxisTrigger(1, false));
- inputManager.addListener(this, "Up");
- inputManager.addMapping("mouseLeft", new MouseAxisTrigger(0, true));
- inputManager.addListener(this, "mouseLeft");
- inputManager.addMapping("mouseRight", new MouseAxisTrigger(0, false));
- inputManager.addListener(this, "mouseRight");
- //mouse scroll wheel controls
- inputManager.addMapping("ZoomIn", new MouseAxisTrigger(2, true));
- inputManager.addListener(this, "ZoomIn");
- inputManager.addMapping("ZoomOut", new MouseAxisTrigger(2, false));
- inputManager.addListener(this, "ZoomOut");
- //mouse controls (single click register)
- inputManager.addMapping("orbitPoint", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
- inputManager.addListener(this, "orbitPoint");
- inputManager.addMapping("move", new MouseButtonTrigger(MouseInput.BUTTON_RIGHT));
- inputManager.addListener(this, "move");
- //keyboard controls
- inputManager.addMapping("Orbit", new KeyTrigger(KeyInput.KEY_SPACE));
- inputManager.addListener(this, "Orbit");
- inputManager.addMapping("OrbitSelection", new KeyTrigger(KeyInput.KEY_LCONTROL));
- inputManager.addListener(this, "OrbitSelection");
- inputManager.addMapping("defaultView", new KeyTrigger(KeyInput.KEY_NUMPAD3));
- inputManager.addMapping("defaultView", new KeyTrigger(KeyInput.KEY_NUMPAD1));
- inputManager.addListener(this, "defaultView");
- inputManager.addMapping("top", new KeyTrigger(KeyInput.KEY_NUMPAD5));
- inputManager.addListener(this, "top");
- inputManager.addMapping("bottom", new KeyTrigger(KeyInput.KEY_NUMPAD0));
- inputManager.addListener(this, "bottom");
- inputManager.addMapping("left", new KeyTrigger(KeyInput.KEY_NUMPAD4));
- inputManager.addListener(this, "left");
- inputManager.addMapping("right", new KeyTrigger(KeyInput.KEY_NUMPAD6));
- inputManager.addListener(this, "right");
- inputManager.addMapping("front", new KeyTrigger(KeyInput.KEY_NUMPAD2));
- inputManager.addListener(this, "front");
- inputManager.addMapping("back", new KeyTrigger(KeyInput.KEY_NUMPAD8));
- inputManager.addListener(this, "back");
- inputManager.addMapping("createPlane", new KeyTrigger(KeyInput.KEY_ADD));
- inputManager.addListener(this, "createPlane");
- }
- public void onAction(String binding, boolean isPressed, float tpf)
- {
- if (binding.equals("toggleRotate") && enabled)
- {
- if (isPressed)
- {
- canRotate = true;
- inputManager.setCursorVisible(false);
- } else
- {
- canRotate = false;
- inputManager.setCursorVisible(true);
- }
- }
- //left mouse button pressed to orbit around a selected point
- if (binding.equals("move"))
- {
- if (isPressed)
- {
- // Reset results list.
- CollisionResults results = new CollisionResults();
- // Convert screen click to 3d position
- Vector2f click2d = inputManager.getCursorPosition();
- Vector3f click3d = OrbitCamera.cam.getWorldCoordinates(
- new Vector2f(click2d.getX(), click2d.getY()), 0f);
- Vector3f dir = OrbitCamera.cam.getWorldCoordinates(
- new Vector2f(click2d.getX(), click2d.getY()), 1f).subtractLocal(click3d);
- // Aim the ray from the clicked spot forwards.
- Ray ray = new Ray(click3d, dir);
- System.out.println("SCREEN: " + OrbitCamera.cam.getScreenCoordinates(dir));
- // Collect intersections between ray and all nodes in results list.
- rootNode.collideWith(ray, results);
- // (Print the results so we see what is going on:)
- Vector3f pt = null;
- for (int i = 0; i < results.size(); i++)
- {
- // (For each “hit”, we know distance, impact point, geometry.)
- float dist = results.getCollision(i).getDistance();
- pt = results.getCollision(i).getContactPoint();
- String target = results.getCollision(i).getGeometry().getName();
- System.out.println("Selection #" + i + ": " + target + " at " + pt + ", " + dist + " WU away.");
- }
- // Vector3f ori = new Vector3f(inputManager.getCursorPosition().x, inputManager.getCursorPosition().y , -1f);
- //
- // Vector3f dest = new Vector3f(0f,0f,1f);
- //
- // Ray ray = new Ray(ori, dest);
- //
- //
- //
- // CollisionResults results = new CollisionResults();
- //
- // guiNode.collideWith(ray, results);
- // System.out.println(ray + " " + guiNode.collideWith(ray, results) + " " + results.getClosestCollision() + guiNode.getChildren());
- // Use the results -- we rotate the selected geometry.
- if (results.size() > 0)
- {
- // The closest result is the target that the player picked:
- Geometry target = null;
- target = results.getClosestCollision().getGeometry();
- System.out.println(target.getName());
- if (target.getName().equals("moveX+"))
- {
- Main.sceneNode.move(Main.sceneNode.getLocalTranslation().add(0.1f, 0, 0));
- }
- if (target.getName().equals("moveY+"))
- {
- Main.sceneNode.move(Main.sceneNode.getLocalTranslation().add(0, 0.1f, 0));
- }
- if (target.getName().equals("moveZ+"))
- {
- Main.sceneNode.move(Main.sceneNode.getLocalTranslation().add(0, 0, 0.1f));
- }
- if (target.getName().equals("moveX-"))
- {
- Main.sceneNode.move(Main.sceneNode.getLocalTranslation().add(-0.1f, 0, 0));
- }
- if (target.getName().equals("moveY-"))
- {
- Main.sceneNode.move(Main.sceneNode.getLocalTranslation().add(0, -0.1f, 0));
- }
- if (target.getName().equals("moveZ-"))
- {
- Main.sceneNode.move(Main.sceneNode.getLocalTranslation().add(0, 0, -0.1f));
- }
- //code here for what to do with selection
- } else
- {
- }
- }
- }
- //space key use to enable and disable rotation
- if (binding.equals("Orbit"))
- {
- if (isPressed)
- {
- if (OrbitCamera.enabled == false)
- {
- System.out.println("ALLOW ORBIT");
- OrbitCamera.enabled = true;
- } else
- {
- System.out.println("DISALLOW ORBIT");
- OrbitCamera.enabled = false;
- }
- }
- }
- if (binding.equals("createPlane"))
- {
- if (isPressed)
- {
- // orbit.updateFocus(new Vector3f(12.9f,5.65f,12.7f));
- Canvas.createPlane(modelNode);
- System.out.println("CREATING NEW PLANE");
- }
- }
- if (binding.equals("defaultView"))
- {
- if (isPressed)
- {
- orbit.moveCamera(0.39324224f, -5.1840777f);
- System.out.println("TOP VIEW");
- }
- }
- if (binding.equals("top"))
- {
- if (isPressed)
- {
- orbit.moveCamera(1.57f, -6.2807655f);
- System.out.println("TOP VIEW");
- }
- }
- if (binding.equals("bottom"))
- {
- if (isPressed)
- {
- orbit.moveCamera(-1.57f, -6.2807655f);
- System.out.println("BOTTOM VIEW");
- }
- }
- if (binding.equals("front"))
- {
- if (isPressed)
- {
- orbit.moveCamera(0f, -0.0014692545f);
- System.out.println("FRONT VIEW");
- }
- }
- if (binding.equals("back"))
- {
- if (isPressed)
- {
- orbit.moveCamera(0f, -3.1411176f);
- System.out.println("BACK VIEW");
- }
- }
- if (binding.equals("left"))
- {
- if (isPressed)
- {
- orbit.moveCamera(0f, 1.5707964f);
- System.out.println("LEFT VIEW");
- }
- }
- if (binding.equals("right"))
- {
- if (isPressed)
- {
- orbit.moveCamera(0f, -1.5707964f);
- System.out.println("RIGHT VIEW");
- }
- }
- if (binding.equals("OrbitSelection"))
- {
- if (isPressed)
- {
- orbitSelection = true;
- System.out.println("orbit selection");
- }
- }
- }
- public void onAnalog(String name, float value, float tpf)
- {
- //continuous controls for camera orbit
- if (name.equals("mouseLeft"))
- {
- rotateCamera(-value);
- //SceneNavigation.rotateHorizontal(-value);
- }
- if (name.equals("mouseRight"))
- {
- rotateCamera(value);
- // SceneNavigation.rotateHorizontal(value);
- }
- if (name.equals("Down"))
- {
- vRotateCamera(value);
- // SceneNavigation.rotateVertical(value);
- }
- if (name.equals("Up"))
- {
- vRotateCamera(-value);
- // SceneNavigation.rotateVertical(-value);
- }
- if (name.equals("ZoomIn"))
- {
- zoomCamera(value);
- }
- if (name.equals("ZoomOut"))
- {
- zoomCamera(-value);
- }
- }
- public Control cloneForSpatial(Spatial spatial)
- {
- return null;
- }
- public void setSpatial(Spatial spatial)
- {
- }
- public void update(float tpf)
- {
- }
- public void render(RenderManager rm, ViewPort vp)
- {
- }
- public void write(JmeExporter ex) throws IOException
- {
- }
- public void read(JmeImporter im) throws IOException
- {
- // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement