LibGDX - 获取ImageTextButton中标签单元格的宽度

时间:2016-05-18 05:31:32

标签: libgdx

我在容器表中有ImageTextButtons。我想动态更改此表的宽度,并让ImageTextButtons以特定方式响应。我希望他们做的是根据所显示文本的百分比淡入标签文本。

我相信我正在动态设置表格宽度和内容。根据LibGDX文档,我们不应该显式设置窗口小部件维度,而应该是表(容器)单元格维度。所以对于测试,我有一个简单的clickListener,就像这样的按钮;

    button.addListener(new ClickListener(){
        @Override
        public void clicked(InputEvent event, float x, float y) {
            super.clicked(event, x, y);
            float max = container.getCell(button).getMaxWidth();
            container.getCell(button).width(max*0.9f);
            container.invalidate();
        }
    });

这很好用,我点击它,单元格(和小部件)每次缩小90%。

对于下一步,我认为在我的自定义ImageTextButton的draw()方法中就这么简单;

    glyphWidth = getLabel().getGlyphLayout().width;
    labelCellWidth = getLabelCell().getMinWidth(); ///<--problem here

    float glyphOpacityMultiplier = MathUtils.clamp(labelCellWidth/glyphWidth, 0f, 1f);

    origLabelColor = getLabel().getColor().cpy();
    newLabelColor = origLabelColor.cpy();
    newLabelColor = new Color(newLabelColor.r, newLabelColor.g, newLabelColor.b, newLabelColor.a*glyphOpacityMultiplier);

    getLabel().setColor(newLabelColor);
    super.draw(batch, parentAlpha);
    getLabel().setColor(origLabelColor);

这一切都有效,除了我无法找到任何方法来给我包含标签的Cell的实际宽度。由于ImageTextButton本身也是一个Table,我也尝试在ClickListener中执行button.invalidate()但它没有帮助。这些是我到目前为止尝试过的方法;

    getLabelCell().getMinWidth();
    getLabelCell().getMaxWidth();
    getLabelCell().getPrefWidth();
    getLabelCell().getActorWidth();
    getLabelCell().getSpaceLeft();
    getLabelCell().getSpaceRight();

它们似乎都没有更新并给我正确的大小,但我知道某些东西在某处工作,因为我在ImageTextButton上打开了“setClip(true)”,并且标签文本被成功地剪切到了调整大小时的NinePatch背景。那么我该如何获得正确的尺寸?

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

糟糕..解决了它。我认为包含标签的单元格会被NinePatch背景偏移并调整大小,但不是,它是全尺寸但有填充。因此答案是;

    labelCellWidth = getWidth()-getPadLeft()-getPadRight();