Removed ViewContext.
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / gui / humaninput / UserInputTracker.java
1 /*
2  * Sixth 3D engine. Copyright ©2012-2018, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
3  *
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.
7  *
8  */
9
10 package eu.svjatoslav.sixth.e3d.gui.humaninput;
11
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;
16
17 import javax.swing.*;
18 import java.awt.event.*;
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Map;
23
24 public class UserInputTracker
25         implements MouseMotionListener, KeyListener, MouseListener, MouseWheelListener, ViewRenderListener {
26
27     /**
28      * <pre>
29      * Key is keyboard key code.
30      * Value is system milliseconds when key was pressed.
31      *
32      * So by reading the map one can determine currently pressed keys as well as duration.
33      * </pre>
34      */
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;
45
46     public UserInputTracker(final ViewPanel viewPanel) {
47         this.viewPanel = viewPanel;
48         bind(viewPanel);
49     }
50
51     /**
52      * {@inheritDoc}
53      */
54     @Override
55     public boolean beforeRender(final ViewPanel viewPanel, final int millisecondsSinceLastFrame) {
56
57         boolean viewUpdateNeeded = handleDetectedMouseClicks(viewPanel);
58
59         viewUpdateNeeded |= handleDetectedKeyEvents();
60
61         viewPanel.getKeyboardFocusTracker().getCurrentFocusOwner().beforeRender(viewPanel,
62                 millisecondsSinceLastFrame);
63
64         viewUpdateNeeded |= trackMouse();
65
66         return viewUpdateNeeded;
67     }
68
69     private void bind(final JPanel panel) {
70         panel.addMouseMotionListener(this);
71
72         panel.addKeyListener(this);
73
74         panel.addMouseListener(this);
75
76         panel.addMouseWheelListener(this);
77     }
78
79     private boolean handleDetectedKeyEvents() {
80         boolean keyEventsHandled = false;
81
82         final UserInputHandler currentFocusOwner = viewPanel.getKeyboardFocusTracker().getCurrentFocusOwner();
83
84         synchronized (detectedKeyEvents) {
85             if (currentFocusOwner == null) {
86                 detectedKeyEvents.clear();
87                 return false;
88             }
89
90             while (!detectedKeyEvents.isEmpty()) {
91                 final KeyEvent keyEvent = detectedKeyEvents.remove(0);
92
93                 switch (keyEvent.getID()) {
94                     case KeyEvent.KEY_PRESSED:
95                         currentFocusOwner.keyPressed(keyEvent, viewPanel);
96                         keyEventsHandled = true;
97                         break;
98
99                     case KeyEvent.KEY_RELEASED:
100                         currentFocusOwner.keyReleased(keyEvent, viewPanel);
101                         keyEventsHandled = true;
102                         break;
103                 }
104             }
105         }
106
107         return keyEventsHandled;
108     }
109
110     /**
111      * @return <code>true</code> if view needs to be repainted.
112      */
113     private synchronized boolean handleDetectedMouseClicks(final ViewPanel viewPanel) {
114         if (detectedMouseClicks.isEmpty()) {
115
116             if (currentMouseLocation != null)
117                 viewPanel.getRenderingContext().mouseClick = new MouseClick(currentMouseLocation, 0);
118
119             if (mouseMoved) {
120                 mouseMoved = false;
121                 return true;
122             } else
123                 return false;
124         }
125
126         viewPanel.getRenderingContext().mouseClick = detectedMouseClicks.remove(0);
127
128         return true;
129     }
130
131     boolean isKeyPressed(final int keyCode) {
132         return pressedKeysToPressedTimeMap.containsKey(keyCode);
133     }
134
135     @Override
136     public void keyPressed(final KeyEvent evt) {
137         synchronized (detectedKeyEvents) {
138             pressedKeysToPressedTimeMap.put(evt.getKeyCode(), System.currentTimeMillis());
139             detectedKeyEvents.add(evt);
140             viewPanel.repaintDuringNextViewUpdate();
141         }
142     }
143
144     @Override
145     public void keyReleased(final KeyEvent evt) {
146         synchronized (detectedKeyEvents) {
147             pressedKeysToPressedTimeMap.remove(evt.getKeyCode());
148             detectedKeyEvents.add(evt);
149             viewPanel.repaintDuringNextViewUpdate();
150         }
151     }
152
153     @Override
154     public void keyTyped(final KeyEvent e) {
155     }
156
157     @Override
158     public void mouseClicked(final MouseEvent e) {
159         synchronized (detectedMouseClicks) {
160             detectedMouseClicks.add(new MouseClick(e.getX(), e.getY(), e.getButton()));
161         }
162     }
163
164     @Override
165     public void mouseDragged(final java.awt.event.MouseEvent evt) {
166         final Point2D mouseLocation = new Point2D(evt.getX(), evt.getY());
167
168         if (oldMouseCoordinatesWhenDragging == null) {
169             oldMouseCoordinatesWhenDragging = mouseLocation;
170             return;
171         }
172
173         mouseDraggedDirection.add(mouseLocation.clone().subtract(oldMouseCoordinatesWhenDragging));
174
175         oldMouseCoordinatesWhenDragging = mouseLocation;
176     }
177
178     @Override
179     public void mouseEntered(final MouseEvent e) {
180         mouseWithinWindow = true;
181     }
182
183     @Override
184     public synchronized void mouseExited(final MouseEvent e) {
185         mouseWithinWindow = false;
186         currentMouseLocation = null;
187     }
188
189     @Override
190     public synchronized void mouseMoved(final MouseEvent e) {
191         currentMouseLocation = new Point2D(e.getX(), e.getY());
192         mouseMoved = true;
193     }
194
195     @Override
196     public void mousePressed(final MouseEvent e) {
197     }
198
199     @Override
200     public void mouseReleased(final java.awt.event.MouseEvent evt) {
201         oldMouseCoordinatesWhenDragging = null;
202     }
203
204     @Override
205     public void mouseWheelMoved(final java.awt.event.MouseWheelEvent evt) {
206         wheelMovedDirection += evt.getWheelRotation();
207     }
208
209     /**
210      * Interpret mouse movement
211      */
212     private boolean trackMouse() {
213         final Avatar avatar = viewPanel.getAvatar();
214         trackDragging(avatar);
215         trackVerticalScrolling(avatar);
216
217         boolean repaintNeeded = !mouseDraggedDirection.isZero() || (wheelMovedDirection != 0);
218
219         // reset movement counters
220         wheelMovedDirection = 0;
221         mouseDraggedDirection.zero();
222
223         return repaintNeeded;
224     }
225
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();
230     }
231
232     private void trackDragging(Avatar avatar) {
233         // TODO: need to detect whether user moved mouse or touch screen
234
235         // for mouse
236         avatar.setAngleXZ(avatar.getAngleXZ() - ((float) mouseDraggedDirection.x / 50));
237         avatar.setAngleYZ(avatar.getAngleYZ() - ((float) mouseDraggedDirection.y / 50));
238
239         // for touch screen
240         // avatar.setAngleXZ(avatar.getAngleXZ() + ((float)
241         // mouseDraggedDirection.x / 50));
242         // avatar.setAngleYZ(avatar.getAngleYZ() + ((float)
243         // mouseDraggedDirection.y / 50));
244     }
245
246 }