X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Feu%2Fsvjatoslav%2Fsixth%2Fe3d%2Fgui%2Fhumaninput%2FHIDInputTracker.java;h=c08fa8e21969d3d15ee40f27d267af94273c4083;hb=a3ff3683bd0a025061667b26b6fcf56fe20f0afc;hp=ee625d5079f4216ee10a3bd3a2b97826b46395db;hpb=4bb8945294848559aab76e248207781c6e097714;p=sixth-3d.git diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/HIDInputTracker.java b/src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/HIDInputTracker.java index ee625d5..c08fa8e 100755 --- a/src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/HIDInputTracker.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/HIDInputTracker.java @@ -1,17 +1,11 @@ /* - * Sixth 3D engine. Copyright ©2012-2018, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of version 3 of the GNU Lesser General Public License - * or later as published by the Free Software Foundation. - * + * Sixth 3D engine. Author: Svjatoslav Agejenko. + * This project is released under Creative Commons Zero (CC0) license. */ - package eu.svjatoslav.sixth.e3d.gui.humaninput; import eu.svjatoslav.sixth.e3d.geometry.Point2D; import eu.svjatoslav.sixth.e3d.gui.Avatar; -import eu.svjatoslav.sixth.e3d.gui.RenderingContext; import eu.svjatoslav.sixth.e3d.gui.ViewPanel; import eu.svjatoslav.sixth.e3d.gui.ViewRenderListener; @@ -22,24 +16,26 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +/** + * This class is responsible for tracking human input devices (keyboard, mouse, etc.) and + * forwarding those inputs to subsequent virtual components. + */ public class HIDInputTracker implements MouseMotionListener, KeyListener, MouseListener, MouseWheelListener, ViewRenderListener { /** - *
-     * Key is keyboard key code.
-     * Value is system milliseconds when key was pressed.
-     *
-     * So by reading the map one can determine currently pressed keys as well as duration.
-     * 
+ *

Map of pressed keys.

+ *

Key is mouse button code.

+ *

Value is system milliseconds when button was pressed.

+ *

So by reading the map one can determine currently pressed buttons as well as duration.

*/ private final Map pressedKeysToPressedTimeMap = new HashMap<>(); - private final List detectedMouseClicks = new ArrayList<>(); + private final List detectedMouseEvents = new ArrayList<>(); private final List detectedKeyEvents = new ArrayList<>(); + private final Point2D mouseDraggedDirection = new Point2D(); + private final ViewPanel viewPanel; private int wheelMovedDirection = 0; - private Point2D mouseDraggedDirection = new Point2D(); private Point2D oldMouseCoordinatesWhenDragging; - private ViewPanel viewPanel; private Point2D currentMouseLocation; private boolean mouseMoved; private boolean mouseWithinWindow = false; @@ -75,7 +71,7 @@ public class HIDInputTracker implements * @return true if view needs to be repainted. */ private boolean handleKeyboardEvents() { - final UserInputHandler currentFocusOwner = viewPanel.getKeyboardFocusStack().getCurrentFocusOwner(); + final KeyboardInputHandler currentFocusOwner = viewPanel.getKeyboardFocusStack().getCurrentFocusOwner(); ArrayList unprocessedKeyboardEvents = getUnprocessedKeyboardEvents(); return currentFocusOwner != null @@ -94,7 +90,7 @@ public class HIDInputTracker implements * @return true if view update is needed. */ private boolean forwardKeyboardEventsToFocusOwner( - UserInputHandler currentFocusOwner, ArrayList keyEvents) { + KeyboardInputHandler currentFocusOwner, ArrayList keyEvents) { boolean viewUpdateNeeded = false; for (KeyEvent keyEvent : keyEvents) @@ -103,7 +99,7 @@ public class HIDInputTracker implements return viewUpdateNeeded; } - private boolean processKeyEvent(UserInputHandler currentFocusOwner, KeyEvent keyEvent) { + private boolean processKeyEvent(KeyboardInputHandler currentFocusOwner, KeyEvent keyEvent) { switch (keyEvent.getID()) { case KeyEvent.KEY_PRESSED: return currentFocusOwner.keyPressed(keyEvent, viewPanel); @@ -118,39 +114,41 @@ public class HIDInputTracker implements * @return true if view needs to be repainted. */ private synchronized boolean handleMouseClicksAndHover(final ViewPanel viewPanel) { - MouseClick unprocessedMouseClick = findUnprocessedMouseClick(); + boolean rerenderNeeded = false; + MouseEvent event = findClickLocationToTrace(); + if (event != null) { + // process mouse clicks as a first priority + rerenderNeeded = true; + } else { + // when there are no mouse clicks, process mouse hovering + + if (mouseMoved) { + mouseMoved = false; + // we would like to re-render frame when user moved mouse, to see what objects mouse is hovering over + rerenderNeeded = true; + } + + if (currentMouseLocation != null) { + // mouse click with button 0 amounts to mouse hovering event + event = new MouseEvent(currentMouseLocation, 0); + } + } + + if (viewPanel.getRenderingContext() != null) + viewPanel.getRenderingContext().setMouseEvent(event); - if (unprocessedMouseClick != null) { - viewPanel.getRenderingContext().mouseClick = unprocessedMouseClick; - return false; - } else - return handleMouseHovering(viewPanel); + return rerenderNeeded; } - private MouseClick findUnprocessedMouseClick() { - synchronized (detectedMouseClicks) { - if (detectedMouseClicks.isEmpty()) + private MouseEvent findClickLocationToTrace() { + synchronized (detectedMouseEvents) { + if (detectedMouseEvents.isEmpty()) return null; - return detectedMouseClicks.remove(0); + return detectedMouseEvents.remove(0); } } - private boolean handleMouseHovering(ViewPanel viewPanel) { - if (currentMouseLocation != null) { - RenderingContext renderingContext = viewPanel.getRenderingContext(); - if (renderingContext != null) - renderingContext.mouseClick = new MouseClick(currentMouseLocation, 0); - // mouse click with button 0 amounts to mouse hovering event - } - - if (mouseMoved) { - mouseMoved = false; - return true; - } else - return false; - } - boolean isKeyPressed(final int keyCode) { return pressedKeysToPressedTimeMap.containsKey(keyCode); } @@ -176,9 +174,9 @@ public class HIDInputTracker implements } @Override - public void mouseClicked(final MouseEvent e) { - synchronized (detectedMouseClicks) { - detectedMouseClicks.add(new MouseClick(e.getX(), e.getY(), e.getButton())); + public void mouseClicked(final java.awt.event.MouseEvent e) { + synchronized (detectedMouseEvents) { + detectedMouseEvents.add(new MouseEvent(e.getX(), e.getY(), e.getButton())); } } @@ -197,24 +195,24 @@ public class HIDInputTracker implements } @Override - public void mouseEntered(final MouseEvent e) { + public void mouseEntered(final java.awt.event.MouseEvent e) { mouseWithinWindow = true; } @Override - public synchronized void mouseExited(final MouseEvent e) { + public synchronized void mouseExited(final java.awt.event.MouseEvent e) { mouseWithinWindow = false; currentMouseLocation = null; } @Override - public synchronized void mouseMoved(final MouseEvent e) { + public synchronized void mouseMoved(final java.awt.event.MouseEvent e) { currentMouseLocation = new Point2D(e.getX(), e.getY()); mouseMoved = true; } @Override - public void mousePressed(final MouseEvent e) { + public void mousePressed(final java.awt.event.MouseEvent e) { } @Override @@ -244,7 +242,8 @@ public class HIDInputTracker implements * @return true if view needs to be repainted. */ private boolean handleMouseDragging() { - // TODO: need to detect whether user moved mouse or touch screen + // TODO: It would be nice here to detect somehow whether user moved mouse or touch screen. + // in case of touch screen, we would like to reverse movement along X and Y axis. final Avatar avatar = viewPanel.getAvatar(); // for mouse