libGDX json TextButton how to align label?

2019-06-09 19:33发布

问题:

Is there a way to put the following code into a json skin file?

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;
}

Here's my actual json skin sheet:

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

I've tried various things, because I know that TextButton contains a datafield label and therefore I thougth I probably could create somehow a json Label template, which I enter into the TextButton, but unfortunatelly that's not working...

"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}
}

Thanks in advance!

回答1:

I hope to explain well, plus I think you discuss something in another question, well, my English is not very good and what not this is exactly the result you want, but hopefully this will serve the NEXT class extends Button, and TEXTBUTTON class has variable label pirivate end, why not override the methods that are involved this is a simple example that just have not implemented the capture exceptions ect, this is just an example.

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;

}

}

for use this example, in your class:

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

in your .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 for used to activate, alignment from json, use true.

boolean padLabelJson for used to activate, pading from json, use true.



标签: json libgdx skin