Jump to content

Draw 2D shape on HUD when key pressed


Roboguy99

Recommended Posts

To draw on the HUD you use RenderGameOverlayEvent (careful, choose one of the subevents and an event type, otherwise you draw multiple times per frame).

To draw shapes you can use standard OpenGL or the "Tessellator" (doesn't really have anything to do with tessellating, it is just an very thin abstraction over OpenGL calls that are then sent to teh GPU in a more compact format).

 

For the key: Just make a KeyBinding instance and register it (ClientRegistry.registerKeyBinding). It will then track the state of that key automatically and you can just check keybind.isKeyDown to check if the key is down wherever you want.

 

I'm going to need more help with this, my RenderGameOverlayEvent appears to be doing nothing (perhaps I needed to specify something extra?). Here is the code:

 

@SubscribeEvent(priority = EventPriority.NORMAL)
    public void renderOverlay(RenderGameOverlayEvent event)
    {
	System.out.println("fghgh");
	int x = Minecraft.getMinecraft().displayWidth/2;
	int y = Minecraft.getMinecraft().displayHeight/2;
	int r = 200;

	GL11.glBegin( GL11.GL_TRIANGLE_FAN );
        GL11.glVertex2f(x, y);
        for( int n = 0; n <= 18; n++ )
        {
            float t = 2 * 3.14152f * (float)n / (float)18;
            GL11.glVertex2d(x + Math.sin(t) * r, y + Math.cos(t) * r);
        }
        
        	GL11.glRectd(x, y, x+100, y+200);
        GL11.glEnd();
    }

I have no idea what I'm doing.

Link to comment
Share on other sites

Firstly:

[b[careful, choose one of the subevents and an event type, otherwise you draw multiple times per frame[/b]

 

Did you register your event handler?

 

I have this line in my main class:

FMLCommonHandler.instance().bus().register(new EventsHandler());

 

Is that correct? I've tried loading it at pre and post init stages with nothing happening.

I have no idea what I'm doing.

Link to comment
Share on other sites

MinecraftForge.EVENT_BUS.register(new ForgeEvents());

 

Also - it should be in client proxy. (since it is rendering, you should separate common and client events)

 

There are Forge and FML events - 2 different buses.

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

Link to comment
Share on other sites

MinecraftForge.EVENT_BUS.register(new ForgeEvents());

 

Also - it should be in client proxy. (since it is rendering, you should separate common and client events)

 

There are Forge and FML events - 2 different buses.

 

It's working now, thanks.

I have no idea what I'm doing.

Link to comment
Share on other sites

EventName.SubEventName

 

Eg

EventChunkData.Load

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Pick one, it won't matter in 90% of cases.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Pick one, it won't matter in 90% of cases.

 

Ok at this point I'm just going to show you what I've done so far and hopefully you'll be able to help me better. Pressing the keybinding while holding the item brings up a 100% opaque circle in the middle of the screen, which occasionally changes from black to very dark brown when I change direction - it appears to be ignoring the Color4f line altogether.

 

EDIT: I have changed the class to extend Gui and checked to render after the experience bar. I now get the desired semi-transparent circle, but if I turn around it sometimes disappears.

 

Here is the updated code:

 

public class EventsHandler extends Gui
{
private static final float PI = 3.14152f;
private static final int RADIUS = 90;
private static final int SECTORS = 200;

@SideOnly(Side.CLIENT)
@SubscribeEvent(priority = EventPriority.NORMAL)
public void onRenderExperienceBar(RenderGameOverlayEvent event)
{
	if(event.isCancelable() || event.type != ElementType.EXPERIENCE)
    {      
      return;
    }

	Item holding;
	if (Minecraft.getMinecraft().thePlayer.getHeldItem() != null)
	{
		holding = Minecraft.getMinecraft().thePlayer.getHeldItem().getItem();
		if (holding instanceof ItemBag /*&& HotbarBag.bagHUD.getIsKeyPressed()*/)
		{
			BagInventory inventory = new BagInventory(Minecraft.getMinecraft().thePlayer.getHeldItem());

			int items = 0;
			for(int i = 0; i < inventory.getSizeInventory(); i++)
			{
				if (inventory.getStackInSlot(i) != null) items++;
			}

			// Place circle slightly above centre of screen
			int x = event.resolution.getScaledWidth() / 2;
			int y = event.resolution.getScaledHeight() / 2 - 15;

			if (items != 0)
			{
				float multiplier = this.SECTORS / items;
				// System.out.println(multiplier);

				//GL11.glPushMatrix();
				GL11.glColor4f(0.6F, 0.6F, 0.6F, 0.3F);
				GL11.glBegin(GL11.GL_TRIANGLE_FAN);
				GL11.glVertex2f(x, y);
				for(int i = 0; i < items; i++)
				{
					for(float n = 0 + (i * multiplier); n <= (this.SECTORS / items) + (i * multiplier); n += 1)
					{
						float t = 2 * PI * (float) n / (float) this.SECTORS;
						GL11.glVertex2d(x + Math.sin(t) * this.RADIUS, y + Math.cos(t) * this.RADIUS);
					}
				}
				GL11.glEnd();

				ItemStack itemStack = inventory.getStackInSlot(1);
				//EntityItem entity = new EntityItem(Minecraft.getMinecraft().theWorld, 0, 0, 0, itemStack);
				//GL11.glPopMatrix();
			}
		}
	}
}
}

 

 

What I want is a semi-transparent grey circle. I assume it's just a few minor OpenGL things I'm not sure about causing this.

I have no idea what I'm doing.

Link to comment
Share on other sites

Important thing - rendering phase (added) - always pick one. Same thing appears in TickEvents.

Also - never too many Push/Pops :D

 

public class EventsHandler extends Gui
{
private static final float PI = 3.14152f;
private static final int RADIUS = 90;
private static final int SECTORS = 200;

@SideOnly(Side.CLIENT)
@SubscribeEvent(priority = EventPriority.NORMAL)
public void onRenderExperienceBar(RenderGameOverlayEvent.Post event)
{
	Item holding;
	if (Minecraft.getMinecraft().thePlayer.getHeldItem() != null)
	{
		if (event.type == ElementType.ALL)
		{
		holding = Minecraft.getMinecraft().thePlayer.getHeldItem().getItem();
		if (holding instanceof ItemBag && HotbarBag.bagHUD.getIsKeyPressed())
		{
			BagInventory inventory = new BagInventory(Minecraft.getMinecraft().thePlayer.getHeldItem());

			int items = 0;
			for(int i = 0; i < inventory.getSizeInventory(); i++)
			{
				if (inventory.getStackInSlot(i) != null) items++;
			}

			int x = event.resolution.getScaledWidth() / 2;
			int y = event.resolution.getScaledHeight() / 2 - 15;

			if (items != 0)
			{
				float multiplier = this.SECTORS / items;

				GL11.glPushMatrix();
				GL11.glColor4f(0.6F, 0.6F, 0.6F, 0.3F);
				GL11.glBegin(GL11.GL_TRIANGLE_FAN);
				GL11.glVertex2f(x, y);
				for(int i = 0; i < items; i++)
				{
					for(float n = 0 + (i * multiplier); n <= (this.SECTORS / items) + (i * multiplier); n += 1)
					{
						float t = 2 * PI * (float) n / (float) this.SECTORS;
						GL11.glVertex2d(x + Math.sin(t) * this.RADIUS, y + Math.cos(t) * this.RADIUS);
					}
				}
				GL11.glEnd();

				ItemStack itemStack = inventory.getStackInSlot(1);
				EntityItem entity = new EntityItem(Minecraft.getMinecraft().theWorld, 0, 0, 0, itemStack);
				GL11.glPopMatrix();
			}
		}
		}
	}
}
}

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

Link to comment
Share on other sites

Important thing - rendering phase (added) - always pick one. Same thing appears in TickEvents.

Also - never too many Push/Pops :D

 

public class EventsHandler extends Gui
{
private static final float PI = 3.14152f;
private static final int RADIUS = 90;
private static final int SECTORS = 200;

@SideOnly(Side.CLIENT)
@SubscribeEvent(priority = EventPriority.NORMAL)
public void onRenderExperienceBar(RenderGameOverlayEvent.Post event)
{
	Item holding;
	if (Minecraft.getMinecraft().thePlayer.getHeldItem() != null)
	{
		if (event.type == ElementType.ALL)
		{
		holding = Minecraft.getMinecraft().thePlayer.getHeldItem().getItem();
		if (holding instanceof ItemBag && HotbarBag.bagHUD.getIsKeyPressed())
		{
			BagInventory inventory = new BagInventory(Minecraft.getMinecraft().thePlayer.getHeldItem());

			int items = 0;
			for(int i = 0; i < inventory.getSizeInventory(); i++)
			{
				if (inventory.getStackInSlot(i) != null) items++;
			}

			int x = event.resolution.getScaledWidth() / 2;
			int y = event.resolution.getScaledHeight() / 2 - 15;

			if (items != 0)
			{
				float multiplier = this.SECTORS / items;

				GL11.glPushMatrix();
				GL11.glColor4f(0.6F, 0.6F, 0.6F, 0.3F);
				GL11.glBegin(GL11.GL_TRIANGLE_FAN);
				GL11.glVertex2f(x, y);
				for(int i = 0; i < items; i++)
				{
					for(float n = 0 + (i * multiplier); n <= (this.SECTORS / items) + (i * multiplier); n += 1)
					{
						float t = 2 * PI * (float) n / (float) this.SECTORS;
						GL11.glVertex2d(x + Math.sin(t) * this.RADIUS, y + Math.cos(t) * this.RADIUS);
					}
				}
				GL11.glEnd();

				ItemStack itemStack = inventory.getStackInSlot(1);
				EntityItem entity = new EntityItem(Minecraft.getMinecraft().theWorld, 0, 0, 0, itemStack);
				GL11.glPopMatrix();
			}
		}
		}
	}
}
}

 

For some odd reason it still stops/starts drawing as you change orientation. For some reason it seems to only work if you look south or east.

 

EDIT: It's not working at all now. I have changed nothing and the code is still being executed, but nothing is being drawn.

I have no idea what I'm doing.

Link to comment
Share on other sites

Bump. This is still broken and I'm not sure why. The code is being run, but nothing appears on the screen.

 

Here is the code, as far as I can see the same as when it was mostly working.

public class EventsHandler extends Gui
{
private static final int RADIUS = 90;
private static final int SECTORS = 200;

@SideOnly(Side.CLIENT)
@SubscribeEvent(priority = EventPriority.NORMAL)
public void onRenderExperienceBar(RenderGameOverlayEvent.Post event)
{
	Item holding;
	if (Minecraft.getMinecraft().thePlayer.getHeldItem() != null)
	{
		if (event.type == ElementType.ALL)
		{
			holding = Minecraft.getMinecraft().thePlayer.getHeldItem().getItem();
			if (holding instanceof ItemBag && HotbarBag.bagHUD.getIsKeyPressed())
			{
				BagInventory inventory = new BagInventory(Minecraft.getMinecraft().thePlayer.getHeldItem());

				int items = 0;
				for(int i = 0; i < inventory.getSizeInventory(); i++)
				{
					if (inventory.getStackInSlot(i) != null) items++;
				}

				int x = event.resolution.getScaledWidth() / 2;
				int y = event.resolution.getScaledHeight() / 2 - 15;

				if (items != 0)
				{
					float multiplier = this.SECTORS / items;

					GL11.glPushMatrix();
					GL11.glColor4f(0.6F, 0.6F, 0.6F, 0.3F);
					GL11.glBegin(GL11.GL_TRIANGLE_FAN);
					GL11.glVertex2f(x, y);
					for(int i = 0; i < items; i++)
					{
						for(float n = 0 + (i * multiplier); n <= (this.SECTORS / items) + (i * multiplier); n += 1)
						{
							double t = 2 * Math.PI * (float) n / (float) this.SECTORS;
							GL11.glVertex2d(x + Math.sin(t) * this.RADIUS, y + Math.cos(t) * this.RADIUS);
						}
					}
					GL11.glEnd();

					// ItemStack itemStack = inventory.getStackInSlot(1);
					// EntityItem entity = new
					// EntityItem(Minecraft.getMinecraft().theWorld, 0, 0,
					// 0, itemStack);
					GL11.glPopMatrix();

					float mouseX = Mouse.getX();
					float mouseY = Mouse.getY();
				}
			}
		}
	}
}
}

I have no idea what I'm doing.

Link to comment
Share on other sites

Since you are drawing without texture, you probably have to disable GL_TEXTURE_2D and re-enable it afterwards using glDisable / glEnable with GL_TEXTURE_2D as parameter.

 

That's fixed it, thanks!

 

We'll ignore the part where after doing what you said, I spent ages trying to fix the keybinding not working because I forgot I rebound the key.

I have no idea what I'm doing.

Link to comment
Share on other sites

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.