diff options
author | xander <xander@isxander.dev> | 2022-09-14 22:06:44 +0100 |
---|---|---|
committer | xander <xander@isxander.dev> | 2022-09-14 22:06:44 +0100 |
commit | e8b6e383b368706c992425f7de7023f24d376d7c (patch) | |
tree | c446479b16a1fb76b4bb6c239110e6d56a0c3b2c /src/main/java/dev | |
parent | 866bf73bbf18d0ffa0265aeade2066e1316175d0 (diff) | |
download | YetAnotherConfigLib-e8b6e383b368706c992425f7de7023f24d376d7c.tar.gz YetAnotherConfigLib-e8b6e383b368706c992425f7de7023f24d376d7c.tar.bz2 YetAnotherConfigLib-e8b6e383b368706c992425f7de7023f24d376d7c.zip |
scrollable categories
Diffstat (limited to 'src/main/java/dev')
5 files changed, 114 insertions, 22 deletions
diff --git a/src/main/java/dev/isxander/yacl/gui/CategoryListWidget.java b/src/main/java/dev/isxander/yacl/gui/CategoryListWidget.java new file mode 100644 index 0000000..9291329 --- /dev/null +++ b/src/main/java/dev/isxander/yacl/gui/CategoryListWidget.java @@ -0,0 +1,86 @@ +package dev.isxander.yacl.gui; + +import com.google.common.collect.ImmutableList; +import com.mojang.blaze3d.systems.RenderSystem; +import dev.isxander.yacl.api.ConfigCategory; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.Element; +import net.minecraft.client.gui.Selectable; +import net.minecraft.client.gui.widget.ElementListWidget; +import net.minecraft.client.util.math.MatrixStack; + +import java.util.List; + +public class CategoryListWidget extends ElementListWidget<CategoryListWidget.CategoryEntry> { + private final YACLScreen yaclScreen; + + public CategoryListWidget(MinecraftClient client, YACLScreen yaclScreen, int screenWidth, int screenHeight) { + super(client, screenWidth / 3, yaclScreen.searchFieldWidget.y - 5, 0, yaclScreen.searchFieldWidget.y - 5, 21); + this.yaclScreen = yaclScreen; + setRenderBackground(false); + setRenderHorizontalShadows(false); + + for (ConfigCategory category : yaclScreen.config.categories()) { + addEntry(new CategoryEntry(category)); + } + } + + @Override + protected void renderList(MatrixStack matrices, int mouseX, int mouseY, float delta) { + double d = this.client.getWindow().getScaleFactor(); + RenderSystem.enableScissor(0, (int)((yaclScreen.height - bottom) * d), (int)(width * d), (int)(height * d)); + super.renderList(matrices, mouseX, mouseY, delta); + RenderSystem.disableScissor(); + } + + @Override + public int getRowWidth() { + return width - width / 10; + } + + @Override + public int getRowLeft() { + return super.getRowLeft() - 2; + } + + @Override + protected int getScrollbarPositionX() { + return width - 2; + } + + public class CategoryEntry extends Entry<CategoryEntry> { + private final CategoryWidget categoryButton; + public final int categoryIndex; + + public CategoryEntry(ConfigCategory category) { + this.categoryIndex = yaclScreen.config.categories().indexOf(category); + categoryButton = new CategoryWidget( + yaclScreen, + category, + categoryIndex, + getRowLeft(), 0, + getRowWidth(), 20 + ); + } + + @Override + public void render(MatrixStack matrices, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta) { + if (mouseY > bottom) { + mouseY = -20; + } + + categoryButton.y = y; + categoryButton.render(matrices, mouseX, mouseY, tickDelta); + } + + @Override + public List<? extends Element> children() { + return ImmutableList.of(categoryButton); + } + + @Override + public List<? extends Selectable> selectableChildren() { + return ImmutableList.of(categoryButton); + } + } +} diff --git a/src/main/java/dev/isxander/yacl/gui/CategoryWidget.java b/src/main/java/dev/isxander/yacl/gui/CategoryWidget.java index 31dacb9..634056a 100644 --- a/src/main/java/dev/isxander/yacl/gui/CategoryWidget.java +++ b/src/main/java/dev/isxander/yacl/gui/CategoryWidget.java @@ -9,7 +9,7 @@ public class CategoryWidget extends TooltipButtonWidget { public CategoryWidget(YACLScreen screen, ConfigCategory category, int categoryIndex, int x, int y, int width, int height) { super(screen, x, y, width, height, category.name(), category.tooltip(), btn -> { screen.searchFieldWidget.setText(""); - screen.changeCategory(screen.categoryButtons.indexOf(btn)); + screen.changeCategory(categoryIndex); }); this.categoryIndex = categoryIndex; } diff --git a/src/main/java/dev/isxander/yacl/gui/OptionListWidget.java b/src/main/java/dev/isxander/yacl/gui/OptionListWidget.java index d6a28f3..466c44c 100644 --- a/src/main/java/dev/isxander/yacl/gui/OptionListWidget.java +++ b/src/main/java/dev/isxander/yacl/gui/OptionListWidget.java @@ -40,7 +40,7 @@ public class OptionListWidget extends ElementListWidget<OptionListWidget.Entry> } public void refreshOptions() { - super.children().clear(); + clearEntries(); List<ConfigCategory> categories = new ArrayList<>(); if (yaclScreen.currentCategoryIdx == -1) { diff --git a/src/main/java/dev/isxander/yacl/gui/SearchFieldWidget.java b/src/main/java/dev/isxander/yacl/gui/SearchFieldWidget.java index 68f050f..2af677d 100644 --- a/src/main/java/dev/isxander/yacl/gui/SearchFieldWidget.java +++ b/src/main/java/dev/isxander/yacl/gui/SearchFieldWidget.java @@ -28,6 +28,7 @@ public class SearchFieldWidget extends TextFieldWidget { @Override public void write(String text) { yaclScreen.optionList.setScrollAmount(0); + yaclScreen.categoryList.setScrollAmount(0); for (OptionListWidget.Entry entry : yaclScreen.optionList.children()) { if (entry instanceof OptionListWidget.GroupSeparatorEntry groupSeparatorEntry) { groupSeparatorEntry.setExpanded(true); diff --git a/src/main/java/dev/isxander/yacl/gui/YACLScreen.java b/src/main/java/dev/isxander/yacl/gui/YACLScreen.java index 0439da7..53fa2bf 100644 --- a/src/main/java/dev/isxander/yacl/gui/YACLScreen.java +++ b/src/main/java/dev/isxander/yacl/gui/YACLScreen.java @@ -26,7 +26,8 @@ public class YACLScreen extends Screen { private final Screen parent; public OptionListWidget optionList; - public final List<CategoryWidget> categoryButtons; +// public final List<CategoryWidget> categoryButtons; + public CategoryListWidget categoryList; public TooltipButtonWidget finishedSaveButton, cancelResetButton, undoButton; public SearchFieldWidget searchFieldWidget; @@ -39,34 +40,34 @@ public class YACLScreen extends Screen { super(config.title()); this.config = config; this.parent = parent; - this.categoryButtons = new ArrayList<>(); +// this.categoryButtons = new ArrayList<>(); this.currentCategoryIdx = 0; } @Override protected void init() { - categoryButtons.clear(); +// categoryButtons.clear(); int columnWidth = width / 3; int padding = columnWidth / 20; columnWidth = Math.min(columnWidth, 400); int paddedWidth = columnWidth - padding * 2; - Dimension<Integer> categoryDim = Dimension.ofInt(width / 3 / 2, padding, paddedWidth, 20); - int idx = 0; - for (ConfigCategory category : config.categories()) { - CategoryWidget categoryWidget = new CategoryWidget( - this, - category, - idx, - categoryDim.x() - categoryDim.width() / 2, categoryDim.y(), - categoryDim.width(), categoryDim.height() - ); - - categoryButtons.add(categoryWidget); - addDrawableChild(categoryWidget); - - idx++; - categoryDim.move(0, 21); - } +// Dimension<Integer> categoryDim = Dimension.ofInt(width / 3 / 2, padding, paddedWidth, 20); +// int idx = 0; +// for (ConfigCategory category : config.categories()) { +// CategoryWidget categoryWidget = new CategoryWidget( +// this, +// category, +// idx, +// categoryDim.x() - categoryDim.width() / 2, categoryDim.y(), +// categoryDim.width(), categoryDim.height() +// ); +// +// categoryButtons.add(categoryWidget); +// addDrawableChild(categoryWidget); +// +// idx++; +// categoryDim.move(0, 21); +// } Dimension<Integer> actionDim = Dimension.ofInt(width / 3 / 2, height - padding - 20, paddedWidth, 20); finishedSaveButton = new TooltipButtonWidget(this, actionDim.x() - actionDim.width() / 2, actionDim.y(), actionDim.width(), actionDim.height(), Text.empty(), Text.empty(), (btn) -> { @@ -107,6 +108,9 @@ public class YACLScreen extends Screen { searchFieldWidget = new SearchFieldWidget(this, textRenderer, width / 3 / 2 - paddedWidth / 2 + 1, undoButton.y - 22, paddedWidth - 2, 18, Text.translatable("yacl.gui.search"), Text.translatable("yacl.gui.search")); + categoryList = new CategoryListWidget(client, this, width, height); + addSelectableChild(categoryList); + updateActionAvailability(); addDrawableChild(searchFieldWidget); addDrawableChild(cancelResetButton); @@ -124,6 +128,7 @@ public class YACLScreen extends Screen { renderBackground(matrices); super.render(matrices, mouseX, mouseY, delta); + categoryList.render(matrices, mouseX, mouseY, delta); searchFieldWidget.render(matrices, mouseX, mouseY, delta); optionList.render(matrices, mouseX, mouseY, delta); |