
net.vectorpublish.desktop.vp.pd.official.TechnicalMouseDrag Maven / Gradle / Ivy
/*
* Copyright (c) 2016, Peter Rader. All rights reserved.
* ___ ___ __ ______ __ __ __ __
* | | |.-----..----.| |_ .-----..----.| __ \.--.--.| |--.| ||__|.-----.| |--.
* | | || -__|| __|| _|| _ || _|| __/| | || _ || || ||__ --|| |
* \_____/ |_____||____||____||_____||__| |___| |_____||_____||__||__||_____||__|__|
*
* http://www.gnu.org/licenses/gpl-3.0.html
*/
package net.vectorpublish.desktop.vp.pd.official;
import java.awt.Component;
import java.awt.event.MouseEvent;
import net.vectorpublish.desktop.vp.api.ui.MouseParticipant;
/**
* The dragging of the mouse.
*
* This instance and the starting position will be the same over the whole
* drag-process but the position will change.
*/
public class TechnicalMouseDrag {
/**
* The informations about when and where the mouse-drag starts.
*/
private final TechnicalMouseDown mouseDown;
/**
* The current x-axis position of the mouse.
*/
private int currentTechnicalX;
/**
* The current y-axis position of the mouse.
*/
private int currentTechnicalY;
/**
* The information about shift hold.
*/
private boolean shiftDown;
private boolean altDown;
private boolean controlDown;
private MouseParticipant handler;
/**
* Default constructor, requires a concrete mouse-position and the
* {@link TechnicalMouseDown} the draging actually started.
*
* @param mouseDown
* The mouse-down for this drag.
* @param technicalEvent
* The technical event to get the keys and position.
*/
public TechnicalMouseDrag(TechnicalMouseDown mouseDown, MouseEvent technicalEvent) {
this.mouseDown = mouseDown;
currentTechnicalX = technicalEvent.getX();
currentTechnicalY = technicalEvent.getY();
controlDown = technicalEvent.isControlDown();
shiftDown = technicalEvent.isShiftDown();
updateAltKey(technicalEvent.isAltDown());
}
/**
* Returns the current position on the x-axis of the mouse-cursor.
*
* This is relative to the {@link Component}.
*
* @return The current x-position.
*/
public int getCurrentTechnicalX() {
return currentTechnicalX;
}
/**
* Returns the current position on the y-axis of the mouse-cursor.
*
* This is relative to the {@link Component}.
*
* @return The current y-position.
*/
public int getCurrentTechnicalY() {
return currentTechnicalY;
}
/**
* Get the degrees from the starting position.
*
* 0 means the east-poision of the mouse. The degrees increment in clockwise
* order. There is no 360 degrees, use 0 for it.
*
* @return The degrees the mouse has moved since start dragging.
*/
public double getDegreeFromStart() {
return Math.toDegrees(Math.atan2(getTechnicalDistanceX(), getTechnicalDistanceY()));
}
/**
* Get the distance from the starting position.
*
*
* Can be 0
, a negative value is not possible.
*
* @return The distance from the start position.
*/
public int getTechnicalDistance() {
return (int) Math.sqrt(Math.pow(getTechnicalDistanceX(), 2) + Math.pow(getTechnicalDistanceY(), 2));
}
/**
* The distance from the starting position on the x-axis.
*
* A negative value means that the mouse is on the left side.
*
* @return The distance on the x-axis, a negative value means the mouse has
* moved to the left side.
*/
public int getTechnicalDistanceX() {
return currentTechnicalX - getTechnicalMouseDown().getTechnicalStartX();
}
/**
* The distance from the starting position on the y-axis.
*
* A negative value means that the mouse is on the upper side.
*
* @return The distance on the x-axis, a negative value means the mouse has
* moved up.
*/
public int getTechnicalDistanceY() {
return currentTechnicalY - getTechnicalMouseDown().getTechnicalStartY();
}
/**
* Returns the technical mouse-down that is responsible for the drag.
*
* @return The mousedown responsible for the drag.
*/
public TechnicalMouseDown getTechnicalMouseDown() {
return mouseDown;
}
/**
* Returns if a handler already has been attached to this event.
*
* @return true
if a handler has been attached.
*/
public boolean hasHandler() {
return handler != null;
}
/**
* Returns if the mouse-event has the same values as this instance.
*
* This happens if either the mouse has moved too fast to the old position
* or when the user pressed a different button.
*
* @param event
* The event actualy incomming.
* @return true
if the position has not been changed,
* false
if the actual position is different from this
* actual values.
*/
public boolean hasSameTechnicalPositionAndKeys(MouseEvent event) {
final boolean buttonsUnChanged = event.isShiftDown() == shiftDown & event.isAltDown() == altDown & event.isControlDown() == controlDown;
return buttonsUnChanged & event.getX() == currentTechnicalX & event.getY() == currentTechnicalY;
}
/**
* Returns if alt-key is down.
*
* @return true
if the alt-key is down, false
if
* not.
*/
public boolean isAltDown() {
return altDown;
}
/**
* Returns if the ctrl-key is down.
*
* @return true
if the ctrl-key is down, false
if
* not.
*/
public boolean isControlDown() {
return controlDown;
}
/**
* Checks if the participant is the registered handler.
*
* @param targetParticipant
* The participant.
* @return true
if the participant is the handler
* @throws NullPointerException
* If the argument is null
.
*/
public boolean isHandler(MouseParticipant targetParticipant) throws NullPointerException {
if (targetParticipant == null) {
throw new NullPointerException();
}
return targetParticipant == handler;
}
/**
* Returns if the shift-key is down.
*
*
* @return true
if the shift-key is down, false
if
* not.
*/
public boolean isShiftDown() {
return shiftDown;
}
/**
* Sets the handler for this dragging.
*
* If a handler already set to the dragging its detached and the new handler
* is attached.
*
* @param participant
* The participant.
*/
public void setHandler(MouseParticipant participant) {
this.handler = participant;
}
@Override
public String toString() {
final String desc = "[" + this.mouseDown + " to " + currentTechnicalX + "x" + currentTechnicalY + "]";
return TechnicalMouseDrag.class.getSimpleName() + "@" + Integer.toHexString(hashCode()) + desc;
}
/**
* Updates the status of the alt-key.
*
* @param altDown
* The status of the alt-key,
*/
public void updateAltKey(boolean altDown) {
this.altDown = altDown;
}
/**
* Updates the status of the ctrl-key.
*
* @param ctrlDown
* true
if the key has been pressed,
* false
if the key has released.
*/
public void updateCtrlKey(boolean ctrlDown) {
controlDown = ctrlDown;
}
/**
* Updates the status of the shift-key.
*
* @param shiftDown
* true
if the shift-key has been pressed,
* false
if its released.
*/
public void updateShiftKey(boolean shiftDown) {
this.shiftDown = shiftDown;
}
/**
* Updates the position of the dragging. Also updates the keys that are
* pressed durring dragging.
*
* @param event
* The event responsible for the position-change.
*/
public void updateTechnicalPositionAndKeys(MouseEvent event) {
currentTechnicalX = event.getX();
currentTechnicalY = event.getY();
shiftDown = event.isShiftDown();
controlDown = event.isControlDown();
altDown = event.isAltDown();
}
}