【问题标题】:libGDX json TextButton how to align label?libGDX json TextButton如何对齐标签?
【发布时间】:2014-12-31 12:37:07
【问题描述】:

有没有办法将以下代码放入json皮肤文件中?

private TextButton createMainMenuButton(String name, ClickListener listener){
    TextButton button = new TextButton(name, skin, "MainMenuStyle");
    button.addListener(listener);
    //How to put the following two lines into a JSON skin file?
    button.getLabel().setAlignment(Align.left);
    button.getLabelCell().pad(5,20,5,20);
    return button;
}

这是我实际的 json 皮肤表:

"com.badlogic.gdx.scenes.scene2d.ui.TextButton$TextButtonStyle": {
    "default" : { "font" : "arial_narrow_w_32", "up" : "button" },
    "MainMenuStyle" : { "font" : "arial_narrow_w_32", "over" : "overBgMainMenu"}
}

我尝试了各种方法,因为我知道 TextButton 包含一个数据字段标签,因此我认为我可能可以以某种方式创建一个 json 标签模板,我将其输入到 TextButton 中,但不幸的是这不起作用...

"com.badlogic.gdx.scenes.scene2d.ui.TextButton": {
    "MainMenu" : { "style" : "MainMenuStyle", "label" : "MainMenuLabel"}
},

"com.badlogic.gdx.scenes.scene2d.ui.TextButton$TextButtonStyle": {
    "default" : { "font" : "arial_narrow_w_32", "up" : "button" },
    "MainMenuStyle" : { "font" : "arial_narrow_w_32", "over" : "overBgMainMenu"}
},

"com.badlogic.gdx.scenes.scene2d.ui.Label": {
    "MainMenuLabel" : {"style" : "MainMenuLabelStyle", "lineAlign" : "left",   "cellDefaults" : "MainMenuLabelCell"}
},

"com.badlogic.gdx.scenes.scene2d.ui.Cell": {
    "MainMenuLabelCell" : {"padTop" : 5, "padBottom" : 5, "padLeft": 20, "padRight" : 20}
}

提前致谢!

【问题讨论】:

  • 这无法完成。 TextButton 不支持使用 LabelStyle...它只是直接使用 TextButtonStyle 为 Label 着色。即使支持,LabelStyle 也不支持填充或对齐,尽管我不明白为什么不能在拉取请求中添加对齐。

标签: json libgdx skin


【解决方案1】:

我希望解释清楚,另外我想你在另一个问题中讨论了一些东西, 好吧,我的英语不是很好,这不是你想要的结果,但希望这将服务于 NEXT 类扩展 Button,并且 TEXTBUTTON 类具有变量标签 private end,为什么不覆盖所涉及的方法这是一个只是没有实现捕获异常等的简单示例,这只是一个示例。

public class OverflowTextButton extends Button{

private final Label label;
private TextButtonStyleOverflow style;

public OverflowTextButton (String text, Skin skin) {
    this(text, skin.get(TextButtonStyleOverflow.class), false, false);
    setSkin(skin);
}

public OverflowTextButton (String text, Skin skin, String styleName) {
    this(text, skin.get(styleName, TextButtonStyleOverflow.class), false, false);
    setSkin(skin);
}

public OverflowTextButton (String text, TextButtonStyleOverflow style, boolean alignLabelJson, boolean padLabelJson) {
    super();
    setStyle(style);
    this.style = style;
    label = new Label(text, new LabelStyle(style.font, style.fontColor));



    if(alignLabelJson == true){
        Gdx.app.log("OverflowTextButton   align", ""+style.labelJson.name);

        label.setAlignment(getLabelAlignJson(style.labelJson.align));
        add(label).expand().fill();

    }else{

        label.setAlignment(Align.center);
        add(label).expand().fill();

    }

    if(padLabelJson == true){

        add(label).pad(style.labelJson.padTop,
                       style.labelJson.padLeft,
                       style.labelJson.padBottom,
                       style.labelJson.padRight);
    }

    setSize(getPrefWidth(), getPrefHeight());
}

public int getLabelAlignJson(int alignCase){//Exp

    int alignJson = alignCase;

    switch (alignCase) {

    case 1:

        alignJson = 1 << 1;
        return alignJson;

    case 2:

        alignJson = 1 << 0;
        return alignJson;

    case 3:

        alignJson = 1 << 2;
        return alignJson;

    case 4:

        alignJson = 1 << 3;
        return alignJson;

    case 5:

        alignJson = 1 << 4;
        return alignJson;

    default:
        return alignJson;
    }
}

public void setStyle (ButtonStyle style) {
    if (style == null) {
        throw new NullPointerException("style cannot be null");
    }
    if (!(style instanceof TextButtonStyleOverflow)) throw new IllegalArgumentException("style must be a TextButtonStyle.");
    super.setStyle(style);
    this.style = (TextButtonStyleOverflow)style;
    if (label != null) {
        TextButtonStyleOverflow textButtonStyle = (TextButtonStyleOverflow)style;
        LabelStyle labelStyle = label.getStyle();
        labelStyle.font = textButtonStyle.font;
        labelStyle.fontColor = textButtonStyle.fontColor;
        label.setStyle(labelStyle);
    }
}

public TextButtonStyleOverflow getStyle () {
    return style;
}

public void draw (Batch batch, float parentAlpha) {
    Color fontColor;
    if (isDisabled() && style.disabledFontColor != null)
        fontColor = style.disabledFontColor;
    else if (isPressed() && style.downFontColor != null)
        fontColor = style.downFontColor;
    else if (isChecked() && style.checkedFontColor != null)
        fontColor = (isOver() && style.checkedOverFontColor != null) ? style.checkedOverFontColor : style.checkedFontColor;
    else if (isOver() && style.overFontColor != null)
        fontColor = style.overFontColor;
    else
        fontColor = style.fontColor;
    if (fontColor != null) label.getStyle().fontColor = fontColor;
    super.draw(batch, parentAlpha);
}

public Label getLabel () {
    return label;
}

public Cell getLabelCell () {
    return getCell(label);
}

public void setText (String text) {
    label.setText(text);
}

public CharSequence getText () {
    return label.getText();
}

/** The style for a text button, see {@link TextButton}.
 * @author  */
static public class TextButtonStyleOverflow extends ButtonStyle {
    public BitmapFont font;
    /** Optional. */
    public Color fontColor, downFontColor, overFontColor, checkedFontColor, checkedOverFontColor, disabledFontColor;

