|
3 | 3 | import com.cleanroommc.modularui.drawable.text.TextRenderer; |
4 | 4 | import com.cleanroommc.modularui.screen.RichTooltip; |
5 | 5 | import com.cleanroommc.modularui.screen.RichTooltipEvent; |
| 6 | +import com.cleanroommc.modularui.screen.viewport.GuiContext; |
6 | 7 | import com.cleanroommc.modularui.utils.Alignment; |
7 | 8 | import com.cleanroommc.modularui.utils.Color; |
8 | 9 | import com.cleanroommc.modularui.utils.NumberFormat; |
|
15 | 16 | import net.minecraft.client.renderer.BufferBuilder; |
16 | 17 | import net.minecraft.client.renderer.GlStateManager; |
17 | 18 | import net.minecraft.client.renderer.RenderItem; |
| 19 | +import net.minecraft.client.renderer.entity.RenderManager; |
18 | 20 | import net.minecraft.client.renderer.texture.TextureAtlasSprite; |
19 | 21 | import net.minecraft.client.renderer.texture.TextureMap; |
| 22 | +import net.minecraft.entity.Entity; |
| 23 | +import net.minecraft.entity.EntityLivingBase; |
20 | 24 | import net.minecraft.item.ItemStack; |
21 | 25 | import net.minecraft.util.ResourceLocation; |
22 | 26 | import net.minecraftforge.client.event.RenderTooltipEvent; |
|
29 | 33 | import org.jetbrains.annotations.Nullable; |
30 | 34 |
|
31 | 35 | import java.util.List; |
| 36 | +import java.util.function.Consumer; |
32 | 37 |
|
33 | 38 | public class GuiDraw { |
34 | 39 |
|
@@ -561,4 +566,89 @@ public static void drawTooltipBackground(ItemStack stack, List<String> lines, in |
561 | 566 | // bottom accent border |
562 | 567 | drawVerticalGradientRect(x - 3, y + height + 2, textWidth + 6, 1, borderColorEnd, borderColorEnd); |
563 | 568 | } |
| 569 | + |
| 570 | + /** |
| 571 | + * Draws an entity. Note that this does NOT do any necessary setup for rendering the entity. Please see |
| 572 | + * {@link #drawEntity(Entity, float, float, float, float, Consumer, Consumer)} for a full draw method. |
| 573 | + * |
| 574 | + * @param entity entity to draw. |
| 575 | + * @see #drawEntity(Entity, float, float, float, float, Consumer, Consumer) |
| 576 | + */ |
| 577 | + public static void drawEntityRaw(Entity entity) { |
| 578 | + RenderManager rendermanager = Minecraft.getMinecraft().getRenderManager(); |
| 579 | + rendermanager.setPlayerViewY(180.0F); |
| 580 | + rendermanager.setRenderShadow(false); |
| 581 | + rendermanager.renderEntity(entity, 0.0D, 0.0D, 0.0D, 0.0F, 1.0F, false); |
| 582 | + rendermanager.setRenderShadow(true); |
| 583 | + } |
| 584 | + |
| 585 | + /** |
| 586 | + * A simple method to a draw an entity in a GUI. Using the consumers is not always ideal to modify and restore entity state. In those |
| 587 | + * cases just copy and paste this method and put your code where the consumers would be called. The entity will be scaled so that it |
| 588 | + * fits right in the given size when untransformed (default). When transforming during pre draw, you may need to manually correct the |
| 589 | + * scale and offset. |
| 590 | + * |
| 591 | + * @param entity entity to draw |
| 592 | + * @param x x pos |
| 593 | + * @param y y pos |
| 594 | + * @param w the width of the area where the entity should be drawn |
| 595 | + * @param h the height of the area where the entity should be drawn |
| 596 | + * @param z the z layer ({@link GuiContext#getCurrentDrawingZ()} if drawn in a MUI) |
| 597 | + * @param preDraw a function to call before rendering. Transform or modify the entity here. |
| 598 | + * @param postDraw a function to call after rendering. Restore old entity state here if needed. |
| 599 | + * @param <T> type of the entity to render |
| 600 | + */ |
| 601 | + public static <T extends Entity> void drawEntity(T entity, float x, float y, float w, float h, float z, @Nullable Consumer<T> preDraw, @Nullable Consumer<T> postDraw) { |
| 602 | + GlStateManager.pushMatrix(); |
| 603 | + Platform.setupDrawEntity(entity, x, y, w, h, z); |
| 604 | + if (preDraw != null) preDraw.accept(entity); |
| 605 | + drawEntityRaw(entity); |
| 606 | + if (postDraw != null) postDraw.accept(entity); |
| 607 | + Platform.endDrawEntity(); |
| 608 | + GlStateManager.popMatrix(); |
| 609 | + } |
| 610 | + |
| 611 | + /** |
| 612 | + * Draws an entity which looks in the direction of the mouse like the player render in the player inventory does. |
| 613 | + * The code was copied from |
| 614 | + * {@link net.minecraft.client.gui.inventory.GuiInventory#drawEntityOnScreen(int, int, int, float, float, EntityLivingBase) GuiInventory.drawEntityOnScreen}. |
| 615 | + * |
| 616 | + * @param entity entity to draw |
| 617 | + * @param x x pos |
| 618 | + * @param y y pos |
| 619 | + * @param w the width of the area where the entity should be drawn |
| 620 | + * @param h the height of the area where the entity should be drawn |
| 621 | + * @param z the z layer ({@link GuiContext#getCurrentDrawingZ()} if drawn in a MUI) |
| 622 | + * @param mouseX current x pos of the mouse |
| 623 | + * @param mouseY current y pos of the mouse |
| 624 | + */ |
| 625 | + public static void drawEntityLookingAtMouse(EntityLivingBase entity, float x, float y, float w, float h, float z, int mouseX, int mouseY) { |
| 626 | + GlStateManager.pushMatrix(); |
| 627 | + Platform.setupDrawEntity(entity, x, y, w, h, z); |
| 628 | + |
| 629 | + // pre draw |
| 630 | + float f = entity.renderYawOffset; |
| 631 | + float f1 = entity.rotationYaw; |
| 632 | + float f2 = entity.rotationPitch; |
| 633 | + float f3 = entity.prevRotationYawHead; |
| 634 | + float f4 = entity.rotationYawHead; |
| 635 | + GlStateManager.rotate(-((float) Math.atan(mouseY / 40.0F)) * 20.0F, 1.0F, 0.0F, 0.0F); |
| 636 | + entity.renderYawOffset = (float) Math.atan(mouseX / 40.0F) * 20.0F; |
| 637 | + entity.rotationYaw = (float) Math.atan(mouseX / 40.0F) * 40.0F; |
| 638 | + entity.rotationPitch = -((float) Math.atan(mouseY / 40.0F)) * 20.0F; |
| 639 | + entity.rotationYawHead = entity.rotationYaw; |
| 640 | + entity.prevRotationYawHead = entity.rotationYaw; |
| 641 | + |
| 642 | + drawEntityRaw(entity); |
| 643 | + |
| 644 | + // post draw |
| 645 | + entity.renderYawOffset = f; |
| 646 | + entity.rotationYaw = f1; |
| 647 | + entity.rotationPitch = f2; |
| 648 | + entity.prevRotationYawHead = f3; |
| 649 | + entity.rotationYawHead = f4; |
| 650 | + |
| 651 | + Platform.endDrawEntity(); |
| 652 | + GlStateManager.popMatrix(); |
| 653 | + } |
564 | 654 | } |
0 commit comments