2 * Sixth 3D engine. Author: Svjatoslav Agejenko.
3 * This project is released under Creative Commons Zero (CC0) license.
5 package eu.svjatoslav.sixth.e3d.gui;
7 import eu.svjatoslav.sixth.e3d.gui.humaninput.MouseEvent;
8 import eu.svjatoslav.sixth.e3d.gui.humaninput.MouseInteractionController;
11 import java.awt.image.BufferedImage;
12 import java.awt.image.DataBufferByte;
13 import java.awt.image.WritableRaster;
15 public class RenderingContext {
17 public static final int bufferedImageType = BufferedImage.TYPE_4BYTE_ABGR;
18 public final Graphics2D graphics;
19 public final byte[] pixels;
20 public final int width;
21 public final int height;
22 public final int xCenter;
23 public final int yCenter;
24 public final double zoom;
25 final BufferedImage bufferedImage;
26 public int frameNumber = 0;
29 * UI component that mouse is currently hovering over.
31 private MouseInteractionController currentMouseOverComponent;
33 public void prepareForNewFrameRendering(){
35 objectUnderMouse = null;
39 * Mouse click event that needs to be processed.
41 private MouseEvent mouseEvent;
43 public void setMouseEvent(MouseEvent mouseEvent) {
44 this.mouseEvent = mouseEvent;
47 public MouseEvent getMouseEvent() {
52 * Item that user clicked on.
54 private MouseInteractionController objectUnderMouse;
57 * Called when given object was detected under mouse cursor, while processing {@link #mouseEvent}.
58 * Because objects are rendered back to front. The last method caller will set the top-most object, if
59 * there are multiple objects under mouse cursor.
61 public void setObjectUnderMouse(MouseInteractionController objectUnderMouse) {
62 this.objectUnderMouse = objectUnderMouse;
65 public RenderingContext(final int width, final int height) {
69 this.xCenter = width / 2;
70 this.yCenter = height / 2;
72 this.zoom = width / 3d;
74 bufferedImage = new BufferedImage(width, height, bufferedImageType);
76 final WritableRaster raster = bufferedImage.getRaster();
77 final DataBufferByte dbi = (DataBufferByte) raster.getDataBuffer();
78 pixels = dbi.getData();
80 graphics = (Graphics2D) bufferedImage.getGraphics();
81 graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
82 graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
86 * @return <code>true</code> if view repaint is needed.
88 public boolean handleDetectedComponentMouseEvents() {
89 if (objectUnderMouse != null) {
90 if (mouseEvent.button == 0) {
92 if (currentMouseOverComponent == null) {
93 currentMouseOverComponent = objectUnderMouse;
94 return currentMouseOverComponent.mouseEntered();
95 } else if (currentMouseOverComponent != objectUnderMouse) {
96 boolean viewRepaintNeeded = currentMouseOverComponent.mouseExited();
97 currentMouseOverComponent = objectUnderMouse;
98 return viewRepaintNeeded | currentMouseOverComponent.mouseEntered();
102 return objectUnderMouse.mouseClicked();
104 } else if (currentMouseOverComponent != null) {
105 boolean viewRepaintNeeded = currentMouseOverComponent.mouseExited();
106 currentMouseOverComponent = null;
107 return viewRepaintNeeded;