Skip to content

Commit

Permalink
Move subcategory buttons to the right if the category has no entries
Browse files Browse the repository at this point in the history
Mild positioning tweaks to category buttons to leave more space for
the description (they wasted quite a bit of space with advancement
locks disabled)
  • Loading branch information
Hubry committed Dec 22, 2020
1 parent 9162768 commit d60c0ec
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package vazkii.patchouli.client.book.gui;

import com.mojang.blaze3d.matrix.MatrixStack;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.widget.TextFieldWidget;
import net.minecraft.client.gui.widget.button.Button;
import net.minecraft.client.resources.I18n;

import vazkii.patchouli.client.book.BookCategory;
import vazkii.patchouli.client.book.BookEntry;
Expand All @@ -12,7 +16,8 @@

public class GuiBookCategory extends GuiBookEntryList {

BookCategory category;
private final BookCategory category;
private int subcategoryButtonCount;

public GuiBookCategory(Book book, BookCategory category) {
super(book, category.getName());
Expand All @@ -29,18 +34,36 @@ protected Collection<BookEntry> getEntries() {
return category.getEntries();
}

@Override
void drawForegroundElements(MatrixStack ms, int mouseX, int mouseY, float partialTicks) {
super.drawForegroundElements(ms, mouseX, mouseY, partialTicks);
if (getEntries().isEmpty() && subcategoryButtonCount <= 16 && subcategoryButtonCount > 0) {
int bottomSeparator = TOP_PADDING + 37 + 24 * ((subcategoryButtonCount - 1) / 4 + 1);
drawSeparator(ms, book, RIGHT_PAGE_X, bottomSeparator);
}
}

@Override
protected void addSubcategoryButtons() {
int i = 0;
List<BookCategory> categories = new ArrayList<>(book.contents.categories.values());
categories.removeIf(cat -> cat.getParentCategory() != category || cat.shouldHide());
Collections.sort(categories);

int baseY = TOP_PADDING + PAGE_HEIGHT - (categories.size() / 4) * 20 - (!book.advancementsEnabled() ? 46 : 68);
subcategoryButtonCount = categories.size();

int baseX, baseY;
boolean rightPageFree = getEntries().isEmpty();
if (rightPageFree) {
baseX = RIGHT_PAGE_X + 10;
baseY = TOP_PADDING + 25;
} else {
baseX = LEFT_PAGE_X + 10;
baseY = TOP_PADDING + PAGE_HEIGHT - (categories.size() / 4) * 20 - (!book.advancementsEnabled() ? 38 : 64);
}

for (BookCategory ocategory : categories) {
int x = LEFT_PAGE_X + 10 + (i % 4) * 24;
int y = baseY + (i / 4) * 20;
int x = baseX + (i % 4) * 24;
int y = baseY + (i / 4) * (rightPageFree ? 24 : 20);

Button button = new GuiButtonCategory(this, x, y, ocategory, this::handleButtonCategory);
addButton(button);
Expand All @@ -50,6 +73,33 @@ protected void addSubcategoryButtons() {
}
}

@Override
protected String getChapterListTitle() {
if (getEntries().isEmpty() && subcategoryButtonCount > 0) {
return I18n.format("patchouli.gui.lexicon.categories");
}
return super.getChapterListTitle();
}

@Override
protected String getNoEntryMessage() {
if (subcategoryButtonCount > 0) {
return "";
}
return super.getNoEntryMessage();
}

@Override
protected TextFieldWidget createSearchBar() {
TextFieldWidget widget = super.createSearchBar();
if (getEntries().isEmpty()) {
widget.active = false;
widget.setEnabled(false);
widget.setVisible(false);
}
return widget;
}

@Override
protected boolean doesEntryCountForProgress(BookEntry entry) {
return entry.getCategory() == category;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,20 @@ public void init() {
Collections.sort(allEntries);
}

searchField = new TextFieldWidget(font, 160, 170, 90, 12, StringTextComponent.EMPTY);
searchField.setMaxStringLength(32);
searchField.setEnableBackgroundDrawing(false);
searchField.setCanLoseFocus(false);
searchField.setFocused2(true);

this.searchField = createSearchBar();
dependentButtons = new ArrayList<>();
buildEntryButtons();
}

protected TextFieldWidget createSearchBar() {
TextFieldWidget field = new TextFieldWidget(font, 160, 170, 90, 12, StringTextComponent.EMPTY);
field.setMaxStringLength(32);
field.setEnableBackgroundDrawing(false);
field.setCanLoseFocus(false);
field.setFocused2(true);
return field;
}

protected abstract String getDescriptionText();
protected abstract Collection<BookEntry> getEntries();

Expand All @@ -86,7 +90,7 @@ void drawForegroundElements(MatrixStack ms, int mouseX, int mouseY, float partia

if (spread == 0) {
drawCenteredStringNoShadow(ms, getTitle().func_241878_f(), LEFT_PAGE_X + PAGE_WIDTH / 2, TOP_PADDING, book.headerColor);
drawCenteredStringNoShadow(ms, I18n.format("patchouli.gui.lexicon.chapters"), RIGHT_PAGE_X + PAGE_WIDTH / 2, TOP_PADDING, book.headerColor);
drawCenteredStringNoShadow(ms, getChapterListTitle(), RIGHT_PAGE_X + PAGE_WIDTH / 2, TOP_PADDING, book.headerColor);

drawSeparator(ms, book, LEFT_PAGE_X, TOP_PADDING + 12);
drawSeparator(ms, book, RIGHT_PAGE_X, TOP_PADDING + 12);
Expand All @@ -113,11 +117,19 @@ void drawForegroundElements(MatrixStack ms, int mouseX, int mouseY, float partia
drawCenteredStringNoShadow(ms, I18n.format("patchouli.gui.lexicon.sad"), GuiBook.RIGHT_PAGE_X / 2 + GuiBook.PAGE_WIDTH / 4, 47, 0x999999);
ms.scale(0.5F, 0.5F, 0.5F);
} else {
drawCenteredStringNoShadow(ms, I18n.format("patchouli.gui.lexicon.no_entries"), GuiBook.RIGHT_PAGE_X + GuiBook.PAGE_WIDTH / 2, 80, 0x333333);
drawCenteredStringNoShadow(ms, getNoEntryMessage(), GuiBook.RIGHT_PAGE_X + GuiBook.PAGE_WIDTH / 2, 80, 0x333333);
}
}
}

protected String getChapterListTitle() {
return I18n.format("patchouli.gui.lexicon.chapters");
}

protected String getNoEntryMessage() {
return I18n.format("patchouli.gui.lexicon.no_entries");
}

@Override
public boolean mouseClickedScaled(double mouseX, double mouseY, int mouseButton) {
return text.click(mouseX, mouseY, mouseButton)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "Subcategories",
"description": "Tests for subcategories. This category just has a bunch of filler subcategories to see how button wrapping looks.",
"description": "Tests for subcategories. No entries here, so the subcategory buttons take their place.",
"icon": "minecraft:writable_book"
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "Empty Sub-sub-category",
"parent": "patchouli:subcategories/normal_subcategory",
"description": "Empty sub-sub-category with no entries",
"icon": "minecraft:iron_ingot"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "Empty Sub-sub-category 2",
"parent": "patchouli:subcategories/normal_subcategory",
"description": "Empty sub-sub-category with no entries",
"icon": "minecraft:gold_ingot"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "Empty Sub-sub-category 3",
"parent": "patchouli:subcategories/normal_subcategory",
"description": "Empty sub-sub-category with no entries",
"icon": "minecraft:iron_pickaxe"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "Empty Sub-sub-category 4",
"parent": "patchouli:subcategories/normal_subcategory",
"description": "Empty sub-sub-category with no entries",
"icon": "minecraft:iron_axe"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "Empty Sub-sub-category 5",
"parent": "patchouli:subcategories/normal_subcategory",
"description": "Empty sub-sub-category with no entries",
"icon": "minecraft:iron_sword"
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Normal Subcategory",
"parent": "patchouli:subcategories",
"description": "Normal subcategory with some entries",
"description": "Normal subcategory with some entries and some empty subcategories to observe the behavior of category button wrapping.",
"icon": "minecraft:cooked_chicken"
}

0 comments on commit d60c0ec

Please sign in to comment.