Statistics
| Revision:

root / branches / 3D_Animation_prepto_osgvp_2_2_0 / extensions / ext3Dgui / src / org / gvsig / gvsig3dgui / behavior / RectangleBehavior3D.java @ 26998

History | View | Annotate | Download (6.09 KB)

1
package org.gvsig.gvsig3dgui.behavior;
2

    
3
import java.awt.Graphics;
4
import java.awt.Point;
5
import java.awt.Rectangle;
6
import java.awt.event.MouseEvent;
7
import java.awt.geom.Point2D;
8
import java.awt.geom.Rectangle2D;
9

    
10
import org.apache.log4j.Logger;
11
import org.gvsig.gvsig3d.map3d.GraphicLayer3D;
12
import org.gvsig.gvsig3d.map3d.MapContext3D;
13
import org.gvsig.osgvp.core.osg.Vec3;
14
import org.gvsig.osgvp.exceptions.OSGVPException;
15
import org.gvsig.osgvp.exceptions.node.NodeException;
16
import org.gvsig.osgvp.features.SelectionRectangle;
17
import org.gvsig.osgvp.features.Polygon.PolygonType;
18
import org.gvsig.osgvp.planets.PlanetViewer;
19
import org.gvsig.osgvp.viewer.Intersections;
20

    
21
import com.iver.andami.messages.NotificationManager;
22
import com.iver.cit.gvsig.fmap.tools.BehaviorException;
23
import com.iver.cit.gvsig.fmap.tools.Behavior.RectangleBehavior;
24
import com.iver.cit.gvsig.fmap.tools.Events.RectangleEvent;
25
import com.iver.cit.gvsig.fmap.tools.Listeners.RectangleListener;
26

    
27
public class RectangleBehavior3D extends RectangleBehavior {
28

    
29
        private Point2D m_FirstPoint;
30
        private Point2D m_LastPoint;
31
        private RectangleListener listener;
32
        private SelectionRectangle rectangle;
33
        private int height;
34
        private int width;
35

    
36
        private static Logger logger = Logger.getLogger(GraphicLayer3D.class
37
                        .getName());
38

    
39
        public RectangleBehavior3D(RectangleListener zili) {
40
                super(zili);
41
                // TODO Auto-generated constructor stub
42
                listener = zili;
43
                try {
44
                        rectangle = new SelectionRectangle();
45
                } catch (OSGVPException e) {
46
                        logger.error("Command: "
47
                                        + "Error creating new selection rectangle.", e);
48
                }
49

    
50
        }
51

    
52
        @Override
53
        public void mouseDragged(MouseEvent e) {
54
                // TODO Auto-generated method stub
55
                // super.mouseDragged(e);
56
                // Painting
57
                rectangle.setMouseCoords((int) e.getPoint().getX(), height
58
                                - (int) e.getPoint().getY());
59
                try {
60
                        rectangle.update();
61
                } catch (OSGVPException e1) {
62
                        logger.error("Command: " + "Error updating rectangle selection.",
63
                                        e1);
64
                }
65

    
66
                rectangle.setType(PolygonType.FILLED_POLYGON);
67

    
68
        }
69

    
70
        @Override
71
        public void mousePressed(MouseEvent e) {
72
                // TODO Auto-generated method stub
73
                MapContext3D mcontext = (MapContext3D) getMapControl().getMapContext();
74
                try {
75
                        ((PlanetViewer) mcontext.getCanvas3d().getOSGViewer())
76
                                        .addNodeToHUD(rectangle);
77
                } catch (NodeException e1) {
78
                        logger.error("Command: "
79
                                        + "Error adding the rectangle selecton to the hud node.",
80
                                        e1);
81
                }
82
                height = mcontext.getCanvas3d().getHeight();
83
                width = mcontext.getCanvas3d().getWidth();
84

    
85
                if (e.getButton() == MouseEvent.BUTTON1) {
86
                        m_FirstPoint = e.getPoint();
87
                        // Put here the code to star drawing the rectangle
88
                        rectangle.setFirstPoint((int) m_FirstPoint.getX(), height
89
                                        - (int) m_FirstPoint.getY());
90
                        rectangle.setMouseCoords((int) m_FirstPoint.getX(), height
91
                                        - (int) m_FirstPoint.getY());
92

    
93
                        try {
94
                                rectangle.update();
95
                        } catch (OSGVPException e1) {
96
                                logger.error("Command: "
97
                                                + "Error updating rectangle selection.", e1);
98
                        }
99
                        rectangle.setType(PolygonType.EMPTY_POLYGON);
100
                }
101
        }
102

    
103
        @Override
104
        public void mouseReleased(MouseEvent e) throws BehaviorException {
105

    
106
                if (m_FirstPoint == null)
107
                        return;
108
                Point2D p1 = new Point2D.Double();
109
                Point2D p2 = new Point2D.Double();
110
                Point pScreen = e.getPoint();
111

    
112
                // ViewPort vp = getMapControl().getMapContext().getViewPort();
113

    
114
                // p1 = vp.toMapPoint(m_FirstPoint);
115
                // p2 = vp.toMapPoint(pScreen);
116
                Vec3 p1Vec = coordinatesIntersection(m_FirstPoint);
117
                p1.setLocation(p1Vec.x(), p1Vec.y());
118
                Vec3 p2Vec = coordinatesIntersection(pScreen);
119
                p2.setLocation(p2Vec.x(), p2Vec.y());
120

    
121
                if (e.getButton() == MouseEvent.BUTTON1) {
122
                        // Fijamos el nuevo extent
123
                        Rectangle2D.Double r = new Rectangle2D.Double();
124
                        r.setFrameFromDiagonal(p1, p2);
125

    
126
                        Rectangle2D rectPixel = new Rectangle();
127
                        rectPixel.setFrameFromDiagonal(m_FirstPoint, pScreen);
128

    
129
                        RectangleEvent event = new RectangleEvent(r, e, rectPixel);
130
                        listener.rectangle(event);
131

    
132
                }
133

    
134
                // Deleted rectangle
135
                rectangle.setMouseCoords((int) m_FirstPoint.getX(), height
136
                                - (int) m_FirstPoint.getY());
137
                try {
138
                        rectangle.update();
139
                } catch (OSGVPException e1) {
140
                        logger.error("Command: " + "Error updating rectangle selection.",
141
                                        e1);
142
                }
143
                rectangle.setType(PolygonType.EMPTY_POLYGON);
144

    
145
                m_FirstPoint = null;
146
                m_LastPoint = null;
147
        }
148

    
149
        @Override
150
        public void paintComponent(Graphics g) {
151
                // TODO Auto-generated method stub
152
                // super.paintComponent(g);
153
                MapContext3D mcontext = (MapContext3D) getMapControl().getMapContext();
154
                mcontext.getCanvas3d().repaint();
155
        }
156

    
157
        public Vec3 coordinatesIntersection(Point2D pScreen) {
158
                Vec3 intersection;
159

    
160
                // System.err.println("Coordenadas de pantalla " + pScreen.getX() + ","+
161
                // pScreen.getY());
162
                MapContext3D mcontext = (MapContext3D) getMapControl().getMapContext();
163
                Intersections hits = mcontext.getCanvas3d().getOSGViewer().rayPick(
164
                                (int) pScreen.getX(), (int) pScreen.getY());
165
                Point2D pWorld = new Point2D.Double();
166
                if (hits.containsIntersections()) {
167
                        // get XYZ coordinates on planet
168
                        Vec3 hit = hits.getFirstIntersection().getIntersectionPoint();
169
                        // convert to geo coordinates
170

    
171
                        // System.err.println("Interseccion de osg " + hit.x() + ","+
172
                        // hit.y());
173
                        if (mcontext.getProjection().getAbrev().compareToIgnoreCase(
174
                                        "EPSG:4326") == 0) {
175
                                Vec3 geoPt = mcontext.getPlanet()
176
                                                .convertXYZToLatLongHeight(hit);
177
                                // Swap the coordinates X and Y, because they are invert.
178
                                intersection = new Vec3(geoPt.y(), geoPt.x(), geoPt.z());
179
                        } else {
180
                                intersection = hit;
181
                        }
182
                } else {
183
                        if (mcontext.getProjection().getAbrev().compareToIgnoreCase(
184
                                        "EPSG:4326") == 0) {
185
                                pWorld.setLocation(360, 120);
186
                                intersection = new Vec3(360, 120, 0);
187
                        } else {
188
                                intersection = new Vec3(1e100, 1e100, 0);
189
                                // pWorld.setLocation(1e100, 1e100);
190
                        }
191
                }
192
                NotificationManager.addInfo("Obteniendo punto de informacion "
193
                                + intersection.x() + "    ,   " + intersection.y() + "  ,  "
194
                                + intersection.z());
195
                return intersection;
196
        }
197

    
198
}