From fc32a0fb22df3ed7a1fa77f183bc08bed3d46950 Mon Sep 17 00:00:00 2001
From: ferriarnus <61201275+ferriarnus@users.noreply.github.com>
Date: Mon, 28 Mar 2022 10:50:46 +0200
Subject: [PATCH] Added abstractwidget support
-added a method to add abstractwidgets
-added a override method for key presses, so they can be used in components (for example allow the editbox to work).
-added a test case component with an editbox.
---
.../api/IComponentRenderContext.java | 5 ++
.../patchouli/api/ICustomComponent.java | 10 ++++
.../patchouli/client/book/BookPage.java | 4 ++
.../client/book/gui/GuiBookEntry.java | 14 +++++
.../client/book/page/PageTemplate.java | 5 ++
.../client/book/template/BookTemplate.java | 11 ++++
.../book/template/TemplateComponent.java | 4 ++
.../template/component/ComponentCustom.java | 5 ++
.../template/test/EditBoxComponentTest.java | 55 +++++++++++++++++++
.../templates/custom_widget_components.json | 10 ++++
.../templates/custom_widget_component.json | 10 ++++
11 files changed, 133 insertions(+)
create mode 100644 Common/src/main/java/vazkii/patchouli/client/book/template/test/EditBoxComponentTest.java
create mode 100644 Common/src/main/resources/assets/patchouli/patchouli_books/comprehensive_test_book/en_us/entries/templates/custom_widget_components.json
create mode 100644 Common/src/main/resources/assets/patchouli/patchouli_books/comprehensive_test_book/en_us/templates/custom_widget_component.json
diff --git a/Common/src/main/java/vazkii/patchouli/api/IComponentRenderContext.java b/Common/src/main/java/vazkii/patchouli/api/IComponentRenderContext.java
index 9f8f34f21..7bd404966 100644
--- a/Common/src/main/java/vazkii/patchouli/api/IComponentRenderContext.java
+++ b/Common/src/main/java/vazkii/patchouli/api/IComponentRenderContext.java
@@ -4,6 +4,9 @@
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.client.gui.components.Button;
+import net.minecraft.client.gui.components.Widget;
+import net.minecraft.client.gui.components.events.GuiEventListener;
+import net.minecraft.client.gui.narration.NarratableEntry;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.Style;
@@ -35,6 +38,8 @@ public interface IComponentRenderContext {
@Deprecated(forRemoval = true) // use addWidget
void registerButton(Button button, int pageNum, Runnable onClick);
+
+ public void addAbstractWidget(AbstractWidget drawableElement, int pageNum);
void addWidget(AbstractWidget button, int pageNum);
diff --git a/Common/src/main/java/vazkii/patchouli/api/ICustomComponent.java b/Common/src/main/java/vazkii/patchouli/api/ICustomComponent.java
index 69aa491e8..557eb6264 100644
--- a/Common/src/main/java/vazkii/patchouli/api/ICustomComponent.java
+++ b/Common/src/main/java/vazkii/patchouli/api/ICustomComponent.java
@@ -2,6 +2,8 @@
import com.mojang.blaze3d.vertex.PoseStack;
+import vazkii.patchouli.client.book.BookPage;
+
/**
* An interface for API level custom components for templates.
*
@@ -41,4 +43,12 @@ default boolean mouseClicked(IComponentRenderContext context, double mouseX, dou
return false;
}
+ /**
+ * Called on key press. If it returns true, the normal key press logic will not be called. Note that the key
+ * press logic in this method is called even when the result is false.
+ */
+ default boolean overwriteKeyPressed(BookPage page, int keyCode, int scanCode, int modifiers) {
+ return false;
+ }
+
}
diff --git a/Common/src/main/java/vazkii/patchouli/client/book/BookPage.java b/Common/src/main/java/vazkii/patchouli/client/book/BookPage.java
index 95640d927..a881343fe 100644
--- a/Common/src/main/java/vazkii/patchouli/client/book/BookPage.java
+++ b/Common/src/main/java/vazkii/patchouli/client/book/BookPage.java
@@ -70,6 +70,10 @@ public void render(PoseStack ms, int mouseX, int mouseY, float pticks) {}
public boolean mouseClicked(double mouseX, double mouseY, int mouseButton) {
return false;
}
+
+ public boolean overwriteKeyPressed(int keyCode, int scanCode, int modifiers) {
+ return false;
+ }
public boolean canAdd(Book book) {
return flag == null || flag.isEmpty() || PatchouliConfig.getConfigFlag(flag);
diff --git a/Common/src/main/java/vazkii/patchouli/client/book/gui/GuiBookEntry.java b/Common/src/main/java/vazkii/patchouli/client/book/gui/GuiBookEntry.java
index fe8a9064d..e6e259794 100644
--- a/Common/src/main/java/vazkii/patchouli/client/book/gui/GuiBookEntry.java
+++ b/Common/src/main/java/vazkii/patchouli/client/book/gui/GuiBookEntry.java
@@ -5,6 +5,9 @@
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.client.gui.components.Button;
+import net.minecraft.client.gui.components.Widget;
+import net.minecraft.client.gui.components.events.GuiEventListener;
+import net.minecraft.client.gui.narration.NarratableEntry;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.Style;
@@ -283,9 +286,20 @@ public void addWidget(AbstractWidget widget, int pageNum) {
widget.y += bookTop;
addRenderableWidget(widget);
}
+
+ @Override
+ public void addAbstractWidget(AbstractWidget drawableElement, int pageNum) {
+ drawableElement.x += bookLeft + ((pageNum % 2) == 0 ? LEFT_PAGE_X : RIGHT_PAGE_X);
+ drawableElement.y += bookTop;
+
+ addRenderableWidget(drawableElement);
+ }
@Override
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
+ if ((leftPage != null && leftPage.overwriteKeyPressed(keyCode, scanCode, modifiers)) || (rightPage != null && rightPage.overwriteKeyPressed(keyCode, scanCode, modifiers))) {
+ return true;
+ }
if (Minecraft.getInstance().options.keyInventory.matches(keyCode, scanCode)) {
this.onClose();
return true;
diff --git a/Common/src/main/java/vazkii/patchouli/client/book/page/PageTemplate.java b/Common/src/main/java/vazkii/patchouli/client/book/page/PageTemplate.java
index 21b88c09d..5e64fc10f 100644
--- a/Common/src/main/java/vazkii/patchouli/client/book/page/PageTemplate.java
+++ b/Common/src/main/java/vazkii/patchouli/client/book/page/PageTemplate.java
@@ -45,4 +45,9 @@ public void render(PoseStack ms, int mouseX, int mouseY, float pticks) {
public boolean mouseClicked(double mouseX, double mouseY, int mouseButton) {
return template.mouseClicked(this, mouseX, mouseY, mouseButton);
}
+
+ @Override
+ public boolean overwriteKeyPressed(int keyCode, int scanCode, int modifiers) {
+ return template.overwriteKeyPressed(this, keyCode, scanCode, modifiers);
+ }
}
diff --git a/Common/src/main/java/vazkii/patchouli/client/book/template/BookTemplate.java b/Common/src/main/java/vazkii/patchouli/client/book/template/BookTemplate.java
index a5b417742..ffee74533 100644
--- a/Common/src/main/java/vazkii/patchouli/client/book/template/BookTemplate.java
+++ b/Common/src/main/java/vazkii/patchouli/client/book/template/BookTemplate.java
@@ -174,5 +174,16 @@ private void createProcessor() {
attemptedCreatingProcessor = true;
}
}
+
+ public boolean overwriteKeyPressed(BookPage page, int keyCode, int scanCode, int modifiers) {
+ if (compiled) {
+ for (TemplateComponent c : components) {
+ if (c.isVisible && c.overwriteKeyPressed(page, keyCode, scanCode, modifiers)) {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
}
diff --git a/Common/src/main/java/vazkii/patchouli/client/book/template/TemplateComponent.java b/Common/src/main/java/vazkii/patchouli/client/book/template/TemplateComponent.java
index bf470fc6d..0d12af554 100644
--- a/Common/src/main/java/vazkii/patchouli/client/book/template/TemplateComponent.java
+++ b/Common/src/main/java/vazkii/patchouli/client/book/template/TemplateComponent.java
@@ -86,6 +86,10 @@ public void render(PoseStack ms, BookPage page, int mouseX, int mouseY, float pt
public boolean mouseClicked(BookPage page, double mouseX, double mouseY, int mouseButton) {
return false;
}
+
+ public boolean overwriteKeyPressed(BookPage page, int keyCode, int scanCode, int modifiers) {
+ return false;
+ }
@Override
public void onVariablesAvailable(UnaryOperator lookup) {
diff --git a/Common/src/main/java/vazkii/patchouli/client/book/template/component/ComponentCustom.java b/Common/src/main/java/vazkii/patchouli/client/book/template/component/ComponentCustom.java
index ef662014d..1a696ca37 100644
--- a/Common/src/main/java/vazkii/patchouli/client/book/template/component/ComponentCustom.java
+++ b/Common/src/main/java/vazkii/patchouli/client/book/template/component/ComponentCustom.java
@@ -51,5 +51,10 @@ public void onDisplayed(BookPage page, GuiBookEntry parent, int left, int top) {
public boolean mouseClicked(BookPage page, double mouseX, double mouseY, int mouseButton) {
return callbacks.mouseClicked(page.parent, mouseX, mouseY, mouseButton);
}
+
+ @Override
+ public boolean overwriteKeyPressed(BookPage page, int keyCode, int scanCode, int modifiers) {
+ return callbacks.overwriteKeyPressed(page, keyCode, scanCode, modifiers);
+ }
}
diff --git a/Common/src/main/java/vazkii/patchouli/client/book/template/test/EditBoxComponentTest.java b/Common/src/main/java/vazkii/patchouli/client/book/template/test/EditBoxComponentTest.java
new file mode 100644
index 000000000..6e869bf2c
--- /dev/null
+++ b/Common/src/main/java/vazkii/patchouli/client/book/template/test/EditBoxComponentTest.java
@@ -0,0 +1,55 @@
+package vazkii.patchouli.client.book.template.test;
+
+import java.util.function.UnaryOperator;
+
+import com.mojang.blaze3d.vertex.PoseStack;
+
+import net.minecraft.client.Minecraft;
+import net.minecraft.client.gui.components.EditBox;
+import net.minecraft.network.chat.TextComponent;
+import net.minecraft.world.entity.vehicle.Minecart;
+import vazkii.patchouli.api.IComponentRenderContext;
+import vazkii.patchouli.api.ICustomComponent;
+import vazkii.patchouli.api.IVariable;
+import vazkii.patchouli.client.book.BookPage;
+
+public class EditBoxComponentTest implements ICustomComponent{
+ private transient int x, y, page;
+ private Object editbox;
+
+ @Override
+ public void onVariablesAvailable(UnaryOperator lookup) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void build(int componentX, int componentY, int pageNum) {
+ x = componentX;
+ y = componentY;
+ page = pageNum;
+ this.editbox = new EditBox(Minecraft.getInstance().font, x, y, 120, 20, new TextComponent(""));
+ }
+
+ @Override
+ public void render(PoseStack ms, IComponentRenderContext context, float pticks, int mouseX, int mouseY) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void onDisplayed(IComponentRenderContext context) {
+ this.editbox = new EditBox(Minecraft.getInstance().font, x, y, 100, 20, new TextComponent(""));
+ context.addAbstractWidget((EditBox) editbox, page);
+ }
+
+ @Override
+ public boolean overwriteKeyPressed(BookPage page, int keyCode, int scanCode, int modifiers) {
+ if (((EditBox)editbox).isFocused()) {
+ ((EditBox)editbox).keyPressed(keyCode, scanCode, modifiers);
+ return true;
+ }
+ return ICustomComponent.super.overwriteKeyPressed(page, keyCode, scanCode, modifiers);
+ }
+
+}
diff --git a/Common/src/main/resources/assets/patchouli/patchouli_books/comprehensive_test_book/en_us/entries/templates/custom_widget_components.json b/Common/src/main/resources/assets/patchouli/patchouli_books/comprehensive_test_book/en_us/entries/templates/custom_widget_components.json
new file mode 100644
index 000000000..570b4b91a
--- /dev/null
+++ b/Common/src/main/resources/assets/patchouli/patchouli_books/comprehensive_test_book/en_us/entries/templates/custom_widget_components.json
@@ -0,0 +1,10 @@
+{
+ "name": "Custom Widget Components",
+ "category": "patchouli:templates",
+ "icon": "minecraft:written_book",
+ "pages": [
+ {
+ "type": "patchouli:custom_widget_component"
+ }
+ ]
+}
diff --git a/Common/src/main/resources/assets/patchouli/patchouli_books/comprehensive_test_book/en_us/templates/custom_widget_component.json b/Common/src/main/resources/assets/patchouli/patchouli_books/comprehensive_test_book/en_us/templates/custom_widget_component.json
new file mode 100644
index 000000000..0011020f8
--- /dev/null
+++ b/Common/src/main/resources/assets/patchouli/patchouli_books/comprehensive_test_book/en_us/templates/custom_widget_component.json
@@ -0,0 +1,10 @@
+{
+ "components": [
+ {
+ "type": "patchouli:custom",
+ "class": "vazkii.patchouli.client.book.template.test.EditBoxComponentTest",
+ "x": 10,
+ "y": 40
+ }
+ ]
+}