gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 05:07:18 24 4
gpt4 key购买 nike

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

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

这是我的完整工作类MyStage,它实现了Screen和我的自定义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. 列出项目

您的问题是 TextScrollActor 的大小为 0(W=0,H=0)。

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

另一个问题是你为什么要实现自己的 Actor ?因为这个案例完美地覆盖了带有配置皮肤的Label

关于java - 将 BitmapFont.draw 从自定义 Actor 添加到 ScrollPane/Table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27677241/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com