summaryrefslogtreecommitdiffstats
path: root/core/src/ch/asynk/tankontank/ui/Widget.java
blob: e49833a07ee1fc6954b0365ce2cdad7528c1402c (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
package ch.asynk.tankontank.ui;

import com.badlogic.gdx.utils.Disposable;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;

import ch.asynk.tankontank.engine.gfx.Drawable;

public abstract class Widget implements Disposable, Drawable
{
    public boolean blocked;
    public boolean visible;
    protected float padding;
    protected Rectangle rect;
    protected Position position;
    protected Widget parent;

    protected Widget()
    {
        this.parent = null;
        this.blocked = false;
        this.visible = true;
        this.padding = 0f;
        this.rect = new Rectangle(0, 0, 0, 0);
        this.position = Position.MIDDLE_CENTER;
    }

    public float getX() { return rect.x; }
    public float getY() { return rect.y; }
    public float getWidth() { return rect.width; }
    public float getHeight() { return rect.height; }

    public void translate(float dx, float dy)
    {
        rect.x += dx;
        rect.y += dy;
    }

    public void setPosition(Rectangle r)
    {
        rect.set(r);
    }

    public void setPosition(float x, float y)
    {
        rect.x = x;
        rect.y = y;
    }

    public void setPosition(float x, float y, float w, float h)
    {
        rect.set(x, y, w, h);
    }

    public void setPosition(Position position)
    {
        this.position = position;
        setParent(this.parent);
    }

    public void setPosition(Position position, Widget parent)
    {
        this.position = position;
        setParent(parent);
    }

    public void setParent(Widget parent)
    {
        this.parent = parent;
        if (parent == null) {
            rect.x = position.getX(rect.width);
            rect.y = position.getY(rect.height);
        } else {
            rect.x = position.getX(parent, rect.width);
            rect.y = position.getY(parent, rect.height);
        }
        // might trigger something if overriden
        setPosition(rect.x, rect.y);
    }

    public boolean hit(float x, float y)
    {
        if (blocked || !visible) return false;
        return rect.contains(x, y);
    }

    @Override
    public void drawDebug(ShapeRenderer shapes)
    {
        if (!visible) return;
        shapes.rect(rect.x, rect.y, rect.width, rect.height);
    }
}