Jump to content

How to have a scrollable list of options on a screen? [1.19.2] [SOLVED]


RInventor7

Recommended Posts

Hi! I have a custom block entity, which has a screen. I have managed to get the EditBox and a CheckBox working. Now I want to add a scrollable list of strings where the player can choose between options and finally select one, which would then open another screen according to the selection made by the player.  How to achieve this long list with a  scrollbar that would open another screen after selecting? What GUI component should I use for that? And are there any examples available somewhere? Thanks in advance. 

Edited by RInventor7
Link to comment
Share on other sites

ObjectSelectionList. e.g. CreateBuffetWorldScreen.BiomeList

Edited by warjort

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

Thanks, that did the trick. Everything works fine, but it renders the dirt background for some reason. I want to render my own texture and it actually does render below the dirt background. Is there any way to stop the dirt background from rendering?

 

Here's my code:

package com.rinventor....;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Nullable;

import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import com.rinventor.transportmod.MyMod;
import com.rinventor.transportmod.core.init.ModNetwork;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiComponent;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.components.ObjectSelectionList;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

public class CScreen8 extends AbstractContainerScreen<CMenu8> {
	private final Player entity;
	private final Component data;
	private CScreen8.LinesList list;
	String line;
	List<String> lines;

	public CScreen8(CMenu8 container, Inventory inventory, Component text) {
		super(container, inventory, text);
		this.entity = container.entity;
		this.imageWidth = 320;
		this.imageHeight = 240;
		this.data = text;
		this.line = "";
		this.lines = new ArrayList<String>(List.of("A", "B", "C"));
	}

	private static final ResourceLocation texture = new ResourceLocation(MyMod.MOD_ID + ":textures/cmenu.png");

	@Override
	public void render(PoseStack ms, int mouseX, int mouseY, float partialTicks) {
		this.renderBackground(ms);
		super.render(ms, mouseX, mouseY, partialTicks);
		this.renderTooltip(ms, mouseX, mouseY);
		this.list.render(ms, mouseX, mouseY, partialTicks);
	}

	@Override
	protected void renderBg(PoseStack ms, float partialTicks, int gx, int gy) {
		RenderSystem.setShaderColor(1, 1, 1, 1);
		RenderSystem.setShaderTexture(0, texture);
		GuiComponent.blit(ms, this.leftPos, this.topPos, 0, 0, imageWidth, 198, imageWidth, imageHeight);
	}

	@Override
	public boolean keyPressed(int key, int b, int c) {
		if (key == 256) {
			this.minecraft.player.closeContainer();
			return true;
		}
		return super.keyPressed(key, b, c);
	}

	@Override
	protected void renderLabels(PoseStack ps, int mouseX, int mouseY) {
		String id = "menu1.transport";
		String id = data.getString().trim();
		this.font.draw(ps, Component.translatable(id).getString(), 7, 7, -16711792);
	}

	@SuppressWarnings("resource")
	@Override
	public void onClose() {
		super.onClose();
		Minecraft.getInstance().keyboardHandler.setSendRepeatsToGui(false);
	}

	@Override
	public void init() {
		super.init();
		this.minecraft.keyboardHandler.setSendRepeatsToGui(true);

		this.list = new CScreen8.LinesList();
		this.addWidget(this.list);

		this.addRenderableWidget(new Button(leftPos + imageWidth - 24, topPos + 4, 20, 20, Component.literal("<-"), e -> {
			ModNetwork.INSTANCE.sendToServer(new CScreenButtonMessage(7, ""));
			CScreenButtonMessage.handleButtonAction(entity, 7, "");
		}));
	}

	@OnlyIn(Dist.CLIENT)
	class LinesList extends ObjectSelectionList<CScreen8.LinesList.Entry> {
		LinesList() {
			super(CScreen8.this.minecraft, CScreen8.this.width-150, CScreen8.this.height-150, 50, CScreen8.this.height - 37, 16);
			for (String l : CScreen8.this.lines) {
				this.addEntry(new CScreen8.LinesList.Entry(l));
			}
		}

		protected boolean isFocused() {
			return CScreen8.this.getFocused() == this;
		}

		public void setSelected(@Nullable CScreen8.LinesList.Entry entry) {
			super.setSelected(entry);
			if (entry != null) {
				CScreen8.this.line = entry.line;
			}
			// AFTER SELECTED ->
//			ModNetwork.INSTANCE.sendToServer(new CScreenButtonMessage(0, "test"));
//			CScreenButtonMessage.handleButtonAction(entity, 0, "test");
		}

		@OnlyIn(Dist.CLIENT)
		class Entry extends ObjectSelectionList.Entry<CScreen8.LinesList.Entry> {
			final String line;

			public Entry(String entry) {
				this.line = entry;
			}

			public Component getNarration() {
				return Component.translatable("narrator.select", this.line);
			}

			public void render(PoseStack p_95802_, int p_95803_, int p_95804_, int p_95805_, int p_95806_, int p_95807_, int p_95808_, int p_95809_,
					boolean p_95810_, float p_95811_) {
				GuiComponent.drawString(p_95802_, CScreen8.this.font, this.line, p_95805_ + 5, p_95804_ + 2, 16777215);
			}

			public boolean mouseClicked(double p_95798_, double p_95799_, int p_95800_) {
				if (p_95800_ == 0) {
					LinesList.this.setSelected(this);
					return true;
				}
				else {
					return false;
				}
			}
		}
	}

}

 

Link to comment
Share on other sites

AbstractSelectionList.setRenderBackground()

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

  • RInventor7 changed the title to How to have a scrollable list of options on a screen? [1.19.2] [SOLVED]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.