From 70ee733b25c56bed539b89ff5507ae0af842d68a Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Sun, 22 Jul 2018 23:36:06 +0300 Subject: [PATCH] Code refactoring. --- .../eu/svjatoslav/sixth/e3d/gui/Avatar.java | 4 +- .../sixth/e3d/gui/GuiComponent.java | 4 +- .../svjatoslav/sixth/e3d/gui/ViewFrame.java | 2 +- .../svjatoslav/sixth/e3d/gui/ViewPanel.java | 99 ++++++++++--------- ...InputTracker.java => HIDInputTracker.java} | 12 +-- ...usTracker.java => KeyboardFocusStack.java} | 24 ++--- ...a => WorldNavigationUserInputTracker.java} | 16 +-- 7 files changed, 80 insertions(+), 81 deletions(-) rename src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/{UserInputTracker.java => HIDInputTracker.java} (96%) rename src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/{KeyboardFocusTracker.java => KeyboardFocusStack.java} (65%) rename src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/{WorldNavigationTracker.java => WorldNavigationUserInputTracker.java} (82%) diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/gui/Avatar.java b/src/main/java/eu/svjatoslav/sixth/e3d/gui/Avatar.java index 52d78b2..852dc97 100755 --- a/src/main/java/eu/svjatoslav/sixth/e3d/gui/Avatar.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/gui/Avatar.java @@ -70,7 +70,7 @@ public class Avatar implements ViewRenderListener { public boolean beforeRender(final ViewPanel viewPanel, final int millisecondsSinceLastFrame) { final Point3D locationBeforeUpdate = new Point3D(location); - translateAvatarLocation(millisecondsSinceLastFrame); + translateAvatarLocationBasedOnMovementVector(millisecondsSinceLastFrame); applyFrictionToUserMovement(millisecondsSinceLastFrame); return isFrameRepaintNeeded(locationBeforeUpdate); } @@ -139,7 +139,7 @@ public class Avatar implements ViewRenderListener { * Therefore we take frame rendering time into account when translating * avatar between consecutive frames. */ - private void translateAvatarLocation(int millisecondsPassedSinceLastFrame) { + private void translateAvatarLocationBasedOnMovementVector(int millisecondsPassedSinceLastFrame) { location.x -= (float) sin(getAngleXZ()) * getMovementVector().z * SPEED_MULTIPLIER * millisecondsPassedSinceLastFrame; diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/gui/GuiComponent.java b/src/main/java/eu/svjatoslav/sixth/e3d/gui/GuiComponent.java index 75dbbf4..70350e0 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/gui/GuiComponent.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/gui/GuiComponent.java @@ -95,7 +95,7 @@ public class GuiComponent extends AbstractCompositeShape implements @Override public boolean keyPressed(final KeyEvent event, final ViewPanel viewPanel) { if (event.getKeyChar() == KeyboardHelper.ESC) - viewPanel.getKeyboardFocusTracker().popFocusOwner(); + viewPanel.getKeyboardFocusStack().popFocusOwner(); return true; } @@ -106,7 +106,7 @@ public class GuiComponent extends AbstractCompositeShape implements @Override public void mouseClicked() { - viewPanel.getKeyboardFocusTracker().setFocusOwner(this); + viewPanel.getKeyboardFocusStack().pushFocusOwner(this); } @Override diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/gui/ViewFrame.java b/src/main/java/eu/svjatoslav/sixth/e3d/gui/ViewFrame.java index 874673f..1383c72 100755 --- a/src/main/java/eu/svjatoslav/sixth/e3d/gui/ViewFrame.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/gui/ViewFrame.java @@ -46,7 +46,7 @@ public class ViewFrame extends JFrame implements WindowListener { addWindowListener(this); } - public void addResizeListener() { + private void addResizeListener() { addComponentListener(new ComponentListener() { // This method is called after the component's size changes @Override diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/gui/ViewPanel.java b/src/main/java/eu/svjatoslav/sixth/e3d/gui/ViewPanel.java index 6fda153..b23bf5b 100755 --- a/src/main/java/eu/svjatoslav/sixth/e3d/gui/ViewPanel.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/gui/ViewPanel.java @@ -9,9 +9,9 @@ package eu.svjatoslav.sixth.e3d.gui; -import eu.svjatoslav.sixth.e3d.gui.humaninput.KeyboardFocusTracker; +import eu.svjatoslav.sixth.e3d.gui.humaninput.KeyboardFocusStack; import eu.svjatoslav.sixth.e3d.gui.humaninput.MouseInteractionController; -import eu.svjatoslav.sixth.e3d.gui.humaninput.UserInputTracker; +import eu.svjatoslav.sixth.e3d.gui.humaninput.HIDInputTracker; import eu.svjatoslav.sixth.e3d.renderer.raster.ShapeCollection; import javax.swing.*; @@ -27,9 +27,9 @@ import java.util.Timer; */ public class ViewPanel extends JPanel implements ComponentListener { private static final long serialVersionUID = 1683277888885045387L; - private final UserInputTracker userInputTracker = new UserInputTracker(this); - private final KeyboardFocusTracker keyboardFocusTracker = new KeyboardFocusTracker( - this); + public Color backgroundColor = Color.BLACK; + private final HIDInputTracker HIDInputTracker = new HIDInputTracker(this); + private final KeyboardFocusStack keyboardFocusStack = new KeyboardFocusStack(this); private final Avatar avatar = new Avatar(); private final ShapeCollection rootShapeCollection = new ShapeCollection(); private final List viewRenderListeners = new ArrayList<>(); @@ -56,7 +56,7 @@ public class ViewPanel extends JPanel implements ComponentListener { private boolean viewRepaintNeeded = true; public ViewPanel() { viewRenderListeners.add(avatar); - viewRenderListeners.add(userInputTracker); + viewRenderListeners.add(HIDInputTracker); initializePanelLayout(); @@ -69,16 +69,16 @@ public class ViewPanel extends JPanel implements ComponentListener { return avatar; } - public KeyboardFocusTracker getKeyboardFocusTracker() { - return keyboardFocusTracker; + public KeyboardFocusStack getKeyboardFocusStack() { + return keyboardFocusStack; } public ShapeCollection getRootShapeCollection() { return rootShapeCollection; } - public UserInputTracker getUserInputTracker() { - return userInputTracker; + public HIDInputTracker getHIDInputTracker() { + return HIDInputTracker; } public void addViewUpdateListener(final ViewRenderListener listener) { @@ -120,10 +120,6 @@ public class ViewPanel extends JPanel implements ComponentListener { return new java.awt.Dimension(640, 480); } - public RenderingContext getRenderBuffer() { - return renderingContext; - } - public RenderingContext getRenderingContext() { return renderingContext; } @@ -163,29 +159,31 @@ public class ViewPanel extends JPanel implements ComponentListener { requestFocusInWindow(); } - public void renderFrame() { - // build new render buffer if needed, this happens when window was just - // created or resized - if ((renderingContext == null) - || (renderingContext.width != getWidth()) - || (renderingContext.height != getHeight())) + private void renderFrame() { + if (isNewRenderingContextNeeded()) renderingContext = new RenderingContext(getWidth(), getHeight()); - // clear drawing area - { - renderingContext.graphics.setColor(Color.BLACK); - renderingContext.graphics.fillRect(0, 0, getWidth(), getHeight()); - } - // paint root geometry collection to the offscreen render buffer + clearCanvas(); rootShapeCollection.paint(this, renderingContext); - // draw rendered offscreen image to visible screen + // draw rendered offscreen buffer to visible screen final Graphics graphics = getGraphics(); if (graphics != null) graphics.drawImage(renderingContext.bufferedImage, 0, 0, null); } + private void clearCanvas() { + renderingContext.graphics.setColor(backgroundColor); + renderingContext.graphics.fillRect(0, 0, getWidth(), getHeight()); + } + + private boolean isNewRenderingContextNeeded() { + return (renderingContext == null) + || (renderingContext.width != getWidth()) + || (renderingContext.height != getHeight()); + } + /** * Calling this methods tells 3D engine that current 3D view needs to be * repainted on first opportunity. @@ -233,42 +231,47 @@ public class ViewPanel extends JPanel implements ComponentListener { * It tells view to update itself. View can decide if actual re-rendering of * graphics is needed. */ - public void updateView() { + void updateView() { if (renderingContext != null) { renderingContext.mouseClick = null; renderingContext.clickedItem = null; } - // compute time passed since last view update - final long currentTime = System.currentTimeMillis(); + final int millisecondsPassedSinceLastUpdate = getMillisecondsPassedSinceLastUpdate(); - if (lastUpdateMillis == 0) { - lastUpdateMillis = currentTime; - return; + boolean renderFrame = notifyViewRenderListeners(millisecondsPassedSinceLastUpdate); + + if (viewRepaintNeeded) { + viewRepaintNeeded = false; + renderFrame = true; } - final int millisecondsPassedSinceLastUpdate = (int) (currentTime - lastUpdateMillis); - lastUpdateMillis = currentTime; + // abort rendering if window size is invalid + if ((getWidth() <= 0) || (getHeight() <= 0)) + renderFrame = false; - // notify update listeners - boolean reRenderFrame = false; + if (renderFrame) { + renderFrame(); + handleDetectedComponentMouseEvents(); + } + } + private boolean notifyViewRenderListeners(int millisecondsPassedSinceLastUpdate) { + boolean reRenderFrame = false; for (final ViewRenderListener listener : viewRenderListeners) if (listener.beforeRender(this, millisecondsPassedSinceLastUpdate)) reRenderFrame = true; + return reRenderFrame; + } - // abort rendering if window size is invalid - if ((getWidth() <= 0) || (getHeight() <= 0)) - return; + private int getMillisecondsPassedSinceLastUpdate() { + final long currentTime = System.currentTimeMillis(); - if (viewRepaintNeeded) { - viewRepaintNeeded = false; - reRenderFrame = true; - } + if (lastUpdateMillis == 0) + lastUpdateMillis = currentTime; - if (reRenderFrame) { - renderFrame(); - handleDetectedComponentMouseEvents(); - } + final int millisecondsPassedSinceLastUpdate = (int) (currentTime - lastUpdateMillis); + lastUpdateMillis = currentTime; + return millisecondsPassedSinceLastUpdate; } } diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/UserInputTracker.java b/src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/HIDInputTracker.java similarity index 96% rename from src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/UserInputTracker.java rename to src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/HIDInputTracker.java index 3060691..6015ee7 100755 --- a/src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/UserInputTracker.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/HIDInputTracker.java @@ -21,7 +21,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -public class UserInputTracker implements +public class HIDInputTracker implements MouseMotionListener, KeyListener, MouseListener, MouseWheelListener, ViewRenderListener { /** @@ -43,7 +43,7 @@ public class UserInputTracker implements private boolean mouseMoved; private boolean mouseWithinWindow = false; - public UserInputTracker(final ViewPanel viewPanel) { + public HIDInputTracker(final ViewPanel viewPanel) { this.viewPanel = viewPanel; bind(viewPanel); } @@ -53,12 +53,10 @@ public class UserInputTracker implements */ @Override public boolean beforeRender(final ViewPanel viewPanel, final int millisecondsSinceLastFrame) { - boolean viewUpdateNeeded = handleKeyboardEvents(); viewUpdateNeeded |= handleMouseClicksAndHover(viewPanel); viewUpdateNeeded |= handleMouseDragging(); viewUpdateNeeded |= handleMouseVerticalScrolling(); - return viewUpdateNeeded; } @@ -76,11 +74,11 @@ public class UserInputTracker implements * @return true if view needs to be repainted. */ private boolean handleKeyboardEvents() { - final UserInputHandler currentFocusOwner = viewPanel.getKeyboardFocusTracker().getCurrentFocusOwner(); + final UserInputHandler currentFocusOwner = viewPanel.getKeyboardFocusStack().getCurrentFocusOwner(); ArrayList unprocessedKeyboardEvents = getUnprocessedKeyboardEvents(); - return currentFocusOwner == null ? false : - forwardKeyboardEventsToFocusOwner(currentFocusOwner, unprocessedKeyboardEvents); + return currentFocusOwner != null + && forwardKeyboardEventsToFocusOwner(currentFocusOwner, unprocessedKeyboardEvents); } private ArrayList getUnprocessedKeyboardEvents() { diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/KeyboardFocusTracker.java b/src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/KeyboardFocusStack.java similarity index 65% rename from src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/KeyboardFocusTracker.java rename to src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/KeyboardFocusStack.java index 65e9374..4ee7ed8 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/KeyboardFocusTracker.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/KeyboardFocusStack.java @@ -13,16 +13,16 @@ import eu.svjatoslav.sixth.e3d.gui.ViewPanel; import java.util.Stack; -public class KeyboardFocusTracker { +public class KeyboardFocusStack { private final ViewPanel viewPanel; - WorldNavigationTracker defaultInputHandler = new WorldNavigationTracker(); - Stack inputHandlers = new Stack<>(); + private WorldNavigationUserInputTracker defaultInputHandler = new WorldNavigationUserInputTracker(); + private Stack inputHandlers = new Stack<>(); private UserInputHandler currentUserInputHandler; - public KeyboardFocusTracker(final ViewPanel viewPanel) { + public KeyboardFocusStack(final ViewPanel viewPanel) { this.viewPanel = viewPanel; - setFocusOwner(defaultInputHandler); + pushFocusOwner(defaultInputHandler); } public UserInputHandler getCurrentFocusOwner() { @@ -30,21 +30,18 @@ public class KeyboardFocusTracker { } public void popFocusOwner() { - if (currentUserInputHandler == null) - return; + if (currentUserInputHandler != null) + currentUserInputHandler.focusLost(viewPanel); if (inputHandlers.isEmpty()) return; - currentUserInputHandler.focusLost(viewPanel); - currentUserInputHandler = inputHandlers.pop(); - currentUserInputHandler.focusReceived(viewPanel); } - public void setFocusOwner(final UserInputHandler inputHandler) { - if (currentUserInputHandler == inputHandler) + public void pushFocusOwner(final UserInputHandler newInputHandler) { + if (currentUserInputHandler == newInputHandler) return; if (currentUserInputHandler != null) { @@ -52,8 +49,7 @@ public class KeyboardFocusTracker { inputHandlers.push(currentUserInputHandler); } - currentUserInputHandler = inputHandler; - + currentUserInputHandler = newInputHandler; currentUserInputHandler.focusReceived(viewPanel); } } diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/WorldNavigationTracker.java b/src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/WorldNavigationUserInputTracker.java similarity index 82% rename from src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/WorldNavigationTracker.java rename to src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/WorldNavigationUserInputTracker.java index 28d485c..d80924c 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/WorldNavigationTracker.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/WorldNavigationUserInputTracker.java @@ -15,7 +15,7 @@ import eu.svjatoslav.sixth.e3d.gui.textEditorComponent.KeyboardHelper; import java.awt.event.KeyEvent; -public class WorldNavigationTracker implements UserInputHandler { +public class WorldNavigationUserInputTracker implements UserInputHandler { @Override public boolean beforeRender(final ViewPanel viewPanel, @@ -37,23 +37,25 @@ public class WorldNavigationTracker implements UserInputHandler { @Override public boolean keyPressed(final KeyEvent event, final ViewPanel viewContext) { - return true; + return false; } @Override public boolean keyReleased(final KeyEvent event, final ViewPanel viewContext) { - return true; + return false; } /** * interpret currently pressed keys */ - public void trackKeys(final long millisecondsSinceLastFrame, - final ViewPanel viewContext) { + private void trackKeys(final long millisecondsSinceLastFrame, + final ViewPanel viewPanel) { + + System.out.println("Track keys!"); - final UserInputTracker inputTracker = viewContext.getUserInputTracker(); + final HIDInputTracker inputTracker = viewPanel.getHIDInputTracker(); - final Avatar avatar = viewContext.getAvatar(); + final Avatar avatar = viewPanel.getAvatar(); final double actualAcceleration = millisecondsSinceLastFrame * avatar.avatarAcceleration -- 2.20.1