Statistics
| Revision:

gvsig-3d / 2.1 / trunk / org.gvsig.gvsig3d / org.gvsig.gvsig3d.lib / org.gvsig.gvsig3d.lib.impl / src / main / java / org / gvsig / gvsig3d / impl / hud / HudImpl.java @ 385

History | View | Annotate | Download (8.86 KB)

1
/* gvSIG 3D extension for gvSIG
2
 *
3
 * Copyright (C) 2012 Prodevelop.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *   Prodevelop, S.L.
22
 *   Pza. Don Juan de Villarrasa, 14 - 5
23
 *   46001 Valencia
24
 *   Spain
25
 *
26
 *   +34 963 510 612
27
 *   +34 963 510 968
28
 *   prode@prodevelop.es
29
 *   http://www.prodevelop.es
30
 */
31
/*
32
 * AUTHORS:
33
 * 2012 AI2 - Instituto Universitario de Automatica e Informatica Industrial.
34
 * Universitat Politecnica de Valencia (UPV)
35
 * http://www.ai2.upv.es
36
 */
37

    
38

    
39
package org.gvsig.gvsig3d.impl.hud;
40

    
41
import java.awt.Component;
42
import java.awt.event.MouseEvent;
43
import java.awt.event.MouseMotionListener;
44

    
45
import org.gvsig.gvsig3d.Gvsig3DManager;
46
import org.gvsig.gvsig3d.compass.Compass;
47
import org.gvsig.gvsig3d.hud.Hud;
48
import org.gvsig.gvsig3d.impl.compass.CompassImpl;
49
import org.gvsig.gvsig3d.resources.ResourcesFactory;
50
import org.gvsig.i18n.Messages;
51
import org.gvsig.osgvp.core.osg.Group;
52
import org.gvsig.osgvp.core.osg.Node;
53
import org.gvsig.osgvp.core.osg.Vec3;
54
import org.gvsig.osgvp.core.osg.Vec4;
55
import org.gvsig.osgvp.exceptions.InvalidValueException;
56
import org.gvsig.osgvp.exceptions.node.NodeException;
57
import org.gvsig.osgvp.features.Text;
58
import org.gvsig.osgvp.terrain.Terrain;
59
import org.gvsig.osgvp.util.EventHandler;
60
import org.gvsig.osgvp.util.UpdateNodeListener;
61
import org.gvsig.osgvp.viewer.IViewerContainer;
62
import org.slf4j.Logger;
63
import org.slf4j.LoggerFactory;
64

    
65
/**
66
 * @author Jesus Zarzoso- jzarzoso@ai2.upv.es
67
 * @version $Id$
68
 * 
69
 */
