From e87345f40ecb4ea526bd8d5dca58afaaee9ec6b3 Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Sat, 29 Apr 2023 19:00:33 +0300 Subject: [PATCH] Improved code readability --- .../svjatoslav/sixth/e3d/gui/ViewFrame.java | 3 ++ .../svjatoslav/sixth/e3d/gui/ViewPanel.java | 42 +++++++++---------- .../{io => gui/humaninput}/Connexion3D.java | 13 +++--- ...InputTracker.java => HIDEventTracker.java} | 11 ++++- .../WorldNavigationUserInputTracker.java | 2 +- .../e3d/gui/humaninput/package-info.java | 6 +++ .../gui/textEditorComponent/package-info.java | 7 ++++ 7 files changed, 54 insertions(+), 30 deletions(-) rename src/main/java/eu/svjatoslav/sixth/e3d/{io => gui/humaninput}/Connexion3D.java (61%) rename src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/{HIDInputTracker.java => HIDEventTracker.java} (97%) create mode 100644 src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/package-info.java create mode 100644 src/main/java/eu/svjatoslav/sixth/e3d/gui/textEditorComponent/package-info.java 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 0c6ef51..bbae44c 100755 --- a/src/main/java/eu/svjatoslav/sixth/e3d/gui/ViewFrame.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/gui/ViewFrame.java @@ -85,6 +85,9 @@ public class ViewFrame extends JFrame implements WindowListener { }); } + /** + * Exit the application. + */ public void exit() { if (getViewPanel() != null) { getViewPanel().stop(); 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 9c0681c..a511bc7 100755 --- a/src/main/java/eu/svjatoslav/sixth/e3d/gui/ViewPanel.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/gui/ViewPanel.java @@ -4,7 +4,7 @@ */ package eu.svjatoslav.sixth.e3d.gui; -import eu.svjatoslav.sixth.e3d.gui.humaninput.HIDInputTracker; +import eu.svjatoslav.sixth.e3d.gui.humaninput.HIDEventTracker; import eu.svjatoslav.sixth.e3d.gui.humaninput.KeyboardFocusStack; import eu.svjatoslav.sixth.e3d.renderer.raster.ShapeCollection; @@ -21,7 +21,7 @@ import java.util.concurrent.ConcurrentHashMap; */ public class ViewPanel extends JPanel implements ComponentListener { private static final long serialVersionUID = 1683277888885045387L; - private final HIDInputTracker HIDInputTracker = new HIDInputTracker(this); + private final HIDEventTracker HIDEventTracker = new HIDEventTracker(this); private final KeyboardFocusStack keyboardFocusStack; private final Avatar avatar = new Avatar(); private final ShapeCollection rootShapeCollection = new ShapeCollection(); @@ -49,7 +49,7 @@ public class ViewPanel extends JPanel implements ComponentListener { public ViewPanel() { viewRenderListeners.add(avatar); - viewRenderListeners.add(HIDInputTracker); + viewRenderListeners.add(HIDEventTracker); keyboardFocusStack = new KeyboardFocusStack(this); @@ -72,8 +72,8 @@ public class ViewPanel extends JPanel implements ComponentListener { return rootShapeCollection; } - public HIDInputTracker getHIDInputTracker() { - return HIDInputTracker; + public HIDEventTracker getHIDInputTracker() { + return HIDEventTracker; } public void addViewUpdateListener(final ViewRenderListener listener) { @@ -145,13 +145,17 @@ public class ViewPanel extends JPanel implements ComponentListener { } /** - * Calling this methods tells 3D engine that current 3D view needs to be + * Calling these methods tells 3D engine that current 3D view needs to be * repainted on first opportunity. */ public void repaintDuringNextViewUpdate() { viewRepaintNeeded = true; } + /** + * Set target frames per second rate for this view. Target FPS can be changed at runtime. + * @param frameRate target frames per second rate for this view. + */ public void setFrameRate(final int frameRate) { if (canvasUpdateTimerTask != null) { canvasUpdateTimerTask.cancel(); @@ -163,25 +167,21 @@ public class ViewPanel extends JPanel implements ComponentListener { targetFPS = frameRate; - if (frameRate > 0) { - canvasUpdateTimer = new Timer(); - canvasUpdateTimerTask = new ViewUpdateTimerTask(this); + if (frameRate <= 0) return; - canvasUpdateTimer.schedule(canvasUpdateTimerTask, 0, - 1000 / frameRate); - } + canvasUpdateTimer = new Timer(); + canvasUpdateTimerTask = new ViewUpdateTimerTask(this); + + // schedule timer task to run in frequency according to defined frame rate + canvasUpdateTimer.schedule(canvasUpdateTimerTask, 0, + 1000 / frameRate); } + /** + * Stops rendering of this view. + */ public void stop() { - if (canvasUpdateTimerTask != null) { - canvasUpdateTimerTask.cancel(); - canvasUpdateTimerTask = null; - } - - if (canvasUpdateTimer != null) { - canvasUpdateTimer.cancel(); - canvasUpdateTimer = null; - } + setFrameRate(0); } /** diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/io/Connexion3D.java b/src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/Connexion3D.java similarity index 61% rename from src/main/java/eu/svjatoslav/sixth/e3d/io/Connexion3D.java rename to src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/Connexion3D.java index 90e7306..027dbf0 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/io/Connexion3D.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/Connexion3D.java @@ -2,18 +2,18 @@ * Sixth 3D engine. Author: Svjatoslav Agejenko. * This project is released under Creative Commons Zero (CC0) license. */ -package eu.svjatoslav.sixth.e3d.io; +package eu.svjatoslav.sixth.e3d.gui.humaninput; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; /** - * 3D Connexion mouse adapter. - *

- * Idea is to read Linux device file and interpret resulting numbers. - *

- * TODO: unfinished + * I have Space Mouse Compact 3D Connexion mouse: https://3dconnexion.com/us/product/spacemouse-compact/ + * + * I discovered that it is possible to read raw data from it by reading /dev/hidraw4 file. + * + * TODO: reverse engineer the data format and implement a driver for it. */ public class Connexion3D { @@ -24,6 +24,7 @@ public class Connexion3D { "/dev/hidraw4")); + // for testing purposes while (true) { System.out.print(in.read() + " "); System.out.println("\n"); diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/HIDInputTracker.java b/src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/HIDEventTracker.java similarity index 97% rename from src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/HIDInputTracker.java rename to src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/HIDEventTracker.java index c08fa8e..19dee50 100755 --- a/src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/HIDInputTracker.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/HIDEventTracker.java @@ -20,7 +20,7 @@ import java.util.Map; * This class is responsible for tracking human input devices (keyboard, mouse, etc.) and * forwarding those inputs to subsequent virtual components. */ -public class HIDInputTracker implements +public class HIDEventTracker implements MouseMotionListener, KeyListener, MouseListener, MouseWheelListener, ViewRenderListener { /** @@ -40,7 +40,10 @@ public class HIDInputTracker implements private boolean mouseMoved; private boolean mouseWithinWindow = false; - public HIDInputTracker(final ViewPanel viewPanel) { + /** + * Construct new tracker for specified panel. + */ + public HIDEventTracker(final ViewPanel viewPanel) { this.viewPanel = viewPanel; bind(viewPanel); } @@ -57,6 +60,10 @@ public class HIDInputTracker implements return viewUpdateNeeded; } + /** + * Bind this tracker to specified panel. + * @param panel panel to bind to. + */ private void bind(final JPanel panel) { panel.addMouseMotionListener(this); diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/WorldNavigationUserInputTracker.java b/src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/WorldNavigationUserInputTracker.java index 9c8415e..f6af6d2 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/WorldNavigationUserInputTracker.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/WorldNavigationUserInputTracker.java @@ -16,7 +16,7 @@ public class WorldNavigationUserInputTracker implements KeyboardInputHandler, Vi public boolean beforeRender(final ViewPanel viewPanel, final int millisecondsSinceLastFrame) { - final HIDInputTracker inputTracker = viewPanel.getHIDInputTracker(); + final HIDEventTracker inputTracker = viewPanel.getHIDInputTracker(); final Avatar avatar = viewPanel.getAvatar(); diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/package-info.java b/src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/package-info.java new file mode 100644 index 0000000..dfbbd22 --- /dev/null +++ b/src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/package-info.java @@ -0,0 +1,6 @@ +package eu.svjatoslav.sixth.e3d.gui.humaninput; + +/** + * This package is responsible for tracking human input devices (keyboard, mouse, etc.) and + * forwarding those inputs to subsequent virtual components. + */ \ No newline at end of file diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/gui/textEditorComponent/package-info.java b/src/main/java/eu/svjatoslav/sixth/e3d/gui/textEditorComponent/package-info.java new file mode 100644 index 0000000..3773a0a --- /dev/null +++ b/src/main/java/eu/svjatoslav/sixth/e3d/gui/textEditorComponent/package-info.java @@ -0,0 +1,7 @@ +package eu.svjatoslav.sixth.e3d.gui.textEditorComponent; + +/** + * This package contains a text editor component. + * + * It is a simple text editor. + */ \ No newline at end of file -- 2.20.1