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 HIDInputTracker implements
25 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 HIDInputTracker(final ViewPanel viewPanel) {
47 this.viewPanel = viewPanel;
55 public boolean beforeRender(final ViewPanel viewPanel, final int millisecondsSinceLastFrame) {
56 boolean viewUpdateNeeded = handleKeyboardEvents();
57 viewUpdateNeeded |= handleMouseClicksAndHover(viewPanel);
58 viewUpdateNeeded |= handleMouseDragging();
59 viewUpdateNeeded |= handleMouseVerticalScrolling();
60 return viewUpdateNeeded;
63 private void bind(final JPanel panel) {
64 panel.addMouseMotionListener(this);
66 panel.addKeyListener(this);
68 panel.addMouseListener(this);
70 panel.addMouseWheelListener(this);
74 * @return <code>true</code> if view needs to be repainted.
76 private boolean handleKeyboardEvents() {
77 final UserInputHandler currentFocusOwner = viewPanel.getKeyboardFocusStack().getCurrentFocusOwner();
78 ArrayList<KeyEvent> unprocessedKeyboardEvents = getUnprocessedKeyboardEvents();
80 return currentFocusOwner != null
81 && forwardKeyboardEventsToFocusOwner(currentFocusOwner, unprocessedKeyboardEvents);
84 private ArrayList<KeyEvent> getUnprocessedKeyboardEvents() {
85 synchronized (detectedKeyEvents) {
86 ArrayList<KeyEvent> result = new ArrayList<>(detectedKeyEvents);
87 detectedKeyEvents.clear();
93 * @return <code>true</code> if view update is needed.
95 private boolean forwardKeyboardEventsToFocusOwner(
96 UserInputHandler currentFocusOwner, ArrayList<KeyEvent> keyEvents) {
97 boolean viewUpdateNeeded = false;
99 for (KeyEvent keyEvent : keyEvents)
100 viewUpdateNeeded |= processKeyEvent(currentFocusOwner, keyEvent);
102 return viewUpdateNeeded;
105 private boolean processKeyEvent(UserInputHandler currentFocusOwner, KeyEvent keyEvent) {
106 switch (keyEvent.getID()) {
107 case KeyEvent.KEY_PRESSED:
108 return currentFocusOwner.keyPressed(keyEvent, viewPanel);
110 case KeyEvent.KEY_RELEASED:
111 return currentFocusOwner.keyReleased(keyEvent, viewPanel);
117 * @return <code>true</code> if view needs to be repainted.
119 private synchronized boolean handleMouseClicksAndHover(final ViewPanel viewPanel) {
120 MouseClick unprocessedMouseClick = findUnprocessedMouseClick();
122 if (unprocessedMouseClick != null) {
123 viewPanel.getRenderingContext().mouseClick = unprocessedMouseClick;
126 return handleMouseHovering(viewPanel);
129 private MouseClick findUnprocessedMouseClick() {
130 synchronized (detectedMouseClicks) {
131 if (detectedMouseClicks.isEmpty())
134 return detectedMouseClicks.remove(0);
138 private boolean handleMouseHovering(ViewPanel viewPanel) {
139 if (currentMouseLocation != null)
140 // mouse click with button 0 amounts to mouse hovering event
141 viewPanel.getRenderingContext().mouseClick = new MouseClick(currentMouseLocation, 0);
150 boolean isKeyPressed(final int keyCode) {
151 return pressedKeysToPressedTimeMap.containsKey(keyCode);
155 public void keyPressed(final KeyEvent evt) {
156 synchronized (detectedKeyEvents) {
157 pressedKeysToPressedTimeMap.put(evt.getKeyCode(), System.currentTimeMillis());
158 detectedKeyEvents.add(evt);
163 public void keyReleased(final KeyEvent evt) {
164 synchronized (detectedKeyEvents) {
165 pressedKeysToPressedTimeMap.remove(evt.getKeyCode());
166 detectedKeyEvents.add(evt);
171 public void keyTyped(final KeyEvent e) {
175 public void mouseClicked(final MouseEvent e) {
176 synchronized (detectedMouseClicks) {
177 detectedMouseClicks.add(new MouseClick(e.getX(), e.getY(), e.getButton()));
182 public void mouseDragged(final java.awt.event.MouseEvent evt) {
183 final Point2D mouseLocation = new Point2D(evt.getX(), evt.getY());
185 if (oldMouseCoordinatesWhenDragging == null) {
186 oldMouseCoordinatesWhenDragging = mouseLocation;
190 mouseDraggedDirection.add(mouseLocation.clone().subtract(oldMouseCoordinatesWhenDragging));
192 oldMouseCoordinatesWhenDragging = mouseLocation;
196 public void mouseEntered(final MouseEvent e) {
197 mouseWithinWindow = true;
201 public synchronized void mouseExited(final MouseEvent e) {
202 mouseWithinWindow = false;
203 currentMouseLocation = null;
207 public synchronized void mouseMoved(final MouseEvent e) {
208 currentMouseLocation = new Point2D(e.getX(), e.getY());
213 public void mousePressed(final MouseEvent e) {
217 public void mouseReleased(final java.awt.event.MouseEvent evt) {
218 oldMouseCoordinatesWhenDragging = null;
222 public void mouseWheelMoved(final java.awt.event.MouseWheelEvent evt) {
223 wheelMovedDirection += evt.getWheelRotation();
227 * @return <code>true</code> if view needs to be repainted.
229 private boolean handleMouseVerticalScrolling() {
230 final Avatar avatar = viewPanel.getAvatar();
231 final double actualAcceleration = 50 * avatar.avatarAcceleration * (1 + (avatar.getMovementSpeed() / 10));
232 avatar.getMovementVector().y += (wheelMovedDirection * actualAcceleration);
233 avatar.enforceSpeedLimit();
234 boolean repaintNeeded = wheelMovedDirection != 0;
235 wheelMovedDirection = 0;
236 return repaintNeeded;
240 * @return <code>true</code> if view needs to be repainted.
242 private boolean handleMouseDragging() {
243 // TODO: need to detect whether user moved mouse or touch screen
245 final Avatar avatar = viewPanel.getAvatar();
247 avatar.setAngleXZ(avatar.getAngleXZ() - ((float) mouseDraggedDirection.x / 50));
248 avatar.setAngleYZ(avatar.getAngleYZ() - ((float) mouseDraggedDirection.y / 50));
251 // avatar.setAngleXZ(avatar.getAngleXZ() + ((float)
252 // mouseDraggedDirection.x / 50));
253 // avatar.setAngleYZ(avatar.getAngleYZ() + ((float)
254 // mouseDraggedDirection.y / 50));
256 boolean viewUpdateNeeded = !mouseDraggedDirection.isZero();
257 mouseDraggedDirection.zero();
258 return viewUpdateNeeded;