70
public class HudImpl extends Hud implements MouseMotionListener, EventHandler {
71
        private IViewerContainer _canvas3d = null;
72
        private Terrain _terrain = null;
73
        private String _lonText;
74
        private String _latText;
75
        private String _lon;
76
        private String _lat;
77
        private Text _textoHud;
78
        private CompassImpl _compass;
79
        private static String _north;
80
        private static String _south;
81
        private static String _east;
82
        private static String _west;
83
        final static private Logger _logger = LoggerFactory
84
        .getLogger(Gvsig3DManager.class);
85

    
86
        static {
87
                _north = Messages.getText("North");
88
                _south = Messages.getText("South");
89
                _east = Messages.getText("East");
90
                _west = Messages.getText("West");
91

    
92
        }
93

    
94
        private int _projectionType;
95
        
96

    
97
        /**
98
         * Constructor
99
         * 
100
         * @param m_canvas3d
101
         *            Viewer instance
102
         * @param m_terrain
103
         *            Planet instance
104
         */
105
        public HudImpl(IViewerContainer canvas3d, Terrain terrain) {
106
                super();
107
                _canvas3d = canvas3d;
108
                _terrain = terrain;
109
                _projectionType = terrain.getCoordinateSystemType();
110
                // Inicialize object
111
                init();
112
        }
113

    
114
        /**
115
         * Inicilize the object params
116
         */
117
        private void init() {
118

    
119
                try {
120
                        _textoHud = new Text();
121
                        _compass = new CompassImpl(_terrain);
122
                        if (_projectionType == Terrain.CoordinateSystemType.GEOCENTRIC) {
123
                                _compass.setPanetType(Compass.Mode.SPHERIC);
124
                                //compass in geocentric views is not working, disabling for now
125
                                _compass.setEnabledNode(false);
126
                        } else
127
                                _compass.setPanetType(Compass.Mode.FLAT);
128
                } catch (NodeException e1) {
129
                        // TODO Auto-generated catch block
130
                        e1.printStackTrace();
131
                }
132
                if (getProjectionType() == Terrain.CoordinateSystemType.GEOCENTRIC) {
133
                        // Setting up longitud and latitud string
134
                        _lonText = "longitude";
135
                        _latText = "latitude";
136
                } else {
137
                        _lonText = "X" + " ";
138
                        _latText = "Y" + " ";
139
                }
140

    
141
                // Adding text to group
142
                try {
143
                        this.addChild(_textoHud);
144
                        this.addChild(_compass);
145
                } catch (NodeException e) {
146
                        _logger.error("Comand:" + "Error al a�adir nodo al hud.", e);
147
                }
148

    
149
                // Setting up the lighting mode to disable (rgaitan)
150
                try {
151
                        getOrCreateStateSet().setLightingMode(
152
                                        Node.Mode.OFF | Node.Mode.PROTECTED);
153
                } catch (InvalidValueException e) {
154
                        _logger.error("Comand:" + "Error al inicializar las luces.", e);
155
                }
156
                ;
157

    
158
                // Seting up text
159
                _textoHud.setCharacterSize(14);
160
                _textoHud.setColor(new Vec4(1.0f, 1.0f, 1.0f, 1.0f));
161
                _textoHud.setBackdropColor(0.0f, 0.0f, 1.0f, 1.0f);
162

    
163
                if (ResourcesFactory.exitsResouce("arial.ttf"))
164
                        _textoHud.setFont(ResourcesFactory.getResourcePath("arial.ttf"));
165
                else {
166
                        // TODO: This freeze the execution.. disable when working.
167
                        _textoHud.setFont("arial.ttf");
168
                }
169

    
170
                _textoHud.setPosition(10, 10, 0);
171
                _textoHud.setBackdropType(Text.BackdropType.OUTLINE);
172
                _textoHud.setAlignment(Text.AlignmentType.LEFT_CENTER);
173
                
174

    
175
                _compass.setUpdateListener(new UpdateNodeListener() {
176

    
177
                        public void update(Node arg0) {
178
                                _compass.update(_canvas3d.getOSGViewer().getCamera());
179
                                updateHud();                                
180

    
181
                        }
182
                });
183

    
184
                // disabling compass.
185
                //_compass.setEnabledNode(true);
186
                _compass.setScale(new Vec3(75, 75, 75));
187
                // _compass.setPosition(new
188
                // Vec3(_canvas3d.getWidth()-70,_canvas3d.getHeight()-70,0));
189

    
190
                // Add mouse listener to viewer
191
                ((Component) _canvas3d).addMouseMotionListener(this);
192
                // _canvas3d.getOSGViewer().addEventHandler(this);
193

    
194
                _canvas3d.addComponentListener(new java.awt.event.ComponentAdapter() {
195
                        public void componentResized(java.awt.event.ComponentEvent evt) {
196
                                updateHud();
197
                        }
198
                });
199
                
200
                // Update Hud
201
                updateHud();
202
        }
203

    
204
        public void setCompassPosition(Vec3 pos) {
205

    
206
                _compass.setPosition(pos);
207

    
208
        }
209
        
210

    
211

    
212
        /**
213
         * This method updates information of the HUD
214
         */
215
        public void updateHud() {
216

    
217
                if (_terrain.getCoordinateSystemType() == Terrain.CoordinateSystemType.GEOCENTRIC) {
218
                        // Getting longitud and latitud informacion from planet
219
                        _lon = HudImpl.getSexagesinal(_terrain.getLongitude(), true);
220
                        _lat = HudImpl.getSexagesinal(_terrain.getLatitude(), false);
221

    
222
                        // Updating text information
223
                        _textoHud.setText(_lonText + " " + _lon + " " + _latText + " "
224
                                        + _lat);
225

    
226
                } else {
227
                        // Getting longitud and latitud informacion from planet
228
                        _lon = Double.toString(_terrain.getLongitude());
229
                        _lat = Double.toString(_terrain.getLatitude());
230

    
231
                        // Updating text information
232
                        _textoHud.setText(_lonText + " " + _lon + " " + _latText + " "
233
                                        + _lat);
234
                }
235

    
236
                _compass.setPosition(new Vec3(_canvas3d.getWidth() - 50, _canvas3d
237
                                .getHeight() - 50, 0));
238
                
239
                                
240
                
241
                // Repainting view
242
                /*
243
                 * if (_canvas3d != null) _canvas3d.repaint();
244
                 */
245
        }
246

    
247
        public String getLat() {
248
                return _lat;
249
        }
250

    
251
        public void setLat(String lat) {
252
                _lat = lat;
253
        }
254

    
255
        public String getLatText() {
256
                return _latText;
257
        }
258

    
259
        public void setLatText(String latText) {
260
                _latText = latText;
261
        }
262

    
263
        public String getLon() {
264
                return _lon;
265
        }
266

    
267
        public void setLon(String lon) {
268
                _lon = lon;
269
        }
270

    
271
        public String getLonText() {
272
                return _lonText;
273
        }
274

    
275
        public void setLonText(String lonText) {
276
                _lonText = lonText;
277
        }
278

    
279
        /**
280
         * To transform longitud and latitud to sexagesinal format degress minuts
281
         * seconds
282
         * 
283
         * @param num
284
         *            number to transform
285
         * @param lat
286
         *            is tatitud or not
287
         * @return sexagesinal format
288
         */
289
        public static String getSexagesinal(double num, boolean lat) {
290

    
291
                String result = "";
292
                String ori = "";
293

    
294
                // Setting up North or South and East or West
295
                if (num < 0) {
296
                        num = num * (-1);
297
                        if (lat) {
298
                                ori = _east;// south;// Messages.getText("South");
299
                        } else {
300
                                ori = _north;// north;// Messages.getText("North");
301
                        }
302
                } else {
303
                        if (lat) {
304
                                ori = _west;// west;// Messages.getText("West");
305
                        } else {
306
                                ori = _south;// east;// Messages.getText("East");
307
                        }
308
                }
309

    
310
                // transform degrees in sexagesinal format
311
                int grados = (int) num;
312
                double resG = num - grados;
313
                int minutos = (int) (resG * 60);
314
                double minutosD = (resG * 60);
315
                double resM = minutosD - minutos;
316
                int segundos = (int) (resM * 60);
317
                String cadG = "";
318
                if (grados < 10)
319
                        cadG = cadG + "0";
320
                cadG = cadG + grados;
321

    
322
                String cadM = "";
323
                if (minutos < 10)
324
                        cadM = cadM + "0";
325
                cadM = cadM + minutos;
326

    
327
                String cadS = "";
328
                if (segundos < 10)
329
                        cadS = cadS + "0";
330
                cadS = cadS + segundos;
331

    
332
                // Building result string
333
                result = cadG + " " + cadM + " " + cadS + " " + ori;
334

    
335
                return result;
336
        }
337

    
338
        // MOUSE MOTION EVENTS
339

    
340
        public void mouseDragged(MouseEvent e) {
341
                // Updating Hud information+
342
                // If not update the hud information when mouses dragged the hud don�t
343
                // change
344
                updateHud();
345

    
346
        }
347

    
348
        public void mouseMoved(MouseEvent e) {
349
                // TODO Auto-generated method stub
350
                updateHud();
351
        }
352

    
353
        public int getProjectionType() {
354
                return _projectionType;
355
        }
356

    
357
        public void setProjectionType(int projectionType) {
358
                _projectionType = projectionType;
359
        }
360

    
361
        public void handle() {
362

    
363
                updateHud();
364

    
365
        }
366

    
367

    
368
}