/
DragRouter.java
51 lines (41 loc) · 1.39 KB
/
DragRouter.java
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
package mezz.jei.common.input.handlers;
import mezz.jei.common.input.IDragHandler;
import mezz.jei.common.input.UserInput;
import net.minecraft.client.gui.screens.Screen;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.Optional;
public class DragRouter {
private final List<IDragHandler> handlers;
@Nullable
private IDragHandler dragStartedCallback;
public DragRouter(IDragHandler... handlers) {
this.handlers = List.of(handlers);
}
public void handleGuiChange() {
cancelDrag();
}
public boolean startDrag(Screen screen, UserInput input) {
cancelDrag();
this.dragStartedCallback = this.handlers.stream()
.map(i -> i.handleDragStart(screen, input))
.flatMap(Optional::stream)
.findFirst()
.orElse(null);
return this.dragStartedCallback != null;
}
public boolean completeDrag(Screen screen, UserInput input) {
if (this.dragStartedCallback == null) {
return false;
}
boolean result = this.dragStartedCallback.handleDragComplete(screen, input);
this.dragStartedCallback = null;
return result;
}
public void cancelDrag() {
if (this.dragStartedCallback != null) {
this.dragStartedCallback.handleDragCanceled();
this.dragStartedCallback = null;
}
}
}