/*
- * Sixth 3D engine. Copyright ©2012-2017, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
+ * Sixth 3D engine. Copyright ©2012-2018, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 3 of the GNU Lesser General Public License
import eu.svjatoslav.sixth.e3d.geometry.Point2D;
import eu.svjatoslav.sixth.e3d.gui.Avatar;
-import eu.svjatoslav.sixth.e3d.gui.View;
+import eu.svjatoslav.sixth.e3d.gui.ViewPanel;
import eu.svjatoslav.sixth.e3d.gui.ViewContext;
-import eu.svjatoslav.sixth.e3d.gui.ViewUpdateListener;
+import eu.svjatoslav.sixth.e3d.gui.ViewRenderListener;
import javax.swing.*;
import java.awt.event.*;
import java.util.Map;
public class UserInputTracker
- implements MouseMotionListener, KeyListener, MouseListener, MouseWheelListener, ViewUpdateListener {
+ implements MouseMotionListener, KeyListener, MouseListener, MouseWheelListener, ViewRenderListener {
/**
* <pre>
private final Map<Integer, Long> pressedKeysToPressedTimeMap = new HashMap<>();
private final List<MouseClick> detectedMouseClicks = new ArrayList<>();
private final List<KeyEvent> detectedKeyEvents = new ArrayList<>();
- public int wheelMovedDirection = 0;
- Point2D mouseDraggedDirection = new Point2D();
- Point2D oldMouseCoordinatesWhenDragging;
- ViewContext viewContext;
- Point2D currentMouseLocation;
- boolean mouseMoved;
+ private int wheelMovedDirection = 0;
+ private Point2D mouseDraggedDirection = new Point2D();
+ private Point2D oldMouseCoordinatesWhenDragging;
+ private ViewContext viewContext;
+ private Point2D currentMouseLocation;
+ private boolean mouseMoved;
private boolean mouseWithinWindow = false;
public UserInputTracker(final ViewContext viewContext) {
* {@inheritDoc}
*/
@Override
- public boolean beforeViewUpdate(final ViewContext viewContext, final int millisecondsSinceLastFrame) {
+ public boolean beforeRender(final ViewContext viewContext, final int millisecondsSinceLastFrame) {
- boolean viewUpdateNeeded = handleDetectedMouseClicks(viewContext.getView());
+ boolean viewUpdateNeeded = handleDetectedMouseClicks(viewContext.getViewPanel());
viewUpdateNeeded |= handleDetectedKeyEvents();
- viewContext.getKeyboardFocusTracker().getCurrentFocusOwner().beforeViewUpdate(viewContext,
+ viewContext.getKeyboardFocusTracker().getCurrentFocusOwner().beforeRender(viewContext,
millisecondsSinceLastFrame);
+
viewUpdateNeeded |= trackMouse();
return viewUpdateNeeded;
panel.addMouseWheelListener(this);
}
- public boolean handleDetectedKeyEvents() {
+ private boolean handleDetectedKeyEvents() {
boolean keyEventsHandled = false;
final UserInputHandler currentFocusOwner = viewContext.getKeyboardFocusTracker().getCurrentFocusOwner();
}
/**
- * Returns <code>true</code> if mouse events are detected and view needs to
- * be repainted.
+ * @return <code>true</code> if view needs to be repainted.
*/
- public synchronized boolean handleDetectedMouseClicks(final View view) {
+ private synchronized boolean handleDetectedMouseClicks(final ViewPanel viewPanel) {
if (detectedMouseClicks.isEmpty()) {
if (currentMouseLocation != null)
- view.getRenderingContext().mouseClick = new MouseClick(currentMouseLocation, 0);
+ viewPanel.getRenderingContext().mouseClick = new MouseClick(currentMouseLocation, 0);
if (mouseMoved) {
mouseMoved = false;
return false;
}
- view.getRenderingContext().mouseClick = detectedMouseClicks.remove(0);
+ viewPanel.getRenderingContext().mouseClick = detectedMouseClicks.remove(0);
return true;
}
- public boolean isKeyPressed(final int keyCode) {
+ boolean isKeyPressed(final int keyCode) {
return pressedKeysToPressedTimeMap.containsKey(keyCode);
}
synchronized (detectedKeyEvents) {
pressedKeysToPressedTimeMap.put(evt.getKeyCode(), System.currentTimeMillis());
detectedKeyEvents.add(evt);
- viewContext.getView().repaintDuringNextViewUpdate();
+ viewContext.getViewPanel().repaintDuringNextViewUpdate();
}
}
synchronized (detectedKeyEvents) {
pressedKeysToPressedTimeMap.remove(evt.getKeyCode());
detectedKeyEvents.add(evt);
- viewContext.getView().repaintDuringNextViewUpdate();
+ viewContext.getViewPanel().repaintDuringNextViewUpdate();
}
}
/**
* Interpret mouse movement
*/
- public boolean trackMouse() {
+ private boolean trackMouse() {
final Avatar avatar = viewContext.getAvatar();
+ trackDragging(avatar);
+ trackVerticalScrolling(avatar);
+
+ boolean repaintNeeded = !mouseDraggedDirection.isZero() || (wheelMovedDirection != 0);
+
+ // reset movement counters
+ wheelMovedDirection = 0;
+ mouseDraggedDirection.zero();
- // track mouse dragging
+ return repaintNeeded;
+ }
+
+ private void trackVerticalScrolling(Avatar avatar) {
+ final double actualAcceleration = 50 * avatar.avatarAcceleration * (1 + (avatar.getMovementSpeed() / 10));
+ avatar.getMovementVector().y += (wheelMovedDirection * actualAcceleration);
+ avatar.enforceSpeedLimit();
+ }
+ private void trackDragging(Avatar avatar) {
// TODO: need to detect whether user moved mouse or touch screen
// for mouse
// mouseDraggedDirection.x / 50));
// avatar.setAngleYZ(avatar.getAngleYZ() + ((float)
// mouseDraggedDirection.y / 50));
-
- // track mouse wheel movements
- final double actualAcceleration = 50 * avatar.avatarAcceleration * (1 + (avatar.getMovementSpeed() / 10));
-
- avatar.getMovementDirection().y += (wheelMovedDirection * actualAcceleration);
- avatar.enforceSpeedLimit();
-
- // check if view shall be repainted
- boolean repaintNeeded;
- repaintNeeded = (mouseDraggedDirection.x != 0) || (mouseDraggedDirection.y != 0) || (wheelMovedDirection != 0);
-
- // reset movement counters
- wheelMovedDirection = 0;
- mouseDraggedDirection.zero();
-
- return repaintNeeded;
}
}