-
Notifications
You must be signed in to change notification settings - Fork 364
Expand file tree
/
Copy pathAbstractWidget.java
More file actions
355 lines (312 loc) · 9.7 KB
/
AbstractWidget.java
File metadata and controls
355 lines (312 loc) · 9.7 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package com.gregtechceu.gtceu.api.mui.widget;
import com.gregtechceu.gtceu.api.mui.base.widget.IDelegatingWidget;
import com.gregtechceu.gtceu.api.mui.base.widget.INotifyEnabled;
import com.gregtechceu.gtceu.api.mui.base.widget.IWidget;
import com.gregtechceu.gtceu.api.mui.widget.sizer.Area;
import com.gregtechceu.gtceu.api.mui.widget.sizer.StandardResizer;
import com.gregtechceu.gtceu.client.mui.screen.ModularPanel;
import com.gregtechceu.gtceu.client.mui.screen.ModularScreen;
import com.gregtechceu.gtceu.client.mui.screen.viewport.ModularGuiContext;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.MustBeInvokedByOverriders;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Objects;
/**
* Very basic implementation of {@link IWidget}.
*/
public abstract class AbstractWidget implements IWidget {
// gui context
private boolean valid = false;
private IWidget parent = null;
private ModularPanel panel = null;
private ModularGuiContext context = null;
@Nullable
private String name;
private boolean enabled = true;
private int timeHovered = -1;
private int timeBelowMouse = -1;
private final Area area = new Area();
private StandardResizer resizer;
/**
* Returns the screen of the panel of this widget is being opened in.
*
* @return the screen of this widget
* @throws IllegalStateException if {@link #isValid()} returns false
*/
@Override
public ModularScreen getScreen() {
return getPanel().getScreen();
}
@Override
public void scheduleResize() {
this.resizer.markDirty();
}
@Override
public boolean requiresResize() {
return this.resizer.requiresResize();
}
/**
* Called when a panel is opened. Use {@link #onInit()} and {@link #afterInit()} for custom logic.
*
* @param parent the parent this element belongs to
* @param late true if this is called some time after the widget tree of the parent has been initialised
*/
@ApiStatus.Internal
@Override
public final void initialise(@NotNull IWidget parent, boolean late) {
this.timeHovered = -1;
this.timeBelowMouse = -1;
if (this.resizer == null) {
throw new IllegalStateException(
"Resizer must be set before the widget initializes! Affected widget: " + this);
}
if (!(this instanceof ModularPanel)) {
this.parent = parent;
this.panel = parent.getPanel();
this.context = parent.getContext();
getArea().z(parent.getArea().z() + 1);
if (parent instanceof AbstractWidget aw) {
this.resizer.initialize(aw.resizer, parent.getScreen().getResizeNode());
} else {
this.resizer.initialize(parent.resizer(), parent.getScreen().getResizeNode());
}
}
this.valid = true;
onInitInternal(late);
onInit();
if (hasChildren()) {
for (IWidget child : getChildren()) {
child.initialise(this, false);
}
}
afterInit();
onResized();
}
void onInitInternal(boolean late) {}
/**
* Called after this widget is initialised and before the children are initialised.
*/
@ApiStatus.OverrideOnly
public void onInit() {}
/**
* Called after this widget is initialised and after the children are initialised.
*/
@ApiStatus.OverrideOnly
public void afterInit() {}
/**
* Called when this widget is removed from the widget tree or after the panel is closed.
* Overriding this is fine, but super must be called.
*/
@MustBeInvokedByOverriders
@Override
public void dispose() {
if (hasChildren()) {
for (IWidget child : getChildren()) {
child.dispose();
}
}
if (!(this instanceof ModularPanel)) {
this.panel = null;
this.parent = null;
this.context = null;
}
resizer().dispose();
this.timeHovered = -1;
this.timeBelowMouse = -1;
this.valid = false;
}
// -------------------
// === Gui context ===
// -------------------
/**
* Returns if this widget is currently part of an open panel. Only if this is true information about parent, panel
* and gui context can
* be obtained.
*
* @return true if this widget is part of an open panel
*/
@Override
public boolean isValid() {
return valid;
}
@Override
public void onUpdate() {
if (isHovering()) this.timeHovered++;
if (isBelowMouse()) this.timeBelowMouse++;
}
@MustBeInvokedByOverriders
@Override
public void onMouseStartHover() {
this.timeHovered = 0;
}
@MustBeInvokedByOverriders
@Override
public void onMouseEndHover() {
this.timeHovered = -1;
}
@MustBeInvokedByOverriders
@Override
public void onMouseEnterArea() {
this.timeBelowMouse = 0;
}
@MustBeInvokedByOverriders
@Override
public void onMouseLeaveArea() {
this.timeBelowMouse = -1;
}
@Override
public boolean isHoveringFor(int ticks) {
return timeHovered >= ticks;
}
@Override
public boolean isBelowMouseFor(int ticks) {
return timeBelowMouse >= ticks;
}
public int getTicksHovered() {
return timeHovered;
}
public int getTicksBelowMouse() {
return timeBelowMouse;
}
/**
* Returns the area of this widget. This contains information such as position, size, relative position to parent,
* padding and margin.
* Even tho this is a mutable object, you should refrain from modifying the values.
*
* @return area of this widget
*/
@Override
public Area getArea() {
return area;
}
/**
* Shortcut to get the area of the parent
*
* @return parent area
*/
public Area getParentArea() {
IWidget parent = getParent();
while (parent instanceof IDelegatingWidget dw) {
parent = dw.getParent();
}
return parent.getArea();
}
/**
* Returns if this widget is currently enabled. Disabled widgets (and all its children) are not rendered and can't
* be interacted with.
*
* @return true if this widget is enabled.
*/
@Override
public boolean isEnabled() {
return this.enabled;
}
/**
* Sets enabled state. Disabled widgets (and all its children) are not rendered and can't be interacted with.
*
* @param enabled enabled state
*/
@Override
public void setEnabled(boolean enabled) {
if (this.enabled != enabled) {
this.enabled = enabled;
if (isValid() && getParent() instanceof INotifyEnabled notifyEnabled) {
notifyEnabled.onChildChangeEnabled(this, enabled);
}
}
}
/**
* Returns the parent of this widget. If this is a {@link ModularPanel} this will always return null contrary to the
* annotation.
*
* @return the screen of this widget
* @throws IllegalStateException if {@link #isValid()} returns false
*/
@Override
public @NotNull IWidget getParent() {
if (!isValid()) {
throw new IllegalStateException(this + " is not in a valid state!");
}
return parent;
}
/**
* Returns the gui context of the screen this widget is part of.
*
* @return the screen of this widget
* @throws IllegalStateException if {@link #isValid()} returns false
*/
@Override
public ModularGuiContext getContext() {
if (!isValid()) {
throw new IllegalStateException(this + " is not in a valid state!");
}
return context;
}
/**
* Used to set the gui context on panels internally.
*/
@ApiStatus.Internal
protected final void setContext(ModularGuiContext context) {
this.context = context;
}
/**
* Returns the panel of this widget is being opened in.
*
* @return the screen of this widget
* @throws IllegalStateException if {@link #isValid()} returns false
*/
@Override
public @NotNull ModularPanel getPanel() {
if (!isValid()) {
throw new IllegalStateException(this + " is not in a valid state!");
}
return panel;
}
@Override
public @NotNull StandardResizer resizer() {
return this.resizer;
}
protected final StandardResizer rawResizer() {
return this.resizer;
}
protected void resizer(StandardResizer resizer) {
Objects.requireNonNull(resizer);
if (this.resizer == resizer) return;
if (isValid() && this.resizer != null) {
resizer.replacementOf(this.resizer);
}
this.resizer = resizer;
}
@Override
public @Nullable String getName() {
return name;
}
protected void setName(String name) {
this.name = name;
}
public boolean isName(String name) {
return Objects.equals(name, this.name);
}
public boolean nameContains(String part) {
return this.name != null && this.name.contains(part);
}
/**
* This is only used in {@link #toString()}.
*
* @return the simple class name or other fitting name
*/
protected String getTypeName() {
return getClass().getSimpleName();
}
/**
* @return the simple class plus the debug name if set
*/
@Override
public String toString() {
if (getName() != null) {
return getTypeName() + "#" + getName();
}
return getTypeName();
}
}