Merge pull request 'Ajout de la prise en charge des clics sur la grille' (#1) from feature_grideditor into master

Reviewed-on: https://dwarves.iut-fbleau.fr/gitiut/souidi/SAE21_2022/pulls/1
This commit is contained in:
Lyanis SOUIDI 2023-04-28 02:53:49 +02:00
commit 4113056350
4 changed files with 32 additions and 7 deletions

View File

@ -35,14 +35,26 @@ public class GridController {
if (square.isWall()) { if (square.isWall()) {
square.setEmpty(); square.setEmpty();
} else { } else {
square.setWall(); try {
square.setWall();
} catch (Exception ex) {
JOptionPane.showMessageDialog(view, ex.getMessage(), "Erreur", JOptionPane.ERROR_MESSAGE);
}
} }
view.repaint(); view.repaint();
} else if (editMode == Mode.THESEE) { } else if (editMode == Mode.THESEE) {
model.getThesee().setSquare(square); try {
model.getThesee().setSquare(square);
} catch (Exception ex) {
JOptionPane.showMessageDialog(view, ex.getMessage(), "Erreur", JOptionPane.ERROR_MESSAGE);
}
view.repaint(); view.repaint();
} else if (editMode == Mode.EXIT) { } else if (editMode == Mode.EXIT) {
square.setExit(); try {
square.setExit();
} catch (Exception ex) {
JOptionPane.showMessageDialog(view, ex.getMessage(), "Erreur", JOptionPane.ERROR_MESSAGE);
}
view.repaint(); view.repaint();
} }
} }

View File

@ -26,7 +26,16 @@ public class GridView extends JPanel {
} }
public Square click(MouseEvent e) { public Square click(MouseEvent e) {
// TODO: Détection du clic sur une case if ((e.getX() < this.gridStartX) || (e.getX() > (this.gridStartX + this.gridSize)) || (e.getY() < this.gridStartY) || (e.getY() > (this.gridStartY + this.gridSize))) return null;
int x = (e.getX() - this.gridStartX) / this.squareSize;
int y = (e.getY() - this.gridStartY) / this.squareSize;
if ((x >= 0) && (x < this.model.getSize()) && (y >= 0) && (y < this.model.getSize())) {
try {
return this.model.getSquare(y, x);
} catch (Exception ex) {
return null;
}
}
return null; return null;
} }

View File

@ -37,14 +37,17 @@ public class Square {
/** /**
* Sets the current square as a wall * Sets the current square as a wall
*/ */
public void setWall() { public void setWall() throws Exception {
if (this.gridModel.getThesee().getSquare() == this) throw new Exception("Vous ne pouvez pas placer un mur sur la même case que Thésée. Déplacez d'abord Thésée puis réessayez.");
if (this.isExit()) throw new Exception("Vous ne pouvez pas placer un mur sur la même case que la sortie. Déplacez d'abord la sortie puis réessayez.");
this.type = 1; this.type = 1;
} }
/** /**
* Removes the existing exit from the grid (if it exists) and sets the current square as an exit * Removes the existing exit from the grid (if it exists) and sets the current square as an exit
*/ */
public void setExit() { public void setExit() throws Exception {
if (this.gridModel.getThesee().getSquare() == this) throw new Exception("Vous ne pouvez pas placer la sortie sur la même case que Thésée. Déplacez d'abord Thésée puis réessayez.");
for (int i = 0; i < this.gridModel.getSize(); i++) { for (int i = 0; i < this.gridModel.getSize(); i++) {
for (int j = 0; j < this.gridModel.getSize(); j++) { for (int j = 0; j < this.gridModel.getSize(); j++) {
try { try {

View File

@ -2,7 +2,8 @@ public class Thesee {
private Square square; private Square square;
private Square intialSquare; private Square intialSquare;
public void setSquare(Square square) { public void setSquare(Square square) throws Exception {
if (square.isExit()) throw new Exception("Vous ne pouvez pas placer Thesee sur la même case que la sortie. Déplacez d'abord la sortie puis réessayez.");
square.setEmpty(); square.setEmpty();
this.square = square; this.square = square;
if (this.intialSquare == null) this.intialSquare = this.square; if (this.intialSquare == null) this.intialSquare = this.square;