Advertisement
Guest User

Untitled

a guest
Mar 29th, 2015
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.08 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package mygame;
  6.  
  7. import com.jme3.collision.CollisionResults;
  8. import com.jme3.export.JmeExporter;
  9. import com.jme3.export.JmeImporter;
  10. import com.jme3.input.InputManager;
  11. import com.jme3.input.KeyInput;
  12. import com.jme3.input.MouseInput;
  13. import com.jme3.input.controls.ActionListener;
  14. import com.jme3.input.controls.AnalogListener;
  15. import com.jme3.input.controls.KeyTrigger;
  16. import com.jme3.input.controls.MouseAxisTrigger;
  17. import com.jme3.input.controls.MouseButtonTrigger;
  18. import com.jme3.math.Quaternion;
  19. import com.jme3.math.Ray;
  20. import com.jme3.math.Vector2f;
  21. import com.jme3.math.Vector3f;
  22. import com.jme3.renderer.RenderManager;
  23. import com.jme3.renderer.ViewPort;
  24. import com.jme3.scene.Geometry;
  25. import com.jme3.scene.Node;
  26. import com.jme3.scene.Spatial;
  27. import com.jme3.scene.control.Control;
  28. import java.io.IOException;
  29. import static mygame.Main.orbitSelection;
  30. import static mygame.Main.orbit;
  31. import static mygame.Main.modelNode;
  32. import static mygame.OrbitCamera.enabled;
  33. import static mygame.OrbitCamera.canRotate;
  34. import static mygame.OrbitCamera.rotateCamera;
  35. import static mygame.OrbitCamera.vRotateCamera;
  36. import static mygame.OrbitCamera.zoomCamera;
  37.  
  38.  
  39. public class Controls implements ActionListener, AnalogListener, Control
  40. {
  41.  
  42.     private InputManager inputManager;
  43.     private Node rootNode, guiNode;
  44.  
  45.     Controls(InputManager inputManager, Node rootNode, Node guiNode)
  46.     {
  47.         this.inputManager = inputManager;
  48.         this.rootNode = rootNode;
  49.         this.guiNode = guiNode;
  50.     }
  51.  
  52.     public void setupKeys()
  53.     {
  54.  
  55.         //mouse control right (continuous running of command until button is released)
  56.         inputManager.addMapping("select", new MouseButtonTrigger(MouseInput.BUTTON_RIGHT));
  57.  
  58.         //orbit  controls
  59.         inputManager.addMapping("toggleRotate", new MouseButtonTrigger(0));
  60.         inputManager.addListener(this, "toggleRotate");
  61.  
  62.         //movement controls
  63.         inputManager.addMapping("Down", new MouseAxisTrigger(1, true));
  64.         inputManager.addListener(this, "Down");
  65.         inputManager.addMapping("Up", new MouseAxisTrigger(1, false));
  66.         inputManager.addListener(this, "Up");
  67.         inputManager.addMapping("mouseLeft", new MouseAxisTrigger(0, true));
  68.         inputManager.addListener(this, "mouseLeft");
  69.         inputManager.addMapping("mouseRight", new MouseAxisTrigger(0, false));
  70.         inputManager.addListener(this, "mouseRight");
  71.         //mouse scroll wheel controls
  72.         inputManager.addMapping("ZoomIn", new MouseAxisTrigger(2, true));
  73.         inputManager.addListener(this, "ZoomIn");
  74.         inputManager.addMapping("ZoomOut", new MouseAxisTrigger(2, false));
  75.         inputManager.addListener(this, "ZoomOut");
  76.  
  77.  
  78.  
  79.         //mouse controls (single click register)
  80.         inputManager.addMapping("orbitPoint", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
  81.         inputManager.addListener(this, "orbitPoint");
  82.         inputManager.addMapping("move", new MouseButtonTrigger(MouseInput.BUTTON_RIGHT));
  83.         inputManager.addListener(this, "move");
  84.         //keyboard controls
  85.         inputManager.addMapping("Orbit", new KeyTrigger(KeyInput.KEY_SPACE));
  86.         inputManager.addListener(this, "Orbit");
  87.         inputManager.addMapping("OrbitSelection", new KeyTrigger(KeyInput.KEY_LCONTROL));
  88.         inputManager.addListener(this, "OrbitSelection");
  89.         inputManager.addMapping("defaultView", new KeyTrigger(KeyInput.KEY_NUMPAD3));
  90.         inputManager.addMapping("defaultView", new KeyTrigger(KeyInput.KEY_NUMPAD1));
  91.         inputManager.addListener(this, "defaultView");
  92.         inputManager.addMapping("top", new KeyTrigger(KeyInput.KEY_NUMPAD5));
  93.         inputManager.addListener(this, "top");
  94.         inputManager.addMapping("bottom", new KeyTrigger(KeyInput.KEY_NUMPAD0));
  95.         inputManager.addListener(this, "bottom");
  96.         inputManager.addMapping("left", new KeyTrigger(KeyInput.KEY_NUMPAD4));
  97.         inputManager.addListener(this, "left");
  98.         inputManager.addMapping("right", new KeyTrigger(KeyInput.KEY_NUMPAD6));
  99.         inputManager.addListener(this, "right");
  100.         inputManager.addMapping("front", new KeyTrigger(KeyInput.KEY_NUMPAD2));
  101.         inputManager.addListener(this, "front");
  102.         inputManager.addMapping("back", new KeyTrigger(KeyInput.KEY_NUMPAD8));
  103.         inputManager.addListener(this, "back");
  104.         inputManager.addMapping("createPlane", new KeyTrigger(KeyInput.KEY_ADD));
  105.         inputManager.addListener(this, "createPlane");
  106.  
  107.  
  108.  
  109.  
  110.     }
  111.  
  112.     public void onAction(String binding, boolean isPressed, float tpf)
  113.     {
  114.  
  115.         if (binding.equals("toggleRotate") && enabled)
  116.         {
  117.             if (isPressed)
  118.             {
  119.  
  120.  
  121.                 canRotate = true;
  122.                 inputManager.setCursorVisible(false);
  123.  
  124.             } else
  125.             {
  126.                 canRotate = false;
  127.                 inputManager.setCursorVisible(true);
  128.             }
  129.         }
  130.         //left mouse button pressed to orbit around a selected point
  131.         if (binding.equals("move"))
  132.         {
  133.             if (isPressed)
  134.             {
  135.  
  136.  
  137.                     // Reset results list.
  138.                     CollisionResults results = new CollisionResults();
  139.                     // Convert screen click to 3d position
  140.                     Vector2f click2d = inputManager.getCursorPosition();
  141.                     Vector3f click3d = OrbitCamera.cam.getWorldCoordinates(
  142.                             new Vector2f(click2d.getX(), click2d.getY()), 0f);
  143.                     Vector3f dir = OrbitCamera.cam.getWorldCoordinates(
  144.                             new Vector2f(click2d.getX(), click2d.getY()), 1f).subtractLocal(click3d);
  145.                     // Aim the ray from the clicked spot forwards.
  146.                     Ray ray = new Ray(click3d, dir);
  147.                     System.out.println("SCREEN: " + OrbitCamera.cam.getScreenCoordinates(dir));
  148.                     // Collect intersections between ray and all nodes in results list.
  149.                     rootNode.collideWith(ray, results);
  150.                     // (Print the results so we see what is going on:)
  151.                     Vector3f pt = null;
  152.                     for (int i = 0; i < results.size(); i++)
  153.                     {
  154.                         // (For each β€œhit”, we know distance, impact point, geometry.)
  155.                         float dist = results.getCollision(i).getDistance();
  156.                         pt = results.getCollision(i).getContactPoint();
  157.                         String target = results.getCollision(i).getGeometry().getName();
  158.                         System.out.println("Selection #" + i + ": " + target + " at " + pt + ", " + dist + " WU away.");
  159.                     }
  160.  
  161. //                Vector3f ori = new Vector3f(inputManager.getCursorPosition().x, inputManager.getCursorPosition().y , -1f);
  162. //
  163. //                Vector3f dest = new Vector3f(0f,0f,1f);
  164. //
  165. //                Ray ray = new Ray(ori, dest);
  166. //
  167. //
  168. //
  169. //                CollisionResults results = new CollisionResults();
  170. //
  171. //                guiNode.collideWith(ray, results);
  172. //                System.out.println(ray + "   " + guiNode.collideWith(ray, results) + "   " + results.getClosestCollision() + guiNode.getChildren());
  173.  
  174.                 // Use the results -- we rotate the selected geometry.
  175.                 if (results.size() > 0)
  176.                 {
  177.                     // The closest result is the target that the player picked:
  178.                     Geometry target = null;
  179.                     target = results.getClosestCollision().getGeometry();
  180.                     System.out.println(target.getName());
  181.  
  182.                     if (target.getName().equals("moveX+"))
  183.                     {
  184.  
  185.                         Main.sceneNode.move(Main.sceneNode.getLocalTranslation().add(0.1f, 0, 0));
  186.  
  187.                     }
  188.                     if (target.getName().equals("moveY+"))
  189.                     {
  190.  
  191.                         Main.sceneNode.move(Main.sceneNode.getLocalTranslation().add(0, 0.1f, 0));
  192.                     }
  193.                     if (target.getName().equals("moveZ+"))
  194.                     {
  195.  
  196.                         Main.sceneNode.move(Main.sceneNode.getLocalTranslation().add(0, 0, 0.1f));
  197.                     }
  198.                     if (target.getName().equals("moveX-"))
  199.                     {
  200.  
  201.                         Main.sceneNode.move(Main.sceneNode.getLocalTranslation().add(-0.1f, 0, 0));
  202.  
  203.                     }
  204.                     if (target.getName().equals("moveY-"))
  205.                     {
  206.  
  207.                         Main.sceneNode.move(Main.sceneNode.getLocalTranslation().add(0, -0.1f, 0));
  208.                     }
  209.                     if (target.getName().equals("moveZ-"))
  210.                     {
  211.  
  212.                         Main.sceneNode.move(Main.sceneNode.getLocalTranslation().add(0, 0, -0.1f));
  213.                     }
  214.  
  215.                     //code here for what to do with selection
  216.                 } else
  217.                 {
  218.                 }
  219.             }
  220.         }
  221.  
  222.         //space key use to enable and disable rotation
  223.         if (binding.equals("Orbit"))
  224.         {
  225.             if (isPressed)
  226.             {
  227.                 if (OrbitCamera.enabled == false)
  228.                 {
  229.                     System.out.println("ALLOW ORBIT");
  230.                     OrbitCamera.enabled = true;
  231.                 } else
  232.                 {
  233.                     System.out.println("DISALLOW ORBIT");
  234.                     OrbitCamera.enabled = false;
  235.                 }
  236.  
  237.             }
  238.         }
  239.  
  240.         if (binding.equals("createPlane"))
  241.         {
  242.             if (isPressed)
  243.             {
  244.                 // orbit.updateFocus(new Vector3f(12.9f,5.65f,12.7f));
  245.                 Canvas.createPlane(modelNode);
  246.                 System.out.println("CREATING NEW PLANE");
  247.  
  248.             }
  249.         }
  250.         if (binding.equals("defaultView"))
  251.         {
  252.             if (isPressed)
  253.             {
  254.                 orbit.moveCamera(0.39324224f, -5.1840777f);
  255.                 System.out.println("TOP VIEW");
  256.  
  257.             }
  258.         }
  259.  
  260.         if (binding.equals("top"))
  261.         {
  262.             if (isPressed)
  263.             {
  264.  
  265.                 orbit.moveCamera(1.57f, -6.2807655f);
  266.                 System.out.println("TOP VIEW");
  267.  
  268.             }
  269.         }
  270.         if (binding.equals("bottom"))
  271.         {
  272.             if (isPressed)
  273.             {
  274.                 orbit.moveCamera(-1.57f, -6.2807655f);
  275.                 System.out.println("BOTTOM VIEW");
  276.  
  277.             }
  278.         }
  279.         if (binding.equals("front"))
  280.         {
  281.             if (isPressed)
  282.             {
  283.                 orbit.moveCamera(0f, -0.0014692545f);
  284.                 System.out.println("FRONT VIEW");
  285.  
  286.             }
  287.         }
  288.         if (binding.equals("back"))
  289.         {
  290.             if (isPressed)
  291.             {
  292.                 orbit.moveCamera(0f, -3.1411176f);
  293.                 System.out.println("BACK VIEW");
  294.  
  295.             }
  296.         }
  297.  
  298.         if (binding.equals("left"))
  299.         {
  300.             if (isPressed)
  301.             {
  302.                 orbit.moveCamera(0f, 1.5707964f);
  303.                 System.out.println("LEFT VIEW");
  304.  
  305.             }
  306.         }
  307.  
  308.         if (binding.equals("right"))
  309.         {
  310.             if (isPressed)
  311.             {
  312.  
  313.                 orbit.moveCamera(0f, -1.5707964f);
  314.                 System.out.println("RIGHT VIEW");
  315.  
  316.             }
  317.         }
  318.  
  319.         if (binding.equals("OrbitSelection"))
  320.         {
  321.             if (isPressed)
  322.             {
  323.                 orbitSelection = true;
  324.                 System.out.println("orbit selection");
  325.  
  326.             }
  327.         }
  328.     }
  329.  
  330.     public void onAnalog(String name, float value, float tpf)
  331.     {
  332.         //continuous controls for camera orbit
  333.         if (name.equals("mouseLeft"))
  334.         {
  335.  
  336.             rotateCamera(-value);
  337.             //SceneNavigation.rotateHorizontal(-value);
  338.  
  339.         }
  340.         if (name.equals("mouseRight"))
  341.         {
  342.             rotateCamera(value);
  343.             // SceneNavigation.rotateHorizontal(value);
  344.         }
  345.         if (name.equals("Down"))
  346.         {
  347.             vRotateCamera(value);
  348.             // SceneNavigation.rotateVertical(value);
  349.         }
  350.         if (name.equals("Up"))
  351.         {
  352.             vRotateCamera(-value);
  353.             // SceneNavigation.rotateVertical(-value);
  354.         }
  355.         if (name.equals("ZoomIn"))
  356.         {
  357.             zoomCamera(value);
  358.         }
  359.         if (name.equals("ZoomOut"))
  360.         {
  361.             zoomCamera(-value);
  362.         }
  363.  
  364.     }
  365.  
  366.  
  367.     public Control cloneForSpatial(Spatial spatial)
  368.     {
  369.         return null;
  370.     }
  371.  
  372.     public void setSpatial(Spatial spatial)
  373.     {
  374.     }
  375.  
  376.     public void update(float tpf)
  377.     {
  378.     }
  379.  
  380.     public void render(RenderManager rm, ViewPort vp)
  381.     {
  382.     }
  383.  
  384.     public void write(JmeExporter ex) throws IOException
  385.     {
  386.     }
  387.  
  388.     public void read(JmeImporter im) throws IOException
  389.     {
  390.       //  throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  391.     }
  392. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement