Fixed git clone URL
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / gui / ViewPanel.java
index 7cf2b2f..e068bf5 100755 (executable)
@@ -1,10 +1,10 @@
 /*
- * Sixth 3D engine. Author: Svjatoslav Agejenko. 
+ * Sixth 3D engine. Author: Svjatoslav Agejenko.
  * This project is released under Creative Commons Zero (CC0) license.
  */
 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;
 
@@ -18,27 +18,37 @@ import java.util.concurrent.ConcurrentHashMap;
 
 /**
  * Java Swing GUI panel that contains canvas for 3D rendering.
+ * Usually it is used as a part of {@link ViewFrame}.
  */
 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 HIDEventTracker HIDEventTracker = new HIDEventTracker(this);
     private final KeyboardFocusStack keyboardFocusStack;
     private final Avatar avatar = new Avatar();
     private final ShapeCollection rootShapeCollection = new ShapeCollection();
     private final Set<ViewRenderListener> viewRenderListeners = ConcurrentHashMap.newKeySet();
+    public Color backgroundColor = Color.BLACK;
+
     /**
-     * Last time this view was updated.
+     * Stores milliseconds when last frame was updated. This is needed to calculate time delta between frames.
+     * Time delta is used to calculate smooth animation.
      */
     private long lastUpdateMillis = 0;
+
+    /**
+     * Timer that is used to update canvas at target FPS rate.
+     */
     private Timer canvasUpdateTimer;
+
     private ViewUpdateTimerTask canvasUpdateTimerTask;
     private RenderingContext renderingContext = null;
+
     /**
-     * Currently target FPS for this view. It can be changed at runtime. Also when nothing
-     * changes in the view, then frames are not really repainted.
+     * Currently target frames per second rate for this view. Target FPS can be changed at runtime.
+     * 3D engine tries to be smart and only repaints screen when there are visible changes.
      */
     private int targetFPS = 60;
+
     /**
      * Set to true if it is known than next frame reeds to be painted. Flag is cleared
      * immediately after frame got updated.
@@ -47,7 +57,7 @@ public class ViewPanel extends JPanel implements ComponentListener {
 
     public ViewPanel() {
         viewRenderListeners.add(avatar);
-        viewRenderListeners.add(HIDInputTracker);
+        viewRenderListeners.add(HIDEventTracker);
 
         keyboardFocusStack = new KeyboardFocusStack(this);
 
@@ -70,8 +80,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) {
@@ -143,13 +153,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();
@@ -161,25 +175,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);
     }
 
     /**
@@ -189,7 +199,7 @@ public class ViewPanel extends JPanel implements ComponentListener {
      * It tells view to update itself. View can decide if actual re-rendering of
      * graphics is needed.
      */
-    void updateView() {
+    void ensureThatViewIsUpToDate() {
         maintainRenderingContext();
 
         final int millisecondsPassedSinceLastUpdate = getMillisecondsPassedSinceLastUpdate();
@@ -204,7 +214,7 @@ public class ViewPanel extends JPanel implements ComponentListener {
         // abort rendering if window size is invalid
         if ((getWidth() > 0) && (getHeight() > 0) && renderFrame) {
             renderFrame();
-            viewRepaintNeeded = renderingContext.handleDetectedComponentMouseEvents();
+            viewRepaintNeeded = renderingContext.handlePossibleComponentMouseEvent();
         }
 
     }
@@ -213,11 +223,12 @@ public class ViewPanel extends JPanel implements ComponentListener {
         int panelWidth = getWidth();
         int panelHeight = getHeight();
 
-        if (panelWidth <= 0 || panelHeight <=0){
+        if (panelWidth <= 0 || panelHeight <= 0) {
             renderingContext = null;
             return;
         }
 
+        // create new rendering context if window size has changed
         if ((renderingContext == null)
                 || (renderingContext.width != panelWidth)
                 || (renderingContext.height != panelHeight)) {