Jump to content

help on Gui Sliders


UltraTechX

Recommended Posts

I recently started working on a mod and I ran into a GUI problem, I can;t figure out how to get a slider in my GUI, many things online say to use a controlList but I says its not there when i use this.controlList.add(); So can someone give me a slider code and how to get it working.  Thanks! :D

I'm working on something big!  As long as there arent alot of big issues... [D

Link to comment
Share on other sites

ok, I figured it out, you need to create a separate slider other than the default, then call it from there.

 

My Slider Code:

 

package tutorial.generic;

 

import net.minecraft.client.Minecraft;

import net.minecraft.client.gui.FontRenderer;

import net.minecraft.client.gui.GuiButton;

import net.minecraft.client.renderer.GlStateManager;

 

import org.lwjgl.opengl.GL11;

 

public class GuiSliderFixed extends GuiButton {

 

public float sliderValue = 1.0F;

public float sliderMaxValue = 1.0F;

public float sliderMinValue = 1.0F;

public boolean dragging = false;

public String label;

 

public GuiSliderFixed(int id, int x, int y, String label, float startingValue, float maxValue, float minValue) {

    super(id, x, y, 150, 20, label);

    this.label = label;

    this.sliderValue = startingValue;

    this.sliderMaxValue = maxValue;

    this.sliderMinValue = minValue;

}

 

protected int getHoverState(boolean par1) {

    return 0;

}

 

 

@Override

public void drawButton(Minecraft mc, int mouseX, int mouseY)

{

    if (this.visible)

    {

        FontRenderer fontrenderer = mc.fontRendererObj;

        mc.getTextureManager().bindTexture(buttonTextures);

        GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);

        this.hovered = mouseX >= this.xPosition && mouseY >= this.yPosition && mouseX < this.xPosition + this.width && mouseY < this.yPosition + this.height;

        int k = this.getHoverState(this.hovered);

        GlStateManager.enableBlend();

        GlStateManager.tryBlendFuncSeparate(770, 771, 1, 0);

        GlStateManager.blendFunc(770, 771);

        this.drawTexturedModalRect(this.xPosition, this.yPosition, 0, 46 + k * 20, this.width / 2, this.height);

        this.drawTexturedModalRect(this.xPosition + this.width / 2, this.yPosition, 200 - this.width / 2, 46 + k * 20, this.width / 2, this.height);

        this.mouseDragged(mc, mouseX, mouseY);

        int l = 14737632;

 

        if (packedFGColour != 0)

        {

            l = packedFGColour;

        }

        else if (!this.enabled)

        {

            l = 10526880;

        }

        else if (this.hovered)

        {

            l = 16777120;

        }

 

        this.drawCenteredString(fontrenderer, this.displayString, this.xPosition + this.width / 2, this.yPosition + (this.height - 8) / 2, l);

    }

}

 

protected void mouseDragged(Minecraft par1Minecraft, int par2, int par3) {

    if (this.enabled && this.visible && this.packedFGColour == 0) {

        if (this.dragging) {

            this.sliderValue = (float) (par2 - (this.xPosition + 4)) / (float) (this.width - 8);

 

            if (this.sliderValue < 0.0F) {

                this.sliderValue = 0.0F;

            }

 

            if (this.sliderValue > 1.0F) {

                this.sliderValue = 1.0F;

            }

 

        }

 

        this.displayString = label + ": " + (int) (sliderValue * sliderMaxValue);

        GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);

        this.drawTexturedModalRect(this.xPosition + (int) (this.sliderValue * (float) (this.width - 8)), this.yPosition, 0, 66, 4, 20);

        this.drawTexturedModalRect(this.xPosition + (int) (this.sliderValue * (float) (this.width - 8)) + 4, this.yPosition, 196, 66, 4, 20);

    }

}

 

public boolean mousePressed(Minecraft par1Minecraft, int par2, int par3) {

    if (super.mousePressed(par1Minecraft, par2, par3)) {

        this.sliderValue = (float) (par2 - (this.xPosition + 4)) / (float) (this.width - 8);

 

        if (this.sliderValue < 0.0F) {

            this.sliderValue = 0.0F;

        }

 

        if (this.sliderValue > 1.0F) {

            this.sliderValue = 1.0F;

        }

 

        this.dragging = true;

        return true;

    } else {

        return false;

    }

}

 

public void mouseReleased(int par1, int par2) {

    this.dragging = false;

}

}

My GUI Code:

 

package tutorial.generic;

 

import java.awt.Color;

