libGDX json TextButton如何对齐标签? [英] libGDX json TextButton how to align label?

查看:104
本文介绍了libGDX json TextButton如何对齐标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将以下代码放入json外观文件中?

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

这是我实际的json皮肤表:

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

我已经尝试了各种方法,因为我知道TextButton包含一个数据字段标签,因此我想我可以以某种方式创建一个JSON Label模板,然后将其输入到TextButton中,但是不幸的是,这是行不通的...

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

提前谢谢!

推荐答案

我希望能很好地解释,再加上我认为您在讨论另一个问题, 好吧,我的英语不是很好,这不是您想要的结果,但是希望这将为NEXT类扩展Button和TEXTBUTTON类提供变量标签pirivate end,为什么不重写涉及的方法,这是一个尚未实现捕获异常等的简单示例,这只是一个示例.

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

.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) {

布尔alignLabelJson用于激活,从json对齐,使用true.

boolean alignLabelJson for used to activate, alignment from json, use true.

布尔padLabelJson用于激活,从json填充,使用true.

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

这篇关于libGDX json TextButton如何对齐标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