public class SnapContext extends Object
This class should be used by IInputMode
implementations and similar code as follows:
initializeDrag(IInputModeContext, PointD)
needs to be called.dragInitialized()
needs to be called after the mode has performed all of its own initialization.handleMove(PointD, boolean)
needs to be called for each incremental
movement of the user's input device.
handleMove(PointD, boolean)
has been called, the mode should perform the move and report back to the
instance by calling the dragged(PointD, SnapState)
method.
cancelDrag()
should be called.dragFinished(PointD, boolean)
method should be called.Constructor and Description |
---|
SnapContext()
Initializes a new instance of the
SnapContext class. |
Modifier and Type | Method and Description |
---|---|
void |
addCleanedUpListener(IEventHandler<InputModeEventArgs> cleanedUpEvent)
Adds the given listener for the
CleanedUp event that occurs when this instance has been cleaned up. |
void |
addCollectSnapResultsListener(IEventHandler<CollectSnapResultsEventArgs> collectSnapResultsEvent)
Adds the given listener for the
CollectSnapResults event that occurs while moving
items and handles. |
void |
addInitializedListener(IEventHandler<InputModeEventArgs> initializedEvent)
Adds the given listener for the
Initialized event that occurs when this instance has been initialized. |
void |
addInitializingListener(IEventHandler<InputModeEventArgs> initializingEvent)
Adds the given listener for the
Initializing event that occurs when this instance is about to be initialized. |
void |
cancelDrag()
Called when a drag has been canceled.
|
protected void |
cleanUp()
Performs clean up procedures.
|
ICanvasObjectDescriptor |
createSnapResultCanvasObjectDescriptor()
Helper method used by
createSnapResultsModelManager(CanvasControl) to create the ICanvasObjectDescriptor
for the SnapResult type. |
CollectionModelManager<SnapResult> |
createSnapResultsModelManager(CanvasControl canvas)
Helper method that creates the a
CollectionModelManager that can be used to present the current
SnapResult s in the provided CanvasControl . |
SnapState |
dragFinished(PointD newLocation,
boolean snappingDisabled)
Called when a drag has been successfully finished.
|
void |
dragged(PointD mouseLocation,
SnapState finalSnapState)
This method needs to be called by the client
IInputMode that initialized
this instance after it has performed the actual move operation with the adjusted coordinates after a call to
handleMove(PointD, boolean) . |
void |
dragInitialized()
Collects a list of snap lines.
|
protected IInputModeContext |
getCurrentInputModeContext()
Gets the current input mode context.
|
PointD |
getOriginalLocation()
Gets the original location of the mouse at the time the gesture was initialized.
|
double |
getSnapDistance()
Gets the global snap distance, which is the maximum distance between the current mouse coordinates and the coordinates
to which the mouse will snap.
|
IObservableCollection<SnapResult> |
getSnapResults()
The collection of
SnapResult s. |
SnapState |
handleMove(PointD newLocation,
boolean snappingDisabled)
Handles a move.
|
void |
initializeDrag(IInputModeContext context,
PointD originalLocation)
Called by
IInputMode implementations when an interactive drag is started. |
boolean |
isEnabled()
Gets a value indicating whether this
SnapContext is enabled. |
boolean |
isInitialized()
Whether the context is initialized.
|
boolean |
isInitializing()
Gets a value indicating whether this instance is currently initializing.
|
boolean |
isSnapResultVisualizationEnabled()
Gets a value indicating whether to visualize the snap results.
|
protected void |
onCleanUp(InputModeEventArgs eventArgs)
Raises the
cleanUp() event. |
protected void |
onCollectSnapResults(CollectSnapResultsEventArgs args)
Raises the
CollectSnapResults event. |
protected void |
onInitialized(InputModeEventArgs eventArgs)
Raises the
Initialized event. |
protected void |
onInitializing(InputModeEventArgs eventArgs)
Raises the
Initializing event. |
protected SnapState |
processSnapResults(ArrayList<SnapResult> snapResults,
PointD originalLocation,
PointD newLocation)
Chooses the current
SnapResult s from a temporary list. |
void |
removeCleanedUpListener(IEventHandler<InputModeEventArgs> cleanedUpEvent)
Removes the given listener for the
CleanedUp event that occurs when this instance has been cleaned up. |
void |
removeCollectSnapResultsListener(IEventHandler<CollectSnapResultsEventArgs> collectSnapResultsEvent)
Removes the given listener for the
CollectSnapResults event that occurs while moving
items and handles. |
void |
removeInitializedListener(IEventHandler<InputModeEventArgs> initializedEvent)
Removes the given listener for the
Initialized event that occurs when this instance has been initialized. |
void |
removeInitializingListener(IEventHandler<InputModeEventArgs> initializingEvent)
Removes the given listener for the
Initializing event that occurs when this instance is about to be
initialized. |
void |
setEnabled(boolean value)
Sets a value indicating whether this
SnapContext is enabled. |
void |
setSnapDistance(double value)
Sets the global snap distance, which is the maximum distance between the current mouse coordinates and the coordinates
to which the mouse will snap.
|
void |
setSnapResultVisualizationEnabled(boolean value)
Sets a value indicating whether to visualize the snap results.
|
IInputModeContext |
wrapContext(IInputModeContext context)
Helper method that wraps the given
context so that a ILookup.lookup(Class) query
on the wrapped context for the SnapContext type yields this instance. |
public SnapContext()
SnapContext
class.public final void addCleanedUpListener(IEventHandler<InputModeEventArgs> cleanedUpEvent)
CleanedUp
event that occurs when this instance has been cleaned up.cleanedUpEvent
- The listener to add.cleanUp()
,
removeCleanedUpListener(IEventHandler)
public final void addCollectSnapResultsListener(IEventHandler<CollectSnapResultsEventArgs> collectSnapResultsEvent)
CollectSnapResults
event that occurs while moving
items and handles.
The moving model items must register for this event, preferably in the IDragHandler.initializeDrag(IInputModeContext)
method of the IDragHandler
.
The event handler has to add SnapResult
s for the moving object to the list. The
IDragHandler
s of the moved items have to take care themselves whether to add a
handler to the list depending on the their settings.
collectSnapResultsEvent
- The listener to add.removeCollectSnapResultsListener(IEventHandler)
public final void addInitializedListener(IEventHandler<InputModeEventArgs> initializedEvent)
Initialized
event that occurs when this instance has been initialized.initializedEvent
- The listener to add.initializeDrag(IInputModeContext, PointD)
,
removeInitializedListener(IEventHandler)
public final void addInitializingListener(IEventHandler<InputModeEventArgs> initializingEvent)
Initializing
event that occurs when this instance is about to be initialized.initializingEvent
- The listener to add.initializeDrag(IInputModeContext, PointD)
,
removeInitializingListener(IEventHandler)
public void cancelDrag()
Clears all temporary lists and sets IsInitialized
back to false, as well as performs cleanUp()
and finally triggers the CleanedUp
event.
protected void cleanUp()
This is called in response to cancelDrag()
and dragFinished(PointD, boolean)
as well as initially
during initializeDrag(IInputModeContext, PointD)
.
public ICanvasObjectDescriptor createSnapResultCanvasObjectDescriptor()
createSnapResultsModelManager(CanvasControl)
to create the ICanvasObjectDescriptor
for the SnapResult
type.SnapResult
s in a CanvasControl
.public CollectionModelManager<SnapResult> createSnapResultsModelManager(CanvasControl canvas)
CollectionModelManager
that can be used to present the current
SnapResult
s in the provided CanvasControl
.canvas
- The control to initialize the CollectionModelManager
with.SnapResult
s.public SnapState dragFinished(PointD newLocation, boolean snappingDisabled)
Clears all temporary lists and sets IsInitialized
back to false. Also the cleanUp()
method is invoked and the CleanedUp
event is triggered.
newLocation
- The current mouse location.snappingDisabled
- If set to true
snapping is temporarily disabled. This will remove the snap line visualizations and return a SnapState
with the unchanged coordinates and the SnapTypes.NOT_SNAPPED
SnapType
.SnapState
that specifies the adjusted coordinates and how they have been adjusted. The unchanged coordinates
if snapping has been temporarily disabled by setting snappingDisabled
to true
.public void dragged(PointD mouseLocation, SnapState finalSnapState)
IInputMode
that initialized
this instance after it has performed the actual move operation with the adjusted coordinates after a call to
handleMove(PointD, boolean)
.
Most of the client code implementations will just call this method with the exact values returned by
handleMove(PointD, boolean)
. This method does not need to be called if snapping has been temporarily
disabled by calling handleMove(PointD, boolean)
with its snappingDisabled
parameter set to
true
.
mouseLocation
- The actual mouse location.finalSnapState
- The final snap state that contains the location that has been used by the client code for the move and how it has been
snapped.public void dragInitialized()
Snap lines represent possible horizontal or vertical lines to which a moved item can "snap".
This method is called at the start of MoveInputMode
's or
HandleInputMode
's gesture after all items to be moved or reshaped have been
added.
protected final IInputModeContext getCurrentInputModeContext()
IsInitialized
or
IsInitializing
, otherwise null
.public final PointD getOriginalLocation()
initializeDrag(IInputModeContext, PointD)
public final double getSnapDistance()
The distance is interpreted in view coordinates.
The default is 5.0d
CollectSnapResultsEventArgs.getSnapDistance()
,
setSnapDistance(double)
public final IObservableCollection<SnapResult> getSnapResults()
SnapResult
s.
This collection is automatically updated during moving the mouse.
public SnapState handleMove(PointD newLocation, boolean snappingDisabled)
This method returns the adjusted (mouse) coordinates. It also manages the collection of SnapResult
s. If snapping
should be temporarily (for a mouse move) disabled, snappingDisabled
has to be set to true
.
newLocation
- The current mouse location.snappingDisabled
- If set to true
snapping is temporarily disabled. This will remove the snap line visualizations and return a SnapState
with the unchanged coordinates and the SnapType
SnapTypes.NOT_SNAPPED
.snappingDisabled
to true
.public void initializeDrag(IInputModeContext context, PointD originalLocation)
IInputMode
implementations when an interactive drag is started.
Updates IsInitializing
, OriginalLocation
, and CurrentInputModeContext
and then triggers the Initializing
event.
IllegalStateException
- If this context is already initialized
.context
- The context in which the interactive drag is started.originalLocation
- The original location of the mouse.public final boolean isEnabled()
SnapContext
is enabled.true
if enabled; false
otherwise.setEnabled(boolean)
public final boolean isInitialized()
The context is initialized between the calls to dragInitialized()
and dragFinished(PointD, boolean)
or
cancelDrag()
.
public final boolean isInitializing()
true
if this instance is initializing; false
otherwise.addInitializedListener(IEventHandler)
,
addInitializingListener(IEventHandler)
,
isInitialized()
public final boolean isSnapResultVisualizationEnabled()
true
if the results should be visualized false
otherwise.createSnapResultsModelManager(CanvasControl)
,
setSnapResultVisualizationEnabled(boolean)
protected void onCleanUp(InputModeEventArgs eventArgs)
cleanUp()
event.eventArgs
- The InputModeEventArgs
instance containing the event data.protected void onCollectSnapResults(CollectSnapResultsEventArgs args)
CollectSnapResults
event.args
- The CollectSnapResultsEventArgs
instance containing the event data.protected void onInitialized(InputModeEventArgs eventArgs)
Initialized
event.eventArgs
- The InputModeEventArgs
instance containing the event data.protected void onInitializing(InputModeEventArgs eventArgs)
Initializing
event.eventArgs
- The InputModeEventArgs
instance containing the event data.protected SnapState processSnapResults(ArrayList<SnapResult> snapResults, PointD originalLocation, PointD newLocation)
SnapResult
s from a temporary list.
Chooses from a list of possible results the most important ones. Also maintains the collection of snap results.
snapResults
- A temporary list of suggested SnapResult
s.originalLocation
- The point in world coordinates where the mouse move started.newLocation
- The current mouse location.public final void removeCleanedUpListener(IEventHandler<InputModeEventArgs> cleanedUpEvent)
CleanedUp
event that occurs when this instance has been cleaned up.cleanedUpEvent
- The listener to remove.cleanUp()
,
addCleanedUpListener(IEventHandler)
public final void removeCollectSnapResultsListener(IEventHandler<CollectSnapResultsEventArgs> collectSnapResultsEvent)
CollectSnapResults
event that occurs while moving
items and handles.
The moving model items must register for this event, preferably in the IDragHandler.initializeDrag(IInputModeContext)
method of the IDragHandler
.
The event handler has to add SnapResult
s for the moving object to the list. The
IDragHandler
s of the moved items have to take care themselves whether to add a
handler to the list depending on the their settings.
collectSnapResultsEvent
- The listener to remove.addCollectSnapResultsListener(IEventHandler)
public final void removeInitializedListener(IEventHandler<InputModeEventArgs> initializedEvent)
Initialized
event that occurs when this instance has been initialized.initializedEvent
- The listener to remove.initializeDrag(IInputModeContext, PointD)
,
addInitializedListener(IEventHandler)
public final void removeInitializingListener(IEventHandler<InputModeEventArgs> initializingEvent)
Initializing
event that occurs when this instance is about to be
initialized.initializingEvent
- The listener to remove.initializeDrag(IInputModeContext, PointD)
,
addInitializingListener(IEventHandler)
public final void setEnabled(boolean value)
SnapContext
is enabled.value
- true
if enabled; false
otherwise.isEnabled()
public final void setSnapDistance(double value)
The distance is interpreted in view coordinates.
The default is 5.0d
value
- The SnapDistance to set.CollectSnapResultsEventArgs.getSnapDistance()
,
getSnapDistance()
public final void setSnapResultVisualizationEnabled(boolean value)
value
- true
if the results should be visualized false
otherwise.createSnapResultsModelManager(CanvasControl)
,
isSnapResultVisualizationEnabled()
public IInputModeContext wrapContext(IInputModeContext context)
context
so that a ILookup.lookup(Class)
query
on the wrapped context for the SnapContext
type yields this instance.context
- The context to wrap and delegate all calls to.SnapContext
type.