From: Svjatoslav Agejenko Date: Fri, 13 Jul 2018 23:44:54 +0000 (+0300) Subject: Removed ViewContext. X-Git-Tag: sixth-3d-1.2~33 X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=commitdiff_plain;h=8bceefbdb7316557ee77902cc6ac216f5c1ff160;p=sixth-3d.git Removed ViewContext. --- diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/gui/Avatar.java b/src/main/java/eu/svjatoslav/sixth/e3d/gui/Avatar.java index 7f0f698..8dae301 100755 --- a/src/main/java/eu/svjatoslav/sixth/e3d/gui/Avatar.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/gui/Avatar.java @@ -70,7 +70,7 @@ public class Avatar implements ViewRenderListener { } @Override - public boolean beforeRender(final ViewContext viewContext, final int millisecondsSinceLastFrame) { + public boolean beforeRender(final ViewPanel viewPanel, final int millisecondsSinceLastFrame) { final Point3D locationBeforeUpdate = new Point3D(location); translateAvatarLocation(millisecondsSinceLastFrame); diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/gui/GuiComponent.java b/src/main/java/eu/svjatoslav/sixth/e3d/gui/GuiComponent.java index 8ab5c90..15e2d77 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/gui/GuiComponent.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/gui/GuiComponent.java @@ -25,21 +25,21 @@ public class GuiComponent extends AbstractCompositeShape implements UserInputHandler, MouseInteractionController { private static final String GROUP_GUI_FOCUS = "gui.focus"; - public final ViewContext viewContext; + public final ViewPanel viewPanel; Box containingBox = new Box(); private WireframeBox borders = null; private boolean borderShown = false; public GuiComponent(final Transform transform, - final ViewContext viewContext, final Point3D size) { + final ViewPanel viewPanel, final Point3D size) { super(transform); - this.viewContext = viewContext; + this.viewPanel = viewPanel; setDimensions(size); } @Override - public boolean beforeRender(final ViewContext viewContext, + public boolean beforeRender(final ViewPanel viewPanel, final int millisecondsSinceLastFrame) { return false; } @@ -56,12 +56,12 @@ public class GuiComponent extends AbstractCompositeShape implements } @Override - public void focusLost(final ViewContext viewContext) { + public void focusLost(final ViewPanel viewPanel) { hideBorder(); } @Override - public void focusReceived(final ViewContext viewContext) { + public void focusReceived(final ViewPanel viewPanel) { showBorder(); } @@ -91,18 +91,18 @@ public class GuiComponent extends AbstractCompositeShape implements } @Override - public void keyPressed(final KeyEvent event, final ViewContext viewContext) { + public void keyPressed(final KeyEvent event, final ViewPanel viewPanel) { if (event.getKeyChar() == KeyboardHelper.ESC) - viewContext.getKeyboardFocusTracker().popFocusOwner(); + viewPanel.getKeyboardFocusTracker().popFocusOwner(); } @Override - public void keyReleased(final KeyEvent event, final ViewContext viewContext) { + public void keyReleased(final KeyEvent event, final ViewPanel viewPanel) { } @Override public void mouseClicked() { - viewContext.getKeyboardFocusTracker().setFocusOwner(this); + viewPanel.getKeyboardFocusTracker().setFocusOwner(this); } @Override diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/gui/ViewContext.java b/src/main/java/eu/svjatoslav/sixth/e3d/gui/ViewContext.java deleted file mode 100644 index be949c8..0000000 --- a/src/main/java/eu/svjatoslav/sixth/e3d/gui/ViewContext.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * 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. - * - */ - -package eu.svjatoslav.sixth.e3d.gui; - -import eu.svjatoslav.sixth.e3d.gui.humaninput.KeyboardFocusTracker; -import eu.svjatoslav.sixth.e3d.gui.humaninput.UserInputTracker; -import eu.svjatoslav.sixth.e3d.renderer.raster.ShapeCollection; - -public class ViewContext { - - private final UserInputTracker userInputTracker = new UserInputTracker(this); - - private final KeyboardFocusTracker keyboardFocusTracker = new KeyboardFocusTracker( - this); - - private final Avatar avatar = new Avatar(); - - private final ViewPanel viewPanel; - - private final ShapeCollection rootShapeCollection = new ShapeCollection(); - - public ViewContext(final ViewPanel viewPanel) { - this.viewPanel = viewPanel; - } - - public Avatar getAvatar() { - return avatar; - } - - public KeyboardFocusTracker getKeyboardFocusTracker() { - return keyboardFocusTracker; - } - - public ShapeCollection getRootShapeCollection() { - return rootShapeCollection; - } - - public UserInputTracker getUserInputTracker() { - return userInputTracker; - } - - public ViewPanel getViewPanel() { - return viewPanel; - } - -} 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 feda7df..f38d3c0 100755 --- a/src/main/java/eu/svjatoslav/sixth/e3d/gui/ViewPanel.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/gui/ViewPanel.java @@ -9,7 +9,10 @@ package eu.svjatoslav.sixth.e3d.gui; +import eu.svjatoslav.sixth.e3d.gui.humaninput.KeyboardFocusTracker; import eu.svjatoslav.sixth.e3d.gui.humaninput.MouseInteractionController; +import eu.svjatoslav.sixth.e3d.gui.humaninput.UserInputTracker; +import eu.svjatoslav.sixth.e3d.renderer.raster.ShapeCollection; import javax.swing.*; import java.awt.*; @@ -20,10 +23,34 @@ import java.util.List; import java.util.Timer; public class ViewPanel extends JPanel implements ComponentListener { + private final UserInputTracker userInputTracker = new UserInputTracker(this); + + private final KeyboardFocusTracker keyboardFocusTracker = new KeyboardFocusTracker( + this); + + private final Avatar avatar = new Avatar(); + + private final ShapeCollection rootShapeCollection = new ShapeCollection(); + + + public Avatar getAvatar() { + return avatar; + } + + public KeyboardFocusTracker getKeyboardFocusTracker() { + return keyboardFocusTracker; + } + + public ShapeCollection getRootShapeCollection() { + return rootShapeCollection; + } + + public UserInputTracker getUserInputTracker() { + return userInputTracker; + } private static final long serialVersionUID = 1683277888885045387L; private final List viewRenderListeners = new ArrayList<>(); - private final ViewContext context = new ViewContext(this); /** * Last time this view was updated. */ @@ -50,11 +77,8 @@ public class ViewPanel extends JPanel implements ComponentListener { private boolean viewRepaintNeeded = true; public ViewPanel() { - viewRenderListeners.add(context.getAvatar()); - - // initialize input tracker - context.getUserInputTracker().bind(this); - viewRenderListeners.add(context.getUserInputTracker()); + viewRenderListeners.add(avatar); + viewRenderListeners.add(userInputTracker); initializePanelLayout(); @@ -87,10 +111,6 @@ public class ViewPanel extends JPanel implements ComponentListener { viewRepaintNeeded = true; } - public ViewContext getContext() { - return context; - } - @Override public Dimension getMaximumSize() { return getPreferredSize(); @@ -164,7 +184,7 @@ public class ViewPanel extends JPanel implements ComponentListener { } // paint root geometry collection to the offscreen render buffer - context.getRootShapeCollection().paint(context, renderingContext); + rootShapeCollection.paint(this, renderingContext); // draw rendered offscreen image to visible screen final Graphics graphics = getGraphics(); @@ -240,8 +260,7 @@ public class ViewPanel extends JPanel implements ComponentListener { boolean reRenderFrame = false; for (final ViewRenderListener listener : viewRenderListeners) - if (listener.beforeRender(context, - millisecondsPassedSinceLastUpdate)) + if (listener.beforeRender(this, millisecondsPassedSinceLastUpdate)) reRenderFrame = true; // abort rendering if window size is invalid diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/gui/ViewRenderListener.java b/src/main/java/eu/svjatoslav/sixth/e3d/gui/ViewRenderListener.java index 0977ef7..5444852 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/gui/ViewRenderListener.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/gui/ViewRenderListener.java @@ -23,5 +23,5 @@ public interface ViewRenderListener { * @return true if underlying view shall be re-rendered. If at least one of the view update listeners * returns true, view is re-rendered. */ - boolean beforeRender(ViewContext viewContext, final int millisecondsSinceLastFrame); + boolean beforeRender(ViewPanel viewPanel, final int millisecondsSinceLastFrame); } diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/KeyboardFocusTracker.java b/src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/KeyboardFocusTracker.java index b7919bc..65e9374 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/KeyboardFocusTracker.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/KeyboardFocusTracker.java @@ -9,19 +9,19 @@ package eu.svjatoslav.sixth.e3d.gui.humaninput; -import eu.svjatoslav.sixth.e3d.gui.ViewContext; +import eu.svjatoslav.sixth.e3d.gui.ViewPanel; import java.util.Stack; public class KeyboardFocusTracker { - private final ViewContext viewContext; + private final ViewPanel viewPanel; WorldNavigationTracker defaultInputHandler = new WorldNavigationTracker(); Stack inputHandlers = new Stack<>(); private UserInputHandler currentUserInputHandler; - public KeyboardFocusTracker(final ViewContext viewContext) { - this.viewContext = viewContext; + public KeyboardFocusTracker(final ViewPanel viewPanel) { + this.viewPanel = viewPanel; setFocusOwner(defaultInputHandler); } @@ -36,11 +36,11 @@ public class KeyboardFocusTracker { if (inputHandlers.isEmpty()) return; - currentUserInputHandler.focusLost(viewContext); + currentUserInputHandler.focusLost(viewPanel); currentUserInputHandler = inputHandlers.pop(); - currentUserInputHandler.focusReceived(viewContext); + currentUserInputHandler.focusReceived(viewPanel); } public void setFocusOwner(final UserInputHandler inputHandler) { @@ -48,12 +48,12 @@ public class KeyboardFocusTracker { return; if (currentUserInputHandler != null) { - currentUserInputHandler.focusLost(viewContext); + currentUserInputHandler.focusLost(viewPanel); inputHandlers.push(currentUserInputHandler); } currentUserInputHandler = inputHandler; - currentUserInputHandler.focusReceived(viewContext); + currentUserInputHandler.focusReceived(viewPanel); } } diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/UserInputHandler.java b/src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/UserInputHandler.java index ae9f8d9..21fd667 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/UserInputHandler.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/UserInputHandler.java @@ -9,19 +9,19 @@ package eu.svjatoslav.sixth.e3d.gui.humaninput; -import eu.svjatoslav.sixth.e3d.gui.ViewContext; +import eu.svjatoslav.sixth.e3d.gui.ViewPanel; import eu.svjatoslav.sixth.e3d.gui.ViewRenderListener; import java.awt.event.KeyEvent; public interface UserInputHandler extends ViewRenderListener { - void focusLost(ViewContext viewContext); + void focusLost(ViewPanel viewPanel); - void focusReceived(ViewContext viewContext); + void focusReceived(ViewPanel viewPanel); - void keyPressed(KeyEvent event, ViewContext viewContext); + void keyPressed(KeyEvent event, ViewPanel viewPanel); - void keyReleased(KeyEvent event, ViewContext viewContext); + void keyReleased(KeyEvent event, ViewPanel viewPanel); } diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/UserInputTracker.java b/src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/UserInputTracker.java index 1dd6f7a..63bfd19 100755 --- a/src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/UserInputTracker.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/UserInputTracker.java @@ -12,7 +12,6 @@ package eu.svjatoslav.sixth.e3d.gui.humaninput; import eu.svjatoslav.sixth.e3d.geometry.Point2D; import eu.svjatoslav.sixth.e3d.gui.Avatar; import eu.svjatoslav.sixth.e3d.gui.ViewPanel; -import eu.svjatoslav.sixth.e3d.gui.ViewContext; import eu.svjatoslav.sixth.e3d.gui.ViewRenderListener; import javax.swing.*; @@ -39,26 +38,27 @@ public class UserInputTracker private int wheelMovedDirection = 0; private Point2D mouseDraggedDirection = new Point2D(); private Point2D oldMouseCoordinatesWhenDragging; - private ViewContext viewContext; + private ViewPanel viewPanel; private Point2D currentMouseLocation; private boolean mouseMoved; private boolean mouseWithinWindow = false; - public UserInputTracker(final ViewContext viewContext) { - this.viewContext = viewContext; + public UserInputTracker(final ViewPanel viewPanel) { + this.viewPanel = viewPanel; + bind(viewPanel); } /** * {@inheritDoc} */ @Override - public boolean beforeRender(final ViewContext viewContext, final int millisecondsSinceLastFrame) { + public boolean beforeRender(final ViewPanel viewPanel, final int millisecondsSinceLastFrame) { - boolean viewUpdateNeeded = handleDetectedMouseClicks(viewContext.getViewPanel()); + boolean viewUpdateNeeded = handleDetectedMouseClicks(viewPanel); viewUpdateNeeded |= handleDetectedKeyEvents(); - viewContext.getKeyboardFocusTracker().getCurrentFocusOwner().beforeRender(viewContext, + viewPanel.getKeyboardFocusTracker().getCurrentFocusOwner().beforeRender(viewPanel, millisecondsSinceLastFrame); viewUpdateNeeded |= trackMouse(); @@ -66,7 +66,7 @@ public class UserInputTracker return viewUpdateNeeded; } - public void bind(final JPanel panel) { + private void bind(final JPanel panel) { panel.addMouseMotionListener(this); panel.addKeyListener(this); @@ -79,7 +79,7 @@ public class UserInputTracker private boolean handleDetectedKeyEvents() { boolean keyEventsHandled = false; - final UserInputHandler currentFocusOwner = viewContext.getKeyboardFocusTracker().getCurrentFocusOwner(); + final UserInputHandler currentFocusOwner = viewPanel.getKeyboardFocusTracker().getCurrentFocusOwner(); synchronized (detectedKeyEvents) { if (currentFocusOwner == null) { @@ -92,12 +92,12 @@ public class UserInputTracker switch (keyEvent.getID()) { case KeyEvent.KEY_PRESSED: - currentFocusOwner.keyPressed(keyEvent, viewContext); + currentFocusOwner.keyPressed(keyEvent, viewPanel); keyEventsHandled = true; break; case KeyEvent.KEY_RELEASED: - currentFocusOwner.keyReleased(keyEvent, viewContext); + currentFocusOwner.keyReleased(keyEvent, viewPanel); keyEventsHandled = true; break; } @@ -137,7 +137,7 @@ public class UserInputTracker synchronized (detectedKeyEvents) { pressedKeysToPressedTimeMap.put(evt.getKeyCode(), System.currentTimeMillis()); detectedKeyEvents.add(evt); - viewContext.getViewPanel().repaintDuringNextViewUpdate(); + viewPanel.repaintDuringNextViewUpdate(); } } @@ -146,7 +146,7 @@ public class UserInputTracker synchronized (detectedKeyEvents) { pressedKeysToPressedTimeMap.remove(evt.getKeyCode()); detectedKeyEvents.add(evt); - viewContext.getViewPanel().repaintDuringNextViewUpdate(); + viewPanel.repaintDuringNextViewUpdate(); } } @@ -210,7 +210,7 @@ public class UserInputTracker * Interpret mouse movement */ private boolean trackMouse() { - final Avatar avatar = viewContext.getAvatar(); + final Avatar avatar = viewPanel.getAvatar(); trackDragging(avatar); trackVerticalScrolling(avatar); diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/WorldNavigationTracker.java b/src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/WorldNavigationTracker.java index cc99c42..44e09d2 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/WorldNavigationTracker.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/gui/humaninput/WorldNavigationTracker.java @@ -10,7 +10,7 @@ package eu.svjatoslav.sixth.e3d.gui.humaninput; import eu.svjatoslav.sixth.e3d.gui.Avatar; -import eu.svjatoslav.sixth.e3d.gui.ViewContext; +import eu.svjatoslav.sixth.e3d.gui.ViewPanel; import eu.svjatoslav.sixth.e3d.gui.textEditorComponent.KeyboardHelper; import java.awt.event.KeyEvent; @@ -18,34 +18,34 @@ import java.awt.event.KeyEvent; public class WorldNavigationTracker implements UserInputHandler { @Override - public boolean beforeRender(final ViewContext viewContext, + public boolean beforeRender(final ViewPanel viewPanel, final int millisecondsSinceLastFrame) { - trackKeys(millisecondsSinceLastFrame, viewContext); + trackKeys(millisecondsSinceLastFrame, viewPanel); return false; } @Override - public void focusLost(final ViewContext viewContext) { + public void focusLost(final ViewPanel viewPanel) { } @Override - public void focusReceived(final ViewContext viewContext) { + public void focusReceived(final ViewPanel viewContext) { } @Override - public void keyPressed(final KeyEvent event, final ViewContext viewContext) { + public void keyPressed(final KeyEvent event, final ViewPanel viewContext) { } @Override - public void keyReleased(final KeyEvent event, final ViewContext viewContext) { + public void keyReleased(final KeyEvent event, final ViewPanel viewContext) { } /** * interpret currently pressed keys */ public void trackKeys(final long millisecondsSinceLastFrame, - final ViewContext viewContext) { + final ViewPanel viewContext) { final UserInputTracker inputTracker = viewContext.getUserInputTracker(); diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/gui/textEditorComponent/TextEditComponent.java b/src/main/java/eu/svjatoslav/sixth/e3d/gui/textEditorComponent/TextEditComponent.java index b7c633f..bba0717 100755 --- a/src/main/java/eu/svjatoslav/sixth/e3d/gui/textEditorComponent/TextEditComponent.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/gui/textEditorComponent/TextEditComponent.java @@ -10,10 +10,10 @@ package eu.svjatoslav.sixth.e3d.gui.textEditorComponent; import eu.svjatoslav.sixth.e3d.geometry.Point2D; +import eu.svjatoslav.sixth.e3d.gui.ViewPanel; import eu.svjatoslav.sixth.e3d.math.Transform; import eu.svjatoslav.sixth.e3d.gui.GuiComponent; import eu.svjatoslav.sixth.e3d.gui.TextPointer; -import eu.svjatoslav.sixth.e3d.gui.ViewContext; import eu.svjatoslav.sixth.e3d.renderer.raster.Color; import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.textcanvas.TextCanvas; @@ -40,8 +40,8 @@ public class TextEditComponent extends GuiComponent implements ClipboardOwner { boolean repaintPage = false; public TextEditComponent(final Transform transform, - final ViewContext viewContext, final Point2D size) { - super(transform, viewContext, size.to3D()); + final ViewPanel viewPanel, final Point2D size) { + super(transform, viewPanel, size.to3D()); cursorLocation = new TextPointer(0, 0); @@ -249,8 +249,8 @@ public class TextEditComponent extends GuiComponent implements ClipboardOwner { * Parse key presses. */ @Override - public void keyPressed(final KeyEvent event, final ViewContext viewContext) { - super.keyPressed(event, viewContext); + public void keyPressed(final KeyEvent event, final ViewPanel viewPanel) { + super.keyPressed(event, viewPanel); processKeyEvent(event); diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/ShapeCollection.java b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/ShapeCollection.java index 2faf880..5210515 100755 --- a/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/ShapeCollection.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/ShapeCollection.java @@ -10,11 +10,11 @@ package eu.svjatoslav.sixth.e3d.renderer.raster; import eu.svjatoslav.sixth.e3d.geometry.Point3D; -import eu.svjatoslav.sixth.e3d.math.Transform; -import eu.svjatoslav.sixth.e3d.math.TransformPipe; import eu.svjatoslav.sixth.e3d.gui.Avatar; import eu.svjatoslav.sixth.e3d.gui.RenderingContext; -import eu.svjatoslav.sixth.e3d.gui.ViewContext; +import eu.svjatoslav.sixth.e3d.gui.ViewPanel; +import eu.svjatoslav.sixth.e3d.math.Transform; +import eu.svjatoslav.sixth.e3d.math.TransformPipe; import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.AbstractShape; import java.util.ArrayList; @@ -39,7 +39,7 @@ public class ShapeCollection { shapes.clear(); } - public synchronized void paint(final ViewContext viewContext, + public synchronized void paint(final ViewPanel viewPanel, final RenderingContext renderingContext) { renderingContext.frameNumber++; @@ -48,7 +48,7 @@ public class ShapeCollection { transformPipe.clear(); // translate scene according to camera current location - final Avatar avatar = viewContext.getAvatar(); + final Avatar avatar = viewPanel.getAvatar(); // rotate scene according to avatar looking direction transformPipe.addTransform(new Transform(new Point3D(), avatar