implement pop animation

This commit is contained in:
くぁーりぃ committed 2022-09-12 21:57:22 +09:00
1 parent 8aea03c251
commit 0ce4118e36
4 files changed
+50 -25

No files matched your search

@@ -1,21 +1,26 @@
package codechicken.nei;
import static codechicken.nei.ItemsGrid.SLOT_SIZE;
import codechicken.nei.guihook.GuiContainerManager;
import net.minecraft.item.ItemStack;
import org.lwjgl.opengl.GL11;
public class AnimatedItemStack {
private static final int steep = 5;
private static final float scaleSpeed = 0.1f;
private int lastPosX;
private int lastPosY;
private int lastPosX = -1;
private int lastPosY = -1;
private float currentScale = 0;
private boolean doPopAnim;
public ItemStack itemStack;
public AnimatedItemStack(ItemStack itemStack, int startPosX, int startPosY) {
public AnimatedItemStack(ItemStack itemStack, boolean doPopAnim) {
this.itemStack = itemStack;
lastPosX = startPosX;
lastPosY = startPosY;
this.doPopAnim = doPopAnim;
}
public void drawItem(int x, int y) {
@@ -23,20 +28,40 @@ public class AnimatedItemStack {
}
public void drawItem(int x, int y, boolean smallAmount, String stackSize) {
if (x != lastPosX || y != lastPosY) {
if (doPopAnim && currentScale < 1) {
drawPoppingItem(x, y, smallAmount, stackSize);
} else if (lastPosX != x || lastPosY != y) {
drawMovingItem(x, y, smallAmount, stackSize);
} else {
GuiContainerManager.drawItem(x, y, itemStack, smallAmount, stackSize);
}
}
private void drawPoppingItem(int x, int y, boolean smallAmount, String stackSize) {
currentScale += scaleSpeed;
GL11.glScalef(currentScale, currentScale, currentScale); // push & pop matrix crashes the game
GuiContainerManager.drawItem(
(int) ((x + (SLOT_SIZE / 2 - currentScale * SLOT_SIZE / 2)) / currentScale),
(int) ((y + (SLOT_SIZE / 2 - currentScale * SLOT_SIZE / 2)) / currentScale),
itemStack,
smallAmount,
stackSize);
GL11.glScalef(1 / currentScale, 1 / currentScale, 1 / currentScale);
}
private void drawMovingItem(int x, int y, boolean smallAmount, String stackSize) {
int xDiff = x - lastPosX;
int yDiff = y - lastPosY;
int moveX = xDiff / steep + ((xDiff % steep == 0) ? 0 : (xDiff > 0 ? 1 : -1));
int moveY = yDiff / steep + ((yDiff % steep == 0) ? 0 : (yDiff > 0 ? 1 : -1));
GuiContainerManager.drawItem(lastPosX + moveX, lastPosY + moveY, itemStack, smallAmount, stackSize);
lastPosX = lastPosX + moveX;
lastPosY = lastPosY + moveY;
if (lastPosX != -1 && lastPosY != -1) {
int xDiff = x - lastPosX;
int yDiff = y - lastPosY;
int moveX = xDiff / steep + ((xDiff % steep == 0) ? 0 : (xDiff > 0 ? 1 : -1));
int moveY = yDiff / steep + ((yDiff % steep == 0) ? 0 : (yDiff > 0 ? 1 : -1));
GuiContainerManager.drawItem(lastPosX + moveX, lastPosY + moveY, itemStack, smallAmount, stackSize);
lastPosX = lastPosX + moveX;
lastPosY = lastPosY + moveY;
} else {
lastPosX = x;
lastPosY = y;
GuiContainerManager.drawItem(x, y, itemStack, smallAmount, stackSize);
}
}
}
@@ -223,7 +223,7 @@ public class BookmarkPanel extends PanelWidget {
}
public void addItem(ItemStack stackA, BookmarkStackMeta meta) {
addRealItem(stackA);
addRealItem(stackA, true);
metadata.add(meta);
onGridChanged();
}
@@ -233,13 +233,13 @@ public class BookmarkPanel extends PanelWidget {
addItem(stackA, meta);
return;
}
addRealItem(index, stackA);
addRealItem(index, stackA, true);
metadata.add(index, meta);
onGridChanged();
}
public void replaceItem(int idx, ItemStack stack) {
setRealItem(idx, stack);
setRealItem(idx, stack, true);
onGridChanged();
}
+1 -1
View File
@@ -58,7 +58,7 @@ public class ItemPanel extends PanelWidget {
if (newItems != null) {
realItems.clear();
addRealItems(newItems);
addRealItems(newItems, false);
newItems = null;
onGridChanged();
}
+8 -8
View File
@@ -58,20 +58,20 @@ public class ItemsGrid {
return realItems.get(idx).itemStack;
}
protected void addRealItems(Collection<ItemStack> itemStack) {
itemStack.forEach(this::addRealItem);
protected void addRealItems(Collection<ItemStack> itemStack, boolean doPopAnim) {
itemStack.forEach(e -> addRealItem(e, doPopAnim));
}
protected void addRealItem(ItemStack itemStack) {
realItems.add(new AnimatedItemStack(itemStack, marginLeft, marginTop));
protected void addRealItem(ItemStack itemStack, boolean doPopAnim) {
realItems.add(new AnimatedItemStack(itemStack, doPopAnim));
}
protected void addRealItem(int index, ItemStack itemStack) {
realItems.add(index, new AnimatedItemStack(itemStack, marginLeft, marginTop));
protected void addRealItem(int index, ItemStack itemStack, boolean doPopAnim) {
realItems.add(index, new AnimatedItemStack(itemStack, doPopAnim));
}
protected void setRealItem(int index, ItemStack itemStack) {
realItems.set(index, new AnimatedItemStack(itemStack, marginLeft, marginTop));
protected void setRealItem(int index, ItemStack itemStack, boolean doPopAnim) {
realItems.set(index, new AnimatedItemStack(itemStack, doPopAnim));
}
public int size() {