forked from j62/ctbrec
1
0
Fork 0

Add mouse click behaviour to range slider

This commit is contained in:
0xboobface 2020-06-30 20:28:44 +02:00
parent 3382c7ff54
commit 4f52259aa9
2 changed files with 38 additions and 12 deletions

View File

@ -5,6 +5,9 @@ import java.util.List;
import com.sun.javafx.scene.control.behavior.BehaviorBase; // NOSONAR
import com.sun.javafx.scene.control.inputmap.InputMap; // NOSONAR
import javafx.scene.Node;
import javafx.scene.input.MouseEvent;
public class RangeSliderBehavior<T extends Number> extends BehaviorBase<RangeSlider<T>> {
private RangeSlider<T> rangeSlider;
@ -12,6 +15,13 @@ public class RangeSliderBehavior<T extends Number> extends BehaviorBase<RangeSli
public RangeSliderBehavior(RangeSlider<T> rangeSlider) {
super(rangeSlider);
this.rangeSlider = rangeSlider;
rangeSlider.addEventFilter(MouseEvent.MOUSE_CLICKED, this::sliderClicked);
}
private void sliderClicked(MouseEvent me) {
Node source = (Node) me.getSource();
double positionPercentage = me.getX() / source.getBoundsInParent().getWidth();
moveClosestThumbTo(positionPercentage);
}
@Override
@ -26,9 +36,9 @@ public class RangeSliderBehavior<T extends Number> extends BehaviorBase<RangeSli
*/
public void lowThumbDragged(double position) {
T newPosition = getNewPosition(position);
T high = rangeSlider.getRange().getTicks().get(rangeSlider.getHigh().intValue());
T high = getHigh();
if (newPosition.doubleValue() >= high.doubleValue()) {
newPosition = rangeSlider.getRange().getTicks().get(rangeSlider.getLow().intValue());
newPosition = getLow();
}
rangeSlider.setLow(newPosition);
}
@ -39,13 +49,34 @@ public class RangeSliderBehavior<T extends Number> extends BehaviorBase<RangeSli
*/
public void highThumbDragged(double position) {
T newPosition = getNewPosition(position);
T low = rangeSlider.getRange().getTicks().get(rangeSlider.getLow().intValue());
T low = getLow();
if (newPosition.doubleValue() <= low.doubleValue()) {
newPosition = rangeSlider.getRange().getTicks().get(rangeSlider.getHigh().intValue());
newPosition = getHigh();
}
rangeSlider.setHigh(newPosition);
}
public void moveClosestThumbTo(double positionPercentage) {
T newPosition = getNewPosition(positionPercentage);
T low = getLow();
T high = getHigh();
double distToLow = Math.abs(low.doubleValue() - newPosition.doubleValue());
double distToHigh = Math.abs(high.doubleValue() - newPosition.doubleValue());
if (distToLow < distToHigh) {
rangeSlider.setLow(newPosition);
} else {
rangeSlider.setHigh(newPosition);
}
}
private T getLow() {
return rangeSlider.getRange().getTicks().get(rangeSlider.getLow().intValue());
}
private T getHigh() {
return rangeSlider.getRange().getTicks().get(rangeSlider.getHigh().intValue());
}
/**
* Calculate the new position of the thumb given the clicked/dragged position
*
@ -60,4 +91,5 @@ public class RangeSliderBehavior<T extends Number> extends BehaviorBase<RangeSli
index = Math.min(ticks.size() - 1, Math.max(0, index));
return ticks.get(index);
}
}

View File

@ -31,8 +31,8 @@ public class RangeSliderSkin extends SkinBase<RangeSlider<?>> {
this.behavior = behavior;
initTrack();
initThumbs(thumbRange);
registerChangeListener(control.getLow(), (obsVal) -> getSkinnable().requestLayout());
registerChangeListener(control.getHigh(), (obsVal) -> getSkinnable().requestLayout());
registerChangeListener(control.getLow(), obsVal -> getSkinnable().requestLayout());
registerChangeListener(control.getHigh(), obsVal -> getSkinnable().requestLayout());
}
private void initThumbs(ThumbRange t) {
@ -44,8 +44,6 @@ public class RangeSliderSkin extends SkinBase<RangeSlider<?>> {
preDragThumbPoint = t.low.localToParent(me.getX(), me.getY());
preDragPos = (getSkinnable().getLow().doubleValue() - getSkinnable().getMinimum().doubleValue()) / (getMaxMinusMinNoZero());
});
t.low.setOnMouseDragged(me -> {
Point2D cur = t.low.localToParent(me.getX(), me.getY());
double dragPos = (isHorizontal()) ? cur.getX() - preDragThumbPoint.getX() : -(cur.getY() - preDragThumbPoint.getY());
@ -56,15 +54,12 @@ public class RangeSliderSkin extends SkinBase<RangeSlider<?>> {
preDragThumbPoint = t.high.localToParent(me.getX(), me.getY());
preDragPos = (getSkinnable().getHigh().doubleValue() - getSkinnable().getMinimum().doubleValue()) / (getMaxMinusMinNoZero());
});
t.high.setOnMouseDragged(me -> {
boolean orientation = getSkinnable().getOrientation() == Orientation.HORIZONTAL;
double trackLen = orientation ? track.getWidth() : track.getHeight();
Point2D cur = t.high.localToParent(me.getX(), me.getY());
double dragPos = getSkinnable().getOrientation() != Orientation.HORIZONTAL ? -(cur.getY() - preDragThumbPoint.getY()) : cur.getX() - preDragThumbPoint.getX();
behavior.highThumbDragged(preDragPos + dragPos / trackLen);
});
}
@ -208,7 +203,6 @@ public class RangeSliderSkin extends SkinBase<RangeSlider<?>> {
if (isHorizontal()) {
if (showTickMarks) {
double w = Math.max(140, tickLine.prefWidth(-1));
System.err.println("computePrefWidth " + w);
return w;
} else {
return 140;