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

import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.math.Rectangle;

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

public class Root extends Container
{
    private Element touched;
    private boolean resized;

    public Root(int c)
    {
        this.alignment = Alignment.ABSOLUTE;
        this.children = new IterableSet<Element>(c);
    }

    public void resize(Rectangle r)
    {
        resize(r.x, r.y, r.width, r.height);
    }

    public void resize(float width, float height)
    {
        resize(getX(), getY(), width, height);
    }

    public void resize(float x, float y, float width, float height)
    {
        setPosition(x, y, width, height);
        resized = true;
    }

    @Override public void computeGeometry(Rectangle area, boolean resized)
    {
        rect.x = x;
        rect.y = y;
        innerRect.set(getInnerX(), getInnerY(), getInnerWidth(), getInnerHeight());
        super.computeGeometry(innerRect, resized);
    }

    public Element touched()
    {
        return touched;
    }

    @Override public void draw(Batch batch)
    {
        if (dirty || damaged || resized) {
            computeGeometry(null, resized);
            resized = false;
        }
        super.draw(batch);
    }

    @Override public Element touch(float x, float y)
    {
        touched = super.touch(x, y);
        return touched;
    }

    @Override public boolean drag(float x, float y, int dx, int dy)
    {
        if (touched != null && touched != this) {
            if (touched.drag(x, y, dx, dy))
                return true;
            touched = null;
        }
        return false;
    }
}