libGDX 使用 WidgetGroup 而不是 Table 作为 ScrollPane 小部件的 ScrollPane?
libGDX ScrollPane with WidgetGroup instead of Table as the ScrollPane widget?
据我了解,通过使用 Table 作为 ScrollPane 小部件,ScrollPane 可以添加多个小部件(这是在 libGDX 存储库中的 ScrollPane 测试中完成的) .
我想实现类似的东西,但希望在 ScrollPane 小部件中对许多参与者进行绝对定位,而不是通过使用 Table 作为 ScrollPane 小部件提供的表格定位。
我最终得到了这个不起作用的代码,但根据 libGDX javadoc 应该是这样,但我不知道为什么它不起作用!
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);
屏幕上完全没有任何显示;
但是它确实适用于下面的代码(这显然不是我想要的,因为 ScrollPane 小部件是一个只能有一个演员的容器)
// 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);
有没有办法将 WidgetGroup 与 ScrollPane 一起使用,或者我可以通过任何方式实现我需要的功能。
谢谢
已接受答案的替代实施方式
阅读接受的答案后,我决定创建自己的 class,实现如下;
// 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);
使用 WidgetGroup 时,您必须自己设置尺寸。 Image actor 不知道首选 width/heights 应该是什么,并将默认为 0.
未经测试的代码,但我自己使用了类似的东西:
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);
据我了解,通过使用 Table 作为 ScrollPane 小部件,ScrollPane 可以添加多个小部件(这是在 libGDX 存储库中的 ScrollPane 测试中完成的) .
我想实现类似的东西,但希望在 ScrollPane 小部件中对许多参与者进行绝对定位,而不是通过使用 Table 作为 ScrollPane 小部件提供的表格定位。
我最终得到了这个不起作用的代码,但根据 libGDX javadoc 应该是这样,但我不知道为什么它不起作用!
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);
屏幕上完全没有任何显示;
但是它确实适用于下面的代码(这显然不是我想要的,因为 ScrollPane 小部件是一个只能有一个演员的容器)
// 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);
有没有办法将 WidgetGroup 与 ScrollPane 一起使用,或者我可以通过任何方式实现我需要的功能。
谢谢
已接受答案的替代实施方式
阅读接受的答案后,我决定创建自己的 class,实现如下;
// 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);
使用 WidgetGroup 时,您必须自己设置尺寸。 Image actor 不知道首选 width/heights 应该是什么,并将默认为 0.
未经测试的代码,但我自己使用了类似的东西:
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);