source: java/client_3D/src/com/framsticks/net/client3D/Viewer.java @ 66

Last change on this file since 66 was 66, checked in by Maciej Komosinski, 13 years ago

set 'eol-style' to 'native'

  • Property svn:eol-style set to native
File size: 7.2 KB
Line 
1package com.framsticks.net.client3D;
2
3import java.awt.BorderLayout;
4import java.awt.Container;
5import java.awt.event.MouseEvent;
6import java.awt.event.MouseListener;
7import java.awt.event.MouseMotionListener;
8import java.awt.event.MouseWheelEvent;
9import java.awt.event.MouseWheelListener;
10import java.io.File;
11import java.io.FilenameFilter;
12import java.io.IOException;
13import java.util.HashMap;
14import java.util.Set;
15
16import javax.media.opengl.GLCanvas;
17import javax.swing.JFrame;
18import javax.swing.JPanel;
19
20/**
21 * A window rendering the whole scene using OpenGL.
22 *
23 * @author vorg
24 */
25public class Viewer extends JPanel implements MouseListener,
26                MouseMotionListener {
27        static final long serialVersionUID = 1;
28        private Renderer renderer;
29        private HashMap<String, cgfxEffect> styles;
30        private Creature[] creatures;
31        private int clickX;
32        private int clickY;
33        private float clickRotY;
34        private float clickRotX;
35        private App app = null;
36
37        /**
38         * Constructor.
39         *
40         * @param app
41         *            An application instance.
42         */
43        public Viewer(App app) throws IOException {
44                this.app = app;
45                init();
46        }
47
48        /**
49         * Constructor.
50         */
51        public Viewer() throws IOException {
52                init();
53        }
54
55        /**
56         * Creates a rendering window.
57         *
58         * @return A window's handler.
59         */
60        public JFrame showInFrame() {
61                JFrame frame = new JFrame("Framsticks 3D View");
62                JFrame.setDefaultLookAndFeelDecorated(true);
63                frame.setSize(500, 500);
64                if (app != null) {
65                        frame.setLocationRelativeTo(app);
66                        frame.setLocation(app.getX() - frame.getSize().width, app.getY());
67                }
68                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
69                frame.setVisible(true);
70                Container contentPane = frame.getContentPane();
71                contentPane.setLayout(new BorderLayout());
72                contentPane.add(this, BorderLayout.CENTER);
73                return frame;
74        }
75
76        private void init() throws IOException {
77                styles = new HashMap<String, cgfxEffect>();
78                createCanvas();
79
80                MouseWheelListener listener = new MouseWheelListener() {
81                        // float zoomFactor = 0.1f;
82                        public void mouseWheelMoved(MouseWheelEvent e) {
83                                renderer.setTranslation(renderer.getTranslation()
84                                                * (float) (e.getWheelRotation() * 0.1 + 1.0));
85                        }
86                };
87                this.addMouseWheelListener(listener);
88                // Log.getInstance().addLoggerListener(new ILogListener() {
89                // public void onMesssage(String category, String text) {
90                // System.out.println(category + " > " + text );
91                // }
92                // });
93
94        }
95
96        /**
97         * Creates a surface object for OpenGL rendering.
98         */
99        private void createCanvas() throws IOException {
100                GLCanvas canvas = new GLCanvas();
101                renderer = new Renderer();
102
103                canvas.addGLEventListener(renderer);
104                canvas.addMouseListener(this);
105                canvas.addMouseMotionListener(this);
106
107                canvas.setVisible(true);
108
109                this.setOpaque(true);
110
111                this.setLayout(new BorderLayout());
112                this.add(canvas, BorderLayout.CENTER);
113                this.setVisible(true);
114
115                final String shadersPath = "res/shaders";
116                File dir = new File(shadersPath);
117                if (!dir.canRead()) {
118                        throw new IOException("Cannot access " + shadersPath);
119                }
120                if (!dir.isDirectory()) {
121                        throw new IOException(shadersPath + " is not a directory.");
122                }
123
124                // It is also possible to filter the list of returned files.
125                // This example does not return any files that start with `.'.
126                FilenameFilter filter = new FilenameFilter() {
127                        public boolean accept(File dir, String name) {
128                                return name.matches(".*[cC][gG][fF][xX]$");
129                        }
130                };
131                String[] children = dir.list(filter);
132                dir = new File("../..");
133                for (int i = 0; i < children.length; i++) {
134                        String name = children[i].substring(0, children[i].length() - 5);
135                        cgfxEffect effect = new cgfxEffect(name);
136
137                        styles.put(name, effect);
138                        if (app != null) {
139                                app.AddStyle(name);
140                        }
141                        // effect.LoadByName(name, context)
142                        // children[i];
143
144                        Log.getInstance().log("dbg", name);
145                }
146                setStyle("basic"); //seems even simpler (and thus supported) than "default"
147        }
148
149        /**
150         * Sets creatures to render.
151         *
152         * @param creatures
153         *            Creatures to render.
154         */
155        public void setCreatures(Creature[] creatures) {
156                this.creatures = creatures;
157                renderer.setCreatures(creatures);
158        }
159
160        /**
161         * Sets a world to render.
162         *
163         * @param world
164         *            A world to render.
165         */
166        public void setWorld(World world) {
167                renderer.setWorld(world);
168        }
169
170        /**
171         * Gets a set of style names.
172         *
173         * @return A set of style names.
174         */
175        public Set<String> getStyleNames() {
176                return styles.keySet();
177        }
178
179        /**
180         * Sets a currently activated style. If a style doesn't exist, it prints a
181         * warning and a list of available styles.
182         *
183         * @param styleName
184         *            Name of a style to turn on.
185         */
186        public void setStyle(String styleName) {
187                if (styles.containsKey(styleName)) {
188                        renderer.setStyle(styles.get(styleName));
189                } else {
190                        Log.getInstance().log("wrn", "Style: " + styleName + " not found");
191                        Log.getInstance().log("dbg", "Available styles:");
192                        for (String key : styles.keySet()) {
193                                Log.getInstance().log("dbg", key);
194                        }
195                }
196        }
197
198        /**
199         * Switches the view to a creature from a specified group, with a specified
200         * index. Passing value <value>1</value> in both parameters turns on the
201         * general view of the whole world.
202         *
203         * @param group
204         *            Number of a group.
205         * @param index
206         *            Index of a creature.
207         */
208        public void setView(int group, int index) {
209                if (creatures == null) {
210                        return;
211                }
212                renderer.resetTranslation();
213                if ((group == -1) || (index == -1)) {
214                        // if ((creatures != null) && creatures.length > 0)
215                        renderer.setCreatures(creatures);
216                        renderer.setModelType(Creature.ModelType.MechParts);
217                } else {
218                        Creature creature = null;
219                        for (Creature c : creatures) {
220                                if ((c.getGroup() == group) && (c.getIndex() == index)) {
221                                        creature = c;
222                                        break;
223                                }
224                        }
225                        if (creature != null) {
226                                renderer.setCreatures(new Creature[] { creature });
227                                renderer.setModelType(Creature.ModelType.Parts);
228                        } else {
229                                renderer.setCreatures(creatures);
230                                renderer.setModelType(Creature.ModelType.MechParts);
231                        }
232
233                }
234        }
235
236        /* (non-Javadoc)
237         * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
238         */
239        public void mouseClicked(MouseEvent e) {
240        }
241
242        /* (non-Javadoc)
243         * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
244         */
245        public void mouseEntered(MouseEvent e) {
246        }
247
248        /* (non-Javadoc)
249         * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
250         */
251        public void mouseExited(MouseEvent e) {
252        }
253
254        /* (non-Javadoc)
255         * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
256         */
257        public void mousePressed(MouseEvent e) {
258                clickX = e.getX();
259                clickY = e.getY();
260                clickRotY = renderer.getRotationY();
261                clickRotX = renderer.getRotationX();
262        }
263
264        /* (non-Javadoc)
265         * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
266         */
267        public void mouseReleased(MouseEvent e) {
268        }
269
270        /* (non-Javadoc)
271         * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
272         */
273        public void mouseDragged(MouseEvent e) {
274                float difX = (e.getX() - clickX) / (float) getWidth();
275                float difY = (e.getY() - clickY) / (float) getHeight();
276                renderer.setRotationY(clickRotY + 180 * difX);
277                renderer.setRotationX(clickRotX + 180 * difY);
278        }
279
280        /* (non-Javadoc)
281         * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
282         */
283        public void mouseMoved(MouseEvent e) {
284        }
285}
Note: See TracBrowser for help on using the repository browser.