Skip to content

Commit

Permalink
Add API to control creative inventory screen (#3814)
Browse files Browse the repository at this point in the history
* Add API to control creative inventory screen

* Rename methods

* Apply suggestions from code review

Co-authored-by: haykam821 <24855774+haykam821@users.noreply.github.com>

* Update fabric-item-group-api-v1/src/client/java/net/fabricmc/fabric/api/client/itemgroup/v1/FabricCreativeInventoryScreen.java

Co-authored-by: haykam821 <24855774+haykam821@users.noreply.github.com>

---------

Co-authored-by: haykam821 <24855774+haykam821@users.noreply.github.com>

(cherry picked from commit 00ab0a6)
  • Loading branch information
modmuss50 committed Jun 8, 2024
1 parent bd772e8 commit e0fdb01
Show file tree
Hide file tree
Showing 8 changed files with 257 additions and 127 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.fabricmc.fabric.api.client.itemgroup.v1;

import java.util.List;

import net.minecraft.client.gui.screen.ingame.CreativeInventoryScreen;
import net.minecraft.item.ItemGroup;

/**
* Fabric provided extensions to {@link CreativeInventoryScreen}.
* This interface is automatically implemented on all creative inventory screens via Mixin and interface injection.
*/
public interface FabricCreativeInventoryScreen {
/**
* Switches to the page with the given index if it exists.
*
* @param page the index of the page to switch to
* @return Returns true when the page was changed
*/
default boolean switchToPage(int page) {
throw new AssertionError("Implemented by mixin");
}

/**
* Switches to the next page if it exists.
*
* @return Returns true when the page was changed
*/
default boolean switchToNextPage() {
return switchToPage(getCurrentPage() + 1);
}

/**
* Switches to the previous page if it exists.
*
* @return Returns true when the page was changed
*/
default boolean switchToPreviousPage() {
return switchToPage(getCurrentPage() - 1);
}

/**
* Returns the index of the current page.
*/
default int getCurrentPage() {
throw new AssertionError("Implemented by mixin");
}

/**
* Returns the total number of pages.
*/
default int getPageCount() {
throw new AssertionError("Implemented by mixin");
}

/**
* Returns an ordered list containing the item groups on the requested page.
*/
default List<ItemGroup> getItemGroupsOnPage(int page) {
throw new AssertionError("Implemented by mixin");
}

/**
* Returns the page index of the given item group.
*
* <p>Item groups appearing on every page always return the current page index.
*
* @param itemGroup the item group to get the page index for
* @return the page index of the item group
*/
default int getPage(ItemGroup itemGroup) {
throw new AssertionError("Implemented by mixin");
}

/**
* Returns whether there are additional pages to show on top of the default vanilla pages.
*
* @return true if there are additional pages
*/
default boolean hasAdditionalPages() {
throw new AssertionError("Implemented by mixin");
}

/**
* Returns the {@link ItemGroup} that is associated with the currently selected tab.
*
* @return the currently selected {@link ItemGroup}
*/
default ItemGroup getSelectedItemGroup() {
throw new AssertionError("Implemented by mixin");
}

/**
* Sets the currently selected tab to the given {@link ItemGroup}.
*
* @param itemGroup the {@link ItemGroup} to select
* @return true if the tab was successfully selected
*/
default boolean setSelectedItemGroup(ItemGroup itemGroup) {
throw new AssertionError("Implemented by mixin");
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import net.minecraft.client.MinecraftClient;
Expand All @@ -30,31 +31,33 @@
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;

import net.fabricmc.fabric.impl.itemgroup.FabricItemGroup;
import net.fabricmc.fabric.impl.itemgroup.FabricItemGroupImpl;

public class FabricCreativeGuiComponents {
private static final Identifier BUTTON_TEX = new Identifier("fabric", "textures/gui/creative_buttons.png");
private static final double TABS_PER_PAGE = FabricItemGroup.TABS_PER_PAGE;
private static final double TABS_PER_PAGE = FabricItemGroupImpl.TABS_PER_PAGE;
public static final Set<ItemGroup> COMMON_GROUPS = Set.of(ItemGroups.SEARCH, ItemGroups.INVENTORY, ItemGroups.HOTBAR).stream()
.map(Registries.ITEM_GROUP::getOrThrow)
.collect(Collectors.toSet());

public static int getPageCount() {
return (int) Math.ceil((ItemGroups.getGroupsToDisplay().size() - COMMON_GROUPS.size()) / TABS_PER_PAGE);
}

public static class ItemGroupButtonWidget extends ButtonWidget {
final CreativeGuiExtensions extensions;
final CreativeInventoryScreen gui;
final CreativeInventoryScreen screen;
final Type type;

public ItemGroupButtonWidget(int x, int y, Type type, CreativeGuiExtensions extensions) {
super(x, y, 11, 12, type.text, (bw) -> type.clickConsumer.accept(extensions), ButtonWidget.DEFAULT_NARRATION_SUPPLIER);
this.extensions = extensions;
public ItemGroupButtonWidget(int x, int y, Type type, CreativeInventoryScreen screen) {
super(x, y, 11, 12, type.text, (bw) -> type.clickConsumer.accept(screen), ButtonWidget.DEFAULT_NARRATION_SUPPLIER);
this.type = type;
this.gui = (CreativeInventoryScreen) extensions;
this.screen = screen;
}

@Override
protected void renderWidget(DrawContext drawContext, int mouseX, int mouseY, float delta) {
this.active = extensions.fabric_isButtonEnabled(type);
this.visible = extensions.fabric_isButtonVisible(type);
this.active = type.isEnabled.test(screen);
this.visible = screen.hasAdditionalPages();

if (!this.visible) {
return;
Expand All @@ -65,22 +68,23 @@ protected void renderWidget(DrawContext drawContext, int mouseX, int mouseY, flo
drawContext.drawTexture(BUTTON_TEX, this.getX(), this.getY(), u + (type == Type.NEXT ? 11 : 0), v, 11, 12);

if (this.isHovered()) {
int pageCount = (int) Math.ceil((ItemGroups.getGroupsToDisplay().size() - COMMON_GROUPS.size()) / TABS_PER_PAGE);
drawContext.drawTooltip(MinecraftClient.getInstance().textRenderer, Text.translatable("fabric.gui.creativeTabPage", extensions.fabric_currentPage() + 1, pageCount), mouseX, mouseY);
drawContext.drawTooltip(MinecraftClient.getInstance().textRenderer, Text.translatable("fabric.gui.creativeTabPage", screen.getCurrentPage() + 1, getPageCount()), mouseX, mouseY);
}
}
}

public enum Type {
NEXT(Text.literal(">"), CreativeGuiExtensions::fabric_nextPage),
PREVIOUS(Text.literal("<"), CreativeGuiExtensions::fabric_previousPage);
NEXT(Text.literal(">"), CreativeInventoryScreen::switchToNextPage, screen -> screen.getCurrentPage() + 1 < screen.getPageCount()),
PREVIOUS(Text.literal("<"), CreativeInventoryScreen::switchToPreviousPage, screen -> screen.getCurrentPage() != 0);

final Text text;
final Consumer<CreativeGuiExtensions> clickConsumer;
final Consumer<CreativeInventoryScreen> clickConsumer;
final Predicate<CreativeInventoryScreen> isEnabled;

Type(Text text, Consumer<CreativeGuiExtensions> clickConsumer) {
Type(Text text, Consumer<CreativeInventoryScreen> clickConsumer, Predicate<CreativeInventoryScreen> isEnabled) {
this.text = text;
this.clickConsumer = clickConsumer;
this.isEnabled = isEnabled;
}
}
}
Loading

0 comments on commit e0fdb01

Please sign in to comment.