package org.saturnclient.ui.elements; import org.saturnclient.common.ref.asset.IdentifierRef; import org.saturnclient.config.Theme; import org.saturnclient.ui.Element; import org.saturnclient.ui.ElementContext; import org.saturnclient.ui.RenderScope; /** * A square {@link Button} showing an icon, which turns the accent color on * hover. */ public class IconButton extends Element { private static final float ICON_RATIO = 0.48f; private final IdentifierRef icon; private final Runnable onClick; public IconButton(IdentifierRef icon, Runnable onClick) { this.icon = icon; this.onClick = onClick; this.width = Button.HEIGHT; this.height = Button.HEIGHT; } @Override public void render(RenderScope renderScope, ElementContext ctx) { boolean hovered = ctx.isHovering(); Button.drawSurface(renderScope, width, height, hovered); int size = (int) (Math.min(width, height) * ICON_RATIO); renderScope.drawTexture(icon, (width - size) / 2, (height - size) / 2, 0, 0, size, size, hovered ? Theme.ACCENT.value : Theme.PRIMARY_FG.value); } @Override public void click(int mouseX, int mouseY) { onClick.run(); } }