I'm trying to implement a notification system with LibGDX. As we know, Android has it's Toast which is great, but I can't use it in Desktop mode.
I tried extending Dialog etc, adding as an actor and it all worked, but there's one problem - it still needs to be within the 'stage' object range, cause I have to add the 'notification actor' to the stage.
Do you have any ideas how to implement it in a static way, that the notification dialog/label/window would be 'callable' from the whole app?
Here's my code:
public class Notification extends Dialog {
public enum NotificationType{
SUCCESS, FAIL, INFO;
}
static Skin skin = new Skin(Gdx.files.internal("data/notificationskin.json"));
Label label;
public Notification(String text, NotificationType type) {
super("", skin);
setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()/10);
setPosition(0, Gdx.graphics.getHeight() + getHeight());
label = new Label(text, skin);
add(label).center();
label.setColor(Color.BLUE);
setKeepWithinStage(false);
show();
}
public void show(){
MoveToAction moveTo = new MoveToAction();
moveTo.setPosition(getX(), Gdx.graphics.getHeight() - getHeight());
moveTo.setDuration(2f);
MoveToAction moveBack = new MoveToAction();
moveBack.setPosition(getX(), Gdx.graphics.getHeight() + getHeight());
moveBack.setDuration(2f);
SequenceAction action = new SequenceAction(moveTo, Actions.delay(1.0f), moveBack, Actions.removeActor());
addAction(action);
}
}
To show the toast I need to call:
Notification not = new Notification("You're now connected", SUCCESS);
stage.addActor(not);
which is bad - it should be static.