将BitmapFont.draw从自定义Actor添加到ScrollPane / Table

时间:2014-12-28 14:17:55

标签: java libgdx

我创建了一个名为Actor的自定义TestScrollActor,在其BitmapFont.drawWraped方法上调用draw

CharSequence/Text会不时附加,因此我需要将此自定义Actor添加到我的垂直滚动Table。文本显示正确,但根本不滚动。

以下是我的完整工作班MyStage,它实现了ScreenCustom Actor

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.utils.viewport.StretchViewport;

public class MyStage implements Screen{

    private BitmapFont font;
    private String text = "The modern encyclopedia was developed from the "
            + "dictionary in the 18th century. Historically, both encyclopedias "
            + "and dictionaries have been researched and written by well-educated, "
            + "well-informed content experts, but they are significantly different "
            + "in structure. A dictionary is a linguistic work which primarily focuses "
            + "on alphabetical listing of words and their definitions. Synonymous words "
            + "and those related by the subject matter are to be found scattered around "
            + "the dictionary, giving no obvious place for in-depth treatment. Thus, a dictionary "
            + "typically provides limited information, analysis or background for the word defined."
            + " While it may offer a definition, it may leave the reader lacking in understanding the meaning,"
            + " significance or limitations of a term, and how the term relates to a broader field of knowledge. "
            + "An encyclopedia is, allegedly, not written in order to convince, although one of its goals is indeed "
            + "to convince its reader about its own veracity. In the terms of Aristotle's Modes of persuasion, a "
            + "dictionary should persuade the reader through logos (conveying only appropriate emotions); it will be"
            + " expected to have a lack of pathos (it should not stir up irrelevant emotions), and to have little ethos "
            + "except that of the dictionary itself. To address those needs, an encyclopedia article is typically not limited to "
            + "simple definitions, and is not limited to defining an individual word, but provides a more extensive meaning for a "
            + "subject or discipline. In addition to defining and listing synonymous terms for the topic, the article is able to treat "
            + "the topic's more extensive meaning in more depth and convey the most relevant accumulated knowledge on that subject. "
            + "An encyclopedia article also often includes many maps and illustrations, as well as bibliography and statistics.";

    private Stage stage;

    public static float myGameWidth = 400;
    public static float myGameHeight = 640;

    @Override
    public void show() {

        stage = new Stage(new StretchViewport(myGameWidth,myGameHeight));

        InputMultiplexer inputM = new InputMultiplexer();
        inputM.addProcessor(stage);
        Gdx.input.setInputProcessor(inputM);

        font =  new BitmapFont();

        TextScrollActor textScrollActor = new TextScrollActor(text);

        //create scrollable table
        LabelStyle labelStyle = new LabelStyle();
        labelStyle.font = font;
        labelStyle.fontColor = Color.WHITE;

        final Table scrollTable = new Table();
        scrollTable.top();

        scrollTable.add(textScrollActor).height(300);
        scrollTable.row();

        final ScrollPane scroller = new ScrollPane(scrollTable);

        final Table table = new Table();
        table.setSize(300, 400);
        table.setPosition(25, 300);
        table.add(scroller).fill().expand();

        this.stage.addActor(table);

    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        //Update the stage
        stage.draw();
        stage.act(delta);
    }

    @Override
    public void resize(int width, int height) {}

    @Override
    public void dispose() {}

    @Override
    public void hide() {dispose();}

    @Override
    public void pause() {}

    @Override
    public void resume() {}


    public class TextScrollActor extends Actor{

        private BitmapFont font;
        private String text;

        public TextScrollActor(String text) {
            this.text = text;

            font =  new BitmapFont();
            font.setColor(Color.WHITE);
        }

        @Override
        public void draw(Batch batch, float parentAlpha) {

            font.drawWrapped(batch,text,50, 450, 280);
        }
    }
}

1 个答案:

答案 0 :(得分:2)

  1. 列表项
  2. 您的问题是TextScrollActor 0大小(W = 0,H = 0)。

    如果你制作自己的演员并且它具有自定义绘图功能,你应该始终注意它的宽度和高度值并自己改变它。 Actor默认使用(0,0)大小,不同的实现根据其绘图内容更改它。因此,要制作滚动工作,您只需要计算文本所需的空间(一些BitmapFont#getBounds方法应该可以帮助您)并将该尺寸设置为Actor宽度和高度。这应该有用。

    其他问题是你为什么要实施自己的演员?因为这种情况完全覆盖Label配置的皮肤。