2 * Sixth 3D engine. Copyright ©2012-2018, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 3 of the GNU Lesser General Public License
6 * or later as published by the Free Software Foundation.
10 package eu.svjatoslav.sixth.e3d.gui.humaninput;
12 import eu.svjatoslav.sixth.e3d.geometry.Point2D;
13 import eu.svjatoslav.sixth.e3d.gui.Avatar;
14 import eu.svjatoslav.sixth.e3d.gui.ViewPanel;
15 import eu.svjatoslav.sixth.e3d.gui.ViewRenderListener;
18 import java.awt.event.*;
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.List;
24 public class UserInputTracker
25 implements MouseMotionListener, KeyListener, MouseListener, MouseWheelListener, ViewRenderListener {
29 * Key is keyboard key code.
30 * Value is system milliseconds when key was pressed.
32 * So by reading the map one can determine currently pressed keys as well as duration.
35 private final Map<Integer, Long> pressedKeysToPressedTimeMap = new HashMap<>();
36 private final List<MouseClick> detectedMouseClicks = new ArrayList<>();
37 private final List<KeyEvent> detectedKeyEvents = new ArrayList<>();
38 private int wheelMovedDirection = 0;
39 private Point2D mouseDraggedDirection = new Point2D();
40 private Point2D oldMouseCoordinatesWhenDragging;
41 private ViewPanel viewPanel;
42 private Point2D currentMouseLocation;
43 private boolean mouseMoved;
44 private boolean mouseWithinWindow = false;
46 public UserInputTracker(final ViewPanel viewPanel) {
47 this.viewPanel = viewPanel;
55 public boolean beforeRender(final ViewPanel viewPanel, final int millisecondsSinceLastFrame) {
57 boolean viewUpdateNeeded = handleDetectedMouseClicks(viewPanel);
59 viewUpdateNeeded |= handleDetectedKeyEvents();
61 viewPanel.getKeyboardFocusTracker().getCurrentFocusOwner().beforeRender(viewPanel,
62 millisecondsSinceLastFrame);
64 viewUpdateNeeded |= trackMouse();
66 return viewUpdateNeeded;
69 private void bind(final JPanel panel) {
70 panel.addMouseMotionListener(this);
72 panel.addKeyListener(this);
74 panel.addMouseListener(this);
76 panel.addMouseWheelListener(this);
79 private boolean handleDetectedKeyEvents() {
80 boolean keyEventsHandled = false;
82 final UserInputHandler currentFocusOwner = viewPanel.getKeyboardFocusTracker().getCurrentFocusOwner();
84 synchronized (detectedKeyEvents) {
85 if (currentFocusOwner == null) {
86 detectedKeyEvents.clear();
90 while (!detectedKeyEvents.isEmpty()) {
91 final KeyEvent keyEvent = detectedKeyEvents.remove(0);
93 switch (keyEvent.getID()) {
94 case KeyEvent.KEY_PRESSED:
95 currentFocusOwner.keyPressed(keyEvent, viewPanel);
96 keyEventsHandled = true;
99 case KeyEvent.KEY_RELEASED:
100 currentFocusOwner.keyReleased(keyEvent, viewPanel);
101 keyEventsHandled = true;
107 return keyEventsHandled;
111 * @return <code>true</code> if view needs to be repainted.
113 private synchronized boolean handleDetectedMouseClicks(final ViewPanel viewPanel) {
114 if (detectedMouseClicks.isEmpty()) {
116 if (currentMouseLocation != null)
117 viewPanel.getRenderingContext().mouseClick = new MouseClick(currentMouseLocation, 0);
126 viewPanel.getRenderingContext().mouseClick = detectedMouseClicks.remove(0);
131 boolean isKeyPressed(final int keyCode) {
132 return pressedKeysToPressedTimeMap.containsKey(keyCode);
136 public void keyPressed(final KeyEvent evt) {
137 synchronized (detectedKeyEvents) {
138 pressedKeysToPressedTimeMap.put(evt.getKeyCode(), System.currentTimeMillis());
139 detectedKeyEvents.add(evt);
140 viewPanel.repaintDuringNextViewUpdate();
145 public void keyReleased(final KeyEvent evt) {
146 synchronized (detectedKeyEvents) {
147 pressedKeysToPressedTimeMap.remove(evt.getKeyCode());
148 detectedKeyEvents.add(evt);
149 viewPanel.repaintDuringNextViewUpdate();
154 public void keyTyped(final KeyEvent e) {
158 public void mouseClicked(final MouseEvent e) {
159 synchronized (detectedMouseClicks) {
160 detectedMouseClicks.add(new MouseClick(e.getX(), e.getY(), e.getButton()));
165 public void mouseDragged(final java.awt.event.MouseEvent evt) {
166 final Point2D mouseLocation = new Point2D(evt.getX(), evt.getY());
168 if (oldMouseCoordinatesWhenDragging == null) {
169 oldMouseCoordinatesWhenDragging = mouseLocation;
173 mouseDraggedDirection.add(mouseLocation.clone().subtract(oldMouseCoordinatesWhenDragging));
175 oldMouseCoordinatesWhenDragging = mouseLocation;
179 public void mouseEntered(final MouseEvent e) {
180 mouseWithinWindow = true;
184 public synchronized void mouseExited(final MouseEvent e) {
185 mouseWithinWindow = false;
186 currentMouseLocation = null;
190 public synchronized void mouseMoved(final MouseEvent e) {
191 currentMouseLocation = new Point2D(e.getX(), e.getY());
196 public void mousePressed(final MouseEvent e) {
200 public void mouseReleased(final java.awt.event.MouseEvent evt) {
201 oldMouseCoordinatesWhenDragging = null;
205 public void mouseWheelMoved(final java.awt.event.MouseWheelEvent evt) {
206 wheelMovedDirection += evt.getWheelRotation();
210 * Interpret mouse movement
212 private boolean trackMouse() {
213 final Avatar avatar = viewPanel.getAvatar();
214 trackDragging(avatar);
215 trackVerticalScrolling(avatar);
217 boolean repaintNeeded = !mouseDraggedDirection.isZero() || (wheelMovedDirection != 0);
219 // reset movement counters
220 wheelMovedDirection = 0;
221 mouseDraggedDirection.zero();
223 return repaintNeeded;
226 private void trackVerticalScrolling(Avatar avatar) {
227 final double actualAcceleration = 50 * avatar.avatarAcceleration * (1 + (avatar.getMovementSpeed() / 10));
228 avatar.getMovementVector().y += (wheelMovedDirection * actualAcceleration);
229 avatar.enforceSpeedLimit();
232 private void trackDragging(Avatar avatar) {
233 // TODO: need to detect whether user moved mouse or touch screen
236 avatar.setAngleXZ(avatar.getAngleXZ() - ((float) mouseDraggedDirection.x / 50));
237 avatar.setAngleYZ(avatar.getAngleYZ() - ((float) mouseDraggedDirection.y / 50));
240 // avatar.setAngleXZ(avatar.getAngleXZ() + ((float)
241 // mouseDraggedDirection.x / 50));
242 // avatar.setAngleYZ(avatar.getAngleYZ() + ((float)
243 // mouseDraggedDirection.y / 50));