X-Git-Url: http://www2.svjatoslav.eu/gitweb/?p=sixth-3d.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Feu%2Fsvjatoslav%2Fsixth%2Fe3d%2Fgui%2Fhumaninput%2FUserInputTracker.java;h=105a5824539fda115773f6fed8f997538193bf0a;hp=dedfd7820984668927af1f8c2219ef64ffb2fb0b;hb=baab2e2c2ad89695293f3136311c585c9a5afed1;hpb=2e7e46514dd35006e9dde07b1959540078292691 diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/UserInputTracker.java b/src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/UserInputTracker.java index dedfd78..105a582 100755 --- a/src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/UserInputTracker.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/UserInputTracker.java @@ -13,7 +13,7 @@ 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.ViewContext; -import eu.svjatoslav.sixth.e3d.gui.ViewUpdateListener; +import eu.svjatoslav.sixth.e3d.gui.ViewRenderListener; import javax.swing.*; import java.awt.event.*; @@ -23,7 +23,7 @@ import java.util.List; import java.util.Map; public class UserInputTracker - implements MouseMotionListener, KeyListener, MouseListener, MouseWheelListener, ViewUpdateListener { + implements MouseMotionListener, KeyListener, MouseListener, MouseWheelListener, ViewRenderListener { /** *
@@ -36,12 +36,12 @@ public class UserInputTracker
     private final Map pressedKeysToPressedTimeMap = new HashMap<>();
     private final List detectedMouseClicks = new ArrayList<>();
     private final List 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) {
@@ -52,14 +52,15 @@ public class UserInputTracker
      * {@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());
 
         viewUpdateNeeded |= handleDetectedKeyEvents();
 
-        viewContext.getKeyboardFocusTracker().getCurrentFocusOwner().beforeViewUpdate(viewContext,
+        viewContext.getKeyboardFocusTracker().getCurrentFocusOwner().beforeRender(viewContext,
                 millisecondsSinceLastFrame);
+
         viewUpdateNeeded |= trackMouse();
 
         return viewUpdateNeeded;
@@ -75,7 +76,7 @@ public class UserInputTracker
         panel.addMouseWheelListener(this);
     }
 
-    public boolean handleDetectedKeyEvents() {
+    private boolean handleDetectedKeyEvents() {
         boolean keyEventsHandled = false;
 
         final UserInputHandler currentFocusOwner = viewContext.getKeyboardFocusTracker().getCurrentFocusOwner();
@@ -107,10 +108,9 @@ public class UserInputTracker
     }
 
     /**
-     * Returns true if mouse events are detected and view needs to
-     * be repainted.
+     * @return true if view needs to be repainted.
      */
-    public synchronized boolean handleDetectedMouseClicks(final View view) {
+    private synchronized boolean handleDetectedMouseClicks(final View view) {
         if (detectedMouseClicks.isEmpty()) {
 
             if (currentMouseLocation != null)
@@ -128,7 +128,7 @@ public class UserInputTracker
         return true;
     }
 
-    public boolean isKeyPressed(final int keyCode) {
+    boolean isKeyPressed(final int keyCode) {
         return pressedKeysToPressedTimeMap.containsKey(keyCode);
     }
 
@@ -209,11 +209,27 @@ public class UserInputTracker
     /**
      * Interpret mouse movement
      */
-    public boolean trackMouse() {
+    private boolean trackMouse() {
         final Avatar avatar = viewContext.getAvatar();
+        trackDragging(avatar);
+        trackVerticalScrolling(avatar);
+
+        boolean repaintNeeded = !mouseDraggedDirection.isZero() || (wheelMovedDirection != 0);
 
-        // track mouse dragging
+        // reset movement counters
+        wheelMovedDirection = 0;
+        mouseDraggedDirection.zero();
+
+        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
@@ -225,22 +241,6 @@ public class UserInputTracker
         // 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.getMovementVector().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;
     }
 
 }