-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathModularUI.java
More file actions
238 lines (192 loc) · 8.36 KB
/
Copy pathModularUI.java
File metadata and controls
238 lines (192 loc) · 8.36 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
package gregtech.api.gui;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableBiMap;
import com.google.common.collect.ImmutableList;
import gregtech.api.gui.resources.TextureArea;
import gregtech.api.gui.widgets.*;
import gregtech.api.gui.widgets.ProgressWidget.MoveType;
import gregtech.api.util.Position;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraftforge.items.IItemHandlerModifiable;
import java.util.ArrayList;
import java.util.List;
import java.util.function.DoubleSupplier;
import java.util.function.Supplier;
/**
* ModularUI is user-interface implementation concrete, based on widgets system
* Each widget acts unique and manage different things
* All widget information is synced to client from server for correct rendering
* Widgets and UI are both-sided, so widgets should equal on both sides
* However widget data will sync, widgets themselves, background, sizes and other important info will not
* To open and create ModularUI, see {@link UIFactory}
*/
public final class ModularUI implements ISizeProvider {
public final ImmutableBiMap<Integer, Widget> guiWidgets;
public final TextureArea backgroundPath;
private int screenWidth, screenHeight;
private final int width, height;
private final ImmutableList<Runnable> uiOpenCallback;
private final ImmutableList<Runnable> uiCloseCallback;
public boolean isJEIHandled;
/**
* UIHolder of this modular UI
*/
public final IUIHolder holder;
public final EntityPlayer entityPlayer;
public ModularUI(ImmutableBiMap<Integer, Widget> guiWidgets, ImmutableList<Runnable> openListeners, ImmutableList<Runnable> closeListeners, TextureArea backgroundPath, int width, int height, IUIHolder holder, EntityPlayer entityPlayer) {
this.guiWidgets = guiWidgets;
this.uiOpenCallback = openListeners;
this.uiCloseCallback = closeListeners;
this.backgroundPath = backgroundPath;
this.width = width;
this.height = height;
this.holder = holder;
this.entityPlayer = entityPlayer;
}
public List<Widget> getFlatVisibleWidgetCollection() {
List<Widget> widgetList = new ArrayList<>(guiWidgets.size());
for (Widget widget : guiWidgets.values()) {
widgetList.add(widget);
if (widget instanceof AbstractWidgetGroup)
widgetList.addAll(((AbstractWidgetGroup) widget).getContainedWidgets(false));
}
return widgetList;
}
public void updateScreenSize(int screenWidth, int screenHeight) {
this.screenWidth = screenWidth;
this.screenHeight = screenHeight;
Position displayOffset = new Position(getGuiLeft(), getGuiTop());
guiWidgets.values().forEach(widget -> widget.setParentPosition(displayOffset));
}
public void initWidgets() {
guiWidgets.values().forEach(widget -> {
widget.setGui(this);
widget.setSizes(this);
widget.initWidget();
});
}
public void triggerOpenListeners() {
uiOpenCallback.forEach(Runnable::run);
}
public void triggerCloseListeners() {
uiCloseCallback.forEach(Runnable::run);
}
public static Builder defaultBuilder() {
return new Builder(GuiTextures.BACKGROUND, 176, 166);
}
public static Builder defaultBuilder(int height) {
return new Builder(GuiTextures.BACKGROUND, 176, height);
}
public static Builder borderedBuilder() {
return new Builder(GuiTextures.BORDERED_BACKGROUND, 195, 136);
}
public static Builder extendedBuilder() {
return new Builder(GuiTextures.BACKGROUND, 176, 216);
}
public static Builder builder(TextureArea background, int width, int height) {
return new Builder(background, width, height);
}
@Override
public int getScreenWidth() {
return screenWidth;
}
@Override
public int getScreenHeight() {
return screenHeight;
}
@Override
public int getWidth() {
return width;
}
@Override
public int getHeight() {
return height;
}
/**
* Simple builder for ModularUI objects
*/
public static class Builder {
private ImmutableBiMap.Builder<Integer, Widget> widgets = ImmutableBiMap.builder();
private ImmutableList.Builder<Runnable> openListeners = ImmutableList.builder();
private ImmutableList.Builder<Runnable> closeListeners = ImmutableList.builder();
private TextureArea background;
private int width, height;
private int nextFreeWidgetId = 0;
public Builder(TextureArea background, int width, int height) {
Preconditions.checkNotNull(background);
this.background = background;
this.width = width;
this.height = height;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public Builder widget(Widget widget) {
Preconditions.checkNotNull(widget);
widgets.put(nextFreeWidgetId++, widget);
return this;
}
public Builder label(int x, int y, String localizationKey) {
return widget(new LabelWidget(x, y, localizationKey));
}
public Builder label(int x, int y, String localizationKey, int color) {
return widget(new LabelWidget(x, y, localizationKey, color, new Object[0]));
}
public Builder image(int x, int y, int width, int height, TextureArea area) {
return widget(new ImageWidget(x, y, width, height, area));
}
public Builder dynamicLabel(int x, int y, Supplier<String> text, int color) {
return widget(new DynamicLabelWidget(x, y, text, color));
}
public Builder slot(IItemHandlerModifiable itemHandler, int slotIndex, int x, int y, TextureArea... overlays) {
return widget(new SlotWidget(itemHandler, slotIndex, x, y).setBackgroundTexture(overlays));
}
public Builder progressBar(DoubleSupplier progressSupplier, int x, int y, int width, int height, TextureArea texture, MoveType moveType) {
return widget(new ProgressWidget(progressSupplier, x, y, width, height, texture, moveType));
}
public Builder bindPlayerInventory(InventoryPlayer inventoryPlayer) {
bindPlayerInventory(inventoryPlayer, GuiTextures.SLOT);
return this;
}
public Builder bindPlayerInventory(InventoryPlayer inventoryPlayer, int startY) {
bindPlayerInventory(inventoryPlayer, GuiTextures.SLOT, 7, startY);
return this;
}
public Builder bindPlayerInventory(InventoryPlayer inventoryPlayer, TextureArea imageLocation) {
return bindPlayerInventory(inventoryPlayer, imageLocation, 7, 84);
}
public Builder bindPlayerInventory(InventoryPlayer inventoryPlayer, TextureArea imageLocation, int x, int y) {
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 9; col++) {
this.widget(new SlotWidget(inventoryPlayer, col + (row + 1) * 9, x + col * 18, y + row * 18)
.setBackgroundTexture(imageLocation)
.setLocationInfo(true, false));
}
}
return bindPlayerHotbar(inventoryPlayer, imageLocation, x, y + 58);
}
public Builder bindPlayerHotbar(InventoryPlayer inventoryPlayer, TextureArea imageLocation, int x, int y) {
for (int slot = 0; slot < 9; slot++) {
this.widget(new SlotWidget(inventoryPlayer, slot, x + slot * 18, y)
.setBackgroundTexture(imageLocation)
.setLocationInfo(true, true));
}
return this;
}
public Builder bindOpenListener(Runnable onContainerOpen) {
this.openListeners.add(onContainerOpen);
return this;
}
public Builder bindCloseListener(Runnable onContainerClose) {
this.closeListeners.add(onContainerClose);
return this;
}
public ModularUI build(IUIHolder holder, EntityPlayer player) {
return new ModularUI(widgets.build(), openListeners.build(), closeListeners.build(), background, width, height, holder, player);
}
}
}