summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/rustanddust/engine/gfx/StackedImages.java
blob: 1e1dbcf312298697c2cf4dca16c405190c9ddfb3 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package ch.asynk.rustanddust.engine.gfx;

import com.badlogic.gdx.utils.Disposable;

import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.utils.Array;

public class StackedImages implements Drawable, Disposable
{
    private boolean enabled[];
    private Array<Sprite> sprites;

    public StackedImages(TextureAtlas atlas)
    {
        this.sprites = atlas.createSprites();
        this.enabled = new boolean[sprites.size];
    }

    @Override
    public void dispose()
    {
    }

    public void disableAll()
    {
        for (int i = 0; i < sprites.size; i++)
            enabled[i] = false;
    }

    public void enable(int i, boolean enable)
    {
        enabled[i] = enable;
    }

    public boolean isEnabled(int i)
    {
        return enabled[i];
    }

    public boolean isEnabled()
    {
        for (int i = 0; i < sprites.size; i++)
            if (enabled[i]) return true;
        return false;
    }

    public void setScale(float scale)
    {
        for (int i = 0, n = sprites.size; i < n; i++)
            sprites.get(i).setScale(scale);
    }

    public void setAlpha(float alpha)
    {
        for (int i = 0, n = sprites.size; i < n; i++)
            sprites.get(i).setAlpha(alpha);
    }

    public void rotate(int i, float r)
    {
        sprites.get(i).setRotation(r);
    }

    public void setRotation(float r)
    {
        for (int i = 0, n = sprites.size; i < n; i++)
            sprites.get(i).setRotation(r);
    }

    public void translate(float dx, float dy)
    {
        for (int i = 0, n =  sprites.size; i < n; i++)
            sprites.get(i).translate(dx, dy);
    }

    public void centerOn(float cx, float cy)
    {
        for (int i = 0, n = sprites.size; i < n; i++) {
            float x = (cx - (sprites.get(i).getWidth() / 2f));
            float y = (cy - (sprites.get(i).getHeight() / 2f));
            sprites.get(i).setPosition(x, y);
        }
    }

    @Override
    public void draw(Batch batch)
    {
        for (int i = 0, n = sprites.size; i < n; i++) {
            if (enabled[i])
                sprites.get(i).draw(batch);
        }
    }

    @Override
    public void drawDebug(ShapeRenderer shapes)
    {
        for (int i = 0, n = sprites.size; i < n; i++) {
            if (enabled[i]) {
                Sprite sprite = sprites.get(0);
                float w = sprite.getWidth();
                float h = sprite.getHeight();
                shapes.rect(sprite.getX(), sprite.getY(), (w / 2f), (h / 2f), w, h, sprite.getScaleX(), sprite.getScaleY(), sprite.getRotation());
                return;
            }
        }
    }
}