Changeset 5098 in josm for trunk


Ignore:
Timestamp:
2012-03-18T11:42:54+01:00 (8 years ago)
Author:
xeen
Message:

Reduce repaints required when in draw mode. This should improve performance a bit with target highlighting activated. See #7503

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/mapmode/DrawAction.java

    r5053 r5098  
    22package org.openstreetmap.josm.actions.mapmode;
    33
    4 import javax.swing.JCheckBoxMenuItem;
    5 import javax.swing.JMenuItem;
     4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
     5import static org.openstreetmap.josm.tools.I18n.marktr;
    66import static org.openstreetmap.josm.tools.I18n.tr;
    77import static org.openstreetmap.josm.tools.I18n.trn;
    8 import static org.openstreetmap.josm.tools.I18n.marktr;
    9 import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
    108
    119import java.awt.AWTEvent;
     
    1412import java.awt.Cursor;
    1513import java.awt.Graphics2D;
    16 import java.awt.MenuItem;
    1714import java.awt.Point;
    1815import java.awt.Stroke;
     
    3835import java.util.Set;
    3936import java.util.TreeSet;
     37
    4038import javax.swing.AbstractAction;
     39import javax.swing.JCheckBoxMenuItem;
     40import javax.swing.JMenuItem;
    4141import javax.swing.JOptionPane;
    4242import javax.swing.JPopupMenu;
     
    6666import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    6767import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher;
     68import org.openstreetmap.josm.tools.Geometry;
    6869import org.openstreetmap.josm.tools.ImageProvider;
    6970import org.openstreetmap.josm.tools.Pair;
    7071import org.openstreetmap.josm.tools.Shortcut;
    7172import org.openstreetmap.josm.tools.Utils;
    72 import org.openstreetmap.josm.tools.Geometry;
    7373
    7474/**
     
    8484    private Node mouseOnExistingNode;
    8585    private Set<Way> mouseOnExistingWays = new HashSet<Way>();
     86    // old highlights store which primitives are currently highlighted. This
     87    // is true, even if target highlighting is disabled since the status bar
     88    // derives its information from this list as well.
    8689    private Set<OsmPrimitive> oldHighlights = new HashSet<OsmPrimitive>();
     90    // new highlights contains a list of primitives that should be highlighted
     91    // but haven’t been so far. The idea is to compare old and new and only
     92    // repaint if there are changes.
     93    private Set<OsmPrimitive> newHighlights = new HashSet<OsmPrimitive>();
    8794    private boolean drawHelperLine;
    8895    private boolean wayIsFinished = false;
     
    132139    private void redrawIfRequired() {
    133140        updateStatusLine();
     141        // repaint required if the helper line is active.
     142        boolean needsRepaint = drawHelperLine && !wayIsFinished;
     143        // move newHighlights to oldHighlights; only update changed primitives
     144        for(OsmPrimitive x : newHighlights) {
     145            if(oldHighlights.contains(x)) {
     146                continue;
     147            }
     148            needsRepaint = true;
     149            x.setHighlighted(true);
     150        }
     151        oldHighlights.removeAll(newHighlights);
     152        for(OsmPrimitive x : oldHighlights) {
     153            x.setHighlighted(false);
     154            needsRepaint = true;
     155        }
     156        oldHighlights = newHighlights;
     157
    134158        if ((!drawHelperLine || wayIsFinished) && !drawTargetHighlight)
    135159            return;
     160
    136161        // update selection to reflect which way being modified
    137162        if (currentBaseNode != null && getCurrentDataSet().getSelected().isEmpty() == false) {
    138163            Way continueFrom = getWayForNode(currentBaseNode);
    139             if (alt && continueFrom != null) {
     164            if (alt && continueFrom != null && (!currentBaseNode.isSelected() || continueFrom.isSelected())) {
    140165                getCurrentDataSet().beginUpdate(); // to prevent the selection listener to screw around with the state
    141166                getCurrentDataSet().addSelected(currentBaseNode);
    142167                getCurrentDataSet().clearSelection(continueFrom);
    143168                getCurrentDataSet().endUpdate();
    144             } else if (!alt && continueFrom != null) {
     169                needsRepaint = true;
     170            } else if (!alt && continueFrom != null && !continueFrom.isSelected()) {
    145171                getCurrentDataSet().addSelected(continueFrom);
    146             }
    147         }
    148         Main.map.mapView.repaint();
     172                needsRepaint = true;
     173            }
     174        }
     175
     176        if(needsRepaint) {
     177            Main.map.mapView.repaint();
     178        }
    149179    }
    150180
     
    228258        computeHelperLine();
    229259        addHighlighting();
    230         redrawIfRequired();
    231260    }
    232261
     
    278307        computeHelperLine();
    279308        addHighlighting();
    280         redrawIfRequired();
    281309    }
    282310
     
    303331        computeHelperLine();
    304332        removeHighlighting();
    305         redrawIfRequired();
    306333    }
    307334
     
    579606        computeHelperLine();
    580607        removeHighlighting();
    581         redrawIfRequired();
    582608    }
    583609
     
    707733        computeHelperLine();
    708734        addHighlighting();
    709         redrawIfRequired();
    710735    }
    711736
     
    723748            return;
    724749        }
    725 
    726         double distance = -1;
    727         double angle = -1;
    728750
    729751        Collection<OsmPrimitive> selection = getCurrentDataSet().getSelected();
     
    972994    /**
    973995     * Takes the data from computeHelperLine to determine which ways/nodes should be highlighted
    974      * (if feature enabled). Also sets the target cursor if appropriate.
     996     * (if feature enabled). Also sets the target cursor if appropriate. It adds the to-be-
     997     * highlighted primitives to newHighlights but does not actually highlight them. This work is
     998     * done in redrawIfRequired. This means, calling addHighlighting() without redrawIfRequired()
     999     * will leave the data in an inconsistent state.
     1000     *
     1001     * The status bar derives its information from oldHighlights, so in order to update the status
     1002     * bar both addHighlighting() and repaintIfRequired() are needed, since former fills newHighlights
     1003     * and latter processes them into oldHighlights.
    9751004     */
    9761005    private void addHighlighting() {
    977         removeHighlighting();
     1006        newHighlights = new HashSet<OsmPrimitive>();
     1007
    9781008        // if ctrl key is held ("no join"), don't highlight anything
    9791009        if (ctrl) {
    9801010            Main.map.mapView.setNewCursor(cursor, this);
     1011            redrawIfRequired();
    9811012            return;
    9821013        }
     
    9901021        if (mouseOnExistingNode != null) {
    9911022            Main.map.mapView.setNewCursor(cursorJoinNode, this);
    992             // We also need this list for the statusbar help text
    993             oldHighlights.add(mouseOnExistingNode);
    994             if(drawTargetHighlight) {
    995                 mouseOnExistingNode.setHighlighted(true);
    996         }
     1023            newHighlights.add(mouseOnExistingNode);
     1024            redrawIfRequired();
    9971025            return;
    9981026        }
     
    10011029        if (mouseOnExistingWays.size() == 0) {
    10021030            Main.map.mapView.setNewCursor(cursor, this);
     1031            redrawIfRequired();
    10031032            return;
    10041033        }
    10051034
    10061035        Main.map.mapView.setNewCursor(cursorJoinWay, this);
    1007 
    1008         // We also need this list for the statusbar help text
    1009         oldHighlights.addAll(mouseOnExistingWays);
    1010         if (!drawTargetHighlight) return;
    1011         for (Way w : mouseOnExistingWays) {
    1012             w.setHighlighted(true);
    1013         }
     1036        newHighlights.addAll(mouseOnExistingWays);
     1037        redrawIfRequired();
    10141038    }
    10151039
    10161040    /**
    1017      * Removes target highlighting from primitives
     1041     * Removes target highlighting from primitives. Issues repaint if required.
    10181042     */
    10191043    private void removeHighlighting() {
    1020         for(OsmPrimitive prim : oldHighlights) {
    1021             prim.setHighlighted(false);
    1022         }
    1023         oldHighlights = new HashSet<OsmPrimitive>();
     1044        newHighlights = new HashSet<OsmPrimitive>();
     1045        redrawIfRequired();
    10241046    }
    10251047
     
    10411063            g2.setColor(selectedColor);
    10421064            g2.setStroke(new BasicStroke(3, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
    1043         } else if (!snapHelper.drawConstructionGeometry) {
    1044             return;
    1045         }
     1065        } else if (!snapHelper.drawConstructionGeometry)
     1066            return;
    10461067        GeneralPath b = new GeneralPath();
    10471068        Point p1=mv.getPoint(currentBaseNode);
     
    11691190                        wayIsFinished=false;
    11701191                    }  else {
    1171                     // if more than 1 node were affected by previous command,
    1172                     // we have no way to continue, so we forget about found node
     1192                        // if more than 1 node were affected by previous command,
     1193                        // we have no way to continue, so we forget about found node
    11731194                        n=null;
    11741195                        break;
     
    11781199            // select last added node - maybe we will continue drawing from it
    11791200            if (n!=null) getCurrentDataSet().addSelected(n);
    1180        }
     1201        }
    11811202    }
    11821203
     
    16161637        @Override
    16171638        public void actionPerformed(ActionEvent e) {
    1618                if (snapHelper!=null) snapHelper.toggleSnapping();
     1639            if (snapHelper!=null) snapHelper.toggleSnapping();
    16191640        }
    16201641    }
Note: See TracChangeset for help on using the changeset viewer.