summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/gdx/boardgame/ui/Scissors.java
blob: 096b883eda7074ec711a490e7101a4a409784fa1 (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
package ch.asynk.gdx.boardgame.ui;

import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.scenes.scene2d.utils.ScissorStack;

import ch.asynk.gdx.boardgame.utils.IterableArray;

public class Scissors
{
    private static IterableArray<Rectangle> array = new IterableArray<Rectangle>(5);

    private static Camera camera;
    private static Batch batch;

    public static void clear()
    {
        array.clear();
    }

    public static void invalidate()
    {
        for (int i = 0; i < array.size(); i++)
            array.replace(null, i);
    }

    public static void register(Camera _camera, Batch _batch)
    {
        camera = _camera;
        batch = _batch;
        invalidate();
    }

    public static int compute(int idx, Rectangle area)
    {
        if (idx < 0) {
            array.add(compute(area));
            return array.size() - 1;
        } else {
            array.replace(compute(area), idx);
            return idx;
        }
    }

    public static Rectangle get(int idx, Rectangle area)
    {
        Rectangle r = array.get(idx);
        if (r == null) {
            array.replace(compute(area), idx);
            r = array.get(idx);
        }
        return r;
    }

    public static Rectangle compute(Rectangle area)
    {
        Rectangle scissor = new Rectangle();
        compute(area, scissor);
        return scissor;
    }

    public static void compute(Rectangle area, Rectangle scissor)
    {
        ScissorStack.calculateScissors(camera, batch.getTransformMatrix(), area, scissor);
    }
}