diff options
| author | shedaniel <daniel@shedaniel.me> | 2021-03-09 21:30:12 +0800 |
|---|---|---|
| committer | shedaniel <daniel@shedaniel.me> | 2021-03-09 21:30:12 +0800 |
| commit | 64bc9937d6ec04c6d66240a84b4fb345026c0b12 (patch) | |
| tree | 62e055c5cc64875300bdf69ac9a4400fb4b28dd9 /runtime/src/main/java/me/shedaniel/rei/gui/CurrentDraggingStack.java | |
| parent | 69d386214f5d3471a3ef1e5533037cdc32648c57 (diff) | |
| download | RoughlyEnoughItems-64bc9937d6ec04c6d66240a84b4fb345026c0b12.tar.gz RoughlyEnoughItems-64bc9937d6ec04c6d66240a84b4fb345026c0b12.tar.bz2 RoughlyEnoughItems-64bc9937d6ec04c6d66240a84b4fb345026c0b12.zip | |
Very primitive background rendering for JEI plugins
Signed-off-by: shedaniel <daniel@shedaniel.me>
Diffstat (limited to 'runtime/src/main/java/me/shedaniel/rei/gui/CurrentDraggingStack.java')
| -rw-r--r-- | runtime/src/main/java/me/shedaniel/rei/gui/CurrentDraggingStack.java | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/runtime/src/main/java/me/shedaniel/rei/gui/CurrentDraggingStack.java b/runtime/src/main/java/me/shedaniel/rei/gui/CurrentDraggingStack.java new file mode 100644 index 000000000..6f464100e --- /dev/null +++ b/runtime/src/main/java/me/shedaniel/rei/gui/CurrentDraggingStack.java @@ -0,0 +1,125 @@ +package me.shedaniel.rei.gui; + +import com.mojang.blaze3d.vertex.PoseStack; +import me.shedaniel.math.Point; +import me.shedaniel.math.Rectangle; +import me.shedaniel.math.impl.PointHelper; +import me.shedaniel.rei.RoughlyEnoughItemsCore; +import me.shedaniel.rei.api.gui.drag.DraggableStack; +import me.shedaniel.rei.api.gui.drag.DraggableStackProvider; +import me.shedaniel.rei.api.gui.drag.DraggableStackVisitor; +import me.shedaniel.rei.api.gui.drag.DraggingContext; +import me.shedaniel.rei.api.gui.widgets.Widget; +import me.shedaniel.rei.gui.widget.LateRenderable; +import net.minecraft.client.gui.components.events.GuiEventListener; +import org.jetbrains.annotations.Nullable; + +import java.util.Collections; +import java.util.List; +import java.util.Optional; +import java.util.function.Supplier; + +public class CurrentDraggingStack extends Widget implements LateRenderable, DraggingContext { + private DraggableStackProvider provider; + private DraggableStackVisitor visitor; + @Nullable + private DraggableEntry entry; + + public void set(DraggableStackProvider provider, DraggableStackVisitor visitor) { + this.provider = provider; + this.visitor = visitor; + } + + @Override + public void render(PoseStack matrices, int mouseX, int mouseY, float delta) { + if (entry != null && entry.dragging) { + if (!RoughlyEnoughItemsCore.isLeftModePressed) { + drop(); + return; + } + matrices.pushPose(); + matrices.translate(0, 0, 600); + entry.stack.render(matrices, new Rectangle(mouseX - 8, mouseY - 8, 16, 16), mouseX, mouseY, delta); + matrices.popPose(); + } + } + + @Override + public List<? extends GuiEventListener> children() { + return Collections.emptyList(); + } + + @Override + public boolean mouseClicked(double mouseX, double mouseY, int button) { + drop(); + DraggableStack hoveredStack = provider.getHoveredStack(this, mouseX, mouseY); + if (hoveredStack != null) { + entry = new DraggableEntry(hoveredStack, new Point(mouseX, mouseY)); + return true; + } + return false; + } + + @Override + public boolean mouseReleased(double d, double e, int i) { + return false; + } + + @Override + public boolean mouseDragged(double mouseX1, double mouseY1, int button, double mouseX2, double mouseY2) { + if (entry != null && !entry.dragging) { + Point startPoint = entry.start; + double xDistance = Math.abs(startPoint.x - mouseX1); + double yDistance = Math.abs(startPoint.y - mouseY1); + double requiredDistance = 4; + + if (xDistance * xDistance + yDistance * yDistance > requiredDistance * requiredDistance) { + entry.dragging = true; + entry.stack.drag(); + } + } + + return entry != null; + } + + private boolean drop() { + if (entry != null && entry.dragging) { + Optional<DraggableStackVisitor.Acceptor> acceptor = visitor.visitDraggedStack(entry.stack); + entry.stack.release(acceptor.isPresent()); + acceptor.ifPresent(a -> a.accept(entry.stack)); + entry = null; + return true; + } + + entry = null; + return false; + } + + @Override + @Nullable + public DraggableStack getCurrentStack() { + return entry != null && entry.dragging ? entry.stack : null; + } + + @Override + @Nullable + public Point getCurrentPosition() { + return isDraggingStack() ? PointHelper.ofMouse() : null; + } + + @Override + public void registerRenderBackToPosition(DraggableStack stack, Supplier<Point> position) { + + } + + private class DraggableEntry { + private final DraggableStack stack; + private final Point start; + private boolean dragging = false; + + private DraggableEntry(DraggableStack stack, Point start) { + this.stack = stack; + this.start = start; + } + } +} |
