Added a settings dialog but it is very sloppy done

This commit is contained in:
Ziver Koc 2007-04-29 21:52:36 +00:00
parent e280861f2d
commit 88fcd15266
4 changed files with 406 additions and 676 deletions

View file

@ -40,6 +40,18 @@ public class LWJGLGameWindow {
this(800, 600, 60, false, title);
}
/**
* Shows the properties dialog first
*
* @param title The title of the window
* @param status The status of the properties dialog
*/
public LWJGLGameWindow(String title,int fps, int status){
this.title = title;
this.fps = fps;
new LWJGLPropertiesDialog(this, status);
}
/**
* Creates a custom window
* @param width The width of the window
@ -56,9 +68,15 @@ public class LWJGLGameWindow {
this.title = title;
MultiPrintStream.makeInstance(new MultiPrintStream("log.txt"));
try {
initDisplay();
DisplayMode dm = new DisplayMode(width, height);
Display.setDisplayMode(dm);
Display.setTitle(title);
Display.setFullscreen(fullscreen);
initOpenGl();
run();
} catch (Exception e) {
e.printStackTrace(System.err);
@ -66,21 +84,37 @@ public class LWJGLGameWindow {
} finally {
cleanup();
}
System.exit(0);
System.exit(0);
}
public void createWindow(DisplayMode dm, boolean fullscreen){
LWJGLGameWindow.width = dm.getWidth();
LWJGLGameWindow.height = dm.getHeight();
this.fullscreen = fullscreen;
MultiPrintStream.makeInstance(new MultiPrintStream("log.txt"));
try {
Display.setDisplayMode(dm);
Display.setTitle(title);
Display.setFullscreen(fullscreen);
initOpenGl();
run();
} catch (Exception e) {
e.printStackTrace(System.err);
Sys.alert(title, "An error occured and the game will exit.");
} finally {
cleanup();
}
System.exit(0);
}
/**
* Initialise the game
* @throws Exception if init fails
*/
private void initDisplay() throws Exception {
DisplayMode dm = new DisplayMode(width, height);
Display.setDisplayMode(dm);
Display.setTitle(title);
Display.setFullscreen(fullscreen);
private void initOpenGl() throws Exception {
// grab the mouse, dont want that hideous cursor when we're playing!
Mouse.setGrabbed(false);
@ -128,8 +162,6 @@ public class LWJGLGameWindow {
cam = new Camera();
init();
}
/**

View file

@ -0,0 +1,334 @@
package ei.engine;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JButton;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Properties;
import javax.swing.JLabel;
public class LWJGLPropertiesDialog implements ActionListener{
public static final int SOW_ONCE = 0;
public static final int SOW_ALWAYS = 1;
private JFrame jFrame = null; // @jve:decl-index=0:visual-constraint="57,133"
private JCheckBox fullscreen = null;
private JPanel jPanel = null;
private JComboBox resulution = null;
private JComboBox pixelres = null;
private JComboBox freq = null;
private JButton ok = null;
private JButton cancel = null;
private JLabel jLabel = null;
private JLabel jLabel1 = null;
private JLabel jLabel2 = null;
private LWJGLGameWindow window;
private DisplayMode[] display;
public static void main(String[] args){
}
public LWJGLPropertiesDialog(LWJGLGameWindow window, int status){
try {
display = Display.getAvailableDisplayModes();
} catch (LWJGLException e) {
e.printStackTrace();
}
this.window = window;
Properties settings = loadProp();
getJFrame(display);
if(settings != null){
setSelected(resulution,settings.getProperty("Res"));
setSelected(pixelres,settings.getProperty("Pixel"));
setSelected(freq,settings.getProperty("Freq"));
fullscreen.setSelected(Boolean.parseBoolean(settings.getProperty("Fullscreen")));
if(status == SOW_ONCE){
actionPerformed(new ActionEvent(ok,0,"loaded settings"));
}
else if(status == SOW_ALWAYS){
jFrame.setVisible(true);
}
}
else{
jFrame.setVisible(true);
}
}
private void setSelected(JComboBox box, String sel){
for(int i=0; i<box.getItemCount() ;i++){
if(((String)box.getItemAt(i)).equals(sel)){
box.setSelectedIndex(i);
}
}
}
private Properties loadProp(){
Properties prop = new Properties();
FileInputStream fis;
try {
fis = new FileInputStream("window.ini");
prop.load(fis);
} catch (Exception e) {
prop = null;
}
return prop;
}
private void saveProp(Properties p){
FileOutputStream fos;
try {
fos = new FileOutputStream("window.ini");
p.store(fos, "LWJGLWindow");
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void actionPerformed(ActionEvent event) {
if(event.getSource() == ok){
DisplayMode dm = findDisplay();
if(dm != null){
jFrame.setVisible(false);
jFrame = null;
Properties settings = new Properties();
settings.setProperty("Res",dm.getWidth()+"x"+dm.getHeight());
settings.setProperty("Pixel",""+dm.getBitsPerPixel());
settings.setProperty("Freq",""+dm.getFrequency());
settings.setProperty("Fullscreen",""+fullscreen.isSelected());
saveProp(settings);
window.createWindow(dm, fullscreen.isSelected());
}
}
else if(event.getSource() == cancel){
System.exit(0);
}
}
private DisplayMode findDisplay(){
for(int i=0; i<display.length ;i++){
if(((String)resulution.getSelectedItem()).equals(display[i].getWidth()+"x"+display[i].getHeight()) &&
Integer.parseInt((String)pixelres.getSelectedItem()) == display[i].getBitsPerPixel() &&
Integer.parseInt((String)freq.getSelectedItem()) == display[i].getFrequency()){
return display[i];
}
}
return null;
}
/**
* This method initializes jFrame
*
* @return javax.swing.JFrame
*/
private JFrame getJFrame(DisplayMode[] d) {
if (jFrame == null) {
jFrame = new JFrame();
jFrame.setContentPane(getJPanel(d));
jFrame.pack();
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
Dimension size = jFrame.getSize();
jFrame.setLocation(dim.width/2-size.width/2,dim.height/2-size.height/2);
}
return jFrame;
}
/**
* This method initializes fullscreen
*
* @return javax.swing.JCheckBox
*/
private JCheckBox getFullscreen() {
if (fullscreen == null) {
fullscreen = new JCheckBox();
fullscreen.setText("Fullscreen");
fullscreen.setName("fullscreen");
}
return fullscreen;
}
/**
* This method initializes jPanel
*
* @return javax.swing.JPanel
*/
private JPanel getJPanel(DisplayMode[] d) {
if (jPanel == null) {
jLabel2 = new JLabel();
jLabel2.setText("Freq: ");
jLabel1 = new JLabel();
jLabel1.setText("Pixel");
jLabel = new JLabel();
jLabel.setText("Resulution:");
jPanel = new JPanel();
jPanel.setLayout(new FlowLayout());
jPanel.add(jLabel, null);
jPanel.add(getResulutionButton(d), null);
jPanel.add(jLabel1, null);
jPanel.add(getPixelresButton(d), null);
jPanel.add(jLabel2, null);
jPanel.add(getFreqButton(d), null);
jPanel.add(getFullscreen(), null);
jPanel.add(getOk(), null);
jPanel.add(getCancel(), null);
}
return jPanel;
}
/**
* This method initializes resulution
*
* @return javax.swing.JComboBox
*/
private JComboBox getResulutionButton(DisplayMode[] d) {
if (resulution == null) {
resulution = new JComboBox();
resulution.setName("resulution");
setList(resulution, getResulution(display));
resulution.addActionListener(this);
}
return resulution;
}
/**
* This method initializes pixelres
*
* @return javax.swing.JComboBox
*/
private JComboBox getPixelresButton(DisplayMode[] d) {
if (pixelres == null) {
pixelres = new JComboBox();
pixelres.setName("pixelres");
setList(pixelres, getPixelres(display));
pixelres.addActionListener(this);
}
return pixelres;
}
/**
* This method initializes freq
*
* @return javax.swing.JComboBox
*/
private JComboBox getFreqButton(DisplayMode[] d) {
if (freq == null) {
freq = new JComboBox();
freq.setName("freq");
setList(freq, getFreq(display));
freq.addActionListener(this);
}
return freq;
}
/**
* This method initializes ok
*
* @return javax.swing.JButton
*/
private JButton getOk() {
if (ok == null) {
ok = new JButton();
ok.setText("OK");
ok.addActionListener(this);
}
return ok;
}
/**
* This method initializes cancel
*
* @return javax.swing.JButton
*/
private JButton getCancel() {
if (cancel == null) {
cancel = new JButton();
cancel.setText("Cancel");
cancel.addActionListener(this);
}
return cancel;
}
private String[] getResulution(DisplayMode[] d){
ArrayList<String> list = new ArrayList<String>();
for(int i=0; i<d.length ;i++){
if(!list.contains(d[i].getWidth()+"x"+d[i].getHeight())){
list.add(d[i].getWidth()+"x"+d[i].getHeight());
}
}
String[] res = new String[list.size()];
for(int i=0; i<list.size() ;i++){
res[i] = list.get(i);
}
Arrays.sort(res);
return res;
}
private String[] getPixelres(DisplayMode[] d){
ArrayList<String> list = new ArrayList<String>();
for(int i=0; i<d.length ;i++){
if(!list.contains(""+d[i].getBitsPerPixel())){
list.add(""+d[i].getBitsPerPixel());
}
}
String[] pixel = new String[list.size()];
for(int i=0; i<list.size() ;i++){
pixel[i] = list.get(i);
}
sortString(pixel);
return pixel;
}
private String[] getFreq(DisplayMode[] d){
ArrayList<String> list = new ArrayList<String>();
for(int i=0; i<d.length ;i++){
if(!list.contains(""+d[i].getFrequency())){
list.add(""+d[i].getFrequency());
}
}
String[] freq = new String[list.size()];
for(int i=0; i<list.size() ;i++){
freq[i] = list.get(i);
}
sortString(freq);
return freq;
}
private void setList(JComboBox box, String[] list){
box.removeAllItems();
for(int i=0; i<list.length ;i++){
box.addItem(list[i]);
}
}
private void sortString(String[] list){
for(int i=0; i<list.length ;i++){
for(int j=i; j<list.length ;j++){
if(Integer.parseInt(list[i]) > Integer.parseInt(list[j])){
String temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
}
}
}

View file

@ -1,6 +1,7 @@
package ei.game;
import ei.engine.LWJGLGameWindow;
import ei.engine.LWJGLPropertiesDialog;
import ei.engine.state.GameStateManager;
import ei.game.gamestate.InGameState;
import ei.game.gamestate.MenuState;
@ -14,7 +15,7 @@ public class EI extends LWJGLGameWindow{
}
public EI() {
super("EI");
super("EI",60,LWJGLPropertiesDialog.SOW_ALWAYS);
}
protected void init(){