Skip to content

Commit

Permalink
Merge pull request #110 from FTBTeam/feature/1.21/better-widget-controll
Browse files Browse the repository at this point in the history
[1.21] Allow Sidebar Buttons to be moveable
  • Loading branch information
desht authored Aug 9, 2024
2 parents 406506d + b6fb15e commit 88efc67
Show file tree
Hide file tree
Showing 29 changed files with 1,184 additions and 861 deletions.
27 changes: 5 additions & 22 deletions common/src/main/java/dev/ftb/mods/ftblibrary/FTBLibraryClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import dev.architectury.event.events.client.ClientGuiEvent;
import dev.architectury.event.events.client.ClientTickEvent;
import dev.architectury.hooks.client.screen.ScreenAccess;
import dev.architectury.platform.Platform;
import dev.architectury.registry.ReloadListenerRegistry;
import dev.ftb.mods.ftblibrary.config.FTBLibraryClientConfig;
import dev.ftb.mods.ftblibrary.config.ui.EditConfigScreen;
Expand All @@ -13,33 +12,22 @@
import dev.ftb.mods.ftblibrary.ui.CursorType;
import dev.ftb.mods.ftblibrary.ui.IScreenWrapper;
import dev.ftb.mods.ftblibrary.util.client.ClientUtils;
import me.shedaniel.rei.api.client.config.ConfigObject;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.components.events.GuiEventListener;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.client.gui.screens.inventory.EffectRenderingInventoryScreen;
import net.minecraft.server.packs.PackType;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;

