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

913 lines
29 KiB
Java
Raw Normal View History

2016-12-11 01:17:30 +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;
import itdelatrisu.opsu.GameImage;
import itdelatrisu.opsu.Options;
2016-12-11 01:17:30 +01:00
import itdelatrisu.opsu.Options.GameOption;
2016-12-11 02:08:35 +01:00
import itdelatrisu.opsu.Options.GameOption.OptionType;
2016-12-11 01:17:30 +01:00
import itdelatrisu.opsu.Utils;
2016-12-11 02:18:50 +01:00
import itdelatrisu.opsu.audio.SoundController;
import itdelatrisu.opsu.audio.SoundEffect;
2016-12-11 01:17:30 +01:00
import itdelatrisu.opsu.ui.Colors;
import itdelatrisu.opsu.ui.Fonts;
import itdelatrisu.opsu.ui.UI;
2017-01-29 11:28:24 +01:00
import itdelatrisu.opsu.ui.animations.AnimationEquation;
2016-12-11 02:08:35 +01:00
import org.newdawn.slick.*;
2017-01-29 11:28:24 +01:00
import org.newdawn.slick.gui.TextField;
2017-01-19 16:03:53 +01:00
import yugecin.opsudance.core.DisplayContainer;
import yugecin.opsudance.core.state.OverlayOpsuState;
2017-01-29 11:28:24 +01:00
import yugecin.opsudance.utils.FontUtil;
2016-12-11 01:17:30 +01:00
2017-01-19 16:03:53 +01:00
public class OptionsOverlay extends OverlayOpsuState {
2016-12-11 01:17:30 +01:00
2017-01-19 16:03:53 +01:00
private final DisplayContainer displayContainer;
2016-12-11 02:08:35 +01:00
2017-01-29 11:28:24 +01:00
private static final float BG_ALPHA = 0.7f;
private static final float LINEALPHA = 0.8f;
private static final Color COL_BG = new Color(Color.black);
private static final Color COL_WHITE = new Color(1f, 1f, 1f);
private static final Color COL_PINK = new Color(235, 117, 139);
private static final Color COL_CYAN = new Color(88, 218, 254);
private static final Color COL_GREY = new Color(55, 55, 57);
private static final Color COL_BLUE = new Color(Colors.BLUE_BACKGROUND);
private static final Color COL_COMBOBOX_HOVER = new Color(185, 19, 121);
private static final float INDICATOR_ALPHA = 0.8f;
private static final Color COL_INDICATOR = new Color(Color.black);
/** Duration, in ms, of the show (slide-in) animation. */
private static final int SHOWANIMATIONTIME = 1000;
/** Current value of the animation timer. */
private int animationtime;
/** Duration, in ms, of the hide animation. */
private int hideAnimationTime;
/** How much the show animation progressed when the hide request was made. */
private float hideAnimationStartProgress;
/** Target duration, in ms, of the move animation for the indicator. */
private static final int INDICATORMOVEANIMATIONTIME = 166;
/** Selected option indicator virtual position. */
private int indicatorPos;
/** Selected option indicator offset to next position. */
private int indicatorOffsetToNextPos;
/** Selected option indicator move to next position animation time past. */
private int indicatorMoveAnimationTime;
/** Target duration, in ms, of the fadeout animation for the indicator. */
private static final int INDICATORHIDEANIMATIONTIME = 500;
/** Selected option indicator hide animation time past. */
private int indicatorHideAnimationTime;
2017-01-19 19:23:31 +01:00
private Listener listener;
2017-01-19 16:03:53 +01:00
private Image sliderBallImg;
private Image checkOnImg;
private Image checkOffImg;
2017-01-29 11:28:24 +01:00
private Image chevronDownImg;
private Image chevronRightImg;
private Image searchImg;
2016-12-11 01:17:30 +01:00
2017-01-29 11:28:24 +01:00
private OptionTab[] sections;
2016-12-11 16:41:56 +01:00
2016-12-11 01:17:30 +01:00
private GameOption hoverOption;
private GameOption selectedOption;
2017-01-29 11:28:24 +01:00
private GameOption selectedListOption;
2016-12-11 16:41:56 +01:00
2016-12-11 10:08:22 +01:00
private int sliderOptionStartX;
private int sliderOptionLength;
private boolean isAdjustingSlider;
2016-12-11 16:41:56 +01:00
2017-01-29 11:28:24 +01:00
private static final int LISTOPENANIMATIONTIME = 200;
private static final int LISTCLOSEANIMATIONTIME = 175;
private int listAnimationTime;
2016-12-11 02:08:35 +01:00
private boolean isListOptionOpen;
2016-12-11 16:41:56 +01:00
private int listItemHeight;
2016-12-11 11:07:28 +01:00
private int listStartX;
private int listStartY;
private int listWidth;
private int listHeight;
private int listHoverIndex;
2016-12-11 01:17:30 +01:00
2017-01-29 11:28:24 +01:00
private int finalWidth;
2016-12-11 01:17:30 +01:00
private int width;
private int height;
private int optionWidth;
private int optionStartX;
private int optionStartY;
private int optionHeight;
2017-01-29 11:28:24 +01:00
private int optionTextOffsetY;
private int controlImageSize;
private int controlImagePadding;
private static final int LINEWIDTH = 3;
private int paddingRight;
private int paddingLeft;
private int paddingTextLeft;
private int textOptionsY;
private int textChangeY;
private int posSearchY;
private int textSearchYOffset;
2016-12-11 01:17:30 +01:00
private int scrollOffset;
private int maxScrollOffset;
2016-12-11 01:17:30 +01:00
private int mousePressY;
private boolean keyEntryLeft;
private boolean keyEntryRight;
2016-12-11 16:32:21 +01:00
private int prevMouseX;
private int prevMouseY;
private int sliderSoundDelay;
2017-01-29 11:28:24 +01:00
private TextField searchField;
private String lastSearchText;
public OptionsOverlay(DisplayContainer displayContainer, OptionTab[] sections) {
2017-01-19 16:03:53 +01:00
this.displayContainer = displayContainer;
2016-12-11 01:17:30 +01:00
2017-01-29 11:28:24 +01:00
this.sections = sections;
2016-12-11 01:17:30 +01:00
2016-12-12 18:46:57 +01:00
listHoverIndex = -1;
2017-01-29 11:28:24 +01:00
searchField = new TextField(displayContainer, null, 0, 0, 0, 0);
2017-01-19 16:03:53 +01:00
}
2017-01-19 19:23:31 +01:00
public void setListener(Listener listener) {
this.listener = listener;
}
2017-01-19 16:03:53 +01:00
@Override
public void revalidate() {
super.revalidate();
2016-12-12 18:46:57 +01:00
2017-01-29 11:28:24 +01:00
finalWidth = Math.max((int) (displayContainer.width * 0.321f), 340);
2017-01-19 16:03:53 +01:00
height = displayContainer.height;
2016-12-11 01:17:30 +01:00
// calculate positions
2017-01-29 11:28:24 +01:00
paddingRight = (int) (displayContainer.width * 0.009375f); // not so accurate
paddingLeft = (int) (displayContainer.width * 0.0180f); // not so accurate
paddingTextLeft = paddingLeft + LINEWIDTH + (int) (displayContainer.width * 0.00625); // not so accurate
optionStartX = paddingTextLeft;
textOptionsY = Fonts.LARGE.getLineHeight() * 2;
textChangeY = textOptionsY + Fonts.LARGE.getLineHeight();
posSearchY = textChangeY + Fonts.MEDIUM.getLineHeight() * 2;
textSearchYOffset = Fonts.MEDIUM.getLineHeight() / 2;
optionStartY = posSearchY + Fonts.MEDIUM.getLineHeight() + Fonts.LARGE.getLineHeight();
optionHeight = (int) (Fonts.MEDIUM.getLineHeight() * 1.3f);
optionTextOffsetY = (int) ((optionHeight - Fonts.MEDIUM.getLineHeight()) / 2f);
listItemHeight = (int) (optionHeight * 4f / 5f);
2017-01-29 11:28:24 +01:00
controlImageSize = (int) (Fonts.MEDIUM.getLineHeight() * 0.7f);
controlImagePadding = (optionHeight - controlImageSize) / 2;
sliderBallImg = GameImage.CONTROL_SLIDER_BALL.getImage().getScaledCopy(controlImageSize, controlImageSize);
checkOnImg = GameImage.CONTROL_CHECK_ON.getImage().getScaledCopy(controlImageSize, controlImageSize);
checkOffImg = GameImage.CONTROL_CHECK_OFF.getImage().getScaledCopy(controlImageSize, controlImageSize);
chevronDownImg = GameImage.CHEVRON_DOWN.getImage().getScaledCopy(controlImageSize, controlImageSize);
chevronRightImg = GameImage.CHEVRON_RIGHT.getImage().getScaledCopy(controlImageSize, controlImageSize);
chevronRightImg.setImageColor(0f, 0f, 0f);
int searchImgSize = (int) (Fonts.LARGE.getLineHeight() * 0.75f);
searchImg = GameImage.SEARCH.getImage().getScaledCopy(searchImgSize, searchImgSize);
2016-12-11 01:17:30 +01:00
2017-01-29 11:28:24 +01:00
scrollOffset = 0;
2016-12-11 01:17:30 +01:00
}
2017-01-19 16:03:53 +01:00
@Override
public void onRender(Graphics g) {
2017-01-29 11:28:24 +01:00
g.setClip(0, 0, width, height);
2016-12-11 01:17:30 +01:00
// bg
2017-01-29 11:28:24 +01:00
g.setColor(COL_BG);
2016-12-11 01:17:30 +01:00
g.fillRect(0, 0, width, height);
// title
renderTitle();
2017-01-29 11:28:24 +01:00
renderIndicator(g);
2016-12-11 01:17:30 +01:00
// options
renderOptions(g);
2017-01-29 11:28:24 +01:00
renderOpenList(g);
renderSearch(g);
2016-12-11 11:07:28 +01:00
// scrollbar
2017-01-29 11:28:24 +01:00
g.setColor(COL_WHITE);
g.fillRect(width - 5, ((float) scrollOffset / (maxScrollOffset)) * (height - 45), 5, 45);
2016-12-11 11:07:28 +01:00
g.clearClip();
2016-12-11 01:17:30 +01:00
2016-12-11 02:33:50 +01:00
// UI
2016-12-11 01:17:30 +01:00
UI.getBackButton().draw();
2016-12-11 02:33:50 +01:00
// tooltip
2017-01-19 16:03:53 +01:00
renderTooltip(g);
2016-12-11 12:47:33 +01:00
// key input options
if (keyEntryLeft || keyEntryRight) {
renderKeyEntry(g);
}
}
2017-01-29 11:28:24 +01:00
private void renderIndicator(Graphics g) {
g.setColor(COL_INDICATOR);
int indicatorPos = this.indicatorPos;
if (indicatorMoveAnimationTime > 0) {
indicatorMoveAnimationTime += displayContainer.renderDelta;
if (indicatorMoveAnimationTime > INDICATORMOVEANIMATIONTIME) {
indicatorMoveAnimationTime = 0;
indicatorPos += indicatorOffsetToNextPos;
indicatorOffsetToNextPos = 0;
this.indicatorPos = indicatorPos;
} else {
indicatorPos += AnimationEquation.OUT_BACK.calc((float) indicatorMoveAnimationTime / INDICATORMOVEANIMATIONTIME) * indicatorOffsetToNextPos;
}
}
g.fillRect(0, indicatorPos - scrollOffset, width, optionHeight);
}
private void renderKeyEntry(Graphics g) {
2017-01-29 11:28:24 +01:00
g.setColor(COL_BG);
g.fillRect(0, 0, displayContainer.width, height);
g.setColor(COL_WHITE);
String prompt = (keyEntryLeft) ? "Please press the new left-click key." : "Please press the new right-click key.";
2017-01-29 11:28:24 +01:00
int y = (displayContainer.height - Fonts.LARGE.getLineHeight()) / 2;
FontUtil.drawCentered(Fonts.LARGE, displayContainer.width, 0, y, prompt, COL_WHITE);
2016-12-11 02:33:50 +01:00
}
2017-01-19 16:03:53 +01:00
private void renderTooltip(Graphics g) {
2016-12-11 02:33:50 +01:00
if (hoverOption != null) {
2017-01-29 11:28:24 +01:00
String tip = hoverOption.getDescription();
if (hoverOption.getType() == OptionType.NUMERIC) {
tip = "(" + hoverOption.getValueString() + ") " + tip;
}
UI.updateTooltip(displayContainer.renderDelta, tip, false);
2017-01-19 16:03:53 +01:00
UI.drawTooltip(g);
2016-12-11 02:33:50 +01:00
}
2016-12-11 01:17:30 +01:00
}
private void renderOptions(Graphics g) {
int y = -scrollOffset + optionStartY;
2017-01-29 11:28:24 +01:00
maxScrollOffset = optionStartY;
boolean render = true;
2017-01-29 11:28:24 +01:00
int lastNonSkippedSectionIndex = 0;
int sectionIndex = 0;
for (; sectionIndex < sections.length; sectionIndex++) {
lastNonSkippedSectionIndex = sectionIndex;
OptionTab section = sections[sectionIndex];
if (section.filtered) {
continue;
}
int lineStartY = (int) (y + Fonts.LARGE.getLineHeight() * 0.6f);
if (render) {
if (section.options == null) {
FontUtil.drawRightAligned(Fonts.XLARGE, width, -paddingRight, (int) (y + Fonts.XLARGE.getLineHeight() * 0.3f), section.name, COL_CYAN);
} else {
Fonts.MEDIUMBOLD.drawString(paddingTextLeft, lineStartY, section.name, COL_WHITE);
}
2016-12-11 01:17:30 +01:00
}
2017-01-29 11:28:24 +01:00
y += Fonts.LARGE.getLineHeight() * 1.5f;
maxScrollOffset += Fonts.LARGE.getLineHeight() * 1.5f;
if (section.options == null) {
continue;
}
int lineHeight = (int) (Fonts.LARGE.getLineHeight() * 0.9f);
for (int optionIndex = 0; optionIndex < section.options.length; optionIndex++) {
GameOption option = section.options[optionIndex];
if (!option.showCondition() || option.isFiltered()) {
2016-12-11 01:17:30 +01:00
continue;
}
2017-01-29 11:28:24 +01:00
if ((y > -optionHeight && render) || (isListOptionOpen && hoverOption == option)) {
renderOption(g, option, y);
2016-12-11 01:17:30 +01:00
}
y += optionHeight;
2017-01-29 11:28:24 +01:00
maxScrollOffset += optionHeight;
lineHeight += optionHeight;
2016-12-11 01:17:30 +01:00
if (y > height) {
render = false;
2017-01-29 11:28:24 +01:00
sectionIndex = sections.length;
maxScrollOffset += (section.options.length - optionIndex - 1) * optionHeight;
2016-12-11 01:17:30 +01:00
}
}
2017-01-29 11:28:24 +01:00
g.setColor(COL_GREY);
g.fillRect(paddingLeft, lineStartY, LINEWIDTH, lineHeight);
}
// iterate over skipped options to correctly calculate max scroll offset
for (sectionIndex = lastNonSkippedSectionIndex + 1; sectionIndex < sections.length; sectionIndex++) {
maxScrollOffset += Fonts.LARGE.getLineHeight() * 1.5f;
if (sections[sectionIndex].options != null) {
maxScrollOffset += sections[sectionIndex].options.length * optionHeight;
}
}
maxScrollOffset -= height * 2 / 3;
if (maxScrollOffset < 0) {
maxScrollOffset = 0;
}
if (scrollOffset > maxScrollOffset) {
scrollOffset = maxScrollOffset;
2016-12-11 01:17:30 +01:00
}
2016-12-11 11:07:28 +01:00
}
2016-12-11 11:07:28 +01:00
private void renderOpenList(Graphics g) {
2017-01-29 11:28:24 +01:00
if (!isListOptionOpen && listAnimationTime == 0) {
return;
}
float progress;
int listItemHeight;
if (isListOptionOpen) {
listAnimationTime = Math.min(LISTOPENANIMATIONTIME, listAnimationTime + displayContainer.renderDelta);
progress = (float) listAnimationTime / LISTOPENANIMATIONTIME;
} else {
listAnimationTime -= displayContainer.renderDelta;
if (listAnimationTime <= 0) {
listAnimationTime = 0;
return;
}
progress = (float) listAnimationTime / LISTCLOSEANIMATIONTIME;
}
listItemHeight = (int) (this.listItemHeight * progress);
listHeight = (int) (this.listHeight * progress);
final int borderRadius = 6;
float blackAlphaA = Colors.BLACK_ALPHA_85.a;
float whiteA = COL_WHITE.a;
float bgA = COL_BG.a;
COL_WHITE.a *= progress;
COL_BG.a *= progress;
Colors.BLACK_ALPHA_85.a *= progress;
2016-12-11 11:07:28 +01:00
g.setColor(Colors.BLACK_ALPHA_85);
2017-01-29 11:28:24 +01:00
g.fillRoundRect(listStartX, listStartY, listWidth, listHeight, borderRadius, 15);
Object[] listItems = selectedListOption.getListItems();
2016-12-11 11:07:28 +01:00
if (listHoverIndex != -1) {
2017-01-29 11:28:24 +01:00
g.setColor(COL_COMBOBOX_HOVER);
if (listHoverIndex == 0) {
g.fillRoundRect(listStartX, listStartY + listHoverIndex * listItemHeight, listWidth, listItemHeight, borderRadius, 15);
g.fillRect(listStartX, listStartY + listHoverIndex * listItemHeight + listItemHeight / 2, listWidth, listItemHeight / 2);
} else if (listHoverIndex == listItems.length - 1) {
g.fillRoundRect(listStartX, listStartY + listHoverIndex * listItemHeight, listWidth, listItemHeight, borderRadius, 15);
g.fillRect(listStartX, listStartY + listHoverIndex * listItemHeight, listWidth, listItemHeight / 2);
} else {
g.fillRect(listStartX, listStartY + listHoverIndex * listItemHeight, listWidth, listItemHeight);
}
chevronRightImg.draw(listStartX + 2, listStartY + listHoverIndex * listItemHeight + (listItemHeight - controlImageSize) / 2, COL_BG);
2016-12-11 11:07:28 +01:00
}
int y = listStartY;
2017-01-29 11:28:24 +01:00
String selectedValue = selectedListOption.getValueString();
2016-12-11 11:07:28 +01:00
for (Object item : listItems) {
2017-01-29 11:28:24 +01:00
String text = item.toString();
Font font;
if (text.equals(selectedValue)) {
font = Fonts.MEDIUMBOLD;
} else {
font = Fonts.MEDIUM;
}
font.drawString(listStartX + 20, y - Fonts.MEDIUM.getLineHeight() * 0.05f, item.toString(), COL_WHITE);
2016-12-11 16:41:56 +01:00
y += listItemHeight;
2016-12-11 11:07:28 +01:00
}
2017-01-29 11:28:24 +01:00
COL_BG.a = bgA;
COL_WHITE.a = whiteA;
Colors.BLACK_ALPHA_85.a = blackAlphaA;
2016-12-11 01:17:30 +01:00
}
2017-01-29 11:28:24 +01:00
private void renderOption(Graphics g, GameOption option, int y) {
2016-12-11 02:08:35 +01:00
OptionType type = option.getType();
Object[] listItems = option.getListItems();
if (listItems != null) {
2017-01-29 11:28:24 +01:00
renderListOption(g, option, y, listItems);
2016-12-11 02:08:35 +01:00
} else if (type == OptionType.BOOLEAN) {
2017-01-29 11:28:24 +01:00
renderCheckOption(option, y);
2016-12-11 02:08:35 +01:00
} else if (type == OptionType.NUMERIC) {
2017-01-29 11:28:24 +01:00
renderSliderOption(g, option, y);
2016-12-11 02:08:35 +01:00
} else {
2017-01-29 11:28:24 +01:00
renderGenericOption(option, y);
2016-12-11 02:08:35 +01:00
}
}
2017-01-29 11:28:24 +01:00
private void renderListOption(Graphics g, GameOption option, int y, Object[] listItems) {
2016-12-11 10:40:21 +01:00
int nameLen = Fonts.MEDIUM.getWidth(option.getName());
2017-01-29 11:28:24 +01:00
Fonts.MEDIUM.drawString(optionStartX, y + optionTextOffsetY, option.getName(), COL_WHITE);
final int padding = (int) (optionHeight / 10f);
nameLen += 15;
final int comboboxStartX = optionStartX + nameLen;
final int comboboxWidth = optionWidth - nameLen;
final int borderRadius = 6;
if (comboboxWidth <= controlImageSize) {
return;
}
Color backColor = COL_BG;
if (hoverOption == option
&& comboboxStartX <= displayContainer.mouseX && displayContainer.mouseX < comboboxStartX + comboboxWidth
&& y + padding <= displayContainer.mouseY && displayContainer.mouseY < y + padding + listItemHeight) {
backColor = COL_COMBOBOX_HOVER;
2016-12-11 11:07:28 +01:00
}
g.setColor(backColor);
2017-01-29 11:28:24 +01:00
g.fillRoundRect(comboboxStartX, y + padding, comboboxWidth, listItemHeight, borderRadius, 15);
Fonts.MEDIUM.drawString(comboboxStartX + 4, y + optionTextOffsetY, option.getValueString(), COL_WHITE);
chevronDownImg.draw(width - paddingRight - controlImageSize / 3 - controlImageSize, y + controlImagePadding, COL_WHITE);
2016-12-11 11:07:28 +01:00
if (isListOptionOpen && hoverOption == option) {
2017-01-29 11:28:24 +01:00
listStartX = comboboxStartX;
listStartY = y + optionHeight;
listWidth = comboboxWidth;
2016-12-11 16:41:56 +01:00
listHeight = listItems.length * listItemHeight;
2016-12-11 11:07:28 +01:00
}
2016-12-11 02:08:35 +01:00
}
2017-01-29 11:28:24 +01:00
private void renderCheckOption(GameOption option, int y) {
2016-12-11 02:08:35 +01:00
if (option.getBooleanValue()) {
2017-01-29 11:28:24 +01:00
checkOnImg.draw(optionStartX, y + controlImagePadding, COL_PINK);
2016-12-11 02:08:35 +01:00
} else {
2017-01-29 11:28:24 +01:00
checkOffImg.draw(optionStartX, y + controlImagePadding, COL_PINK);
2016-12-11 02:08:35 +01:00
}
2017-01-29 11:28:24 +01:00
Fonts.MEDIUM.drawString(optionStartX + 30, y + optionTextOffsetY, option.getName(), COL_WHITE);
2016-12-11 02:08:35 +01:00
}
2017-01-29 11:28:24 +01:00
private void renderSliderOption(Graphics g, GameOption option, int y) {
final int padding = 10;
2016-12-11 02:08:35 +01:00
int nameLen = Fonts.MEDIUM.getWidth(option.getName());
2017-01-29 11:28:24 +01:00
Fonts.MEDIUM.drawString(optionStartX, y + optionTextOffsetY, option.getName(), COL_WHITE);
int sliderLen = optionWidth - nameLen - padding;
if (sliderLen <= 1) {
return;
}
int sliderStartX = optionStartX + nameLen + padding;
int sliderEndX = optionStartX + optionWidth;
2016-12-11 10:08:22 +01:00
if (hoverOption == option) {
if (!isAdjustingSlider) {
sliderOptionLength = sliderLen;
2017-01-29 11:28:24 +01:00
sliderOptionStartX = sliderStartX;
2016-12-11 10:08:22 +01:00
} else {
sliderLen = sliderOptionLength;
}
}
2017-01-29 11:28:24 +01:00
float sliderValue = (float) (option.getIntegerValue() - option.getMinValue()) / (option.getMaxValue() - option.getMinValue());
float sliderBallPos = sliderStartX + (int) ((sliderLen - controlImageSize) * sliderValue);
2016-12-11 02:08:35 +01:00
g.setLineWidth(3f);
2017-01-29 11:28:24 +01:00
g.setColor(COL_PINK);
if (sliderValue > 0.0001f) {
g.drawLine(sliderStartX, y + optionHeight / 2, sliderBallPos, y + optionHeight / 2);
}
sliderBallImg.draw(sliderBallPos, y + controlImagePadding, COL_PINK);
if (sliderValue < 0.999f) {
float a = COL_PINK.a;
COL_PINK.a *= 0.45f;
g.setColor(COL_PINK);
g.drawLine(sliderBallPos + controlImageSize, y + optionHeight / 2, sliderEndX, y + optionHeight / 2);
COL_PINK.a = a;
}
2016-12-11 02:08:35 +01:00
}
2017-01-29 11:28:24 +01:00
private void renderGenericOption(GameOption option, int y) {
2016-12-11 02:08:35 +01:00
String value = option.getValueString();
int valueLen = Fonts.MEDIUM.getWidth(value);
2017-01-29 11:28:24 +01:00
Fonts.MEDIUM.drawString(optionStartX, y + optionTextOffsetY, option.getName(), COL_WHITE);
Fonts.MEDIUM.drawString(optionStartX + optionWidth - valueLen, y + optionTextOffsetY, value, COL_BLUE);
2016-12-11 01:17:30 +01:00
}
2017-01-29 11:28:24 +01:00
private void renderTitle() {
FontUtil.drawCentered(Fonts.LARGE, width, 0, textOptionsY - scrollOffset, "Options", COL_WHITE);
FontUtil.drawCentered(Fonts.MEDIUM, width, 0, textChangeY - scrollOffset, "Change the way opsu! behaves", COL_PINK);
2016-12-11 01:17:30 +01:00
}
2017-01-29 11:28:24 +01:00
private void renderSearch(Graphics g) {
int ypos = posSearchY + textSearchYOffset - scrollOffset;
if (scrollOffset > posSearchY) {
ypos = textSearchYOffset;
g.setColor(COL_BG);
g.fillRect(0, 0, width, textSearchYOffset * 2 + Fonts.LARGE.getLineHeight());
}
String searchText = "Type to search!";
if (lastSearchText.length() > 0) {
searchText = lastSearchText;
}
FontUtil.drawCentered(Fonts.LARGE, width, 0, ypos, searchText, COL_WHITE);
int imgPosX = (width - Fonts.LARGE.getWidth(searchText)) / 2 - searchImg.getWidth() - 10;
searchImg.draw(imgPosX, ypos + Fonts.LARGE.getLineHeight() * 0.25f, COL_WHITE);
2016-12-11 01:17:30 +01:00
}
2017-01-19 16:03:53 +01:00
@Override
public void hide() {
acceptInput = false;
SoundController.playSound(SoundEffect.MENUBACK);
2017-01-29 11:28:24 +01:00
hideAnimationTime = animationtime;
hideAnimationStartProgress = (float) animationtime / SHOWANIMATIONTIME;
2017-01-19 16:03:53 +01:00
}
@Override
public void show() {
2017-01-29 11:28:24 +01:00
indicatorPos = -optionHeight;
indicatorOffsetToNextPos = 0;
indicatorMoveAnimationTime = 0;
indicatorHideAnimationTime = 0;
2017-01-19 16:03:53 +01:00
acceptInput = true;
active = true;
2017-01-29 11:28:24 +01:00
animationtime = 0;
resetSearch();
2017-01-19 16:03:53 +01:00
}
@Override
public void onPreRenderUpdate() {
int mouseX = displayContainer.mouseX;
int mouseY = displayContainer.mouseY;
int delta = displayContainer.renderDelta;
2017-01-29 11:28:24 +01:00
searchField.performKeyRepeat();
updateShowHideAnimation(delta);
if (animationtime <= 0) {
active = false;
return;
}
if (sliderSoundDelay > 0) {
sliderSoundDelay -= delta;
}
2016-12-11 16:32:21 +01:00
if (mouseX - prevMouseX == 0 && mouseY - prevMouseY == 0) {
2017-01-29 11:28:24 +01:00
updateIndicatorAlpha();
2016-12-11 16:32:21 +01:00
return;
}
prevMouseX = mouseX;
prevMouseY = mouseY;
2016-12-11 01:17:30 +01:00
updateHoverOption(mouseX, mouseY);
2017-01-29 11:28:24 +01:00
updateIndicatorAlpha();
2016-12-11 01:17:30 +01:00
UI.getBackButton().hoverUpdate(delta, mouseX, mouseY);
2016-12-11 10:08:22 +01:00
if (isAdjustingSlider) {
int sliderValue = hoverOption.getIntegerValue();
2017-01-19 16:03:53 +01:00
updateSliderOption();
if (hoverOption.getIntegerValue() - sliderValue != 0 && sliderSoundDelay <= 0) {
sliderSoundDelay = 90;
SoundController.playSound(SoundEffect.MENUHIT);
}
2016-12-11 11:07:28 +01:00
} else if (isListOptionOpen) {
if (listStartX <= mouseX && mouseX < listStartX + listWidth && listStartY <= mouseY && mouseY < listStartY + listHeight) {
2016-12-11 16:41:56 +01:00
listHoverIndex = (mouseY - listStartY) / listItemHeight;
2016-12-11 11:07:28 +01:00
} else {
listHoverIndex = -1;
}
2016-12-11 10:08:22 +01:00
}
2016-12-11 01:17:30 +01:00
}
2017-01-29 11:28:24 +01:00
private void updateIndicatorAlpha() {
if (hoverOption == null) {
if (indicatorHideAnimationTime < INDICATORHIDEANIMATIONTIME) {
indicatorHideAnimationTime += displayContainer.renderDelta;
if (indicatorHideAnimationTime > INDICATORHIDEANIMATIONTIME) {
indicatorHideAnimationTime = INDICATORHIDEANIMATIONTIME;
}
float progress = AnimationEquation.IN_CUBIC.calc((float) indicatorHideAnimationTime / INDICATORHIDEANIMATIONTIME);
COL_INDICATOR.a = (1f - progress) * INDICATOR_ALPHA;
}
} else if (indicatorHideAnimationTime > 0) {
indicatorHideAnimationTime -= displayContainer.renderDelta * 3;
if (indicatorHideAnimationTime < 0) {
indicatorHideAnimationTime = 0;
}
COL_INDICATOR.a = (1f - (float) indicatorHideAnimationTime / INDICATORHIDEANIMATIONTIME) * INDICATOR_ALPHA;
}
}
private void updateShowHideAnimation(int delta) {
if (acceptInput && animationtime >= SHOWANIMATIONTIME) {
// animation already finished
width = finalWidth;
return;
}
optionWidth = width - optionStartX - paddingRight;
// if acceptInput is false, it means that we're currently hiding ourselves
float progress;
if (acceptInput) {
animationtime += delta;
if (animationtime >= SHOWANIMATIONTIME) {
animationtime = SHOWANIMATIONTIME;
}
progress = AnimationEquation.OUT_EXPO.calc((float) animationtime / SHOWANIMATIONTIME);
} else {
animationtime -= delta;
if (animationtime < 0) {
animationtime = 0;
}
progress = hideAnimationStartProgress * AnimationEquation.IN_EXPO.calc((float) animationtime / hideAnimationTime);
}
width = (int) (progress * finalWidth);
COL_BG.a = BG_ALPHA * progress;
COL_WHITE.a = progress;
COL_PINK.a = progress;
COL_CYAN.a = progress;
COL_GREY.a = progress * LINEALPHA;
COL_BLUE.a = progress;
COL_COMBOBOX_HOVER.a = progress;
COL_INDICATOR.a = progress * (1f - (float) indicatorHideAnimationTime / INDICATORHIDEANIMATIONTIME) * INDICATOR_ALPHA;
}
2017-01-19 16:03:53 +01:00
@Override
public boolean onMousePressed(int button, int x, int y) {
if (keyEntryLeft || keyEntryRight) {
keyEntryLeft = keyEntryRight = false;
2017-01-19 16:03:53 +01:00
return true;
}
2016-12-11 11:07:28 +01:00
if (isListOptionOpen) {
2017-01-29 11:28:24 +01:00
if (listStartX <= x && x < listStartX + listWidth && listStartY <= y && y < listStartY + listHeight) {
if (0 <= listHoverIndex && listHoverIndex < hoverOption.getListItems().length) {
hoverOption.clickListItem(listHoverIndex);
if (listener != null) {
listener.onSaveOption(hoverOption);
}
2017-01-19 19:23:31 +01:00
}
SoundController.playSound(SoundEffect.MENUCLICK);
2016-12-11 11:07:28 +01:00
}
isListOptionOpen = false;
2017-01-29 11:28:24 +01:00
listAnimationTime = LISTCLOSEANIMATIONTIME;
2016-12-11 11:07:28 +01:00
listHoverIndex = -1;
updateHoverOption(x, y);
2017-01-29 11:28:24 +01:00
return false;
}
if (x > width) {
return false;
2016-12-11 11:07:28 +01:00
}
mousePressY = y;
2016-12-12 18:49:26 +01:00
selectedOption = hoverOption;
2016-12-11 01:17:30 +01:00
2016-12-12 18:46:32 +01:00
if (hoverOption != null) {
if (hoverOption.getListItems() != null) {
2016-12-11 11:07:28 +01:00
isListOptionOpen = true;
2017-01-29 11:28:24 +01:00
selectedListOption = hoverOption;
listAnimationTime = 0;
2016-12-12 18:46:32 +01:00
} else if (hoverOption.getType() == OptionType.NUMERIC) {
2016-12-11 11:07:28 +01:00
isAdjustingSlider = sliderOptionStartX <= x && x < sliderOptionStartX + sliderOptionLength;
2016-12-11 16:32:21 +01:00
if (isAdjustingSlider) {
2017-01-19 16:03:53 +01:00
updateSliderOption();
2016-12-11 16:32:21 +01:00
}
2016-12-11 11:07:28 +01:00
}
2016-12-11 10:08:22 +01:00
}
2017-01-19 16:03:53 +01:00
return true;
}
2017-01-19 16:03:53 +01:00
@Override
public boolean onMouseReleased(int button, int x, int y) {
selectedOption = null;
2017-01-19 19:23:31 +01:00
if (isAdjustingSlider && listener != null) {
listener.onSaveOption(hoverOption);
2016-12-11 16:32:21 +01:00
}
2016-12-11 10:08:22 +01:00
isAdjustingSlider = false;
sliderOptionLength = 0;
// check if clicked, not dragged
if (Math.abs(y - mousePressY) >= 5) {
2017-01-19 16:03:53 +01:00
return true;
}
2016-12-11 02:08:35 +01:00
2017-01-29 11:28:24 +01:00
if (x > finalWidth) {
return false;
}
if (hoverOption != null) {
if (hoverOption.getType() == OptionType.BOOLEAN) {
2017-01-19 16:03:53 +01:00
hoverOption.click();
2017-01-19 19:23:31 +01:00
if (listener != null) {
listener.onSaveOption(hoverOption);
}
SoundController.playSound(SoundEffect.MENUHIT);
2017-01-19 16:03:53 +01:00
return true;
} else if (hoverOption == GameOption.KEY_LEFT) {
keyEntryLeft = true;
} else if (hoverOption == GameOption.KEY_RIGHT) {
keyEntryLeft = true;
}
}
2016-12-11 02:14:43 +01:00
2017-01-29 11:28:24 +01:00
/*
2016-12-11 16:33:32 +01:00
// check if tab was clicked
2016-12-11 02:14:43 +01:00
int tScrollOffset = 0;
2016-12-11 16:33:32 +01:00
for (OptionTab tab : tabs) {
if (tab.button.contains(x, y)) {
scrollOffset = tScrollOffset;
SoundController.playSound(SoundEffect.MENUCLICK);
2017-01-19 16:03:53 +01:00
return true;
2016-12-11 02:14:43 +01:00
}
tScrollOffset += Fonts.MEDIUM.getLineHeight() * 2;
2016-12-11 16:33:32 +01:00
tScrollOffset += tab.options.length * optionHeight;
2016-12-11 02:14:43 +01:00
}
2017-01-29 11:28:24 +01:00
*/
2017-01-19 19:23:31 +01:00
if (UI.getBackButton().contains(x, y)){
hide();
if (listener != null) {
listener.onLeaveOptionsMenu();
}
2017-01-19 19:23:31 +01:00
}
2017-01-19 16:03:53 +01:00
return true;
2016-12-11 01:17:30 +01:00
}
2017-01-19 16:03:53 +01:00
@Override
public boolean onMouseDragged(int oldx, int oldy, int newx, int newy) {
2016-12-11 10:08:22 +01:00
if (!isAdjustingSlider) {
scrollOffset = Utils.clamp(scrollOffset + oldy - newy, 0, maxScrollOffset);
}
2017-01-19 16:03:53 +01:00
return true;
2016-12-11 01:17:30 +01:00
}
2017-01-19 16:03:53 +01:00
@Override
public boolean onMouseWheelMoved(int delta) {
2016-12-11 10:08:22 +01:00
if (!isAdjustingSlider) {
scrollOffset = Utils.clamp(scrollOffset - delta, 0, maxScrollOffset);
2016-12-11 10:08:22 +01:00
}
updateHoverOption(prevMouseX, prevMouseY);
2017-01-19 16:03:53 +01:00
return true;
2016-12-11 01:17:30 +01:00
}
2017-01-19 16:03:53 +01:00
@Override
public boolean onKeyPressed(int key, char c) {
if (keyEntryRight) {
Options.setGameKeyRight(key);
keyEntryRight = false;
return true;
}
if (keyEntryLeft) {
Options.setGameKeyLeft(key);
keyEntryLeft = false;
return true;
}
2017-01-19 19:23:31 +01:00
if (key == Input.KEY_ESCAPE) {
if (isListOptionOpen) {
isListOptionOpen = false;
2017-01-29 11:28:24 +01:00
listAnimationTime = LISTCLOSEANIMATIONTIME;
2017-01-19 19:23:31 +01:00
listHoverIndex = -1;
return true;
2017-01-19 19:23:31 +01:00
}
2017-01-29 11:28:24 +01:00
if (lastSearchText.length() != 0) {
resetSearch();
updateHoverOption(prevMouseX, prevMouseY);
return true;
}
2017-01-19 19:23:31 +01:00
hide();
if (listener != null) {
listener.onLeaveOptionsMenu();
}
return true;
2016-12-11 01:17:30 +01:00
}
2017-01-19 19:23:31 +01:00
2017-01-29 11:28:24 +01:00
searchField.keyPressed(key, c);
if (!searchField.getText().equals(lastSearchText)) {
lastSearchText = searchField.getText().toLowerCase();
updateSearch();
}
return true;
2016-12-11 01:17:30 +01:00
}
2017-01-19 16:03:53 +01:00
@Override
public boolean onKeyReleased(int key, char c) {
return false;
}
private void updateSliderOption() {
2016-12-12 18:46:32 +01:00
int min = hoverOption.getMinValue();
int max = hoverOption.getMaxValue();
2017-01-19 16:03:53 +01:00
int value = min + Math.round((float) (max - min) * (displayContainer.mouseX - sliderOptionStartX) / (sliderOptionLength));
2016-12-12 18:46:32 +01:00
hoverOption.setValue(Utils.clamp(value, min, max));
2016-12-11 16:32:21 +01:00
}
2016-12-11 01:17:30 +01:00
private void updateHoverOption(int mouseX, int mouseY) {
if (isListOptionOpen || keyEntryLeft || keyEntryRight) {
2016-12-11 11:07:28 +01:00
return;
}
2016-12-11 02:23:02 +01:00
if (selectedOption != null) {
hoverOption = selectedOption;
return;
}
2016-12-11 01:17:30 +01:00
hoverOption = null;
2017-01-29 11:28:24 +01:00
if (mouseX > width) {
2016-12-11 01:17:30 +01:00
return;
}
int mouseVirtualY = scrollOffset + mouseY - optionStartY;
2017-01-29 11:28:24 +01:00
for (int sectionIndex = 0; sectionIndex < sections.length; sectionIndex++) {
OptionTab section = sections[sectionIndex];
if (section.filtered) {
continue;
}
mouseVirtualY -= Fonts.LARGE.getLineHeight() * 1.5f;
if (section.options == null) {
continue;
}
for (int optionIndex = 0; optionIndex < section.options.length; optionIndex++) {
GameOption option = section.options[optionIndex];
if (option.isFiltered() || !option.showCondition()) {
2016-12-11 01:17:30 +01:00
continue;
}
if (mouseVirtualY <= optionHeight) {
if (mouseVirtualY >= 0) {
2017-01-29 11:28:24 +01:00
int indicatorPos = scrollOffset + mouseY - mouseVirtualY;
if (indicatorPos != this.indicatorPos + indicatorOffsetToNextPos) {
this.indicatorPos += indicatorOffsetToNextPos; // finish the current moving animation
indicatorOffsetToNextPos = indicatorPos - this.indicatorPos;
indicatorMoveAnimationTime = 1; // starts animation
}
2016-12-11 01:17:30 +01:00
hoverOption = option;
}
return;
}
mouseVirtualY -= optionHeight;
}
}
}
2017-01-29 11:28:24 +01:00
private void resetSearch() {
for (OptionTab section : sections) {
section.filtered = false;
if (section.options == null) {
continue;
}
for (GameOption opt : section.options) {
opt.filter(null);
}
}
searchField.setText("");
lastSearchText = "";
}
private void updateSearch() {
OptionTab lastBigSection = null;
boolean lastBigSectionMatches = false;
for (int sectionIndex = 0; sectionIndex < sections.length; sectionIndex++) {
OptionTab section = sections[sectionIndex];
boolean sectionMatches = section.name.toLowerCase().contains(lastSearchText);
if (section.options == null) {
lastBigSectionMatches = sectionMatches;
lastBigSection = section;
section.filtered = true;
continue;
}
boolean allOptionsHidden = true;
for (int optionIndex = 0; optionIndex < section.options.length; optionIndex++) {
GameOption option = section.options[optionIndex];
if (lastBigSectionMatches || sectionMatches) {
allOptionsHidden = false;
option.filter(null);
continue;
}
if (!option.filter(lastSearchText)) {
allOptionsHidden = false;
}
}
if (allOptionsHidden) {
section.filtered = true;
} else {
lastBigSection.filtered = false;
section.filtered = false;
}
}
updateHoverOption(prevMouseX, prevMouseY);
}
2016-12-11 01:17:30 +01:00
public static class OptionTab {
public final String name;
public final GameOption[] options;
2017-01-29 11:28:24 +01:00
private boolean filtered;
2016-12-11 01:17:30 +01:00
public OptionTab(String name, GameOption[] options) {
this.name = name;
this.options = options;
}
}
2017-01-19 19:23:31 +01:00
public interface Listener {
2016-12-11 01:17:30 +01:00
2017-01-19 19:23:31 +01:00
void onLeaveOptionsMenu();
2016-12-11 01:17:30 +01:00
void onSaveOption(GameOption option);
}
}