Statistics
| Revision:

root / branches / 3D_Animation_prepto_osgvp_2_2_0 / libraries / lib3DMap / src / org / gvsig / gvsig3d / listener / canvasListener.java @ 26999

History | View | Annotate | Download (4.34 KB)

1
package org.gvsig.gvsig3d.listener;
2

    
3
import java.awt.event.KeyEvent;
4
import java.awt.event.KeyListener;
5
import java.awt.event.MouseEvent;
6
import java.awt.event.MouseListener;
7

    
8
import org.gvsig.osgvp.core.osg.Vec3;
9
import org.gvsig.osgvp.planets.Planet;
10
import org.gvsig.osgvp.planets.PlanetViewer;
11
import org.gvsig.osgvp.viewer.Camera;
12
import org.gvsig.osgvp.viewer.IViewerContainer;
13
import org.gvsig.osgvp.viewer.Intersection;
14
import org.gvsig.osgvp.viewer.Intersections;
15
import org.gvsig.osgvp.viewer.OSGViewer;
16

    
17
public class canvasListener implements KeyListener, MouseListener {
18
        private static boolean active = false;
19

    
20
        private IViewerContainer m_canvas3d = null;
21

    
22
        private Planet planet;
23

    
24
        private RotatePlanet updateViewThread;
25

    
26
        private int frameRate = 24;
27

    
28
        private PlanetViewer m_planetViewer;
29

    
30
        private static boolean activeRot;
31

    
32
        public void setCanvas(IViewerContainer m_canvas3d) {
33
                this.m_canvas3d = m_canvas3d;
34
                this.m_planetViewer = (PlanetViewer) m_canvas3d.getOSGViewer();
35
        }
36

    
37
        public void keyPressed(KeyEvent kev) {
38
                // TODO Auto-generated method stub
39
                int codigo = kev.getKeyCode();
40
                switch (kev.getKeyChar()) {
41
                case 'w':
42
                case 'W':
43
                        active = !active;
44
                        if (active) {
45
                                m_planetViewer.setPolygonMode(
46
                                                OSGViewer.PolygonModeType.GL_LINE);
47
                        } else {
48
                                m_planetViewer.setPolygonMode(
49
                                                OSGViewer.PolygonModeType.GL_FILL);
50
                        }
51
                        break;
52
                case 'a':
53
                case 'A':
54
                        Thread thread;
55
                        activeRot = !activeRot;
56
                        if (activeRot) {
57

    
58
                                updateViewThread = new RotatePlanet(1000/frameRate);
59

    
60
                                // Create the thread supplying it with the runnable object
61
                                thread = new Thread(updateViewThread);
62

    
63
                                // Start the thread
64
                                thread.start();
65
                        } else {
66
                                updateViewThread.end();
67
                        }
68
                        break;
69
                        
70
                case 'e':
71
                case 'E':
72
                        updateViewThread.setTime(1000/frameRate++);
73
                        break;
74
                case 'd':
75
                case 'D':
76
                        updateViewThread.setTime(1000/frameRate--);
77
                        break;
78
                }
79

    
80
        }
81

    
82
        public void keyReleased(KeyEvent kev) {
83
                // TODO Auto-generated method stub
84

    
85
        }
86

    
87
        public void keyTyped(KeyEvent kev) {
88
                // TODO Auto-generated method stub
89

    
90
        }
91

    
92
        /**
93
         * Class to rotate the planet. This class implements runnable and contructor
94
         * with time parameter.
95
         * 
96
         * @author julio
97
         * 
98
         */
99
        public class RotatePlanet implements Runnable {
100

    
101
                private boolean finish = false;
102

    
103
                private long time;
104

    
105
                public RotatePlanet(long time) {
106
                        this.time = time;
107
                }
108

    
109
                double lat = 0.0;
110

    
111
                double longi = 0.0;
112

    
113
                // This method is called when the thread runs
114
                public void run() {
115
                        while (true) {
116
                                try {
117
                                        Thread.sleep(time);
118
                                        synchronized (this) {
119
                                                if (finish) {
120
                                                        break;
121
                                                }
122
                                        }
123
                                } catch (InterruptedException e) {
124

    
125
                                        e.printStackTrace();
126
                                }
127
                                // Rotate planet
128
                                longi += 1.0;
129
                                longi = longi > 360 ? 0 : longi;
130
                                Camera cam = m_planetViewer.getCamera();
131
                                Vec3 camPos = planet.convertLatLongHeightToXYZ(new Vec3(lat,
132
                                                longi, cam.getEye().z()));
133

    
134
                                cam.setViewByLookAt(camPos.x(), camPos.y(), 500000 * 16.6,
135
                                                0, 0, 0, 0, 0, 1);
136
                                m_planetViewer.setCamera(cam);
137

    
138
                                // Repainting canvas
139
                                m_canvas3d.repaint();
140
                        }
141
                }
142

    
143
                public synchronized void end() {
144
                        finish = true;
145
                }
146

    
147
                public long getTime() {
148
                        return time;
149
                }
150

    
151
                public void setTime(long time) {
152
                        this.time = time;
153
                }
154

    
155
        }
156

    
157
        public Planet getPlanet() {
158
                return planet;
159
        }
160

    
161
        public void setPlanet(Planet planet) {
162
                this.planet = planet;
163
        }
164

    
165
        public void mouseClicked(MouseEvent e) {
166
                if (e.getButton() == MouseEvent.BUTTON1) {
167
                        Intersections hits = m_planetViewer.rayPick(this.planet, e.getX(), e.getY());
168
                        if(hits.containsIntersections()) {
169
                                Intersection hit = hits.getFirstIntersection();
170
                                System.err.println("Point:" + hit.getIntersectionPoint().toString());
171
                                Vec3 point = planet.convertXYZToLatLongHeight(hit
172
                                                .getIntersectionPoint());
173
                                System.err.println("LatLonHeight: " + point.toString());
174
                        }
175
                }
176
                
177
        }
178

    
179
        public void mouseEntered(MouseEvent arg0) {
180
                // TODO Auto-generated method stub
181
                
182
        }
183

    
184
        public void mouseExited(MouseEvent arg0) {
185
                // TODO Auto-generated method stub
186
                
187
        }
188

    
189
        public void mousePressed(MouseEvent arg0) {
190
                // TODO Auto-generated method stub
191
                
192
        }
193

    
194
        public void mouseReleased(MouseEvent arg0) {
195
                // TODO Auto-generated method stub
196
                
197
        }
198

    
199
}