package com.lucasdnd.ags.ui;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Sound;
import org.newdawn.slick.state.StateBasedGame;
import com.lucasdnd.ags.system.ResourceLoader;
import com.lucasdnd.ags.ui.components.Clickable;
import com.lucasdnd.ags.ui.components.Mouseable;
public class Button extends UIComponent implements Clickable, Mouseable {
protected boolean isMoused; // Indicates if the player has the mouse over the button
protected boolean isPressed; // Indicates if the Button is being pressed
protected Sound clickSound; // Sound that plays when the player clicks
protected boolean isEnabled; // Indicates if the Button is enabled or disabled
protected Image disabledImage; // The sprite to use when the Button is disabled
protected boolean isBlinking; // Indicates if the Button is Blinking or not
protected int blinkDuration; // Blink duration, in milliseconds (delta)
protected int currentBlinkingTime; // Blinking counter
private boolean selected;
/**
* Includes a sprite for disabled Buttons
* @param enabledImages
* @param disabledImages
* @param buttonScale
* @param xPos
* @param yPos
* @throws SlickException
*/
public Button(Image[] enabledImages, Image disabledImage, float buttonScale, float xPos, float yPos) throws SlickException {
this(enabledImages, xPos, yPos);
this.disabledImage = disabledImage;
}
/**
* Buttons that can't be disabled
* @param images
* @param buttonScale
* @param xPos
* @param yPos
* @throws SlickException
*/
public Button(Image[] images, float xPos, float yPos) throws SlickException {
// One image for the normal button, one for the hovered button, one for the pressed button
this.images = images;
// Set the position on the screen
this.xPos = xPos;
this.yPos = yPos;
// Set the sizes
if (images != null) {
width = images[0].getWidth();
height = images[0].getHeight();
}
// Set the sound
clickSound = ResourceLoader.getInstance().clickSound;
// Enabled and Visible at first
isEnabled = true;
visible = true;
// Default blinking duration
blinkDuration = 500;
currentBlinkingTime = 0;
}
/**
* Check for mouse overs, basically
* @param container
* @param game
* @param delta
* @param mouseX
* @param mouseY
* @throws SlickException
*/
public void update(GameContainer container, StateBasedGame game, int delta, boolean leftMouseClicked,
boolean leftMouseDown, int mouseX, int mouseY) throws SlickException {
isPressed = gotPressed(leftMouseDown, mouseX, mouseY);
isMoused = gotMoused(mouseX, mouseY);
// Blinking controller
if(isBlinking) {
// Increase the count
currentBlinkingTime += 2;
// Check if it should change state
if(currentBlinkingTime >= blinkDuration) {
visible = !visible;
currentBlinkingTime = 0;
}
} else {
visible = true;
}
}
/**
* Renders the button, depending on its state
* @param container
* @param game
* @param g
* @throws SlickException
*/
public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException {
if (visible) {
if(!isEnabled) {
disabledImage.draw(xPos, yPos);
} else {
if (isPressed || selected) {
images[2].draw(xPos, yPos);
} else if (isMoused) {
images[1].draw(xPos, yPos);
} else {
images[0].draw(xPos, yPos);
}
}
}
}
/**
* Check if got left clicked
* @throws SlickException
*/
public boolean gotLeftClicked(boolean leftMouseClicked, int mouseX, int mouseY) throws SlickException {
if((leftMouseClicked) &&
(mouseX >= xPos && mouseX <= xPos + width) &&
(mouseY >= yPos && mouseY <= yPos + height)) {
if(!isEnabled) {
ResourceLoader.getInstance().popSound.play();
} else {
clickSound.play();
return true;
}
}
return false;
}
/**
* Check if got right clicked
*/
public boolean gotRightClicked(boolean rightMouseClicked, int mouseX, int mouseY) {
if((rightMouseClicked) &&
(mouseX >= xPos && mouseX <= xPos + width) &&
(mouseY >= yPos && mouseY <= yPos + height)) {
clickSound.play();
return true;
}
return false;
}
/**
* Check if got moused over
*/
public boolean gotMoused(int mouseX, int mouseY) {
if(
(mouseX >= xPos && mouseX <= xPos + width) &&
(mouseY >= yPos && mouseY <= yPos + height)
) {
return true;
}
return false;
}
/**
* Check if the Button is being pressed
*/
public boolean gotPressed(boolean leftMouseDown, int mouseX, int mouseY) {
if((leftMouseDown) &&
(mouseX >= xPos && mouseX <= xPos + width) &&
(mouseY >= yPos && mouseY <= yPos + height)
) {
return true;
}
return false;
}
public boolean isMoused() {
return isMoused;
}
public void setMoused(boolean isMoused) {
this.isMoused = isMoused;
}
public boolean isPressed() {
return isPressed;
}
public void setPressed(boolean isPressed) {
this.isPressed = isPressed;
}
public boolean isEnabled() {
return isEnabled;
}
public void setEnabled(boolean isEnabled) {
this.isEnabled = isEnabled;
}
public boolean isBlinking() {
return isBlinking;
}
public void setBlinking(boolean isBlinking) {
this.isBlinking = isBlinking;
}
public int getBlinkDuration() {
return blinkDuration;
}
public void setBlinkDuration(int blinkDuration) {
this.blinkDuration = blinkDuration;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
}