Jump to content

[1.10.2] Custom gui button not working


NEG2013

Recommended Posts

I'm trying to draw tabs, I have it set up so that when I click a certain area a boolean becomes true and then I'm based on the state of the boolean trying to draw a rectangle so it appears as if the the tab has been select

 

 

method for drawing the tabs:

 

public void drawSelectedTab(){
	int guiX = (width - guiWidth) / 2;
	int guiY = (height - guiHeight) / 2;





	int tabOU;

	if(tabOfficial){
		tabOU = 237;
	}
	else{
		tabOU = 233;
	}

	//tab Official
	drawTexturedModalRect(guiX + 203, guiY + 3,              tabOU, 15,             3, 22);


	int tabCU;
	if(tabCommunity){
		tabCU = 237;
	}
	else{
		tabCU = 233;
	}
	//tab Community
	drawTexturedModalRect(guiX + 203, guiY + 27,             tabCU, 15,             3, 22);

	int tabFU;
	if(tabFavorites){
		tabFU = 237;
	}
	else{
		tabFU = 233;
	}
	//tab Favorites
	drawTexturedModalRect(guiX + 203, guiY + 51,             tabFU, 15,             3, 22);	



}

 

and heres the part that determines which tab has been selected

@Override
protected void mouseClicked(int x, int y, int mouseButton) throws IOException {

	int guiX = (width - guiWidth) / 2;
	int guiY = (height - guiHeight) / 2;


	//tab Official
	if(trueZone(guiX + 205, guiY + 3, 19, 22, x, y)){

		tabOfficial = true;
		tabCommunity = false;
		tabFavorites = false;

		if(tabOfficial){
			System.out.println("Clicked tab Official");
		}
	}//tab Official

	//tab Community
	if(trueZone(guiX + 205, guiY + 27, 19, 22, x, y)){

		tabOfficial = false;
		tabCommunity = true;
		tabFavorites = false;

		if(tabCommunity){
			System.out.println("Clicked tab Community");
		}			
	}//tab Community	

	//tab Favorites
	if(trueZone(guiX + 205, guiY + 51, 19, 22, x, y)){

		tabOfficial = false;
		tabCommunity = false;
		tabFavorites = true;

		if(tabFavorites){
			System.out.println("Clicked tab Favorites");
		}
	}//tab Favorites

 

Heres the entire class:

package com.neg2013.main;

import java.io.IOException;
import java.util.List;

import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.GL11;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.util.ResourceLocation;

public class GuiServerPicker extends GuiScreen{

int buttonWidth = 194;
int buttonHeight = 21;

int tabWidth = 19;
int tabHeight = 22;

int guiWidth = 225;
int guiHeight = 154;

boolean clicked1 = false;
boolean clicked2 = false;
boolean clicked3 = false;
boolean clicked4 = false;
boolean clicked5 = false;
boolean clicked6 = false;

boolean tabOfficial = true;
boolean tabCommunity = false;
boolean tabFavorites = false;

int id = 0;
GuiButton menuButton;

//int serverV = 207;
int ServersOnPage = 5;

@Override
public void drawScreen(int mouseX, int mouseY, float partialTicks) {
	int guiX = (width - guiWidth) / 2;
	int guiY = (height - guiHeight) / 2;

	mc.renderEngine.bindTexture(new ResourceLocation(Reference.MODID, "textures/gui/gui.png"));
	GL11.glColor4f(1, 1, 1, 1);
	drawTexturedModalRect(guiX, guiY, 0, 0, guiWidth, guiHeight);

	serverButtons("NEG2013", "Is", "The", "God", "Of", "Sexiness", ServersOnPage);
	drawSelectedTab();











	super.drawScreen(mouseX, mouseY, partialTicks);

}//drawScreen

	//ADD THE PLAYER COUNT	

public void serverButtons(String name1, String name2, String name3, String name4, String name5, String name6, int ServersOnPage){
	int guiX = (width - guiWidth) / 2;
	int guiY = (height - guiHeight) / 2;

	int color = 0X000000;



	if(ServersOnPage < 1){
		return;
	}
	//server 1
		int i = 0;
		int serverV;

		if(clicked1){
			serverV = 229;
		}
		else{
			serverV = 207;
		}

		fontRendererObj.drawString( name1, guiX + 8 , guiY + 10 + (25 * i), color);
		mc.renderEngine.bindTexture(new ResourceLocation(Reference.MODID, "textures/gui/gui.png"));
		GL11.glColor4f(1, 1, 1, 1);	
		drawTexturedModalRect(guiX + 4 , guiY + 4 + (25 * i), 1, serverV, buttonWidth, buttonHeight);
		fontRendererObj.drawString( name1, guiX + 4 + 4 , guiY + 4 + 6 + (25 * i), color);
			//server 1 end


	if(ServersOnPage < 2){
		return;
	}
	//server 2
		int i1 = 1;
		int server1V;

		if(clicked2){
			server1V = 229;
		}
		else{
			server1V = 207;
		}

		fontRendererObj.drawString( name2, guiX + 8 , guiY + 10 + (25 * i1), color);
		mc.renderEngine.bindTexture(new ResourceLocation(Reference.MODID, "textures/gui/gui.png"));
		GL11.glColor4f(1, 1, 1, 1);	
		drawTexturedModalRect(guiX + 4 , guiY + 4 + (25 * i1), 1, server1V, buttonWidth, buttonHeight);
		fontRendererObj.drawString( name2, guiX + 4 + 4 , guiY + 4 + 6 + (25 * i1), color);
			//server 3 end

	if(ServersOnPage < 3){
		return;
	}
	//server 3
		int i2 = 2;
		int server2V;

		if(clicked3){
			server2V = 229;
		}
		else{
			server2V = 207;
		}

		fontRendererObj.drawString( name3, guiX + 8 , guiY + 10 + (25 * i2), color);
		mc.renderEngine.bindTexture(new ResourceLocation(Reference.MODID, "textures/gui/gui.png"));
		GL11.glColor4f(1, 1, 1, 1);	
		drawTexturedModalRect(guiX + 4 , guiY + 4 + (25 * i2), 1, server2V, buttonWidth, buttonHeight);
		fontRendererObj.drawString( name3, guiX + 4 + 4 , guiY + 4 + 6 + (25 * i2), color);
			//server 3 end

	if(ServersOnPage < 4){
		return;
	}
	//server 4
		int i3 = 3;
		int server3V;

		if(clicked4){
			server3V = 229;
		}
		else{
			server3V = 207;
		}

		fontRendererObj.drawString( name4, guiX + 8 , guiY + 10 + (25 * i3), color);
		mc.renderEngine.bindTexture(new ResourceLocation(Reference.MODID, "textures/gui/gui.png"));
		GL11.glColor4f(1, 1, 1, 1);	
		drawTexturedModalRect(guiX + 4 , guiY + 4 + (25 * i3), 1, server3V, buttonWidth, buttonHeight);
		fontRendererObj.drawString( name4, guiX + 4 + 4 , guiY + 4 + 6 + (25 * i3), color);
			//server 4 end

	if(ServersOnPage < 5){
		return;
	}
	//server 5
		int i4 = 4;
		int server4V;

		if(clicked5){
			server4V = 229;
		}
		else{
			server4V = 207;
		}

		fontRendererObj.drawString( name5, guiX + 8 , guiY + 10 + (25 * i4), color);
		mc.renderEngine.bindTexture(new ResourceLocation(Reference.MODID, "textures/gui/gui.png"));
		GL11.glColor4f(1, 1, 1, 1);	
		drawTexturedModalRect(guiX + 4 , guiY + 4 + (25 * i4), 1, server4V, buttonWidth, buttonHeight);
		fontRendererObj.drawString( name5, guiX + 4 + 4 , guiY + 4 + 6 + (25 * i4), color);
			//server 5 end

	if(ServersOnPage < 6){
		return;
	}
	//server 6
		int i5 = 5;
		int server5V;

		if(clicked6){
			server5V = 229;
		}
		else{
			server5V = 207;
		}

		fontRendererObj.drawString( name6, guiX + 8 , guiY + 10 + (25 * i5), color);
		mc.renderEngine.bindTexture(new ResourceLocation(Reference.MODID, "textures/gui/gui.png"));
		GL11.glColor4f(1, 1, 1, 1);	
		drawTexturedModalRect(guiX + 4 , guiY + 4 + (25 * i5), 1, server5V, buttonWidth, buttonHeight);
		fontRendererObj.drawString( name6, guiX + 4 + 4 , guiY + 4 + 6 + (25 * i5), color);
			//server 6 end
			if(ServersOnPage > 6){
				return;
			}


}//serverButtons

/*if(tabOfficial){
				//tab1
				drawTexturedModalRect(guiX + 203, guiY + 3,              237, 15,             3, 22);

				//tab2
				drawTexturedModalRect(guiX + 203, guiY + 27,             233, 15,             3, 22);

				//tab3
				drawTexturedModalRect(guiX + 203, guiY + 51,             233, 15,             3, 22);

			}//tabO

			if(tabCommunity){
				//tab1
				drawTexturedModalRect(guiX + 203, guiY + 3,              233, 15,             3, 22);

				//tab2
				drawTexturedModalRect(guiX + 203, guiY + 27,             237, 15,            3, 22);

				//tab3
				drawTexturedModalRect(guiX + 203, guiY + 51,             233, 15,             3, 22);

			}//tabC

			if(tabFavorites){
				//tab1
				drawTexturedModalRect(guiX + 203, guiY + 3,              233, 15,            3, 22);

				//tab2
				drawTexturedModalRect(guiX + 203, guiY + 27,             233, 15,             3, 22);

				//tab3
				drawTexturedModalRect(guiX + 203, guiY + 51,             237, 15,             3, 22);

			}//tabF*/

public void connectToServers(){

	//server 1

	if(clicked1){

	}

	//server 2
	if(clicked1){

	}

	//server 3
	if(clicked1){

	}

	//server 4
	if(clicked1){

	}//server 5

	if(clicked1){

	}

	//server 6
	if(clicked1){

	}

}//connectToServers



public void drawSelectedTab(){
	int guiX = (width - guiWidth) / 2;
	int guiY = (height - guiHeight) / 2;





	int tabOU;

	if(tabOfficial){
		tabOU = 237;
	}
	else{
		tabOU = 233;
	}

	//tab Official
	drawTexturedModalRect(guiX + 203, guiY + 3,              tabOU, 15,             3, 22);


	int tabCU;
	if(tabCommunity){
		tabCU = 237;
	}
	else{
		tabCU = 233;
	}
	//tab Community
	drawTexturedModalRect(guiX + 203, guiY + 27,             tabCU, 15,             3, 22);

	int tabFU;
	if(tabFavorites){
		tabFU = 237;
	}
	else{
		tabFU = 233;
	}
	//tab Favorites
	drawTexturedModalRect(guiX + 203, guiY + 51,             tabFU, 15,             3, 22);	



}
	public boolean trueZone(int OffsX, int OffsY, int Width, int Height, int MouseX, int MouseY){
	if(OffsX <= MouseX && MouseX <= OffsX + Width && OffsY <= MouseY && MouseY <= OffsY + Height ){
		return true;
	}
	else{
		return false;	
	}


}
@Override
protected void mouseClicked(int x, int y, int mouseButton) throws IOException {

	int guiX = (width - guiWidth) / 2;
	int guiY = (height - guiHeight) / 2;


	//tab Official
	if(trueZone(guiX + 205, guiY + 3, 19, 22, x, y)){

		tabOfficial = true;
		tabCommunity = false;
		tabFavorites = false;

		if(tabOfficial){
			System.out.println("Clicked tab Official");
		}
	}//tab Official

	//tab Community
	if(trueZone(guiX + 205, guiY + 27, 19, 22, x, y)){

		tabOfficial = false;
		tabCommunity = true;
		tabFavorites = false;

		if(tabCommunity){
			System.out.println("Clicked tab Community");
		}			
	}//tab Community	

	//tab Favorites
	if(trueZone(guiX + 205, guiY + 51, 19, 22, x, y)){

		tabOfficial = false;
		tabCommunity = false;
		tabFavorites = true;

		if(tabFavorites){
			System.out.println("Clicked tab Favorites");
		}
	}//tab Favorites




	if(ServersOnPage < 1){
		return;
	}
	//server1
	if(guiX + 0 <= x && x <= guiX + 0 + buttonWidth && guiY + 0 <= y && y <= guiY + 0 + buttonHeight ){
		System.out.println("Clicked Server 1");
		clicked1 = true;
		clicked2 = false;
		clicked3 = false;
		clicked4 = false;
		clicked5 = false;
		clicked6 = false;
	}
	if(ServersOnPage < 2){
		return;
	}
	//server2
	int i1 = 1;
	if(guiX + 0 <= x && x <= guiX + 0 + buttonWidth && guiY + 0  + (25 * i1) <= y && y <= guiY + 0  + (25 * i1) + buttonHeight ){
		System.out.println("Clicked Server 2");
		clicked1 = false;
		clicked2 = true;
		clicked3 = false;
		clicked4 = false;
		clicked5 = false;
		clicked6 = false;
	}
	if(ServersOnPage < 3){
		return;
	}
	//server3
	int i2 = 2;
	if(guiX + 0 <= x && x <= guiX + 0 + buttonWidth && guiY + 0  + (25 * i2) <= y && y <= guiY + 0  + (25 * i2) + buttonHeight ){
		System.out.println("Clicked Server 3");
		clicked1 = false;
		clicked2 = false;
		clicked3 = true;
		clicked4 = false;
		clicked5 = false;
		clicked6 = false;
	}
	if(ServersOnPage < 4){
		return;
	}
	//server4
	int i3 = 3;
	if(guiX + 0 <= x && x <= guiX + 0 + buttonWidth && guiY + 0  + (25 * i3)<= y && y <= guiY + 0 + (25 * i3) + buttonHeight ){
		System.out.println("Clicked Server 4");
		clicked1 = false;
		clicked2 = false;
		clicked3 = false;
		clicked4 = true;
		clicked5 = false;
		clicked6 = false;
	}
	if(ServersOnPage < 5){
		return;
	}
	//server5
	int i4 = 4;
	if(guiX + 0 <= x && x <= guiX + 0 + buttonWidth && guiY + 0 + (25 * i4) <= y && y <= guiY + 0 + (25 * i4) + buttonHeight ){
		System.out.println("Clicked Server 5");
		clicked1 = false;
		clicked2 = false;
		clicked3 = false;
		clicked4 = false;
		clicked5 = true;
		clicked6 = false;
	}
	if(ServersOnPage < 6){
		return;
	}
	//server6
	int i5 = 5;
	if(guiX + 0 <= x && x <= guiX + 0 + buttonWidth && guiY + 0 + (25 * i5) <= y && y <= guiY + 0 + (25 * i5) + buttonHeight ){
		System.out.println("Clicked Server 6");
		clicked1 = false;
		clicked2 = false;
		clicked3 = false;
		clicked4 = false;
		clicked5 = false;
		clicked6 = true;
	}


	super.mouseClicked(x, y, mouseButton);}//mouseclicked
@Override
protected void actionPerformed(GuiButton button) throws IOException {

	switch(button.id){

	case 666:

		Minecraft.getMinecraft().displayGuiScreen(new GuiServerPicker());

	break;

	}

	super.actionPerformed(button);}	
@Override
public void initGui() {
	buttonList.clear();
	buttonList.add(menuButton = new GuiButton(666, 0, 0, "NEG2013"));


	super.initGui();
}	
@Override
protected void keyTyped(char c, int key) throws IOException {

	switch(key){
	case Keyboard.KEY_E:
		Minecraft.getMinecraft().displayGuiScreen(null);
	break;




	}//switch

	super.keyTyped(c, key);
}//keyTyped	
@Override
public boolean doesGuiPauseGame() {
	return false;
}//doesGuiPauseGame
}//class


 

 

 

If you want to see anything else please just ask.

 

 

Link to comment
Share on other sites

Also instead of Manually drawing the Button why don't you just create a class that extends GuiButton. Then use the vanilla button stuff in gui?

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

Don't use the default button I was saying use the default button "system". Make a class that extends GuiButton(which is where all the button rendering code is). Override the drawButton method and put your rendering code in there. And there is a method in GuiButton that checks if it is hovering over or not.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

This

super.drawScreen(mouseX, mouseY, partialTicks);

Should probably be called at the start aswell. Might not be required, unless you already did that.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

Interesting. I didn't know that. Did you want it to change size when you clicked it? Have you tried increasing the size it increases when you click it just to make sure that it is visible or not?

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

Well if it worked before there are inconsistencies. And I would just switch over to the Vanilla GuiButton system it is much cleaner code. But why do you want to change the size when you click the button if the screen is going to change it won't render. Maybe you wanted it to render when it was being hovered over?

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

I couldn't tell you why and yeah it has been a while since I have used drawTextureModelRect I forgot the parameters. Once you have switch over it should be much easier to pinpoint the problem.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.


×
×
  • Create New...

Important Information

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