Skip to content

Commit

Permalink
Add a JSON text section to test screen
Browse files Browse the repository at this point in the history
  • Loading branch information
SmylerMC committed Jan 29, 2024
1 parent e5ad679 commit ae2d02f
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions forge/src/main/java/fr/thesmyler/smylibgui/screen/TestScreen.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fr.thesmyler.smylibgui.screen;

import com.google.gson.JsonParseException;
import net.smyler.smylib.gui.containers.FlexibleWidgetContainer;
import net.smyler.smylib.gui.containers.WidgetContainer;
import net.smyler.smylib.Animation;
Expand All @@ -24,6 +25,8 @@
import net.minecraftforge.client.event.GuiScreenEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

import static net.smyler.smylib.Color.RED;
import static net.smyler.smylib.Color.WHITE;
import static net.smyler.smylib.SmyLib.getGameClient;

public class TestScreen extends Screen {
Expand Down Expand Up @@ -72,7 +75,8 @@ public void initGui() {
WidgetContainer buttonScreen = new FlexibleWidgetContainer(20, 50, 1, this.width - 40, this.height - 70);
WidgetContainer sliderScreen = new FlexibleWidgetContainer(20, 50, 1, this.width - 40, this.height - 70);
WidgetContainer menuScreen = new FlexibleWidgetContainer(20, 50, 1, this.width - 40, this.height - 70);
this.subScreens = new WidgetContainer[] { textScreen, buttonScreen, sliderScreen, menuScreen};
WidgetContainer jsonTextScreen = new FlexibleWidgetContainer(20, 50, 1, this.width - 40, this.height - 70);
this.subScreens = new WidgetContainer[] { textScreen, buttonScreen, sliderScreen, menuScreen, jsonTextScreen};
for(WidgetContainer container: this.subScreens) container.setDoScissor(false);

TextWidget title = new TextWidget(this.width / 2f, 20, 10, new TextComponentString("SmyLibGui demo test screen"), TextAlignment.CENTER, getGameClient().defaultFont());
Expand Down Expand Up @@ -159,10 +163,36 @@ public void initGui() {
nested.addEntry("menu");
rcm.addSeparator();
rcm.addEntry("Animation", animationMenu);
rcm.useAsRightClick(); // Calling this tells the menu to open whenever it's parent screen is right clicked
rcm.useAsRightClick(); // Calling this tells the menu to open whenever it's parent screen is right-clicked
menuScreen.addWidget(new TextWidget(menuScreen.getWidth() / 2, menuScreen.getHeight() / 2, 1, new TextComponentString("Please right click anywhere"), TextAlignment.CENTER, getGameClient().defaultFont()));
menuScreen.addWidget(rcm);

// ==== JSON text parsing screen ==== //
final TextFieldWidget inputField = new TextFieldWidget(0, 0, 0, jsonTextScreen.getWidth(), getGameClient().defaultFont());
final TextWidget text = new TextWidget(
jsonTextScreen.getWidth() / 2,
(jsonTextScreen.getHeight() - inputField.getHeight()) / 2,
0,
new TextComponentString(""),
TextAlignment.CENTER, getGameClient().defaultFont()
);
jsonTextScreen.addWidget(inputField);
jsonTextScreen.addWidget(text);

jsonTextScreen.scheduleBeforeEachUpdate(() -> {
try {
ITextComponent component = ITextComponent.Serializer.jsonToComponent(inputField.getText());
if (component == null) {
throw new JsonParseException("");
}
text.setText(component);
text.setAnchorY((jsonTextScreen.getHeight() - inputField.getHeight() - text.getHeight()) / 2 + inputField.getHeight());
inputField.setFocusedTextColor(WHITE);
} catch (JsonParseException e) {
inputField.setFocusedTextColor(RED);
}
});


// ==== Getting everything ready and setting up scheduled tasks === //

Expand Down

0 comments on commit ae2d02f

Please sign in to comment.