Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added basic mouse handler
  • Loading branch information
wjg12004 committed Apr 22, 2016
1 parent 7099989 commit ccf5f50
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 3 deletions.
11 changes: 10 additions & 1 deletion src/game/Main.java
Expand Up @@ -12,8 +12,10 @@ import org.lwjgl.opengl.GL11;

import game.graphics.Shader;
import game.input.Input;
import game.input.Mouse;
import game.level.Level;
import game.math.Matrix4f;
import game.menus.*;

public class Main implements Runnable {

Expand All @@ -27,7 +29,8 @@ public class Main implements Runnable {


//Must be explicitly declared to avoid being garbage collected
private Input input = new Input();
private Input input;
private Mouse mouse;

public void start() {
running = true;
Expand All @@ -51,9 +54,13 @@ public class Main implements Runnable {
return;
}

mouse = new Mouse(gameWindow);
input = new Input();

GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
glfwSetWindowPos(gameWindow, (vidmode.width() - width) / 2, (vidmode.height() - height) / 2);
glfwSetKeyCallback(gameWindow, input);
glfwSetCursorPosCallback(gameWindow, mouse);
glfwMakeContextCurrent(gameWindow);
glfwShowWindow(gameWindow);
GL.createCapabilities();
Expand Down Expand Up @@ -88,6 +95,7 @@ public class Main implements Runnable {
while (running) {
switch (gameState) {
case MAIN_MENU:
// TODO: Make a main menu
gameState = State.IN_GAME;
break;
case IN_GAME:
Expand Down Expand Up @@ -118,6 +126,7 @@ public class Main implements Runnable {
}
break;
case PAUSED:
// TODO: Make a pause menu
System.out.println("paused it");
gameState = State.IN_GAME;
break;
Expand Down
33 changes: 33 additions & 0 deletions src/game/input/Mouse.java
@@ -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));
}
}
3 changes: 1 addition & 2 deletions src/game/level/Level.java
Expand Up @@ -3,7 +3,6 @@ package game.level;
import game.entity.Entity;
import game.entity.Player;
import game.level.tiles.*;
import game.level.tiles.Tile;

public class Level {

Expand Down Expand Up @@ -100,6 +99,6 @@ public class Level {
private void playerWallCollisions() {
boolean collided = false;

// Check for collisions, flip flag, move player
// Check for collisions, flip flag, move player back
}
}
77 changes: 77 additions & 0 deletions src/game/menus/Button.java
@@ -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;
}


}

0 comments on commit ccf5f50

Please sign in to comment.