opsu-dance/src/yugecin/opsudance/ui/SBOverlay.java

245 lines
6.6 KiB
Java
Raw Normal View History

2016-11-12 22:30:22 +01:00
/*
* opsu!dance - fork of opsu! with cursordance auto
* Copyright (C) 2016 yugecin
*
* opsu!dance is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* opsu!dance is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with opsu!dance. If not, see <http://www.gnu.org/licenses/>.
*/
package yugecin.opsudance.ui;
2016-11-12 23:55:39 +01:00
import itdelatrisu.opsu.Options;
2016-11-12 22:30:22 +01:00
import itdelatrisu.opsu.audio.MusicController;
import itdelatrisu.opsu.objects.GameObject;
2016-11-12 23:13:41 +01:00
import itdelatrisu.opsu.states.Game;
2016-11-12 22:30:22 +01:00
import itdelatrisu.opsu.ui.Fonts;
import org.newdawn.slick.Color;
2016-11-12 23:31:18 +01:00
import org.newdawn.slick.GameContainer;
2016-11-12 22:30:22 +01:00
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
2016-11-12 23:31:18 +01:00
import org.newdawn.slick.state.StateBasedGame;
import yugecin.opsudance.ObjectColorOverrides;
2016-11-12 22:30:22 +01:00
import java.util.HashMap;
2016-11-12 23:55:39 +01:00
import java.util.Map;
2016-11-12 22:30:22 +01:00
2016-11-12 23:55:39 +01:00
@SuppressWarnings("unchecked")
2016-11-12 22:30:22 +01:00
public class SBOverlay {
2016-11-13 01:45:06 +01:00
public static boolean isActive = false;
2016-11-12 22:30:22 +01:00
private boolean hide;
private boolean menu;
private int width;
private int height;
private int speed;
private GameObject[] gameObjects;
private HashMap[] optionsMap;
private HashMap<Options.GameOption, String> initialOptions;
2016-11-12 22:30:22 +01:00
2016-11-12 23:13:41 +01:00
private int index;
private final Game game;
2016-11-12 22:30:22 +01:00
private final OptionsOverlay options;
2016-11-12 23:13:41 +01:00
public SBOverlay(Game game) {
this.game = game;
2016-11-12 23:55:39 +01:00
options = new OptionsOverlay(this);
initialOptions = new HashMap<>();
2016-11-12 22:30:22 +01:00
}
2016-11-12 23:31:18 +01:00
public void init(GameContainer container, Input input, int width, int height) {
2016-11-12 22:30:22 +01:00
this.width = width;
this.height = height;
speed = 10;
gameObjects = new GameObject[0];
2016-11-12 23:31:18 +01:00
options.init(container, input, width, height);
2016-11-12 22:30:22 +01:00
}
2016-11-12 23:31:18 +01:00
public void render(GameContainer container, StateBasedGame game, Graphics g) {
2016-11-12 22:30:22 +01:00
if (!isActive || hide) {
return;
}
int lh = Fonts.SMALL.getLineHeight();
Fonts.SMALL.drawString(10, height - 50, "speed: C " + (speed / 10f) + " V", Color.cyan);
Fonts.SMALL.drawString(10, height - 50 - lh, "Menu: N", Color.cyan);
Fonts.SMALL.drawString(10, height - 50 - lh * 2, "HIDE: H", Color.cyan);
2016-11-12 23:13:41 +01:00
Fonts.SMALL.drawString(10, height - 50 - lh * 3, "obj: J " + index + " K", Color.cyan);
if (index < optionsMap.length && optionsMap[index] != null) {
int i = 0;
for (Object o : optionsMap[index].entrySet()) {
Map.Entry<Options.GameOption, String> option = (Map.Entry<Options.GameOption, String>) o;
Fonts.SMALL.drawString(10, 50 + i++ * lh, option.getKey().getDisplayName(), Color.cyan);
}
}
2016-11-12 22:30:22 +01:00
if (menu) {
2016-11-12 23:31:18 +01:00
options.render(container, game, g);
2016-11-12 22:30:22 +01:00
}
}
public void update(int mouseX, int mouseY) {
if (!isActive) {
return;
}
if (menu) {
options.update(mouseX, mouseY);
}
}
2016-11-12 23:55:39 +01:00
public boolean keyPressed(int key, char c) {
2016-11-12 22:30:22 +01:00
if (!isActive) {
return false;
}
2016-11-12 23:55:39 +01:00
if (options.keyPressed(key, c)) {
return true;
}
2016-11-12 22:30:22 +01:00
if (key == Input.KEY_C && speed > 0) {
speed -= 1;
if (speed == 0) {
MusicController.pause();
} else {
MusicController.setPitch(speed / 10f);
}
} else if (key == Input.KEY_V && speed < 21) {
if (speed == 0) {
MusicController.resume();
}
speed += 1;
MusicController.setPitch(speed / 10f);
} else if (key == Input.KEY_H) {
hide = !hide;
} else if (key == Input.KEY_N) {
menu = !menu;
2016-11-12 23:13:41 +01:00
if (menu && speed != 0) {
MusicController.pause();
} else if (!menu && speed != 0) {
MusicController.resume();
}
} else if (key == Input.KEY_J && index > 0) {
index--;
setMusicPosition();
goBackOneSBIndex();
2016-11-12 23:13:41 +01:00
} else if (key == Input.KEY_K && index < gameObjects.length - 1) {
index++;
setMusicPosition();
updateIndex(index);
2016-11-13 00:41:04 +01:00
} else if (key == Input.KEY_ESCAPE && menu) {
menu = false;
if (speed != 0) {
MusicController.resume();
}
return true;
2016-11-12 22:30:22 +01:00
}
return false;
}
private void goBackOneSBIndex() {
if (optionsMap[index + 1] != null) {
// new options on previous index, so to revert then we have to reload them all to this point..
final int thisIndex = index;
for (int i = 0; i <= thisIndex; i++) {
updateIndex(thisIndex);
}
}
}
2016-11-12 23:13:41 +01:00
private void setMusicPosition() {
game.setObjectIndex(index);
if (speed != 0) {
MusicController.setPitch(speed / 10f);
MusicController.resume();
} else {
MusicController.pause();
}
}
2016-11-12 22:30:22 +01:00
public void setGameObjects(GameObject[] gameObjects) {
if (this.gameObjects.length != gameObjects.length) {
optionsMap = new HashMap[gameObjects.length];
}
this.gameObjects = gameObjects;
}
2016-11-13 00:43:29 +01:00
public void saveOption(Options.GameOption option) {
2016-11-12 23:55:39 +01:00
if (optionsMap[index] == null) {
optionsMap[index] = new HashMap<>();
}
2016-11-13 00:43:29 +01:00
optionsMap[index].put(option, option.write());
readOption(option);
2016-11-12 23:55:39 +01:00
}
2016-11-12 22:30:22 +01:00
public boolean mousePressed(int button, int x, int y) {
return menu && options.mousePressed(button, x, y);
}
public void mouseDragged(int oldx, int oldy, int newx, int newy) {
if (menu) options.mouseDragged(oldx, oldy, newx, newy);
}
2016-11-12 23:13:41 +01:00
public void updateIndex(int index) {
this.index = index;
2016-11-13 00:27:38 +01:00
if (index >= optionsMap.length) {
return;
}
2016-11-12 23:55:39 +01:00
HashMap options = optionsMap[index];
if (options != null) {
for (Object o : options.entrySet()) {
Map.Entry<Options.GameOption, String> next = (Map.Entry<Options.GameOption, String>) o;
next.getKey().read(next.getValue());
2016-11-13 00:58:50 +01:00
readOption(next.getKey());
2016-11-12 23:55:39 +01:00
}
}
2016-11-12 23:13:41 +01:00
}
2016-11-12 23:31:18 +01:00
public boolean mouseReleased(int button, int x, int y) {
return menu && options.mouseReleased(button, x, y);
}
public boolean mouseWheelMoved(int newValue) {
return menu && options.mouseWheenMoved(newValue);
}
public void enter() {
// enter, save current settings
for (Options.GameOption o : options.getSavedOptionList()) {
initialOptions.put(o, o.write());
}
}
public void leave() {
// leave, revert the settings saved before entering
for (Options.GameOption o : options.getSavedOptionList()) {
if (initialOptions.containsKey(o)) {
o.read(initialOptions.get(o));
2016-11-13 00:58:50 +01:00
readOption(o);
}
}
}
// needed for object color overrides...
private void readOption(Options.GameOption o) {
if (o == Options.GameOption.DANCE_OBJECT_COLOR_OVERRIDE
|| o == Options.GameOption.DANCE_OBJECT_COLOR_OVERRIDE_MIRRORED
|| o == Options.GameOption.DANCE_RGB_OBJECT_INC) {
if (index < gameObjects.length) {
ObjectColorOverrides.hue = gameObjects[index].getHue();
}
2016-11-13 00:58:50 +01:00
for (int i = index; i < gameObjects.length; i++) {
gameObjects[i].updateColor();
}
}
}
2016-11-12 22:30:22 +01:00
}