make sliders slide

This commit is contained in:
yugecin 2016-12-11 10:08:22 +01:00
parent cd06b13f66
commit 1bd5da6434
2 changed files with 34 additions and 3 deletions

View File

@ -1455,7 +1455,7 @@ public class Options {
* @param container the game container * @param container the game container
* @param d the dragged distance (modified by multiplier) * @param d the dragged distance (modified by multiplier)
*/ */
public void drag(GameContainer container, int d) { public void drag(GameContainer container, int d) { // TODO rename this
if (type == OptionType.NUMERIC) if (type == OptionType.NUMERIC)
val = Utils.clamp(val + d, min, max); val = Utils.clamp(val + d, min, max);
} }

View File

@ -44,6 +44,9 @@ public class OptionsOverlay {
private int selectedTab; private int selectedTab;
private GameOption hoverOption; private GameOption hoverOption;
private GameOption selectedOption; private GameOption selectedOption;
private int sliderOptionStartX;
private int sliderOptionLength;
private boolean isAdjustingSlider;
private boolean isListOptionOpen; private boolean isListOptionOpen;
private int width; private int width;
@ -210,6 +213,16 @@ public class OptionsOverlay {
Fonts.MEDIUM.drawString(optionStartX, y, option.getName(), textColor); Fonts.MEDIUM.drawString(optionStartX, y, option.getName(), textColor);
Fonts.MEDIUM.drawString(optionStartX + optionWidth - valueLen, y, value, textColor); Fonts.MEDIUM.drawString(optionStartX + optionWidth - valueLen, y, value, textColor);
int sliderLen = optionWidth - nameLen - valueLen - 50; int sliderLen = optionWidth - nameLen - valueLen - 50;
if (hoverOption == option) {
if (!isAdjustingSlider) {
sliderOptionLength = sliderLen;
sliderOptionStartX = optionStartX + nameLen + 25;
} else {
sliderLen = sliderOptionLength;
}
}
g.setColor(Color.pink); g.setColor(Color.pink);
g.setLineWidth(3f); g.setLineWidth(3f);
g.drawLine(optionStartX + nameLen + 25, y + optionHeight / 2, optionStartX + nameLen + 25 + sliderLen, y + optionHeight / 2); g.drawLine(optionStartX + nameLen + 25, y + optionHeight / 2, optionStartX + nameLen + 25 + sliderLen, y + optionHeight / 2);
@ -244,12 +257,24 @@ public class OptionsOverlay {
public void update(int delta, int mouseX, int mouseY) { public void update(int delta, int mouseX, int mouseY) {
updateHoverOption(mouseX, mouseY); updateHoverOption(mouseX, mouseY);
UI.getBackButton().hoverUpdate(delta, mouseX, mouseY); UI.getBackButton().hoverUpdate(delta, mouseX, mouseY);
if (isAdjustingSlider) {
System.out.println(sliderOptionLength);
int min = selectedOption.getMinValue();
int max = selectedOption.getMaxValue();
int value = min + (int) ((float) (max - min) * (mouseX - sliderOptionStartX) / (sliderOptionLength));
selectedOption.setValue(Utils.clamp(value, min, max));
selectedOption.drag(container, 0);
}
} }
public void mousePressed(int button, int x, int y) { public void mousePressed(int button, int x, int y) {
mousePressY = y; mousePressY = y;
selectedOption = hoverOption; selectedOption = hoverOption;
if (selectedOption != null && selectedOption.getType() == OptionType.NUMERIC) {
isAdjustingSlider = sliderOptionStartX <= x && x < sliderOptionStartX + sliderOptionLength;
}
if (UI.getBackButton().contains(x, y)) { if (UI.getBackButton().contains(x, y)) {
parent.onLeave(); parent.onLeave();
return; return;
@ -258,6 +283,8 @@ public class OptionsOverlay {
public void mouseReleased(int button, int x, int y) { public void mouseReleased(int button, int x, int y) {
selectedOption = null; selectedOption = null;
isAdjustingSlider = false;
sliderOptionLength = 0;
// check if clicked, not dragged // check if clicked, not dragged
if (Math.abs(y - mousePressY) >= 5) { if (Math.abs(y - mousePressY) >= 5) {
@ -286,11 +313,15 @@ public class OptionsOverlay {
} }
public void mouseDragged(int oldx, int oldy, int newx, int newy) { public void mouseDragged(int oldx, int oldy, int newx, int newy) {
scrollOffset = Utils.clamp(scrollOffset + oldy - newy, 0, maxScrollOffset); if (!isAdjustingSlider) {
scrollOffset = Utils.clamp(scrollOffset + oldy - newy, 0, maxScrollOffset);
}
} }
public void mouseWheelMoved(int delta) { public void mouseWheelMoved(int delta) {
scrollOffset = Utils.clamp(scrollOffset - delta, 0, maxScrollOffset - optionStartY); if (!isAdjustingSlider) {
scrollOffset = Utils.clamp(scrollOffset - delta, 0, maxScrollOffset - optionStartY);
}
} }
public void keyPressed(int key, char c) { public void keyPressed(int key, char c) {