Add a context menu to edit boxes type
model: * New methods to directly set boxes type instead of only toggling (setXBox methods) * toggleBox now calls setXBox methods ui: * Add a context menu, enabled on right click, to directly choose the type of a box in the maze * Defer the calculation of the box coordinates in the matrix to two new methods, getBoxRow and getBoxLine
This commit is contained in:
1 parent
37def85f5b
commit
81df80e7ea
7 files changed
+196
-34
No files matched your search
@@ -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
@@ -117,16 +117,28 @@ public class MazeDrawing extends JPanel implements MouseListener {
|
||||
return;
|
||||
}
|
||||
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
int x = e.getX();
|
||||
int y = e.getY();
|
||||
private int getBoxRow(int x) {
|
||||
int dimensionX = mazeApp.getMazeAppModel().getMaze().getDimensionX();
|
||||
int dimensionY = mazeApp.getMazeAppModel().getMaze().getDimensionY();
|
||||
int boxWidth = getWidth() / dimensionX;
|
||||
return x / boxWidth;
|
||||
}
|
||||
|
||||
private int getBoxLine(int y) {
|
||||
int dimensionY = mazeApp.getMazeAppModel().getMaze().getDimensionY();
|
||||
int boxHeight = getHeight() / dimensionY;
|
||||
int boxX = x / boxWidth;
|
||||
int boxY = y / boxHeight;
|
||||
mazeApp.getMazeAppModel().toggleBox(boxX, boxY);
|
||||
return y / boxHeight;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user