Jump to content

[1.7.10] Centering a gui?


Awesome_Spider

Recommended Posts

Ok, I tried it, the background disappeared:

3IbHFTt.png

:/ Did I do it wrong?

...
ScaledResolution sr = new ScaledResolution(Minecraft.getMinecraft(), Minecraft.getMinecraft().displayWidth, Minecraft.getMinecraft().displayWidth);

        int backX = sr.getScaledWidth();
        int backY = sr.getScaledHeight();

        ResourceLocation background = new ResourceLocation(SignificantAdvancements.MODID, "textures/guis/lexiconBackground.png");
        Minecraft.getMinecraft().renderEngine.bindTexture(background);

        this.drawTexturedModalRect(backX, backY, 0, 0, guiWidth, guiHeight);
...

Link to comment
Share on other sites

I don't know if you're still having problems, but the

 

public void drawTexturedModalRect(int x, int y, int textureX, int textureY, int width, int height)

 

method you are using assumes a texture size of 256x256. If your texture is a unique size, drawModalRectWithCustomSizedTexture(...) may provide better results.

 

Link to comment
Share on other sites

Umm... Isn't that what this does:

...
int backX = sr.getScaledWidth();
int backY = sr.getScaledHeight();

this.drawTexturedModalRect((backX - guiWidth) / 2, (backY - guiHeight) / 2, 0, 0, guiWidth, guiHeight);

Yes, it is the same, but why add stuff that's already there :o?

My gui height is right. Could it be because the display has to be an uneven ratio? I mean like the display being 1280 x 720. So why would I use / 2 on both the x and y values? Shouldn't the y value have something like / 4? If I'm not making sense just tell me. :/

Well, devding by 2 gives the middle value...

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Link to comment
Share on other sites

Did you try what yyrkoon said about using drawModalRectWithCustomSizedTexture instead. That's what I normally use and that could be the issue.

 

drawModalRectWithCustomSizedTexture((backX - guiWidth) / 2, (backY - guiHeight) / 2, 0, 0, guiWidth, guiHeight, guiWidth, guiHeight);

Link to comment
Share on other sites

As i said, to center your WHOLE gui, and without worriing to center EACH element, in gui constructor add this, and then use guiTop and guiLeft as offset depart point...:

this.guiLeft = (this.width - this.xSize) / 2;
        this.guiTop = (this.height - this.ySize) / 2;

What happens here, i may represent as:

this.guiLeft = this.width / 2 - this.xSize / 2

What this does: finds middle of screen (width/2) and substracts half of size of gui (xSize/2), in result middle of gui is middle of screen.

But using laws of maths, we can convert this formula to

this.guiLeft = (this.width - this.xSize) / 2

And all that is left to do: each element draw relative to guiLeft and guiTop. Example of drawing background:

this.drawTexturedModalRect(guiLeft, guiTop, 0, 0, guiWidth, guiHeight);

Example of drawing element:

this.drawTexturedModalRect(guiLeft + 123, guiTop + 59, 0, 0, 16, 16);

 

 

PS: like that you don't need to worry about scaled resolution stuff.

BTW: if you haven't access to gui constructor, do this in gui init event.

Link to comment
Share on other sites

Did you try what yyrkoon said about using drawModalRectWithCustomSizedTexture instead. That's what I normally use and that could be the issue.

 

drawModalRectWithCustomSizedTexture((backX - guiWidth) / 2, (backY - guiHeight) / 2, 0, 0, guiWidth, guiHeight, guiWidth, guiHeight);

 

Well, that method doesn't exist for me. :/

 

I think full code is needed to resolve this issue.

 

Ok, here is my gui class:

public class GuiLexicon extends GuiScreen {
    int guiWidth = 192;
    int guiHeight = 256;

    static int GUI_ID = 30;

