/*
- * Sixth 3D engine. Copyright ©2012-2020, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of version 3 of the GNU Lesser General Public License
- * or later as published by the Free Software Foundation.
- *
+ * 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;
/**
* 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 = 30;
+ 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.
public ViewPanel() {
viewRenderListeners.add(avatar);
- viewRenderListeners.add(HIDInputTracker);
+ viewRenderListeners.add(HIDEventTracker);
keyboardFocusStack = new KeyboardFocusStack(this);
return rootShapeCollection;
}
- public HIDInputTracker getHIDInputTracker() {
- return HIDInputTracker;
+ public HIDEventTracker getHIDInputTracker() {
+ return HIDEventTracker;
}
public void addViewUpdateListener(final ViewRenderListener listener) {
}
/**
- * 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();
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);
}
/**
* 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();
// abort rendering if window size is invalid
if ((getWidth() > 0) && (getHeight() > 0) && renderFrame) {
renderFrame();
- viewRepaintNeeded = renderingContext.handleDetectedComponentMouseEvents();
+ viewRepaintNeeded = renderingContext.handlePossibleComponentMouseEvent();
}
}
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)) {