Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Added basic mouse handler
- Loading branch information
Showing
4 changed files
with
121 additions
and
3 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package game.input; | ||
|
||
import static org.lwjgl.glfw.GLFW.*; | ||
|
||
import org.lwjgl.glfw.GLFWCursorPosCallback; | ||
|
||
public class Mouse extends GLFWCursorPosCallback { | ||
|
||
private double xPos, yPos; | ||
private long window; | ||
|
||
public Mouse(long window) { | ||
this.window = window; | ||
} | ||
|
||
@Override | ||
public void invoke(long window, double xPos, double yPos) { | ||
this.xPos = xPos; | ||
this.yPos = yPos; | ||
} | ||
|
||
public double getX() { | ||
return xPos; | ||
} | ||
|
||
public double getY() { | ||
return yPos; | ||
} | ||
|
||
public boolean leftClicked() { | ||
return (1 == glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_1)); | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
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; | ||
} | ||
|
||
|
||
} |