    @Override
    public void drawScreen(int mouseX, int mouseY, float par3) {
        super.drawScreen(mouseX, mouseY, par3);

        ScaledResolution sr = new ScaledResolution(Minecraft.getMinecraft(), Minecraft.getMinecraft().displayWidth, Minecraft.getMinecraft().displayWidth);

        int backX = sr.getScaledWidth();
        int backY = sr.getScaledHeight();

        this.drawTexturedModalRect((backX - guiWidth) / 2, (backY - guiHeight) / 2, 0, 0, guiWidth, guiHeight);

        int stringWidth = this.fontRendererObj.getStringWidth("Lexicon of Knowledge");

        ResourceLocation background = new ResourceLocation(SignificantAdvancements.MODID, "textures/guis/lexiconBackground.png");
        Minecraft.getMinecraft().renderEngine.bindTexture(background);

        this.fontRendererObj.drawString("Lexicon of Knowledge", guiWidth - stringWidth, 50, 0x00FFFF, true);
    }
}

Link to comment
Share on other sites

Appologies, My head was in 1.8. The method exists in 1.7.10, just not in a user friendly form. The method call for you would be func_146110_a. I've copied the 1.8 call below as well so you can better see what the parameters represent. The internals of the 2 methods are the same. Also these are STATIC methods.

 

For the text, you can use the same formula (backx- stringwidth) / 2. The y position you can use the same value as the y coord of the texture.

 

is it safe to assume you texture is also 192x256? If you can open your texture and pad it's width to 256 with transparency (32px on either side), the scaling won't matter and you can use the same draw rect method you've been using (less math ;) )

 

1.8

public static void drawModalRectWithCustomSizedTexture(int x, int y, float u, float v, int width, int height, float textureWidth, float textureHeight)

 

1.7.10

public static void func_146110_a(int p_146110_0_, int p_146110_1_, float p_146110_2_, float p_146110_3_, int p_146110_4_, int p_146110_5_, float p_146110_6_, float p_146110_7_)

 

Link to comment
Share on other sites

Ok, I just don't understand why I would have to use that method, my gui's texture is 256x256. It isn't a custom texture measurement. But I could try that.

 

Edit: Odd, func_146110_a  doesn't exist either. :/

Sorry for repeating, but have you tried what i said you above??? Because it MUST work...

If you're too lazy, here's modified centered code for gui:

public class GuiLexicon extends GuiScreen {
    int guiWidth = 192;
    int guiHeight = 256;
    
    int guiLeft = 0;
    int guiTop = 0;
    
    public GuiLexicon() {
    	guiLeft = (width - guiWidth) / 2;
    	guiTop = (height - guiHeight) / 2;
    }

    static int GUI_ID = 30;

    @Override
    public void drawScreen(int mouseX, int mouseY, float par3) {
        super.drawScreen(mouseX, mouseY, par3);

        Minecraft.getMinecraft().renderEngine.bindTexture(new ResourceLocation(SignificantAdvancements.MODID, "textures/guis/lexiconBackground.png"));
        this.drawTexturedModalRect(guiLeft, guiTop, 0, 0, guiWidth, guiHeight);       
        
        int stringWidth = this.fontRendererObj.getStringWidth(I18n.format("lexiconOfKnowledge.lang"));

        this.fontRendererObj.drawString(I18n.format("lexiconOfKnowledge.lang"), guiWidth - stringWidth, 50, 0x00FFFF, true);
    }
}

 

Ps: this is also multi language friendly!!!

And also, func_146110_a  is static and in Gui.class

Link to comment
Share on other sites

Your texture is the right dimension, so you can safely ignore my comments about the drawRect methods.

 

Most vanilla container textures don't use a full 256 height for the background portion though. I think you book might simply be too tall.

Link to comment
Share on other sites

Your texture is the right dimension, so you can safely ignore my comments about the drawRect methods.

 

Most vanilla container textures don't use a full 256 height for the background portion though. I think you book might simply be too tall.

Nope, it is not.

Link to comment
Share on other sites

ñaa

same trouble but in 1.8 , the diferense is i wanna keep mi icons in boton left

i could get the  sr.getScaledWidth(); to work properly

int backX = sr.getScaledWidth();

int backY = sr.getScaledHeight();

 

 

later i try to fix them calculating the position the hard way, neither i could make it work and end geting tired of it an let it for later   

 

 

