Skip to content

Commit

Permalink
improved text box widget more
Browse files Browse the repository at this point in the history
  • Loading branch information
Trinsdar committed Dec 11, 2023
1 parent 084c7d5 commit 9ac1844
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ public void sendPacket(AbstractGuiEventPacket pkt) {
public void update() {
List<SyncHolder> toSync = new ObjectArrayList<>();
for (SyncHolder sync : this.syncData) {
if (sync.direction == SyncDirection.CLIENT_TO_SERVER) continue;
Object value = sync.source.get();
if (!sync.equality.apply(value, sync.current)) {
sync.current = value;
Expand Down
30 changes: 14 additions & 16 deletions common/src/main/java/muramasa/antimatter/gui/Widget.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.*;
import com.mojang.math.Matrix4f;
import lombok.Getter;
import lombok.Setter;
import muramasa.antimatter.gui.widget.WidgetSupplier;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
Expand All @@ -27,13 +29,17 @@ public abstract class Widget implements IGuiElement {
public final GuiInstance gui;
public final boolean isRemote;
protected IGuiElement parent;
@Getter
@Setter
private Component message = TextComponent.EMPTY;
public final int id;

@Getter
protected boolean enabled = true;
protected boolean shouldRender = true;
protected boolean isClicking = false;

@Setter
private int depth;
private int x, y, w, h = 0;
private int realX, realY;
Expand All @@ -46,14 +52,6 @@ protected Widget(@NotNull final GuiInstance gui, @Nullable final IGuiElement par
updateSize();
}

public void setMessage(Component message) {
this.message = message;
}

public Component getMessage() {
return message;
}

@Environment(EnvType.CLIENT)
protected static void fillGradient(Matrix4f matrix, BufferBuilder builder, int x1, int y1, int x2, int y2, int z, int colorA, int colorB) {
float f = (float) (colorA >> 24 & 255) / 255.0F;
Expand All @@ -79,10 +77,6 @@ public int depth() {
return depth;
}

public void setDepth(int depth) {
this.depth = depth;
}

public void setEnabled(boolean enabled) {
if (enabled != this.enabled) {
this.enabled = enabled;
Expand Down Expand Up @@ -156,14 +150,14 @@ public boolean keyReleased(int keyCode, int scanCode, int modifiers, double mous
return false;
}

public boolean charTyped(char codePoint, int modifiers, double mouseX, double mouseY){
return false;
}


@Environment(EnvType.CLIENT)
public abstract void render(PoseStack matrixStack, double mouseX, double mouseY, float partialTicks);

public boolean isEnabled() {
return enabled;
}

public boolean isVisible() {
return shouldRender;
}
Expand All @@ -187,6 +181,10 @@ public boolean mouseReleased(double mouseX, double mouseY, int button) {
return true;
}

public boolean mouseScrolled(double mouseX, double mouseY, double delta){
return false;
}

@Environment(EnvType.CLIENT)
public void mouseOver(PoseStack stack, double mouseX, double mouseY, float partialTicks) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import muramasa.antimatter.gui.Widget;
import muramasa.antimatter.gui.container.IAntimatterContainer;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.components.events.GuiEventListener;
import net.minecraft.client.gui.narration.NarratableEntry;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.client.gui.screens.inventory.MenuAccess;
import net.minecraft.network.chat.Component;
Expand All @@ -33,6 +35,10 @@ protected void init() {
menu.source().rescale(this);
}

public <U extends GuiEventListener & NarratableEntry> U addMCWidget(U guiEventListener) {
return addWidget(guiEventListener);
}

@Override
public void containerTick() {
super.containerTick();
Expand Down Expand Up @@ -76,6 +82,15 @@ public boolean mouseReleased(double mouseX, double mouseY, int button) {
return super.mouseReleased(mouseX, mouseY, button);
}

@Override
public boolean mouseScrolled(double mouseX, double mouseY, double delta) {
for (Widget wid : menu.source().getWidgets(mouseX, mouseY)) {
if (!wid.isEnabled() || wid.depth() < 0) continue;
if (wid.mouseScrolled(mouseX, mouseY, delta)) return true;
}
return super.mouseScrolled(mouseX, mouseY, delta);
}

@Override
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
double x = mouseX();
Expand All @@ -87,6 +102,28 @@ public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
return super.keyPressed(keyCode, scanCode, modifiers);
}

@Override
public boolean keyReleased(int keyCode, int scanCode, int modifiers) {
double x = mouseX();
double y = mouseY();
for (Widget wid : menu.source().getWidgets(x, y)) {
if (!wid.isEnabled()) continue;
if (wid.keyReleased(keyCode, scanCode, modifiers, x, y)) return true;
}
return super.keyReleased(keyCode, scanCode, modifiers);
}

@Override
public boolean charTyped(char codePoint, int modifiers) {
double x = mouseX();
double y = mouseY();
for (Widget wid : menu.source().getWidgets(x, y)) {
if (!wid.isEnabled()) continue;
if (wid.charTyped(codePoint, modifiers, x, y)) return true;
}
return super.charTyped(codePoint, modifiers);
}

@Override
public void render(PoseStack matrixStack, int mouseX, int mouseY, float partialTicks) {
super.render(matrixStack, mouseX, mouseY, partialTicks);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,97 @@

import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;

public class TextBoxWidget extends Widget {

EditBox textBox;
BiConsumer<IGuiHandler, String> consumer;
Function<IGuiHandler, String> function;

protected TextBoxWidget(@NotNull GuiInstance gui, @Nullable IGuiElement parent, BiConsumer<IGuiHandler, String> consumer) {
protected TextBoxWidget(@NotNull GuiInstance gui, @Nullable IGuiElement parent, BiConsumer<IGuiHandler, String> consumer, Function<IGuiHandler, String> function) {
super(gui, parent);
this.consumer = consumer;
this.function = function;
}

public static WidgetSupplier build(BiConsumer<IGuiHandler, String> consumer) {
return builder((i, p) -> new TextBoxWidget(i, p, consumer));
public static WidgetSupplier build(BiConsumer<IGuiHandler, String> consumer, Function<IGuiHandler, String> function) {
return builder((i, p) -> new TextBoxWidget(i, p, consumer, function));
}

@Override
public void render(PoseStack matrixStack, double mouseX, double mouseY, float partialTicks) {
textBox.render(matrixStack, (int) mouseX, (int) mouseY, partialTicks);
}

@Override
public void onClick(double mouseX, double mouseY, int button) {
super.onClick(mouseX, mouseY, button);
if (isInside(mouseX, mouseY)){
if (button == 0 && !textBox.isFocused()){
textBox.setFocus(true);
} else if (button == 1 && textBox.isFocused()){
textBox.setFocus(false);
}
}
}

@Override
public boolean keyPressed(int keyCode, int scanCode, int modifiers, double mouseX, double mouseY) {
if (textBox.isFocused()){
return textBox.keyPressed(keyCode, scanCode, modifiers);
}
return super.keyPressed(keyCode, scanCode, modifiers, mouseX, mouseY);
}

@Override
public boolean keyReleased(int keyCode, int scanCode, int modifiers, double mouseX, double mouseY) {
if (textBox.isFocused()){
return textBox.keyReleased(keyCode, scanCode, modifiers);
}
return super.keyReleased(keyCode, scanCode, modifiers, mouseX, mouseY);
}

@Override
public boolean charTyped(char codePoint, int modifiers, double mouseX, double mouseY) {
if (textBox.isFocused()) return textBox.charTyped(codePoint, modifiers);
return super.charTyped(codePoint, modifiers, mouseX, mouseY);
}

@Override
public void updateSize() {
super.updateSize();
if (gui.isRemote && textBox != null){
textBox.x = realX();
textBox.y = realY();
}
}

@Override
public void init() {
super.init();

this.gui.syncString(() -> function.apply(gui.handler), this::setValue, ICanSyncData.SyncDirection.SERVER_TO_CLIENT);
this.gui.syncString(() -> textBox.getValue(), s -> consumer.accept(gui.handler, s), ICanSyncData.SyncDirection.CLIENT_TO_SERVER);
if (gui.isRemote){
initTextBox();
} else {
gui.update();
}
}

boolean initialized = false;

private void setValue(String value){
if (!initialized){
textBox.setValue(value);
initialized = true;
}
}

@Environment(EnvType.CLIENT)
protected void initTextBox(){
textBox = new EditBox(Minecraft.getInstance().font, this.getX(), this.getY(), this.getW(), this.getH(), Utils.literal(""));
textBox = new EditBox(Minecraft.getInstance().font, this.realX(), this.realY(), this.getW(), this.getH(), Utils.literal(""));
this.textBox.setMaxLength(32500);
}
}

0 comments on commit 9ac1844

Please sign in to comment.