Add maze edition functionalities #27

Merged
antograb merged 5 commits from editMaze into mvc 2018-01-01 19:28:25 +00:00
7 changed files with 188 additions and 26 deletions
Showing only changes of commit 81df80e7ea - Show all commits

No files matched your search

+40 -19
View File
@@ -34,19 +34,20 @@ public class MazeAppModel extends Observable {
if (departure == null || arrival == null) { if (departure == null || arrival == null) {
previous = null; previous = null;
shortest = null; shortest = null;
return;
} }
else {
previous = (Previous) Dijkstra.dijkstra(maze, departure); previous = (Previous) Dijkstra.dijkstra(maze, departure);
shortest = previous.getShortestPathTo(arrival); shortest = previous.getShortestPathTo(arrival);
} }
setChanged();
notifyObservers(MazeAppModelMessage.MazeRenewal);
}
public void loadMaze(String filename) { public void loadMaze(String filename) {
this.filename = new String(filename); this.filename = new String(filename);
maze = new Maze(this.filename); maze = new Maze(this.filename);
initModelFromMaze(); initModelFromMaze();
saved = true; saved = true;
setChanged();
notifyObservers(MazeAppModelMessage.MazeRenewal);
} }
public Maze getMaze() { public Maze getMaze() {
@@ -86,36 +87,56 @@ public class MazeAppModel extends Observable {
notifyObservers(MazeAppModelMessage.FileChange); notifyObservers(MazeAppModelMessage.FileChange);
} }
public void toggleBox(int x, int y) { public void toggleBox(int boxLine, int boxRow) {
if (maze.getMaze()[y][x].isWalkable()) { if (maze.getMaze()[boxLine][boxRow].isWalkable()) {
if (maze.getMaze()[y][x].isDeparture()) { if (maze.getMaze()[boxLine][boxRow].isDeparture()) {
if (arrival != null) { setABox(boxLine, boxRow);
int arrivalRow = ((EBox) arrival).getX();
int arrivalLine = ((EBox) arrival).getY();
maze.getMaze()[arrivalLine][arrivalRow] =
new EBox("E", arrivalLine, arrivalRow, maze);
} }
maze.getMaze()[y][x] = new ABox("A", y, x, maze); else if (maze.getMaze()[boxLine][boxRow].isArrival()) {
} setEBox(boxLine, boxRow);
else if (maze.getMaze()[y][x].isArrival()) {
maze.getMaze()[y][x] = new EBox("E", y, x, maze);
} }
else { else {
maze.getMaze()[y][x] = new WBox("W", y, x, maze); setWBox(boxLine, boxRow);
} }
} }
else { else {
setDBox(boxLine, boxRow);
}
}
public void setWBox(int boxLine, int boxRow) {
maze.getMaze()[boxLine][boxRow] = new WBox("W", boxLine, boxRow, maze);
initModelFromMaze();
saved = false;
}
public void setEBox(int boxLine, int boxRow) {
maze.getMaze()[boxLine][boxRow] = new EBox("E", boxLine, boxRow, maze);
initModelFromMaze();
saved = false;
}
public void setDBox(int boxLine, int boxRow) {
if (departure != null) { if (departure != null) {
int departureRow = ((EBox) departure).getX(); int departureRow = ((EBox) departure).getX();
int departureLine = ((EBox) departure).getY(); int departureLine = ((EBox) departure).getY();
maze.getMaze()[departureLine][departureRow] = maze.getMaze()[departureLine][departureRow] =
new EBox("E", departureLine, departureRow, maze); new EBox("E", departureLine, departureRow, maze);
} }
maze.getMaze()[y][x] = new DBox("D", y, x, maze); maze.getMaze()[boxLine][boxRow] = new DBox("D", boxLine, boxRow, maze);
initModelFromMaze();
saved = false;
} }
public void setABox(int boxLine, int boxRow) {
if (arrival != null) {
int arrivalRow = ((EBox) arrival).getX();
int arrivalLine = ((EBox) arrival).getY();
maze.getMaze()[arrivalLine][arrivalRow] =
new EBox("E", arrivalLine, arrivalRow, maze);
}
maze.getMaze()[boxLine][boxRow] = new ABox("A", boxLine, boxRow, maze);
initModelFromMaze(); initModelFromMaze();
saved = false; saved = false;
setChanged();
notifyObservers(MazeAppModelMessage.MazeRenewal);
} }
} }
+25
View File
@@ -0,0 +1,25 @@
package ui;
import javax.swing.JPopupMenu;
import javax.swing.JMenuItem;
import maze.Box;
public class BoxTypeEditionMenu extends JPopupMenu {
JMenuItem setWBox;
JMenuItem setEBox;
JMenuItem setDBox;
JMenuItem setABox;
public BoxTypeEditionMenu(MazeApp mazeApp, int boxLine, int boxRow) {
add(setEBox = new SetEBoxMenuItem(mazeApp, boxLine, boxRow));
add(setWBox = new SetWBoxMenuItem(mazeApp, boxLine, boxRow));
add(setDBox = new SetDBoxMenuItem(mazeApp, boxLine, boxRow));
add(setABox = new SetABoxMenuItem(mazeApp, boxLine, boxRow));
Box box = (Box) mazeApp.getMazeAppModel().getMaze().getMaze()[boxLine][boxRow];
setEBox.setEnabled((box.isWalkable() && (box.isDeparture() ||
box.isArrival())) || ! box.isWalkable());
setWBox.setEnabled(box.isWalkable());
setDBox.setEnabled(! box.isDeparture());
setABox.setEnabled(! box.isArrival());
}
}
+19 -7
View File
@@ -117,16 +117,28 @@ public class MazeDrawing extends JPanel implements MouseListener {
return; return;
} }
public void mouseClicked(MouseEvent e) { private int getBoxRow(int x) {
int x = e.getX();
int y = e.getY();
int dimensionX = mazeApp.getMazeAppModel().getMaze().getDimensionX(); int dimensionX = mazeApp.getMazeAppModel().getMaze().getDimensionX();
int dimensionY = mazeApp.getMazeAppModel().getMaze().getDimensionY();
int boxWidth = getWidth() / dimensionX; int boxWidth = getWidth() / dimensionX;
return x / boxWidth;
}
private int getBoxLine(int y) {
int dimensionY = mazeApp.getMazeAppModel().getMaze().getDimensionY();
int boxHeight = getHeight() / dimensionY; int boxHeight = getHeight() / dimensionY;
int boxX = x / boxWidth; return y / boxHeight;
int boxY = y / boxHeight; }
mazeApp.getMazeAppModel().toggleBox(boxX, boxY);
public void mouseClicked(MouseEvent e) {
int boxRow = getBoxRow(e.getX());
int boxLine = getBoxLine(e.getY());
if (e.getButton() == MouseEvent.BUTTON3) {
BoxTypeEditionMenu boxTypeEditionMenu = new BoxTypeEditionMenu(mazeApp, boxLine, boxRow);
boxTypeEditionMenu.show(e.getComponent(), e.getX(), e.getY());
}
else if (e.getButton() == MouseEvent.BUTTON1) {
mazeApp.getMazeAppModel().toggleBox(boxLine, boxRow);
}
return; return;
} }
+26
View File
@@ -0,0 +1,26 @@
package ui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenuItem;
public class SetABoxMenuItem extends JMenuItem
implements ActionListener {
private final MazeApp mazeApp;
private final int boxLine;
private final int boxRow;
public SetABoxMenuItem(MazeApp mazeApp, int boxLine, int boxRow) {
super("Set Arrival Box");
this.mazeApp = mazeApp;
this.boxLine = boxLine;
this.boxRow = boxRow;
addActionListener(this);
}
public void actionPerformed(ActionEvent evt) {
mazeApp.getMazeAppModel().setABox(boxLine, boxRow);
}
}
+26
View File
@@ -0,0 +1,26 @@
package ui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenuItem;
public class SetDBoxMenuItem extends JMenuItem
implements ActionListener {
private final MazeApp mazeApp;
private final int boxLine;
private final int boxRow;
public SetDBoxMenuItem(MazeApp mazeApp, int boxLine, int boxRow) {
super("Set Departure Box");
this.mazeApp = mazeApp;
this.boxLine = boxLine;
this.boxRow = boxRow;
addActionListener(this);
}
public void actionPerformed(ActionEvent evt) {
mazeApp.getMazeAppModel().setDBox(boxLine, boxRow);
}
}
+26
View File
@@ -0,0 +1,26 @@
package ui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenuItem;
public class SetEBoxMenuItem extends JMenuItem
implements ActionListener {
private final MazeApp mazeApp;
private final int boxLine;
private final int boxRow;
public SetEBoxMenuItem(MazeApp mazeApp, int boxLine, int boxRow) {
super("Set Empty Box");
this.mazeApp = mazeApp;
this.boxLine = boxLine;
this.boxRow = boxRow;
addActionListener(this);
}
public void actionPerformed(ActionEvent evt) {
mazeApp.getMazeAppModel().setEBox(boxLine, boxRow);
}
}
+26
View File
@@ -0,0 +1,26 @@
package ui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenuItem;
public class SetWBoxMenuItem extends JMenuItem
implements ActionListener {
private final MazeApp mazeApp;
private final int boxLine;
private final int boxRow;
public SetWBoxMenuItem(MazeApp mazeApp, int boxLine, int boxRow) {
super("Set Wall Box");
this.mazeApp = mazeApp;
this.boxLine = boxLine;
this.boxRow = boxRow;
addActionListener(this);
}
public void actionPerformed(ActionEvent evt) {
mazeApp.getMazeAppModel().setWBox(boxLine, boxRow);
}
}