From eef9df9b87cff20322b58e7a9bf4407065c6851e Mon Sep 17 00:00:00 2001 From: Maxim Date: Fri, 8 Apr 2022 12:25:32 +0300 Subject: [PATCH] New mouse events --- src/main/java/mpengine/EngineInput.java | 53 +++++++++++++------ src/main/java/mpengine/example/Example.java | 9 ++-- .../mpengine/listener/MouseClickListener.java | 1 + .../listener/MouseMoveEventListener.java | 7 +++ .../listener/MousePressEventListener.java | 7 +++ .../mpengine/listener/MousePressListener.java | 12 +++++ 6 files changed, 69 insertions(+), 20 deletions(-) create mode 100644 src/main/java/mpengine/listener/MouseMoveEventListener.java create mode 100644 src/main/java/mpengine/listener/MousePressEventListener.java create mode 100644 src/main/java/mpengine/listener/MousePressListener.java diff --git a/src/main/java/mpengine/EngineInput.java b/src/main/java/mpengine/EngineInput.java index 0fef963..a005b10 100644 --- a/src/main/java/mpengine/EngineInput.java +++ b/src/main/java/mpengine/EngineInput.java @@ -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; @@ -14,6 +13,8 @@ public class EngineInput implements KeyListener, MouseMotionListener, MouseListener { private ArrayList mouseClickListeners; + private ArrayList mousePressListeners; + private ArrayList mouseMoveListeners; public static final int KEYS_COUNT = 256; private final boolean[] keys = new boolean[KEYS_COUNT]; @@ -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]; @@ -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 @@ -122,17 +126,18 @@ 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 @@ -140,18 +145,32 @@ 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); + } } diff --git a/src/main/java/mpengine/example/Example.java b/src/main/java/mpengine/example/Example.java index be80665..5bac8d1 100644 --- a/src/main/java/mpengine/example/Example.java +++ b/src/main/java/mpengine/example/Example.java @@ -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 @@ -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); + } } } diff --git a/src/main/java/mpengine/listener/MouseClickListener.java b/src/main/java/mpengine/listener/MouseClickListener.java index a9ba120..f55acc3 100644 --- a/src/main/java/mpengine/listener/MouseClickListener.java +++ b/src/main/java/mpengine/listener/MouseClickListener.java @@ -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()); } diff --git a/src/main/java/mpengine/listener/MouseMoveEventListener.java b/src/main/java/mpengine/listener/MouseMoveEventListener.java new file mode 100644 index 0000000..db38eeb --- /dev/null +++ b/src/main/java/mpengine/listener/MouseMoveEventListener.java @@ -0,0 +1,7 @@ +package mpengine.listener; + +import java.awt.event.MouseEvent; + +public interface MouseMoveEventListener { + void onMove(MouseEvent event); +} diff --git a/src/main/java/mpengine/listener/MousePressEventListener.java b/src/main/java/mpengine/listener/MousePressEventListener.java new file mode 100644 index 0000000..4b9f835 --- /dev/null +++ b/src/main/java/mpengine/listener/MousePressEventListener.java @@ -0,0 +1,7 @@ +package mpengine.listener; + +import java.awt.event.MouseEvent; + +public interface MousePressEventListener { + void onPress(MouseEvent event); +} diff --git a/src/main/java/mpengine/listener/MousePressListener.java b/src/main/java/mpengine/listener/MousePressListener.java new file mode 100644 index 0000000..8232b64 --- /dev/null +++ b/src/main/java/mpengine/listener/MousePressListener.java @@ -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()); + } +}