package org.saturnclient.ui.components; import org.saturnclient.config.Theme; import org.saturnclient.ui.Element; import org.saturnclient.ui.ElementContext; import org.saturnclient.ui.RenderScope; import org.saturnclient.ui.resources.Fonts; /** * The player, wearing their cosmetics, filling the element. Dragging it turns * the player around. */ public class SkinPreview extends Element { public static EntityDrawer DRAWER; /** Degrees turned per GUI pixel dragged */ private static final float DRAG_SPEED = 1.2f; private static final float HINT_SCALE = 0.66f; private float yaw; private boolean dragging = false; /** * @param yaw degrees the player starts turned by: 0 faces the viewer, 180 * shows their back */ public SkinPreview(float yaw) { this.yaw = yaw; } @Override public void render(RenderScope renderScope, ElementContext ctx) { if (DRAWER != null && DRAWER.render(renderScope, width, height, yaw)) { return; } String hint = "Join a world to preview"; int x = (width - (int) (Fonts.getWidth(hint, Theme.getBodyFont()) * HINT_SCALE)) / 2; renderScope.drawText(HINT_SCALE, hint, x, height / 2, Theme.getBodyFont(), Theme.getMutedFg()); } @Override public void mouseClicked(double mouseX, double mouseY, int button) { dragging = mouseX >= 0 && mouseX <= width && mouseY >= 0 && mouseY <= height; } @Override public void mouseDragged(double mouseX, double mouseY, int button, double deltaX, double deltaY) { if (dragging) { yaw = (yaw - (float) deltaX * DRAG_SPEED) % 360; } } @Override public void mouseReleased(double mouseX, double mouseY, int button) { dragging = false; } public static interface EntityDrawer { /** * Draws the client's player filling a {@code width} x {@code height} box, * turned by {@code yaw} degrees (0 faces the viewer). * * @return false if there's no player to draw, such as on the title screen */ public boolean render(RenderScope renderScope, int width, int height, float yaw); } }