| 1 | package net.minecraft.client.gui.components;
|
| 2 |
|
| 3 | import net.fabricmc.api.EnvType;
|
| 4 | import net.fabricmc.api.Environment;
|
| 5 | import net.minecraft.client.gui.ActiveTextCollector;
|
| 6 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 7 | import net.minecraft.client.input.InputWithModifiers;
|
| 8 | import net.minecraft.client.renderer.RenderPipelines;
|
| 9 | import net.minecraft.network.chat.Component;
|
| 10 | import net.minecraft.resources.Identifier;
|
| 11 | import net.minecraft.util.ARGB;
|
| 12 | import org.jspecify.annotations.Nullable;
|
| 13 |
|
| 14 | @Environment(EnvType.CLIENT)
|
| 15 | public abstract class SpriteIconButton extends Button {
|
| 16 | private static final Identifier BUTTON_DISABLED_SPRITE = Identifier.withDefaultNamespace("widget/button_disabled");
|
| 17 | private static final Identifier LOADING_SPRITE = Identifier.withDefaultNamespace("friends/loading");
|
| 18 | private static final Tooltip LOADING_TOOLTIP = Tooltip.create(Component.translatable("gui.friends.button.loading"));
|
| 19 | private static final int LOADING_SPRITE_W = 5;
|
| 20 | private static final int LOADING_SPRITE_H = 2;
|
| 21 | protected final WidgetSprites sprite;
|
| 22 | protected final int spriteWidth;
|
| 23 | protected final int spriteHeight;
|
| 24 | protected final int spriteOffsetX;
|
| 25 | protected final int spriteOffsetY;
|
| 26 | private final boolean switchToLoadingAfterPress;
|
| 27 | private final @Nullable Tooltip defaultTooltip;
|
| 28 | private boolean loading;
|
| 29 |
|
| 30 | private SpriteIconButton(
|
| 31 | final int width,
|
| 32 | final int height,
|
| 33 | final Component message,
|
| 34 | final int spriteWidth,
|
| 35 | final int spriteHeight,
|
| 36 | final int spriteOffsetX,
|
| 37 | final int spriteOffsetY,
|
| 38 | final WidgetSprites sprite,
|
| 39 | final Button.OnPress onPress,
|
| 40 | final @Nullable Component tooltip,
|
| 41 | final Button.@Nullable CreateNarration narration,
|
| 42 | final boolean switchToLoadingAfterPress
|
| 43 | ) {
|
| 44 | super(0, 0, width, height, message, onPress, narration == null ? DEFAULT_NARRATION : narration);
|
| 45 | this.defaultTooltip = tooltip != null ? Tooltip.create(tooltip) : null;
|
| 46 | this.setTooltip(this.defaultTooltip);
|
| 47 | this.spriteWidth = spriteWidth;
|
| 48 | this.spriteHeight = spriteHeight;
|
| 49 | this.spriteOffsetX = spriteOffsetX;
|
| 50 | this.spriteOffsetY = spriteOffsetY;
|
| 51 | this.sprite = sprite;
|
| 52 | this.switchToLoadingAfterPress = switchToLoadingAfterPress;
|
| 53 | }
|
| 54 |
|
| 55 | public void setLoading(final boolean loading) {
|
| 56 | this.setLoading(loading, LOADING_TOOLTIP);
|
| 57 | }
|
| 58 |
|
| 59 | public void setLoading(final boolean loading, final Tooltip loadingTooltip) {
|
| 60 | this.loading = loading;
|
| 61 | if (loading) {
|
| 62 | this.setTooltip(loadingTooltip);
|
| 63 | } else {
|
| 64 | this.setTooltip(this.defaultTooltip);
|
| 65 | }
|
| 66 | }
|
| 67 |
|
| 68 | @Override
|
| 69 | public boolean isActive() {
|
| 70 | return super.isActive() && !this.loading;
|
| 71 | }
|
| 72 |
|
| 73 | @Override
|
| 74 | public void onPress(final InputWithModifiers input) {
|
| 75 | if (this.switchToLoadingAfterPress) {
|
| 76 | this.setLoading(true);
|
| 77 | }
|
| 78 |
|
| 79 | super.onPress(input);
|
| 80 | }
|
| 81 |
|
| 82 | protected void extractSprite(final GuiGraphicsExtractor graphics, final int x, final int y) {
|
| 83 | graphics.blitSprite(
|
| 84 | RenderPipelines.GUI_TEXTURED, this.sprite.get(this.isActive(), this.isHoveredOrFocused()), x, y, this.spriteWidth, this.spriteHeight, this.alpha
|
| 85 | );
|
| 86 | }
|
| 87 |
|
| 88 | protected boolean extractLoadingStateIfLoading(final GuiGraphicsExtractor graphics) {
|
| 89 | if (!this.loading) {
|
| 90 | return false;
|
| 91 | }
|
| 92 |
|
| 93 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, BUTTON_DISABLED_SPRITE, this.getX(), this.getY(), this.getWidth(), this.getHeight(), ARGB.white(this.alpha));
|
| 94 | graphics.blitSprite(
|
| 95 | RenderPipelines.GUI_TEXTURED,
|
| 96 | LOADING_SPRITE,
|
| 97 | this.getX() + (this.getWidth() - 5) / 2,
|
| 98 | this.getY() + (this.getHeight() - 2) / 2,
|
| 99 | 5,
|
| 100 | 2,
|
| 101 | ARGB.white(this.alpha)
|
| 102 | );
|
| 103 | return true;
|
| 104 | }
|
| 105 |
|
| 106 | public static SpriteIconButton.Builder builder(final Component message, final Button.OnPress onPress, final boolean iconOnly) {
|
| 107 | return new SpriteIconButton.Builder(message, onPress, iconOnly);
|
| 108 | }
|
| 109 |
|
| 110 | @Environment(EnvType.CLIENT)
|
| 111 | public static class Builder {
|
| 112 | private final Component message;
|
| 113 | private final Button.OnPress onPress;
|
| 114 | private final boolean iconOnly;
|
| 115 | private int width = 150;
|
| 116 | private int height = 20;
|
| 117 | private @Nullable WidgetSprites sprite;
|
| 118 | private int spriteWidth;
|
| 119 | private int spriteHeight;
|
| 120 | private int spriteOffsetX;
|
| 121 | private int spriteOffsetY;
|
| 122 | private @Nullable Component tooltip;
|
| 123 | private Button.@Nullable CreateNarration narration;
|
| 124 | private boolean switchToLoadingAfterPress;
|
| 125 |
|
| 126 | private Builder(final Component message, final Button.OnPress onPress, final boolean iconOnly) {
|
| 127 | this.message = message;
|
| 128 | this.onPress = onPress;
|
| 129 | this.iconOnly = iconOnly;
|
| 130 | }
|
| 131 |
|
| 132 | public SpriteIconButton.Builder width(final int width) {
|
| 133 | this.width = width;
|
| 134 | return this;
|
| 135 | }
|
| 136 |
|
| 137 | public SpriteIconButton.Builder size(final int width, final int height) {
|
| 138 | this.width = width;
|
| 139 | this.height = height;
|
| 140 | return this;
|
| 141 | }
|
| 142 |
|
| 143 | public SpriteIconButton.Builder sprite(final Identifier sprite, final int spriteWidth, final int spriteHeight) {
|
| 144 | this.sprite = new WidgetSprites(sprite);
|
| 145 | this.spriteWidth = spriteWidth;
|
| 146 | this.spriteHeight = spriteHeight;
|
| 147 | return this;
|
| 148 | }
|
| 149 |
|
| 150 | public SpriteIconButton.Builder sprite(final WidgetSprites sprite, final int spriteWidth, final int spriteHeight) {
|
| 151 | this.sprite = sprite;
|
| 152 | this.spriteWidth = spriteWidth;
|
| 153 | this.spriteHeight = spriteHeight;
|
| 154 | return this;
|
| 155 | }
|
| 156 |
|
| 157 | public SpriteIconButton.Builder spriteOffset(final int spriteOffsetX, final int spriteOffsetY) {
|
| 158 | this.spriteOffsetX = spriteOffsetX;
|
| 159 | this.spriteOffsetY = spriteOffsetY;
|
| 160 | return this;
|
| 161 | }
|
| 162 |
|
| 163 | public SpriteIconButton.Builder tooltip(final Component tooltip) {
|
| 164 | this.tooltip = tooltip;
|
| 165 | return this;
|
| 166 | }
|
| 167 |
|
| 168 | public SpriteIconButton.Builder withTootip() {
|
| 169 | this.tooltip = this.message;
|
| 170 | return this;
|
| 171 | }
|
| 172 |
|
| 173 | public SpriteIconButton.Builder narration(final Button.CreateNarration narration) {
|
| 174 | this.narration = narration;
|
| 175 | return this;
|
| 176 | }
|
| 177 |
|
| 178 | public SpriteIconButton.Builder switchToLoadingAfterPress() {
|
| 179 | this.switchToLoadingAfterPress = true;
|
| 180 | return this;
|
| 181 | }
|
| 182 |
|
| 183 | public SpriteIconButton build() {
|
| 184 | if (this.sprite == null) {
|
| 185 | throw new IllegalStateException("Sprite not set");
|
| 186 | } else {
|
| 187 | return this.iconOnly
|
| 188 | ? new SpriteIconButton.CenteredIcon(
|
| 189 | this.width,
|
| 190 | this.height,
|
| 191 | this.message,
|
| 192 | this.spriteWidth,
|
| 193 | this.spriteHeight,
|
| 194 | this.spriteOffsetX,
|
| 195 | this.spriteOffsetY,
|
| 196 | this.sprite,
|
| 197 | this.onPress,
|
| 198 | this.tooltip,
|
| 199 | this.narration,
|
| 200 | this.switchToLoadingAfterPress
|
| 201 | )
|
| 202 | : new SpriteIconButton.TextAndIcon(
|
| 203 | this.width,
|
| 204 | this.height,
|
| 205 | this.message,
|
| 206 | this.spriteWidth,
|
| 207 | this.spriteHeight,
|
| 208 | this.spriteOffsetX,
|
| 209 | this.spriteOffsetY,
|
| 210 | this.sprite,
|
| 211 | this.onPress,
|
| 212 | this.tooltip,
|
| 213 | this.narration,
|
| 214 | this.switchToLoadingAfterPress
|
| 215 | );
|
| 216 | }
|
| 217 | }
|
| 218 | }
|
| 219 |
|
| 220 | @Environment(EnvType.CLIENT)
|
| 221 | public static class CenteredIcon extends SpriteIconButton {
|
| 222 | protected CenteredIcon(
|
| 223 | final int width,
|
| 224 | final int height,
|
| 225 | final Component message,
|
| 226 | final int spriteWidth,
|
| 227 | final int spriteHeight,
|
| 228 | final int spriteOffsetX,
|
| 229 | final int spriteOffsetY,
|
| 230 | final WidgetSprites sprite,
|
| 231 | final Button.OnPress onPress,
|
| 232 | final @Nullable Component tooltip,
|
| 233 | final Button.@Nullable CreateNarration narration,
|
| 234 | final boolean switchToLoadingAfterPress
|
| 235 | ) {
|
| 236 | super(width, height, message, spriteWidth, spriteHeight, spriteOffsetX, spriteOffsetY, sprite, onPress, tooltip, narration, switchToLoadingAfterPress);
|
| 237 | }
|
| 238 |
|
| 239 | @Override
|
| 240 | public void extractContents(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 241 | if (!this.extractLoadingStateIfLoading(graphics)) {
|
| 242 | this.extractDefaultSprite(graphics);
|
| 243 | int x = this.spriteOffsetX + this.getX() + this.getWidth() / 2 - this.spriteWidth / 2;
|
| 244 | int y = this.spriteOffsetY + this.getY() + this.getHeight() / 2 - this.spriteHeight / 2;
|
| 245 | this.extractSprite(graphics, x, y);
|
| 246 | }
|
| 247 | }
|
| 248 | }
|
| 249 |
|
| 250 | @Environment(EnvType.CLIENT)
|
| 251 | public static class TextAndIcon extends SpriteIconButton {
|
| 252 | protected TextAndIcon(
|
| 253 | final int width,
|
| 254 | final int height,
|
| 255 | final Component message,
|
| 256 | final int spriteWidth,
|
| 257 | final int spriteHeight,
|
| 258 | final int spriteOffsetX,
|
| 259 | final int spriteOffsetY,
|
| 260 | final WidgetSprites sprite,
|
| 261 | final Button.OnPress onPress,
|
| 262 | final @Nullable Component tooltip,
|
| 263 | final Button.@Nullable CreateNarration narration,
|
| 264 | final boolean switchToLoadingAfterPress
|
| 265 | ) {
|
| 266 | super(width, height, message, spriteWidth, spriteHeight, spriteOffsetX, spriteOffsetY, sprite, onPress, tooltip, narration, switchToLoadingAfterPress);
|
| 267 | }
|
| 268 |
|
| 269 | @Override
|
| 270 | public void extractContents(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 271 | if (!this.extractLoadingStateIfLoading(graphics)) {
|
| 272 | this.extractDefaultSprite(graphics);
|
| 273 | int left = this.getX() + 2;
|
| 274 | int right = this.getX() + this.getWidth() - this.spriteWidth - 4;
|
| 275 | int centerX = this.getX() + this.getWidth() / 2;
|
| 276 | ActiveTextCollector output = graphics.textRendererForWidget(this, GuiGraphicsExtractor.HoveredTextEffects.NONE);
|
| 277 | output.acceptScrolling(this.getMessage(), centerX, left, right, this.getY(), this.getY() + this.getHeight());
|
| 278 | int x = this.spriteOffsetX + this.getX() + this.getWidth() - this.spriteWidth - 2;
|
| 279 | int y = this.spriteOffsetY + this.getY() + this.getHeight() / 2 - this.spriteHeight / 2;
|
| 280 | this.extractSprite(graphics, x, y);
|
| 281 | }
|
| 282 | }
|
| 283 | }
|
| 284 | }
|
| 285 |
|