/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package puppyeyes.engine;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.HeadlessException;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.lang.reflect.InvocationTargetException;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import puppyeyes.engine.LowerLevel.LinuxKeyboardRepair;
import puppyeyes.engine.LowerLevel.StepProcessor;
import puppyeyes.engine.Settings.GameSettings;
import puppyeyes.engine.Settings.Input;
/**
* The basic Game Window. Singleton.
*/
public class GameWindow extends JFrame {
private static final long serialVersionUID = 1L; //Unused largely as will not be serializing
private static GameWindow gameWindow;
private static Timer repaintTimer;
private static StepProcessor stepProcessor;
private static Level level;
/**
* Static constructor, initializes static variables before first use of class
*/
{
gameWindow = null;
repaintTimer = null;
stepProcessor=null;
}
public static void initialize(final String title, Level level){
if(level==null) throw new IllegalArgumentException("Initial level cannot be null!");
try {
SwingUtilities.invokeAndWait(new Runnable(){
public void run(){
gameWindow = new GameWindow(title);
}
});
} catch (InvocationTargetException | InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
setLevel(level);
new LinuxKeyboardRepair().install(); //Fix keyrepeat bug on linux
stepProcessor = new StepProcessor();
stepProcessor.start(); //Start step processing
try {
SwingUtilities.invokeAndWait(new Runnable(){
public void run(){
gameWindow.pack();
gameWindow.setVisible(true);
gameWindow.addWindowListener(new WindowAdapter() {
/**
* When the window closes, stop the threads and closes windows cleanly
*/
@Override
public void windowClosing(WindowEvent e){
if(repaintTimer!=null)
repaintTimer.stop();
if(stepProcessor!=null)
stepProcessor.shutdown();
for(Frame f : Frame.getFrames())
f.dispose();
}
});
}
});
} catch (InvocationTargetException | InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
public static Level getLevel() {
return level;
}
public static void setLevel(final Level level) {
try {
SwingUtilities.invokeAndWait(new Runnable(){
public void run(){
gameWindow.getContentPane().remove(level);
GameWindow.level=level;
gameWindow.getContentPane().add(level, BorderLayout.CENTER);
new Input(level);
level.requestFocusInWindow(); //Make the level be what gets events
}
});
} catch (InvocationTargetException | InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if(repaintTimer!=null)
repaintTimer.stop();
// For drawing events
repaintTimer = new Timer(1000/GameSettings.fps, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Paint everything (objects, backgrounds)
gameWindow.repaint();
}
});
repaintTimer.start();
}
private GameWindow(String title){
setContentPane(new JPanel(new BorderLayout()));
}
/*
* Not sure what this is for yet...
*/
@Override
public Point getMousePosition() throws HeadlessException {
return super.getMousePosition();
}
}