summaryrefslogtreecommitdiff
path: root/src/main/java/net/tylermurphy/Minecraft/UI/UIComponent.java
blob: ffe39eb884679bb8d61ef76b9e3df70206444a92 (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
package net.tylermurphy.Minecraft.UI;

import java.util.ArrayList;
import java.util.List;

import org.joml.Matrix4f;
import org.joml.Vector2f;
import org.joml.Vector4f;

import net.tylermurphy.Minecraft.Render.Data.Display;
import net.tylermurphy.Minecraft.Util.Maths;

public abstract class UIComponent {
	
	private List<UIComponent> children = new ArrayList<UIComponent>();

	private Vector4f size = new Vector4f(0,100,0,100);
	private Vector4f position = new Vector4f(0,0,0,0);
	
	protected boolean enabled = true;
	
	private String key;
	
	private int zIndex = 1;
	
	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}
	
	public int getZIndex() {
		return zIndex;
	}

	public void setZIndex(int zIndex) {
		this.zIndex = zIndex;
	}
	
	public UIComponent findKey(String key) {
		if(this.key != null && this.key.equals(key)) return this;
		for(UIComponent component : children) {
			UIComponent c = component.findKey(key);
			if(c!=null) return c;
		}
		return null;
	}
	
	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	};
	
	public void add(UIComponent component) {
		children.add(component);
	};
	
	protected List<UIComponent> getComponents() {
		return children;
	};
	
	public void prepare(UIComponent parent) {
		if(this.enabled) UIMaster.componentBatch.add(this);
		for(UIComponent component : children) {
			component.prepare(component);
		}
	}
	
	public void setSize(float xScale, float xOffset, float yScale, float yOffset) {
		this.size = new Vector4f(xScale,xOffset,yScale,yOffset);
	}
	
	public Vector4f getSize() {
		return size;
	}
	
	public void setPosition(float xScale, float xOffset, float yScale, float yOffset) {
		this.position = new Vector4f(xScale,xOffset,yScale,yOffset);
	}
	
	public Vector4f getPosition() {
		return position;
	}
	
	public Vector2f getConvertedSize() {
		return new Vector2f(
				size.x + size.y/Display.getWidth(),
				size.z + size.w/Display.getHeight()
				);
	}
	
	public Vector2f getConvertedPosition() {
		return new Vector2f(
				(position.x + position.y/Display.getWidth()),
				(position.w/Display.getHeight() + position.z)
				);
	}
	
	public Matrix4f getMatrix() {
		return Maths.createTransformationMatrix(new Vector2f(getConvertedPosition().x*2-1,getConvertedPosition().y*2-1), getConvertedSize());
	}
	
}