3
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ Ugh I have some old code that does this exact thing (it matches appearance too!) but we have a power outage so I can't use my computer! I'll get back to you asap (whenever that might be...) and post it. \$\endgroup\$ – Charanor Apr 24 '17 at 10:21
3
\$\begingroup\$

I have implemented Android-like toast for my project and decided to share it with you! Enjoy: Toast LibGDX (GitHub).

Easy to use:

  1. Include Toast in your LibGDX project

  2. Create ToastFactory for your toasts (using Builder):

    Toast.ToastFactory toastFactory = new Toast.ToastFactory.Builder()
            .font(font)
            .build();
    
  3. Create toast: Toast toast = toastFactory.create("Hello world!", Toast.Length.LONG);

  4. Display it in render(): toast.render(Gdx.graphics.getDeltaTime());
\$\endgroup\$
  • \$\begingroup\$ Hi and welcome to GameDev.StackExchange! Generally, it's preferable to include in your answer's text at least a rough summary of the solution or approach you're recommending, so that the answer remains useful long into the future, even if the link changes/moves or is temporarily offline. You can use the edit link to add more details to your answer. \$\endgroup\$ – DMGregory Nov 6 '17 at 13:30
0
\$\begingroup\$

In your notification class, you use Actions which you can't have if Notification does not inherit from actor class (so you would have to recode those animations by hand). Actors can be drawn like that :

// Will call actor.draw(SpriteBatch batch)
stage.addActor(actor)

Or

actor.draw(batch)

In both case, you need a SpriteBatch to draw something. So you can't draw anything if you have no SpriteBatch, stage provide you one, but you can also have a SpriteBatch dissociated from stage.

In my opinion, you should go the way you do. I agree it looks less beautiful than Toast, but doing something like Toast that can interact with your display without using SpriteBatch will took to much time for doing what you can already do :).

But if you really want to do it that way, you should take a look at Views. For example, Google Play Leaderboards display a view above your stage, so i suppose you can display a view with your toast above your stage too.

Hope this will help !

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.