Better text resolution on texture
[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.gui.humaninput.MouseEvent;
8 import eu.svjatoslav.sixth.e3d.gui.humaninput.MouseInteractionController;
9
10 import java.awt.*;
11 import java.awt.image.BufferedImage;
12 import java.awt.image.DataBufferByte;
13 import java.awt.image.WritableRaster;
14
15 public class RenderingContext {
16
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;
27
28     /**
29      * UI component that mouse is currently hovering over.
30      */
31     private MouseInteractionController currentMouseOverComponent;
32
33     public void prepareForNewFrameRendering(){
34         mouseEvent = null;
35         objectUnderMouse = null;
36     }
37
38     /**
39      * Mouse click event that needs to be processed.
40      */
41     private  MouseEvent mouseEvent;
42
43     public void setMouseEvent(MouseEvent mouseEvent) {
44         this.mouseEvent = mouseEvent;
45     }
46
47     public MouseEvent getMouseEvent() {
48         return mouseEvent;
49     }
50
51     /**
52      * Item that user clicked on.
53      */
54     private MouseInteractionController objectUnderMouse;
55
56     /**
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.
60      */
61     public void setObjectUnderMouse(MouseInteractionController objectUnderMouse) {
62         this.objectUnderMouse = objectUnderMouse;
63     }
64
65     public RenderingContext(final int width, final int height) {
66         this.width = width;
67         this.height = height;
68
69         this.xCenter = width / 2;
70         this.yCenter = height / 2;
71
72         this.zoom = width / 3d;
73
74         bufferedImage = new BufferedImage(width, height, bufferedImageType);
75
76         final WritableRaster raster = bufferedImage.getRaster();
77         final DataBufferByte dbi = (DataBufferByte) raster.getDataBuffer();
78         pixels = dbi.getData();
79
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);
83     }
84
85     /**
86      * @return <code>true</code> if view repaint is needed.
87      */
88     public boolean handleDetectedComponentMouseEvents() {
89         if (objectUnderMouse != null) {
90             if (mouseEvent.button == 0) {
91                 // mouse over
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();
99                 }
100             } else {
101                 // mouse click
102                 return objectUnderMouse.mouseClicked();
103             }
104         } else if (currentMouseOverComponent != null) {
105             boolean viewRepaintNeeded = currentMouseOverComponent.mouseExited();
106             currentMouseOverComponent = null;
107             return viewRepaintNeeded;
108         }
109         return false;
110     }
111
112 }