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