libgdx 中的滚动窗格奇怪缩放
Scrollpane strange scaling in libgdx
在Scrollpane中用字体绘制的文字和在列表中绘制的文字看起来大小不一样。这是我绘制文本的方式:
font.draw(batch, "score", 500, 700);
以及我如何在 ScrollPane 中的列表中绘制文本:
tfBackground = new Texture(Gdx.files.internal("tfbackground.png"));
knob_scroll = new Texture(Gdx.files.internal("knob_scroll.png"));
scroll_horizontal = new Texture(Gdx.files.internal("scroll_horizontal.png"));
sps.background = new TextureRegionDrawable(new TextureRegion(tfBackground));
sps.vScroll = new TextureRegionDrawable(new TextureRegion(scroll_horizontal));
sps.vScrollKnob = new TextureRegionDrawable(new TextureRegion(knob_scroll));
listS = new List.ListStyle();
listS.font = font;
listS.fontColorSelected = Color.BLACK;
listS.fontColorUnselected = Color.GRAY;
listS.selection = new TextureRegionDrawable(new TextureRegion(tfBackground));
scoreList = new List<String>(listS);
items = new Array<String>();
res += "score 1, score 2, score 3, score 4, score 5, score 6, score 7, score 8, score 9, score 10";
String name_score[] = res.split(",");
for(String s: name_score)
{
items.add(s);
}
scoreList.setItems(items);
scoreList.pack();
scrollPane = new ScrollPane(scoreList, sps);
addActor(scrollPane);
这是奇怪的(?)结果:
滚动窗格似乎正在以某种方式缩放。我不希望缩放滚动窗格中的文本。
这是完整的代码:
public class ResultScreen extends AbstractScreen{
private OrthographicCamera camera;
private Viewport viewport;
private BitmapFont font;
private SpriteBatch batch;
private String res;
private float time = 10;
private Texture tfBackground, knob_scroll, scroll_horizontal;
private List<String> scoreList;
private ScrollPane scrollPane;
private List.ListStyle listS;
private ScrollPane.ScrollPaneStyle sps;
private Array<String> items;
public ResultScreen(float time, String res) {
this.time = time;
font = new BitmapFont(Gdx.files.internal("aw.fnt"));
font.setColor(Color.BLACK);
camera = new OrthographicCamera();
viewport = new StretchViewport(800, 1024, camera);
//viewport = new FitViewport(1240, 800, camera);
batch = new SpriteBatch();
camera.position.x = 400;
camera.position.y = 512;
viewport.update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
this.res = res;
fillList();
}
@Override
public void buildStage() {
// TODO Auto-generated method stub
}
@Override
public void resize(int width, int height) {
viewport.update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
}
@Override
public void render(float delta) {
super.render(delta);
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
batch.begin();
font.draw(batch, "score", 500, 700);
batch.end();
draw();
act();
}
private void fillList()
{
sps = new ScrollPane.ScrollPaneStyle();
tfBackground = new Texture(Gdx.files.internal("tfbackground.png"));
knob_scroll = new Texture(Gdx.files.internal("knob_scroll.png"));
scroll_horizontal = new Texture(Gdx.files.internal("scroll_horizontal.png"));
sps.background = new TextureRegionDrawable(new TextureRegion(tfBackground));
sps.vScroll = new TextureRegionDrawable(new TextureRegion(scroll_horizontal));
sps.vScrollKnob = new TextureRegionDrawable(new TextureRegion(knob_scroll));
listS = new List.ListStyle();
listS.font = font;
listS.fontColorSelected = Color.BLACK;
listS.fontColorUnselected = Color.GRAY;
listS.selection = new TextureRegionDrawable(new TextureRegion(tfBackground));
scoreList = new List<String>(listS);
items = new Array<String>();
res += "score 1, score 2, score 3, score 4, score 5, score 6, score 7, score 8, score 9, score 10";
String name_score[] = res.split(",");
for(String s: name_score)
{
items.add(s);
}
scoreList.setItems(items);
scoreList.pack();
scrollPane = new ScrollPane(scoreList, sps);
scrollPane.setWidth(300);
System.out.println(Gdx.graphics.getWidth());
addActor(scrollPane);
}
}
PS: 抽象屏幕扩展舞台并实现屏幕。
您的构造函数不会调用超级class 构造函数,它还需要调用其中一个舞台构造函数以确保您的舞台设置正确。
这里发生的事情是您设置了仅用于 font.draw
并且与舞台使用的不同的相机和视口(和 SpriteBatch,尽管不是问题的一部分)。
您的 class 需要看起来像这样:
public class ResultScreen extends AbstractScreen{
private OrthographicCamera camera;
private Viewport viewport;
private BitmapFont font;
private Batch batch; //<------------changed
private String res;
private float time = 10;
private Texture tfBackground, knob_scroll, scroll_horizontal;
private List<String> scoreList;
private ScrollPane scrollPane;
private List.ListStyle listS;
private ScrollPane.ScrollPaneStyle sps;
private Array<String> items;
public ResultScreen(float time, String res) {
super(new StretchViewport(800, 1024)); //Call an AbstractScreen constructor that calls a Stage constructor
this.time = time;
font = new BitmapFont(Gdx.files.internal("aw.fnt"));
font.setColor(Color.BLACK);
camera = getCamera(); //<------------changed
viewport = getViewport(); //<------------changed
batch = getBatch(); //<------------changed
camera.position.x = 400;
camera.position.y = 512;
// unnecessary can remove because this is called in resize: viewport.update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
this.res = res;
fillList();
}
}
例如,AbstractScreen 可以有这样的构造函数:
public AbstractScreen(Viewport viewport){
super(viewport);
}
在Scrollpane中用字体绘制的文字和在列表中绘制的文字看起来大小不一样。这是我绘制文本的方式:
font.draw(batch, "score", 500, 700);
以及我如何在 ScrollPane 中的列表中绘制文本:
tfBackground = new Texture(Gdx.files.internal("tfbackground.png"));
knob_scroll = new Texture(Gdx.files.internal("knob_scroll.png"));
scroll_horizontal = new Texture(Gdx.files.internal("scroll_horizontal.png"));
sps.background = new TextureRegionDrawable(new TextureRegion(tfBackground));
sps.vScroll = new TextureRegionDrawable(new TextureRegion(scroll_horizontal));
sps.vScrollKnob = new TextureRegionDrawable(new TextureRegion(knob_scroll));
listS = new List.ListStyle();
listS.font = font;
listS.fontColorSelected = Color.BLACK;
listS.fontColorUnselected = Color.GRAY;
listS.selection = new TextureRegionDrawable(new TextureRegion(tfBackground));
scoreList = new List<String>(listS);
items = new Array<String>();
res += "score 1, score 2, score 3, score 4, score 5, score 6, score 7, score 8, score 9, score 10";
String name_score[] = res.split(",");
for(String s: name_score)
{
items.add(s);
}
scoreList.setItems(items);
scoreList.pack();
scrollPane = new ScrollPane(scoreList, sps);
addActor(scrollPane);
这是奇怪的(?)结果:
滚动窗格似乎正在以某种方式缩放。我不希望缩放滚动窗格中的文本。
这是完整的代码:
public class ResultScreen extends AbstractScreen{
private OrthographicCamera camera;
private Viewport viewport;
private BitmapFont font;
private SpriteBatch batch;
private String res;
private float time = 10;
private Texture tfBackground, knob_scroll, scroll_horizontal;
private List<String> scoreList;
private ScrollPane scrollPane;
private List.ListStyle listS;
private ScrollPane.ScrollPaneStyle sps;
private Array<String> items;
public ResultScreen(float time, String res) {
this.time = time;
font = new BitmapFont(Gdx.files.internal("aw.fnt"));
font.setColor(Color.BLACK);
camera = new OrthographicCamera();
viewport = new StretchViewport(800, 1024, camera);
//viewport = new FitViewport(1240, 800, camera);
batch = new SpriteBatch();
camera.position.x = 400;
camera.position.y = 512;
viewport.update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
this.res = res;
fillList();
}
@Override
public void buildStage() {
// TODO Auto-generated method stub
}
@Override
public void resize(int width, int height) {
viewport.update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
}
@Override
public void render(float delta) {
super.render(delta);
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
batch.begin();
font.draw(batch, "score", 500, 700);
batch.end();
draw();
act();
}
private void fillList()
{
sps = new ScrollPane.ScrollPaneStyle();
tfBackground = new Texture(Gdx.files.internal("tfbackground.png"));
knob_scroll = new Texture(Gdx.files.internal("knob_scroll.png"));
scroll_horizontal = new Texture(Gdx.files.internal("scroll_horizontal.png"));
sps.background = new TextureRegionDrawable(new TextureRegion(tfBackground));
sps.vScroll = new TextureRegionDrawable(new TextureRegion(scroll_horizontal));
sps.vScrollKnob = new TextureRegionDrawable(new TextureRegion(knob_scroll));
listS = new List.ListStyle();
listS.font = font;
listS.fontColorSelected = Color.BLACK;
listS.fontColorUnselected = Color.GRAY;
listS.selection = new TextureRegionDrawable(new TextureRegion(tfBackground));
scoreList = new List<String>(listS);
items = new Array<String>();
res += "score 1, score 2, score 3, score 4, score 5, score 6, score 7, score 8, score 9, score 10";
String name_score[] = res.split(",");
for(String s: name_score)
{
items.add(s);
}
scoreList.setItems(items);
scoreList.pack();
scrollPane = new ScrollPane(scoreList, sps);
scrollPane.setWidth(300);
System.out.println(Gdx.graphics.getWidth());
addActor(scrollPane);
}
}
PS: 抽象屏幕扩展舞台并实现屏幕。
您的构造函数不会调用超级class 构造函数,它还需要调用其中一个舞台构造函数以确保您的舞台设置正确。
这里发生的事情是您设置了仅用于 font.draw
并且与舞台使用的不同的相机和视口(和 SpriteBatch,尽管不是问题的一部分)。
您的 class 需要看起来像这样:
public class ResultScreen extends AbstractScreen{
private OrthographicCamera camera;
private Viewport viewport;
private BitmapFont font;
private Batch batch; //<------------changed
private String res;
private float time = 10;
private Texture tfBackground, knob_scroll, scroll_horizontal;
private List<String> scoreList;
private ScrollPane scrollPane;
private List.ListStyle listS;
private ScrollPane.ScrollPaneStyle sps;
private Array<String> items;
public ResultScreen(float time, String res) {
super(new StretchViewport(800, 1024)); //Call an AbstractScreen constructor that calls a Stage constructor
this.time = time;
font = new BitmapFont(Gdx.files.internal("aw.fnt"));
font.setColor(Color.BLACK);
camera = getCamera(); //<------------changed
viewport = getViewport(); //<------------changed
batch = getBatch(); //<------------changed
camera.position.x = 400;
camera.position.y = 512;
// unnecessary can remove because this is called in resize: viewport.update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
this.res = res;
fillList();
}
}
例如,AbstractScreen 可以有这样的构造函数:
public AbstractScreen(Viewport viewport){
super(viewport);
}