Updated copyright
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / gui / RenderingContext.java
1 /*
2  * Sixth 3D engine. Copyright ©2012-2019, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of version 3 of the GNU Lesser General Public License
6  * or later as published by the Free Software Foundation.
7  *
8  */
9
10 package eu.svjatoslav.sixth.e3d.gui;
11
12 import eu.svjatoslav.sixth.e3d.gui.humaninput.MouseEvent;
13 import eu.svjatoslav.sixth.e3d.gui.humaninput.MouseInteractionController;
14
15 import java.awt.*;
16 import java.awt.image.BufferedImage;
17 import java.awt.image.DataBufferByte;
18 import java.awt.image.WritableRaster;
19
20 public class RenderingContext {
21
22     public static final int bufferedImageType = BufferedImage.TYPE_4BYTE_ABGR;
23     public final Graphics2D graphics;
24     public final byte[] pixels;
25     public final int width;
26     public final int height;
27     public final int xCenter;
28     public final int yCenter;
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 currentMouseOverComponent;
37
38     public void prepareForNewFrameRendering(){
39         mouseEvent = null;
40         objectUnderMouse = 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 objectUnderMouse;
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 setObjectUnderMouse(MouseInteractionController objectUnderMouse) {
67         this.objectUnderMouse = objectUnderMouse;
68     }
69
70     public RenderingContext(final int width, final int height) {
71         this.width = width;
72         this.height = height;
73
74         this.xCenter = width / 2;
75         this.yCenter = height / 2;
76
77         this.zoom = width / 3d;
78
79         bufferedImage = new BufferedImage(width, height, bufferedImageType);
80
81         final WritableRaster raster = bufferedImage.getRaster();
82         final DataBufferByte dbi = (DataBufferByte) raster.getDataBuffer();
83         pixels = dbi.getData();
84
85         graphics = (Graphics2D) bufferedImage.getGraphics();
86         graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
87         graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
88     }
89
90     /**
91      * @return <code>true</code> if view repaint is needed.
92      */
93     public boolean handleDetectedComponentMouseEvents() {
94         if (objectUnderMouse != null) {
95             if (mouseEvent.button == 0) {
96                 // mouse over
97                 if (currentMouseOverComponent == null) {
98                     currentMouseOverComponent = objectUnderMouse;
99                     return currentMouseOverComponent.mouseEntered();
100                 } else if (currentMouseOverComponent != objectUnderMouse) {
101                     boolean viewRepaintNeeded = currentMouseOverComponent.mouseExited();
102                     currentMouseOverComponent = objectUnderMouse;
103                     return viewRepaintNeeded | currentMouseOverComponent.mouseEntered();
104                 }
105             } else {
106                 // mouse click
107                 return objectUnderMouse.mouseClicked();
108             }
109         } else if (currentMouseOverComponent != null) {
110             boolean viewRepaintNeeded = currentMouseOverComponent.mouseExited();
111             currentMouseOverComponent = null;
112             return viewRepaintNeeded;
113         }
114         return false;
115     }
116
117 }