Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / lib3DMap / src / org / gvsig / gvsig3d / gui / Hud.java @ 26036

History | View | Annotate | Download (7.43 KB)

1
package org.gvsig.gvsig3d.gui;
2

    
3
import java.awt.Component;
4
import java.awt.event.MouseEvent;
5
import java.awt.event.MouseMotionListener;
6

    
7
import org.apache.log4j.Logger;
8
import org.gvsig.gui.beans.Messages;
9
import org.gvsig.osgvp.Group;
10
import org.gvsig.osgvp.Node;
11
import org.gvsig.osgvp.UpdateNodeListener;
12
import org.gvsig.osgvp.Vec3;
13
import org.gvsig.osgvp.Vec4;
14
import org.gvsig.osgvp.exceptions.InvalidValueException;
15
import org.gvsig.osgvp.exceptions.node.NodeException;
16
import org.gvsig.osgvp.features.Text;
17
import org.gvsig.osgvp.planets.Planet;
18
import org.gvsig.osgvp.viewer.IViewerContainer;
19

    
20
import com.iver.ai2.gvsig3d.resources.ResourcesFactory;
21
import com.iver.andami.PluginServices;
22

    
23
/**
24
 * Use this class for draw items in HUD
25
 * 
26
 * @author julio
27
 * 
28
 */
