[Pathfinding] Add "place wall" action

This commit is contained in:
Thibaud Gasser 2018-09-30 15:25:52 +02:00
parent c69a9ab56f
commit 937e72dfcc
3 changed files with 32 additions and 16 deletions

View File

@ -7,27 +7,18 @@ public class Cell {
private final Point pos; private final Point pos;
private final JLabel content; private final JLabel content;
private boolean selected;
private boolean wall; private boolean wall;
public Cell(Point pos, JLabel content) { Cell(Point pos, JLabel content) {
this.pos = pos; this.pos = pos;
this.content = content; this.content = content;
} }
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
public boolean isWall() { public boolean isWall() {
return wall; return wall;
} }
public void setWall(boolean wall) { void setWall(boolean wall) {
this.wall = wall; this.wall = wall;
} }

View File

@ -56,10 +56,10 @@ public class Grid implements Iterable<Cell> {
content.revalidate(); content.revalidate();
} }
public void setWall(Point loc) { public void setWall(Point loc, boolean wall) {
final Cell c = cells.get(loc.x + size.width * loc.y); final Cell c = cells.get(loc.x + size.width * loc.y);
c.setWall(true); c.setWall(wall);
c.getContent().setBackground(Color.BLACK); c.getContent().setBackground(wall ? Color.BLACK : null);
c.getContent().revalidate(); c.getContent().revalidate();
} }
@ -67,7 +67,6 @@ public class Grid implements Iterable<Cell> {
for (Cell cell : cells) { for (Cell cell : cells) {
cell.getContent().setBackground(null); cell.getContent().setBackground(null);
cell.setWall(false); cell.setWall(false);
cell.setSelected(false);
} }
} }
@ -75,4 +74,5 @@ public class Grid implements Iterable<Cell> {
public Iterator<Cell> iterator() { public Iterator<Cell> iterator() {
return cells.iterator(); return cells.iterator();
} }
} }

View File

@ -24,6 +24,7 @@ public class App extends JFrame {
private Node startNode; private Node startNode;
private Node endNode; private Node endNode;
private JPanel gridPane; private JPanel gridPane;
private boolean wallPlacing = false;
private App() throws HeadlessException { private App() throws HeadlessException {
super("Pathfinding"); super("Pathfinding");
@ -34,6 +35,7 @@ public class App extends JFrame {
} }
private void setupInterface() { private void setupInterface() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
gridPane = new JPanel(); gridPane = new JPanel();
gridPane.setLayout(new GridLayout(ROWS, COLS)); gridPane.setLayout(new GridLayout(ROWS, COLS));
for (Cell c : grid) { for (Cell c : grid) {
@ -45,7 +47,9 @@ public class App extends JFrame {
public void mousePressed(MouseEvent e) { public void mousePressed(MouseEvent e) {
final JLabel at = (JLabel) gridPane.getComponentAt(e.getPoint()); final JLabel at = (JLabel) gridPane.getComponentAt(e.getPoint());
final Point loc = grid.get(at).getPos(); final Point loc = grid.get(at).getPos();
if (startNode == null) { if (wallPlacing) {
doPlaceWall(loc);
} else if (startNode == null) {
grid.setColor(loc, Color.RED); grid.setColor(loc, Color.RED);
startNode = new Node(loc); startNode = new Node(loc);
} else if (endNode == null) { } else if (endNode == null) {
@ -56,6 +60,9 @@ public class App extends JFrame {
}); });
final JMenuBar menuBar = new JMenuBar(); final JMenuBar menuBar = new JMenuBar();
final JMenuItem placeWallItem = new JMenuItem("Place wall");
menuBar.add(placeWallItem);
placeWallItem.addActionListener(actionEvent -> setWallPlacing(placeWallItem));
final JMenuItem runItem = new JMenuItem("Run"); final JMenuItem runItem = new JMenuItem("Run");
menuBar.add(runItem); menuBar.add(runItem);
runItem.addActionListener(actionEvent -> doSearch()); runItem.addActionListener(actionEvent -> doSearch());
@ -70,6 +77,24 @@ public class App extends JFrame {
getContentPane().add(gridPane); getContentPane().add(gridPane);
} }
private void setWallPlacing(JMenuItem placeWallItem) {
// Invert selection state
placeWallItem.setSelected(!placeWallItem.isSelected());
final boolean selected = placeWallItem.isSelected();
if (selected) {
placeWallItem.setBackground(Color.BLUE);
} else {
placeWallItem.setBackground(null);
}
this.wallPlacing = selected;
}
private void doPlaceWall(Point loc) {
final boolean wall = grid.get(loc).isWall();
// Toggle "wall" state
grid.setWall(loc, !wall);
}
private void doReset() { private void doReset() {
pathfinder = null; pathfinder = null;
startNode = null; startNode = null;