package org.saturnclient.ui.components; import org.saturnclient.common.provider.Providers; import org.saturnclient.config.Theme; import org.saturnclient.mod.Mod; import org.saturnclient.ui.Element; import org.saturnclient.ui.ElementContext; import org.saturnclient.ui.RenderScope; import org.saturnclient.ui.components.inputs.Toggle; import org.saturnclient.ui.resources.Fonts; import org.saturnclient.ui.resources.Textures; import org.saturnclient.ui.screens.ModSettings; /** * A mod's card in the mod menu: icon, name, description, a settings button and * an on/off switch. Clicking anywhere but the settings button toggles the mod. */ public class SaturnModule extends Element { public static final int HEIGHT = 76; private static final int PADDING = 12; private static final int ICON_TILE = 44; private static final int ICON = 22; private static final int SETTINGS = 32; private static final int SETTINGS_ICON = 17; private static final int GAP = 12; private static final float NAME_SCALE = 0.74f; private static final float DESCRIPTION_SCALE = 0.64f; private final Mod mod; public SaturnModule(Mod mod, int width) { this.mod = mod; this.width = width; this.height = HEIGHT; } @Override public void render(RenderScope renderScope, ElementContext ctx) { boolean enabled = mod.isEnabled(); boolean settingsHovered = ctx.isHovering(getSettingsX(), getSettingsY(), SETTINGS, SETTINGS); boolean hovered = ctx.isHovering() && !settingsHovered; int border = enabled ? Theme.withAlpha(hovered ? 0.7f : 0.4f, Theme.ACCENT.value) : Theme.withAlpha(hovered ? 0.22f : 0.06f, Theme.FOREGROUND.value); // Card renderScope.drawRoundedRectangle(0, 0, width, height, Theme.WIDGET_RADIUS.value, 1, Theme.PRIMARY.value, border); if (enabled) { renderScope.drawRoundedRectangle(0, 0, width, height, Theme.WIDGET_RADIUS.value, Theme.withAlpha(0.06f, Theme.ACCENT.value)); } // Icon int tileY = (height - ICON_TILE) / 2; renderScope.drawRoundedRectangle(PADDING, tileY, ICON_TILE, ICON_TILE, Theme.WIDGET_RADIUS.value, enabled ? Theme.withAlpha(0.18f, Theme.ACCENT.value) : Theme.withAlpha(0.06f, Theme.FOREGROUND.value)); renderScope.drawTexture(mod.getIconTexture(), PADDING + (ICON_TILE - ICON) / 2, tileY + (ICON_TILE - ICON) / 2, 0, 0, ICON, ICON, Theme.PRIMARY_FG.value); // Name and description int textX = PADDING + ICON_TILE + GAP; int textWidth = getSettingsX() - GAP - textX; int nameHeight = (int) (Fonts.getHeight() * NAME_SCALE); int descriptionHeight = (int) (Fonts.getHeight() * DESCRIPTION_SCALE); int nameY = (height - nameHeight - 5 - descriptionHeight) / 2; renderScope.drawText(NAME_SCALE, Fonts.ellipsize(mod.getName(), Theme.FONT.value, NAME_SCALE, textWidth), textX, nameY, Theme.FONT.value, Theme.FOREGROUND.value); String description = mod.getDescription() == null ? "" : mod.getDescription(); renderScope.drawText(DESCRIPTION_SCALE, Fonts.ellipsize(description, Theme.getBodyFont(), DESCRIPTION_SCALE, textWidth), textX, nameY + nameHeight + 5, Theme.getBodyFont(), Theme.getMutedFg()); // Settings button if (settingsHovered) { renderScope.drawRoundedRectangle(getSettingsX(), getSettingsY(), SETTINGS, SETTINGS, Theme.WIDGET_RADIUS.value, Theme.withAlpha(0.08f, Theme.FOREGROUND.value)); } int settingsIconOffset = (SETTINGS - SETTINGS_ICON) / 2; renderScope.drawTexture(Textures.ICON_SLIDERS, getSettingsX() + settingsIconOffset, getSettingsY() + settingsIconOffset, 0, 0, SETTINGS_ICON, SETTINGS_ICON, settingsHovered ? Theme.ACCENT.value : Theme.getMutedFg()); // Switch Toggle.drawSwitch(renderScope, getSwitchX(), (height - Toggle.SWITCH_HEIGHT) / 2, enabled); } @Override public void click(int mouseX, int mouseY) { boolean settingsClicked = mouseX >= getSettingsX() && mouseX <= getSettingsX() + SETTINGS && mouseY >= getSettingsY() && mouseY <= getSettingsY() + SETTINGS; if (settingsClicked) { Providers.saturn.getClient().setScreen(new ModSettings(mod)); } else { mod.setEnabled(!mod.isEnabled()); } } private int getSwitchX() { return width - PADDING - Toggle.SWITCH_WIDTH; } private int getSettingsX() { return getSwitchX() - 8 - SETTINGS; } private int getSettingsY() { return (height - SETTINGS) / 2; } }