2015-09-16 05:20:53 +02:00
|
|
|
/*
|
|
|
|
* opsu! - an open-source osu! client
|
|
|
|
* Copyright (C) 2014, 2015 Jeffrey Han
|
|
|
|
*
|
|
|
|
* opsu! 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! 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!. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2015-07-02 01:45:13 +02:00
|
|
|
package itdelatrisu.opsu.ui;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Drag to scroll based on:
|
|
|
|
* http://ariya.ofilabs.com/2013/11/javascript-kinetic-scrolling-part-2.html
|
2015-09-16 05:20:53 +02:00
|
|
|
*
|
2015-07-02 01:45:13 +02:00
|
|
|
* @author fluddokt (https://github.com/fluddokt)
|
|
|
|
*/
|
2015-09-16 03:39:35 +02:00
|
|
|
public class KineticScrolling {
|
2015-09-16 05:20:53 +02:00
|
|
|
/** The moving averaging constant. */
|
|
|
|
private static final float AVG_CONST = 0.2f, ONE_MINUS_AVG_CONST = 1 - AVG_CONST;
|
2015-07-02 01:45:13 +02:00
|
|
|
|
2015-09-19 00:49:01 +02:00
|
|
|
/** The constant used to determine how fast the target position will be reached. */
|
2015-09-16 05:20:53 +02:00
|
|
|
private static final int TIME_CONST = 200;
|
|
|
|
|
2015-07-02 01:45:13 +02:00
|
|
|
/** The constant used to determine how much of the velocity will be used to launch to the target. */
|
2015-09-16 05:20:53 +02:00
|
|
|
private static final float AMPLITUDE_CONST = 0.25f;
|
|
|
|
|
2015-07-02 01:45:13 +02:00
|
|
|
/** The current position. */
|
2015-09-16 05:20:53 +02:00
|
|
|
private float position;
|
2015-07-02 01:45:13 +02:00
|
|
|
|
|
|
|
/** The offset to scroll to the target position. */
|
2015-09-16 05:20:53 +02:00
|
|
|
private float amplitude;
|
|
|
|
|
2015-07-02 01:45:13 +02:00
|
|
|
/** The current target to scroll to. */
|
2015-09-16 05:20:53 +02:00
|
|
|
private float target;
|
|
|
|
|
2015-07-02 01:45:13 +02:00
|
|
|
/** The total amount of time since the mouse button was released. */
|
2015-09-16 05:20:53 +02:00
|
|
|
private float totalDelta;
|
2015-07-02 01:45:13 +02:00
|
|
|
|
|
|
|
/** The maximum and minimum value the position can reach. */
|
2015-09-16 05:20:53 +02:00
|
|
|
private float max = Float.MAX_VALUE, min = -Float.MAX_VALUE;
|
|
|
|
|
|
|
|
/** Whether the mouse is currently pressed or not. */
|
|
|
|
private boolean pressed = false;
|
2015-07-02 01:45:13 +02:00
|
|
|
|
2015-09-16 05:20:53 +02:00
|
|
|
/** The change in position since the last update. */
|
|
|
|
private float deltaPosition;
|
2015-07-02 01:45:13 +02:00
|
|
|
|
|
|
|
/** The moving average of the velocity. */
|
2015-09-16 05:20:53 +02:00
|
|
|
private float avgVelocity;
|
|
|
|
|
2015-09-19 00:49:01 +02:00
|
|
|
/** The speed multiplier (divides {@link #TIME_CONST}). */
|
|
|
|
private float speedMultiplier = 1f;
|
|
|
|
|
2017-01-29 16:26:45 +01:00
|
|
|
/** Whether or not to allow overscrolling. */
|
2017-01-29 16:23:52 +01:00
|
|
|
private boolean allowOverScroll;
|
|
|
|
|
2017-01-29 16:26:45 +01:00
|
|
|
/**
|
|
|
|
* Enable or disable the overscrolling flag.
|
|
|
|
* @param allowOverScroll whether or not to allow overscrolling
|
|
|
|
*/
|
2017-01-29 16:23:52 +01:00
|
|
|
public void setAllowOverScroll(boolean allowOverScroll) {
|
|
|
|
this.allowOverScroll = allowOverScroll;
|
|
|
|
}
|
|
|
|
|
2015-07-02 01:45:13 +02:00
|
|
|
/**
|
2015-09-19 00:49:01 +02:00
|
|
|
* Returns the current position.
|
2015-09-16 05:20:53 +02:00
|
|
|
* @return the position
|
2015-07-02 01:45:13 +02:00
|
|
|
*/
|
2015-09-16 05:20:53 +02:00
|
|
|
public float getPosition() { return position; }
|
|
|
|
|
2017-01-29 15:33:01 +01:00
|
|
|
/**
|
|
|
|
* Returns the current position as an int.
|
|
|
|
* @return the position
|
|
|
|
*/
|
|
|
|
public int getIntPosition() { return (int) position; }
|
|
|
|
|
2015-09-19 00:49:01 +02:00
|
|
|
/**
|
|
|
|
* Returns the target position.
|
|
|
|
* @return the target position
|
|
|
|
*/
|
|
|
|
public float getTargetPosition() { return target; }
|
|
|
|
|
2015-07-02 01:45:13 +02:00
|
|
|
/**
|
|
|
|
* Updates the scrolling.
|
|
|
|
* @param delta the elapsed time since the last update
|
|
|
|
*/
|
|
|
|
public void update(float delta) {
|
|
|
|
if (!pressed) {
|
|
|
|
totalDelta += delta;
|
2015-09-19 00:49:01 +02:00
|
|
|
position = target + (float) (-amplitude * Math.exp(-totalDelta / (TIME_CONST / speedMultiplier)));
|
2015-07-02 01:45:13 +02:00
|
|
|
} else {
|
|
|
|
avgVelocity = (ONE_MINUS_AVG_CONST * avgVelocity + AVG_CONST * (deltaPosition * 1000f / delta));
|
2015-09-16 05:20:53 +02:00
|
|
|
|
2015-07-02 01:45:13 +02:00
|
|
|
position += deltaPosition;
|
2015-09-16 05:20:53 +02:00
|
|
|
target = position;
|
2015-07-02 01:45:13 +02:00
|
|
|
deltaPosition = 0;
|
|
|
|
}
|
2017-01-29 16:23:52 +01:00
|
|
|
if (allowOverScroll && pressed) {
|
|
|
|
return;
|
2015-07-02 01:45:13 +02:00
|
|
|
}
|
2017-01-29 16:23:52 +01:00
|
|
|
if (position > max) {
|
|
|
|
if (allowOverScroll) {
|
|
|
|
scrollToPosition(max);
|
|
|
|
} else {
|
|
|
|
amplitude = 0;
|
|
|
|
target = position = max;
|
|
|
|
}
|
|
|
|
} else if (position < min) {
|
|
|
|
if (allowOverScroll) {
|
|
|
|
scrollToPosition(min);
|
|
|
|
} else {
|
|
|
|
amplitude = 0;
|
|
|
|
target = position = min;
|
|
|
|
}
|
2015-07-02 01:45:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scrolls to the position.
|
2015-09-16 05:20:53 +02:00
|
|
|
* @param newPosition the position to scroll to
|
2015-07-02 01:45:13 +02:00
|
|
|
*/
|
|
|
|
public void scrollToPosition(float newPosition) {
|
2015-09-19 18:11:07 +02:00
|
|
|
pressed();
|
|
|
|
released();
|
2015-07-02 01:45:13 +02:00
|
|
|
amplitude = newPosition - position;
|
|
|
|
target = newPosition;
|
|
|
|
totalDelta = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scrolls to an offset from target.
|
2015-09-16 05:20:53 +02:00
|
|
|
* @param offset the offset from the target to scroll to
|
2015-07-02 01:45:13 +02:00
|
|
|
*/
|
|
|
|
public void scrollOffset(float offset) {
|
|
|
|
scrollToPosition(target + offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the position.
|
|
|
|
* @param newPosition the position to be set
|
|
|
|
*/
|
|
|
|
public void setPosition(float newPosition) {
|
|
|
|
pressed();
|
2015-09-16 21:42:00 +02:00
|
|
|
released();
|
2015-07-02 01:45:13 +02:00
|
|
|
target = newPosition;
|
|
|
|
position = target;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the position relative to an offset.
|
2015-09-16 05:20:53 +02:00
|
|
|
* @param offset the offset from the position
|
2015-07-02 01:45:13 +02:00
|
|
|
*/
|
|
|
|
public void addOffset(float offset) {
|
|
|
|
setPosition(position + offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Call this when the mouse button has been pressed.
|
|
|
|
*/
|
|
|
|
public void pressed() {
|
|
|
|
if (pressed)
|
|
|
|
return;
|
|
|
|
pressed = true;
|
|
|
|
avgVelocity = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Call this when the mouse button has been released.
|
|
|
|
*/
|
2015-09-16 21:42:00 +02:00
|
|
|
public void released() {
|
2015-07-02 01:45:13 +02:00
|
|
|
if (!pressed)
|
|
|
|
return;
|
|
|
|
pressed = false;
|
|
|
|
amplitude = AMPLITUDE_CONST * avgVelocity;
|
|
|
|
target = Math.round(target + amplitude);
|
|
|
|
totalDelta = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Call this when the mouse has been dragged.
|
|
|
|
* @param distance the amount that the mouse has been dragged
|
|
|
|
*/
|
|
|
|
public void dragged(float distance) {
|
|
|
|
deltaPosition += distance;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the minimum and maximum bound.
|
|
|
|
* @param min the minimum bound
|
|
|
|
* @param max the maximum bound
|
|
|
|
*/
|
|
|
|
public void setMinMax(float min, float max) {
|
|
|
|
this.min = min;
|
|
|
|
this.max = max;
|
|
|
|
}
|
2015-09-19 00:49:01 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the multiplier for how fast the target position will be reached.
|
|
|
|
* @param multiplier the speed multiplier (e.g. 1f = normal speed, 2f = reaches in half the time)
|
|
|
|
* @throws IllegalArgumentException if the multiplier is negative or zero
|
|
|
|
*/
|
|
|
|
public void setSpeedMultiplier(float multiplier) {
|
|
|
|
if (multiplier <= 0f)
|
|
|
|
throw new IllegalArgumentException("Speed multiplier must be above zero.");
|
|
|
|
this.speedMultiplier = multiplier;
|
|
|
|
}
|
2015-07-02 01:45:13 +02:00
|
|
|
}
|