2

From what I understand a ScrollPane can have multiple widgets added to it by using a Table as the ScrollPane widget (this is how it is done in the ScrollPane tests within the libGDX repo).

I would like to achieve something similar but would like to have absolute positioning of many actors within the ScrollPane widget as opposed to tabular positioning which is provided by using a Table as the ScrollPane widget.

I ended up with this code which does not work, but according to the libGDX javadoc it should and I do not know why it is not working!

stage = getStage();

// Scroll pane outer container
Container<ScrollPane> container = new Container<ScrollPane>();
container.setSize(Game.getWidth(), Game.getHeight());

// Scroll pane inner container
WidgetGroup widgetGroup = new WidgetGroup();
widgetGroup.setFillParent(true);
widgetGroup.addActor(new Image(MAP_TEXTURE_ATLAS.findRegion("map")));

// Scroll pane
ScrollPane scrollPane = new ScrollPane(widgetGroup);
container.setActor(scrollPane);

stage.addActor(container);

Nothing is displayed on the screen at all;

It does however work with the below code (which is obviously not what I want as the ScrollPane widget is a Container which can have only have one actor)

// Scroll pane outer container
Container<ScrollPane> container = new Container<ScrollPane>();
container.setSize(Match3.getWidth(), Match3.getHeight());

// Scroll pane inner container
Container container2 = new Container();
container2.setBackground(new TextureRegionDrawable(MAP_TEXTURE_ATLAS.findRegion("map")));

// Scroll pane
ScrollPane scrollPane = new ScrollPane(container2);
container.setActor(scrollPane);

stage.addActor(container);

Is there a way to use WidgetGroup with ScrollPane, or any way of how I can achieve the functionality that I require.

Thanks

ALTERNATIVE IMPLEMENTATION OF ACCEPTED ANSWER

After reading the accepted answer I decided to create my own class, the implementation is below;

// Scroll pane outer container
Container<ScrollPane> container = new Container<ScrollPane>();
container.setSize(Match3.getWidth(), Match3.getHeight());

class ScrollWidget extends WidgetGroup {

  private float prefHeight;
  private float prefWidth;

  public ScrollWidget(Image image) {

    prefHeight = image.getHeight();
    prefWidth = image.getWidth();

    addActor(image);
  }

  @Override
  public float getPrefHeight() {

    return prefHeight;
  }

  @Override
  public float getPrefWidth() {

    return prefWidth;
  }
}

ScrollWidget scrollWidget = new ScrollWidget(
    new Image(MAP_TEXTURE_ATLAS.findRegion("map"))
);

// Scroll pane
ScrollPane scrollPane = new ScrollPane(scrollWidget);
scrollPane.setOverscroll(false, false);

scrollPane.layout();
scrollPane.updateVisualScroll();
scrollPane.setScrollY(scrollPane.getMaxY());

container.setActor(scrollPane);

stage.addActor(container);
GradeRetro
  • 38
  • 4

1 Answers1

0

When working with a WidgetGroup you have to set sizes yourself. The Image actor has no idea of what preferred width/heights should be and will default to 0.

untested code, but I use something similar myself:

Stage stage = new Stage();
WidgetGroup group = new WidgetGroup();
Scrollpane scrollPane = new ScrollPane(group);
scrollpane.setBounds(0,0,screenWidth,screenHeight);
group.setBounds(0,0,totalWidth,totalHeight);
Image image = new Image(texture);
image.setBounds(0,0,imageWidth,imageHeight);
group.addActor(image);
stage.addActor(scrollPane);
p.streef
  • 3,652
  • 3
  • 26
  • 50
  • Thanks, this was the problem! I have edited my question to include a solution similar to how you solved it. – GradeRetro Mar 30 '16 at 23:22