Fixed in room navigation.
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Sun, 22 Jul 2018 21:10:17 +0000 (00:10 +0300)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Sun, 22 Jul 2018 21:10:17 +0000 (00:10 +0300)
src/main/java/eu/svjatoslav/sixth/e3d/gui/ViewPanel.java
src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/HIDInputTracker.java
src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/WorldNavigationUserInputTracker.java

index b23bf5b..ea55a31 100755 (executable)
@@ -9,18 +9,18 @@
 
 package eu.svjatoslav.sixth.e3d.gui;
 
+import eu.svjatoslav.sixth.e3d.gui.humaninput.HIDInputTracker;
 import eu.svjatoslav.sixth.e3d.gui.humaninput.KeyboardFocusStack;
 import eu.svjatoslav.sixth.e3d.gui.humaninput.MouseInteractionController;
-import eu.svjatoslav.sixth.e3d.gui.humaninput.HIDInputTracker;
 import eu.svjatoslav.sixth.e3d.renderer.raster.ShapeCollection;
 
 import javax.swing.*;
 import java.awt.*;
 import java.awt.event.ComponentEvent;
 import java.awt.event.ComponentListener;
-import java.util.ArrayList;
-import java.util.List;
+import java.util.Set;
 import java.util.Timer;
+import java.util.concurrent.ConcurrentHashMap;
 
 /**
  * Java Swing GUI panel that contains canvas for 3D rendering.
@@ -29,10 +29,10 @@ public class ViewPanel extends JPanel implements ComponentListener {
     private static final long serialVersionUID = 1683277888885045387L;
     public Color backgroundColor = Color.BLACK;
     private final HIDInputTracker HIDInputTracker = new HIDInputTracker(this);
-    private final KeyboardFocusStack keyboardFocusStack = new KeyboardFocusStack(this);
+    private final KeyboardFocusStack keyboardFocusStack;
     private final Avatar avatar = new Avatar();
     private final ShapeCollection rootShapeCollection = new ShapeCollection();
-    private final List<ViewRenderListener> viewRenderListeners = new ArrayList<>();
+    private final Set<ViewRenderListener> viewRenderListeners = ConcurrentHashMap.newKeySet();
     /**
      * Last time this view was updated.
      */
@@ -58,6 +58,8 @@ public class ViewPanel extends JPanel implements ComponentListener {
         viewRenderListeners.add(avatar);
         viewRenderListeners.add(HIDInputTracker);
 
+        keyboardFocusStack = new KeyboardFocusStack(this);
+
         initializePanelLayout();
 
         setFrameRate(targetFPS);
@@ -274,4 +276,13 @@ public class ViewPanel extends JPanel implements ComponentListener {
         lastUpdateMillis = currentTime;
         return millisecondsPassedSinceLastUpdate;
     }
+
+    public void addViewRenderListener(ViewRenderListener viewRenderListener) {
+        viewRenderListeners.add(viewRenderListener);
+    }
+
+    public void removeViewRenderListener(ViewRenderListener viewRenderListener) {
+        viewRenderListeners.remove(viewRenderListener);
+    }
+
 }
index 6015ee7..ee625d5 100755 (executable)
@@ -11,6 +11,7 @@ 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.RenderingContext;
 import eu.svjatoslav.sixth.e3d.gui.ViewPanel;
 import eu.svjatoslav.sixth.e3d.gui.ViewRenderListener;
 
@@ -136,9 +137,12 @@ public class HIDInputTracker implements
     }
 
     private boolean handleMouseHovering(ViewPanel viewPanel) {
-        if (currentMouseLocation != null)
+        if (currentMouseLocation != null) {
+            RenderingContext renderingContext = viewPanel.getRenderingContext();
+            if (renderingContext != null)
+               renderingContext.mouseClick = new MouseClick(currentMouseLocation, 0);
             // mouse click with button 0 amounts to mouse hovering event
-            viewPanel.getRenderingContext().mouseClick = new MouseClick(currentMouseLocation, 0);
+        }
 
         if (mouseMoved) {
             mouseMoved = false;
index d80924c..de51857 100644 (file)
@@ -21,17 +21,40 @@ public class WorldNavigationUserInputTracker implements UserInputHandler {
     public boolean beforeRender(final ViewPanel viewPanel,
                                 final int millisecondsSinceLastFrame) {
 
-        trackKeys(millisecondsSinceLastFrame, viewPanel);
+        final HIDInputTracker inputTracker = viewPanel.getHIDInputTracker();
+
+        final Avatar avatar = viewPanel.getAvatar();
+
+        final double actualAcceleration = (long) millisecondsSinceLastFrame
+                * avatar.avatarAcceleration
+                * (1 + (avatar.getMovementSpeed() / 10));
+
+        if (inputTracker.isKeyPressed(KeyboardHelper.UP))
+            avatar.getMovementVector().z += actualAcceleration;
+
+        if (inputTracker.isKeyPressed(KeyboardHelper.DOWN))
+            avatar.getMovementVector().z -= actualAcceleration;
+
+        if (inputTracker.isKeyPressed(KeyboardHelper.RIGHT))
+            avatar.getMovementVector().x += actualAcceleration;
+
+        if (inputTracker.isKeyPressed(KeyboardHelper.LEFT))
+            avatar.getMovementVector().x -= actualAcceleration;
+
+        avatar.enforceSpeedLimit();
+
         return false;
     }
 
     @Override
     public boolean focusLost(final ViewPanel viewPanel) {
+        viewPanel.removeViewRenderListener(this);
         return false;
     }
 
     @Override
-    public boolean focusReceived(final ViewPanel viewContext) {
+    public boolean focusReceived(final ViewPanel viewPanel) {
+        viewPanel.addViewRenderListener(this);
         return false;
     }
 
@@ -45,34 +68,4 @@ public class WorldNavigationUserInputTracker implements UserInputHandler {
         return false;
     }
 
-    /**
-     * interpret currently pressed keys
-     */
-    private void trackKeys(final long millisecondsSinceLastFrame,
-                           final ViewPanel viewPanel) {
-
-        System.out.println("Track keys!");
-
-        final HIDInputTracker inputTracker = viewPanel.getHIDInputTracker();
-
-        final Avatar avatar = viewPanel.getAvatar();
-
-        final double actualAcceleration = millisecondsSinceLastFrame
-                * avatar.avatarAcceleration
-                * (1 + (avatar.getMovementSpeed() / 10));
-
-        if (inputTracker.isKeyPressed(KeyboardHelper.UP))
-            avatar.getMovementVector().z += actualAcceleration;
-
-        if (inputTracker.isKeyPressed(KeyboardHelper.DOWN))
-            avatar.getMovementVector().z -= actualAcceleration;
-
-        if (inputTracker.isKeyPressed(KeyboardHelper.RIGHT))
-            avatar.getMovementVector().x += actualAcceleration;
-
-        if (inputTracker.isKeyPressed(KeyboardHelper.LEFT))
-            avatar.getMovementVector().x -= actualAcceleration;
-
-        avatar.enforceSpeedLimit();
-    }
 }