ce8c4a00d4fa4493801098b5e63bf5ed938c1538
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / gui / RenderingContext.java
1 /*
2  * Sixth 3D engine. Author: Svjatoslav Agejenko. 
3  * This project is released under Creative Commons Zero (CC0) license.
4  */
5 package eu.svjatoslav.sixth.e3d.gui;
6
7 import eu.svjatoslav.sixth.e3d.geometry.Point2D;
8 import eu.svjatoslav.sixth.e3d.gui.humaninput.MouseEvent;
9 import eu.svjatoslav.sixth.e3d.gui.humaninput.MouseInteractionController;
10
11 import java.awt.*;
12 import java.awt.image.BufferedImage;
13 import java.awt.image.DataBufferByte;
14 import java.awt.image.WritableRaster;
15
16 public class RenderingContext {
17
18     public static final int bufferedImageType = BufferedImage.TYPE_4BYTE_ABGR;
19     public final Graphics2D graphics;
20     public final byte[] pixels;
21     public final int width;
22     public final int height;
23     /**
24      * Center of the screen in screen space (pixels).
25      * This is the point where (0,0) coordinate of the world space is rendered.
26      */
27     public final Point2D centerCoordinate;
28
29     public final double zoom;
30     final BufferedImage bufferedImage;
31     public int frameNumber = 0;
32
33     /**
34      * UI component that mouse is currently hovering over.
35      */
36     private MouseInteractionController objectPreviouslyUnderMouseCursor;
37
38     public void prepareForNewFrameRendering(){
39         mouseEvent = null;
40         currentObjectUnderMouseCursor = null;
41     }
42
43     /**
44      * Mouse click event that needs to be processed.
45      */
46     private  MouseEvent mouseEvent;
47
48     public void setMouseEvent(MouseEvent mouseEvent) {
49         this.mouseEvent = mouseEvent;
50     }
51
52     public MouseEvent getMouseEvent() {
53         return mouseEvent;
54     }
55
56     /**
57      * Item that user clicked on.
58      */
59     private MouseInteractionController currentObjectUnderMouseCursor;
60
61     /**
62      * Called when given object was detected under mouse cursor, while processing {@link #mouseEvent}.
63      * Because objects are rendered back to front. The last method caller will set the top-most object, if
64      * there are multiple objects under mouse cursor.
65      */
66     public void setCurrentObjectUnderMouseCursor(MouseInteractionController currentObjectUnderMouseCursor) {
67         this.currentObjectUnderMouseCursor = currentObjectUnderMouseCursor;
68     }
69
70     public RenderingContext(final int width, final int height) {
71         this.width = width;
72         this.height = height;
73         this.centerCoordinate = new Point2D(width / 2d, height / 2d);
74         this.zoom = width / 3d;
75
76         bufferedImage = new BufferedImage(width, height, bufferedImageType);
77
78         final WritableRaster raster = bufferedImage.getRaster();
79         final DataBufferByte dbi = (DataBufferByte) raster.getDataBuffer();
80         pixels = dbi.getData();
81
82         graphics = (Graphics2D) bufferedImage.getGraphics();
83         graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
84         graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
85     }
86
87     /**
88      * @return <code>true</code> if view repaint is needed.
89      */
90     public boolean handlePossibleComponentMouseEvent() {
91         if (mouseEvent == null) return false;
92
93         boolean viewRepaintNeeded = false;
94
95         if (objectPreviouslyUnderMouseCursor != currentObjectUnderMouseCursor) {
96             // Mouse cursor has just entered or left component.
97             viewRepaintNeeded = objectPreviouslyUnderMouseCursor != null && objectPreviouslyUnderMouseCursor.mouseExited();
98             viewRepaintNeeded |= currentObjectUnderMouseCursor != null && currentObjectUnderMouseCursor.mouseEntered();
99             objectPreviouslyUnderMouseCursor = currentObjectUnderMouseCursor;
100         }
101
102         if (mouseEvent.button != 0 && currentObjectUnderMouseCursor != null) {
103             // Mouse button was clicked on some component.
104             viewRepaintNeeded |= currentObjectUnderMouseCursor.mouseClicked(mouseEvent.button);
105         }
106
107         return viewRepaintNeeded;
108     }
109
110 }