Skip to content

Commit

Permalink
Added abstractwidget support
Browse files Browse the repository at this point in the history
-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.
  • Loading branch information
ferriarnus committed Mar 28, 2022
1 parent 1db331b commit 2d408ef
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

import com.mojang.blaze3d.vertex.PoseStack;

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;
Expand Down Expand Up @@ -33,6 +37,8 @@ public interface IComponentRenderContext {
void setHoverTooltipComponents(List<Component> tooltip);

void registerButton(Button button, int pageNum, Runnable onClick);

public void addAbstractWidget(AbstractWidget drawableElement, int pageNum);

ResourceLocation getBookTexture();

Expand Down
10 changes: 10 additions & 0 deletions Common/src/main/java/vazkii/patchouli/api/ICustomComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <br>
Expand Down Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import com.mojang.blaze3d.vertex.PoseStack;

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;
Expand Down Expand Up @@ -285,9 +289,20 @@ public void registerButton(Button button, int pageNum, Runnable onClick) {
customButtons.put(button, onClick);
addRenderableWidget(button);
}

@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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<IVariable> lookup) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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<IVariable> 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);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "Custom Widget Components",
"category": "patchouli:templates",
"icon": "minecraft:written_book",
"pages": [
{
"type": "patchouli:custom_widget_component"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"components": [
{
"type": "patchouli:custom",
"class": "vazkii.patchouli.client.book.template.test.EditBoxComponentTest",
"x": 10,
"y": 40
}
]
}

0 comments on commit 2d408ef

Please sign in to comment.