generated from CleanroomMC/TemplateDevEnv
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathTestGui.java
124 lines (109 loc) · 5.31 KB
/
TestGui.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
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
package com.cleanroommc.modularui.test;
import com.cleanroommc.modularui.ModularUI;
import com.cleanroommc.modularui.api.IPanelHandler;
import com.cleanroommc.modularui.api.drawable.IDrawable;
import com.cleanroommc.modularui.api.drawable.IKey;
import com.cleanroommc.modularui.drawable.GuiTextures;
import com.cleanroommc.modularui.screen.CustomModularScreen;
import com.cleanroommc.modularui.screen.ModularPanel;
import com.cleanroommc.modularui.screen.viewport.ModularGuiContext;
import com.cleanroommc.modularui.widget.Widget;
import com.cleanroommc.modularui.widgets.ButtonWidget;
import com.cleanroommc.modularui.widgets.Dialog;
import com.cleanroommc.modularui.widgets.SortableListWidget;
import com.cleanroommc.modularui.widgets.layout.Grid;
import com.cleanroommc.modularui.widgets.layout.Row;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import org.jetbrains.annotations.NotNull;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
@SideOnly(Side.CLIENT)
public class TestGui extends CustomModularScreen {
private List<String> lines;
private List<String> configuredOptions;
private Map<String, AvailableElement> availableElements;
@Override
public void onClose() {
ModularUI.LOGGER.info("New values: {}", this.configuredOptions);
}
@Override
public @NotNull ModularPanel buildUI(ModularGuiContext context) {
if (this.lines == null) {
this.lines = IntStream.range(0, 20).mapToObj(i -> "Option " + (i + 1)).collect(Collectors.toList());
this.configuredOptions = this.lines;
this.availableElements = new Object2ObjectOpenHashMap<>();
}
final Map<String, SortableListWidget.Item<String>> items = new Object2ObjectOpenHashMap<>();
for (String line : this.lines) {
items.put(line, new SortableListWidget.Item<>(line).child(item -> new Row()
.child(new Widget<>()
.addTooltipLine(line)
.background(GuiTextures.BUTTON_CLEAN)
.overlay(IKey.str(line))
.expanded().heightRel(1f))
.child(new ButtonWidget<>()
.onMousePressed(button -> item.removeSelfFromList())
.overlay(GuiTextures.CROSS_TINY.asIcon().size(10))
.width(10).heightRel(1f))));
}
SortableListWidget<String> sortableListWidget = new SortableListWidget<String>()
.children(configuredOptions, items::get)
.debugName("sortable list");
List<List<AvailableElement>> availableMatrix = Grid.mapToMatrix(2, this.lines, (index, value) -> {
AvailableElement availableElement = new AvailableElement().overlay(IKey.str(value))
.size(60, 14)
.addTooltipLine(value)
.onMousePressed(mouseButton1 -> {
if (this.availableElements.get(value).available) {
sortableListWidget.child(items.get(value));
this.availableElements.get(value).available = false;
}
return true;
});
this.availableElements.put(value, availableElement);
return availableElement;
});
for (String value : this.lines) {
this.availableElements.get(value).available = !this.configuredOptions.contains(value);
}
ModularPanel panel = ModularPanel.defaultPanel("test");
panel.child(sortableListWidget
.onRemove(stringItem -> this.availableElements.get(stringItem.getWidgetValue()).available = true)
.pos(10, 10)
.bottom(23)
.width(100));
IPanelHandler otherPanel = IPanelHandler.simple(panel, (mainPanel, player) -> {
ModularPanel panel1 = new Dialog<>("Option Selection").setDisablePanelsBelow(false).setDraggable(false).size(150, 120);
return panel1.child(ButtonWidget.panelCloseButton())
.child(new Grid()
.matrix(availableMatrix)
.scrollable()
.pos(7, 7).right(14).bottom(7).debugName("available list"));
}, true);
panel.child(new ButtonWidget<>()
.bottom(7).size(12, 12).leftRel(0.5f)
.overlay(GuiTextures.ADD)
.onMouseTapped(mouseButton -> {
otherPanel.openPanel();
return true;
}));
return panel;
}
private static class AvailableElement extends ButtonWidget<AvailableElement> {
private boolean available = true;
private final IDrawable activeBackground = GuiTextures.BUTTON_CLEAN;
private final IDrawable background = GuiTextures.SLOT_FLUID;
@Override
public AvailableElement background(IDrawable... background) {
throw new UnsupportedOperationException("Use overlay()");
}
@Override
public IDrawable getBackground() {
return this.available ? this.activeBackground : this.background;
}
}
}