this code works well only when = 2  , normal size in the menu 

            Minecraft mc;
            displayWidth=mc.displayWidth;
            displayHeight=mc.displayHeight;
            guiscale=mc.gameSettings.guiScale;
            
            switch(guiscale)
            {
            case 0: ;break;
            case 3:
            {
            	posX=16; //controls the Icons size
                posY=((displayHeight/2)-25);
                x=40;  //gui size    
            } ;break;
            
            case 2:
            {
            	posX=30; //controls the Icons size
                posY=((displayHeight/2)-60);
                x=40;  //gui size    
            } ;break;
            
            case 1:
            {
            	posX=16; //controls the Icons size
                posY=((displayHeight)-100);
                x=10;  //gui size    
            } ;break;
            
            
            }


       
        //Prepare rendering
        GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
        GL11.glDisable(GL11.GL_LIGHTING);
        
        
        
        if (municion<0)
        {
        municion=0;	
        }	
        
        
        //drawModalRectWithCustomSizedtext0ure(int x, int y,float u,float v,int width,int height,float text0ureWidth,float text0ureHeight);
        
        this.mc.renderEngine.bindTexture(new ResourceLocation(icono0));
        this.drawModalRectWithCustomSizedTexture( (posX - 5), (posY), 0.0F, 0.0F, x, x, xf, xf); //imagen al 60%
        //this.drawModalRectWithCustomSizedtext0ure(posX + 8, posY + 8, 0.0F, 0.0F, 200, 200 , 200.0F, 200.0F);
        
        this.mc.renderEngine.bindTexture(new ResourceLocation(icono1));
        this.drawModalRectWithCustomSizedTexture( (posX + x ), (posY ), 0.0F, 0.0F, (int) (x * 1.5), (x / 3), (xf * 1.5F), (xf / 3) ); //imagen al 60%
        
        this.mc.renderEngine.bindTexture(new ResourceLocation(icono2));
        this.drawModalRectWithCustomSizedTexture( (posX + x ), (posY + (x / 3) ), 0.0F, 0.0F, (int)  (((x * 1.0) / balas ) * municion), (x / 3), (xf * 1.0F), (xf / 3) ); 
        
        this.mc.renderEngine.bindTexture(new ResourceLocation(icono3));
        this.drawModalRectWithCustomSizedTexture( (posX + x ), (posY + ((x / 3) * 2) ), 0.0F, 0.0F, (int) (x * 1.5), (x / 3), (xf * 1.5F), (xf / 3) ); 
        
        
        
        this.mc.fontRendererObj.drawStringWithShadow(text0,  posX, ((posY ) + x) , 0xffcccccc);



Link to comment
Share on other sites

Hmm... Well, I don't know if this matters, but I'm extending GuiScreen. I'm not using GuiContainer or anything. Does that change anything? Surely not. :/

In gui screen you have guiTop, guiLeft, xSize, ySize predefined for you to center your gui. If you don't extend it, you can center it manualy, as i wrote above.

