diff --git a/src/fr/enssat/BoulderDash/controllers/LevelEditorController.java b/src/fr/enssat/BoulderDash/controllers/LevelEditorController.java index c5a7a410..d1e40eda 100644 --- a/src/fr/enssat/BoulderDash/controllers/LevelEditorController.java +++ b/src/fr/enssat/BoulderDash/controllers/LevelEditorController.java @@ -9,135 +9,134 @@ import fr.enssat.BoulderDash.helpers.LevelSaveHelper; import fr.enssat.BoulderDash.models.LevelModel; import fr.enssat.BoulderDash.views.HelpView; import fr.enssat.BoulderDash.views.LevelEditorView; -import fr.enssat.BoulderDash.controllers.NavigationBetweenViewController; import javax.swing.*; /** * LevelEditorController - * + *

* Manages the level editor controller. * - * @author Valerian Saliou - * @since 2015-06-19 + * @author Valerian Saliou + * @since 2015-06-19 */ public class LevelEditorController implements ActionListener { - private LevelModel levelModel; - private LevelEditorView levelEditorView; - private NavigationBetweenViewController nav; + private LevelModel levelModel; + private LevelEditorView levelEditorView; + private NavigationBetweenViewController nav; - /** - * Class constructor' - * - * @param levelModel Level model - */ - public LevelEditorController(LevelModel levelModel, NavigationBetweenViewController nav) { - this.levelModel = levelModel; - this.levelModel.setShowCursor(true); + /** + * Class constructor' + * + * @param levelModel Level model + */ + public LevelEditorController(LevelModel levelModel, NavigationBetweenViewController nav) { + this.levelModel = levelModel; + this.levelModel.setShowCursor(true); - this.nav = nav; - this.nav.getAudioLoadHelper().stopMusic(); - - this.levelEditorView = new LevelEditorView(this, levelModel, nav); + this.nav = nav; + this.nav.getAudioLoadHelper().stopMusic(); - // Pre-bind event watcher (hack to fix a Java issue) - this.levelModel.decrementCursorXPosition(); - } + this.levelEditorView = new LevelEditorView(this, levelModel, nav); - /** - * Handles the 'action performed' event - * - * @param event Action event - */ - public void actionPerformed(ActionEvent event) { - switch(event.getActionCommand()) { - case "menu": - this.levelEditorView.setVisible(false); - this.nav.setMenuView(); - this.nav.getAudioLoadHelper().startMusic("game"); + // Pre-bind event watcher (hack to fix a Java issue) + this.levelModel.decrementCursorXPosition(); + } - break; + /** + * Handles the 'action performed' event + * + * @param event Action event + */ + public void actionPerformed(ActionEvent event) { + switch (event.getActionCommand()) { + case "menu": + this.levelEditorView.setVisible(false); + this.nav.getMenuView().setVisible(true); + this.nav.getAudioLoadHelper().startMusic("game"); - case "save": - // Check constraints - try { - this.levelModel.checkConstraints(); + break; - // Save action (direct save) - String levelId = this.levelEditorView.getSelectedLevel(); - LevelSaveHelper levelSave; + case "save": + // Check constraints + try { + this.levelModel.checkConstraints(); - if(levelId == null || levelId.isEmpty()) { - // Create a new level - levelSave = new LevelSaveHelper(levelModel.getGroundLevelModel()); - } else { - // Overwrite existing level - levelSave = new LevelSaveHelper(levelId, levelModel.getGroundLevelModel()); - } + // Save action (direct save) + String levelId = this.levelEditorView.getSelectedLevel(); + LevelSaveHelper levelSave; - JFrame frameDialog = new JFrame("Info"); - JOptionPane.showMessageDialog(frameDialog, "Level saved"); + if (levelId == null || levelId.isEmpty()) { + // Create a new level + levelSave = new LevelSaveHelper(levelModel.getGroundLevelModel()); + } else { + // Overwrite existing level + levelSave = new LevelSaveHelper(levelId, levelModel.getGroundLevelModel()); + } - this.levelEditorView.openedLevelChange(levelSave.getLevelId()); - } catch(LevelConstraintNotRespectedException e) { - JFrame frameDialog = new JFrame("Error"); - JOptionPane.showMessageDialog(frameDialog, e.getMessage()); - } + JFrame frameDialog = new JFrame("Info"); + JOptionPane.showMessageDialog(frameDialog, "Level saved"); - break; - - case "delete": - String levelId = this.levelEditorView.getSelectedLevel(); - JFrame frameDialog = new JFrame("Info"); - - if(levelId == null || levelId.isEmpty()) { - JOptionPane.showMessageDialog(frameDialog, "Level not yet saved, no need to delete it!"); - } else { - new LevelRemoveHelper(levelId); - JOptionPane.showMessageDialog(frameDialog, "Level deleted!"); - - this.levelEditorView.openedLevelChange(null); - } - break; - - case "help": - new HelpView(); - break; - - case "new": - this.levelEditorView.openedLevelChange(null); - break; + this.levelEditorView.openedLevelChange(levelSave.getLevelId()); + } catch (LevelConstraintNotRespectedException e) { + JFrame frameDialog = new JFrame("Error"); + JOptionPane.showMessageDialog(frameDialog, e.getMessage()); } - this.getLevelEditorView().getLevelEditorGroundView().grabFocus(); + break; + + case "delete": + String levelId = this.levelEditorView.getSelectedLevel(); + JFrame frameDialog = new JFrame("Info"); + + if (levelId == null || levelId.isEmpty()) { + JOptionPane.showMessageDialog(frameDialog, "Level not yet saved, no need to delete it!"); + } else { + new LevelRemoveHelper(levelId); + JOptionPane.showMessageDialog(frameDialog, "Level deleted!"); + + this.levelEditorView.openedLevelChange(null); + } + break; + + case "help": + new HelpView(); + break; + + case "new": + this.levelEditorView.openedLevelChange(null); + break; } - /** - * Gets the level editor view - * - * @return Level editor view - */ - public LevelEditorView getLevelEditorView() { - return levelEditorView; - } + this.getLevelEditorView().getLevelEditorGroundView().grabFocus(); + } + + /** + * Gets the level editor view + * + * @return Level editor view + */ + public LevelEditorView getLevelEditorView() { + return levelEditorView; + } + + /** + * Gets level model + * + * @return Level model + */ + public LevelModel getLevelModel() { + return this.levelModel; + } + + /** + * Sets the level editor view + * + * @param levelEditorView Level editor view + */ + public void setLevelEditorView(LevelEditorView levelEditorView) { + this.levelEditorView = levelEditorView; + } - /** - * Gets level model - * - * @return Level model - */ - public LevelModel getLevelModel() { - return this.levelModel; - } - /** - * Sets the level editor view - * - * @param levelEditorView Level editor view - */ - public void setLevelEditorView(LevelEditorView levelEditorView) { - this.levelEditorView = levelEditorView; - } - - } \ No newline at end of file diff --git a/src/fr/enssat/BoulderDash/controllers/NavigationBetweenViewController.java b/src/fr/enssat/BoulderDash/controllers/NavigationBetweenViewController.java index db190f48..e2ca1da0 100644 --- a/src/fr/enssat/BoulderDash/controllers/NavigationBetweenViewController.java +++ b/src/fr/enssat/BoulderDash/controllers/NavigationBetweenViewController.java @@ -6,127 +6,121 @@ import java.awt.event.ActionListener; import fr.enssat.BoulderDash.helpers.AudioLoadHelper; import fr.enssat.BoulderDash.models.LevelModel; import fr.enssat.BoulderDash.views.MenuView; -import fr.enssat.BoulderDash.controllers.LevelEditorController; -import fr.enssat.BoulderDash.controllers.GameController; /** * Controller to navigate between the different views - * - * @author Colin Leverger * + * @author Colin Leverger */ public class NavigationBetweenViewController implements ActionListener { - private LevelEditorController levelEditorController; - private MenuView menuView; - private AudioLoadHelper audioLoadHelper; - private LevelModel levelModelForGame, levelModelForEditor; - private GameController gameController; - private String pickedLevelIdentifier; + private LevelEditorController levelEditorController; + private MenuView menuView; + private AudioLoadHelper audioLoadHelper; + private LevelModel levelModelForGame, levelModelForEditor; + private GameController gameController; + private String pickedLevelIdentifier; - /** - * Class constructor - */ - public NavigationBetweenViewController() { - this.audioLoadHelper = new AudioLoadHelper(); + /** + * Class constructor + */ + public NavigationBetweenViewController() { + this.audioLoadHelper = new AudioLoadHelper(); - // Play game music - this.getAudioLoadHelper().startMusic("game"); + // Play game music + this.getAudioLoadHelper().startMusic("game"); - // Creation of the first view - this.menuView = new MenuView(this); - } + // Creation of the first view + this.menuView = new MenuView(this); + } - /** - * Action performed event handler - * - * @param event Action event - */ - @Override - public void actionPerformed(ActionEvent event) { - switch (event.getActionCommand()) { - case "quit": - System.exit(0); - break; + /** + * Action performed event handler + * + * @param event Action event + */ + @Override + public void actionPerformed(ActionEvent event) { + switch (event.getActionCommand()) { + case "quit": + System.exit(0); + break; - case "editor": - // New blank model for editor - this.levelModelForEditor = new LevelModel(audioLoadHelper); - this.levelEditorController = new LevelEditorController(this.levelModelForEditor, this); + case "editor": + // New blank model for editor + this.levelModelForEditor = new LevelModel(audioLoadHelper); + this.levelEditorController = new LevelEditorController(this.levelModelForEditor, this); - this.levelEditorController.getLevelEditorView().setVisible(true); - this.levelEditorController.getLevelEditorView().getLevelEditorGroundView().grabFocus(); + this.levelEditorController.getLevelEditorView().setVisible(true); + this.levelEditorController.getLevelEditorView().getLevelEditorGroundView().grabFocus(); - if (gameController != null) { - this.gameController.getGameView().setVisible(false); - } + if (gameController != null) { + this.gameController.getGameView().setVisible(false); + } - break; + break; - case "game": - // Reinit the levelModelForGame... - pickedLevelIdentifier = this.menuView.getLevelIdentifier(); + case "game": + // Reinit the levelModelForGame... + pickedLevelIdentifier = this.menuView.getLevelIdentifier(); - this.levelModelForGame = new LevelModel(pickedLevelIdentifier, audioLoadHelper); - this.gameController = new GameController(levelModelForGame, audioLoadHelper, this); + this.levelModelForGame = new LevelModel(pickedLevelIdentifier, audioLoadHelper); + this.gameController = new GameController(levelModelForGame, audioLoadHelper, this); - if (levelEditorController != null) { - this.levelEditorController.getLevelEditorView().setVisible(false); - } + if (levelEditorController != null) { + this.levelEditorController.getLevelEditorView().setVisible(false); + } - this.gameController.getGameView().setVisible(true); - this.gameController.getGameView().getGameFieldView().grabFocus(); + this.gameController.getGameView().setVisible(true); + this.gameController.getGameView().getGameFieldView().grabFocus(); - break; - } - - this.menuView.setVisible(false); - } - - /** - * Get the audio load helper - * - * @return Audio load helper - */ - public AudioLoadHelper getAudioLoadHelper() { - return this.audioLoadHelper; + break; } - /** - * Get the first view - * - * @return First view - */ - public MenuView getMenuView() { - return this.menuView; - } + this.menuView.setVisible(false); + } - /** - * Set the first view - * - * @param menuView - */ - public MenuView setMenuView() { - this.menuView = new MenuView(this); - return menuView; - } + /** + * Get the audio load helper + * + * @return Audio load helper + */ + public AudioLoadHelper getAudioLoadHelper() { + return this.audioLoadHelper; + } + + /** + * Get the first view + * + * @return First view + */ + public MenuView getMenuView() { + return this.menuView; + } + + // menuView has already been created in the constructor! No need to recreate it. + // + //public MenuView setMenuView() { + // this.menuView = new MenuView(this); + // return menuView; + //} + + /** + * Get the pickedLevel + * + * @return pickedLevelIdentifier Picked level identifier + */ + public String getPickedLevelIdentifier() { + return pickedLevelIdentifier; + } + + /** + * Set the pickedLevelIdentifier + * + * @param pickedLevelIdentifier Picked level identifier + */ + public void setPickedLevelIdentifier(String pickedLevelIdentifier) { + this.pickedLevelIdentifier = pickedLevelIdentifier; + } - /** - * Get the pickedLevel - * - * @return pickedLevelIdentifier Picked level identifier - */ - public String getPickedLevelIdentifier() { - return pickedLevelIdentifier; - } - /** - * Set the pickedLevelIdentifier - * - * @param pickedLevelIdentifier Picked level identifier - */ - public void setPickedLevelIdentifier(String pickedLevelIdentifier) { - this.pickedLevelIdentifier = pickedLevelIdentifier; - } - - }