Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved lists with DropdownBox support #265

Open
wants to merge 4 commits into
base: v15
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@
import net.fabricmc.api.Environment;
import net.minecraft.client.KeyMapping;
import net.minecraft.client.Minecraft;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextColor;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;

import java.util.List;
import java.util.function.Function;
Expand Down Expand Up @@ -155,4 +159,30 @@ default DropdownMenuBuilder<String> startStringDropdownMenu(Component fieldNameK
default DropdownMenuBuilder<String> startStringDropdownMenu(Component fieldNameKey, String value, Function<String, Component> toTextFunction) {
return startDropdownMenu(fieldNameKey, TopCellElementBuilder.of(value, s -> s, toTextFunction), new DefaultSelectionCellCreator<>());
}

<T> DropdownListBuilder<T> startDropdownList(Component fieldNameKey, List<T> value, Function<T, SelectionTopCellElement<T>> topCellCreator, SelectionCellCreator<T> cellCreator);

default DropdownListBuilder<ResourceLocation> startItemIdentifierList(Component fieldNameKey, List<ResourceLocation> value) {
DropdownListBuilder<ResourceLocation> entry = startDropdownList(fieldNameKey, value, element -> DropdownMenuBuilder.TopCellElementBuilder.ofItemIdentifier(BuiltInRegistries.ITEM.get(element)), DropdownMenuBuilder.CellCreatorBuilder.ofItemIdentifier());
entry.setSelections(BuiltInRegistries.ITEM.keySet());
return entry;
}

default DropdownListBuilder<ResourceLocation> startBlockIdentifierList(Component fieldNameKey, List<ResourceLocation> value) {
DropdownListBuilder<ResourceLocation> entry = startDropdownList(fieldNameKey, value, element -> DropdownMenuBuilder.TopCellElementBuilder.ofBlockIdentifier(BuiltInRegistries.BLOCK.get(element)), DropdownMenuBuilder.CellCreatorBuilder.ofBlockIdentifier());
entry.setSelections(BuiltInRegistries.BLOCK.keySet());
return entry;
}

default DropdownListBuilder<Item> startItemObjectList(Component fieldNameKey, List<Item> value) {
DropdownListBuilder<Item> entry = startDropdownList(fieldNameKey, value, element -> DropdownMenuBuilder.TopCellElementBuilder.ofItemObject(element), DropdownMenuBuilder.CellCreatorBuilder.ofItemObject());
entry.setSelections(BuiltInRegistries.ITEM);
return entry;
}

default DropdownListBuilder<Block> startBlockObjectList(Component fieldNameKey, List<Block> value) {
DropdownListBuilder<Block> entry = startDropdownList(fieldNameKey, value, element -> DropdownMenuBuilder.TopCellElementBuilder.ofBlockObject(element), DropdownMenuBuilder.CellCreatorBuilder.ofBlockObject());
entry.setSelections(BuiltInRegistries.BLOCK);
return entry;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ public void setErrorSupplier(Supplier<Optional<Component>> errorSupplier) {

public abstract void render(GuiGraphics graphics, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean isSelected, float delta);

public void lateRender(GuiGraphics graphics, int mouseX, int mouseY, float delta) {}

public int getMorePossibleHeight() {
return 0;
}

public void updateSelected(boolean isSelected) {}

public boolean isRequiresRestart() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,21 @@ public void render(GuiGraphics graphics, int index, int y, int x, int entryWidth
}
}

@Override
public void lateRender(GuiGraphics graphics, int mouseX, int mouseY, float delta) {
super.lateRender(graphics, mouseX, mouseY, delta);
BaseListCell focused = !isExpanded() || getFocused() == null || !(getFocused() instanceof BaseListCell) ? null : (BaseListCell) getFocused();
if(focused != null) {
focused.lateRender(graphics, mouseX, mouseY, delta);
}
}

@Override
public int getMorePossibleHeight() {
BaseListCell focused = !isExpanded() || getFocused() == null || !(getFocused() instanceof BaseListCell) ? null : (BaseListCell) getFocused();
return focused != null ? focused.getMorePossibleHeight() : 0;
}

@Override
public void updateSelected(boolean isSelected) {
for (C cell : cells) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public class DropdownBoxEntry<T> extends TooltipListEntry<T> {
protected SelectionElement<T> selectionElement;
@NotNull private final Supplier<T> defaultValue;
private boolean suggestionMode = true;
protected boolean dontReFocus = false;

@ApiStatus.Internal
@Deprecated
Expand All @@ -76,7 +77,7 @@ public DropdownBoxEntry(Component fieldName, @NotNull Component resetButtonKey,
this.resetButton = Button.builder(resetButtonKey, widget -> {
selectionElement.topRenderer.setValue(defaultValue.get());
}).bounds(0, 0, Minecraft.getInstance().font.width(resetButtonKey) + 6, 20).build();
this.selectionElement = new SelectionElement<>(this, new Rectangle(0, 0, 150, 20), new DefaultDropdownMenuElement<>(selections == null ? ImmutableList.of() : ImmutableList.copyOf(selections)), topRenderer, cellCreator);
this.selectionElement = new SelectionElement<>(this, new Rectangle(0, 0, fieldName.getString().isBlank() ? 300 : 150, 20), new DefaultDropdownMenuElement<>(selections == null ? ImmutableList.of() : ImmutableList.copyOf(selections)), topRenderer, cellCreator);
}

@Override
Expand All @@ -88,16 +89,17 @@ public void render(GuiGraphics graphics, int index, int y, int x, int entryWidth
this.selectionElement.active = isEditable();
this.selectionElement.bounds.y = y;
Component displayedFieldName = getDisplayedFieldName();
boolean hasName = !displayedFieldName.getString().isBlank();
if (Minecraft.getInstance().font.isBidirectional()) {
graphics.drawString(Minecraft.getInstance().font, displayedFieldName.getVisualOrderText(), window.getGuiScaledWidth() - x - Minecraft.getInstance().font.width(displayedFieldName), y + 6, getPreferredTextColor());
this.resetButton.setX(x);
this.selectionElement.bounds.x = x + resetButton.getWidth() + 1;
} else {
graphics.drawString(Minecraft.getInstance().font, displayedFieldName.getVisualOrderText(), x, y + 6, getPreferredTextColor());
this.resetButton.setX(x + entryWidth - resetButton.getWidth());
this.selectionElement.bounds.x = x + entryWidth - 150 + 1;
this.selectionElement.bounds.x = x + (hasName ? entryWidth - 150 : 0) + 1;
}
this.selectionElement.bounds.width = 150 - resetButton.getWidth() - 4;
this.selectionElement.bounds.width = (hasName ? 150 : entryWidth) - resetButton.getWidth() - 4;
resetButton.render(graphics, mouseX, mouseY, delta);
selectionElement.render(graphics, mouseX, mouseY, delta);
}
Expand Down Expand Up @@ -171,13 +173,22 @@ public boolean mouseScrolled(double double_1, double double_2, double amountX, d
return selectionElement.mouseScrolled(double_1, double_2, amountX, amountY);
}

@Override
public boolean mouseClicked(double double_1, double double_2, int int_1) {
boolean b = super.mouseClicked(double_1, double_2, int_1);
if (dontReFocus) {
setFocused(null);
dontReFocus = false;
}
return b;
}

public static class SelectionElement<R> extends AbstractContainerEventHandler implements Renderable {
protected Rectangle bounds;
protected boolean active;
protected SelectionTopCellElement<R> topRenderer;
protected DropdownBoxEntry<R> entry;
protected DropdownMenuElement<R> menu;
protected boolean dontReFocus = false;

public SelectionElement(DropdownBoxEntry<R> entry, Rectangle bounds, DropdownMenuElement<R> menu, SelectionTopCellElement<R> topRenderer, SelectionCellCreator<R> cellCreator) {
this.bounds = bounds;
Expand Down Expand Up @@ -230,17 +241,6 @@ public R getValue() {
public List<? extends GuiEventListener> children() {
return Lists.newArrayList(topRenderer, menu);
}

@Override
public boolean mouseClicked(double double_1, double double_2, int int_1) {
dontReFocus = false;
boolean b = super.mouseClicked(double_1, double_2, int_1);
if (dontReFocus) {
setFocused(null);
dontReFocus = false;
}
return b;
}
}

public static abstract class DropdownMenuElement<R> extends AbstractContainerEventHandler {
Expand All @@ -253,6 +253,10 @@ public SelectionCellCreator<R> getCellCreator() {
return cellCreator;
}

public int getCellWidth() {
return getCellCreator().getCellWidth() > 0 ? getCellCreator().getCellWidth() : getEntry().selectionElement.bounds.width;
}

@NotNull
public final DropdownBoxEntry<R> getEntry() {
return entry;
Expand Down Expand Up @@ -408,18 +412,19 @@ private void updatePosition(float delta) {
@Override
public void lateRender(GuiGraphics graphics, int mouseX, int mouseY, float delta) {
int last10Height = getHeight();
int cWidth = getCellCreator().getCellWidth();
graphics.fill(lastRectangle.x, lastRectangle.y + lastRectangle.height, lastRectangle.x + cWidth, lastRectangle.y + lastRectangle.height + last10Height + 1, isExpanded() ? -1 : -6250336);
graphics.fill(lastRectangle.x + 1, lastRectangle.y + lastRectangle.height + 1, lastRectangle.x + cWidth - 1, lastRectangle.y + lastRectangle.height + last10Height, -16777216);
int cWidth = getCellWidth();
graphics.pose().pushPose();
graphics.pose().translate(0, 0, 300f);
graphics.fill(lastRectangle.x, lastRectangle.y + lastRectangle.height, lastRectangle.x + cWidth, lastRectangle.y + lastRectangle.height + last10Height + 1, isExpanded() ? 0xFFFFFFFF : -6250336);
graphics.fill(lastRectangle.x + 1, lastRectangle.y + lastRectangle.height + 1, lastRectangle.x + cWidth - 1, lastRectangle.y + lastRectangle.height + last10Height, 0xFF000000);
graphics.pose().translate(0, 0, 300f);

ScissorsHandler.INSTANCE.scissor(new Rectangle(lastRectangle.x, lastRectangle.y + lastRectangle.height + 1, cWidth - 6, last10Height - 1));
double yy = lastRectangle.y + lastRectangle.height - scroll;
double yy = lastRectangle.y + lastRectangle.height - scroll + 1;
for (SelectionCellElement<R> cell : currentElements) {
if (yy + getCellCreator().getCellHeight() >= lastRectangle.y + lastRectangle.height && yy <= lastRectangle.y + lastRectangle.height + last10Height + 1) {
graphics.fill(lastRectangle.x + 1, (int) yy, lastRectangle.x + getCellCreator().getCellWidth(), (int) yy + getCellCreator().getCellHeight(), 0xFF000000);
cell.render(graphics, mouseX, mouseY, lastRectangle.x, (int) yy, getMaxScrollPosition() > 6 ? getCellCreator().getCellWidth() - 6 : getCellCreator().getCellWidth(), getCellCreator().getCellHeight(), delta);
graphics.fill(lastRectangle.x + 1, (int) yy, lastRectangle.x + cWidth, (int) yy + getCellCreator().getCellHeight(), 0xFF000000);
cell.render(graphics, mouseX, mouseY, lastRectangle.x, (int) yy, getMaxScrollPosition() > 6 ? cWidth - 6 : cWidth, getCellCreator().getCellHeight(), delta);
} else
cell.dontRender(graphics, delta);
yy += getCellCreator().getCellHeight();
Expand All @@ -429,12 +434,12 @@ public void lateRender(GuiGraphics graphics, int mouseX, int mouseY, float delta
if (currentElements.isEmpty()) {
Font textRenderer = Minecraft.getInstance().font;
Component text = Component.translatable("text.cloth-config.dropdown.value.unknown");
graphics.drawString(textRenderer, text.getVisualOrderText(), (int) (lastRectangle.x + getCellCreator().getCellWidth() / 2f - textRenderer.width(text) / 2f), lastRectangle.y + lastRectangle.height + 3, -1);
graphics.drawString(textRenderer, text.getVisualOrderText(), (int) (lastRectangle.x + cWidth / 2f - textRenderer.width(text) / 2f), lastRectangle.y + lastRectangle.height + 3, -1);
}

if (getMaxScrollPosition() > 6) {
RenderSystem.setShader(GameRenderer::getPositionTexColorShader);
int scrollbarPositionMinX = lastRectangle.x + getCellCreator().getCellWidth() - 6;
int scrollbarPositionMinX = lastRectangle.x + cWidth - 6;
int scrollbarPositionMaxX = scrollbarPositionMinX + 6;
int height = (int) (((last10Height) * (last10Height)) / this.getMaxScrollPosition());
height = Mth.clamp(height, 32, last10Height - 8);
Expand Down Expand Up @@ -470,7 +475,7 @@ public int getHeight() {

@Override
public boolean isMouseOver(double mouseX, double mouseY) {
return isExpanded() && mouseX >= lastRectangle.x && mouseX <= lastRectangle.x + getCellCreator().getCellWidth() && mouseY >= lastRectangle.y + lastRectangle.height && mouseY <= lastRectangle.y + lastRectangle.height + getHeight() + 1;
return isExpanded() && mouseX >= lastRectangle.x && mouseX <= lastRectangle.x + getCellWidth() && mouseY >= lastRectangle.y + lastRectangle.height && mouseY <= lastRectangle.y + lastRectangle.height + getHeight() + 1;
}

@Override
Expand Down Expand Up @@ -505,15 +510,27 @@ public boolean mouseScrolled(double mouseX, double mouseY, double amountX, doubl
}

protected void updateScrollingState(double double_1, double double_2, int int_1) {
this.scrolling = isExpanded() && lastRectangle != null && int_1 == 0 && double_1 >= (double) lastRectangle.x + getCellCreator().getCellWidth() - 6 && double_1 < (double) (lastRectangle.x + getCellCreator().getCellWidth());
this.scrolling = isExpanded() && lastRectangle != null && int_1 == 0 && double_1 >= (double) lastRectangle.x + getCellWidth() - 6 && double_1 < (double) (lastRectangle.x + getCellWidth());
}

@Override
public boolean mouseClicked(double double_1, double double_2, int int_1) {
if (!isExpanded())
return false;
updateScrollingState(double_1, double_2, int_1);
return super.mouseClicked(double_1, double_2, int_1) || scrolling;

if(!isMouseOver(double_1, double_2)) {
getEntry().dontReFocus = true;
getEntry().setFocused(null);
return true;
} else {
boolean elementClicked = super.mouseClicked(double_1, double_2, int_1);
if(elementClicked) {
getEntry().dontReFocus = true;
getEntry().setFocused(null);
}
return elementClicked || scrolling;
}
}

public void offset(double value, boolean animated) {
Expand Down Expand Up @@ -548,7 +565,7 @@ public static abstract class SelectionCellCreator<R> {
public abstract int getDropBoxMaxHeight();

public int getCellWidth() {
return 132;
return -1;
}
}

Expand Down Expand Up @@ -652,8 +669,8 @@ public boolean mouseClicked(double mouseX, double mouseY, int int_1) {
boolean b = rendering && mouseX >= x && mouseX <= x + width && mouseY >= y && mouseY <= y + height;
if (b) {
getEntry().selectionElement.topRenderer.setValue(r);
getEntry().selectionElement.setFocused(null);
getEntry().selectionElement.dontReFocus = true;
getEntry().setFocused(null);
getEntry().dontReFocus = true;
return true;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public MultiElementListEntry(Component categoryName, T object, List<AbstractConf
this.object = object;
this.entries = entries;
this.expanded = defaultExpanded;
this.widget = new MultiElementListEntry<T>.CategoryLabelWidget();
this.widget = new MultiElementListEntry.CategoryLabelWidget();
this.children = Lists.newArrayList(widget);
this.children.addAll(entries);
this.setReferenceProviderEntries((List) entries);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,18 @@ public void render(GuiGraphics graphics, int index, int y, int x, int entryWidth
nestedEntry.render(graphics, index, y, x, entryWidth, entryHeight, mouseX, mouseY, isSelected, delta);
}

@Override
public void lateRender(GuiGraphics graphics, int mouseX, int mouseY, float delta) {
nestedEntry.setParent((DynamicEntryListWidget) listListEntry.getParent());
nestedEntry.setScreen(listListEntry.getConfigScreen());
nestedEntry.lateRender(graphics, mouseX, mouseY, delta);
}

@Override
public int getMorePossibleHeight() {
return nestedEntry.getMorePossibleHeight();
}

@Override
public List<? extends GuiEventListener> children() {
return Collections.singletonList(nestedEntry);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import java.util.List;
import java.util.UUID;
import java.util.function.Function;

@Environment(EnvType.CLIENT)
public class ConfigEntryBuilderImpl implements ConfigEntryBuilder {
Expand Down Expand Up @@ -176,4 +177,8 @@ public <T> DropdownMenuBuilder<T> startDropdownMenu(Component fieldNameKey, Sele
return new DropdownMenuBuilder<>(resetButtonKey, fieldNameKey, topCellElement, cellCreator);
}

@Override
public <T> DropdownListBuilder<T> startDropdownList(Component fieldNameKey, List<T> value, Function<T, SelectionTopCellElement<T>> topCellCreator, SelectionCellCreator<T> cellCreator) {
return new DropdownListBuilder<>(resetButtonKey, fieldNameKey, value, topCellCreator, cellCreator);
}
}
Loading