KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > buchuki > ensmer > InterfaceManager


1 /*
2  * Copyright 2004 Dusty Phillips
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.buchuki.ensmer;
17
18 import com.sun.j3d.utils.picking.PickCanvas;
19 import com.sun.j3d.utils.picking.PickResult;
20 import java.awt.*;
21 import java.awt.event.*;
22 import java.awt.image.BufferedImage JavaDoc;
23 import javax.media.j3d.*;
24 import javax.vecmath.*;
25 import com.sun.j3d.utils.universe.SimpleUniverse;
26
27
28 /**
29  * This class manages the GUI components and events associated with the user
30  * interface. To a lesser extent, it also manages the Java3D Scenegraph. The
31  * class tracks the main Canvas3D and the events activated on it. It is
32  * responsible for managing the actual 3D objects created by <code>Frontend</code>
33  * s and managed by <code>Area</code>s.
34  *
35  * @author Dusty Phillips [dusty@buchuki.com]
36  */

37 public class InterfaceManager {
38     
39     /**
40      * Construct an InterfaceManager. Create and display the frame, maximized.
41      * Initialize the Canvas3D and universe. Does not add any objects to the
42      * scene. If objects have been prevayled, they should be added when the
43      * <code>AreaManager</code> is initialized.
44      */

45     public InterfaceManager() {
46         Frame ensmerFrame = createWindow();
47         ensmerFrame.add(createCanvas());
48         ensmerFrame.setVisible(true);
49         hiddenCursor = Toolkit.getDefaultToolkit().createCustomCursor(
50         new BufferedImage JavaDoc(1, 1, BufferedImage.TYPE_INT_ARGB),
51         new Point(0, 0), "Invisible Cursor");
52     }
53     
54     /**
55      * Display a particular Area's branhgraph. This entails removing all current
56      * branchgraphs and loading the one given by the area.
57      *
58      * @param area the new Area who's scene should be displayed
59      */

60     public void displayArea(Area area) {
61         BranchGroup newBranchGraph = area.getBranchGraph();
62         if (branchGraph == null) {
63             universe.getLocale().addBranchGraph(newBranchGraph);
64         }
65         else {
66             universe.getLocale().replaceBranchGraph(branchGraph, newBranchGraph);
67         }
68         branchGraph = newBranchGraph;
69         setViewPosition(area.getUserPosition());
70     }
71     
72     /**
73      * Retrieve a reference to the Canvas3D that objects are displayed on.
74      *
75      * @return the Canvas
76      */

77     public Canvas3D getCanvas() {
78         return canvas;
79     }
80     
81     /**
82      * Obtain a reference to a <em>copy</em> of the current view Transform, in
83      * Matrix4f format.
84      *
85      * @return a copy of the current view Transform
86      */

87     public Matrix4f getViewPosition() {
88         Transform3D transform = new Transform3D();
89         universe.getViewingPlatform().getViewPlatformTransform()
90         .getTransform(transform);
91         Matrix4f retval = new Matrix4f();
92         transform.get(retval);
93         return retval;
94     }
95     
96     /**
97      * Set the Viewing Transform based on an <em>absolute</em> Matrix4f
98      *
99      * @param position the new Matrix4f for the viewing Transform
100      */

101     public void setViewPosition(Matrix4f position) {
102         Transform3D transform = new Transform3D(position);
103         universe.getViewingPlatform().getViewPlatformTransform()
104         .setTransform(transform);
105     }
106     
107     /**
108      * Show or hide the mouse cursor over the Canvas.
109      *
110      * @param visible if true, show the cursor, otherwise hide it
111      */

112     public void setCursorVisible(boolean visible) {
113         if (visible) {
114             canvas.setCursor(Cursor.getDefaultCursor());
115         }
116         else {
117             canvas.setCursor(hiddenCursor);
118         }
119     }
120     
121     /**
122      * Return a pickResult picking the closest object in the xPos/yPos coordinate
123      * on the canvas
124      *
125      * @param xPos X position on the canvas to pick from
126      * @param yPos Y position on the canvas to pick from
127      * @return a pick result indicating the picked object; can be null if no
128      * object was picked
129      */

130     public PickResult pickClosest(int xPos, int yPos) {
131         pickCanvas.setShapeLocation(xPos, yPos);
132         return pickCanvas.pickClosest();
133     }
134     
135     /**
136      * Return the Long identifier of a picked object at the given position.
137      * This class searches the SceneGraphPath of the picked node for the highest
138      * (in the SceneGraph: closest to the locale) BranchGroup that has pick
139      * reporting and a Long user data. This allows the frontends to enable
140      * pick reporting independently for their own purposes on lower level
141      * scenegraphs.
142      *
143      * @param xPos X position on the canvas to pick from
144      * @param yPos Y position on the canvas to pick from
145      * @return a Long identifier associated with an object intersecting the
146      * pick ray, or null if no identifier was picked
147      */

148     public Long JavaDoc pickObject(int xPos, int yPos) {
149         PickResult result = pickClosest(xPos, yPos);
150         if (result == null) {
151             return null;
152         }
153         SceneGraphPath path = result.getSceneGraphPath();
154         for (int i = 0; i < path.nodeCount(); i++) {
155             if (path.getNode(i) instanceof BranchGroup) {
156                 BranchGroup possible = (BranchGroup) path.getNode(i);
157                 Object JavaDoc obj = possible.getUserData();
158                 if (obj instanceof Long JavaDoc) {
159                     return (Long JavaDoc) obj;
160                 }
161             }
162         }
163         return null;
164         //PREVIOUS TEST (didn't use SceneGraphPath)
165
//if (result != null && (node = result.getNode(PickResult.BRANCH_GROUP)) != null) {
166

167     }
168     
169     /**
170      * Initialize the window on behalf of the constructor.
171      *
172      * @return The newly created Frame
173      */

174     private Frame createWindow() {
175         Frame frame = new Frame("Ensmer");
176         frame.setLayout(new BorderLayout());
177         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
178         frame.setBounds(0, 0, screenSize.width, screenSize.height);
179         frame.addWindowListener(new EnsmerCloser());
180         return frame;
181     }
182     
183     /**
184      * Set up a Canvas3D and universe on behalf of the constructor. Initialize
185      * instance variables related to the universe
186      *
187      * @return a configured Canvas3D suitable for use in the Frame
188      */

189     private Canvas3D createCanvas() {
190         canvas = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
191         universe = new SimpleUniverse(canvas);
192         universe.getViewer().getView().setBackClipDistance(100);
193         platformBounds = new BoundingLeaf(new BoundingSphere(new Point3d(), 1000));
194         BranchGroup grp = new BranchGroup();
195         grp.addChild(platformBounds);
196         universe.getViewingPlatform().getViewPlatformTransform().addChild(grp);
197         
198         pickCanvas = new PickCanvas(canvas, universe.getLocale());
199         pickCanvas.setTolerance(0.0f);
200         pickCanvas.setMode(pickCanvas.GEOMETRY);
201         
202         BranchGroup root = new BranchGroup();
203         
204         AmbientLight lightA = new AmbientLight();
205         lightA.setInfluencingBoundingLeaf(platformBounds);
206         root.addChild(lightA);
207         DirectionalLight lightD = new DirectionalLight(new Color3f(1f, 1f, 1f),
208         new Vector3f(0f, -1f, -1f));
209         //PointLight lightD = new PointLight();
210
lightD.setInfluencingBoundingLeaf(platformBounds);
211         root.addChild(lightD);
212         universe.addBranchGraph(root);
213         return canvas;
214     }
215     
216     /**
217      * The canvas that is displayed and receives input events
218      */

219     private Canvas3D canvas;
220     
221     /**
222      * A custom invisible cursor to be displayed when the cursor is hidden
223      */

224     private Cursor hiddenCursor;
225     
226     /**
227      * The universe that contains the scenegraph
228      */

229     private SimpleUniverse universe;
230     
231     /**
232      * A pointer to the universe's current (single) BranchGraph, if one exists.
233      * This is the BranchGraph associated with an area. Non area-specific things
234      * (like lights) are in a separate BranchGraph.
235      */

236     private BranchGroup branchGraph;
237     
238     /**
239      * A bounding leaf attached to the view platform
240      */

241     private BoundingLeaf platformBounds;
242     
243     /**
244      * A PickCanvas associated with the window. used for object selection
245      */

246     private PickCanvas pickCanvas;
247     
248     /**
249      * Class to listen to window events on the Frame. Specifically, when the
250      * frame is closed, let EnsmerManager know so it can exit.
251      *
252      * @author Dusty Phillips [dusty@buchuki.com]
253      */

254     private class EnsmerCloser extends WindowAdapter {
255         /**
256          * Description of the Method
257          *
258          * @param e Description of the Parameter
259          */

260         public void windowClosing(WindowEvent e) {
261             EnsmerManager.instance().closeEnsmer();
262         }
263     }
264 }
265
266
Popular Tags