29
public class Hud extends Group implements MouseMotionListener {
30

    
31
        private IViewerContainer m_canvas3d = null;
32
        
33
        private Planet m_planet = null;
34

    
35
        private String lonText;
36

    
37
        private String latText;
38

    
39
        private String lon;
40

    
41
        private String lat;
42

    
43
        private Text textoHud;
44

    
45
        private static String north;
46

    
47
        private static String south;
48

    
49
        private static String east;
50

    
51
        private static String west;
52
        
53
        private static Compass compass;
54
        
55
        private static Logger logger = Logger.getLogger(Hud.class.getName());
56

    
57
        static {
58
                north = Messages.getText("North");
59
                south = Messages.getText("South");
60
                east = Messages.getText("East");
61
                west = Messages.getText("West");
62

    
63
        }
64

    
65
        private int projectionType;
66

    
67
        /**
68
         * Constructor
69
         * 
70
         * @param m_canvas3d
71
         *            Viewer instance
72
         * @param m_planet
73
         *            Planet instance
74
         */
75
        public Hud(IViewerContainer m_canvas3d, Planet m_planet) {
76
                super();
77
                this.m_canvas3d = m_canvas3d;
78
                this.m_planet = m_planet;
79
                this.projectionType = m_planet.getCoordinateSystemType();
80
                // Inicialize object
81
                init();
82
        }
83

    
84
        /**
85
         * Inicilize the object params
86
         */
87
        private void init() {
88

    
89
                try {
90
                        textoHud = new Text();
91
                        compass = new Compass(m_planet);
92
                        if(projectionType == Planet.CoordinateSystemType.GEOCENTRIC){
93
                                compass.setPanetType(Compass.Mode.SPHERIC);
94
                        }
95
                        else compass.setPanetType(Compass.Mode.FLAT);
96
                } catch (NodeException e1) {
97
                        // TODO Auto-generated catch block
98
                        e1.printStackTrace();
99
                }
100
                if (getProjectionType() == Planet.CoordinateSystemType.GEOCENTRIC) {
101
                        // Setting up longitud and latitud string
102
                        lonText = PluginServices.getText(this, "Ext3D.longitude");
103
                        latText = PluginServices.getText(this, "Ext3D.latitude");
104
                } else {
105
                        lonText = PluginServices.getText(this, "X") + " ";
106
                        ;
107
                        latText = PluginServices.getText(this, "Y") + " ";
108
                        ;
109
                }
110

    
111
                // Adding text to group
112
                try {
113
                        this.addChild(textoHud);
114
                        this.addChild(compass);
115
                } catch (NodeException e) {
116
                        logger.error("Comand:" + "Error al a�adir nodo al hud.",e);
117
                }
118
                
119
                //Setting up the lighting mode to disable (rgaitan)
120
                try {
121
                        getOrCreateStateSet().setLightingMode(Node.Mode.OFF | Node.Mode.PROTECTED);
122
                } catch (InvalidValueException e) {
123
                        logger.error("Comand:" + "Error al inicializar las luces.",e);
124
                };
125

    
126
                // Seting up text
127
                textoHud.setCharacterSize(14);
128
                textoHud.setColor(new Vec4(1.0f, 1.0f, 1.0f, 1.0f));
129
                textoHud.setBackdropColor(0.0f, 0.0f, 1.0f, 1.0f);
130

    
131
                if (ResourcesFactory.exitsResouce("arial.ttf"))
132
                        textoHud.setFont(ResourcesFactory.getResourcePath("arial.ttf"));
133
                else {
134
                        // TODO: This freeze the execution.. disable when working. 
135
                        textoHud.setFont("arial.ttf");
136
                }
137

    
138
                textoHud.setPosition(10, 10, 0);
139
                textoHud.setBackdropType(Text.BackdropType.OUTLINE);
140
                textoHud.setAlignment(Text.AlignmentType.LEFT_CENTER);
141
                
142
                compass.setUpdateListener(new UpdateNodeListener(){
143

    
144
                        public void update(Node arg0) {
145
                                compass.update(m_canvas3d.getOSGViewer().getCamera());
146
                                
147
                        }});
148

    
149
                // Add mouse listener to viewer
150
                ((Component) m_canvas3d).addMouseMotionListener(this);
151

    
152
                // Update Hud
153
                updateHud();
154
        }
155

    
156
        /**
157
         * This method updates information of the HUD
158
         */
159
        public void updateHud() {
160

    
161
                if (m_planet.getCoordinateSystemType() == Planet.CoordinateSystemType.GEOCENTRIC) {
162
                        // Getting longitud and latitud informacion from planet
163
                        lon = Hud.getSexagesinal(m_planet.getLongitude(), true);
164
                        lat = Hud.getSexagesinal(m_planet.getLatitude(), false);
165
                        
166

    
167
                        // Updating text information
168
                        textoHud.setText(lonText + " " + lon + " " + latText + " " + lat);
169
                } else {
170
                        // Getting longitud and latitud informacion from planet
171
                        lon = Double.toString(m_planet.getLongitude());
172
                        lat = Double.toString(m_planet.getLatitude());
173

    
174
                        // Updating text information
175
                        textoHud.setText(lonText + " " + lon + " " + latText + " " + lat);
176
                }
177
                
178
                compass.setScale(new Vec3(75,75,75));
179
                compass.setPosition(new Vec3(m_canvas3d.getWidth()-70,m_canvas3d.getHeight()-70,0));
180
                // Repainting view
181
                if (m_canvas3d != null)
182
                        m_canvas3d.repaint();
183
        }
184

    
185
        public String getLat() {
186
                return lat;
187
        }
188

    
189
        public void setLat(String lat) {
190
                this.lat = lat;
191
        }
192

    
193
        public String getLatText() {
194
                return latText;
195
        }
196

    
197
        public void setLatText(String latText) {
198
                this.latText = latText;
199
        }
200

    
201
        public String getLon() {
202
                return lon;
203
        }
204

    
205
        public void setLon(String lon) {
206
                this.lon = lon;
207
        }
208

    
209
        public String getLonText() {
210
                return lonText;
211
        }
212

    
213
        public void setLonText(String lonText) {
214
                this.lonText = lonText;
215
        }
216

    
217
        /**
218
         * To transform longitud and latitud to sexagesinal format degress minuts
219
         * seconds
220
         * 
221
         * @param num
222
         *            number to transform
223
         * @param lat
224
         *            is tatitud or not
225
         * @return sexagesinal format
226
         */
227
        public static String getSexagesinal(double num, boolean lat) {
228

    
229
                String result = "";
230
                String ori = "";
231
                
232
                // Setting up North or South and East or West
233
                if (num < 0) {
234
                        num = num * (-1);
235
                        if (lat) {
236
                                ori = east;//south;// Messages.getText("South");
237
                        } else {
238
                                ori = north;//north;// Messages.getText("North");
239
                        }
240
                } else {
241
                        if (lat) {
242
                                ori = west;//west;// Messages.getText("West");
243
                        } else {
244
                                ori = south;//east;// Messages.getText("East");
245
                        }
246
                }
247

    
248
                // transform degrees in sexagesinal format
249
                int grados = (int) num;
250
                double resG = num - grados;
251
                int minutos = (int) (resG * 60);
252
                double minutosD = (resG * 60);
253
                double resM = minutosD - minutos;
254
                int segundos = (int) (resM * 60);
255
                String cadG = "";
256
                if (grados < 10)
257
                        cadG = cadG + "0";
258
                cadG = cadG + grados;
259

    
260
                String cadM = "";
261
                if (minutos < 10)
262
                        cadM = cadM + "0";
263
                cadM = cadM + minutos;
264

    
265
                String cadS = "";
266
                if (segundos < 10)
267
                        cadS = cadS + "0";
268
                cadS = cadS + segundos;
269

    
270
                // Building result string
271
                result = cadG + " " + cadM + " " + cadS + " " + ori;
272

    
273
                return result;
274
        }
275

    
276
        // MOUSE MOTION EVENTS
277

    
278
        public void mouseDragged(MouseEvent e) {
279
                // Updating Hud information+
280
                // If not update the hud information when mouses dragged the hud don�t change
281
                updateHud();
282

    
283
                // System.out.println("***************************************");
284
                // System.out.println("Longitud : " + m_planet.getLongitude());
285
                // System.out.println("Latitud : " + m_planet.getLatitude());
286
                // System.out.println("***************************************");
287

    
288
        }
289

    
290
        public void mouseMoved(MouseEvent e) {
291
                // TODO Auto-generated method stub
292
                updateHud();
293
        }
294

    
295
        public int getProjectionType() {
296
                return projectionType;
297
        }
298

    
299
        public void setProjectionType(int projectionType) {
300
                this.projectionType = projectionType;
301
        }
302

    
303
}