| 1 | package net.minecraft.client.gui.components;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.platform.cursor.CursorTypes;
|
| 4 | import java.time.Duration;
|
| 5 | import java.util.function.Consumer;
|
| 6 | import net.fabricmc.api.EnvType;
|
| 7 | import net.fabricmc.api.Environment;
|
| 8 | import net.minecraft.client.Minecraft;
|
| 9 | import net.minecraft.client.gui.ActiveTextCollector;
|
| 10 | import net.minecraft.client.gui.ComponentPath;
|
| 11 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 12 | import net.minecraft.client.gui.components.events.GuiEventListener;
|
| 13 | import net.minecraft.client.gui.layouts.LayoutElement;
|
| 14 | import net.minecraft.client.gui.narration.NarratableEntry;
|
| 15 | import net.minecraft.client.gui.narration.NarratedElementType;
|
| 16 | import net.minecraft.client.gui.narration.NarrationElementOutput;
|
| 17 | import net.minecraft.client.gui.navigation.FocusNavigationEvent;
|
| 18 | import net.minecraft.client.gui.navigation.ScreenRectangle;
|
| 19 | import net.minecraft.client.input.MouseButtonEvent;
|
| 20 | import net.minecraft.client.input.MouseButtonInfo;
|
| 21 | import net.minecraft.client.resources.sounds.SimpleSoundInstance;
|
| 22 | import net.minecraft.client.sounds.SoundManager;
|
| 23 | import net.minecraft.network.chat.Component;
|
| 24 | import net.minecraft.network.chat.ComponentUtils;
|
| 25 | import net.minecraft.network.chat.MutableComponent;
|
| 26 | import net.minecraft.network.chat.Style;
|
| 27 | import net.minecraft.sounds.SoundEvents;
|
| 28 | import org.jspecify.annotations.Nullable;
|
| 29 |
|
| 30 | @Environment(EnvType.CLIENT)
|
| 31 | public abstract class AbstractWidget implements LayoutElement, Renderable, GuiEventListener, NarratableEntry {
|
| 32 | protected int width;
|
| 33 | protected int height;
|
| 34 | private int x;
|
| 35 | private int y;
|
| 36 | protected Component message;
|
| 37 | protected boolean isHovered;
|
| 38 | public boolean active = true;
|
| 39 | public boolean visible = true;
|
| 40 | protected float alpha = 1.0F;
|
| 41 | private int tabOrderGroup;
|
| 42 | private boolean focused;
|
| 43 | private final WidgetTooltipHolder tooltip = new WidgetTooltipHolder();
|
| 44 |
|
| 45 | public AbstractWidget(final int x, final int y, final int width, final int height, final Component message) {
|
| 46 | this.x = x;
|
| 47 | this.y = y;
|
| 48 | this.width = width;
|
| 49 | this.height = height;
|
| 50 | this.message = message;
|
| 51 | }
|
| 52 |
|
| 53 | @Override
|
| 54 | public int getHeight() {
|
| 55 | return this.height;
|
| 56 | }
|
| 57 |
|
| 58 | @Override
|
| 59 | public final void extractRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 60 | if (this.visible) {
|
| 61 | this.isHovered = graphics.containsPointInScissor(mouseX, mouseY) && this.areCoordinatesInRectangle(mouseX, mouseY);
|
| 62 | this.extractWidgetRenderState(graphics, mouseX, mouseY, a);
|
| 63 | this.extractTooltipForNextRenderPass(graphics, mouseX, mouseY);
|
| 64 | }
|
| 65 | }
|
| 66 |
|
| 67 | protected void extractTooltipForNextRenderPass(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY) {
|
| 68 | this.tooltip.refreshTooltipForNextRenderPass(graphics, mouseX, mouseY, this.isHovered(), this.isFocused(), this.getRectangle());
|
| 69 | }
|
| 70 |
|
| 71 | protected void handleCursor(final GuiGraphicsExtractor graphics) {
|
| 72 | if (this.isHovered()) {
|
| 73 | graphics.requestCursor(this.isActive() ? CursorTypes.POINTING_HAND : CursorTypes.NOT_ALLOWED);
|
| 74 | }
|
| 75 | }
|
| 76 |
|
| 77 | public void setTooltip(final @Nullable Tooltip tooltip) {
|
| 78 | this.tooltip.set(tooltip);
|
| 79 | }
|
| 80 |
|
| 81 | public void setTooltipDelay(final Duration delay) {
|
| 82 | this.tooltip.setDelay(delay);
|
| 83 | }
|
| 84 |
|
| 85 | protected MutableComponent createNarrationMessage() {
|
| 86 | return wrapDefaultNarrationMessage(this.getMessage());
|
| 87 | }
|
| 88 |
|
| 89 | public static MutableComponent wrapDefaultNarrationMessage(final Component message) {
|
| 90 | return Component.translatable("gui.narrate.button", message);
|
| 91 | }
|
| 92 |
|
| 93 | protected abstract void extractWidgetRenderState(GuiGraphicsExtractor graphics, int mouseX, int mouseY, float a);
|
| 94 |
|
| 95 | protected void extractScrollingStringOverContents(final ActiveTextCollector output, final Component message, final int margin) {
|
| 96 | int left = this.getX() + margin;
|
| 97 | int right = this.getX() + this.getWidth() - margin;
|
| 98 | int top = this.getY();
|
| 99 | int bottom = this.getY() + this.getHeight();
|
| 100 | output.acceptScrollingWithDefaultCenter(message, left, right, top, bottom);
|
| 101 | }
|
| 102 |
|
| 103 | public void onClick(final MouseButtonEvent event, final boolean doubleClick) {
|
| 104 | }
|
| 105 |
|
| 106 | public void onRelease(final MouseButtonEvent event) {
|
| 107 | }
|
| 108 |
|
| 109 | protected void onDrag(final MouseButtonEvent event, final double dx, final double dy) {
|
| 110 | }
|
| 111 |
|
| 112 | @Override
|
| 113 | public boolean mouseClicked(final MouseButtonEvent event, final boolean doubleClick) {
|
| 114 | if (!this.isActive()) {
|
| 115 | return false;
|
| 116 | }
|
| 117 |
|
| 118 | if (this.isValidClickButton(event.buttonInfo())) {
|
| 119 | boolean isMouseOver = this.isMouseOver(event.x(), event.y());
|
| 120 | if (isMouseOver) {
|
| 121 | this.playDownSound(Minecraft.getInstance().getSoundManager());
|
| 122 | this.onClick(event, doubleClick);
|
| 123 | return true;
|
| 124 | }
|
| 125 | }
|
| 126 |
|
| 127 | return false;
|
| 128 | }
|
| 129 |
|
| 130 | @Override
|
| 131 | public boolean mouseReleased(final MouseButtonEvent event) {
|
| 132 | if (this.isValidClickButton(event.buttonInfo())) {
|
| 133 | this.onRelease(event);
|
| 134 | return true;
|
| 135 | } else {
|
| 136 | return false;
|
| 137 | }
|
| 138 | }
|
| 139 |
|
| 140 | protected boolean isValidClickButton(final MouseButtonInfo buttonInfo) {
|
| 141 | return buttonInfo.button() == 1;
|
| 142 | }
|
| 143 |
|
| 144 | @Override
|
| 145 | public boolean mouseDragged(final MouseButtonEvent event, final double dx, final double dy) {
|
| 146 | if (this.isValidClickButton(event.buttonInfo())) {
|
| 147 | this.onDrag(event, dx, dy);
|
| 148 | return true;
|
| 149 | } else {
|
| 150 | return false;
|
| 151 | }
|
| 152 | }
|
| 153 |
|
| 154 | @Override
|
| 155 | public @Nullable ComponentPath nextFocusPath(final FocusNavigationEvent navigationEvent) {
|
| 156 | if (!this.isActive()) {
|
| 157 | return null;
|
| 158 | } else {
|
| 159 | return !this.isFocused() ? ComponentPath.leaf(this) : null;
|
| 160 | }
|
| 161 | }
|
| 162 |
|
| 163 | @Override
|
| 164 | public boolean isMouseOver(final double mouseX, final double mouseY) {
|
| 165 | return this.isActive() && this.areCoordinatesInRectangle(mouseX, mouseY);
|
| 166 | }
|
| 167 |
|
| 168 | public void playDownSound(final SoundManager soundManager) {
|
| 169 | playButtonClickSound(soundManager);
|
| 170 | }
|
| 171 |
|
| 172 | public static void playButtonClickSound(final SoundManager soundManager) {
|
| 173 | soundManager.play(SimpleSoundInstance.forUI(SoundEvents.UI_BUTTON_CLICK, 1.0F));
|
| 174 | }
|
| 175 |
|
| 176 | @Override
|
| 177 | public int getWidth() {
|
| 178 | return this.width;
|
| 179 | }
|
| 180 |
|
| 181 | public void setWidth(final int width) {
|
| 182 | this.width = width;
|
| 183 | }
|
| 184 |
|
| 185 | public void setHeight(final int height) {
|
| 186 | this.height = height;
|
| 187 | }
|
| 188 |
|
| 189 | public void setAlpha(final float alpha) {
|
| 190 | this.alpha = alpha;
|
| 191 | }
|
| 192 |
|
| 193 | public float getAlpha() {
|
| 194 | return this.alpha;
|
| 195 | }
|
| 196 |
|
| 197 | public void setMessage(final Component message) {
|
| 198 | this.message = message;
|
| 199 | }
|
| 200 |
|
| 201 | public Component getMessage() {
|
| 202 | return this.message;
|
| 203 | }
|
| 204 |
|
| 205 | @Override
|
| 206 | public boolean isFocused() {
|
| 207 | return this.focused;
|
| 208 | }
|
| 209 |
|
| 210 | public boolean isHovered() {
|
| 211 | return this.isHovered;
|
| 212 | }
|
| 213 |
|
| 214 | public boolean isHoveredOrFocused() {
|
| 215 | return this.isHovered() || this.isFocused();
|
| 216 | }
|
| 217 |
|
| 218 | @Override
|
| 219 | public boolean isActive() {
|
| 220 | return this.visible && this.active;
|
| 221 | }
|
| 222 |
|
| 223 | @Override
|
| 224 | public void setFocused(final boolean focused) {
|
| 225 | this.focused = focused;
|
| 226 | }
|
| 227 |
|
| 228 | @Override
|
| 229 | public NarratableEntry.NarrationPriority narrationPriority() {
|
| 230 | if (this.isFocused()) {
|
| 231 | return NarratableEntry.NarrationPriority.FOCUSED;
|
| 232 | } else {
|
| 233 | return this.isHovered ? NarratableEntry.NarrationPriority.HOVERED : NarratableEntry.NarrationPriority.NONE;
|
| 234 | }
|
| 235 | }
|
| 236 |
|
| 237 | @Override
|
| 238 | public final void updateNarration(final NarrationElementOutput output) {
|
| 239 | this.updateWidgetNarration(output);
|
| 240 | this.tooltip.updateNarration(output);
|
| 241 | }
|
| 242 |
|
| 243 | protected abstract void updateWidgetNarration(final NarrationElementOutput output);
|
| 244 |
|
| 245 | protected void defaultButtonNarrationText(final NarrationElementOutput output) {
|
| 246 | output.add(NarratedElementType.TITLE, this.createNarrationMessage());
|
| 247 | if (this.active) {
|
| 248 | if (this.isFocused()) {
|
| 249 | output.add(NarratedElementType.USAGE, Component.translatable("narration.button.usage.focused"));
|
| 250 | } else {
|
| 251 | output.add(NarratedElementType.USAGE, Component.translatable("narration.button.usage.hovered"));
|
| 252 | }
|
| 253 | }
|
| 254 | }
|
| 255 |
|
| 256 | @Override
|
| 257 | public int getX() {
|
| 258 | return this.x;
|
| 259 | }
|
| 260 |
|
| 261 | @Override
|
| 262 | public void setX(final int x) {
|
| 263 | this.x = x;
|
| 264 | }
|
| 265 |
|
| 266 | @Override
|
| 267 | public int getY() {
|
| 268 | return this.y;
|
| 269 | }
|
| 270 |
|
| 271 | @Override
|
| 272 | public void setY(final int y) {
|
| 273 | this.y = y;
|
| 274 | }
|
| 275 |
|
| 276 | public int getRight() {
|
| 277 | return this.getX() + this.getWidth();
|
| 278 | }
|
| 279 |
|
| 280 | public int getBottom() {
|
| 281 | return this.getY() + this.getHeight();
|
| 282 | }
|
| 283 |
|
| 284 | @Override
|
| 285 | public void visitWidgets(final Consumer<AbstractWidget> widgetVisitor) {
|
| 286 | widgetVisitor.accept(this);
|
| 287 | }
|
| 288 |
|
| 289 | public void setSize(final int width, final int height) {
|
| 290 | this.width = width;
|
| 291 | this.height = height;
|
| 292 | }
|
| 293 |
|
| 294 | @Override
|
| 295 | public ScreenRectangle getRectangle() {
|
| 296 | return LayoutElement.super.getRectangle();
|
| 297 | }
|
| 298 |
|
| 299 | private boolean areCoordinatesInRectangle(final double x, final double y) {
|
| 300 | return x >= this.getX() && y >= this.getY() && x < this.getRight() && y < this.getBottom();
|
| 301 | }
|
| 302 |
|
| 303 | public void setRectangle(final int width, final int height, final int x, final int y) {
|
| 304 | this.setSize(width, height);
|
| 305 | this.setPosition(x, y);
|
| 306 | }
|
| 307 |
|
| 308 | @Override
|
| 309 | public int getTabOrderGroup() {
|
| 310 | return this.tabOrderGroup;
|
| 311 | }
|
| 312 |
|
| 313 | public void setTabOrderGroup(final int tabOrderGroup) {
|
| 314 | this.tabOrderGroup = tabOrderGroup;
|
| 315 | }
|
| 316 |
|
| 317 | @Environment(EnvType.CLIENT)
|
| 318 | public abstract static class WithInactiveMessage extends AbstractWidget {
|
| 319 | private Component inactiveMessage;
|
| 320 |
|
| 321 | public static Component defaultInactiveMessage(final Component activeMessage) {
|
| 322 | return ComponentUtils.mergeStyles(activeMessage, Style.EMPTY.withColor(-6250336));
|
| 323 | }
|
| 324 |
|
| 325 | public WithInactiveMessage(final int x, final int y, final int width, final int height, final Component message) {
|
| 326 | super(x, y, width, height, message);
|
| 327 | this.inactiveMessage = defaultInactiveMessage(message);
|
| 328 | }
|
| 329 |
|
| 330 | @Override
|
| 331 | public Component getMessage() {
|
| 332 | return this.active ? super.getMessage() : this.inactiveMessage;
|
| 333 | }
|
| 334 |
|
| 335 | @Override
|
| 336 | public void setMessage(final Component message) {
|
| 337 | super.setMessage(message);
|
| 338 | this.inactiveMessage = defaultInactiveMessage(message);
|
| 339 | }
|
| 340 | }
|
| 341 | }
|
| 342 |
|