Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
GameNoodle/src/game/menus/Button.java
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
77 lines (56 sloc)
1.63 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package game.menus; | |
import java.awt.Rectangle; | |
import org.lwjgl.opengl.GL11; | |
import game.input.Mouse; | |
public class Button { | |
int xPos, yPos, width, height; | |
String text; | |
boolean clickable = true; | |
boolean visible = true; | |
private Mouse mouse; | |
private Rectangle collisionRect; | |
public Button(int xPos, int yPos, int width, int height, String text, Mouse mouse) { | |
this.xPos = xPos; | |
this.yPos = yPos; | |
this.width = width; | |
this.height = height; | |
this.text = text; | |
this.mouse = mouse; | |
collisionRect = new Rectangle(); | |
collisionRect.x = xPos; | |
collisionRect.y = yPos; | |
collisionRect.height = height; | |
collisionRect.width = width; | |
} | |
public void draw() { | |
if (visible) { | |
if (mouseOver() && clickable) { | |
GL11.glColor3f(.7f, .7f, .7f); | |
GL11.glBegin(GL11.GL_QUADS); | |
GL11.glVertex2d(xPos - 1, yPos + 1); | |
GL11.glVertex2d(xPos + width + 1, yPos + 1); | |
GL11.glVertex2d(xPos + width + 1, yPos + height - 1); | |
GL11.glVertex2d(xPos - 1, yPos + height - 1); | |
GL11.glEnd(); | |
GL11.glBegin(GL11.GL_QUADS); | |
GL11.glVertex2d(xPos + 1, yPos - 1); | |
GL11.glVertex2d(xPos + width - 1, yPos - 1); | |
GL11.glVertex2d(xPos + width - 1, yPos + height + 1); | |
GL11.glVertex2d(xPos + 1, yPos + height + 1); | |
GL11.glEnd(); | |
} | |
} | |
} | |
public boolean clicked() { | |
return collisionRect.contains(mouse.getX(), mouse.getY()) && mouse.leftClicked(); | |
} | |
public boolean mouseOver() { | |
return collisionRect.contains(mouse.getX(), mouse.getY()); | |
} | |
public void setClickable(boolean clickable) { | |
this.clickable = clickable; | |
} | |
public void setVisible(boolean visible) { | |
this.visible = visible; | |
} | |
} |