X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Feu%2Fsvjatoslav%2Fsixth%2Fe3d%2Fgui%2FViewPanel.java;h=e068bf50f670c49f2e2fdbf9446d37d22bbcbc84;hb=HEAD;hp=84d45293f38aa19095ee72b5d1ef99c9c18f23d4;hpb=9dcd9d8a7d3bc16eb6fde3681cd32e02dc0707e9;p=sixth-3d.git 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 84d4529..e068bf5 100755 --- a/src/main/java/eu/svjatoslav/sixth/e3d/gui/ViewPanel.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/gui/ViewPanel.java @@ -1,17 +1,11 @@ /* - * Sixth 3D engine. Copyright ©2012-2018, 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.gui.humaninput.MouseInteractionController; import eu.svjatoslav.sixth.e3d.renderer.raster.ShapeCollection; import javax.swing.*; @@ -24,39 +18,46 @@ 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 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; + /** - * UI component that mouse is currently hovering over. - */ - private MouseInteractionController currentMouseOverComponent; - /** - * 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. */ private boolean viewRepaintNeeded = true; + public ViewPanel() { viewRenderListeners.add(avatar); - viewRenderListeners.add(HIDInputTracker); + viewRenderListeners.add(HIDEventTracker); keyboardFocusStack = new KeyboardFocusStack(this); @@ -79,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) { @@ -126,28 +127,6 @@ public class ViewPanel extends JPanel implements ComponentListener { return renderingContext; } - private void handleDetectedComponentMouseEvents() { - if (renderingContext.objectUnderMouse != null) { - if (renderingContext.mouseEvent.button == 0) { - // mouse over - if (currentMouseOverComponent == null) { - currentMouseOverComponent = renderingContext.objectUnderMouse; - viewRepaintNeeded |= currentMouseOverComponent.mouseEntered(); - } else if (currentMouseOverComponent != renderingContext.objectUnderMouse) { - viewRepaintNeeded |= currentMouseOverComponent.mouseExited(); - currentMouseOverComponent = renderingContext.objectUnderMouse; - viewRepaintNeeded |= currentMouseOverComponent.mouseEntered(); - } - } else { - // mouse click - viewRepaintNeeded |= renderingContext.objectUnderMouse.mouseClicked(); - } - } else if (currentMouseOverComponent != null) { - viewRepaintNeeded |= currentMouseOverComponent.mouseExited(); - currentMouseOverComponent = null; - } - } - private void initializePanelLayout() { setFocusCycleRoot(true); setOpaque(true); @@ -158,7 +137,6 @@ public class ViewPanel extends JPanel implements ComponentListener { } private void renderFrame() { - // paint root geometry collection to the offscreen render buffer clearCanvas(); rootShapeCollection.paint(this, renderingContext); @@ -175,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(); @@ -193,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); } /** @@ -221,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(); @@ -236,7 +214,7 @@ public class ViewPanel extends JPanel implements ComponentListener { // abort rendering if window size is invalid if ((getWidth() > 0) && (getHeight() > 0) && renderFrame) { renderFrame(); - handleDetectedComponentMouseEvents(); + viewRepaintNeeded = renderingContext.handlePossibleComponentMouseEvent(); } } @@ -245,19 +223,19 @@ 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)) { renderingContext = new RenderingContext(panelWidth, panelHeight); } - renderingContext.mouseEvent = null; - renderingContext.objectUnderMouse = null; + renderingContext.prepareForNewFrameRendering(); } private boolean notifyViewRenderListeners(int millisecondsPassedSinceLastUpdate) {