Class SnapMouseOwner

All Implemented Interfaces:
EventEmitterInterface, MouseListener, MouseMotionListener, MouseWheelListener, EventListener
Direct Known Subclasses:
TransformMouseOwner

public class SnapMouseOwner extends MouseOwner
A MouseOwner which enables child classes which extend it, the capability to snap to GObjects.

This class:

  • Detects nearby GObjects (points and midpoints of lines) within a small radius of the mouse cursor.
  • Highlights these potential snap targets visually in the scene.
  • Broadcasts a EventType.SNAP_TO_OBJ event when the user right-clicks on a highlighted snap target.
Subclasses can extend this to enable snapping behaviour for their specific mouse interactions, such as transforming objects to align with other objects in the scene.
Author:
Lehlogonolo Poole
See Also:
  • Field Details

    • offset

      public static int offset
      The radius (in pixels) around the mouse cursor within which GObjects are considered potential snap targets.
    • snappingCandidates

      protected ArrayList<GObject> snappingCandidates
      A list of GObjects that are currently within the snapping region around the mouse cursor. These are potential targets for snapping.
    • hoveringOver

      private GObject hoveringOver
      The GObject that the mouse is currently hovering over within the snapping region. This object will be the target if a snap event is triggered.
    • uuid

      private final UUID uuid
      A unique identifier for this SnapMouseOwner instance, used for managing overlaps with the StaticRefs.getSceneManager().
    • enabled

      private boolean enabled
      A flag indicating whether the snapping functionality is currently enabled.
    • overlapScheduled

      private boolean overlapScheduled
      A flag indicating whether the overlap consumer has been scheduled with the scene manager.
    • mouseLoc

      private ScreenPoint mouseLoc
      The current screen coordinates of the mouse cursor.
  • Constructor Details

    • SnapMouseOwner

      public SnapMouseOwner(MOwner owner, UUID uuid)
      Constructs a new SnapMouseOwner.
      Parameters:
      owner - The owner of this mouse interaction.
      uuid - A unique identifier for this instance, used for managing overlaps with the SceneManager.
  • Method Details

    • consumer

      private Consumer<Graphics2D> consumer()
      Returns a Consumer<Graphics2D> that handles the visual representation of snapping candidates. This consumer is scheduled with the SceneManager to draw overlays, identifying and highlighting potential snap targets (points and midpoints of lines) within the snapping region.
      Returns:
      A Consumer that draws the snapping overlays.
    • mouseClicked

      public void mouseClicked(MouseEvent e)
      Handles mouse click events. If a right-click occurs on a highlighted snap target, it broadcasts a EventType.SNAP_TO_OBJ event with the hoveringOver object.
      Specified by:
      mouseClicked in interface MouseListener
      Overrides:
      mouseClicked in class MouseAdapter
      Parameters:
      e - The MouseEvent generated by the click.
    • isWithinRegion

      private boolean isWithinRegion(ScreenPoint target)
      Checks if a given ScreenPoint is within the current snapping region defined by mouseLoc and offset.
      Parameters:
      target - The ScreenPoint to check.
      Returns:
      true if the target is within the region, false otherwise.
    • snapEnabled

      public void snapEnabled()
      Enables the snapping functionality for this SnapMouseOwner. Subclasses must call this method to activate snapping.
    • getMouseLocation

      public ScreenPoint getMouseLocation()
    • mouseMoved

      public void mouseMoved(MouseEvent e)
      Handles mouse movement events. When the mouse moves, it updates the mouseLoc, clears previous snapping candidates, and queries the SelectionManager for new potential snap targets within the snapping region. It also schedules the overlap consumer if not already scheduled.
      Specified by:
      mouseMoved in interface MouseMotionListener
      Overrides:
      mouseMoved in class MouseOwner
      Parameters:
      e - The MouseEvent generated by the mouse movement.
    • clear

      public void clear()
      Clears all snapping-related state, including removing the overlap consumer from the SceneManager, clearing snapping candidates, and resetting the hoveringOver object. This should be called when snapping is no longer needed.
      Overrides:
      clear in class MouseOwner
    • getSnappingCandidates

      public ArrayList<GObject> getSnappingCandidates()
      Returns the list of GObjects that are currently considered potential snapping candidates.
      Returns:
      An ArrayList of GObjects.