    public LabelJson labelJson;

    public TextButtonStyleOverflow () {
    }

    public TextButtonStyleOverflow (Drawable up, Drawable down, Drawable checked, BitmapFont font) {
        super(up, down, checked);
        this.font = font;
    }

    public TextButtonStyleOverflow (TextButtonStyleOverflow style) {
        super(style);
        this.font = style.font;
        this.labelJson = style.labelJson;

        if (style.fontColor != null) this.fontColor = new Color(style.fontColor);
        if (style.downFontColor != null) this.downFontColor = new Color(style.downFontColor);
        if (style.overFontColor != null) this.overFontColor = new Color(style.overFontColor);
        if (style.checkedFontColor != null) this.checkedFontColor = new Color(style.checkedFontColor);
        if (style.checkedOverFontColor != null) this.checkedFontColor = new Color(style.checkedOverFontColor);
        if (style.disabledFontColor != null) this.disabledFontColor = new Color(style.disabledFontColor);
    }
}
static public class LabelJson{
    public String name;
    public int align;
    public int padTop;
    public int padBottom;
    public int padLeft;
    public int padRight;

}

}

在你的课堂上使用这个例子:

TextButtonStyleOverflow style = skinMenuPrincipal.get("pruebasLabel", TextButtonStyleOverflow.class);
button = new OverflowTextButton("newText", style, false, true);
button.setBounds(50, 50, 300, 300);

在您的 .json 中:

com.YOUR.PACKAGE.OverflowTextButton$LabelJson: {
top:    { name: top,    align: 1, padTop : 5, padBottom : 5, padLeft: 20, padRight : 20 },
center: { name: center, align: 2, padTop : 5, padBottom : 5, padLeft: 20, padRight : 20 },
bottom: { name: bottom, align: 3, padTop : 5, padBottom : 5, padLeft: 20, padRight : 20 },
left:   { name: left,   align: 4, padTop : 5, padBottom : 5, padLeft: 20, padRight : 20 },
right:  { name: right,  align: 5, padTop : 5, padBottom : 5, padLeft: 20, padRight : 20 }

},

com.YOUR.PACKAGE.OverflowTextButton$TextButtonStyleOverflow: {
    default: { down: default-round-down, up: default-round, font: default-font, fontColor: white },
    toggle: { down: default-round-down, up: default-round, checked: default-round-down, font: default-font, fontColor: white, downFontColor: red },
    pruebasLabel: { labelJson: left, down: default-round-down, up: default-round, checked: default-round-down, font: default-font, fontColor: white, downFontColor: red }
},

.

public OverflowTextButton (String text, TextButtonStyleOverflow style, boolean alignLabelJson, boolean padLabelJson) {

boolean alignLabelJson 用于激活,从json对齐,使用true。

boolean padLabelJson 用于激活,从json填充,使用true。

【讨论】:

  • 感谢您 - 解决了我的问题并激励了我更多;)
  • 我很高兴它对你有用,如果你会使用很多,你可以创建两个静态公共类 LabelJson,一个用于 aling,一个用于填充等文件 json更干净,如果您在案例编号中输入大于 5,您也可以创建异常你的问候哈哈
猜你喜欢
  • 2014-07-27
  • 1970-01-01
  • 2022-11-01
  • 2015-02-26
  • 2023-03-22
  • 2017-12-30
  • 1970-01-01
  • 1970-01-01
  • 2014-11-29
相关资源
最近更新 更多