X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2Fmain%2Fjava%2Feu%2Fsvjatoslav%2Fsixth%2Fe3d%2Fgui%2Fhumaninput%2FUserInputTracker.java;h=1dd6f7a3f54a1a9349ea68dde8e86277e0125c65;hb=96f76bab30b47db83a1c9061b8916c69657f1e13;hp=dedfd7820984668927af1f8c2219ef64ffb2fb0b;hpb=2e7e46514dd35006e9dde07b1959540078292691;p=sixth-3d.git 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..1dd6f7a 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 @@ -11,9 +11,9 @@ package eu.svjatoslav.sixth.e3d.gui.humaninput; 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.*; @@ -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 MappressedKeysToPressedTimeMap = 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()); + boolean viewUpdateNeeded = handleDetectedMouseClicks(viewContext.getViewPanel()); 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,14 +108,13 @@ public class UserInputTracker } /** - * Returns true
if mouse events are detected and view needs to - * be repainted. + * @returntrue
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; @@ -123,12 +123,12 @@ public class UserInputTracker 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); } @@ -137,7 +137,7 @@ public class UserInputTracker synchronized (detectedKeyEvents) { pressedKeysToPressedTimeMap.put(evt.getKeyCode(), System.currentTimeMillis()); detectedKeyEvents.add(evt); - viewContext.getView().repaintDuringNextViewUpdate(); + viewContext.getViewPanel().repaintDuringNextViewUpdate(); } } @@ -146,7 +146,7 @@ public class UserInputTracker synchronized (detectedKeyEvents) { pressedKeysToPressedTimeMap.remove(evt.getKeyCode()); detectedKeyEvents.add(evt); - viewContext.getView().repaintDuringNextViewUpdate(); + viewContext.getViewPanel().repaintDuringNextViewUpdate(); } } @@ -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; } }