(Sir, if you're lazy to do it your self, look above: i posted code for your centered gui, you can use it...)

Please reply me, for that i understand that you read this post and tried it...

Link to comment
Share on other sites

Hmm... Well, I don't know if this matters, but I'm extending GuiScreen. I'm not using GuiContainer or anything. Does that change anything? Surely not. :/

In gui screen you have guiTop, guiLeft, xSize, ySize predefined for you to center your gui. If you don't extend it, you can center it manualy, as i wrote above.

(Sir, if you're lazy to do it your self, look above: i posted code for your centered gui, you can use it...)

Please reply me, for that i understand that you read this post and tried it...

 

Those fields aren't there :/ They don't exist for me.

 

Edit: I typed them and they showed up red, I looked in the GuiScreen class, not there. They don't exist. :/

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Sometimes, tweaking load order or specific configs can also help ease those conflicts.
    • I have recently tired downloading forge so i can run mods for hypixel skyblock but whenever i download forge and do it the same way as videos and tutorials forge keeps downloading as log file and doesnt download the .jar file any help will be aprecciated.
    • So I have created a Create Mod Server with my friends and i to play everyone can join play except one of my friends because when he tries to launch the game from the CurseForge Launcher it will load but never open or give him a crash report, we have tried everything from deleting CF and reinstalling it to Updating his drivers, IDK what to do if anyone can help I would be very Grateful
    • I get this error when trying to start the server for forged mods. In the code below it mentions distant horizons but no matter what mod I put in (deleting distant horizons) it still gives an error and doesn't launch Crash Report UUID: 3e91d5c7-18a7-43c2-a935-a8d28de560d1 FML: 47.3 Forge: net.minecraftforge:47.3.10[23:33:47] [main/ERROR] [minecraft/Main]: Failed to start the minecraft server net.minecraftforge.fml.LoadingFailedException: Loading errors encountered: [    Distant Horizons (distanthorizons) encountered an error during the sided_setup event phase§7java.lang.ExceptionInInitializerError: null]     at net.minecraftforge.fml.ModLoader.waitForTransition(ModLoader.java:246) ~[fmlcore-1.20.1-47.3.10.jar%23104!/:?] {}     at net.minecraftforge.fml.ModLoader.lambda$dispatchAndHandleError$20(ModLoader.java:210) ~[fmlcore-1.20.1-47.3.10.jar%23104!/:?] {}     at java.util.Optional.ifPresent(Optional.java:178) ~[?:?] {}     at net.minecraftforge.fml.ModLoader.dispatchAndHandleError(ModLoader.java:210) ~[fmlcore-1.20.1-47.3.10.jar%23104!/:?] {}     at net.minecraftforge.fml.ModLoader.lambda$loadMods$15(ModLoader.java:190) ~[fmlcore-1.20.1-47.3.10.jar%23104!/:?] {}     at java.lang.Iterable.forEach(Iterable.java:75) ~[?:?] {}     at net.minecraftforge.fml.ModLoader.loadMods(ModLoader.java:190) ~[fmlcore-1.20.1-47.3.10.jar%23104!/:?] {}     at net.minecraft.server.loading.ServerModLoader.load(ServerModLoader.java:31) ~[forge-1.20.1-47.3.10-universal.jar%23108!/:?] {re:classloading}     at net.minecraft.server.Main.main(Main.java:125) ~[server-1.20.1-20230612.114412-srg.jar%23103!/:?] {re:classloading}     at jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103) ~[?:?] {}     at java.lang.reflect.Method.invoke(Method.java:580) ~[?:?] {}     at net.minecraftforge.fml.loading.targets.CommonLaunchHandler.runTarget(CommonLaunchHandler.java:111) ~[fmlloader-1.20.1-47.3.10.jar%2369!/:?] {}     at net.minecraftforge.fml.loading.targets.CommonLaunchHandler.lambda$run$1(CommonLaunchHandler.java:103) ~[fmlloader-1.20.1-47.3.10.jar%2369!/:?] {}     at net.minecraftforge.fml.loading.targets.CommonServerLaunchHandler.lambda$makeService$0(CommonServerLaunchHandler.java:27) ~[fmlloader-1.20.1-47.3.10.jar%2369!/:?] {}     at cpw.mods.modlauncher.LaunchServiceHandlerDecorator.launch(LaunchServiceHandlerDecorator.java:30) ~[modlauncher-10.0.9.jar%2355!/:?] {}     at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:53) ~[modlauncher-10.0.9.jar%2355!/:?] {}     at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:71) ~[modlauncher-10.0.9.jar%2355!/:?] {}     at cpw.mods.modlauncher.Launcher.run(Launcher.java:108) ~[modlauncher-10.0.9.jar%2355!/:?] {}     at cpw.mods.modlauncher.Launcher.main(Launcher.java:78) ~[modlauncher-10.0.9.jar%2355!/:?] {}     at cpw.mods.bootstraplauncher.BootstrapLauncher.main(BootstrapLauncher.java:141) ~[bootstraplauncher-1.1.2.jar:?] {}  
    • This error keeps coming up after player tries to join server, doesn't happen to anyone else.  Error: Internal Exception: io.netty.handler.codec.DecoderException: java.lang.ArrayIndexOutOfBoundsException: Index 17196645 out of bounds for length 13 Heres the latest.log https://pastebin.com/uaw3KC0K  Heres the debug.log https://drive.google.com/file/d/1QzqtCMUf7ps1Iz85AOsMM7W8QhbHjBeU/view?usp=sharing
  • Topics

×
×
  • Create New...

Important Information

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