package tamagotchi.model;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.KeyboardFocusManager;
import java.io.File;
import java.io.IOException;
import java.util.Observable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import tamagotchi.core.EventFactory;
import tamagotchi.view.GamePanel;
import tamagotchi.view.MenuPanel;
import tamagotchi.view.OptionPanel;
/**
* Cette classe permet de gerer les methodes metier ainsi que les données de l'IHM
* @author mastersnes
* @see EventFactory
* @see GamePanel
* @see MenuPanel
* @see OptionPanel
*/
public final class GUIModel extends Observable {
/**
* La dimension de la fenetre (peut etre modifié dans les options)
*/
public static final Dimension SCREEN_SIZE = new Dimension(800, 600);
private static JPanel currentPane = null;
private boolean exit = false;
private boolean resize = false;
private Image icon = null;
/**
* Constructeur par defaut, initialise simplement les evenements et l'affichage
*/
public GUIModel() {
try {
icon = ImageIO.read(new File("ressources/system/icon"));
} catch (IOException ex) {
Logger.getLogger(GUIModel.class.getName()).log(Level.SEVERE, null, ex);
}
switchToMenu();
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new EventFactory());
}
/**
* Renvoie l'ecran courant
* @see MenuPanel
* @see OptionPanel
* @see GamePanel
* @return
*/
public JPanel getCurrentPane() {
return currentPane;
}
/**
* Change l'ecran courant pour celui du jeu
* @see GamePanel
* @see GameModel
*/
public void switchToGame() {
currentPane = new GamePanel(new GameModel(this));
setChanged();
notifyObservers();
}
/**
* Change l'ecran courant pour celui des options
* @see OptionPanel
* @see OptionModel
*/
public void switchToOption() {
currentPane = new OptionPanel(new OptionModel(this));
setChanged();
notifyObservers();
}
/**
* Change l'ecran courant pour celui du menu
* @see MenuPanel
* @see MenuModel
*
*/
public void switchToMenu() {
currentPane = new MenuPanel(new MenuModel(this));
setChanged();
notifyObservers();
}
/*
* Quitte le jeu
*/
public void switchToExit() {
setExit(true);
setChanged();
notifyObservers();
}
/**
* Verifie si le jeu doit etre quitté
* @return the exit
*/
public boolean isExit() {
return exit;
}
private void setExit(final boolean exit) {
this.exit = exit;
}
/**
* Redefinie la resolution de la fenetre
* @param resolution
*/
public void setResolution(final String resolution) {
final String[] resolutionSplit = resolution.split(" * ");
final int width = Integer.parseInt(resolutionSplit[0]);
final int height = Integer.parseInt(resolutionSplit[2]);
SCREEN_SIZE.width = width;
SCREEN_SIZE.height = height;
setResize(true);
setChanged();
notifyObservers();
}
/**
* Verifie si la fenetre doit etre redimensionnée
* @return the resize
*/
public boolean isResize() {
return resize;
}
/**
* Definie si la fenetre doit etre redimensionnée
* @param resize the resize to set
*/
public void setResize(final boolean resize) {
this.resize = resize;
}
/**
* @return the icon
*/
public Image getIcon() {
return icon;
}
}