下面列出了javafx.scene.input.ScrollEvent#SCROLL_STARTED 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void sendScrollEventToFX(EventType<ScrollEvent> eventType,
double scrollX, double scrollY, int x, int y, int stateMask,
boolean inertia) {
// up to and including SWT 4.5, direction was inverted for pan
// gestures on the Mac
// (see https://bugs.eclipse.org/bugs/show_bug.cgi?id=481331)
final double multiplier = ("cocoa".equals(SWT.getPlatform())
&& SWT.getVersion() < 4600) ? -5.0 : 5.0;
if (eventType == ScrollEvent.SCROLL_STARTED) {
totalScrollX = 0;
totalScrollY = 0;
} else if (inertia) {
// inertia events do not belong to the gesture,
// thus total scroll is not accumulated
totalScrollX = scrollX;
totalScrollY = scrollY;
} else {
// accumulate total scroll as long as the gesture occurs
totalScrollX += scrollX;
totalScrollY += scrollY;
}
final Point los = toDisplay(x, y);
scheduleSceneRunnable(new ISceneRunnable() {
@Override
public void run(TKSceneListenerWrapper sceneListener) {
sceneListener.scrollEvent(eventType, scrollX, scrollY,
totalScrollX, totalScrollY, multiplier, multiplier,
0, 0, 0, 0, 0, x, y, los.x, los.y,
(stateMask & SWT.SHIFT) != 0,
(stateMask & SWT.CONTROL) != 0,
(stateMask & SWT.ALT) != 0,
(stateMask & SWT.COMMAND) != 0, false, inertia);
}
});
}
@Override
public void handle( ScrollEvent event ) {
EventType<? extends Event> eventType = event.getEventType();
if ( eventType == ScrollEvent.SCROLL_STARTED ) {
//mouse wheel events never send SCROLL_STARTED
ignoring = true;
} else if ( eventType == ScrollEvent.SCROLL_FINISHED ) {
//end non-mouse wheel event
ignoring = false;
} else if ( eventType == ScrollEvent.SCROLL &&
//If we are allowing mouse wheel zooming
mouseWheelZoomAllowed.get() &&
//If we aren't between SCROLL_STARTED and SCROLL_FINISHED
!ignoring &&
//inertia from non-wheel gestures might have touch count of 0
!event.isInertia() &&
//Only care about vertical wheel events
event.getDeltaY() != 0 &&
//mouse wheel always has touch count of 0
event.getTouchCount() == 0 ) {
//Find out which axes to zoom based on the strategy
double eventX = event.getX();
double eventY = event.getY();
DefaultChartInputContext context = new DefaultChartInputContext( chartInfo, eventX, eventY );
AxisConstraint zoomMode = mouseWheelAxisConstraintStrategy.getConstraint( context );
if ( zoomMode == AxisConstraint.None )
return;
//If we are are doing a zoom animation, stop it. Also of note is that we don't zoom the
//mouse wheel zooming. Because the mouse wheel can "fly" and generate a lot of events,
//animation doesn't work well. Plus, as the mouse wheel changes the view a small amount in
//a predictable way, it "looks like" an animation when you roll it.
//We might experiment with mouse wheel zoom animation in the future, though.
zoomAnimation.stop();
//At this point we are a mouse wheel event, based on everything I've read
Point2D dataCoords = chartInfo.getDataCoordinates( eventX, eventY );
//Determine the proportion of change to the lower and upper bounds based on how far the
//cursor is along the axis.
double xZoomBalance = getBalance( dataCoords.getX(),
getXAxisLowerBound(), getXAxisUpperBound() );
double yZoomBalance = getBalance( dataCoords.getY(),
getYAxisLowerBound(), getYAxisUpperBound() );
//Are we zooming in or out, based on the direction of the roll
double direction = -Math.signum( event.getDeltaY() );
//TODO: Do we need to handle "continuous" scroll wheels that don't work based on ticks?
//If so, the 0.2 needs to be modified
double zoomAmount = 0.2 * direction;
if ( zoomMode == AxisConstraint.Both || zoomMode == AxisConstraint.Horizontal ) {
double xZoomDelta = ( getXAxisUpperBound() - getXAxisLowerBound() ) * zoomAmount;
xAxis.setAutoRanging( false );
setXAxisLowerBound( getXAxisLowerBound() - xZoomDelta * xZoomBalance );
setXAxisUpperBound( getXAxisUpperBound() + xZoomDelta * ( 1 - xZoomBalance ) );
}
if ( zoomMode == AxisConstraint.Both || zoomMode == AxisConstraint.Vertical ) {
double yZoomDelta = ( getYAxisUpperBound() - getYAxisLowerBound() ) * zoomAmount;
yAxis.setAutoRanging( false );
setYAxisLowerBound( getYAxisLowerBound() - yZoomDelta * yZoomBalance );
setYAxisUpperBound( getYAxisUpperBound() + yZoomDelta * ( 1 - yZoomBalance ) );
}
}
}