forked from AppliedEnergistics/GuideME
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLytSlot.java
More file actions
111 lines (90 loc) · 3.21 KB
/
Copy pathLytSlot.java
File metadata and controls
111 lines (90 loc) · 3.21 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
package guideme.document.block;
import guideme.document.LytRect;
import guideme.document.interaction.GuideTooltip;
import guideme.document.interaction.InteractiveElement;
import guideme.document.interaction.ItemTooltip;
import guideme.layout.LayoutContext;
import guideme.render.GuiAssets;
import guideme.render.GuiSprite;
import guideme.render.RenderContext;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Ingredient;
/**
* Renders a standard Minecraft GUI slot.
*/
public class LytSlot extends LytBlock implements InteractiveElement {
private static final int ITEM_SIZE = 16;
private static final int PADDING = 1;
private static final int LARGE_PADDING = 5;
public static final int OUTER_SIZE = ITEM_SIZE + 2 * PADDING;
public static final int OUTER_SIZE_LARGE = ITEM_SIZE + 2 * LARGE_PADDING;
private static final int CYCLE_TIME = 2000;
private boolean largeSlot;
private boolean visibleSlot = true;
private final ItemStack[] stacks;
public LytSlot(Ingredient ingredient) {
this.stacks = ingredient.getItems();
}
public LytSlot(ItemStack stack) {
this.stacks = new ItemStack[] { stack };
}
public boolean isLargeSlot() {
return largeSlot;
}
public void setLargeSlot(boolean largeSlot) {
this.largeSlot = largeSlot;
}
public boolean isSlotVisible() {return visibleSlot;}
public void setSlotVisible(boolean visibleSlot){this.visibleSlot = visibleSlot;}
@Override
protected LytRect computeLayout(LayoutContext context, int x, int y, int availableWidth) {
if (largeSlot) {
return new LytRect(x, y, OUTER_SIZE_LARGE, OUTER_SIZE_LARGE);
} else {
return new LytRect(x, y, OUTER_SIZE, OUTER_SIZE);
}
}
@Override
protected void onLayoutMoved(int deltaX, int deltaY) {
}
@Override
public void renderBatch(RenderContext context, MultiBufferSource buffers) {
}
@Override
public void render(RenderContext context) {
var x = bounds.x();
var y = bounds.y();
if (visibleSlot) {
GuiSprite texture;
if (largeSlot) {
texture = GuiAssets.LARGE_SLOT;
} else {
texture = GuiAssets.SLOT;
}
context.fillIcon(bounds, texture);
}
var padding = largeSlot ? LARGE_PADDING : PADDING;
var stack = getDisplayedStack();
if (!stack.isEmpty()) {
context.renderItem(stack, x + padding, y + padding, visibleSlot? 1: 0, ITEM_SIZE, ITEM_SIZE);
}
}
@Override
public Optional<GuideTooltip> getTooltip(float x, float y) {
var stack = getDisplayedStack();
if (stack.isEmpty()) {
return Optional.empty();
}
return Optional.of(new ItemTooltip(stack));
}
private ItemStack getDisplayedStack() {
if (stacks.length == 0) {
return ItemStack.EMPTY;
}
var cycle = System.nanoTime() / TimeUnit.MILLISECONDS.toNanos(CYCLE_TIME);
return stacks[(int) (cycle % stacks.length)];
}
}