| 1 | package net.minecraft.client.renderer.state.gui;
|
| 2 |
|
| 3 | import net.fabricmc.api.EnvType;
|
| 4 | import net.fabricmc.api.Environment;
|
| 5 | import net.minecraft.client.gui.navigation.ScreenRectangle;
|
| 6 | import net.minecraft.client.renderer.item.TrackingItemStackRenderState;
|
| 7 | import net.minecraft.util.Mth;
|
| 8 | import net.minecraft.world.phys.AABB;
|
| 9 | import org.joml.Matrix3x2f;
|
| 10 | import org.jspecify.annotations.Nullable;
|
| 11 |
|
| 12 | @Environment(EnvType.CLIENT)
|
| 13 | public final class GuiItemRenderState implements ScreenArea {
|
| 14 | private final Matrix3x2f pose;
|
| 15 | private final TrackingItemStackRenderState itemStackRenderState;
|
| 16 | private final int x;
|
| 17 | private final int y;
|
| 18 | private final @Nullable ScreenRectangle scissorArea;
|
| 19 | private final @Nullable ScreenRectangle oversizedItemBounds;
|
| 20 | private final @Nullable ScreenRectangle bounds;
|
| 21 |
|
| 22 | public GuiItemRenderState(
|
| 23 | final Matrix3x2f pose, final TrackingItemStackRenderState itemStackRenderState, final int x, final int y, final @Nullable ScreenRectangle scissorArea
|
| 24 | ) {
|
| 25 | this.pose = pose;
|
| 26 | this.itemStackRenderState = itemStackRenderState;
|
| 27 | this.x = x;
|
| 28 | this.y = y;
|
| 29 | this.scissorArea = scissorArea;
|
| 30 | this.oversizedItemBounds = this.itemStackRenderState().isOversizedInGui() ? this.calculateOversizedItemBounds() : null;
|
| 31 | this.bounds = this.calculateBounds(this.oversizedItemBounds != null ? this.oversizedItemBounds : new ScreenRectangle(this.x, this.y, 16, 16));
|
| 32 | }
|
| 33 |
|
| 34 | private @Nullable ScreenRectangle calculateOversizedItemBounds() {
|
| 35 | AABB aabb = this.itemStackRenderState.getModelBoundingBox();
|
| 36 | int actualXSize = Mth.ceil(aabb.getXsize() * 16.0);
|
| 37 | int actualYSize = Mth.ceil(aabb.getYsize() * 16.0);
|
| 38 | if (actualXSize <= 16 && actualYSize <= 16) {
|
| 39 | return null;
|
| 40 | }
|
| 41 |
|
| 42 | float xOffset = (float)(aabb.minX * 16.0);
|
| 43 | float yOffset = (float)(aabb.maxY * 16.0);
|
| 44 | int flooredXOffset = Mth.floor(xOffset);
|
| 45 | int flooredYOffset = Mth.floor(yOffset);
|
| 46 | int actualX = this.x + flooredXOffset + 8;
|
| 47 | int actualY = this.y - flooredYOffset + 8;
|
| 48 | return new ScreenRectangle(actualX, actualY, actualXSize, actualYSize);
|
| 49 | }
|
| 50 |
|
| 51 | private @Nullable ScreenRectangle calculateBounds(final ScreenRectangle itemBounds) {
|
| 52 | ScreenRectangle bounds = itemBounds.transformMaxBounds(this.pose);
|
| 53 | return this.scissorArea != null ? this.scissorArea.intersection(bounds) : bounds;
|
| 54 | }
|
| 55 |
|
| 56 | public Matrix3x2f pose() {
|
| 57 | return this.pose;
|
| 58 | }
|
| 59 |
|
| 60 | public TrackingItemStackRenderState itemStackRenderState() {
|
| 61 | return this.itemStackRenderState;
|
| 62 | }
|
| 63 |
|
| 64 | public int x() {
|
| 65 | return this.x;
|
| 66 | }
|
| 67 |
|
| 68 | public int y() {
|
| 69 | return this.y;
|
| 70 | }
|
| 71 |
|
| 72 | public @Nullable ScreenRectangle scissorArea() {
|
| 73 | return this.scissorArea;
|
| 74 | }
|
| 75 |
|
| 76 | public @Nullable ScreenRectangle oversizedItemBounds() {
|
| 77 | return this.oversizedItemBounds;
|
| 78 | }
|
| 79 |
|
| 80 | @Override
|
| 81 | public @Nullable ScreenRectangle bounds() {
|
| 82 | return this.bounds;
|
| 83 | }
|
| 84 | }
|
| 85 |
|