0

I am trying to pause a progress bar. How would I go about it? I have a listener on a Pause button where I could do the pause but I cannot stop the animation.

Here is the code that currently runs the progress bar.

    Pixmap pixmap = new Pixmap(100, 50, Pixmap.Format.RGBA8888);
    pixmap.setColor(Color.CYAN);
    pixmap.fill();

    TextureRegionDrawable drawable = new TextureRegionDrawable(new TextureRegion(new Texture(pixmap)));
    pixmap.dispose();

    ProgressBar.ProgressBarStyle progressBarStyle = new ProgressBar.ProgressBarStyle();
    progressBarStyle.background = drawable;


    pixmap = new Pixmap(0, 50, Pixmap.Format.RGBA8888);
    pixmap.setColor(Color.RED);
    pixmap.fill();
    drawable = new TextureRegionDrawable(new TextureRegion(new Texture(pixmap)));
    pixmap.dispose();

    progressBarStyle.knob = drawable;

    pixmap = new Pixmap(100, 50, Pixmap.Format.RGBA8888);
    pixmap.setColor(Color.RED);
    pixmap.fill();
    drawable = new TextureRegionDrawable(new TextureRegion(new Texture(pixmap)));
    pixmap.dispose();

    progressBarStyle.knobBefore = drawable;

    ProgressBar bar = new ProgressBar(0.0f, 1.0f, 0.01f, false, progressBarStyle);
    bar.setBounds(100, 1600, 875, 50);
    bar.setAnimateDuration(25)

    bar.setValue(1f);

    stage.addActor(bar);
Alen Paul Varghese
  • 1,278
  • 14
  • 27
robs
  • 101
  • 1
  • 2
  • 10

1 Answers1

0

One option is to bypass the animation completely, update the progress bar from your render callback (by default libgdx combines these callbacks into one), and rely on the pause + resume callbacks at the application-level. Here's an example based on your initial code. Note the bar has no animation and a max value of 25, just to make the progress updates a bit more intuitive.

public class ProgressBarTest implements ApplicationListener {

    boolean active;
    SpriteBatch batch;
    Stage stage;
    int width = 380;
    int height = 180;
    ProgressBar bar;

    @Override
    public void create () {
        active = true;
        batch = new SpriteBatch();
        stage = new Stage(new FitViewport(width, height));

        Pixmap pixmap = new Pixmap(100, 50, Pixmap.Format.RGBA8888);
        pixmap.setColor(Color.CYAN);
        pixmap.fill();

        TextureRegionDrawable drawable = new TextureRegionDrawable(new TextureRegion(new Texture(pixmap)));
        pixmap.dispose();

        ProgressBar.ProgressBarStyle progressBarStyle = new ProgressBar.ProgressBarStyle();
        progressBarStyle.background = drawable;


        pixmap = new Pixmap(0, 50, Pixmap.Format.RGBA8888);
        pixmap.setColor(Color.RED);
        pixmap.fill();
        drawable = new TextureRegionDrawable(new TextureRegion(new Texture(pixmap)));
        pixmap.dispose();

        progressBarStyle.knob = drawable;

        pixmap = new Pixmap(100, 50, Pixmap.Format.RGBA8888);
        pixmap.setColor(Color.RED);
        pixmap.fill();
        drawable = new TextureRegionDrawable(new TextureRegion(new Texture(pixmap)));
        pixmap.dispose();

        progressBarStyle.knobBefore = drawable;

        bar = new ProgressBar(0f, 25f, 0.01f, false, progressBarStyle);
        bar.setBounds(100, 1600, 875, 50);

        stage.setRoot(new Table() {
            {
                add(bar);
                setFillParent(true);
                setSize(width, height);
            }
        });
    }

    @Override
    public void resize(int x, int y) {

    }

    @Override
    public void render () {
        if (active && bar.getValue() < bar.getMaxValue()){
            bar.setValue(bar.getValue() + Gdx.graphics.getDeltaTime());
            if (bar.getValue() >= bar.getMaxValue()){
                System.out.println("Filled up!");
            }
        }
        stage.act();

        ScreenUtils.clear(0, 0, 0, 1);
        batch.begin();
        stage.draw();
        batch.end();
    }


    @Override
    public void pause() {
        System.out.println("Paused!");
        active = false;
    }

    @Override
    public void resume() {
        System.out.println("Resumed!");
        active = true;
    }

    @Override
    public void dispose () {
        batch.dispose();
        stage.dispose();
    }

}

Maybe something like this works for your use case?

EDIT: Also, if you want to keep the animation, you can probably get away with conditionally calling stage.act() when active is true.

adketuri
  • 196
  • 5
  • Thanks for your help. Your mods work as a standalone project but when I add it to a project with a stage and other actors I cannot get it to draw the progress bar.. I have tried passing my current stage through but with no success. – robs Feb 07 '23 at 07:49
  • I did try your second suggestion ... conditionally calling stage.act. This worked. Thank you for your advice. – robs Feb 07 '23 at 10:56