public class FTBLibraryClient {
/**
* Meaning of the different values: 0 = No, 1 = Yes, 2 = Only in inventory, 3 = Managed by integration
* (should this be an enum instead at this point?)
*/
public static int showButtons = 1;

public static CursorType lastCursorType = null;

public static void init() {
FTBLibraryClientConfig.load();

// when using REI >= 6, disable the regular sidebar buttons,
// we'll be using REI's system favourites instead.
if (Platform.isModLoaded("roughlyenoughitems")) {
showButtons = 3;
}

// Datagens hahayes
if (Minecraft.getInstance() == null) {
return;
Expand Down Expand Up @@ -73,24 +61,19 @@ private static void clientTick(Minecraft client) {

ClientUtils.RUN_LATER.clear();
}

}

public static boolean areButtonsVisible(@Nullable Screen gui) {
if (Minecraft.getInstance().level == null || Minecraft.getInstance().player == null) {
return false;
}

if (showButtons == 0 || showButtons == 2 && !(gui instanceof EffectRenderingInventoryScreen)) {
if (!FTBLibraryClientConfig.SIDEBAR_ENABLED.get()) {
return false;
}

if(showButtons == 3 && Platform.isModLoaded("roughlyenoughitems")) {
if (ConfigObject.getInstance().isFavoritesEnabled()) {
return false;
}
}

return gui instanceof AbstractContainerScreen && !SidebarButtonManager.INSTANCE.getGroups().isEmpty();
return gui instanceof AbstractContainerScreen && !SidebarButtonManager.INSTANCE.getButtons().isEmpty();
}

public static void editConfig(boolean isClientConfig) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package dev.ftb.mods.ftblibrary.api.sidebar;

import com.mojang.blaze3d.systems.RenderSystem;
import dev.ftb.mods.ftblibrary.icon.Color4I;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;

import java.util.function.Supplier;

public interface ButtonOverlayRender {

/**
* Called when the button is rendering
* graphics is aligned so that 0, 0 is the top left corner of the button
* @param graphics The graphics object
* @param font The font object
* @param buttonSize The size of the button
*/
void render(GuiGraphics graphics, Font font, int buttonSize);

static ButtonOverlayRender ofSimpleString(Supplier<String> customTextHandler) {
return (graphics, font, buttonSize) -> {
String text = customTextHandler.get();
if (!text.isEmpty()) {
var nw = font.width(text);
Color4I.LIGHT_RED.draw(graphics, buttonSize - nw, -1, nw + 1, 9);
graphics.drawString(font, text, buttonSize - nw + 1, 0, 0xFFFFFFFF);
RenderSystem.setShaderColor(1F, 1F, 1F, 1F);
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package dev.ftb.mods.ftblibrary.api.sidebar;

import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;

import java.util.List;
import java.util.function.BooleanSupplier;
import java.util.function.Supplier;

public interface SidebarButton {

/**
* @return the id of the button used for saving config data created from the location button in resource path
*/
ResourceLocation getId();

/**
* Register a condition that must be met for the button to be visible
* @param condition a condition that must be met for the button to be visible
*/
void addVisibilityCondition(BooleanSupplier condition);

/**
* Register a custom overlay renderer to render on top of the button icon
* @param renderer the renderer to render on top of the button icon
*/
void addOverlayRender(ButtonOverlayRender renderer);

/**
* Override the default tooltip displayed when hovering over the button
* @param tooltipOverride a supplier that returns the tooltip to be displayed when hovering over the button
*/
void setTooltipOverride(Supplier<List<Component>> tooltipOverride);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package dev.ftb.mods.ftblibrary.api.sidebar;

import dev.architectury.event.Event;
import dev.architectury.event.EventFactory;
import dev.ftb.mods.ftblibrary.sidebar.RegisteredSidebarButton;

import java.util.function.Consumer;

public class SidebarButtonCreatedEvent {
public static final Event<Consumer<SidebarButtonCreatedEvent>> EVENT = EventFactory.createConsumerLoop(SidebarButtonCreatedEvent.class);

private final RegisteredSidebarButton button;

public SidebarButtonCreatedEvent(RegisteredSidebarButton button) {
this.button = button;
}

public RegisteredSidebarButton getButton() {
return button;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package dev.ftb.mods.ftblibrary.config;

import dev.ftb.mods.ftblibrary.snbt.config.BooleanValue;
import dev.ftb.mods.ftblibrary.snbt.config.IntArrayValue;
import dev.ftb.mods.ftblibrary.snbt.config.SNBTConfig;
import dev.ftb.mods.ftblibrary.snbt.config.*;

import java.util.HashMap;

import static dev.ftb.mods.ftblibrary.FTBLibrary.MOD_ID;
import static dev.ftb.mods.ftblibrary.snbt.config.ConfigUtil.LOCAL_DIR;
Expand All @@ -26,6 +26,14 @@ public interface FTBLibraryClientConfig {
IntArrayValue RECENT = COLOR.addIntArray("recents", new int[0])
.comment("Colors recently selected in the color selector");

SNBTConfig SIDEBAR = CONFIG.addGroup("sidebar");
BooleanValue SIDEBAR_ENABLED = SIDEBAR.addBoolean("enabled", true)
.comment("Enable the sidebar");
EnumValue<SidebarPosition> SIDEBAR_POSITION = SIDEBAR.addEnum("position", SidebarPosition.NAME_MAP, SidebarPosition.TOP_LEFT)
.comment("Position of the sidebar");

StringSidebarMapValue SIDEBAR_BUTTONS = SIDEBAR.add(new StringSidebarMapValue(SIDEBAR, "buttons", new HashMap<>()));

static void load() {
loadDefaulted(CONFIG, LOCAL_DIR, MOD_ID);
}
Expand All @@ -44,4 +52,29 @@ static ConfigGroup getConfigGroup() {

return group;
}

public enum SidebarPosition {
TOP_LEFT(false, false),
TOP_RIGHT(false, true),
BOTTOM_LEFT(true, false),
BOTTOM_RIGHT(true, true);

private final boolean isBottom;
private final boolean isRight;

SidebarPosition(boolean isBottom, boolean isRight) {
this.isBottom = isBottom;
this.isRight = isRight;
}

public boolean isBottom() {
return isBottom;
}

public boolean isRight() {
return isRight;
}

public static final NameMap<SidebarPosition> NAME_MAP = NameMap.of(TOP_LEFT, SidebarPosition.values()).baseNameKey("ftblibrary.panel.position").create();
}
}
11 changes: 11 additions & 0 deletions common/src/main/java/dev/ftb/mods/ftblibrary/icon/Icon.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,20 @@

import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.mojang.datafixers.FunctionType;
import com.mojang.datafixers.util.Pair;
import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
import com.mojang.serialization.Decoder;
import com.mojang.serialization.DynamicOps;
import com.mojang.serialization.Encoder;
import dev.ftb.mods.ftblibrary.config.ImageResourceConfig;
import dev.ftb.mods.ftblibrary.math.PixelBuffer;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.util.ExtraCodecs;
import org.jetbrains.annotations.Nullable;

import java.net.URI;
Expand All @@ -20,6 +29,8 @@ public static Color4I empty() {
return Color4I.EMPTY_ICON;
}

public static final Codec<Icon> CODEC = ExtraCodecs.JSON.xmap(Icon::getIcon, Icon::getJson);

public static final StreamCodec<FriendlyByteBuf,Icon> STREAM_CODEC = new StreamCodec<>() {
@Override
public Icon decode(FriendlyByteBuf buf) {
Expand Down
Loading

0 comments on commit 88efc67

Please sign in to comment.