2 * Sixth 3D engine. Author: Svjatoslav Agejenko.
3 * This project is released under Creative Commons Zero (CC0) license.
5 package eu.svjatoslav.sixth.e3d.gui.humaninput;
7 import eu.svjatoslav.sixth.e3d.geometry.Point2D;
8 import eu.svjatoslav.sixth.e3d.gui.Avatar;
9 import eu.svjatoslav.sixth.e3d.gui.ViewPanel;
10 import eu.svjatoslav.sixth.e3d.gui.ViewRenderListener;
13 import java.awt.event.*;
14 import java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.List;
20 * This class is responsible for tracking human input devices (keyboard, mouse, etc.) and
21 * forwarding those inputs to subsequent virtual components.
23 public class HIDInputTracker implements
24 MouseMotionListener, KeyListener, MouseListener, MouseWheelListener, ViewRenderListener {
27 * <p> Map of pressed keys. </p>
28 * <p> Key is mouse button code. </p>
29 * <p> Value is system milliseconds when button was pressed. </p>
30 * <p> So by reading the map one can determine currently pressed buttons as well as duration. </p>
32 private final Map<Integer, Long> pressedKeysToPressedTimeMap = new HashMap<>();
33 private final List<MouseEvent> detectedMouseEvents = new ArrayList<>();
34 private final List<KeyEvent> detectedKeyEvents = new ArrayList<>();
35 private int wheelMovedDirection = 0;
36 private final Point2D mouseDraggedDirection = new Point2D();
37 private Point2D oldMouseCoordinatesWhenDragging;
38 private final ViewPanel viewPanel;
39 private Point2D currentMouseLocation;
40 private boolean mouseMoved;
41 private boolean mouseWithinWindow = false;
43 public HIDInputTracker(final ViewPanel viewPanel) {
44 this.viewPanel = viewPanel;
52 public boolean beforeRender(final ViewPanel viewPanel, final int millisecondsSinceLastFrame) {
53 boolean viewUpdateNeeded = handleKeyboardEvents();
54 viewUpdateNeeded |= handleMouseClicksAndHover(viewPanel);
55 viewUpdateNeeded |= handleMouseDragging();
56 viewUpdateNeeded |= handleMouseVerticalScrolling();
57 return viewUpdateNeeded;
60 private void bind(final JPanel panel) {
61 panel.addMouseMotionListener(this);
63 panel.addKeyListener(this);
65 panel.addMouseListener(this);
67 panel.addMouseWheelListener(this);
71 * @return <code>true</code> if view needs to be repainted.
73 private boolean handleKeyboardEvents() {
74 final KeyboardInputHandler currentFocusOwner = viewPanel.getKeyboardFocusStack().getCurrentFocusOwner();
75 ArrayList<KeyEvent> unprocessedKeyboardEvents = getUnprocessedKeyboardEvents();
77 return currentFocusOwner != null
78 && forwardKeyboardEventsToFocusOwner(currentFocusOwner, unprocessedKeyboardEvents);
81 private ArrayList<KeyEvent> getUnprocessedKeyboardEvents() {
82 synchronized (detectedKeyEvents) {
83 ArrayList<KeyEvent> result = new ArrayList<>(detectedKeyEvents);
84 detectedKeyEvents.clear();
90 * @return <code>true</code> if view update is needed.
92 private boolean forwardKeyboardEventsToFocusOwner(
93 KeyboardInputHandler currentFocusOwner, ArrayList<KeyEvent> keyEvents) {
94 boolean viewUpdateNeeded = false;
96 for (KeyEvent keyEvent : keyEvents)
97 viewUpdateNeeded |= processKeyEvent(currentFocusOwner, keyEvent);
99 return viewUpdateNeeded;
102 private boolean processKeyEvent(KeyboardInputHandler currentFocusOwner, KeyEvent keyEvent) {
103 switch (keyEvent.getID()) {
104 case KeyEvent.KEY_PRESSED:
105 return currentFocusOwner.keyPressed(keyEvent, viewPanel);
107 case KeyEvent.KEY_RELEASED:
108 return currentFocusOwner.keyReleased(keyEvent, viewPanel);
114 * @return <code>true</code> if view needs to be repainted.
116 private synchronized boolean handleMouseClicksAndHover(final ViewPanel viewPanel) {
117 boolean rerenderNeeded = false;
118 MouseEvent event = findClickLocationToTrace();
120 // process mouse clicks as a first priority
121 rerenderNeeded = true;
123 // when there are no mouse clicks, process mouse hovering
127 // we would like to re-render frame when user moved mouse, to see what objects mouse is hovering over
128 rerenderNeeded = true;
131 if (currentMouseLocation != null) {
132 // mouse click with button 0 amounts to mouse hovering event
133 event = new MouseEvent(currentMouseLocation, 0);
137 if (viewPanel.getRenderingContext() != null)
138 viewPanel.getRenderingContext().setMouseEvent(event);
140 return rerenderNeeded;
143 private MouseEvent findClickLocationToTrace() {
144 synchronized (detectedMouseEvents) {
145 if (detectedMouseEvents.isEmpty())
148 return detectedMouseEvents.remove(0);
152 boolean isKeyPressed(final int keyCode) {
153 return pressedKeysToPressedTimeMap.containsKey(keyCode);
157 public void keyPressed(final KeyEvent evt) {
158 synchronized (detectedKeyEvents) {
159 pressedKeysToPressedTimeMap.put(evt.getKeyCode(), System.currentTimeMillis());
160 detectedKeyEvents.add(evt);
165 public void keyReleased(final KeyEvent evt) {
166 synchronized (detectedKeyEvents) {
167 pressedKeysToPressedTimeMap.remove(evt.getKeyCode());
168 detectedKeyEvents.add(evt);
173 public void keyTyped(final KeyEvent e) {
177 public void mouseClicked(final java.awt.event.MouseEvent e) {
178 synchronized (detectedMouseEvents) {
179 detectedMouseEvents.add(new MouseEvent(e.getX(), e.getY(), e.getButton()));
184 public void mouseDragged(final java.awt.event.MouseEvent evt) {
185 final Point2D mouseLocation = new Point2D(evt.getX(), evt.getY());
187 if (oldMouseCoordinatesWhenDragging == null) {
188 oldMouseCoordinatesWhenDragging = mouseLocation;
192 mouseDraggedDirection.add(mouseLocation.clone().subtract(oldMouseCoordinatesWhenDragging));
194 oldMouseCoordinatesWhenDragging = mouseLocation;
198 public void mouseEntered(final java.awt.event.MouseEvent e) {
199 mouseWithinWindow = true;
203 public synchronized void mouseExited(final java.awt.event.MouseEvent e) {
204 mouseWithinWindow = false;
205 currentMouseLocation = null;
209 public synchronized void mouseMoved(final java.awt.event.MouseEvent e) {
210 currentMouseLocation = new Point2D(e.getX(), e.getY());
215 public void mousePressed(final java.awt.event.MouseEvent e) {
219 public void mouseReleased(final java.awt.event.MouseEvent evt) {
220 oldMouseCoordinatesWhenDragging = null;
224 public void mouseWheelMoved(final java.awt.event.MouseWheelEvent evt) {
225 wheelMovedDirection += evt.getWheelRotation();
229 * @return <code>true</code> if view needs to be repainted.
231 private boolean handleMouseVerticalScrolling() {
232 final Avatar avatar = viewPanel.getAvatar();
233 final double actualAcceleration = 50 * avatar.avatarAcceleration * (1 + (avatar.getMovementSpeed() / 10));
234 avatar.getMovementVector().y += (wheelMovedDirection * actualAcceleration);
235 avatar.enforceSpeedLimit();
236 boolean repaintNeeded = wheelMovedDirection != 0;
237 wheelMovedDirection = 0;
238 return repaintNeeded;
242 * @return <code>true</code> if view needs to be repainted.
244 private boolean handleMouseDragging() {
245 // TODO: It would be nice here to detect somehow whether user moved mouse or touch screen.
246 // in case of touch screen, we would like to reverse movement along X and Y axis.
248 final Avatar avatar = viewPanel.getAvatar();
250 avatar.setAngleXZ(avatar.getAngleXZ() - ((float) mouseDraggedDirection.x / 50));
251 avatar.setAngleYZ(avatar.getAngleYZ() - ((float) mouseDraggedDirection.y / 50));
254 // avatar.setAngleXZ(avatar.getAngleXZ() + ((float)
255 // mouseDraggedDirection.x / 50));
256 // avatar.setAngleYZ(avatar.getAngleYZ() + ((float)
257 // mouseDraggedDirection.y / 50));
259 boolean viewUpdateNeeded = !mouseDraggedDirection.isZero();
260 mouseDraggedDirection.zero();
261 return viewUpdateNeeded;