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.
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.
*/
viewRenderListeners.add(avatar);
viewRenderListeners.add(HIDInputTracker);
+ keyboardFocusStack = new KeyboardFocusStack(this);
+
initializePanelLayout();
setFrameRate(targetFPS);
lastUpdateMillis = currentTime;
return millisecondsPassedSinceLastUpdate;
}
+
+ public void addViewRenderListener(ViewRenderListener viewRenderListener) {
+ viewRenderListeners.add(viewRenderListener);
+ }
+
+ public void removeViewRenderListener(ViewRenderListener viewRenderListener) {
+ viewRenderListeners.remove(viewRenderListener);
+ }
+
}
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;
}
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;
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;
}
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();
- }
}