diff options
| author | isxander <xander@isxander.dev> | 2024-04-11 18:43:06 +0100 |
|---|---|---|
| committer | isxander <xander@isxander.dev> | 2024-04-11 18:43:06 +0100 |
| commit | 04fe933f4c24817100f3101f088accf55a621f8a (patch) | |
| tree | feff94ca3ab4484160e69a24f4ee38522381950e /src/main/java/dev/isxander/yacl3/gui | |
| parent | 831b894fdb7fe3e173d81387c8f6a2402b8ccfa9 (diff) | |
| download | YetAnotherConfigLib-04fe933f4c24817100f3101f088accf55a621f8a.tar.gz YetAnotherConfigLib-04fe933f4c24817100f3101f088accf55a621f8a.tar.bz2 YetAnotherConfigLib-04fe933f4c24817100f3101f088accf55a621f8a.zip | |
Extremely fragile and broken multiversion build with stonecutter
Diffstat (limited to 'src/main/java/dev/isxander/yacl3/gui')
64 files changed, 6801 insertions, 0 deletions
diff --git a/src/main/java/dev/isxander/yacl3/gui/AbstractWidget.java b/src/main/java/dev/isxander/yacl3/gui/AbstractWidget.java new file mode 100644 index 0000000..6f92749 --- /dev/null +++ b/src/main/java/dev/isxander/yacl3/gui/AbstractWidget.java @@ -0,0 +1,100 @@ +package dev.isxander.yacl3.gui; + +import dev.isxander.yacl3.api.utils.Dimension; +import dev.isxander.yacl3.gui.utils.ButtonTextureRenderer; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.Font; +import net.minecraft.client.gui.GuiGraphics; +import net.minecraft.client.gui.components.Renderable; +import net.minecraft.client.gui.components.events.GuiEventListener; +import net.minecraft.client.gui.narration.NarratableEntry; +import net.minecraft.client.gui.narration.NarrationElementOutput; +import net.minecraft.client.resources.sounds.SimpleSoundInstance; +import net.minecraft.sounds.SoundEvents; + +import java.awt.Color; + +public abstract class AbstractWidget implements GuiEventListener, Renderable, NarratableEntry { + protected final Minecraft client = Minecraft.getInstance(); + protected final Font textRenderer = client.font; + protected final int inactiveColor = 0xFFA0A0A0; + + private Dimension<Integer> dim; + + public AbstractWidget(Dimension<Integer> dim) { + this.dim = dim; + } + + public boolean canReset() { + return false; + } + + @Override + public boolean isMouseOver(double mouseX, double mouseY) { + if (dim == null) return false; + return this.dim.isPointInside((int) mouseX, (int) mouseY); + } + + public void setDimension(Dimension<Integer> dim) { + this.dim = dim; + } + + public Dimension<Integer> getDimension() { + return dim; + } + + @Override + public NarrationPriority narrationPriority() { + return NarrationPriority.NONE; + } + + public void unfocus() { + + } + + public boolean matchesSearch(String query) { + return true; + } + + @Override + public void updateNarration(NarrationElementOutput builder) { + + } + + protected void drawButtonRect(GuiGraphics graphics, int x1, int y1, int x2, int y2, boolean hovered, boolean enabled) { + if (x1 > x2) { + int xx1 = x1; + x1 = x2; + x2 = xx1; + } + if (y1 > y2) { + int yy1 = y1; + y1 = y2; + y2 = yy1; + } + int width = x2 - x1; + int height = y2 - y1; + + ButtonTextureRenderer.render(graphics, x1, y1, width, height, enabled, hovered); + } + + protected void drawOutline(GuiGraphics graphics, int x1, int y1, int x2, int y2, int width, int color) { + graphics.fill(x1, y1, x2, y1 + width, color); + graphics.fill(x2, y1, x2 - width, y2, color); + graphics.fill(x1, y2, x2, y2 - width, color); + graphics.fill(x1, y1, x1 + width, y2, color); + } + + protected int multiplyColor(int hex, float amount) { + Color color = new Color(hex, true); + + return new Color(Math.max((int)(color.getRed() * amount), 0), + Math.max((int)(color.getGreen() * amount), 0), + Math.max((int)(color.getBlue() * amount), 0), + color.getAlpha()).getRGB(); + } + + public void playDownSound() { + Minecraft.getInstance().getSoundManager().play(SimpleSoundInstance.forUI(SoundEvents.UI_BUTTON_CLICK, 1.0F)); + } +} diff --git a/src/main/java/dev/isxander/yacl3/gui/DescriptionWithName.java b/src/main/java/dev/isxander/yacl3/gui/DescriptionWithName.java new file mode 100644 index 0000000..6ad72e8 --- /dev/null +++ b/src/main/java/dev/isxander/yacl3/gui/DescriptionWithName.java @@ -0,0 +1,11 @@ +package dev.isxander.yacl3.gui; + +import dev.isxander.yacl3.api.OptionDescription; +import net.minecraft.ChatFormatting; +import net.minecraft.network.chat.Component; + +public record DescriptionWithName(Component name, OptionDescription description) { + public static DescriptionWithName of(Component name, OptionDescription description) { + return new DescriptionWithName(name.copy().withStyle(ChatFormatting.BOLD), description); + } +} diff --git a/src/main/java/dev/isxander/yacl3/gui/ElementListWidgetExt.java b/src/main/java/dev/isxander/yacl3/gui/ElementListWidgetExt.java new file mode 100644 index 0000000..742125b --- /dev/null +++ b/src/main/java/dev/isxander/yacl3/gui/ElementListWidgetExt.java @@ -0,0 +1,274 @@ +package dev.isxander.yacl3.gui; + +import com.mojang.blaze3d.platform.InputConstants; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.GuiGraphics; +import net.minecraft.client.gui.components.AbstractWidget; +import net.minecraft.client.gui.components.ContainerObjectSelectionList; +import net.minecraft.client.gui.components.events.GuiEventListener; +import net.minecraft.client.gui.layouts.LayoutElement; +import net.minecraft.client.gui.navigation.ScreenRectangle; +import net.minecraft.util.Mth; +import org.jetbrains.annotations.Nullable; + +import java.util.function.Consumer; + +public class ElementListWidgetExt<E extends ElementListWidgetExt.Entry<E>> extends ContainerObjectSelectionList<E> implements LayoutElement { + protected static final int SCROLLBAR_WIDTH = 6; + + private double smoothScrollAmount = getScrollAmount(); + private boolean returnSmoothAmount = false; + private final boolean doSmoothScrolling; + private boolean usingScrollbar; + + public ElementListWidgetExt(Minecraft client, int x, int y, int width, int height, boolean smoothScrolling) { + /*? if >1.20.2 {*/ + super(client, width, x, y, height); + /*? } else {*//* + super(client, width, height, y, y + height, 22); + this.x0 = x; + this.x1 = x + width; + *//*?}*/ + this.doSmoothScrolling = smoothScrolling; + setRenderHeader(false, 0); + } + + @Override + public boolean mouseScrolled(double mouseX, double mouseY, /*? if >1.20.2 {*/ double horizontal, /*?}*/ double vertical) { + double scroll = vertical; + /*? if >1.20.2 {*/ + scroll += horizontal; + /*?}*/ + + // default implementation bases scroll step from total height of entries, this is constant + this.setScrollAmount(this.getScrollAmount() - scroll * 20); + return true; + } + + @Override + protected int getScrollbarPosition() { + // default implementation does not respect left/right + return this.getX() + this.getWidth() - SCROLLBAR_WIDTH; + } + + @Override + /*? if >1.20.2 { */ + public void renderWidget(GuiGraphics graphics, int mouseX, int mouseY, float delta) + /*?} else { *//* + public void render(GuiGraphics graphics, int mouseX, int mouseY, float delta) + *//*?}*/ + { + if (usingScrollbar) { + resetSmoothScrolling(); + } + + smoothScrollAmount = Mth.lerp(Minecraft.getInstance().getDeltaFrameTime() * 0.5, smoothScrollAmount, getScrollAmount()); + returnSmoothAmount = true; + + + graphics.enableScissor(this.getX(), this.getY(), this.getX() + this.getWidth(), this.getY() + this.getHeight()); + + /*? if >1.20.2 { */ + super.renderWidget(graphics, mouseX, mouseY, delta); + /*?} else { *//* + super.render(graphics, mouseX, mouseY, delta); + *//*?}*/ + + graphics.disableScissor(); + + returnSmoothAmount = false; + } + + @Override + public boolean mouseClicked(double mouseX, double mouseY, int button) { + if (button == 0 && mouseX >= getScrollbarPosition() && mouseX < getScrollbarPosition() + SCROLLBAR_WIDTH) { + usingScrollbar = true; + } + + return super.mouseClicked(mouseX, mouseY, button); + } + + @Override + public boolean mouseReleased(double mouseX, double mouseY, int button) { + if (button == 0) { + usingScrollbar = false; + } + + return super.mouseReleased(mouseX, mouseY, button); + } + + public void updateDimensions(ScreenRectangle rectangle) { + this.setX(rectangle.left()); + this.setY(rectangle.top()); + this.setWidth(rectangle.width()); + this.setHeight(rectangle.height()); + } + + /** + * awful code to only use smooth scroll state when rendering, + * not other code that needs target scroll amount + */ + @Override + public double getScrollAmount() { + if (returnSmoothAmount && doSmoothScrolling) + return smoothScrollAmount; + + return super.getScrollAmount(); + } + + protected void resetSmoothScrolling() { + this.smoothScrollAmount = super.getScrollAmount(); + } + + @Nullable + @Override + protected E getEntryAtPosition(double x, double y) { + y += getScrollAmount(); + + if (x < this.getX() || x > this.getX() + this.getWidth()) + return null; + + int currentY = this.getY() - headerHeight + 4; + for (E entry : children()) { + if (y >= currentY && y <= currentY + entry.getItemHeight()) { + return entry; + } + + currentY += entry.getItemHeight(); + } + + return null; + } + + /* + below code is licensed from cloth-config under LGPL3 + modified to inherit vanilla's EntryListWidget and use yarn mappings + + code is responsible for having dynamic item heights + */ + + @Override + protected int getMaxPosition() { + return children().stream().map(E::getItemHeight).reduce(0, Integer::sum) + headerHeight; + } + + @Override + protected void centerScrollOn(E entry) { + double d = (this.height) / -2d; + for (int i = 0; i < this.children().indexOf(entry) && i < this.getItemCount(); i++) + d += children().get(i).getItemHeight(); + this.setScrollAmount(d); + } + + @Override + protected int getRowTop(int index) { + int integer = getY() + 4 - (int) this.getScrollAmount() + headerHeight; + for (int i = 0; i < children().size() && i < index; i++) + integer += children().get(i).getItemHeight(); + return integer; + } + + @Override + /*? if >1.20.4 {*//* + protected void renderListItems(GuiGraphics graphics, int mouseX, int mouseY, float delta) + *//*? } else {*/ + protected void renderList(GuiGraphics graphics, int mouseX, int mouseY, float delta) + /*?}*/ + { + int left = this.getRowLeft(); + int right = this.getRowWidth(); + int count = this.getItemCount(); + + for(int i = 0; i < count; ++i) { + E entry = children().get(i); + int top = this.getRowTop(i); + int bottom = top + entry.getItemHeight(); + int entryHeight = entry.getItemHeight() - 4; + if (bottom >= this.getY() && top <= this.getY() + this.getHeight()) { + this.renderItem(graphics, mouseX, mouseY, delta, i, left, top, right, entryHeight); + } + } + } + + /* END cloth config code */ + + @Override + public void visitWidgets(Consumer<AbstractWidget> consumer) { + } + + public abstract static class Entry<E extends Entry<E>> extends ContainerObjectSelectionList.Entry<E> { + @Override + public boolean mouseClicked(double mouseX, double mouseY, int button) { + for (GuiEventListener child : this.children()) { + if (child.mouseClicked(mouseX, mouseY, button)) { + if (button == InputConstants.MOUSE_BUTTON_LEFT) + this.setDragging(true); + return true; + } + } + + return false; + } + + @Override + public boolean mouseDragged(double mouseX, double mouseY, int button, double deltaX, double deltaY) { + if (isDragging() && button == InputConstants.MOUSE_BUTTON_LEFT) { + for (GuiEventListener child : this.children()) { + if (child.mouseDragged(mouseX, mouseY, button, deltaX, deltaY)) + return true; + } + } + return false; + } + + public int getItemHeight() { + return 22; + } + } + + /*? if <1.20.3 {*//* + @Override + public int getX() { + return x0; + } + + @Override + public int getY() { + return y0; + } + + @Override + public void setX(int x) { + int width = this.getWidth(); + x0 = x; + x1 = x + width; + } + + @Override + public void setY(int y) { + int height = this.getHeight(); + y0 = y; + y1 = y + height; + } + + public void setWidth(int width) { + x1 = x0 + width; + this.width = width; + } + + public void setHeight(int height) { + y1 = y0 + height; + this.height = height; + } + + @Override + public int getWidth() { + return width; + } + + @Override + public int getHeight() { + return height; + } + *//*?}*/ +} diff --git a/src/main/java/dev/isxander/yacl3/gui/LowProfileButtonWidget.java b/src/main/java/dev/isxander/yacl3/gui/LowProfileButtonWidget.java new file mode 100644 index 0000000..3f5822f --- /dev/null +++ b/src/main/java/dev/isxander/yacl3/gui/LowProfileButtonWidget.java @@ -0,0 +1,28 @@ +package dev.isxander.yacl3.gui; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.GuiGraphics; +import net.minecraft.client.gui.components.Button; +import net.minecraft.client.gui.components.Tooltip; +import net.minecraft.network.chat.Component; + +public class LowProfileButtonWidget extends Button { + public LowProfileButtonWidget(int x, int y, int width, int height, Component message, OnPress onPress) { + super(x, y, width, height, message, onPress, DEFAULT_NARRATION); + |
