Jump to content

[1.16.2] Text written on screen is vertical instead of horizontal


Recommended Posts

Posted

Hello folks,

 

I want to make some HUD components (some text on the screen) for my mod. My code so far looks like this:

@Mod.EventBusSubscriber(modid = ElementalCombat.MOD_ID, bus = Bus.FORGE)
public class DisplayDefenseData {
	
	@SubscribeEvent
	public static void onRenderGameOverlay(RenderGameOverlayEvent.Text event)
	{
		Minecraft mc = Minecraft.getInstance();
		if(mc.player != null) {
			DefenseData defData = ElementalCombatAPI.getDefenseData(mc.player);
			HashMap<String, Integer> styleMap = defData.getStyleFactor();
			HashMap<String, Integer> elementMap = defData.getElementFactor();
			
			double scale = Configuration.scale();
			//RenderSystem.scaled(scale, scale, scale);
			styleMap.forEach((key, factor) -> {
				mc.fontRenderer.func_238418_a_(new StringTextComponent(DefenseDataHelper.toPercentageString(key, factor)), Configuration.posX(), Configuration.posY(), 0x000000, Integer.MAX_VALUE);
			});

			elementMap.forEach((key, factor) -> {
				mc.fontRenderer.func_238418_a_(new StringTextComponent(DefenseDataHelper.toPercentageString(key, factor)), Configuration.posX(), Configuration.posY(), 0x000000, Integer.MAX_VALUE);
			});
		}
	}

}

The RenderSystem.scaled(...) was from my testing, because I also want to be able to scale the text.

This code does actually display the information, sadly the text is only vertical and overlaps. I know, that the overlapping part can be fixed by adding a value to the x- and y-position (or does '\n' in the StringTextComponent result in a new line?). My main problem is the vertically written text. Anyone know, what I can do to write horizontal?

Posted
31 minutes ago, Tavi007 said:

My main problem is the vertically written text. Anyone know, what I can do to write horizontal?

Well that would make sense, you are using a font renderer that writes vertically. You can just use FontRenderer#func_243246_a or FontRenderer#func_243248_b without the drop shadow.

Posted

Thanks for your reply. Can you please elaborate, what the different input parameters from function func_243246_a do? For example I have no idea, what I'm supposed to do with the MatrixStack (or what it even does).

Posted
1 minute ago, Tavi007 said:

For example I have no idea, what I'm supposed to do with the MatrixStack (or what it even does)

There should be an instance provided by the event. It's just the rotations, translations, and scales used by the game to specify where to render your object.

 

The other is an ITextComponent which you should know and then followed by the x and y values.

Posted

Thank you :)

got the text displaying correctly now, only the overlapping needs to be fixed. I tested "\n" and it didn't work. Do StringTextComponent support anything similar for creating a new line? Computing new coords for each line will work, but I would like to know, if there are other options, that I'm not aware of.

Posted (edited)

I ended up with computing the position every time. Mostly because I don't want to read the obfuscated mess, that FontRenderer currently is. 😅

Here is my working code, that displays the text starting from (posX, posY).

package Tavi007.ElementalCombat.events;

import java.util.HashMap;

import com.mojang.blaze3d.matrix.MatrixStack;

import Tavi007.ElementalCombat.Configuration;
import Tavi007.ElementalCombat.ElementalCombat;
import Tavi007.ElementalCombat.ElementalCombatAPI;
import Tavi007.ElementalCombat.capabilities.defense.DefenseData;
import Tavi007.ElementalCombat.util.DefenseDataHelper;
import net.minecraft.client.Minecraft;
import net.minecraft.util.text.StringTextComponent;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber.Bus;

@Mod.EventBusSubscriber(modid = ElementalCombat.MOD_ID, bus = Bus.FORGE)
public class DisplayDefenseData {
	
	@SubscribeEvent
	public static void onRenderGameOverlay(RenderGameOverlayEvent.Text event)
	{
		Minecraft mc = Minecraft.getInstance();
		if(mc.player != null) {
			DefenseData defData = ElementalCombatAPI.getDefenseData(mc.player);
			HashMap<String, Integer> styleMap = defData.getStyleFactor();
			HashMap<String, Integer> elementMap = defData.getElementFactor();
			
			MatrixStack matrixStack = event.getMatrixStack();
			float scale = (float) Configuration.scale();
			matrixStack.scale(scale, scale, scale);
			
			float posX = Configuration.posX();
			float posY = Configuration.posY();
			float textHeight = mc.fontRenderer.FONT_HEIGHT;

			displayMap(styleMap, matrixStack, posX, posY);
			displayMap(elementMap, matrixStack, posX, posY + styleMap.size()*textHeight);
		}
	}
	
	private static void displayMap(HashMap<String, Integer> map, MatrixStack matrixStack, float posX, float posY) {
		Minecraft mc = Minecraft.getInstance();

		float textHeight = mc.fontRenderer.FONT_HEIGHT;
		final int[] i = {0};
		map.forEach ( (key, factor) -> {
			mc.fontRenderer.func_243248_b(matrixStack, new StringTextComponent(DefenseDataHelper.toPercentageString(key, factor)), posX, posY + i[0]*textHeight, Integer.MAX_VALUE);
			i[0] += 1;
		});
	}
}

If you have any idea, how I could improve my code, please tell me.

 

I'm quite happy with the result so far, tho I do wish for some background similar to tooltips for my hud. Do you have an idea, where I can read more about this topic?

Edited by Tavi007
Posted
1 hour ago, Tavi007 said:

Do you have an idea, where I can read more about this topic?

Probably doesn't exist. Most information can either be retrieved from looking through the source or asking someone who've might have done it. Reading more about a particular topic most likely won't be well known or even present unless someone is copying someone else's explanation of a system.

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.