AlertBox not fully removing?

Hey,

Reporting a potential bug for Alert boxes in the GUI although it could be moreso a stupid thing on my end.

public class MessageBox extends AlertBox {
Screen screen;

public MessageBox(Screen screen, String text) {
    super(screen, "description", new Vector2f(200, 200));
    setMsg(text);
    this.screen = screen;
}   

@Override
public void onButtonOkPressed(MouseButtonEvent arg0, boolean arg1) {
    screen.removeElement(this);
    System.out.println("OK BUTTON PRESSED " + text);
}  

}

The method ‘onButtonOkPressed’ seems to get called when it shouldn’t be. Aka, after it has been created and on choosing ‘OK’ should be removed.

This method, which calls and creates it is definitely not called multiple times.
[java] public void displayDescriptionBox(String text) {
da = new MessageBox(screen, text);
screen.addElement(da);
setState(GUIState.DESCRIPTIONBOX);
}[/java]

I press ‘space bar’ on a specific object to get a description, the box opens and displays it. If I keep pressing space bar, the method is repeatedly called (but the description box isn’t created or displayed so it’s just the ‘OK’ method being called. The same text that was displayed get’s shown as well).

I changed my action button (the space bar) to something else so the space bar is not used in my code at all. Yet, space bar keeps calling ‘onButtonOkPressed’.

If I click either mouse button, it stops doing it. But it has to be the mouse buttons, none of the keyboard ones.

1 Like

For the space bar, buttons with tab focus response to this key input by default… you can override the onKey methods to bypass this, though, I will probably be updating how mapping is handled to avoid this sort of thing in the future.

[java]
@Override
public void onKeyPress(KeyInputEvent evt) { }
@Override
public void onKeyRelease(KeyInputEvent evt) { }
[/java]

As for the focus, this is supposed ot be reset on hide… however, I have apparently done something wrong. Let me see about updating this and I’ll post as soon as the fix is available.

I’ll also drop a note here once you are able to set the default key for simulating mouse clicks for controls. This will also have the option of setting it to null if you just don’t want this feature (or, I guess not-feature atm) :stuck_out_tongue:

No idea why I am posting this, but I didn’t realize that I had added a method to the Element class that is quite useful (I literally just stumbled across this again… apparently I failed to add JavaDoc info on this method. And the method is… drum-roll:

[java]
Element.centerToParent();
[/java]

It does just what it says! I have been setting the position of centered objects for ever now… instead of adding them with Vector2f.ZERO and then calling this method.

This has NOTHING to do with what you posted here… but it’s cool!

2 Likes
@avpeacock said: I press 'space bar' on a specific object to get a description, the box opens and displays it. If I keep pressing space bar, the method is repeatedly called (but the description box isn't created or displayed so it's just the 'OK' method being called. The same text that was displayed get's shown as well).

I changed my action button (the space bar) to something else so the space bar is not used in my code at all. Yet, space bar keeps calling ‘onButtonOkPressed’.

If I click either mouse button, it stops doing it. But it has to be the mouse buttons, none of the keyboard ones.

Are you removing the window from the Screen when the button is clicked? I just noticed you recreate the window each time (which is just fine), however hide() and hideWithEffect() do not destroy the window by default. They simple cal removeFromParent, leaving the reference in it’s parent’s list of child elements.

The way to get this to happen for the case above is:

[java]
// Create a new hide effect for the window.
Effect rem = new Effect(Effect.EffectEvent.Hide, Effect.EffectType.Fade, 0.25f);
rem.setDestroyOnHide(true);
// Add this to your window and it should overwrite the original effect.
do.addEffect(rem);
[/java]

1 Like
@t0neg0d said: Are you removing the window from the Screen when the button is clicked? I just noticed you recreate the window each time (which is just fine), however hide() and hideWithEffect() do not destroy the window by default. They simple cal removeFromParent, leaving the reference in it's parent's list of child elements.

The way to get this to happen for the case above is:

[java]
// Create a new hide effect for the window.
Effect rem = new Effect(Effect.EffectEvent.Hide, Effect.EffectType.Fade, 0.25f);
rem.setDestroyOnHide(true);
// Add this to your window and it should overwrite the original effect.
do.addEffect(rem);
[/java]

Ah that explains why the reference was still there when I thought I was removing it with screen.removeElement! Thanks!

@avpeacock said: Ah that explains why the reference was still there when I thought I was removing it with screen.removeElement! Thanks!

I think what is happening is, TabFocus is holding a reference to the button… which means, I still need to fix this, however, hopefully this will temporarily fix the issue in the meantime!

1 Like
@t0neg0d said: I *think* what is happening is, TabFocus is holding a reference to the button... which means, I still need to fix this, however, hopefully this will temporarily fix the issue in the meantime!

Hmm, i’ve shoved this in the constructor and no joy at the moment.

[java]Effect rem = new Effect(Effect.EffectType.FadeOut, Effect.EffectEvent.Hide, 0.25f);
rem.setDestroyOnHide(true);
addEffect(rem);[/java]

And no worries, I can still work around the problem another way if needbe :).

@avpeacock said: Hmm, i've shoved this in the constructor and no joy at the moment.

[java]Effect rem = new Effect(Effect.EffectType.FadeOut, Effect.EffectEvent.Hide, 0.25f);
rem.setDestroyOnHide(true);
addEffect(rem);[/java]

And no worries, I can still work around the problem another way if needbe :).

What happens when you call displayDescriptionBox a second time after closing the window?

EDIT: See next post

Omg… I’m so sorry…

This is supposed to get called but seems that it is not happening /boggle

[java]
screen.setKeyboardElement(null);
[/java]

In your button’s event handler.

@avpeacock
Just a quick update. I’ll be adding some updates over the next few days to try and help people fix these types of situations without having to wait for an update.

  1. I have not yet cleaned up the library, in the sense that element/variable scope is still in transition. Most will become protected allowing you to make adjustments when extending classes.
  2. Any default key binding added to controls will become adjustable. The keybinds was an initial pass to accommodate Forms and Tab Focus.

P.S. Did the suggestion above fix the issue for you? My local test here showed it working…

@t0neg0d said: What happens when you call displayDescriptionBox a second time after closing the window?

EDIT: See next post

I normally get errors about multiple elements of the same name on the screen. It was a bug I was aware of but have not yet fixed - should definitely update displayDescriptionBox to remove the element completely if it is on the screen.

Sorry for the delayed response - have been moving into a new house =). I’ll checkout the fix and respond as soon as things are setup at home. Thankyou so much! :slight_smile:

1 Like