001: /*
002: * $Header: /cvs/j3dfly/J3dFly/src/org/jdesktop/j3dfly/SelectionControl.java,v 1.1 2005/04/20 21:04:21 paulby Exp $
003: *
004: * Sun Public License Notice
005: *
006: * The contents of this file are subject to the Sun Public License Version
007: * 1.0 (the "License"). You may not use this file except in compliance with
008: * the License. A copy of the License is available at http://www.sun.com/
009: *
010: * The Original Code is Java 3D(tm) Fly Through.
011: * The Initial Developer of the Original Code is Paul Byrne.
012: * Portions created by Paul Byrne are Copyright (C) 2002.
013: * All Rights Reserved.
014: *
015: * Contributor(s): Paul Byrne.
016: *
017: **/
018: package org.jdesktop.j3dfly;
019:
020: import java.awt.event.MouseListener;
021: import java.awt.event.MouseMotionListener;
022: import java.awt.event.MouseEvent;
023: import java.util.LinkedList;
024:
025: import javax.media.j3d.Locale;
026: import javax.media.j3d.Canvas3D;
027: import com.sun.j3d.utils.picking.PickCanvas;
028: import com.sun.j3d.utils.picking.PickResult;
029:
030: /**
031: *
032: * @author paulby
033: * @version $Revision: 1.1 $
034: */
035: public class SelectionControl extends Object implements MouseListener,
036: MouseMotionListener {
037:
038: public static final int MOUSE_CLICKED = 0x01;
039: public static final int MOUSE_PRESSED = 0x02;
040: public static final int MOUSE_RELEASED = 0x04;
041: public static final int MOUSE_MOVED = 0x08;
042: public static final int MOUSE_DRAGGED = 0x0F;
043:
044: protected PickCanvas pickCanvas;
045: private Canvas3D canvas;
046:
047: private int currentFlags = 0;
048:
049: private LinkedList listeners = new LinkedList();
050: private SelectionListener currentListener = null;
051:
052: /** Creates new PickControl */
053: public SelectionControl(Locale locale, Canvas3D canvas) {
054: this .canvas = canvas;
055:
056: pickCanvas = new PickCanvas(canvas, locale);
057: pickCanvas.setTolerance(0f);
058: pickCanvas.setMode(PickCanvas.BOUNDS);
059: }
060:
061: /**
062: * Add a new Listener for pick events. The listener will receive all
063: * SelectionListener events, the flag determines if a pick is performed
064: * before calling the listener
065: *
066: * @param listener The event listener
067: * @param listenFlags Flags for which events should be a combination of
068: * MOUSE_CLICKED, MOUSE_PRESSED, MOUSE_RELEASED, MOUSE_MOVED and MOUSE_DRAGGED
069: */
070: public void pushSelectionListener(SelectionListener listener,
071: int listenFlags) {
072: if (currentFlags == 0) {
073: canvas.addMouseListener(this );
074: canvas.addMouseMotionListener(this );
075: }
076:
077: listeners.add(new ListenerObj(listener, listenFlags));
078: currentFlags = listenFlags;
079: currentListener = listener;
080: }
081:
082: /**
083: * Remove a listener and update the global listenFlags
084: */
085: public void popSelectionListener(SelectionListener listener) {
086:
087: if (currentListener != listener)
088: throw new RuntimeException(
089: "Not poping top SelectionListener");
090:
091: listeners.removeLast();
092: if (listeners.size() != 0) {
093: ListenerObj obj = (ListenerObj) listeners.getLast();
094: currentListener = obj.selectionListener;
095: currentFlags = obj.listenerFlags;
096: } else {
097: currentFlags = 0;
098: canvas.removeMouseListener(this );
099: canvas.removeMouseMotionListener(this );
100: }
101: }
102:
103: protected PickResult doPick(java.awt.event.MouseEvent evt) {
104: pickCanvas.setShapeLocation(evt);
105: PickResult result;
106: try {
107: result = pickCanvas.pickClosest();
108: } catch (javax.media.j3d.CapabilityNotSetException e) {
109: result = null;
110: System.out.println("No Capability to pick object");
111: }
112:
113: return result;
114: }
115:
116: public void mouseReleased(java.awt.event.MouseEvent p1) {
117: PickResult pick = null;
118: if ((currentFlags & MOUSE_RELEASED) != 0)
119: pick = doPick(p1);
120:
121: currentListener.mouseReleased(p1, pick);
122: }
123:
124: public void mouseEntered(java.awt.event.MouseEvent p1) {
125: }
126:
127: public void mouseClicked(java.awt.event.MouseEvent p1) {
128: PickResult pick = null;
129: if ((currentFlags & MOUSE_CLICKED) != 0)
130: pick = doPick(p1);
131:
132: currentListener.mouseClicked(p1, pick);
133: }
134:
135: public void mousePressed(java.awt.event.MouseEvent p1) {
136: if ((p1.getModifiers() & MouseEvent.BUTTON1_MASK) != MouseEvent.BUTTON1_MASK)
137: return;
138:
139: PickResult pick = null;
140: if ((currentFlags & MOUSE_PRESSED) != 0)
141: pick = doPick(p1);
142:
143: currentListener.mousePressed(p1, pick);
144: }
145:
146: public void mouseExited(java.awt.event.MouseEvent p1) {
147: }
148:
149: public void mouseDragged(java.awt.event.MouseEvent p1) {
150: if ((p1.getModifiers() & MouseEvent.BUTTON1_MASK) != MouseEvent.BUTTON1_MASK)
151: return;
152:
153: PickResult pick = null;
154: if ((currentFlags & MOUSE_DRAGGED) != 0)
155: pick = doPick(p1);
156:
157: currentListener.mouseDragged(p1, pick);
158: }
159:
160: public void mouseMoved(java.awt.event.MouseEvent p1) {
161: PickResult pick = null;
162: if ((currentFlags & MOUSE_MOVED) != 0)
163: pick = doPick(p1);
164:
165: currentListener.mouseMoved(p1, pick);
166: }
167:
168: class ListenerObj {
169: public SelectionListener selectionListener;
170: public int listenerFlags;
171:
172: public ListenerObj(SelectionListener listener, int flags) {
173: this.selectionListener = listener;
174: this.listenerFlags = flags;
175: }
176: }
177: }
|