import java.io.IOException;

 

import net.minecraft.client.gui.GuiButton;

import net.minecraft.client.gui.GuiScreen;

import net.minecraft.client.gui.GuiSlider;

import net.minecraft.client.gui.GuiTextField;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.nbt.NBTTagCompound;

import net.minecraft.util.ChatComponentText;

import net.minecraft.util.ResourceLocation;

import net.minecraft.util.StatCollector;

import net.minecraft.world.World;

 

public class guiGenericTileEntity extends GuiScreen{

 

public static NBTTagCompound nameTag = new NBTTagCompound();

private int x, y, z;

private EntityPlayer player;

private World world;

private int xSize, ySize;

public static GuiTextField textField;

public static GuiSliderFixed mySlider;

public static NBTTagCompound maxSpeedTag = new NBTTagCompound();

public guiGenericTileEntity(EntityPlayer player, World world, int x, int y, int z) {

 

    this.x = x;

    this.y = y;

    this.z = z;

    this.player = player;

    this.world = world;

    xSize = 176;

    ySize = 214;

}

 

 

 

private ResourceLocation backgroundimage = new ResourceLocation(Generic.MODID.toLowerCase() + ":" + "textures/gui/guiBackGenericTileEntity.png");

 

@Override

public void drawScreen(int mouseX, int mouseY, float renderPartialTicks) {

    this.mc.getTextureManager().bindTexture(backgroundimage);

    int x = (this.width - xSize) / 2;

    int y = (this.height - ySize) / 2;

    drawTexturedModalRect(x, y, 0, 0, xSize,  ySize);

 

    fontRendererObj.drawString("MTTA System", (int) (width / 2 - (width / 13)), height / 15, 8);

    fontRendererObj.drawString("Train Name :", (int) (width / 2 - (width / 13)), (int) (height / 7.8), 8);

    fontRendererObj.drawString("Max Speed :", (int) (width / 2 - (width / 13)), (int) (height / 3.5), 8);

 

 

 

    textField.drawTextBox();

 

    super.drawScreen(mouseX, mouseY, renderPartialTicks);

}

 

@Override

public boolean doesGuiPauseGame() {

    return false;

}

 

@Override

public void updateScreen(){

    textField.updateCursorCounter();

 

    super.updateScreen();

}

 

@Override

protected void keyTyped(char typedChar, int keyCode) throws IOException{

    textField.textboxKeyTyped(typedChar, keyCode);

    super.keyTyped(typedChar, keyCode);

}

 

@Override

public void initGui(){

 

 

    buttonList.add(new GuiButton(1, (int) (xSize / 3 * 2.35), ySize - (ySize / 18), xSize - 20, height / 12, "Save And Close"));

 

    mySlider = new GuiSliderFixed(3, width / 2 - 75, height / 3, "MPH ", 1.0F, 100.0F, 1.0F);

    buttonList.add(mySlider);

    mySlider.sliderValue = maxSpeedTag.getFloat("MaxSpeed");

 

    textField = new GuiTextField(width / 2, fontRendererObj, width / 2 - 50, (int) (height /  6), 100, 20);

    textField.setMaxStringLength(30);

    textField.setText(nameTag.getString("Name"));

    textField.setFocused(true);

    textField.setCanLoseFocus(false);

    super.initGui();

}

 

@Override

protected void actionPerformed(GuiButton guibutton) {

    //id is the id you give your button

    switch(guibutton.id) {

    case 1:

        player.closeScreen();

        nameTag.setString("Name", textField.getText());

        maxSpeedTag.setFloat("MaxSpeed", mySlider.sliderValue);

        genericTileEntity.processActivate(player, world);

        player.addChatMessage(new ChatComponentText("Saved the current settings for " + textField.getText() + "!"));

        break;

    }

    //Packet code here

    //PacketDispatcher.sendPacketToServer(packet); //send packet

}

 

 

 

 

 

}

Calling the new Slider works like this:

 

create a new GuiSliderFixed in the GUI code ---> example: ---> GuiSliderFixed mySlider = new GuiSliderFixed();

 

set the Parameters to this (int id, int x, int y, String label, float statingValue, float maxValue, float minValue) ---> example: ---> GuiSliderFixed mySlider = new GuiSliderFixed(3, width / 2 - 75, height / 3, "MPH ", 1.0F, 100.0F, 1.0F);

 

add it to buttonList ---> example ---> buttonList.add(mySlider);

 

Hope This Helps!

I'm working on something big!  As long as there arent alot of big issues... [D

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.