Buttons won't change icons

Minor thing … using Button.setButtonIcon() more than once doesn’t work. Buttons ‘icon’ member is lazily created, but there is no way of removing the previous icon.

It is possible to get at the icon element and change the image directly, or use getButtonIcon().removeFromParent() and use reflection to set ‘icon’ to null.

EDIT: Oh, you have to remove it from Screen too.

EDIT 2: In fact even that doesn’t work. Keep getting these …

[java]

tonegod.gui.core.ConflictingIDException
at tonegod.gui.core.Element.addChild(Element.java:378)
at tonegod.gui.core.Element.addChild(Element.java:348)
at tonegod.gui.controls.buttons.Button.setButtonIcon(Button.java:515)
[/java]

when using this code …

[java]

public static Button arrowButton(Screen screen, Button button, String direction) {
    Vector2f sz = screen.getStyle("Common").getVector2f("arrowSize");
    Element el = screen.getElementById(button.getUID() + ":btnIcon");
    if (el != null) {
        screen.removeElement(el);
    }
    LUtil.setInaccessibleField(null, "icon", button, Button.class);
    button.setButtonIcon(sz.x, sz.y, screen.getStyle("Common").getString("arrow" + direction));
    return button;
}

[/java]

EDIT 3:

This works …

[java]
public static Button arrowButton(Screen screen, Button button, String direction) {
Vector2f sz = screen.getStyle(“Common”).getVector2f(“arrowSize”);
if(button.getButtonIcon() != null) {
button.removeChild(button.getButtonIcon());
}
LUtil.setInaccessibleField(null, “icon”, button, Button.class);
button.setButtonIcon(sz.x, sz.y, screen.getStyle(“Common”).getString(“arrow” + direction));
return button;
}
[/java]

RR

@rockfire said: Minor thing ... using Button.setButtonIcon() more than once doesn't work. Buttons 'icon' member is lazily created, but there is no way of removing the previous icon.

It is possible to get at the icon element and change the image directly, or use getButtonIcon().removeFromParent() and use reflection to set ‘icon’ to null.

EDIT: Oh, you have to remove it from Screen too.

EDIT 2: In fact even that doesn’t work. Keep getting these …

[java]

tonegod.gui.core.ConflictingIDException
at tonegod.gui.core.Element.addChild(Element.java:378)
at tonegod.gui.core.Element.addChild(Element.java:348)
at tonegod.gui.controls.buttons.Button.setButtonIcon(Button.java:515)
[/java]

when using this code …

[java]

public static Button arrowButton(Screen screen, Button button, String direction) {
    Vector2f sz = screen.getStyle("Common").getVector2f("arrowSize");
    Element el = screen.getElementById(button.getUID() + ":btnIcon");
    if (el != null) {
        screen.removeElement(el);
    }
    LUtil.setInaccessibleField(null, "icon", button, Button.class);
    button.setButtonIcon(sz.x, sz.y, screen.getStyle("Common").getString("arrow" + direction));
    return button;
}

[/java]

EDIT 3:

This works …

[java]
public static Button arrowButton(Screen screen, Button button, String direction) {
Vector2f sz = screen.getStyle(“Common”).getVector2f(“arrowSize”);
if(button.getButtonIcon() != null) {
button.removeChild(button.getButtonIcon());
}
LUtil.setInaccessibleField(null, “icon”, button, Button.class);
button.setButtonIcon(sz.x, sz.y, screen.getStyle(“Common”).getString(“arrow” + direction));
return button;
}
[/java]

RR

I think you can just:

[java]
button.getButtonIcon().setColorMap(blah);

// or

button.getButtonIcon().setTextureAtlasImage(tex, blah);
[/java]

Until I fix this to be more intuitive.

Ok, thanks. That should be fine for most cases. I do have a few buttons where the icon size might change as well though, so I still need to call setButtonIcon(), even if in this case it doesn’t do anything other than update the internal sizes.