Jump to content

[1.7.10] How would you Keep a HUD GUI Element in place upon MC window resize.


Recommended Posts

Posted

Hi,

 

I have a custom HUD stamina bar and i want to make it stay in place. By stay in place I mean like the Health bar stays in place no matter what size the screen is, no matter what your computers resolution the health bar stays in place relative to the minecraft window. How would you achieve this. I know how to position it using the width and height but I want this position to adjust itself when the user resizes the minecraft window when not in full screen and when going to full screen also so that it looks as if the HUD is in the same place relative to everything else.

 

 

 

@SideOnly(Side.CLIENT)
public class GUIStaminaBar extends Gui
{
private Minecraft mc;

private static final ResourceLocation texture = new ResourceLocation("staminamod:textures/gui/BasicLockBackground.png");

public GUIStaminaBar(Minecraft mc) {
	super();
	this.mc = mc;
	}
@SubscribeEvent(priority=EventPriority.NORMAL)
public void onRenderFoodBar(RenderGameOverlayEvent.Pre event) {
	if (event.type == ElementType.FOOD) {
		event.setCanceled(true);
		return;
	}
	if (event.type != ElementType.FOOD) {
			return;
	}
}

@SubscribeEvent(priority=EventPriority.NORMAL)
public void onRenderExperienceBar(RenderGameOverlayEvent.Post event) {
	if (event.type != ElementType.EXPERIENCE) {
		return;
	}

	BattlePlayerProperties staminaBar = BattlePlayerProperties.get(this.mc.thePlayer);
	if (staminaBar == null || staminaBar.getMaxStamina() == 0) {
		return;
	}

	int width = 256;
	int height = 128;
	this.mc.getTextureManager().bindTexture(texture);

	// Add this block of code before you draw the section of your texture containing transparency
	GL11.glEnable(GL11.GL_BLEND);
	GL11.glDisable(GL11.GL_DEPTH_TEST);
	GL11.glDepthMask(false);
	GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
	GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
	GL11.glDisable(GL11.GL_ALPHA_TEST);
	// Here we draw the background bar which contains a transparent section; note the new size
	drawTexturedModalRect(width, height, 0, 0, 56, 9);
	// You can keep drawing without changing anything
	int manabarwidth = (int)(((float) staminaBar.getCurrentStamina() / staminaBar.getMaxStamina()) * 49);
	drawTexturedModalRect(width + 3, height + 3, 0, 9, manabarwidth, 3);
	String staminaTally = "Mana " + staminaBar.getCurrentStamina() + "/" + staminaBar.getMaxStamina();
	height += -10;
	this.mc.fontRenderer.drawString(staminaTally, width + 1, height, 0);
	this.mc.fontRenderer.drawString(staminaTally, width - 1, height, 0);
	this.mc.fontRenderer.drawString(staminaTally, width, height + 1, 0);
	this.mc.fontRenderer.drawString(staminaTally, width, height - 1, 0);
	this.mc.fontRenderer.drawString(staminaTally, width, height, 9529416);

	GL11.glDisable(GL11.GL_BLEND);
	GL11.glEnable(GL11.GL_DEPTH_TEST);
	GL11.glDepthMask(true);
}
}

 

 

Posted

Pure math mate. Example: to make bar apperar in place of health you will have to go with:

Width: width/2 - 50

Height: height - 50

 

You will mainly want to make width and height dependent to scaled resolution. Scaled res can be drawed straight fron guiScreen or Minecraft.class

 

1.7.10 is no longer supported by forge, you are on your own.

Posted

To make this easier for you when coding, and re usable. Put the function in a Utils class this way you don't have to write down the calculation all the time.

 

Functions:

// The offset is an integer that is applied after the center calculation to offset in any direction.
// The return will be the center position of the object relative to the screen size.
// I also suggest making these static so they can be accessed without initialization.
int verticalCenterPosition(int elementWidth, int offset);
int horizontalCenterPosition(int elementHeight, int offset);

I require Java, both the coffee and the code :)

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.