我正在尝试为libgdx创建一个组件,该组件可以以与html用<p align=”justify”> ... </p>
相同的方式输出文本。
我的想法是添加一些自定义组件,这些组件可以通过调整组件的相对x和y坐标来实现。
import java.util.ArrayList;
import java.util.List;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.badlogic.gdx.scenes.scene2d.ui.WidgetGroup;
import com.badlogic.gdx.utils.Align;
public class Paragraph extends WidgetGroup {
private Label space;
public Paragraph(String text, float width, LabelStyle style) {
super();
setWidth(width);
this.space = new Label(" ", style);
this.space.pack();
String[] words = text.split(" ");
for (String word : words) {
Label label = new Label(word, style);
label.pack();
addActor(label);
}
}
public void layout () {
float size = 0;
List<Actor> elements = new ArrayList<Actor>();
float x = getX(Align.topLeft);
float y = getY(Align.topLeft);
for (Actor actor : this.getChildren().items) {
if (actor != null) {
if (elements.isEmpty()) {
elements.add(actor);
size = actor.getWidth();
} else {
if (size + space.getWidth() + actor.getWidth() <= this.getWidth()) {
elements.add(actor);
size += (space.getWidth() + actor.getWidth());
} else {
float spacing = space.getWidth() + ((getWidth() - size) / elements.size());
Actor element = elements.get(0);
element.setPosition(x, y, Align.topLeft);
x += element.getWidth();
for (int i = 1; i < elements.size(); i++) {
element = elements.get(i);
element.setPosition(x + spacing, y, Align.topLeft);
x += (spacing + element.getWidth());
}
// new line
elements.clear();
x = getX(Align.topLeft);
y += (this.space.getHeight() * 1.5);
elements.add(actor);
size = actor.getWidth();
}
}
}
}
if (elements.isEmpty() == false) {
float spacing = space.getWidth();
Actor element = elements.get(0);
element.setPosition(x, y, Align.topLeft);
x += element.getWidth();
for (int i = 1; i < elements.size(); i++) {
element = elements.get(i);
element.setPosition(x + spacing, y, Align.topLeft);
x += (spacing + element.getWidth());
}
}
}
}
我的问题是我用getX(Align.topLeft)和getY(Align.topLeft)检索的x和y坐标始终返回(0,0) 而不是舞台上的实际坐标。
组件结构如下:
Stage
+ Container
+ Table
+ ScrollPane
+ Table
+ Table
+ Paragraph
+ Paragraph
+ Paragraph
+ Paragraph
+ Image
因此,最终结果是不同段落中包含的所有文本都在彼此之上绘制。不在位置(0,0)上,而是在周围桌子内部的同一位置上。
答案 0 :(得分:0)
我自己弄清楚了。
似乎为它覆盖布局方法是一个坏主意。 现在,我调用与构造函数不同的方法。
我也弄乱了x和y坐标,所以我不得不重写该方法。
Ps .: 我也解决了这个问题,当我找出解决方案时, 并不是关于小部件组的x和y余弦。
import java.util.ArrayList;
import java.util.List;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.badlogic.gdx.scenes.scene2d.ui.WidgetGroup;
import com.badlogic.gdx.utils.Align;
public class Paragraph extends WidgetGroup {
private static final class RowData {
final List<Actor> elements;
final float size;
final boolean lastRow;
RowData(List<Actor> elements, float size, boolean lastRow) {
this.elements = elements;
this.size = size;
this.lastRow = lastRow;
}
static RowData createNewRow(List<Actor> elements, float size) {
return new RowData(elements, size, false);
}
static RowData createLastRow(List<Actor> elements, float size) {
return new RowData(elements, size, true);
}
}
private Label space;
public Paragraph(String text, float width, LabelStyle style) {
super();
setWidth(width);
this.space = new Label(" ", style);
this.space.pack();
String[] words = text.split(" ");
for (String word : words) {
Label label = new Label(word, style);
label.pack();
addActor(label);
}
arrangeActors();
}
private void arrangeActors() {
float size = 0;
float height = space.getHeight();
List<RowData> rows = new ArrayList<RowData>();
List<Actor> elements = new ArrayList<Actor>();
Actor[] actors = this.getChildren().begin();
for (Actor actor : actors) {
if (actor != null) {
if (elements.isEmpty()) {
elements.add(actor);
size = actor.getWidth();
} else if (size + space.getWidth() + actor.getWidth() <= this.getWidth()) {
elements.add(actor);
size += (space.getWidth() + actor.getWidth());
} else {
rows.add(RowData.createNewRow(elements, size));
elements = new ArrayList<Actor>();
height += space.getHeight();
elements.add(actor);
size = actor.getWidth();
}
}
}
this.getChildren().end();
rows.add(RowData.createLastRow(elements, size));
height += space.getHeight();
setHeight(height);
float y = height;
for (RowData data : rows) {
float spacing = space.getWidth();
if (data.lastRow == false) {
spacing += ((getWidth() - data.size) / data.elements.size());
}
Actor element = data.elements.get(0);
element.setPosition(0, y, Align.topLeft);
float x = element.getWidth();
for (int i = 1; i < data.elements.size(); i++) {
element = data.elements.get(i);
element.setPosition(x + spacing, y, Align.topLeft);
x += (spacing + element.getWidth());
}
y -= this.space.getHeight();
}
}
@Override
public float getPrefWidth () {
return getWidth();
}
@Override
public float getPrefHeight () {
return getHeight();
}
}