Ticket #17332: lane-left-hand-traffic-issue.patch

File lane-left-hand-traffic-issue.patch, 11.3 KB (added by abhilekhsingh041992, 7 years ago)
  • src/org/openstreetmap/josm/plugins/turnlanes/gui/LaneGui.java

     
    311311    public Path recalculate(Path inner, Path2D innerLine) {
    312312        area.reset();
    313313
    314         final double W = getContainer().getLaneWidth();
     314        double W = road.getContainer().getModel().isLeftDirection() ? -getContainer().getLaneWidth() : getContainer().getLaneWidth();
     315
    315316        final double L = getLength();
    316317
    317318        final double WW = 3 / getContainer().getMpp();
  • src/org/openstreetmap/josm/plugins/turnlanes/gui/RoadGui.java

     
    543543    }
    544544
    545545    private Point2D getLeftCorner(Road.End end) {
    546         return getCorner(end, true);
     546        if(this.container.getModel().isLeftDirection()) {
     547            return getCorner(end, false);
     548        } else {
     549            return getCorner(end, true);
     550        }
    547551    }
    548552
    549553    private Point2D getRightCorner(Road.End end) {
    550         return getCorner(end, false);
     554        if(this.container.getModel().isLeftDirection()) {
     555            return getCorner(end, true);
     556        } else {
     557            return getCorner(end, false);
     558        }
    551559    }
    552560
    553561    private Point2D getCorner(Road.End end, boolean left) {
    554562        final JunctionGui j = end.isFromEnd() ? a : b;
    555         final double w = left ? getWidth(end, true) : getWidth(end, false);
     563
     564        double w = left ? getWidth(end, false) : getWidth(end, true);
     565        if(!this.container.getModel().isLeftDirection()) {
     566            w = left ? getWidth(end, true) : getWidth(end, false);
     567        }
     568
    556569        final double s = (left ? 1 : -1);
    557570        final double a = getAngle(end) + PI;
    558571        final double t = left ? j.getLeftTrim(end) : j.getRightTrim(end);
     
    597610
    598611        g2d.setColor(Color.RED);
    599612        for (Segment s : segments) {
    600             g2d.fill(new Ellipse2D.Double(s.from.getX() - 1, s.from.getY() - 1, 2, 2));
     613            g2d.fill(new Ellipse2D.Double(s.from.getX() - 0.5, s.from.getY() - 0.5, 1, 1));
    601614        }
    602615
    603616        return result;
     
    667680
    668681            middleArea = new Path2D.Double();
    669682            middleArea.append(middleLines.getPathIterator(null), false);
    670             middleArea.append(middlePath(backward).offset(outerMargin, -1, -1, outerMargin).getIterator(), true);
     683            if(this.container.getModel().isLeftDirection()) {
     684                middleArea.append(middlePath(backward).offset(-outerMargin, -1, -1, -outerMargin).getIterator(), true);
     685            } else {
     686                middleArea.append(middlePath(backward).offset(outerMargin, -1, -1, outerMargin).getIterator(), true);
     687            }
    671688            middleArea.closePath();
    672689            g2d.setColor(Color.GRAY);
    673690        } else {
     
    706723
    707724        final Path middle = middlePath(forward);
    708725
    709         Path innerPath = middle.offset(innerMargin, -1, -1, innerMargin);
     726        Path innerPath;
     727        if(this.container.getModel().isLeftDirection()) {
     728            innerPath = middle.offset(-innerMargin, -1, -1, -innerMargin);
     729        } else {
     730            innerPath = middle.offset(innerMargin, -1, -1, innerMargin);
     731        }
     732
    710733        final List<Path> linePaths = new ArrayList<>();
    711734        linePaths.add(innerPath);
    712735
     
    717740        }
    718741
    719742        final Path2D area = new Path2D.Double();
    720         area(area, middle, innerPath.offset(outerMargin, -1, -1, outerMargin));
     743        if(this.container.getModel().isLeftDirection()) {
     744            area(area, middle, innerPath.offset(-outerMargin, -1, -1, -outerMargin));
     745        } else {
     746            area(area, middle, innerPath.offset(outerMargin, -1, -1, outerMargin));
     747        }
    721748        g2d.setColor(Color.GRAY);
    722749        g2d.fill(area);
    723750
     
    785812
    786813    public Path getLaneMiddle(boolean forward) {
    787814        final Path mid = middlePath(!forward);
    788         final double w = getWidth(forward ? getModel().getFromEnd() : getModel().getToEnd(), true);
    789         final double o = (w - outerMargin) / 2;
    790815
     816        double w = getWidth(forward ? getModel().getFromEnd() : getModel().getToEnd(), true);
     817        double o = (w + outerMargin) / 2;
     818        if(this.container.getModel().isLeftDirection()) {
     819            w = -getWidth(forward ? getModel().getFromEnd() : getModel().getToEnd(), false);
     820            o = (w + (-outerMargin)) / 2;
     821        }
     822
    791823        return o > 0 ? mid.offset(-o, -1, -1, -o) : mid;
    792824    }
    793825
  • src/org/openstreetmap/josm/plugins/turnlanes/gui/TurnLanesDialog.java

     
    66import java.awt.BorderLayout;
    77import java.awt.CardLayout;
    88import java.awt.GridLayout;
     9import java.awt.GridBagConstraints;
    910import java.awt.event.ActionEvent;
    1011import java.util.Collection;
    1112import java.util.HashSet;
     
    1415import javax.swing.ButtonGroup;
    1516import javax.swing.JPanel;
    1617import javax.swing.JToggleButton;
     18import javax.swing.JLabel;
     19import javax.swing.JCheckBox;
    1720
    1821import org.openstreetmap.josm.actions.JosmAction;
    1922import org.openstreetmap.josm.data.osm.DataSelectionListener;
     
    131134    private static final String CARD_VALIDATE = "VALIDATE";
    132135
    133136    private final JPanel body = new JPanel();
     137
     138
    134139    private final JunctionPane junctionPane = new JunctionPane(GuiContainer.empty());
    135140
    136141    private final JToggleButton editButton = new JToggleButton(editAction);
     
    141146    private boolean editing = true;
    142147    private boolean wasShowing = false;
    143148
     149    private ModelContainer modelContainer;
     150    private boolean leftDirection = ModelContainer.empty().isLeftDirection();
     151
    144152    public TurnLanesDialog() {
    145153        super(tr("Turn Lanes"), "turnlanes.png", tr("Edit turn lanes"), null, 200);
    146154
     
    151159        final ButtonGroup group = new ButtonGroup();
    152160        group.add(editButton);
    153161        group.add(validateButton);
     162        addTrafficDirectionCheckBox(buttonPanel);
    154163        buttonPanel.add(editButton);
    155164        buttonPanel.add(validateButton);
    156165
     
    165174        editButton.doClick();
    166175    }
    167176
     177    /**
     178     * Add label and checkbox for traffic direction and change flag
     179     * @param buttonPanel
     180     */
     181    private void addTrafficDirectionCheckBox(JPanel buttonPanel) {
     182        GridBagConstraints constraints = new GridBagConstraints();
     183        JLabel aoiLabel = new JLabel(tr("Left-hand traffic direction:"));
     184        constraints.gridx = 0;
     185        constraints.gridwidth = 1; //next-to-last
     186        constraints.fill = GridBagConstraints.NONE;      //reset to default
     187        constraints.weightx = 0.0;
     188        buttonPanel.add(aoiLabel, constraints);
     189
     190        constraints.gridx = 1;
     191        constraints.gridwidth = GridBagConstraints.REMAINDER;     //end row
     192        constraints.fill = GridBagConstraints.HORIZONTAL;
     193        constraints.weightx = 1.0;
     194        JCheckBox leftDirectionCheckbox = new JCheckBox();
     195        leftDirectionCheckbox.setSelected(leftDirection);
     196        buttonPanel.add(leftDirectionCheckbox, constraints);
     197        leftDirectionCheckbox.addChangeListener(e -> {
     198            if(modelContainer == null) {
     199                return;
     200            }
     201            leftDirection = leftDirectionCheckbox.isSelected();
     202            refresh();
     203        });
     204
     205    }
     206
    168207    @Override
    169208    protected void stateChanged() {
    170209        if (isShowing && !wasShowing) {
     
    178217            final Collection<Node> nodes = org.openstreetmap.josm.tools.Utils.filteredCollection(selected, Node.class);
    179218            final Collection<Way> ways = org.openstreetmap.josm.tools.Utils.filteredCollection(selected, Way.class);
    180219
    181             final ModelContainer mc = nodes.isEmpty() ? ModelContainer.empty() : ModelContainer
     220            modelContainer = nodes.isEmpty() ? ModelContainer.empty() : ModelContainer
    182221                    .createEmpty(nodes, ways);
     222            modelContainer.setLeftDirection(leftDirection);
    183223
    184             junctionPane.setJunction(new GuiContainer(mc));
     224            junctionPane.setJunction(new GuiContainer(modelContainer));
    185225        }
    186226    }
    187227
  • src/org/openstreetmap/josm/plugins/turnlanes/model/ModelContainer.java

     
    2020import org.openstreetmap.josm.tools.Pair;
    2121
    2222public final class ModelContainer {
     23
     24    //set default direction of traffic for connecting lanes
     25    private static final boolean DEFAULT_LEFT_DIRECTION = false;
     26
    2327    private static final ModelContainer EMPTY = new ModelContainer(Collections.<Node>emptySet(),
    24             Collections.<Way>emptySet(), false);
     28            Collections.<Way>emptySet(), false, DEFAULT_LEFT_DIRECTION);
    2529
    2630    public static ModelContainer create(Iterable<Node> primaryNodes, Iterable<Way> primaryWays) {
    2731        return new ModelContainer(new HashSet<>(CollectionUtils.toList(primaryNodes)), new HashSet<>(
    28                 CollectionUtils.toList(primaryWays)), false);
     32                CollectionUtils.toList(primaryWays)), false, DEFAULT_LEFT_DIRECTION);
    2933    }
    3034
    3135    public static ModelContainer createEmpty(Iterable<Node> primaryNodes, Iterable<Way> primaryWays) {
    3236        return new ModelContainer(new HashSet<>(CollectionUtils.toList(primaryNodes)), new HashSet<>(
    33                 CollectionUtils.toList(primaryWays)), true);
     37                CollectionUtils.toList(primaryWays)), true, DEFAULT_LEFT_DIRECTION);
    3438    }
    3539
    3640    public static ModelContainer empty() {
     
    121125
    122126    private final boolean empty;
    123127
    124     private ModelContainer(Set<Node> primaryNodes, Set<Way> primaryWays, boolean empty) {
     128    private boolean leftDirection;
     129
     130    private ModelContainer(Set<Node> primaryNodes, Set<Way> primaryWays, boolean empty, boolean leftDirection) {
    125131        if (empty) {
    126132            this.primaryNodes = Collections.unmodifiableSet(new HashSet<>(primaryNodes));
    127133            this.primaryWays = Collections.unmodifiableSet(new HashSet<>(primaryWays));
     
    151157
    152158            this.empty = junctions.isEmpty();
    153159        }
     160        this.leftDirection = leftDirection;
    154161    }
    155162
    156163    private Set<Pair<Way, Junction>> createPrimaryJunctions() {
     
    282289    }
    283290
    284291    public ModelContainer recalculate() {
    285         return new ModelContainer(primaryNodes, primaryWays, false);
     292        return new ModelContainer(primaryNodes, primaryWays, false, leftDirection);
    286293    }
    287294
    288295    public boolean isPrimary(Junction j) {
     
    297304        return empty;
    298305    }
    299306
     307    public boolean isLeftDirection() {
     308        return leftDirection;
     309    }
     310
     311    public void setLeftDirection(boolean leftDirection) {
     312        this.leftDirection = leftDirection;
     313    }
     314
    300315    public boolean hasRoad(Way w) {
    301316        return roads.containsKey(w);
    302317    }