-
Notifications
You must be signed in to change notification settings - Fork 0
/
UiElement.pde
154 lines (128 loc) · 3.65 KB
/
UiElement.pde
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// UiElement class
// Basic Interface element with basic touch/click/drag functionality
// Coded by Nicolai R. Tellefsen
public class UiElement {
//Physical dimensions
protected float x; //X position
protected float y; //Y position
protected float z; //Z position
protected float width; //Current width
protected float height; //Current height
//Virtual dimensions
protected int priority = 0; //Current priority (for comparison with other UiElements)
protected String id;
//Touch dimensions
protected boolean touchable = false; //Touch enabled?
protected boolean dragging = false; //Dragging enabled?
protected float touchX;
protected float touchY;
protected float touchWidth;
protected float touchHeight;
//Constructor, sets default values
UiElement(float _x, float _y, float _z, float _width, float _height) {
this.x = _x;
this.y = _y;
this.z = _z;
this.width = _width;
this.height = _height;
}
//Enable touch functionality
public boolean addTouch(float _touchX, float _touchY, float _touchWidth, float _touchHeight) {
this.touchX = _touchX;
this.touchY = _touchY;
this.touchWidth = _touchWidth;
this.touchHeight = _touchHeight;
this.touchable = true; //Touch has been enabled
return true;
}
public boolean addTouch() {
return addTouch(this.x,this.y,this.width,this.height);
}
//Disable touch functionality
public boolean removeTouch() {
this.touchable = false;
return true;
}
//Change the size of the touch area
public boolean changeTouchSize(float _touchWidth, float _touchHeight) {
this.touchWidth = _touchWidth;
this.touchHeight = _touchHeight;
return true;
}
//Change object priority
public void setPriority(int _priority) {
this.priority = _priority;
}
//Get object priority
public int getPriority() {
return this.priority;
}
//Change object id
public void setId(String _id) {
this.id = _id;
}
//Get object id
public String getId() {
return this.id;
}
//Check if current mouse/finger position is inside the current touch-area
public boolean isTouched() {
if(!this.touchable) {
return false;
}
if(mouseX >= this.touchX && mouseX <= this.touchX+this.touchWidth) {
if(mouseY >= this.touchY && mouseY <= this.touchY+this.touchHeight) {
return true; // The current mouse position is inside the touch area of the UiElement
}
}
return false;
}
//Placeholder function for subclasses to use, this will run whenever this UiElement is beeing touched
public void touch() {
}
//Start dragging
public void drag() {
if(!this.touchable) {
return;
}
this.dragging = true;
}
//Stop dragging
public void drop() {
if(!this.touchable) {
return;
}
this.dragging = false;
}
//How to handle mouse press
public void mousePressed() {
if(!this.touchable) {
return;
}
if(this.isTouched()) { //We are beeing touched
this.drag(); //Start dragging
this.touch(); //Run touch functionality
}
}
//How to handle mouse drag
public void mouseDragged() {
if(!this.touchable) {
return;
}
/*
if(this.isTouched() || this.dragging == true) { //We are beeing touched OR we are currently beeing dragged
this.touch(); //Run touch functionality
}
*/
if(this.dragging == true) { //We are currently beeing dragged
this.touch(); //Run touch functionality
}
}
//How to handle mouse release
public void mouseReleased() {
if(!this.touchable) {
return;
}
this.drop(); //Stop dragging
}
}