From 4811bde1c7b0723d749e5f9da238aee2ccaf05c5 Mon Sep 17 00:00:00 2001 From: Basique Date: Tue, 16 Jul 2024 21:59:14 +0300 Subject: [PATCH] first prototype of component edit screen --- .../gadget/client/GadgetClient.java | 40 ++++--- .../client/nbt/StackComponentDataScreen.java | 81 +++++++++++++ .../gadget/client/nbt/StackNbtDataScreen.java | 106 ------------------ 3 files changed, 103 insertions(+), 124 deletions(-) create mode 100644 src/main/java/io/wispforest/gadget/client/nbt/StackComponentDataScreen.java delete mode 100644 src/main/java/io/wispforest/gadget/client/nbt/StackNbtDataScreen.java diff --git a/src/main/java/io/wispforest/gadget/client/GadgetClient.java b/src/main/java/io/wispforest/gadget/client/GadgetClient.java index 6841908..b66faeb 100644 --- a/src/main/java/io/wispforest/gadget/client/GadgetClient.java +++ b/src/main/java/io/wispforest/gadget/client/GadgetClient.java @@ -12,8 +12,10 @@ import io.wispforest.gadget.client.gui.GadgetScreen; import io.wispforest.gadget.client.gui.inspector.UIInspector; import io.wispforest.gadget.client.log.ChatLogAppender; +import io.wispforest.gadget.client.nbt.StackComponentDataScreen; import io.wispforest.gadget.client.resource.ViewResourcesScreen; import io.wispforest.gadget.mappings.MappingsManager; +import io.wispforest.gadget.mixin.client.HandledScreenAccessor; import io.wispforest.gadget.network.BlockEntityTarget; import io.wispforest.gadget.network.EntityTarget; import io.wispforest.gadget.network.GadgetNetworking; @@ -41,6 +43,8 @@ import net.minecraft.client.gui.screen.GameMenuScreen; import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.gui.screen.TitleScreen; +import net.minecraft.client.gui.screen.ingame.CreativeInventoryScreen; +import net.minecraft.client.gui.screen.ingame.HandledScreen; import net.minecraft.client.gui.widget.ButtonWidget; import net.minecraft.client.option.KeyBinding; import net.minecraft.entity.Entity; @@ -200,24 +204,24 @@ public void onInitializeClient() { }, TitleScreen.class, GameMenuScreen.class); ScreenEvents.AFTER_INIT.register((client, screen, scaledWidth, scaledHeight) -> { -// if (screen instanceof HandledScreen handled) -// ScreenKeyboardEvents.allowKeyPress(screen).register((screen1, key, scancode, modifiers) -> { -// if (!INSPECT_KEY.matchesKey(key, scancode)) return true; -// -// double mouseX = client.mouse.getX() -// * (double)client.getWindow().getScaledWidth() / (double)client.getWindow().getWidth(); -// double mouseY = client.mouse.getY() -// * (double)client.getWindow().getScaledHeight() / (double)client.getWindow().getHeight(); -// var slot = ((HandledScreenAccessor) handled).callGetSlotAt(mouseX, mouseY); -// -// if (slot == null) return true; -// if (slot instanceof CreativeInventoryScreen.LockableSlot) return true; -// if (slot.getStack().isEmpty()) return true; -// -// client.setScreen(new StackNbtDataScreen(handled, slot)); -// -// return false; -// }); + if (screen instanceof HandledScreen handled) + ScreenKeyboardEvents.allowKeyPress(screen).register((screen1, key, scancode, modifiers) -> { + if (!INSPECT_KEY.matchesKey(key, scancode)) return true; + + double mouseX = client.mouse.getX() + * (double)client.getWindow().getScaledWidth() / (double)client.getWindow().getWidth(); + double mouseY = client.mouse.getY() + * (double)client.getWindow().getScaledHeight() / (double)client.getWindow().getHeight(); + var slot = ((HandledScreenAccessor) handled).callGetSlotAt(mouseX, mouseY); + + if (slot == null) return true; + if (slot instanceof CreativeInventoryScreen.LockableSlot) return true; + if (slot.getStack().isEmpty()) return true; + + client.setScreen(new StackComponentDataScreen(handled, slot)); + + return false; + }); ScreenKeyboardEvents.allowKeyPress(screen).register((screen1, key, scancode, modifiers) -> { if (!Screen.hasShiftDown()) return true; diff --git a/src/main/java/io/wispforest/gadget/client/nbt/StackComponentDataScreen.java b/src/main/java/io/wispforest/gadget/client/nbt/StackComponentDataScreen.java new file mode 100644 index 0000000..f8b1916 --- /dev/null +++ b/src/main/java/io/wispforest/gadget/client/nbt/StackComponentDataScreen.java @@ -0,0 +1,81 @@ +package io.wispforest.gadget.client.nbt; + +import io.wispforest.owo.ui.base.BaseOwoScreen; +import io.wispforest.owo.ui.component.Components; +import io.wispforest.owo.ui.container.Containers; +import io.wispforest.owo.ui.container.FlowLayout; +import io.wispforest.owo.ui.container.ScrollContainer; +import io.wispforest.owo.ui.core.*; +import net.minecraft.client.gui.screen.ingame.HandledScreen; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NbtCompound; +import net.minecraft.nbt.NbtElement; +import net.minecraft.nbt.NbtOps; +import net.minecraft.registry.Registries; +import net.minecraft.screen.slot.Slot; +import net.minecraft.text.Text; +import net.minecraft.util.Formatting; +import org.jetbrains.annotations.NotNull; + +public class StackComponentDataScreen extends BaseOwoScreen { + private final ItemStack stack; + private final HandledScreen parent; + + public StackComponentDataScreen(HandledScreen parent, Slot slot) { + this.stack = slot.getStack(); + this.parent = parent; + } + + @Override + protected @NotNull OwoUIAdapter createAdapter() { + return OwoUIAdapter.create(this, Containers::verticalFlow); + } + + @Override + protected void build(FlowLayout rootComponent) { + rootComponent + .horizontalAlignment(HorizontalAlignment.CENTER) + .verticalAlignment(VerticalAlignment.CENTER) + .surface(Surface.VANILLA_TRANSLUCENT); + + + FlowLayout main = Containers.verticalFlow(Sizing.fill(100), Sizing.content()); + + ScrollContainer scroll = Containers.verticalScroll(Sizing.fill(95), Sizing.fill(100), main) + .scrollbar(ScrollContainer.Scrollbar.flat(Color.ofArgb(0xA0FFFFFF))); + + rootComponent.child(scroll.child(main)); + + main + .padding(Insets.of(15)); + + for (var component : stack.getComponents()) { + FlowLayout full = Containers.verticalFlow(Sizing.content(), Sizing.content()); + FlowLayout row = Containers.horizontalFlow(Sizing.content(), Sizing.content()); + + full.child(row); + main.child(full); + + row.child(Components.label(Text.literal(Registries.DATA_COMPONENT_TYPE.getId(component.type()).toString()) + .append(Text.literal(" = ") + .formatted(Formatting.GRAY)))); + + NbtElement tag = component.encode(client.world.getRegistryManager().getOps(NbtOps.INSTANCE)).getOrThrow(); + + NbtCompound compound; + if (tag instanceof NbtCompound c) { + compound = c; + } else { + compound = new NbtCompound(); + compound.put("", tag); + } + + full.child(new NbtDataIsland(compound, null)); + } + } + + @Override + public void close() { + client.setScreen(parent); + } +} diff --git a/src/main/java/io/wispforest/gadget/client/nbt/StackNbtDataScreen.java b/src/main/java/io/wispforest/gadget/client/nbt/StackNbtDataScreen.java deleted file mode 100644 index 02b105e..0000000 --- a/src/main/java/io/wispforest/gadget/client/nbt/StackNbtDataScreen.java +++ /dev/null @@ -1,106 +0,0 @@ -package io.wispforest.gadget.client.nbt; - -// todo: fix this lmao - -//public class StackNbtDataScreen extends BaseOwoScreen { -// private final NbtDataIsland island; -// private final HandledScreen parent; -// -// public StackNbtDataScreen(HandledScreen parent, Slot slot) { -// var stack = slot.getStack(); -// Consumer reloader = null; -// -// if (ServerData.canReplaceStacks()) { -// reloader = newNbt -> { -// stack.setNbt(newNbt); -// -// if (parent instanceof CreativeInventoryScreen) { -// // Let it handle it. -// return; -// } -// -// GadgetNetworking.CHANNEL.clientHandle().send(new ReplaceStackC2SPacket(slot.id, stack)); -// }; -// } -// -// NbtCompound tag = stack.getNbt(); -// -// if (tag == null) tag = new NbtCompound(); -// -// this.parent = parent; -// this.island = new NbtDataIsland(tag, reloader); -// } -// -// @Override -// protected @NotNull OwoUIAdapter createAdapter() { -// return OwoUIAdapter.create(this, Containers::verticalFlow); -// } -// -// @Override -// protected void build(FlowLayout rootComponent) { -// rootComponent -// .horizontalAlignment(HorizontalAlignment.CENTER) -// .verticalAlignment(VerticalAlignment.CENTER) -// .surface(Surface.VANILLA_TRANSLUCENT); -// -// -// FlowLayout main = Containers.verticalFlow(Sizing.fill(100), Sizing.content()); -// -// ScrollContainer scroll = Containers.verticalScroll(Sizing.fill(95), Sizing.fill(100), main) -// .scrollbar(ScrollContainer.Scrollbar.flat(Color.ofArgb(0xA0FFFFFF))); -// -// rootComponent.child(scroll.child(main)); -// -// main -// .padding(Insets.of(15)); -// -// main.child(island); -// -// FlowLayout sidebar = Containers.verticalFlow(Sizing.content(), Sizing.content()); -// -// if (island.reloader != null) { -// var addButton = Containers.verticalFlow(Sizing.fixed(16), Sizing.fixed(16)) -// .child(Components.label(Text.literal("+")) -// .verticalTextAlignment(VerticalAlignment.CENTER) -// .horizontalTextAlignment(HorizontalAlignment.CENTER) -// .positioning(Positioning.absolute(5, 4)) -// .cursorStyle(CursorStyle.HAND) -// ); -// -// addButton -// .cursorStyle(CursorStyle.HAND); -// -// addButton.mouseEnter().subscribe( -// () -> addButton.surface(Surface.flat(0x80ffffff))); -// -// addButton.mouseLeave().subscribe( -// () -> addButton.surface(Surface.BLANK)); -// -// addButton.mouseDown().subscribe((mouseX, mouseY, button) -> { -// if (button != GLFW.GLFW_MOUSE_BUTTON_LEFT) return false; -// -// UISounds.playInteractionSound(); -// -// island.typeSelector( -// (int) (addButton.x() + mouseX), -// (int) (addButton.y() + mouseY), -// type -> island.child(new KeyAdderWidget(island, NbtPath.EMPTY, type, unused -> true))); -// -// return true; -// }); -// -// sidebar.child(addButton); -// } -// -// sidebar -// .positioning(Positioning.absolute(0, 0)) -// .padding(Insets.of(5)); -// -// rootComponent.child(sidebar); -// } -// -// @Override -// public void close() { -// client.setScreen(parent); -// } -//}