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.state.StateBasedGame; import com.lucasdnd.ags.system.ResourceLoader; public class IconButton extends Button { protected String text; // The text to be rendered with the Button protected Image icon; // The Icon of the Button private final static float scale = 4f; public IconButton(Image[] images, float buttonScale, float xPos, float yPos, String text, Image icon) throws SlickException { super(images, xPos, yPos); this.text = text; this.icon = icon; icon.setFilter(Image.FILTER_NEAREST); } @Override public void render(GameContainer container, StateBasedGame game, Graphics g) throws SlickException { // Render the Image g.scale(scale, scale); if(isPressed) { images[2].draw(xPos, yPos); } else if(isMoused) { images[1].draw(xPos, yPos); } else { images[0].draw(xPos, yPos); } g.scale(1 / scale, 1 / scale); // Render the text, if applicable if(text != null) { g.drawString(text, xPos + 18, yPos + 9); } // Render the Icon, if applicable if(icon != null) { g.scale(2f, 2f); if(isPressed) { g.drawImage(icon, xPos * 2 + width / 2, yPos * 2 + height / 2 - 3f); } else { g.drawImage(icon, xPos * 2 + width / 2, yPos * 2 + height / 2 - 5f); } g.scale(1/2f, 1/2f); } } /** * Check if got left clicked * @throws SlickException */ @Override public boolean gotLeftClicked(boolean leftMouseClicked, int mouseX, int mouseY) throws SlickException { if((leftMouseClicked) && (mouseX >= xPos * scale && mouseX <= xPos * scale + width * scale) && (mouseY >= yPos * scale && mouseY <= yPos * scale + height * scale)) { if(!isEnabled) { ResourceLoader.getInstance().popSound.play(); } else { clickSound.play(); return true; } } return false; } /** * Check if got right clicked */ @Override public boolean gotRightClicked(boolean rightMouseClicked, int mouseX, int mouseY) { if((rightMouseClicked) && (mouseX >= xPos * scale && mouseX <= xPos * scale + width * scale) && (mouseY >= yPos * scale && mouseY <= yPos * scale + height * scale)) { clickSound.play(); return true; } return false; } /** * Check if got moused over */ @Override public boolean gotMoused(int mouseX, int mouseY) { if( (mouseX >= xPos * scale && mouseX <= xPos * scale + width * scale) && (mouseY >= yPos * scale && mouseY <= yPos * scale + height * scale) ) { return true; } return false; } /** * Check if the Button is being pressed */ @Override public boolean gotPressed(boolean leftMouseDown, int mouseX, int mouseY) { if((leftMouseDown) && (mouseX >= xPos * scale && mouseX <= xPos * scale + width * scale) && (mouseY >= yPos * scale && mouseY <= yPos * scale + height * scale) ) { return true; } return false; } public String getText() { return text; } public void setText(String text) { this.text = text; } }