/* * This file is licensed under the MIT License, part of Roughly Enough Items. * Copyright (c) 2018, 2019, 2020 shedaniel * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ 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.client.gui.drag.DraggableStack; import me.shedaniel.rei.api.client.gui.drag.DraggableStackProvider; import me.shedaniel.rei.api.client.gui.drag.DraggableStackVisitor; import me.shedaniel.rei.api.client.gui.drag.DraggingContext; import me.shedaniel.rei.api.client.gui.widgets.Widget; import me.shedaniel.rei.gui.widget.LateRenderable; import me.shedaniel.rei.impl.Animator; import net.minecraft.client.gui.components.events.GuiEventListener; import org.jetbrains.annotations.Nullable; import java.util.*; import java.util.function.Supplier; public class CurrentDraggingStack extends Widget implements LateRenderable, DraggingContext { private DraggableStackProvider provider; private DraggableStackVisitor visitor; @Nullable private DraggableEntry entry; private final List backToOriginals = new ArrayList<>(); 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(); } Iterator iterator = backToOriginals.iterator(); while (iterator.hasNext()) { RenderBackEntry renderBackEntry = iterator.next(); renderBackEntry.update(delta); if (Math.abs(renderBackEntry.x.doubleValue() - renderBackEntry.x.target()) <= 2 && Math.abs(renderBackEntry.y.doubleValue() - renderBackEntry.y.target()) <= 2) { iterator.remove(); } else { matrices.pushPose(); matrices.translate(0, 0, 600); renderBackEntry.stack.render(matrices, new Rectangle(renderBackEntry.x.intValue(), renderBackEntry.y.intValue(), 16, 16), mouseX, mouseY, delta); matrices.popPose(); } } } @Override public List 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 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 renderBackToPosition(DraggableStack stack, Point initialPosition, Supplier position) { backToOriginals.add(new RenderBackEntry(stack, initialPosition, 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; } } private static class RenderBackEntry { private final DraggableStack stack; private final Supplier position; private Animator x = new Animator(); private Animator y = new Animator(); private int lastDestination = -1; public RenderBackEntry(DraggableStack stack, Point initialPosition, Supplier position) { this.stack = stack; this.x.setAs(initialPosition.x - 8); this.y.setAs(initialPosition.y - 8); this.position = position; } public Point getPosition() { return position.get(); } public void update(double delta) { this.x.update(delta); this.y.update(delta); Point position = getPosition(); if (lastDestination != position.hashCode()) { lastDestination = position.hashCode(); this.x.setTo(position.x, 200); this.y.setTo(position.y, 200); } } } }