final Slider width = new Slider(100, 100, 5000);
final Slider height = new Slider(100, 100, 5000);
final Slider xpos = new Slider(0, 0, 1);
final Slider ypos = new Slider(0, 0, 1);
final Content content = new Content();
final Scroller scroll = new Scroller(content);
final Label click = new Label();
// updates the size of the content
final UnitSlot updateSize = new UnitSlot() {
@Override public void onEmit () {
((Content)scroll.content).preferredSize.update(
width.value.get(), height.value.get());
}
};
width.value.connect(updateSize);
height.value.connect(updateSize);
// updates the scroll offset
UnitSlot updatePos = new UnitSlot() {
@Override public void onEmit () {
float x = xpos.value.get() * scroll.hrange.max();
float y = ypos.value.get() * scroll.vrange.max();
scroll.scroll(x, y);
}
};
xpos.value.connect(updatePos);
ypos.value.connect(updatePos);
Button beh = new Button(Behavior.BOTH.name()).onClick(new Slot<Button>() {
@Override public void onEmit (Button source) {
Behavior[] behs = Behavior.values();
Behavior beh = Behavior.valueOf(source.text.get());
beh = behs[(beh.ordinal() + 1) % behs.length];
scroll.setBehavior(beh);
source.text.update(beh.name());
xpos.setVisible(beh.hasHorizontal());
ypos.setVisible(beh.hasVertical());
updateSize.onEmit();
}
});
scroll.contentClicked().connect(new Slot<Pointer.Event>() {
@Override public void onEmit (Event e) {
Point pt = Layer.Util.screenToLayer(content.layer, e.x(), e.y());
click.text.update(pt.x + ", " + pt.y);
}
});
scroll.addListener(new Scroller.Listener() {
@Override public void viewChanged (IDimension contentSize, IDimension scrollSize) {}
@Override public void positionChanged (float x, float y) {
update(xpos, x, scroll.hrange);
update(ypos, y, scroll.vrange);
}
void update (Slider pos, float val, Scroller.Range range) {
if (range.max() > 0) pos.value.update(val / range.max());
}
});
// background so we can see when the content is smaller
scroll.addStyles(Style.BACKGROUND.is(Background.solid(Colors.LIGHT_GRAY).inset(10)));
updatePos.onEmit();
updateSize.onEmit();
return new Group(AxisLayout.vertical().offStretch()).add(
new Group(AxisLayout.horizontal()).add(
new Label("Size:"), new Shim(15, 1), width, new Label("x"), height, beh),
new Group(AxisLayout.horizontal()).add(
new Label("Pos:"), new Shim(15, 1), xpos, ypos),
new Group(AxisLayout.horizontal()).add(
new Label("Click:"), new Shim(15, 1), click),
new Group(AxisLayout.horizontal().offStretch()).setConstraint(AxisLayout.stretched()).
add(scroll.setConstraint(AxisLayout.stretched())));
}