summaryrefslogtreecommitdiff
path: root/core/src/kz/ilotterytea/maxon/ui/AnimatedImage.java
blob: 97cd354d4853d24f4f3db59ff0e652bf1f42de83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package kz.ilotterytea.maxon.ui;

import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.Disposable;

import java.util.ArrayList;

public class AnimatedImage extends Image implements Disposable {
    private final ArrayList<TextureRegion> regions;
    private final int fps;
    private int index = 0, seconds = 0;

    private final boolean stopAnim = false;

    public AnimatedImage(ArrayList<TextureRegion> regions, int fps) {
        super(regions.get(0));
        this.regions = regions;
        this.fps = fps;
    }

    @Override public void act(float delta) {
        if (!stopAnim && seconds >= fps) {
            if (index > regions.size() - 1) {
                index = 0;
            }
            try {
                if (regions.get(index + 1) == null) {
                    index = 0;
                }
            } catch (IndexOutOfBoundsException ignored) {}

            super.setDrawable(new TextureRegionDrawable(regions.get(index)));
            index++;
            seconds = 0;
        }

        seconds++;
        super.act(delta);
    }

    public TextureRegion getFrame(int index) { return regions.get(index); }

    @Override public void dispose() {
        for (TextureRegion reg : regions) {
            if (reg != null) {
                reg.getTexture().dispose();
            }
        }
    }
}