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.*;
*/
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<ViewRenderListener> viewRenderListeners = new ArrayList<>();
private boolean viewRepaintNeeded = true;
public ViewPanel() {
viewRenderListeners.add(avatar);
- viewRenderListeners.add(userInputTracker);
+ viewRenderListeners.add(HIDInputTracker);
initializePanelLayout();
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) {
return new java.awt.Dimension(640, 480);
}
- public RenderingContext getRenderBuffer() {
- return renderingContext;
- }
-
public RenderingContext getRenderingContext() {
return renderingContext;
}
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.
* 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;
}
}