libGDX json TextButton如何对齐标签?

时间:2014-11-04 14:55:46

标签: json libgdx skin

有没有办法将以下代码放入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 Label模板,我将其输入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}
}

提前致谢!

1 个答案:

答案 0 :(得分:0)

我希望能够解释清楚,另外我想你在另一个问题上讨论一些事情,  好吧,我的英语不是很好,这不是你想要的结果,但希望这将为NEXT类扩展Button,而TEXTBUTTON类有变量标签pirivate结束,为什么不覆盖涉及的方法这是一个只是没有实现捕获异常的简单示例,这只是一个例子。

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。