X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Feu%2Fsvjatoslav%2Fsixth%2Fe3d%2Fgui%2FRenderingContext.java;h=ce8c4a00d4fa4493801098b5e63bf5ed938c1538;hb=de8fb260a5e99922231b1d0f437916e796ec6ccb;hp=5e74a94a38951d708ad49d8ae545ef2ec286eb2a;hpb=c9cca2ea38fd301c4383d14178589c09520bea22;p=sixth-3d.git diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/gui/RenderingContext.java b/src/main/java/eu/svjatoslav/sixth/e3d/gui/RenderingContext.java index 5e74a94..ce8c4a0 100644 --- a/src/main/java/eu/svjatoslav/sixth/e3d/gui/RenderingContext.java +++ b/src/main/java/eu/svjatoslav/sixth/e3d/gui/RenderingContext.java @@ -1,15 +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.MouseClick; +import eu.svjatoslav.sixth.e3d.geometry.Point2D; +import eu.svjatoslav.sixth.e3d.gui.humaninput.MouseEvent; import eu.svjatoslav.sixth.e3d.gui.humaninput.MouseInteractionController; import java.awt.*; @@ -20,47 +16,61 @@ import java.awt.image.WritableRaster; public class RenderingContext { public static final int bufferedImageType = BufferedImage.TYPE_4BYTE_ABGR; - - final BufferedImage bufferedImage; - public final Graphics2D graphics; - public final byte[] pixels; - public final int width; public final int height; - - public final int xCenter; - public final int yCenter; + /** + * Center of the screen in screen space (pixels). + * This is the point where (0,0) coordinate of the world space is rendered. + */ + public final Point2D centerCoordinate; public final double zoom; - + final BufferedImage bufferedImage; public int frameNumber = 0; /** - * Used to signal that actual rendering should be performed. It is useful to - * skip rendering when we only want to detect mouse clicks intersections - * without actually updating rendered frame. + * UI component that mouse is currently hovering over. */ - public boolean doRender = true; // TODO: make use of the variable + private MouseInteractionController objectPreviouslyUnderMouseCursor; + + public void prepareForNewFrameRendering(){ + mouseEvent = null; + currentObjectUnderMouseCursor = null; + } /** - * Mouse click. During rendering we can detect which item user clicked on. + * Mouse click event that needs to be processed. */ - public MouseClick mouseClick; + private MouseEvent mouseEvent; + + public void setMouseEvent(MouseEvent mouseEvent) { + this.mouseEvent = mouseEvent; + } + + public MouseEvent getMouseEvent() { + return mouseEvent; + } /** * Item that user clicked on. */ - public MouseInteractionController clickedItem; + private MouseInteractionController currentObjectUnderMouseCursor; + + /** + * Called when given object was detected under mouse cursor, while processing {@link #mouseEvent}. + * Because objects are rendered back to front. The last method caller will set the top-most object, if + * there are multiple objects under mouse cursor. + */ + public void setCurrentObjectUnderMouseCursor(MouseInteractionController currentObjectUnderMouseCursor) { + this.currentObjectUnderMouseCursor = currentObjectUnderMouseCursor; + } public RenderingContext(final int width, final int height) { this.width = width; this.height = height; - - this.xCenter = width / 2; - this.yCenter = height / 2; - + this.centerCoordinate = new Point2D(width / 2d, height / 2d); this.zoom = width / 3d; bufferedImage = new BufferedImage(width, height, bufferedImageType); @@ -70,12 +80,31 @@ public class RenderingContext { pixels = dbi.getData(); graphics = (Graphics2D) bufferedImage.getGraphics(); + graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); + } + + /** + * @return true if view repaint is needed. + */ + public boolean handlePossibleComponentMouseEvent() { + if (mouseEvent == null) return false; + + boolean viewRepaintNeeded = false; + + if (objectPreviouslyUnderMouseCursor != currentObjectUnderMouseCursor) { + // Mouse cursor has just entered or left component. + viewRepaintNeeded = objectPreviouslyUnderMouseCursor != null && objectPreviouslyUnderMouseCursor.mouseExited(); + viewRepaintNeeded |= currentObjectUnderMouseCursor != null && currentObjectUnderMouseCursor.mouseEntered(); + objectPreviouslyUnderMouseCursor = currentObjectUnderMouseCursor; + } - graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, - RenderingHints.VALUE_ANTIALIAS_ON); + if (mouseEvent.button != 0 && currentObjectUnderMouseCursor != null) { + // Mouse button was clicked on some component. + viewRepaintNeeded |= currentObjectUnderMouseCursor.mouseClicked(mouseEvent.button); + } - graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, - RenderingHints.VALUE_TEXT_ANTIALIAS_ON); + return viewRepaintNeeded; } }