Skip to content
This repository has been archived by the owner on Oct 24, 2024. It is now read-only.

Commit

Permalink
Mouse is working!
Browse files Browse the repository at this point in the history
  • Loading branch information
CiroZDP committed Sep 10, 2024
1 parent 9900762 commit 24217d5
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 38 deletions.
3 changes: 1 addition & 2 deletions src/main/java/net/opencraft/renderer/Renderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,8 @@ public void init() {
public void render(Graphics2D g2d) {
Screen.renderCurrent(g2d);

if (Keyboard.isKeyJustPressed(KeyEvent.VK_F3))
if (Keyboard.isKeyClicked(KeyEvent.VK_F3))
F3Screen.toggleVisible();
Keyboard.poll();

if (F3Screen.isVisible())
F3Screen.draw(g2d);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void render(Graphics2D g2d) {
}

private void pollEvents() {
if (!Mouse.isButtonJustPressed(1))
if (!Mouse.isButtonClicked(1))
return;

if (setsel)
Expand Down
13 changes: 4 additions & 9 deletions src/main/java/net/opencraft/renderer/screens/SettingsScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ private SettingsScreen() {

@Override
public void render(Graphics2D g2d) {
pollEvents();
int width = Display.getWidth();
int height = Display.getHeight();

Expand Down Expand Up @@ -90,15 +89,14 @@ public void render(Graphics2D g2d) {
back_arrow.setHighlighted(MouseUtils.inRange(back_arrow));
back_arrow.draw(g2d);

//g2d.drawImage(oc.assets.getArrow(arrow1 ? ARROW_HIGHLIGHTED | ARROW_LEFT : ARROW_NORMAL | ARROW_LEFT), (width - 400) / 2 + 100, 15, 21, 33, null);
//g2d.drawImage(oc.assets.getArrow(arrow2 ? ARROW_HIGHLIGHTED | ARROW_RIGHT : ARROW_NORMAL | ARROW_RIGHT), (width - 400) / 2 + 273, 15, 21, 33, null);

font.drawShadow(g2d, translate(currentTab), (width - 400) / 2 + 155, 37, 0xFFFFFF);

g2d.setColor(Color.WHITE);
g2d.setStroke(new BasicStroke(2F));
g2d.drawLine(0, 60, width, 60);
g2d.drawLine(0, height - 100, width, height - 100);

pollEvents();
}

private void drawGeneralTab(Graphics g, Assets assets) {
Expand Down Expand Up @@ -245,11 +243,8 @@ public static SettingsScreen getInstance() {
}

public void pollEvents() {
if (!Mouse.isButtonDown(1)) {
Mouse.poll();
if (!Mouse.isButtonClicked(1))
return;
}
Mouse.poll();

if (musicBtn) {
SoundManager.toggleSound();
Expand All @@ -263,9 +258,9 @@ public void pollEvents() {
}

if (donesel) {
Screen.setCurrent(Menuscreen.class);
currentTab = "options.generalTab";
GameSettings.save();
Screen.setCurrent(Menuscreen.class);
}
}

Expand Down
34 changes: 20 additions & 14 deletions src/main/java/org/lwjgl/input/Keyboard.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

public class Keyboard implements KeyListener {

private static HashSet<Integer> currentKeys = new HashSet<>();
private static HashSet<Integer> prevKeys = new HashSet<>();

private static HashSet<Integer> pressedKeys = new HashSet<>();
private static Keyboard instance;

private static int currentKeyCode = -1;

private Keyboard(Component parent) {
parent.addKeyListener(this);
parent.setFocusable(true);
Expand All @@ -30,29 +30,35 @@ public static boolean isCreated() {
return instance != null;
}

public static boolean isKeyPressed(int keyCode) {
return currentKeys.contains(keyCode);
public static boolean isKeyDown(int keyCode) {
return pressedKeys.contains(keyCode);
}

public static boolean isKeyJustPressed(int keyCode) {
return currentKeys.contains(keyCode) && !prevKeys.contains(keyCode);
public static boolean isKeyClicked(int keyCode) {
boolean down = isKeyDown(keyCode);
if (down)
pressedKeys.remove(keyCode);

return down;
}

public static void poll() {
prevKeys.clear();
prevKeys.addAll(currentKeys);
public void keyTyped(KeyEvent e) {
}

public void keyTyped(KeyEvent e) {}

@Override
public void keyPressed(KeyEvent e) {
currentKeys.add(e.getKeyCode());
int keyCode = e.getKeyCode();
if (keyCode == currentKeyCode)
return;

pressedKeys.add(e.getKeyCode());
currentKeyCode = keyCode;
}

@Override
public void keyReleased(KeyEvent e) {
currentKeys.remove(e.getKeyCode());
pressedKeys.remove(e.getKeyCode());
currentKeyCode = -1;
}


Expand Down
31 changes: 19 additions & 12 deletions src/main/java/org/lwjgl/input/Mouse.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
import java.awt.Component;
import java.awt.event.*;
import java.util.HashSet;
import java.util.Set;

import javax.swing.JFrame;

public class Mouse implements MouseListener, MouseMotionListener, MouseWheelListener {

private static HashSet<Integer> currentMouseButtons = new HashSet<>();
private static HashSet<Integer> prevMouseButtons = new HashSet<>();
private static Set<Integer> pressed = new HashSet<>();
private static Mouse instance;
private static int x, y, xo, yo, xd, yd;
private static int dwheel;

private static int currentButton = -1;

private Mouse(Component parent) {
if (parent instanceof JFrame)
System.err.println("We don't recommend using JFrame for Mouse detection!");
Expand All @@ -37,17 +39,16 @@ public static boolean isCreated() {
return instance != null;
}

public static void poll() {
prevMouseButtons.clear();
prevMouseButtons.addAll(currentMouseButtons);
}

public static boolean isButtonDown(int button) {
return currentMouseButtons.contains(button);
return pressed.contains(button);
}

public static boolean isButtonJustPressed(int button) {
return currentMouseButtons.contains(button) && !prevMouseButtons.contains(button);
public static boolean isButtonClicked(int button) {
boolean down = isButtonDown(button);
if (down)
pressed.remove(button);

return down;
}

public static int getX() {
Expand Down Expand Up @@ -93,12 +94,18 @@ public void mouseWheelMoved(MouseWheelEvent e) {

@Override
public void mousePressed(MouseEvent e) {
currentMouseButtons.add(e.getButton());
int button = e.getButton();
if (button == currentButton)
return;

pressed.add(e.getButton());
currentButton = button;
}

@Override
public void mouseReleased(MouseEvent e) {
currentMouseButtons.remove(e.getButton());
pressed.remove(e.getButton());
currentButton = -1;
}

public void mouseEntered(MouseEvent e) {}
Expand Down

0 comments on commit 24217d5

Please sign in to comment.