2014-06-30 04:17:04 +02:00
|
|
|
/*
|
|
|
|
* opsu! - an open-source osu! client
|
2015-01-16 18:05:44 +01:00
|
|
|
* Copyright (C) 2014, 2015 Jeffrey Han
|
2014-06-30 04:17:04 +02:00
|
|
|
*
|
|
|
|
* 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-05-29 07:55:57 +02:00
|
|
|
package itdelatrisu.opsu.ui;
|
|
|
|
|
2015-08-13 02:03:50 +02:00
|
|
|
import itdelatrisu.opsu.Utils;
|
2015-08-06 05:28:14 +02:00
|
|
|
import itdelatrisu.opsu.ui.animations.AnimatedValue;
|
|
|
|
import itdelatrisu.opsu.ui.animations.AnimationEquation;
|
2014-06-30 04:17:04 +02:00
|
|
|
|
|
|
|
import org.newdawn.slick.Animation;
|
|
|
|
import org.newdawn.slick.Color;
|
2015-02-02 06:15:16 +01:00
|
|
|
import org.newdawn.slick.Font;
|
2014-06-30 04:17:04 +02:00
|
|
|
import org.newdawn.slick.Image;
|
|
|
|
|
|
|
|
/**
|
2015-02-16 23:53:24 +01:00
|
|
|
* A convenience class for menu buttons, consisting of an image or animation
|
|
|
|
* and coordinates. Multi-part images currently do not support effects.
|
2014-06-30 04:17:04 +02:00
|
|
|
*/
|
2014-12-30 06:00:58 +01:00
|
|
|
public class MenuButton {
|
2015-01-22 06:44:45 +01:00
|
|
|
/** The image associated with the button. */
|
2014-06-30 04:17:04 +02:00
|
|
|
private Image img;
|
|
|
|
|
2015-01-22 06:44:45 +01:00
|
|
|
/** The left and right parts of the button (optional). */
|
2014-06-30 04:17:04 +02:00
|
|
|
private Image imgL, imgR;
|
|
|
|
|
2015-01-22 06:44:45 +01:00
|
|
|
/** The animation associated with the button. */
|
2014-06-30 04:17:04 +02:00
|
|
|
private Animation anim;
|
|
|
|
|
2015-01-22 06:44:45 +01:00
|
|
|
/** The center coordinates. */
|
2014-06-30 04:17:04 +02:00
|
|
|
private float x, y;
|
|
|
|
|
2015-01-22 06:44:45 +01:00
|
|
|
/** The x and y radius of the button (scaled). */
|
2014-06-30 04:17:04 +02:00
|
|
|
private float xRadius, yRadius;
|
|
|
|
|
2015-02-02 06:15:16 +01:00
|
|
|
/** The text to draw on the button. */
|
|
|
|
private String text;
|
|
|
|
|
|
|
|
/** The font to draw the text with. */
|
|
|
|
private Font font;
|
|
|
|
|
|
|
|
/** The color to draw the text with. */
|
|
|
|
private Color color;
|
|
|
|
|
2015-02-16 23:53:24 +01:00
|
|
|
/** Effect types. */
|
|
|
|
private static final int
|
|
|
|
EFFECT_EXPAND = 1,
|
|
|
|
EFFECT_FADE = 2,
|
|
|
|
EFFECT_ROTATE = 4;
|
2015-02-02 06:15:16 +01:00
|
|
|
|
2015-02-16 23:53:24 +01:00
|
|
|
/** The hover actions for this button. */
|
|
|
|
private int hoverEffect = 0;
|
2015-02-02 06:15:16 +01:00
|
|
|
|
2015-08-06 05:28:14 +02:00
|
|
|
/** The hover animation duration, in milliseconds. */
|
|
|
|
private int animationDuration = 100;
|
2015-02-02 06:15:16 +01:00
|
|
|
|
2015-08-06 05:28:14 +02:00
|
|
|
/** The hover animation equation. */
|
|
|
|
private AnimationEquation animationEqn = AnimationEquation.LINEAR;
|
|
|
|
|
2015-08-13 02:03:50 +02:00
|
|
|
/** Whether the animation is advancing forwards (if advancing automatically). */
|
|
|
|
private boolean autoAnimationForward = true;
|
|
|
|
|
2015-08-06 05:28:14 +02:00
|
|
|
/** The scale of the button. */
|
|
|
|
private AnimatedValue scale;
|
|
|
|
|
|
|
|
/** The default max scale of the button. */
|
|
|
|
private static final float DEFAULT_SCALE_MAX = 1.25f;
|
|
|
|
|
|
|
|
/** The alpha level of the button. */
|
|
|
|
private AnimatedValue alpha;
|
|
|
|
|
|
|
|
/** The default base alpha level of the button. */
|
|
|
|
private static final float DEFAULT_ALPHA_BASE = 0.75f;
|
2014-12-24 05:41:37 +01:00
|
|
|
|
2015-02-16 23:53:24 +01:00
|
|
|
/** The scaled expansion direction for the button. */
|
2014-12-24 05:41:37 +01:00
|
|
|
private Expand dir = Expand.CENTER;
|
|
|
|
|
2015-02-16 23:53:24 +01:00
|
|
|
/** Scaled expansion directions. */
|
2015-02-01 08:10:17 +01:00
|
|
|
public enum Expand { CENTER, UP, RIGHT, LEFT, DOWN, UP_RIGHT, UP_LEFT, DOWN_RIGHT, DOWN_LEFT; }
|
2014-12-24 05:41:37 +01:00
|
|
|
|
2015-08-06 05:28:14 +02:00
|
|
|
/** The rotation angle of the button. */
|
|
|
|
private AnimatedValue angle;
|
|
|
|
|
|
|
|
/** The default max rotation angle of the button. */
|
|
|
|
private static final float DEFAULT_ANGLE_MAX = 30f;
|
2015-02-16 23:53:24 +01:00
|
|
|
|
2014-06-30 04:17:04 +02:00
|
|
|
/**
|
|
|
|
* Creates a new button from an Image.
|
2015-01-22 06:44:45 +01:00
|
|
|
* @param img the image
|
|
|
|
* @param x the center x coordinate
|
|
|
|
* @param y the center y coordinate
|
2014-06-30 04:17:04 +02:00
|
|
|
*/
|
2014-12-30 06:00:58 +01:00
|
|
|
public MenuButton(Image img, float x, float y) {
|
2014-06-30 04:17:04 +02:00
|
|
|
this.img = img;
|
2014-12-24 05:41:37 +01:00
|
|
|
this.x = x;
|
|
|
|
this.y = y;
|
|
|
|
this.xRadius = img.getWidth() / 2f;
|
|
|
|
this.yRadius = img.getHeight() / 2f;
|
2014-06-30 04:17:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new button from a 3-part Image.
|
2015-01-22 06:44:45 +01:00
|
|
|
* @param imgCenter the center image
|
|
|
|
* @param imgLeft the left image
|
|
|
|
* @param imgRight the right image
|
|
|
|
* @param x the center x coordinate
|
|
|
|
* @param y the center y coordinate
|
2014-06-30 04:17:04 +02:00
|
|
|
*/
|
2015-01-22 06:44:45 +01:00
|
|
|
public MenuButton(Image imgCenter, Image imgLeft, Image imgRight, float x, float y) {
|
2014-06-30 04:17:04 +02:00
|
|
|
this.img = imgCenter;
|
|
|
|
this.imgL = imgLeft;
|
|
|
|
this.imgR = imgRight;
|
2014-12-24 05:41:37 +01:00
|
|
|
this.x = x;
|
|
|
|
this.y = y;
|
|
|
|
this.xRadius = (img.getWidth() + imgL.getWidth() + imgR.getWidth()) / 2f;
|
|
|
|
this.yRadius = img.getHeight() / 2f;
|
2014-06-30 04:17:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new button from an Animation.
|
2015-01-22 06:44:45 +01:00
|
|
|
* @param anim the animation
|
|
|
|
* @param x the center x coordinate
|
|
|
|
* @param y the center y coordinate
|
2014-06-30 04:17:04 +02:00
|
|
|
*/
|
2014-12-30 06:00:58 +01:00
|
|
|
public MenuButton(Animation anim, float x, float y) {
|
2014-06-30 04:17:04 +02:00
|
|
|
this.anim = anim;
|
2014-12-24 05:41:37 +01:00
|
|
|
this.x = x;
|
|
|
|
this.y = y;
|
|
|
|
this.xRadius = anim.getWidth() / 2f;
|
|
|
|
this.yRadius = anim.getHeight() / 2f;
|
2014-06-30 04:17:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-01-22 06:44:45 +01:00
|
|
|
* Sets a new center x coordinate.
|
2015-09-10 05:51:16 +02:00
|
|
|
* @param x the x coordinate
|
2014-06-30 04:17:04 +02:00
|
|
|
*/
|
|
|
|
public void setX(float x) { this.x = x; }
|
2015-01-22 06:44:45 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a new center y coordinate.
|
2015-09-10 05:51:16 +02:00
|
|
|
* @param y the y coordinate
|
2015-01-22 06:44:45 +01:00
|
|
|
*/
|
2014-06-30 04:17:04 +02:00
|
|
|
public void setY(float y) { this.y = y; }
|
2015-01-22 06:44:45 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the center x coordinate.
|
|
|
|
*/
|
2014-06-30 04:17:04 +02:00
|
|
|
public float getX() { return x; }
|
2015-01-22 06:44:45 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the center y coordinate.
|
|
|
|
*/
|
2014-06-30 04:17:04 +02:00
|
|
|
public float getY() { return y; }
|
|
|
|
|
2015-02-02 06:15:16 +01:00
|
|
|
/**
|
|
|
|
* Sets text to draw in the middle of the button.
|
|
|
|
* @param text the text to draw
|
|
|
|
* @param font the font to use when drawing
|
2015-05-25 17:00:56 +02:00
|
|
|
* @param color the color to draw the text
|
2015-02-02 06:15:16 +01:00
|
|
|
*/
|
|
|
|
public void setText(String text, Font font, Color color) {
|
|
|
|
this.text = text;
|
|
|
|
this.font = font;
|
|
|
|
this.color = color;
|
|
|
|
}
|
|
|
|
|
2014-06-30 04:17:04 +02:00
|
|
|
/**
|
2015-01-22 06:44:45 +01:00
|
|
|
* Returns the associated image.
|
2014-06-30 04:17:04 +02:00
|
|
|
*/
|
|
|
|
public Image getImage() { return img; }
|
2015-01-22 06:44:45 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the associated animation.
|
|
|
|
*/
|
2014-06-30 04:17:04 +02:00
|
|
|
public Animation getAnimation() { return anim; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Draws the button.
|
|
|
|
*/
|
2016-11-05 21:49:20 +01:00
|
|
|
public void draw() { draw(Color.white, 1.0f); }
|
2014-06-30 04:17:04 +02:00
|
|
|
|
|
|
|
/**
|
2016-11-05 21:49:20 +01:00
|
|
|
* Draws the button with a color filter.
|
2014-06-30 04:17:04 +02:00
|
|
|
* @param filter the color to filter with when drawing
|
|
|
|
*/
|
2016-11-05 21:49:20 +01:00
|
|
|
public void draw(Color filter) { draw(filter, 1.0f); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Draw the button with a color filter and scale.
|
|
|
|
* @param filter the color to filter with when drawing
|
|
|
|
* @param scaleoverride the scale to use when drawing
|
|
|
|
*/
|
2015-02-19 06:33:32 +01:00
|
|
|
@SuppressWarnings("deprecation")
|
2016-11-05 21:49:20 +01:00
|
|
|
public void draw(Color filter, float scaleoverride) {
|
2015-02-19 06:33:32 +01:00
|
|
|
// animations: get current frame
|
|
|
|
Image image = this.img;
|
|
|
|
if (image == null) {
|
|
|
|
anim.updateNoDraw();
|
|
|
|
image = anim.getCurrentFrame();
|
|
|
|
}
|
|
|
|
|
|
|
|
// normal images
|
|
|
|
if (imgL == null) {
|
2016-11-05 21:49:20 +01:00
|
|
|
float scaleposmodx = 0;
|
|
|
|
float scaleposmody = 0;
|
|
|
|
if (scaleoverride != 1f) {
|
|
|
|
image = image.getScaledCopy(scaleoverride);
|
|
|
|
scaleposmodx = image.getWidth() / 2 - xRadius;
|
|
|
|
scaleposmody = image.getHeight() / 2 - yRadius;
|
|
|
|
}
|
2015-02-19 06:33:32 +01:00
|
|
|
if (hoverEffect == 0)
|
2016-11-05 21:49:20 +01:00
|
|
|
image.draw(x - xRadius - scaleposmodx, y - yRadius - scaleposmody, filter);
|
2015-02-19 06:33:32 +01:00
|
|
|
else {
|
|
|
|
float oldAlpha = image.getAlpha();
|
|
|
|
float oldAngle = image.getRotation();
|
|
|
|
if ((hoverEffect & EFFECT_EXPAND) > 0) {
|
2015-08-06 05:28:14 +02:00
|
|
|
if (scale.getValue() != 1f) {
|
|
|
|
image = image.getScaledCopy(scale.getValue());
|
2015-02-19 06:33:32 +01:00
|
|
|
image.setAlpha(oldAlpha);
|
2015-02-16 23:53:24 +01:00
|
|
|
}
|
|
|
|
}
|
2015-02-19 06:33:32 +01:00
|
|
|
if ((hoverEffect & EFFECT_FADE) > 0)
|
2015-08-06 05:28:14 +02:00
|
|
|
image.setAlpha(alpha.getValue());
|
2015-02-19 06:33:32 +01:00
|
|
|
if ((hoverEffect & EFFECT_ROTATE) > 0)
|
2015-08-06 05:28:14 +02:00
|
|
|
image.setRotation(angle.getValue());
|
2016-11-05 21:49:20 +01:00
|
|
|
image.draw(x - xRadius - scaleposmodx, y - yRadius - scaleposmody, filter);
|
2015-03-19 08:04:35 +01:00
|
|
|
if (image == this.img) {
|
2015-02-19 06:33:32 +01:00
|
|
|
image.setAlpha(oldAlpha);
|
|
|
|
image.setRotation(oldAngle);
|
2015-02-02 06:15:16 +01:00
|
|
|
}
|
2014-06-30 04:17:04 +02:00
|
|
|
}
|
2015-02-19 06:33:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 3-part images
|
|
|
|
else {
|
|
|
|
if (hoverEffect == 0) {
|
|
|
|
image.draw(x - xRadius + imgL.getWidth(), y - yRadius, filter);
|
|
|
|
imgL.draw(x - xRadius, y - yRadius, filter);
|
|
|
|
imgR.draw(x + xRadius - imgR.getWidth(), y - yRadius, filter);
|
|
|
|
} else if ((hoverEffect & EFFECT_FADE) > 0) {
|
|
|
|
float a = image.getAlpha(), aL = imgL.getAlpha(), aR = imgR.getAlpha();
|
2015-08-06 05:28:14 +02:00
|
|
|
float currentAlpha = alpha.getValue();
|
|
|
|
image.setAlpha(currentAlpha);
|
|
|
|
imgL.setAlpha(currentAlpha);
|
|
|
|
imgR.setAlpha(currentAlpha);
|
2015-02-19 06:33:32 +01:00
|
|
|
image.draw(x - xRadius + imgL.getWidth(), y - yRadius, filter);
|
|
|
|
imgL.draw(x - xRadius, y - yRadius, filter);
|
|
|
|
imgR.draw(x + xRadius - imgR.getWidth(), y - yRadius, filter);
|
|
|
|
image.setAlpha(a);
|
|
|
|
imgL.setAlpha(aL);
|
|
|
|
imgR.setAlpha(aR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// text
|
2015-02-02 06:15:16 +01:00
|
|
|
if (text != null)
|
|
|
|
font.drawString(x - font.getWidth(text) / 2f, y - font.getLineHeight() / 2f, text, color);
|
2014-06-30 04:17:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the coordinates are within the button bounds.
|
2014-12-24 05:41:37 +01:00
|
|
|
* @param cx the x coordinate
|
|
|
|
* @param cy the y coordinate
|
2014-06-30 04:17:04 +02:00
|
|
|
*/
|
|
|
|
public boolean contains(float cx, float cy) {
|
|
|
|
return ((cx > x - xRadius && cx < x + xRadius) &&
|
|
|
|
(cy > y - yRadius && cy < y + yRadius));
|
|
|
|
}
|
2014-12-24 05:41:37 +01:00
|
|
|
|
2015-02-19 21:05:55 +01:00
|
|
|
/**
|
|
|
|
* Returns true if the coordinates are within the button bounds and the
|
|
|
|
* pixel at the specified location has an alpha level above the given bound.
|
|
|
|
* @param cx the x coordinate
|
|
|
|
* @param cy the y coordinate
|
|
|
|
* @param alpha the alpha level lower bound
|
|
|
|
*/
|
|
|
|
public boolean contains(float cx, float cy, float alpha) {
|
|
|
|
Image image = this.img;
|
|
|
|
if (image == null)
|
|
|
|
image = anim.getCurrentFrame();
|
|
|
|
float xRad = img.getWidth() / 2f, yRad = img.getHeight() / 2f;
|
|
|
|
|
|
|
|
return ((cx > x - xRad && cx < x + xRad) &&
|
|
|
|
(cy > y - yRad && cy < y + yRad) &&
|
|
|
|
image.getAlphaAt((int) (cx - (x - xRad)), (int) (cy - (y - yRad))) > alpha);
|
|
|
|
}
|
|
|
|
|
2014-12-24 05:41:37 +01:00
|
|
|
/**
|
2015-02-02 06:15:16 +01:00
|
|
|
* Resets the hover fields for the button.
|
2014-12-24 05:41:37 +01:00
|
|
|
*/
|
2015-02-02 06:15:16 +01:00
|
|
|
public void resetHover() {
|
2015-02-16 23:53:24 +01:00
|
|
|
if ((hoverEffect & EFFECT_EXPAND) > 0) {
|
2015-08-06 05:28:14 +02:00
|
|
|
scale.setTime(0);
|
2015-02-02 06:15:16 +01:00
|
|
|
setHoverRadius();
|
2015-02-16 23:53:24 +01:00
|
|
|
}
|
|
|
|
if ((hoverEffect & EFFECT_FADE) > 0)
|
2015-08-06 05:28:14 +02:00
|
|
|
alpha.setTime(0);
|
2015-02-16 23:53:24 +01:00
|
|
|
if ((hoverEffect & EFFECT_ROTATE) > 0)
|
2015-08-06 05:28:14 +02:00
|
|
|
angle.setTime(0);
|
2015-08-13 02:03:50 +02:00
|
|
|
autoAnimationForward = true;
|
2014-12-24 05:41:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-02-16 23:53:24 +01:00
|
|
|
* Removes all hover effects that have been set for the button.
|
|
|
|
*/
|
2015-08-06 05:28:14 +02:00
|
|
|
public void removeHoverEffects() {
|
|
|
|
this.hoverEffect = 0;
|
|
|
|
this.scale = null;
|
|
|
|
this.alpha = null;
|
|
|
|
this.angle = null;
|
2015-08-13 02:03:50 +02:00
|
|
|
autoAnimationForward = true;
|
2015-08-06 05:28:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the hover animation duration.
|
|
|
|
* @param duration the duration, in milliseconds
|
|
|
|
*/
|
|
|
|
public void setHoverAnimationDuration(int duration) {
|
|
|
|
this.animationDuration = duration;
|
|
|
|
if (scale != null)
|
|
|
|
scale.setDuration(duration);
|
|
|
|
if (alpha != null)
|
|
|
|
alpha.setDuration(duration);
|
|
|
|
if (angle != null)
|
|
|
|
angle.setDuration(duration);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the hover animation equation.
|
|
|
|
* @param eqn the equation to use
|
|
|
|
*/
|
|
|
|
public void setHoverAnimationEquation(AnimationEquation eqn) {
|
|
|
|
this.animationEqn = eqn;
|
|
|
|
if (scale != null)
|
|
|
|
scale.setEquation(eqn);
|
|
|
|
if (alpha != null)
|
|
|
|
alpha.setEquation(eqn);
|
|
|
|
if (angle != null)
|
|
|
|
angle.setEquation(eqn);
|
|
|
|
}
|
2015-02-16 23:53:24 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the "expand" hover effect.
|
2015-02-02 06:15:16 +01:00
|
|
|
*/
|
2015-08-06 05:28:14 +02:00
|
|
|
public void setHoverExpand() { setHoverExpand(DEFAULT_SCALE_MAX, this.dir); }
|
2015-02-02 06:15:16 +01:00
|
|
|
|
|
|
|
/**
|
2015-02-16 23:53:24 +01:00
|
|
|
* Sets the "expand" hover effect.
|
2015-08-06 05:28:14 +02:00
|
|
|
* @param scale the maximum scale factor
|
2014-12-24 05:41:37 +01:00
|
|
|
*/
|
2015-02-02 06:15:16 +01:00
|
|
|
public void setHoverExpand(float scale) { setHoverExpand(scale, this.dir); }
|
|
|
|
|
|
|
|
/**
|
2015-02-16 23:53:24 +01:00
|
|
|
* Sets the "expand" hover effect.
|
2015-02-02 06:15:16 +01:00
|
|
|
* @param dir the expansion direction
|
|
|
|
*/
|
2015-08-06 05:28:14 +02:00
|
|
|
public void setHoverExpand(Expand dir) { setHoverExpand(DEFAULT_SCALE_MAX, dir); }
|
2014-12-24 05:41:37 +01:00
|
|
|
|
|
|
|
/**
|
2015-02-16 23:53:24 +01:00
|
|
|
* Sets the "expand" hover effect.
|
2015-08-06 05:28:14 +02:00
|
|
|
* @param scale the maximum scale factor
|
2015-02-02 06:15:16 +01:00
|
|
|
* @param dir the expansion direction
|
2014-12-24 05:41:37 +01:00
|
|
|
*/
|
2015-02-02 06:15:16 +01:00
|
|
|
public void setHoverExpand(float scale, Expand dir) {
|
2015-02-16 23:53:24 +01:00
|
|
|
hoverEffect |= EFFECT_EXPAND;
|
2015-08-06 05:28:14 +02:00
|
|
|
this.scale = new AnimatedValue(animationDuration, 1f, scale, animationEqn);
|
2015-02-02 06:15:16 +01:00
|
|
|
this.dir = dir;
|
|
|
|
}
|
2014-12-24 05:41:37 +01:00
|
|
|
|
|
|
|
/**
|
2015-02-16 23:53:24 +01:00
|
|
|
* Sets the "fade" hover effect.
|
2015-02-02 06:15:16 +01:00
|
|
|
*/
|
2015-08-06 05:28:14 +02:00
|
|
|
public void setHoverFade() { setHoverFade(DEFAULT_ALPHA_BASE); }
|
2015-02-02 06:15:16 +01:00
|
|
|
|
|
|
|
/**
|
2015-02-16 23:53:24 +01:00
|
|
|
* Sets the "fade" hover effect.
|
2015-08-06 05:28:14 +02:00
|
|
|
* @param baseAlpha the base alpha level to fade in from
|
2015-02-02 06:15:16 +01:00
|
|
|
*/
|
|
|
|
public void setHoverFade(float baseAlpha) {
|
2015-02-16 23:53:24 +01:00
|
|
|
hoverEffect |= EFFECT_FADE;
|
2015-08-06 05:28:14 +02:00
|
|
|
this.alpha = new AnimatedValue(animationDuration, baseAlpha, 1f, animationEqn);
|
2015-02-02 06:15:16 +01:00
|
|
|
}
|
|
|
|
|
2015-02-16 23:53:24 +01:00
|
|
|
/**
|
|
|
|
* Sets the "rotate" hover effect.
|
|
|
|
*/
|
2015-08-06 05:28:14 +02:00
|
|
|
public void setHoverRotate() { setHoverRotate(DEFAULT_ANGLE_MAX); }
|
2015-02-16 23:53:24 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the "rotate" hover effect.
|
2015-08-06 05:28:14 +02:00
|
|
|
* @param maxAngle the maximum rotation angle, in degrees
|
2015-02-16 23:53:24 +01:00
|
|
|
*/
|
|
|
|
public void setHoverRotate(float maxAngle) {
|
|
|
|
hoverEffect |= EFFECT_ROTATE;
|
2015-08-06 05:28:14 +02:00
|
|
|
this.angle = new AnimatedValue(animationDuration, 0f, maxAngle, animationEqn);
|
2015-02-16 23:53:24 +01:00
|
|
|
}
|
|
|
|
|
2015-02-02 06:15:16 +01:00
|
|
|
/**
|
|
|
|
* Processes a hover action depending on whether or not the cursor
|
2014-12-24 05:41:37 +01:00
|
|
|
* is hovering over the button.
|
|
|
|
* @param delta the delta interval
|
|
|
|
* @param cx the x coordinate
|
|
|
|
* @param cy the y coordinate
|
|
|
|
*/
|
|
|
|
public void hoverUpdate(int delta, float cx, float cy) {
|
2015-02-19 21:05:55 +01:00
|
|
|
hoverUpdate(delta, contains(cx, cy));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Processes a hover action depending on whether or not the cursor
|
|
|
|
* is hovering over the button, only if the specified pixel of the
|
|
|
|
* image has an alpha level above the given bound.
|
|
|
|
* @param delta the delta interval
|
|
|
|
* @param cx the x coordinate
|
|
|
|
* @param cy the y coordinate
|
|
|
|
* @param alpha the alpha level lower bound
|
|
|
|
*/
|
|
|
|
public void hoverUpdate(int delta, float cx, float cy, float alpha) {
|
|
|
|
hoverUpdate(delta, contains(cx, cy, alpha));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Processes a hover action depending on whether or not the cursor
|
|
|
|
* is hovering over the button.
|
|
|
|
* @param delta the delta interval
|
|
|
|
* @param isHover true if the cursor is currently hovering over the button
|
|
|
|
*/
|
|
|
|
public void hoverUpdate(int delta, boolean isHover) {
|
2015-02-16 23:53:24 +01:00
|
|
|
if (hoverEffect == 0)
|
2015-02-02 06:15:16 +01:00
|
|
|
return;
|
2015-02-16 23:53:24 +01:00
|
|
|
|
2015-08-06 05:28:14 +02:00
|
|
|
int d = delta * (isHover ? 1 : -1);
|
|
|
|
|
2015-02-16 23:53:24 +01:00
|
|
|
// scale the button
|
|
|
|
if ((hoverEffect & EFFECT_EXPAND) > 0) {
|
2015-08-06 05:28:14 +02:00
|
|
|
if (scale.update(d))
|
2015-02-16 23:53:24 +01:00
|
|
|
setHoverRadius();
|
|
|
|
}
|
|
|
|
|
|
|
|
// fade the button
|
2015-08-06 05:28:14 +02:00
|
|
|
if ((hoverEffect & EFFECT_FADE) > 0)
|
|
|
|
alpha.update(d);
|
2015-02-16 23:53:24 +01:00
|
|
|
|
|
|
|
// rotate the button
|
2015-08-06 05:28:14 +02:00
|
|
|
if ((hoverEffect & EFFECT_ROTATE) > 0)
|
|
|
|
angle.update(d);
|
2014-12-24 05:41:37 +01:00
|
|
|
}
|
|
|
|
|
2015-08-13 02:03:50 +02:00
|
|
|
/**
|
|
|
|
* Automatically advances the hover animation in a loop.
|
|
|
|
* @param delta the delta interval
|
|
|
|
* @param reverseAtEnd whether to reverse or restart the animation upon reaching the end
|
|
|
|
*/
|
|
|
|
public void autoHoverUpdate(int delta, boolean reverseAtEnd) {
|
|
|
|
if (hoverEffect == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
int time = ((hoverEffect & EFFECT_EXPAND) > 0) ? scale.getTime() :
|
|
|
|
((hoverEffect & EFFECT_FADE) > 0) ? alpha.getTime() :
|
|
|
|
((hoverEffect & EFFECT_ROTATE) > 0) ? angle.getTime() : -1;
|
|
|
|
if (time == -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
int d = delta * (autoAnimationForward ? 1 : -1);
|
2015-08-31 00:56:05 +02:00
|
|
|
if (Utils.clamp(time + d, 0, animationDuration) == time) {
|
2015-08-13 02:03:50 +02:00
|
|
|
if (reverseAtEnd)
|
|
|
|
autoAnimationForward = !autoAnimationForward;
|
|
|
|
else {
|
|
|
|
if ((hoverEffect & EFFECT_EXPAND) > 0)
|
|
|
|
scale.setTime(0);
|
|
|
|
if ((hoverEffect & EFFECT_FADE) > 0)
|
|
|
|
alpha.setTime(0);
|
|
|
|
if ((hoverEffect & EFFECT_ROTATE) > 0)
|
|
|
|
angle.setTime(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hoverUpdate(delta, autoAnimationForward);
|
|
|
|
}
|
|
|
|
|
2014-12-24 05:41:37 +01:00
|
|
|
/**
|
|
|
|
* Set x and y radius of the button based on current scale factor
|
|
|
|
* and expansion direction.
|
|
|
|
*/
|
|
|
|
private void setHoverRadius() {
|
2015-02-19 06:33:32 +01:00
|
|
|
Image image = this.img;
|
|
|
|
if (image == null)
|
|
|
|
image = anim.getCurrentFrame();
|
2015-02-19 01:35:26 +01:00
|
|
|
|
2014-12-24 05:41:37 +01:00
|
|
|
int xOffset = 0, yOffset = 0;
|
2015-08-06 05:28:14 +02:00
|
|
|
float currentScale = scale.getValue();
|
2015-02-19 01:35:26 +01:00
|
|
|
if (dir != Expand.CENTER) {
|
|
|
|
// offset by difference between normal/scaled image dimensions
|
2015-08-06 05:28:14 +02:00
|
|
|
xOffset = (int) ((currentScale - 1f) * image.getWidth());
|
|
|
|
yOffset = (int) ((currentScale - 1f) * image.getHeight());
|
2015-02-19 01:35:26 +01:00
|
|
|
if (dir == Expand.UP || dir == Expand.DOWN)
|
|
|
|
xOffset = 0; // no horizontal offset
|
|
|
|
if (dir == Expand.RIGHT || dir == Expand.LEFT)
|
|
|
|
yOffset = 0; // no vertical offset
|
|
|
|
if (dir == Expand.RIGHT || dir == Expand.DOWN_RIGHT || dir == Expand.UP_RIGHT)
|
|
|
|
xOffset *= -1; // flip x for right
|
|
|
|
if (dir == Expand.DOWN || dir == Expand.DOWN_LEFT || dir == Expand.DOWN_RIGHT)
|
|
|
|
yOffset *= -1; // flip y for down
|
2014-12-24 05:41:37 +01:00
|
|
|
}
|
2015-08-06 05:28:14 +02:00
|
|
|
this.xRadius = ((image.getWidth() * currentScale) + xOffset) / 2f;
|
|
|
|
this.yRadius = ((image.getHeight() * currentScale) + yOffset) / 2f;
|
2014-12-24 05:41:37 +01:00
|
|
|
}
|
2015-02-18 04:49:19 +01:00
|
|
|
}
|