为了账号安全,请及时绑定邮箱和手机立即绑定

如何输出对齐对齐的文本

如何输出对齐对齐的文本

慕慕森 2022-11-30 16:20:10
我正在尝试为 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());                    } }我的问题是我用 getX(Align.topLeft) & getY(Align.topLeft) 检索的 x 和 y 坐标总是返回 (0,0) 而不是舞台上的真实坐标。所以最终的结果是不同段落中包含的所有文本都被绘制在彼此之上。不在位置 (0,0) 上,而是在周围表格内的相同位置上。
查看完整描述

1 回答

?
慕运维8079593

TA贡献1876条经验 获得超5个赞

我自己想出来了。


似乎为此覆盖布局方法是个坏主意。我现在从构造函数中调用一个不同的方法。


另外我弄乱了 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();

    }

}



查看完整回答
反对 回复 2022-11-30
  • 1 回答
  • 0 关注
  • 210 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号