Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Commit

Permalink
New mouse events
Browse files Browse the repository at this point in the history
  • Loading branch information
MaximPixel committed Apr 8, 2022
1 parent 913417d commit eef9df9
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 20 deletions.
53 changes: 36 additions & 17 deletions src/main/java/mpengine/EngineInput.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package mpengine;

import mpengine.listener.MouseClickEventListener;
import mpengine.listener.MouseClickListener;
import mpengine.listener.*;

import java.awt.Canvas;
import java.awt.Point;
Expand All @@ -14,6 +13,8 @@

public class EngineInput implements KeyListener, MouseMotionListener, MouseListener {
private ArrayList<MouseClickEventListener> mouseClickListeners;
private ArrayList<MousePressEventListener> mousePressListeners;
private ArrayList<MouseMoveEventListener> mouseMoveListeners;

public static final int KEYS_COUNT = 256;
private final boolean[] keys = new boolean[KEYS_COUNT];
Expand All @@ -23,7 +24,7 @@ public class EngineInput implements KeyListener, MouseMotionListener, MouseListe

private Point mousePos = new Point();

public static final int BUTTONS_COUNT = 5;
public static final int BUTTONS_COUNT = 6;
private final boolean[] buttons = new boolean[BUTTONS_COUNT];
private boolean[] buttons_real = new boolean[BUTTONS_COUNT];
private boolean[] buttons_last = new boolean[BUTTONS_COUNT];
Expand Down Expand Up @@ -110,8 +111,11 @@ public void mouseDragged(MouseEvent e) {
}

@Override
public void mouseMoved(MouseEvent e) {
mousePos = e.getPoint();
public void mouseMoved(MouseEvent event) {
mousePos = event.getPoint();
if (mouseMoveListeners != null) {
mouseMoveListeners.forEach(listener -> listener.onMove(event));
}
}

@Override
Expand All @@ -122,36 +126,51 @@ public void mouseClicked(MouseEvent event) {
}

@Override
public void mouseEntered(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {}

@Override
public void mouseExited(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {}

@Override
public void mousePressed(MouseEvent e) {
buttons[e.getButton()] = true;
buttons_real[e.getButton()] = true;
public void mousePressed(MouseEvent event) {
buttons[event.getButton()] = true;
buttons_real[event.getButton()] = true;
if (mousePressListeners != null) {
mousePressListeners.forEach(listener -> listener.onPress(event));
}
}

@Override
public void mouseReleased(MouseEvent e) {
buttons[e.getButton()] = false;
}

private void checkMouseClickListeners() {
public void addMouseClickListenerEvent(MouseClickEventListener listener) {
if (mouseClickListeners == null) {
mouseClickListeners = new ArrayList<>();
}
}

public void addMouseClickListenerEvent(MouseClickEventListener listener) {
checkMouseClickListeners();
mouseClickListeners.add(listener);
}

public void addMouseClickListener(MouseClickListener listener) {
addMouseClickListenerEvent(listener);
}

public void addMousePressListenerEvent(MousePressEventListener listener) {
if (mousePressListeners == null) {
mousePressListeners = new ArrayList<>();
}
mousePressListeners.add(listener);
}

public void addMousePressListener(MousePressListener listener) {
addMousePressListenerEvent(listener);
}

public void addMouseMoveListenerEvent(MouseMoveEventListener listener) {
if (mouseMoveListeners == null) {
mouseMoveListeners = new ArrayList<>();
}
mouseMoveListeners.add(listener);
}
}
9 changes: 6 additions & 3 deletions src/main/java/mpengine/example/Example.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
import java.awt.*;

public class Example implements IEngineInterface {
private Point mousePoint;

public static void main(String[] args) {
IEngineInterface.start(new Example());
}

@Override
public void init(Settings settings) {
settings.getEngineInput().addMouseClickListener(
(mouseX, mouseY, button) -> System.out.println(mouseX + " " + mouseY + " " + button)
);
settings.getEngineInput().addMouseMoveListenerEvent(event -> mousePoint = event.getPoint());
}

@Override
Expand All @@ -25,5 +25,8 @@ public void updateLoop() {
@Override
public void drawLoop(Canvas canvas, Graphics2D graphics) {
graphics.drawOval(0, 0, canvas.getWidth(), canvas.getHeight());
if (mousePoint != null) {
graphics.drawOval(mousePoint.x, mousePoint.y, 32, 32);
}
}
}
1 change: 1 addition & 0 deletions src/main/java/mpengine/listener/MouseClickListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
public interface MouseClickListener extends MouseClickEventListener {
void onClick(int mouseX, int mouseY, int button);

@Override
default void onClick(MouseEvent event) {
onClick(event.getX(), event.getY(), event.getButton());
}
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/mpengine/listener/MouseMoveEventListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package mpengine.listener;

import java.awt.event.MouseEvent;

public interface MouseMoveEventListener {
void onMove(MouseEvent event);
}
7 changes: 7 additions & 0 deletions src/main/java/mpengine/listener/MousePressEventListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package mpengine.listener;

import java.awt.event.MouseEvent;

public interface MousePressEventListener {
void onPress(MouseEvent event);
}
12 changes: 12 additions & 0 deletions src/main/java/mpengine/listener/MousePressListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package mpengine.listener;

import java.awt.event.MouseEvent;

public interface MousePressListener extends MousePressEventListener {
void onPress(int mouseX, int mouseY, int button);

@Override
default void onPress(MouseEvent event) {
onPress(event.getX(), event.getY(), event.getButton());
}
}

0 comments on commit eef9df9

Please sign in to comment.