make options menu use lists

This commit is contained in:
yugecin 2016-09-27 17:36:10 +02:00
parent d2ff927ab6
commit 4769c1cfda
3 changed files with 107 additions and 26 deletions

View File

@ -40,6 +40,8 @@ public class Container extends AppGameContainer {
/** SlickException causing game failure. */ /** SlickException causing game failure. */
protected SlickException e = null; protected SlickException e = null;
public static Container instance;
/** /**
* Create a new container wrapping a game * Create a new container wrapping a game
* *
@ -48,6 +50,7 @@ public class Container extends AppGameContainer {
*/ */
public Container(Game game) throws SlickException { public Container(Game game) throws SlickException {
super(game); super(game);
instance = this;
} }
/** /**
@ -61,6 +64,7 @@ public class Container extends AppGameContainer {
*/ */
public Container(Game game, int width, int height, boolean fullscreen) throws SlickException { public Container(Game game, int width, int height, boolean fullscreen) throws SlickException {
super(game, width, height, fullscreen); super(game, width, height, fullscreen);
instance = this;
} }
@Override @Override

View File

@ -34,9 +34,7 @@ import java.io.IOException;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.net.URI; import java.net.URI;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.*;
import java.util.HashMap;
import java.util.Locale;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.jar.Attributes; import java.util.jar.Attributes;
import java.util.jar.JarFile; import java.util.jar.JarFile;
@ -282,20 +280,20 @@ public class Options {
public String getValueString() { return resolution.toString(); } public String getValueString() { return resolution.toString(); }
@Override @Override
public void click(GameContainer container) { public Object[] getListItems() {
do { return Resolution.values();
resolution = resolution.next(); }
} while (resolution != Resolution.RES_800_600 && (
container.getScreenWidth() < resolution.getWidth() || @Override
container.getScreenHeight() < resolution.getHeight())); public void clickListItem(int index) {
resolution = Resolution.values()[index];
} }
@Override @Override
public void read(String s) { public void read(String s) {
try { try {
Resolution res = Resolution.valueOf(String.format("RES_%s", s.replace('x', '_'))); resolution = Resolution.valueOf(String.format("RES_%s", s.replace('x', '_')));
resolution = res; } catch (IllegalArgumentException ignored) {}
} catch (IllegalArgumentException e) {}
} }
}, },
// FULLSCREEN ("Fullscreen Mode", "Fullscreen", "Restart to apply changes.", false), // FULLSCREEN ("Fullscreen Mode", "Fullscreen", "Restart to apply changes.", false),
@ -304,9 +302,13 @@ public class Options {
public String getValueString() { return skinName; } public String getValueString() { return skinName; }
@Override @Override
public void click(GameContainer container) { public Object[] getListItems() {
skinDirIndex = (skinDirIndex + 1) % skinDirs.length; return skinDirs;
skinName = skinDirs[skinDirIndex]; }
@Override
public void clickListItem(int index) {
skinName = skinDirs[index];
} }
@Override @Override
@ -319,10 +321,19 @@ public class Options {
} }
@Override @Override
public void click(GameContainer container) { public Object[] getListItems() {
targetFPSindex = (targetFPSindex + 1) % targetFPS.length; String[] list = new String[targetFPS.length];
container.setTargetFrameRate(getTargetFPS()); for (int i = 0; i < targetFPS.length; i++) {
container.setVSync(getTargetFPS() == 60); list[i] = String.format(targetFPS[i] == 60 ? "%dfps (vsync)" : "%dfps", targetFPS[i]);
}
return list;
}
@Override
public void clickListItem(int index) {
targetFPSindex = index;
Container.instance.setTargetFrameRate(targetFPS[index]);
Container.instance.setVSync(targetFPS[index] == 60);
} }
@Override @Override
@ -360,7 +371,14 @@ public class Options {
public String getValueString() { return screenshotFormat[screenshotFormatIndex].toUpperCase(); } public String getValueString() { return screenshotFormat[screenshotFormatIndex].toUpperCase(); }
@Override @Override
public void click(GameContainer container) { screenshotFormatIndex = (screenshotFormatIndex + 1) % screenshotFormat.length; } public Object[] getListItems() {
return screenshotFormat;
}
@Override
public void clickListItem(int index) {
screenshotFormatIndex = index;
}
@Override @Override
public String write() { return Integer.toString(screenshotFormatIndex); } public String write() { return Integer.toString(screenshotFormatIndex); }
@ -658,6 +676,18 @@ public class Options {
*/ */
public void click(GameContainer container) { bool = !bool; } public void click(GameContainer container) { bool = !bool; }
/**
* Get a list of values to choose from
* @return list with value string or null if no list should be shown
*/
public Object[] getListItems() { return null; }
/**
* Fired when an item in the value list has been clicked
* @param index the itemindex which has been clicked
*/
public void clickListItem(int index) { };
/** /**
* Processes a mouse drag action (via override). * Processes a mouse drag action (via override).
* <p> * <p>

View File

@ -31,8 +31,7 @@ import itdelatrisu.opsu.ui.Fonts;
import itdelatrisu.opsu.ui.MenuButton; import itdelatrisu.opsu.ui.MenuButton;
import itdelatrisu.opsu.ui.UI; import itdelatrisu.opsu.ui.UI;
import java.util.Arrays; import java.util.*;
import java.util.Collections;
import org.newdawn.slick.Color; import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer; import org.newdawn.slick.GameContainer;
@ -44,6 +43,7 @@ import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame; import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.state.transition.FadeInTransition; import org.newdawn.slick.state.transition.FadeInTransition;
import org.newdawn.slick.state.transition.EmptyTransition; import org.newdawn.slick.state.transition.EmptyTransition;
import yugecin.opsudance.ui.ItemList;
/** /**
* "Game Options" state. * "Game Options" state.
@ -174,13 +174,18 @@ public class OptionsMenu extends BasicGameState {
private Graphics g; private Graphics g;
private final int state; private final int state;
private final ItemList list;
public OptionsMenu(int state) { public OptionsMenu(int state) {
this.state = state; this.state = state;
list = new ItemList();
} }
@Override @Override
public void init(GameContainer container, StateBasedGame game) public void init(GameContainer container, StateBasedGame game)
throws SlickException { throws SlickException {
list.init(container);
this.container = container; this.container = container;
this.game = game; this.game = game;
this.input = container.getInput(); this.input = container.getInput();
@ -241,7 +246,7 @@ public class OptionsMenu extends BasicGameState {
for (OptionTab tab : OptionTab.VALUES_REVERSED) { for (OptionTab tab : OptionTab.VALUES_REVERSED) {
if (tab != currentTab) if (tab != currentTab)
UI.drawTab(tab.button.getX(), tab.button.getY(), UI.drawTab(tab.button.getX(), tab.button.getY(),
tab.getName(), false, tab == hoverTab); tab.getName(), false, tab == hoverTab && !list.isVisible());
} }
UI.drawTab(currentTab.button.getX(), currentTab.button.getY(), UI.drawTab(currentTab.button.getX(), currentTab.button.getY(),
currentTab.getName(), true, false); currentTab.getName(), true, false);
@ -251,6 +256,10 @@ public class OptionsMenu extends BasicGameState {
g.drawLine(0, lineY, width, lineY); g.drawLine(0, lineY, width, lineY);
g.resetLineWidth(); g.resetLineWidth();
if (list.isVisible()) {
list.render(container, game, g);
}
UI.getBackButton().draw(); UI.getBackButton().draw();
// key entry state // key entry state
@ -282,8 +291,20 @@ public class OptionsMenu extends BasicGameState {
@Override @Override
public int getID() { return state; } public int getID() { return state; }
@Override
public void mouseReleased(int button, int x, int y) {
if (list.isVisible()) {
list.mouseReleased(button, x, y);
}
}
@Override @Override
public void mousePressed(int button, int x, int y) { public void mousePressed(int button, int x, int y) {
if (list.isVisible()) {
list.mousePressed(button, x, y);
return;
}
// key entry state // key entry state
if (keyEntryLeft || keyEntryRight) { if (keyEntryLeft || keyEntryRight) {
keyEntryLeft = keyEntryRight = false; keyEntryLeft = keyEntryRight = false;
@ -313,9 +334,22 @@ public class OptionsMenu extends BasicGameState {
} }
// options (click only) // options (click only)
GameOption option = getOptionAt(y); final GameOption option = getOptionAt(y);
if (option != null) if (option != null) {
Object[] listItems = option.getListItems();
if (listItems == null) {
option.click(container); option.click(container);
} else {
list.setItems(listItems);
list.setClickListener(new Observer() {
@Override
public void update(Observable o, Object arg) {
option.clickListItem((int) arg);
}
});
list.show();
}
}
// special key entry states // special key entry states
if (option == GameOption.KEY_LEFT) { if (option == GameOption.KEY_LEFT) {
@ -329,6 +363,11 @@ public class OptionsMenu extends BasicGameState {
@Override @Override
public void mouseDragged(int oldx, int oldy, int newx, int newy) { public void mouseDragged(int oldx, int oldy, int newx, int newy) {
if (list.isVisible()) {
list.mouseDragged(oldx, oldy, newx, newy);
return;
}
// key entry state // key entry state
if (keyEntryLeft || keyEntryRight) if (keyEntryLeft || keyEntryRight)
return; return;
@ -356,12 +395,20 @@ public class OptionsMenu extends BasicGameState {
@Override @Override
public void mouseWheelMoved(int newValue) { public void mouseWheelMoved(int newValue) {
if (list.isVisible()) {
list.mouseWheelMoved(newValue);
}
if (input.isKeyDown(Input.KEY_LALT) || input.isKeyDown(Input.KEY_RALT)) if (input.isKeyDown(Input.KEY_LALT) || input.isKeyDown(Input.KEY_RALT))
UI.changeVolume((newValue < 0) ? -1 : 1); UI.changeVolume((newValue < 0) ? -1 : 1);
} }
@Override @Override
public void keyPressed(int key, char c) { public void keyPressed(int key, char c) {
if (list.isVisible()) {
list.keyPressed(key, c);
return;
}
// key entry state // key entry state
if (keyEntryLeft || keyEntryRight) { if (keyEntryLeft || keyEntryRight) {
if (keyEntryLeft) if (keyEntryLeft)