diff options
author | Jesse Morgan <jesse@jesterpm.net> | 2011-02-09 01:28:27 +0000 |
---|---|---|
committer | Jesse Morgan <jesse@jesterpm.net> | 2011-02-09 01:28:27 +0000 |
commit | e910f993d6246cef0044c7a9bc46a27c2fb4d0c0 (patch) | |
tree | 8dff4ec141589f9cc4ebf23a0b326b7eda045e1f /src/tesseract | |
parent | 6bd3e52c071e6254657cc82f07d1597546bb4cec (diff) |
Added some menu code.
Diffstat (limited to 'src/tesseract')
-rw-r--r-- | src/tesseract/TesseractUI.java | 101 | ||||
-rw-r--r-- | src/tesseract/menuitems/ParticleMenuItem.java | 49 | ||||
-rw-r--r-- | src/tesseract/menuitems/TesseractMenuItem.java | 54 |
3 files changed, 188 insertions, 16 deletions
diff --git a/src/tesseract/TesseractUI.java b/src/tesseract/TesseractUI.java index 7c7e579..3eb0aca 100644 --- a/src/tesseract/TesseractUI.java +++ b/src/tesseract/TesseractUI.java @@ -8,6 +8,7 @@ import java.awt.event.MouseEvent; import java.awt.event.MouseMotionAdapter; import java.awt.event.MouseWheelEvent; import java.awt.event.MouseWheelListener; +import java.util.ArrayList; import javax.media.j3d.BoundingBox; import javax.media.j3d.Canvas3D; @@ -20,12 +21,16 @@ import javax.swing.JMenuItem; import javax.swing.JPopupMenu; import javax.swing.SwingUtilities; import javax.swing.Timer; +import javax.vecmath.Point2i; import javax.vecmath.Point3d; +import javax.vecmath.Vector3d; import javax.vecmath.Vector3f; import tesseract.forces.Gravity; +import tesseract.menuitems.ParticleMenuItem; import tesseract.objects.Particle; -import tesseract.objects.emitters.ParticleEmitter; +import tesseract.objects.Physical; +import tesseract.objects.PhysicalObject; import com.sun.j3d.utils.universe.SimpleUniverse; @@ -57,6 +62,11 @@ public class TesseractUI extends JFrame { private static final int MILISECONDS_IN_SECOND = 1000; /** + * List of items to appear in add objects menu. + */ + private ArrayList<JMenuItem> myObjectMenuItems; + + /** * A reference to the world. */ private World myWorld; @@ -77,16 +87,27 @@ public class TesseractUI extends JFrame { private double cameraXRotation, cameraYRotation, cameraDistance; /** + * Reference to the currently selected physical object. + */ + private Physical myCurrentObject; + + /** * UI Constructor. */ public TesseractUI() { super("Tesseract Project"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + myCurrentObject = null; + myWorld = new World( new BoundingBox(new Point3d(-UNIT / 2, -UNIT / 2, -UNIT / 2), new Point3d(UNIT / 2, UNIT / 2, UNIT / 2))); + myObjectMenuItems = new ArrayList<JMenuItem>(); + myObjectMenuItems.add(new ParticleMenuItem(myWorld)); + + createMenu(); setupCanvas(); pack(); @@ -99,9 +120,12 @@ public class TesseractUI extends JFrame { // THIS IS WHERE OBJECTS ARE FORCED INTO EXISTANCE // TODO: REMOVE TEST CODE - myWorld.addObject(new Particle(new Vector3f(0, 0, 0), null)); + PhysicalObject p = new Particle(new Vector3f(0, 0, 0), null); + + //myWorld.addObject(p); + myWorld.addForce(new Gravity()); - myWorld.addObject(new ParticleEmitter(new Vector3f(0, 0.49f, 0), 0.5f, null)); + //myWorld.addObject(new ParticleEmitter(new Vector3f(0, 0.49f, 0), 0.5f, null)); } /** @@ -117,6 +141,14 @@ public class TesseractUI extends JFrame { menuBar.add(simulationMenu); + JMenu objectsMenu = new JMenu("Add Object"); + + for (JMenuItem i : myObjectMenuItems) { + objectsMenu.add(i); + } + + menuBar.add(objectsMenu); + /* JCheckBoxMenuItem cMenuItem = new JCheckBoxMenuItem("Enable Particle Emitters", enableEmitters); cMenuItem.addActionListener(new ActionListener() { @@ -181,28 +213,47 @@ public class TesseractUI extends JFrame { private MouseEvent lastDragEvent = null; public void mouseDragged(final MouseEvent e) { - if (lastDragEvent != null) { - cameraXRotation += - Math.toRadians(e.getY() - lastDragEvent.getY()) / 3; + if (myCurrentObject != null) { + Point3d point = mouseToWorld( + new Point2i(e.getX(), e.getY())); + myCurrentObject.setPosition(new Vector3f(point)); + + } else { + Point3d point = mouseToWorld( + new Point2i(e.getX(), e.getY())); + Vector3d direction = new Vector3d(0, 0, -1); + Transform3D t3d = new Transform3D(); + cameraTG.getTransform(t3d); + t3d.transform(direction); - if (cameraXRotation > Math.PI / 2) { - cameraXRotation = Math.PI / 2; + myCurrentObject = myWorld.getObject(point, direction); + System.out.println(point); + System.out.println(myCurrentObject); + + if (lastDragEvent != null) { + cameraXRotation += + Math.toRadians(e.getY() - lastDragEvent.getY()) / 3; + + if (cameraXRotation > Math.PI / 2) { + cameraXRotation = Math.PI / 2; + + } else if (cameraXRotation < -Math.PI / 2) { + cameraXRotation = -Math.PI / 2; + } - } else if (cameraXRotation < -Math.PI / 2) { - cameraXRotation = -Math.PI / 2; + cameraYRotation += + Math.toRadians(e.getX() - lastDragEvent.getX()) / 3; + + updateCamera(); } - cameraYRotation += - Math.toRadians(e.getX() - lastDragEvent.getX()) / 3; - - updateCamera(); + lastDragEvent = e; } - - lastDragEvent = e; } public void mouseMoved(final MouseEvent e) { lastDragEvent = null; + myCurrentObject = null; } }); @@ -231,6 +282,24 @@ public class TesseractUI extends JFrame { } /** + * Convert screen coordinates to world coordinates. + * + * @param mousePosition The mouse position on screen. + * @return A point in the world. + */ + private Point3d mouseToWorld(final Point2i mousePosition) { + Point3d position = new Point3d(); + myCanvas.getPixelLocationInImagePlate( + mousePosition.getX(), mousePosition.getY(), position); + + Transform3D transform = new Transform3D(); + myCanvas.getImagePlateToVworld(transform); + transform.transform(position); + + return position; + } + + /** * Method to update the camera. */ private void updateCamera() { diff --git a/src/tesseract/menuitems/ParticleMenuItem.java b/src/tesseract/menuitems/ParticleMenuItem.java new file mode 100644 index 0000000..1210f36 --- /dev/null +++ b/src/tesseract/menuitems/ParticleMenuItem.java @@ -0,0 +1,49 @@ +package tesseract.menuitems; + +import java.awt.Color; +import java.awt.event.ActionEvent; + +import javax.swing.JColorChooser; +import javax.swing.JOptionPane; +import javax.vecmath.Color3f; +import javax.vecmath.Vector3f; + +import tesseract.World; +import tesseract.objects.Particle; + +/** + * Particle Menu Item. + * + * @author Jesse Morgan + */ +public class ParticleMenuItem extends TesseractMenuItem { + + /** + * Serial ID. + */ + private static final long serialVersionUID = 1L; + + /** + * Constructor for the menu item. + * + * @param theWorld The world into which we add. + */ + public ParticleMenuItem(final World theWorld) { + super(theWorld, "Particle"); + } + + /** + * Action handler. + * + * @param arg0 Unused event info. + */ + public void actionPerformed(final ActionEvent arg0) { + Color c = JColorChooser.showDialog(null, "Particle Color", Color.RED); + + Vector3f pos = + parseVector(JOptionPane.showInputDialog("Enter the position")); + + myWorld.addObject(new Particle(pos, new Color3f(c))); + } + +} diff --git a/src/tesseract/menuitems/TesseractMenuItem.java b/src/tesseract/menuitems/TesseractMenuItem.java new file mode 100644 index 0000000..16925d7 --- /dev/null +++ b/src/tesseract/menuitems/TesseractMenuItem.java @@ -0,0 +1,54 @@ +package tesseract.menuitems; + +import java.awt.event.ActionListener; + +import javax.swing.JMenuItem; +import javax.vecmath.Vector3f; + +import tesseract.World; + +/** + * Abstract class for menu items. + * + * @author Jesse Morgan + */ +public abstract class TesseractMenuItem + extends JMenuItem implements ActionListener { + + /** + * Serial ID. + */ + private static final long serialVersionUID = 1839955501629795920L; + + /** + * The reference to the world. + */ + protected World myWorld; + + /** + * Constructor. + * + * @param theWorld The world in which to add. + * @param theLabel The label for the menu item. + */ + public TesseractMenuItem(final World theWorld, final String theLabel) { + super(theLabel); + + myWorld = theWorld; + + addActionListener(this); + } + + /** + * Utility method to parse a string formatted as x,y,z into a vector3f. + * + * @param input A string to parse. + * @return A vector3f. + */ + protected Vector3f parseVector(final String input) { + + + return new Vector3f(); + } + +} |