我的世界 1.8 gui 滑块问题

minecraft 1.8 gui slider issues

我最近开始制作我的世界 mod 我有一个关于 GUI 的问题 我想创建一个幻灯片,唯一的问题是 ButtonList.add(new GUISlider());

我不明白这些参数,谁能给我解释一下? 谢谢! :D

好的,我明白了,您需要创建一个不同于默认滑块的单独滑块,然后从那里调用它。

我的滑块代码:

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;
}
}

我的 GUI 代码:

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
}





}

调用新的 Slider 是这样的:

  1. 在 GUI 代码中创建一个新的 GuiSliderFixed ---> 示例: ---> GuiSliderFixed mySlider = new GuiSliderFixed();

  2. 将参数设置为此(int id、int x、int y、String label、float statingValue、float maxValue、float minValue)---> 示例:---> GuiSliderFixed mySlider = new GuiSliderFixed(3, width / 2 - 75, height / 3, "MPH ", 1.0F, 100.0F, 1.0F);

  3. 将其添加到 buttonList ---> 示例 ---> buttonList.add(mySlider);

希望对您有所帮助!