Updating resize borders

You may remember I posted a “setStyles” method a while back that rejigs a buttons style after it has been constructed. After restyling some of my buttons, I noticed this didn’t completely work. The problem seems to be ElementQuadGrid cannot have it’s resize borders changed after construction. This is my setStyles code …

[java]
public void setStyles(String styleName) {
final Style style = screen.getStyle(styleName);

    // images and state colours
    final String img = style.getString("defaultImg");
    if (img != null) {
        setColorMap(img);
    }
    if (style.getString("hoverImg") != null) {
        setButtonHoverInfo(
                style.getString("hoverImg"),
                style.getColorRGBA("hoverColor"));
    }
    if (style.getString("pressedImg") != null) {
        setButtonPressedInfo(
                style.getString("pressedImg"),
                style.getColorRGBA("pressedColor"));
    }

    // fonts and text
    setFontSize(style.getFloat("fontSize"));
    setFont(screen.getStyle("Font").getString(style.getString("fontName")));
    setFontColor(style.getColorRGBA("fontColor"));
    setTextVAlign(BitmapFont.VAlign.valueOf(style.getString("textVAlign")));
    setTextAlign(BitmapFont.Align.valueOf(style.getString("textAlign")));
    setTextWrap(LineWrapMode.valueOf(style.getString("textWrap")));
    buttonTextInsets = style.getFloat("buttonTextInsets");

    // borders
    borders.set(style.getVector4f("resizeBorders"));

    // audio
    hoverSound = style.getString("hoverSound");
    useHoverSound = style.getBoolean("useHoverSound");
    hoverSoundVolume = style.getFloat("hoverSoundVolume");
    pressedSound = style.getString("pressedSound");
    usePressedSound = style.getBoolean("usePressedSound");
    pressedSoundVolume = style.getFloat("pressedSoundVolume");

    // Fx
    LUtil.removeEffects(this);
    populateEffects(styleName);
    if (Screen.isAndroid()) {
        removeEffect(Effect.EffectEvent.Hover);
        removeEffect(Effect.EffectEvent.TabFocus);
        removeEffect(Effect.EffectEvent.LoseTabFocus);
    }

    defaultSize = style.getVector2f("defaultSize");
    setDimensions(getOrgDimensions());
    getModel().updateDimensions(getWidth(), getHeight());
    
    // TODO
    // need to somehow tell ElementQuadGrid to update resize borders?

    originalFontColor = fontColor.clone();
}

[/java]

I’ve tried various hackery, including reconstructing ElementQuadGrid and setting it back in Element. Any thoughts?

RR

@rockfire
Oh sweet… yes I do remember this. Before I forget again… because I do… I’ll get this added.

I believe Element has a few setResizeBorders methods that rebuild the mesh. Let me find a specific example…

Here it is: This one is internal to element…

[java]
this.model = new ElementQuadGrid(this.dimensions, borders, imgWidth, imgHeight, pixelWidth, pixelHeight, textureAtlasX, textureAtlasY, textureAtlasW, textureAtlasH);
geom.setMesh(model);
[/java]

Seeeew… I can do one of two things here. Set up a public method for changing this (which may be needed at some point)… or add this rebuild to the method above. Thoughts?

I’m easy either way, as long as I can change the dimensions and the resize borders in a subclass of Button (and others?), I’d be happy camper :slight_smile: If I had to pick, maybe the public method for changing it. This works ok for the dimensions.

@rockfire
Added a rebuildModel() method to Element.
Added the above to Button, however… I had to change

buttonTextInsets = style.getFloat(“buttonTextInsets”);

to

setTextPaddingByKey(“Button”,“textPadding”);

You can add a Vector4f or Float value to the style to adjust the text position with this. Though, it isn’t required.

And I didn’t know what to do with:

buttonTextInsets = style.getFloat(“buttonTextInsets”);

Cool! Oh ignore buttonTextInsets. That is used in my custom layout aware components to get a calculated size. Pretend it isn’t there.

PS. Is it me or is block quoting broken on this forum, every time I use it an submit, it says “you have just said that” or some such nonsense.

It seems to be… I reported the issue in case no one else did.

I had answered one of your posts with something far too long for humans to stay interested and it got et’ So… I gave up. >.<

:oops: Aww pity. I would have read it too! I am not sure if this means anything. :expressionless:

1 Like

Awesome, this works! Resize borders are updated correctly too. I have however hit a tiny bug, I have one button that has NO background image, just an icon. This throws an NPE when setting the style.

[java]

SEVERE Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
java.lang.NullPointerException
at tonegod.gui.core.Element.rebuildModel(Element.java:2517)
at tonegod.gui.controls.buttons.Button.setStyles(Button.java:874)
at org.iceui.controls.FancyWindow.<init>(FancyWindow.java:274)
[/java]

This also makes me think it needs to handle the case where a background image is removed (set to null?). It seems like it currently doesn’t.

EDIT: Oh, just noticed the commited version doesn’t remove existing effects.

@rockfire said: Awesome, this works! Resize borders are updated correctly too. I have however hit a tiny bug, I have one button that has NO background image, just an icon. This throws an NPE when setting the style.

[java]

SEVERE Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
java.lang.NullPointerException
at tonegod.gui.core.Element.rebuildModel(Element.java:2517)
at tonegod.gui.controls.buttons.Button.setStyles(Button.java:874)
at org.iceui.controls.FancyWindow.<init>(FancyWindow.java:274)
[/java]

This also makes me think it needs to handle the case where a background image is removed (set to null?). It seems like it currently doesn’t.

EDIT: Oh, just noticed the commited version doesn’t remove existing effects.

Eh… you’re right on the effects. I was thinking replacing (which happens automatically)… forgetting that there might not be anything to replace it. My bad.

And yes, I’ll take care of the other. Is this the case where setAsContainerOnly() has been called? Or are there other reasons this might happen? (That you can think of)

@t0neg0d said: Eh... you're right on the effects. I was thinking replacing (which happens automatically)... forgetting that there might not be anything to replace it. My bad.

And yes, I’ll take care of the other. Is this the case where setAsContainerOnly() has been called? Or are there other reasons this might happen? (That you can think of)

Mm k. Not a container no, its just a button that has NO “defaultImg” in the style, but it does have an icon set in code. I assume this is a valid thing to do?

@rockfire said: Mm k. Not a container no, its just a button that has NO "defaultImg" in the style, but it does have an icon set in code. I assume this is a valid thing to do?

It’s more efficient to use setAsContIanerOnly… only because the render component is removed. However, in it’s current state, this would still bomb out.

I’ll play around with this today and make sure that it accounts for all of this.