簡體   English   中英

滾動窗格在libgdx中的奇怪縮放

[英]Scrollpane strange scaling in libgdx

由字體繪制和在滾動窗格內的列表中繪制的文本看起來具有不同的大小。 這是我繪制文本的方式:

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:抽象屏幕擴展了舞台並實現了Screen。

您的構造函數不會調用超類構造函數,而該超類構造函數也需要通過調用其中一個Stage構造函數來確保正確設置了Stage。

這里發生的是您設置了僅用於font.draw且與舞台所用的不同的攝影機和視口(和SpriteBatch,雖然不是問題的一部分)。

您的課程需要看起來像這樣:

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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM