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
index 0d16aa4..b6417e5 100644 (file)
@@ -1,12 +1,7 @@
 /*
- * Sixth 3D engine. Copyright ©2012-2019, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of version 3 of the GNU Lesser General Public License
- * or later as published by the Free Software Foundation.
- *
+ * Sixth 3D engine. Author: Svjatoslav Agejenko. 
+ * This project is released under Creative Commons Zero (CC0) license.
  */
-
 package eu.svjatoslav.sixth.e3d.gui;
 
 import eu.svjatoslav.sixth.e3d.gui.humaninput.MouseEvent;
@@ -33,11 +28,11 @@ public class RenderingContext {
     /**
      * UI component that mouse is currently hovering over.
      */
-    private MouseInteractionController currentMouseOverComponent;
+    private MouseInteractionController objectPreviouslyUnderMouseCursor;
 
     public void prepareForNewFrameRendering(){
         mouseEvent = null;
-        objectUnderMouse = null;
+        currentObjectUnderMouseCursor = null;
     }
 
     /**
@@ -56,15 +51,15 @@ public class RenderingContext {
     /**
      * Item that user clicked on.
      */
-    private MouseInteractionController objectUnderMouse;
+    private MouseInteractionController currentObjectUnderMouseCursor;
 
     /**
      * Called when given object was detected under mouse cursor, while processing {@link #mouseEvent}.
      * Because objects are rendered back to front. The last method caller will set the top-most object, if
      * there are multiple objects under mouse cursor.
      */
-    public void setObjectUnderMouse(MouseInteractionController objectUnderMouse) {
-        this.objectUnderMouse = objectUnderMouse;
+    public void setCurrentObjectUnderMouseCursor(MouseInteractionController currentObjectUnderMouseCursor) {
+        this.currentObjectUnderMouseCursor = currentObjectUnderMouseCursor;
     }
 
     public RenderingContext(final int width, final int height) {
@@ -90,28 +85,24 @@ public class RenderingContext {
     /**
      * @return <code>true</code> if view repaint is needed.
      */
-    public boolean handleDetectedComponentMouseEvents() {
-        if (objectUnderMouse != null) {
-            if (mouseEvent.button == 0) {
-                // mouse over
-                if (currentMouseOverComponent == null) {
-                    currentMouseOverComponent = objectUnderMouse;
-                    return currentMouseOverComponent.mouseEntered();
-                } else if (currentMouseOverComponent != objectUnderMouse) {
-                    boolean viewRepaintNeeded = currentMouseOverComponent.mouseExited();
-                    currentMouseOverComponent = objectUnderMouse;
-                    return viewRepaintNeeded | currentMouseOverComponent.mouseEntered();
-                }
-            } else {
-                // mouse click
-                return objectUnderMouse.mouseClicked();
-            }
-        } else if (currentMouseOverComponent != null) {
-            boolean viewRepaintNeeded = currentMouseOverComponent.mouseExited();
-            currentMouseOverComponent = null;
-            return viewRepaintNeeded;
+    public boolean handlePossibleComponentMouseEvent() {
+        if (mouseEvent == null) return false;
+
+        boolean viewRepaintNeeded = false;
+
+        if (objectPreviouslyUnderMouseCursor != currentObjectUnderMouseCursor) {
+            // Mouse cursor has just entered or left component.
+            viewRepaintNeeded = objectPreviouslyUnderMouseCursor != null && objectPreviouslyUnderMouseCursor.mouseExited();
+            viewRepaintNeeded |= currentObjectUnderMouseCursor != null && currentObjectUnderMouseCursor.mouseEntered();
+            objectPreviouslyUnderMouseCursor = currentObjectUnderMouseCursor;
         }
-        return false;
+
+        if (mouseEvent.button != 0 && currentObjectUnderMouseCursor != null) {
+            // Mouse button was clicked on some component.
+            viewRepaintNeeded |= currentObjectUnderMouseCursor.mouseClicked(mouseEvent.button);
+        }
+
+        return viewRepaintNeeded;
     }
 
 }