| 1 | package net.minecraft.client.gui.components;
|
| 2 |
|
| 3 | import java.util.function.Supplier;
|
| 4 | import net.fabricmc.api.EnvType;
|
| 5 | import net.fabricmc.api.Environment;
|
| 6 | import net.minecraft.client.gui.ComponentPath;
|
| 7 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 8 | import net.minecraft.client.gui.narration.NarrationElementOutput;
|
| 9 | import net.minecraft.client.gui.navigation.FocusNavigationEvent;
|
| 10 | import net.minecraft.client.input.MouseButtonEvent;
|
| 11 | import net.minecraft.client.model.Model;
|
| 12 | import net.minecraft.client.model.geom.EntityModelSet;
|
| 13 | import net.minecraft.client.model.geom.ModelLayers;
|
| 14 | import net.minecraft.client.renderer.rendertype.RenderTypes;
|
| 15 | import net.minecraft.client.sounds.SoundManager;
|
| 16 | import net.minecraft.network.chat.CommonComponents;
|
| 17 | import net.minecraft.util.Mth;
|
| 18 | import net.minecraft.world.entity.player.PlayerModelType;
|
| 19 | import net.minecraft.world.entity.player.PlayerSkin;
|
| 20 | import org.jspecify.annotations.Nullable;
|
| 21 |
|
| 22 | @Environment(EnvType.CLIENT)
|
| 23 | public class PlayerSkinWidget extends AbstractWidget {
|
| 24 | private static final float MODEL_HEIGHT = 2.125F;
|
| 25 | private static final float FIT_SCALE = 0.97F;
|
| 26 | private static final float ROTATION_SENSITIVITY = 2.5F;
|
| 27 | private static final float DEFAULT_ROTATION_X = -5.0F;
|
| 28 | private static final float DEFAULT_ROTATION_Y = 30.0F;
|
| 29 | private static final float ROTATION_X_LIMIT = 50.0F;
|
| 30 | private final Model.Simple wideModel;
|
| 31 | private final Model.Simple slimModel;
|
| 32 | private final Supplier<PlayerSkin> skin;
|
| 33 | private float rotationX = -5.0F;
|
| 34 | private float rotationY = 30.0F;
|
| 35 |
|
| 36 | public PlayerSkinWidget(final int width, final int height, final EntityModelSet models, final Supplier<PlayerSkin> skin) {
|
| 37 | super(0, 0, width, height, CommonComponents.EMPTY);
|
| 38 | this.wideModel = new Model.Simple(models.bakeLayer(ModelLayers.PLAYER), RenderTypes::entityTranslucent);
|
| 39 | this.slimModel = new Model.Simple(models.bakeLayer(ModelLayers.PLAYER_SLIM), RenderTypes::entityTranslucent);
|
| 40 | this.skin = skin;
|
| 41 | }
|
| 42 |
|
| 43 | @Override
|
| 44 | protected void extractWidgetRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 45 | float scale = 0.97F * this.getHeight() / 2.125F;
|
| 46 | float pivotY = -1.0625F;
|
| 47 | PlayerSkin skin = this.skin.get();
|
| 48 | Model.Simple model = skin.model() == PlayerModelType.SLIM ? this.slimModel : this.wideModel;
|
| 49 | graphics.skin(model, skin.body().texturePath(), scale, this.rotationX, this.rotationY, -1.0625F, this.getX(), this.getY(), this.getRight(), this.getBottom());
|
| 50 | }
|
| 51 |
|
| 52 | @Override
|
| 53 | protected void onDrag(final MouseButtonEvent event, final double dx, final double dy) {
|
| 54 | this.rotationX = Mth.clamp(this.rotationX - (float)dy * 2.5F, -50.0F, 50.0F);
|
| 55 | this.rotationY += (float)dx * 2.5F;
|
| 56 | }
|
| 57 |
|
| 58 | @Override
|
| 59 | public void playDownSound(final SoundManager soundManager) {
|
| 60 | }
|
| 61 |
|
| 62 | @Override
|
| 63 | protected void updateWidgetNarration(final NarrationElementOutput output) {
|
| 64 | }
|
| 65 |
|
| 66 | @Override
|
| 67 | public @Nullable ComponentPath nextFocusPath(final FocusNavigationEvent navigationEvent) {
|
| 68 | return null;
|
| 69 | }
|
| 70 | }
|
| 71 |
|