Changeset 315 in josm for src


Ignore:
Timestamp:
2007-09-03T00:24:58+02:00 (18 years ago)
Author:
framm
Message:
  • rendering performance improvement by reading preferences only once per render; patch supplied by Brent Easton <b.easton@…>
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/org/openstreetmap/josm/data/osm/visitor/SimplePaintVisitor.java

    r298 r315  
    44import java.awt.Color;
    55import java.awt.Graphics;
     6import java.awt.Graphics2D;
    67import java.awt.Point;
     8import java.awt.Polygon;
    79import java.awt.Rectangle;
     10import java.awt.geom.GeneralPath;
    811import java.awt.geom.Line2D;
    912
     
    2831        public final static Color darkblue = new Color(0,0,128);
    2932        public final static Color darkgreen = new Color(0,128,0);
    30 
     33       
    3134        /**
    3235         * The environment to paint to.
     
    3942       
    4043        public boolean inactive;
    41 
     44       
    4245        protected static final double PHI = Math.toRadians(20);
    4346
     47        /**
     48         * Preferences
     49         */
     50        protected Color inactiveColor;
     51        protected Color selectedColor;
     52        protected Color nodeColor;
     53        protected Color segmentColor;
     54        protected Color dfltWayColor;
     55        protected Color incompleteColor;
     56        protected Color backgroundColor;
     57        protected boolean showDirectionArrow;
     58        protected boolean showOrderNumber;
     59       
     60        /**
     61         * Draw subsequent segments of same color as one Path
     62         */
     63        protected Color currentColor = null;
     64        protected GeneralPath currrentPath = new GeneralPath();
     65       
    4466        public void visitAll(DataSet data) {
     67                inactiveColor = getPreferencesColor("inactive", Color.DARK_GRAY);
     68                selectedColor = getPreferencesColor("selected", Color.WHITE);
     69                nodeColor = getPreferencesColor("node", Color.RED);
     70                segmentColor = getPreferencesColor("segment", darkgreen);
     71                dfltWayColor = getPreferencesColor("way", darkblue);
     72                incompleteColor = getPreferencesColor("incomplete way", darkerblue);
     73                backgroundColor = getPreferencesColor("background", Color.BLACK);
     74                showDirectionArrow = Main.pref.getBoolean("draw.segment.direction");
     75                showOrderNumber = Main.pref.getBoolean("draw.segment.order_number");
     76               
    4577                for (final OsmPrimitive osm : data.segments)
    4678                        if (!osm.deleted && !osm.selected)
     
    4981                        if (!osm.deleted && !osm.selected)
    5082                                osm.visit(this);
     83                displaySegments(null);  // Flush segment cache before nodes
    5184                for (final OsmPrimitive osm : data.nodes)
    5285                        if (!osm.deleted && !osm.selected)
     
    5588                        if (!osm.deleted)
    5689                                osm.visit(this);
     90                displaySegments(null);
    5791        }
    5892
     
    66100                Color color = null;
    67101                if (inactive)
    68                         color = getPreferencesColor("inactive", Color.DARK_GRAY);
     102                        color = inactiveColor;
    69103                else if (n.selected)
    70                         color = getPreferencesColor("selected", Color.WHITE);
     104                        color = selectedColor;
    71105                else
    72                         color = getPreferencesColor("node", Color.RED);
     106                        color = nodeColor;
    73107                drawNode(n, color);
    74108        }
     
    81115                Color color;
    82116                if (inactive)
    83                         color = getPreferencesColor("inactive", Color.DARK_GRAY);
     117                        color = inactiveColor;
    84118                else if (ls.selected)
    85                         color = getPreferencesColor("selected", Color.WHITE);
     119                        color = selectedColor;
    86120                else
    87                         color = getPreferencesColor("segment", darkgreen);
    88                 drawSegment(ls, color, Main.pref.getBoolean("draw.segment.direction"));
     121                        color = segmentColor;
     122                drawSegment(ls, color, showDirectionArrow);
    89123        }
    90124
     
    96130                Color wayColor;
    97131                if (inactive)
    98                         wayColor = getPreferencesColor("inactive", Color.DARK_GRAY);
     132                        wayColor = inactiveColor;
    99133                else {
    100                         wayColor = getPreferencesColor("way", darkblue);
     134                        wayColor = dfltWayColor;
    101135                        for (Segment ls : w.segments) {
    102136                                if (ls.incomplete) {
    103                                         wayColor = getPreferencesColor("incomplete way", darkerblue);
     137                                        wayColor = incompleteColor;
    104138                                        break;
    105139                                }
     
    107141                }
    108142
    109                 boolean showDirectionArrow = Main.pref.getBoolean("draw.segment.direction");
    110                 boolean showOrderNumber = Main.pref.getBoolean("draw.segment.order_number");
    111143                int orderNumber = 0;
    112144                for (Segment ls : w.segments) {
    113145                        orderNumber++;
    114146                        if (!ls.selected) // selected already in good color
    115                                 drawSegment(ls, w.selected && !inactive ? getPreferencesColor("selected", Color.WHITE) : wayColor, showDirectionArrow);
     147                                drawSegment(ls, w.selected && !inactive ? selectedColor : wayColor, showDirectionArrow);
    116148                        if (!ls.incomplete && showOrderNumber)
    117149                                drawOrderNumber(ls, orderNumber);
     
    132164                if (screen.contains(x,y)) {
    133165                        Color c = g.getColor();
    134                         g.setColor(getPreferencesColor("background", Color.BLACK));
     166                        g.setColor(backgroundColor);
    135167                        g.fillRect(x-1, y-12, 8*strlen+1, 14);
    136168                        g.setColor(c);
     
    160192                if (ls.incomplete)
    161193                        return;
    162                 g.setColor(col);
     194                if (col != currentColor) {
     195                        displaySegments(col);
     196                }
     197               
    163198                Point p1 = nc.getPoint(ls.from.eastNorth);
    164199                Point p2 = nc.getPoint(ls.to.eastNorth);
    165200               
    166                 Rectangle screen = g.getClipBounds();
     201                Rectangle screen = g.getClipBounds();           
    167202                Line2D line = new Line2D.Double(p1.x, p1.y, p2.x, p2.y);
    168203                if (screen.contains(p1.x, p1.y, p2.x, p2.y) || screen.intersectsLine(line))
    169204                {
    170                         g.drawLine(p1.x, p1.y, p2.x, p2.y);
     205                        currrentPath.moveTo(p1.x, p1.y);
     206                        currrentPath.lineTo(p2.x, p2.y);
    171207       
    172208                        if (showDirection) {
    173209                                double t = Math.atan2(p2.y-p1.y, p2.x-p1.x) + Math.PI;
    174                         g.drawLine(p2.x,p2.y, (int)(p2.x + 10*Math.cos(t-PHI)), (int)(p2.y + 10*Math.sin(t-PHI)));
    175                         g.drawLine(p2.x,p2.y, (int)(p2.x + 10*Math.cos(t+PHI)), (int)(p2.y + 10*Math.sin(t+PHI)));
    176                         }
     210                                currrentPath.lineTo((int)(p2.x + 10*Math.cos(t-PHI)), (int)(p2.y + 10*Math.sin(t-PHI)));
     211                                currrentPath.moveTo((int)(p2.x + 10*Math.cos(t+PHI)), (int)(p2.y + 10*Math.sin(t+PHI)));
     212                                currrentPath.lineTo(p2.x, p2.y);                        }
     213                }
     214        }
     215       
     216        protected void displaySegments(Color newColor) {
     217                if (currrentPath != null) {
     218                        g.setColor(currentColor);
     219                        ((Graphics2D) g).draw(currrentPath);
     220                        currrentPath = new GeneralPath();
     221                        currentColor = newColor;
    177222                }
    178223        }
Note: See TracChangeset for help on using the changeset viewer.