Improved code readability. Components now aware of what mouse button was clicked.
[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 objectPreviouslyUnderMouseCursor;
32
33     public void prepareForNewFrameRendering(){
34         mouseEvent = null;
35         currentObjectUnderMouseCursor = 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 currentObjectUnderMouseCursor;
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 setCurrentObjectUnderMouseCursor(MouseInteractionController currentObjectUnderMouseCursor) {
62         this.currentObjectUnderMouseCursor = currentObjectUnderMouseCursor;
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 handlePossibleComponentMouseEvent() {
89         if (mouseEvent == null) return false;
90
91         boolean viewRepaintNeeded = false;
92
93         if (objectPreviouslyUnderMouseCursor != currentObjectUnderMouseCursor) {
94             // Mouse cursor has just entered or left component.
95             viewRepaintNeeded = objectPreviouslyUnderMouseCursor != null && objectPreviouslyUnderMouseCursor.mouseExited();
96             viewRepaintNeeded |= currentObjectUnderMouseCursor != null && currentObjectUnderMouseCursor.mouseEntered();
97             objectPreviouslyUnderMouseCursor = currentObjectUnderMouseCursor;
98         }
99
100         if (mouseEvent.button != 0 && currentObjectUnderMouseCursor != null) {
101             // Mouse button was clicked on some component.
102             viewRepaintNeeded |= currentObjectUnderMouseCursor.mouseClicked(mouseEvent.button);
103         }
104
105         return viewRepaintNeeded;
106     }
107
108 }