Ignore:
Timestamp:
2005-10-03T04:18:02+02:00 (19 years ago)
Author:
imi
Message:
  • added Selection Dialog
  • added support for graphic engines with a better default engine
  • reorganized data classes with back references
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/org/openstreetmap/josm/gui/MapView.java

    r7 r8  
    11package org.openstreetmap.josm.gui;
    22
    3 import java.awt.Color;
    43import java.awt.Graphics;
    54import java.awt.Point;
     
    87import java.awt.event.ComponentListener;
    98import java.awt.event.KeyEvent;
    10 import java.util.HashSet;
    11 import java.util.LinkedList;
    12 import java.util.List;
    13 import java.util.Set;
    149
    1510import javax.swing.AbstractAction;
     
    2722import org.openstreetmap.josm.data.osm.Track;
    2823import org.openstreetmap.josm.data.projection.Projection;
     24import org.openstreetmap.josm.gui.engine.Engine;
     25import org.openstreetmap.josm.gui.engine.SimpleEngine;
    2926
    3027/**
     
    8178        public final DataSet dataSet;
    8279
     80        /**
     81         * The drawing engine.
     82         */
     83        private Engine engine;
    8384       
    8485        /**
     
    9495                // initialize the projection
    9596                setProjection(Main.pref.projection.clone());
     97               
     98                // initialize the engine
     99                engine = new SimpleEngine(this);
    96100        }
    97101
     
    136140                        projection.latlon2xy(p);
    137141                }
    138                 return new Point(toScreenX(p.x), toScreenY(p.y));
     142                int x = ((int)Math.round((p.x-center.x) / scale + getWidth()/2));
     143                int y = ((int)Math.round((center.y-p.y) / scale + getHeight()/2));
     144                return new Point(x,y);
    139145        }
    140146
     
    179185               
    180186                // pending line segments
    181                 for (LineSegment ls : dataSet.pendingLineSegments) {
    182                         Point A = getScreenPoint(ls.start.coor);
    183                         Point B = getScreenPoint(ls.end.coor);
     187                for (LineSegment ls : dataSet.pendingLineSegments()) {
     188                        Point A = getScreenPoint(ls.getStart().coor);
     189                        Point B = getScreenPoint(ls.getEnd().coor);
    184190                        double c = A.distanceSq(B);
    185191                        double a = p.distanceSq(B);
     
    194200                // tracks & line segments
    195201                minDistanceSq = Double.MAX_VALUE;
    196                 for (Track t : dataSet.tracks) {
    197                         for (LineSegment ls : t.segments) {
    198                                 Point A = getScreenPoint(ls.start.coor);
    199                                 Point B = getScreenPoint(ls.end.coor);
     202                for (Track t : dataSet.tracks()) {
     203                        for (LineSegment ls : t.segments()) {
     204                                Point A = getScreenPoint(ls.getStart().coor);
     205                                Point B = getScreenPoint(ls.getEnd().coor);
    200206                                double c = A.distanceSq(B);
    201207                                double a = p.distanceSq(B);
     
    213219                // TODO areas
    214220               
    215                
    216221                return null; // nothing found
    217222        }
     
    224229         */
    225230        public void zoomTo(GeoPoint newCenter, double scale) {
     231                boolean oldAutoScale = autoScale;
     232                GeoPoint oldCenter = center;
     233                double oldScale = this.scale;
     234               
    226235                autoScale = false;
    227236                center = newCenter.clone();
     
    229238                this.scale = scale;
    230239                recalculateCenterScale();
    231                 fireStateChanged(); // in case of autoScale, recalculate fired.
     240
     241                firePropertyChange("center", oldCenter, center);
     242                if (oldAutoScale != autoScale)
     243                        firePropertyChange("autoScale", oldAutoScale, autoScale);
     244                if (oldScale != scale)
     245                        firePropertyChange("scale", oldScale, scale);
    232246        }
    233247       
     
    254268                                h = 20;
    255269                        Bounds bounds = dataSet.getBoundsXY();
     270                       
     271                        boolean oldAutoScale = autoScale;
     272                        GeoPoint oldCenter = center;
     273                        double oldScale = this.scale;
     274                       
    256275                        if (bounds == null) {
    257276                                // no bounds means standard scale and center
     
    266285                                scale = Math.max(scaleX, scaleY); // minimum scale to see all of the screen
    267286                        }
    268                         fireStateChanged();
     287
     288                        firePropertyChange("center", oldCenter, center);
     289                        if (oldAutoScale != autoScale)
     290                                firePropertyChange("autoScale", oldAutoScale, autoScale);
     291                        if (oldScale != scale)
     292                                firePropertyChange("scale", oldScale, scale);
    269293                }
    270294                repaint();
     
    283307        @Override
    284308        public void paint(Graphics g) {
    285                 // empty out everything
    286                 g.setColor(Color.BLACK);
    287                 g.fillRect(0,0,getWidth(),getHeight());
    288 
    289                 // draw tracks
    290                 if (dataSet.tracks != null)
    291                         for (Track track : dataSet.tracks)
    292                                 for (LineSegment ls : track.segments) {
    293                                         g.setColor(ls.selected || track.selected ? Color.WHITE : Color.GRAY);
    294                                         g.drawLine(toScreenX(ls.start.coor.x), toScreenY(ls.start.coor.y),
    295                                                         toScreenX(ls.end.coor.x), toScreenY(ls.end.coor.y));
    296                                 }
    297 
    298                 // draw pending line segments
    299                 for (LineSegment ls : dataSet.pendingLineSegments) {
    300                         g.setColor(ls.selected ? Color.WHITE : Color.LIGHT_GRAY);
    301                         g.drawLine(toScreenX(ls.start.coor.x), toScreenY(ls.start.coor.y),
    302                                         toScreenX(ls.end.coor.x), toScreenY(ls.end.coor.y));
    303                 }
    304 
    305                 // draw nodes
    306                 Set<Integer> alreadyDrawn = new HashSet<Integer>();
    307                 int width = getWidth();
    308                 for (Node w : dataSet.nodes) {
    309                         g.setColor(w.selected ? Color.WHITE : Color.RED);
    310                         int x = toScreenX(w.coor.x);
    311                         int y = toScreenY(w.coor.y);
    312                         int size = 3;
    313                         if (alreadyDrawn.contains(y*width+x)) {
    314                                 size = 7;
    315                                 x -= 2;
    316                                 y -= 2;
    317                         } else
    318                                 alreadyDrawn.add(y*width+x);
    319                         g.drawArc(x, y, size, size, 0, 360);
    320                 }
    321         }
    322 
    323         /**
    324          * Return the x-screen coordinate for the given point.
    325          * @param x The X-position (easting) of the desired point
    326          * @return The screen coordinate for the point.
    327          */
    328         public int toScreenX(double x) {
    329                 return (int)Math.round((x-center.x) / scale + getWidth()/2);
    330         }
    331 
    332         /**
    333          * Return the y-screen coordinate for the given point.
    334          * @param y The Y-position (northing) of the desired point
    335          * @return The screen coordinate for the point.
    336          */
    337         public int toScreenY(double y) {
    338                 return (int)Math.round((center.y-y) / scale + getHeight()/2);
    339         }
    340 
    341 
    342         // Event handling functions and data
    343        
    344         /**
    345          * The event list with all state chaned listener
    346          */
    347         List<ChangeListener> listener = new LinkedList<ChangeListener>();
    348         /**
    349          * Add an event listener to the state changed event queue. If passed
    350          * <code>null</code>, nothing happens.
    351          */
    352         public final void addChangeListener(ChangeListener l) {
    353                 if (l != null)
    354                         listener.add(l);
    355         }
    356         /**
    357          * Remove an event listener from the event queue. If passed
    358          * <code>null</code>, nothing happens.
    359          */
    360         public final void removeChangeListener(ChangeListener l) {
    361                 listener.remove(l);
    362         }
    363         /**
    364          * Fires an ChangeEvent. Occours, when an non-data value changed, as example
    365          * the autoScale - state or the center. Is not fired, dataSet internal things
    366          * changes.
    367          */
    368         public final void fireStateChanged() {
    369                 ChangeEvent e = null;
    370                 for(ChangeListener l : listener) {
    371                         if (e == null)
    372                                 e = new ChangeEvent(this);
    373                         l.stateChanged(e);
    374                 }
    375         }
    376        
     309                engine.init(g);
     310                engine.drawBackground(getPoint(0,0,true), getPoint(getWidth(), getHeight(), true));
     311
     312                for (Track t : dataSet.tracks())
     313                        engine.drawTrack(t);
     314                for (LineSegment ls : dataSet.pendingLineSegments())
     315                        engine.drawPendingLineSegment(ls);
     316                for (Node n : dataSet.nodes)
     317                        engine.drawNode(n);
     318        }
     319
    377320        /**
    378321         * Notify from the projection, that something has changed.
     
    393336                if (projection == this.projection)
    394337                        return;
     338
     339                Projection oldProjection = this.projection;
     340               
    395341                if (this.projection != null)
    396342                        this.projection.removeChangeListener(this);
    397343                this.projection = projection;
    398344                projection.addChangeListener(this);
     345               
    399346                stateChanged(new ChangeEvent(this));
     347                firePropertyChange("projection", oldProjection, projection);
    400348        }
    401349
     
    421369                if (this.autoScale != autoScale) {
    422370                        this.autoScale = autoScale;
    423                         fireStateChanged();
     371                        firePropertyChange("autoScale", !autoScale, autoScale);
    424372                }
    425373        }
Note: See TracChangeset for help on using the changeset viewer.