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.RenderingContext;
15 import eu.svjatoslav.sixth.e3d.gui.ViewPanel;
16 import eu.svjatoslav.sixth.e3d.gui.ViewRenderListener;
19 import java.awt.event.*;
20 import java.util.ArrayList;
21 import java.util.HashMap;
22 import java.util.List;
25 public class HIDInputTracker implements
26 MouseMotionListener, KeyListener, MouseListener, MouseWheelListener, ViewRenderListener {
30 * Key is keyboard key code.
31 * Value is system milliseconds when key was pressed.
33 * So by reading the map one can determine currently pressed keys as well as duration.
36 private final Map<Integer, Long> pressedKeysToPressedTimeMap = new HashMap<>();
37 private final List<MouseClick> detectedMouseClicks = new ArrayList<>();
38 private final List<KeyEvent> detectedKeyEvents = new ArrayList<>();
39 private int wheelMovedDirection = 0;
40 private Point2D mouseDraggedDirection = new Point2D();
41 private Point2D oldMouseCoordinatesWhenDragging;
42 private ViewPanel viewPanel;
43 private Point2D currentMouseLocation;
44 private boolean mouseMoved;
45 private boolean mouseWithinWindow = false;
47 public HIDInputTracker(final ViewPanel viewPanel) {
48 this.viewPanel = viewPanel;
56 public boolean beforeRender(final ViewPanel viewPanel, final int millisecondsSinceLastFrame) {
57 boolean viewUpdateNeeded = handleKeyboardEvents();
58 viewUpdateNeeded |= handleMouseClicksAndHover(viewPanel);
59 viewUpdateNeeded |= handleMouseDragging();
60 viewUpdateNeeded |= handleMouseVerticalScrolling();
61 return viewUpdateNeeded;
64 private void bind(final JPanel panel) {
65 panel.addMouseMotionListener(this);
67 panel.addKeyListener(this);
69 panel.addMouseListener(this);
71 panel.addMouseWheelListener(this);
75 * @return <code>true</code> if view needs to be repainted.
77 private boolean handleKeyboardEvents() {
78 final UserInputHandler currentFocusOwner = viewPanel.getKeyboardFocusStack().getCurrentFocusOwner();
79 ArrayList<KeyEvent> unprocessedKeyboardEvents = getUnprocessedKeyboardEvents();
81 return currentFocusOwner != null
82 && forwardKeyboardEventsToFocusOwner(currentFocusOwner, unprocessedKeyboardEvents);
85 private ArrayList<KeyEvent> getUnprocessedKeyboardEvents() {
86 synchronized (detectedKeyEvents) {
87 ArrayList<KeyEvent> result = new ArrayList<>(detectedKeyEvents);
88 detectedKeyEvents.clear();
94 * @return <code>true</code> if view update is needed.
96 private boolean forwardKeyboardEventsToFocusOwner(
97 UserInputHandler currentFocusOwner, ArrayList<KeyEvent> keyEvents) {
98 boolean viewUpdateNeeded = false;
100 for (KeyEvent keyEvent : keyEvents)
101 viewUpdateNeeded |= processKeyEvent(currentFocusOwner, keyEvent);
103 return viewUpdateNeeded;
106 private boolean processKeyEvent(UserInputHandler currentFocusOwner, KeyEvent keyEvent) {
107 switch (keyEvent.getID()) {
108 case KeyEvent.KEY_PRESSED:
109 return currentFocusOwner.keyPressed(keyEvent, viewPanel);
111 case KeyEvent.KEY_RELEASED:
112 return currentFocusOwner.keyReleased(keyEvent, viewPanel);
118 * @return <code>true</code> if view needs to be repainted.
120 private synchronized boolean handleMouseClicksAndHover(final ViewPanel viewPanel) {
121 MouseClick unprocessedMouseClick = findUnprocessedMouseClick();
123 if (unprocessedMouseClick != null) {
124 viewPanel.getRenderingContext().mouseClick = unprocessedMouseClick;
127 return handleMouseHovering(viewPanel);
130 private MouseClick findUnprocessedMouseClick() {
131 synchronized (detectedMouseClicks) {
132 if (detectedMouseClicks.isEmpty())
135 return detectedMouseClicks.remove(0);
139 private boolean handleMouseHovering(ViewPanel viewPanel) {
140 if (currentMouseLocation != null) {
141 RenderingContext renderingContext = viewPanel.getRenderingContext();
142 if (renderingContext != null)
143 renderingContext.mouseClick = new MouseClick(currentMouseLocation, 0);
144 // mouse click with button 0 amounts to mouse hovering event
154 boolean isKeyPressed(final int keyCode) {
155 return pressedKeysToPressedTimeMap.containsKey(keyCode);
159 public void keyPressed(final KeyEvent evt) {
160 synchronized (detectedKeyEvents) {
161 pressedKeysToPressedTimeMap.put(evt.getKeyCode(), System.currentTimeMillis());
162 detectedKeyEvents.add(evt);
167 public void keyReleased(final KeyEvent evt) {
168 synchronized (detectedKeyEvents) {
169 pressedKeysToPressedTimeMap.remove(evt.getKeyCode());
170 detectedKeyEvents.add(evt);
175 public void keyTyped(final KeyEvent e) {
179 public void mouseClicked(final MouseEvent e) {
180 synchronized (detectedMouseClicks) {
181 detectedMouseClicks.add(new MouseClick(e.getX(), e.getY(), e.getButton()));
186 public void mouseDragged(final java.awt.event.MouseEvent evt) {
187 final Point2D mouseLocation = new Point2D(evt.getX(), evt.getY());
189 if (oldMouseCoordinatesWhenDragging == null) {
190 oldMouseCoordinatesWhenDragging = mouseLocation;
194 mouseDraggedDirection.add(mouseLocation.clone().subtract(oldMouseCoordinatesWhenDragging));
196 oldMouseCoordinatesWhenDragging = mouseLocation;
200 public void mouseEntered(final MouseEvent e) {
201 mouseWithinWindow = true;
205 public synchronized void mouseExited(final MouseEvent e) {
206 mouseWithinWindow = false;
207 currentMouseLocation = null;
211 public synchronized void mouseMoved(final MouseEvent e) {
212 currentMouseLocation = new Point2D(e.getX(), e.getY());
217 public void mousePressed(final MouseEvent e) {
221 public void mouseReleased(final java.awt.event.MouseEvent evt) {
222 oldMouseCoordinatesWhenDragging = null;
226 public void mouseWheelMoved(final java.awt.event.MouseWheelEvent evt) {
227 wheelMovedDirection += evt.getWheelRotation();
231 * @return <code>true</code> if view needs to be repainted.
233 private boolean handleMouseVerticalScrolling() {
234 final Avatar avatar = viewPanel.getAvatar();
235 final double actualAcceleration = 50 * avatar.avatarAcceleration * (1 + (avatar.getMovementSpeed() / 10));
236 avatar.getMovementVector().y += (wheelMovedDirection * actualAcceleration);
237 avatar.enforceSpeedLimit();
238 boolean repaintNeeded = wheelMovedDirection != 0;
239 wheelMovedDirection = 0;
240 return repaintNeeded;
244 * @return <code>true</code> if view needs to be repainted.
246 private boolean handleMouseDragging() {
247 // TODO: need to detect whether user moved mouse or touch screen
249 final Avatar avatar = viewPanel.getAvatar();
251 avatar.setAngleXZ(avatar.getAngleXZ() - ((float) mouseDraggedDirection.x / 50));
252 avatar.setAngleYZ(avatar.getAngleYZ() - ((float) mouseDraggedDirection.y / 50));
255 // avatar.setAngleXZ(avatar.getAngleXZ() + ((float)
256 // mouseDraggedDirection.x / 50));
257 // avatar.setAngleYZ(avatar.getAngleYZ() + ((float)
258 // mouseDraggedDirection.y / 50));
260 boolean viewUpdateNeeded = !mouseDraggedDirection.isZero();
261 mouseDraggedDirection.zero();
262 return viewUpdateNeeded;