diff options
author | isXander <xandersmith2008@gmail.com> | 2022-12-14 18:53:30 +0000 |
---|---|---|
committer | isXander <xandersmith2008@gmail.com> | 2022-12-14 18:53:30 +0000 |
commit | 51f4ae2d8e0a6cdcc7d50a037143f48a6132214a (patch) | |
tree | 9a48b8faf7c419cf9295e79b80e11e27fe8671b2 /src/client/java/dev/isxander/yacl/gui | |
parent | 3d1f7eb6c149c14ef9eea98d2d8caa6768f8c51c (diff) | |
download | YetAnotherConfigLib-51f4ae2d8e0a6cdcc7d50a037143f48a6132214a.tar.gz YetAnotherConfigLib-51f4ae2d8e0a6cdcc7d50a037143f48a6132214a.tar.bz2 YetAnotherConfigLib-51f4ae2d8e0a6cdcc7d50a037143f48a6132214a.zip |
lots of minor fixes with lists and abstract builders
Diffstat (limited to 'src/client/java/dev/isxander/yacl/gui')
-rw-r--r-- | src/client/java/dev/isxander/yacl/gui/OptionListWidget.java | 35 | ||||
-rw-r--r-- | src/client/java/dev/isxander/yacl/gui/controllers/ListEntryWidget.java | 3 |
2 files changed, 24 insertions, 14 deletions
diff --git a/src/client/java/dev/isxander/yacl/gui/OptionListWidget.java b/src/client/java/dev/isxander/yacl/gui/OptionListWidget.java index 8284b0e..c18597f 100644 --- a/src/client/java/dev/isxander/yacl/gui/OptionListWidget.java +++ b/src/client/java/dev/isxander/yacl/gui/OptionListWidget.java @@ -4,6 +4,8 @@ import com.google.common.collect.ImmutableList; import dev.isxander.yacl.api.*; import dev.isxander.yacl.api.utils.Dimension; import dev.isxander.yacl.gui.controllers.ListEntryWidget; +import dev.isxander.yacl.impl.ListOptionEntryImpl; +import dev.isxander.yacl.impl.utils.YACLConstants; import net.minecraft.client.MinecraftClient; import net.minecraft.client.font.MultilineText; import net.minecraft.client.font.TextRenderer; @@ -65,7 +67,7 @@ public class OptionListWidget extends ElementListWidgetExt<OptionListWidget.Entr List<OptionEntry> optionEntries = new ArrayList<>(); for (Option<?> option : group.options()) { - OptionEntry entry = new OptionEntry(option, category, group, groupSeparatorEntry, option.controller().provideWidget(yaclScreen, getDefaultEntryPosition())); + OptionEntry entry = new OptionEntry(option, category, group, groupSeparatorEntry, option.controller().provideWidget(yaclScreen, getDefaultEntryDimension())); addEntry(entry); optionEntries.add(entry); } @@ -85,23 +87,31 @@ public class OptionListWidget extends ElementListWidgetExt<OptionListWidget.Entr // find group separator for group GroupSeparatorEntry groupSeparator = super.children().stream().filter(e -> e instanceof GroupSeparatorEntry gs && gs.group == listOption).map(GroupSeparatorEntry.class::cast).findAny().orElse(null); - if (groupSeparator == null) + if (groupSeparator == null) { + YACLConstants.LOGGER.warn("Can't find group seperator to refresh list option entries for list option " + listOption.name()); return; + } for (OptionEntry entry : groupSeparator.optionEntries) super.removeEntry(entry); - groupSeparator.optionEntries.clear(); + + // if no entries, below loop won't run where addEntryBelow() recaches viewable children + if (listOption.options().isEmpty()) { + recacheViewableChildren(); + return; + } + Entry lastEntry = groupSeparator; for (ListOptionEntry<?> listOptionEntry : listOption.options()) { - OptionEntry optionEntry = new OptionEntry(listOptionEntry, category, listOption, groupSeparator, listOptionEntry.controller().provideWidget(yaclScreen, getDefaultEntryPosition())); + OptionEntry optionEntry = new OptionEntry(listOptionEntry, category, listOption, groupSeparator, listOptionEntry.controller().provideWidget(yaclScreen, getDefaultEntryDimension())); addEntryBelow(lastEntry, optionEntry); groupSeparator.optionEntries.add(optionEntry); lastEntry = optionEntry; } } - public Dimension<Integer> getDefaultEntryPosition() { + public Dimension<Integer> getDefaultEntryDimension() { return Dimension.ofInt(getRowLeft(), 0, getRowWidth(), 20); } @@ -245,9 +255,7 @@ public class OptionListWidget extends ElementListWidgetExt<OptionListWidget.Entr this.category = category; this.group = group; this.groupSeparatorEntry = groupSeparatorEntry; - if (option instanceof ListOptionEntry<?> listOptionEntry) - this.widget = new ListEntryWidget(yaclScreen, listOptionEntry, widget); - else this.widget = widget; + this.widget = widget; this.categoryName = category.name().getString().toLowerCase(); this.groupName = group.name().getString().toLowerCase(); if (option.canResetToDefault() && this.widget.canReset()) { @@ -306,7 +314,7 @@ public class OptionListWidget extends ElementListWidgetExt<OptionListWidget.Entr @Override public int getItemHeight() { - return widget.getDimension().height() + 2; + return Math.max(widget.getDimension().height(), resetButton != null ? resetButton.getHeight() : 0) + 2; } @Override @@ -377,13 +385,16 @@ public class OptionListWidget extends ElementListWidgetExt<OptionListWidget.Entr } public void setExpanded(boolean expanded) { + if (this.groupExpanded == expanded) + return; + this.groupExpanded = expanded; updateExpandMinimizeText(); + recacheViewableChildren(); } protected void onExpandButtonPress() { setExpanded(!isExpanded()); - recacheViewableChildren(); } protected void updateExpandMinimizeText() { @@ -447,6 +458,7 @@ public class OptionListWidget extends ElementListWidgetExt<OptionListWidget.Entr this.addListButton = new TooltipButtonWidget(yaclScreen, resetListButton.getX() - 20, -50, 20, 20, Text.of("+"), Text.translatable("yacl.list.add_top"), btn -> { group.insertNewEntryToTop(); + setExpanded(true); }); updateExpandMinimizeText(); @@ -480,15 +492,12 @@ public class OptionListWidget extends ElementListWidgetExt<OptionListWidget.Entr private void minimizeIfUnavailable() { if (!listOption.available() && isExpanded()) { setExpanded(false); - recacheViewableChildren(); } } @Override protected void updateExpandMinimizeText() { super.updateExpandMinimizeText(); - if (resetListButton != null && addListButton != null) - resetListButton.visible = addListButton.visible = isExpanded(); expandMinimizeButton.active = listOption == null || listOption.available(); } diff --git a/src/client/java/dev/isxander/yacl/gui/controllers/ListEntryWidget.java b/src/client/java/dev/isxander/yacl/gui/controllers/ListEntryWidget.java index a548efb..0a5d581 100644 --- a/src/client/java/dev/isxander/yacl/gui/controllers/ListEntryWidget.java +++ b/src/client/java/dev/isxander/yacl/gui/controllers/ListEntryWidget.java @@ -27,7 +27,7 @@ public class ListEntryWidget extends AbstractWidget implements ParentElement { private boolean dragging; public ListEntryWidget(YACLScreen screen, ListOptionEntry<?> listOptionEntry, AbstractWidget entryWidget) { - super(entryWidget.getDimension()); + super(entryWidget.getDimension().withHeight(Math.max(entryWidget.getDimension().height(), 20) - ((listOptionEntry.parentGroup().indexOf(listOptionEntry) == listOptionEntry.parentGroup().options().size() - 1) ? 0 : 2))); // -2 to remove the padding this.listOptionEntry = listOptionEntry; this.listOption = listOptionEntry.parentGroup(); this.optionNameString = listOptionEntry.name().getString().toLowerCase(); @@ -38,6 +38,7 @@ public class ListEntryWidget extends AbstractWidget implements ParentElement { removeButton = new TooltipButtonWidget(screen, dim.xLimit() - 20, dim.y(), 20, 20, Text.of("\u274c"), Text.translatable("yacl.list.remove"), btn -> { listOption.removeEntry(listOptionEntry); + updateButtonStates(); }); moveUpButton = new TooltipButtonWidget(screen, dim.x(), dim.y(), 20, 20, Text.of("\u2191"), Text.translatable("yacl.list.move_up"), btn -> { |