/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package thegame.layers;
import java.awt.*;
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import net.sf.jiga.xtended.impl.game.DisplayModeWrapper;
import net.sf.jiga.xtended.impl.game.RenderingScene;
import net.sf.jiga.xtended.impl.game.XtendedModel;
import net.sf.jiga.xtended.impl.game.gl.GLFX;
import net.sf.jiga.xtended.impl.game.gl.geom.GLGeom;
import net.sf.jiga.xtended.kernel.JXAenvUtils;
import net.sf.jiga.xtended.ui.UIMessage;
import thegame.*;
import thegame.gui.GameMenuElement;
import thegame.gui.SelectionMenuCell;
import thegame.gui.SelectionMenuComponent;
import thegame.io.ReadAndSaveSettings;
import thegame.layers.options.Field;
import thegame.layers.options.FieldAction;
import thegame.layers.options.FieldSlider;
/**
*
* @author www.b23prodtm.info
*/
public class Options extends Thegamelayer implements ReadAndSaveSettingsListener<GameProperties> {
SelectionMenuComponent optionsMenuComponent;
Field<Integer> speed;
Field<DisplayModeWrapper> displayMode;
Field<RenderingHints> renderingHints;
Field<Boolean> debugJXA;
Field<Boolean> debugLoadingActivity;
Field<Boolean> debugRender;
Field<Boolean> debugJXAKernel;
Field<Boolean> debugPhysics;
Field<Boolean> debugGui;
Field<Boolean> fileLoad;
Field<Boolean> fileSave;
Field<Boolean> fileDelete;
Field<Integer> fighterSpeed;
Field<Integer> fighterGravity;
Field<Integer> fighterWeight;
Field<Boolean> soundMute;
Field<Long> roundTime;
Field<Integer> roundCount;
private final ReadAndSaveSettings<GameProperties> rASSet;
public static final String settingsFilename = GameProperties.class.getCanonicalName() + "." + ReadAndSaveSettings._MIME_EXTS[0];
public static final File SETTINGSPATH = new File(JXAenvUtils._USERHOMEDIRECTORY.getPath() + File.separator + "Saved Games" + File.separator + settingsFilename);
/**
* resource path into the jar for an optional File of default settings. May
* return NULL !
*/
public static final URL SETTINGSRESOURCEPATH = GamePropertiesDialog.class.getResource("/thegame/" + settingsFilename);
private void initFields(GameProperties props) {
speed = new FieldSlider<Integer>("MENU_options_speed", new Integer[]{10, 20, 30}, 10);
displayMode = new FieldSlider<DisplayModeWrapper>("MENU_options_resolution", props.dmAvailable, props.dm);
renderingHints = new FieldSlider<RenderingHints>("MENU_options_filtering", props.renderings.values().toArray(new RenderingHints[]{}), props.rendering);
debugJXA = new FieldSlider<Boolean>("MENU_options_debug_jxa", new Boolean[]{true, false}, (props.getDebug() & props._DEBUG_JXA) != 0);
debugLoadingActivity = new FieldSlider<Boolean>("MENU_options_debug_loading_activity", new Boolean[]{true, false}, (props.getDebug() & props._DEBUG_LOADING_ACTIVITY) != 0);
debugRender = new FieldSlider<Boolean>("MENU_options_debug_render", new Boolean[]{true, false}, (props.getDebug() & props._DEBUG_RENDER) != 0);
debugJXAKernel = new FieldSlider<Boolean>("MENU_options_debug_kernel", new Boolean[]{true, false}, (props.getDebug() & props._DEBUG_JXA_KERNEL) != 0);
debugPhysics = new FieldSlider<Boolean>("MENU_options_debug_physics", new Boolean[]{true, false}, (props.getDebug() & props._DEBUG_PHYSICS) != 0);
debugGui = new FieldSlider<Boolean>("MENU_options_debug_gui", new Boolean[]{true, false}, (props.getDebug() & props._DEBUG_GUI) != 0);
fileLoad = new FieldAction<Boolean>("MENU_options_file_load", new FieldAction.Shell() {
@Override
protected Boolean execute() {
return loadSettings();
}
});
fileSave = new FieldAction<Boolean>("MENU_options_file_save", new FieldAction.Shell<Boolean>() {
@Override
protected Boolean execute() {
return saveSettings();
}
});
fileDelete = new FieldAction<Boolean>("MENU_options_file_delete", new FieldAction.Shell<Boolean>() {
@Override
protected Boolean execute() {
rASSet.deleteFile();
return true;
}
});
fighterSpeed = new FieldSlider<Integer>("MENU_options_fighter_speed", GameProperties.fighterSpeeds, props.fighterSpeed);
fighterGravity = new FieldSlider<Integer>("MENU_options_fighter_gravity", GameProperties.fighterGravities, props.fighterGravity);
fighterWeight = new FieldSlider<Integer>("MENU_options_fighter_weight", GameProperties.fighterWeights, props.fighterWeight);
soundMute = new FieldSlider<Boolean>("MENU_options_sound", new Boolean[]{true, false}, props.soundMute);
roundCount = new FieldSlider<Integer>("MENU_options_round_count", GameProperties.roundCounts, props.roundCount);
roundTime = new FieldSlider<Long>("MENU_options_round_time", GameProperties.roundTimes, props.roundTime);
List<SelectionMenuCell> cells = new ArrayList<SelectionMenuCell>();
add(speed, cells);
add(displayMode, cells);
add(renderingHints, cells);
add(debugJXA, cells);
add(debugLoadingActivity, cells);
add(debugRender, cells);
add(debugJXAKernel, cells);
add(debugPhysics, cells);
add(debugGui, cells);
add(fileLoad, cells);
add(fileSave, cells);
add(fileDelete, cells);
add(fighterSpeed, cells);
add(fighterGravity, cells);
add(fighterWeight, cells);
add(soundMute, cells);
add(roundCount, cells);
add(roundTime, cells);
optionsMenuComponent = new SelectionMenuComponent(gui, getName(), cells, SelectionMenuComponent.VERTICAL_LAYOUT);
optionsMenuComponent.setNumCellsToDisplay(5);
}
public boolean loadSettings() {
GameProperties props = rASSet.loadData();
if (props == null) {
return false;
} else {
synchronized (props.bgModels) {
FieldGui.clearBackgroundModelsStack();
for (Iterator<Map.Entry<String, XtendedModel[]>> it = props.bgModels.entrySet().iterator(); it.hasNext();) {
final Map.Entry<String, XtendedModel[]> s = it.next();
FieldGui.setBackgroundModels(s.getKey(), s.getValue());
}
}
synchronized (props.characters) {
FieldGui.clearPlayerModelsStack();
for (XtendedModel model : props.characters) {
FieldGui.addPlayerModel(model);
}
}
gui.setProps(props);
initFields(props);
return true;
}
}
public boolean saveSettings() {
rASSet.saveData();
return true;
}
public Options(FieldGui gui, String name) {
super(gui, name);
/**
* LAYOUT
*/
initFields(gui.getProps());
/**
* IO
*/
this.rASSet = new ReadAndSaveSettings<GameProperties>(SETTINGSPATH, gui.getProps());
rASSet.addListener(this);
}
long prg = UIMessage.newProgress(0, 100, null);
public void dataSaved(GameProperties data, String date) {
gui.logLn("Data File : Saved");
}
public void dataDeleted() {
gui.logLn("Data File ; Deleted");
}
public void dataLoaded(GameProperties data, String date) {
gui.logLn("Data File : Loaded");
data.deployContentsCaches(new CacheLoadListener() {
@Override
public void readCompleted() {
super.readCompleted();
gui.log(":");
}
public void loadBegin() {
super.loadBegin();
gui.log("Cache loading... [");
}
public void loadIsComplete() {
super.loadIsComplete();
gui.logLn("] Done.");
}
}, prg);
}
public void dataNotFound() {
gui.logLn("Data File : Not Found");
}
public void dataError(Object e) {
gui.logLn("Data File : " + e.toString());
}
public void dataSaving(String m) {
gui.logLn("Data File : Saving...");
}
public void dataLoading(String m) {
gui.logLn("Data File : Loading...");
}
/**
* add Field to the options menu (UI intialization process)
*/
private void add(final Field f, List<SelectionMenuCell> cells) {
GameMenuElement._addToMenu(gui, cells, f.name, new Runnable() {
public void run() {
gui.getMenu_handler().setCurrent(f.makeOptionSlider(Options.this, optionsMenuComponent, new ArrayList<SelectionMenuCell>()));
}
}, null);
}
@Override
protected void doLoad() {
super.doLoad();
optionsMenuComponent.reset();
this.gui.getMenu_handler().setCurrent(optionsMenuComponent);
}
@Override
protected void renderToGL(RenderingScene context) {
super.renderToGL(context);
double z = FieldGui.menuDepth;
Dimension menuSize = new Dimension((int) ((float) context.getWidth() * .8f), (int) ((float) context.getHeight() * 0.95f));
Rectangle menuBounds = new Rectangle(
(int) Math.round((float) (context.getWidth() - menuSize.width) / 2f),
context.getHeight() - menuSize.height,
menuSize.width,
menuSize.height);
menuBounds.setSize(FieldGui._GLgetDimZRatio(context, z, menuSize));
gui.getMenu_handler().getCurrent().setBounds(menuBounds);
gui.getMenu_handler().getCurrent().GLpaintComponent(context, z, GLFX.gfx.FX_DOWNREFLEXION_EXT, new Point(0, context.getHeight()), GLGeom.getGlowingColor(Color.ORANGE, FieldGui.glow.glow));
}
}