Minecraft Client 鏁欑▼ #12 缁樺埗ClickGUI

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Minecraft Client 鏁欑▼ #12 缁樺埗ClickGUI相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/over' title='over'>over   tst   toggle   trend   imp   module   text   ota   err   

棣栧彂浜?a href="http://blog.enaium.cn/">Enaium鐨勪釜浜哄崥瀹?/a>


涓€. 鍏堝鍒惰繘鍘?code>FontUtils

FontUtils

package cn.enaium.coreium.utils;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.util.StringUtils;

public class FontUtils {
    private static FontRenderer fontRenderer;

    public static void setupFontUtils() {
        fontRenderer = Minecraft.getMinecraft().fontRendererObj;
    }

    public static int getStringWidth(String text) {
        return fontRenderer.getStringWidth(StringUtils.stripControlCodes(text));
    }

    public static int getFontHeight() {
        return fontRenderer.FONT_HEIGHT;
    }

    public static void drawString(String text, int x, int y, int color) {
        fontRenderer.drawString(text, x, y, color);
    }

    public static void drawStringWithShadow(String text, double x, double y, int color) {
        fontRenderer.drawStringWithShadow(text, (float)x, (float)y, color);
    }

    public static void drawCenteredString(String text, int x, int y, int color) {
        FontUtils.drawString(text, x - fontRenderer.getStringWidth(text) / 2, y, color);
    }

    public static void drawCenteredStringWithShadow(String text, double x, double y, int color) {
        FontUtils.drawStringWithShadow(text, x - (double)(fontRenderer.getStringWidth(text) / 2), y, color);
    }

    public static void drawTotalCenteredString(String text, int x, int y, int color) {
        FontUtils.drawString(text, x - fontRenderer.getStringWidth(text) / 2, y - fontRenderer.FONT_HEIGHT / 2, color);
    }

    public static void drawTotalCenteredStringWithShadow(String text, double x, double y, int color) {
        FontUtils.drawStringWithShadow(text, x - (double)(fontRenderer.getStringWidth(text) / 2), y - (double)((float)fontRenderer.FONT_HEIGHT / 2.0f), color);
    }
}

浜? 寮€濮嬬粯鍒?/p>

ClickGUI

package cn.enaium.coreium.gui.clickgui;

import cn.enaium.coreium.module.Category;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.gui.GuiScreen;

import java.io.IOException;
import java.util.ArrayList;

public class ClickGUI extends GuiScreen {

    public ArrayList<CategoryPanel> categoryPanels;

    public ClickGUI() {
        //娣诲姞Category
        categoryPanels = new ArrayList<>();
        int categoryPanelsY = 5;
        for (Category c : Category.values()) {
            categoryPanels.add(new CategoryPanel(c, 5, categoryPanelsY, 100, 20));
            categoryPanelsY += 30;
        }
    }

    @Override
    public void drawScreen(int mouseX, int mouseY, float partialTicks) {
        for (CategoryPanel c : categoryPanels) {
            c.drawScreen(mouseX, mouseY);//缁樺埗鎵€鏈塁ategory
        }
        super.drawScreen(mouseX, mouseY, partialTicks);
    }

    @Override
    public void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException {
        for (CategoryPanel c : categoryPanels) {
            c.mouseClicked(mouseX, mouseY,mouseButton);//璋冪敤鎵€鏈塁ategoryPanel鐨刴ouseClicked鏂规硶
        }
        super.mouseClicked(mouseX, mouseY, mouseButton);
    }

    @Override
    public void mouseReleased(int mouseX, int mouseY, int state) {
        for (CategoryPanel c : categoryPanels) {
            c.mouseReleased(mouseX, mouseY,state);//璋冪敤鎵€鏈塁ategoryPanel鐨刴ouseReleased鏂规硶
        }
        super.mouseReleased(mouseX, mouseY, state);
    }

    public static boolean isHovered(int mouseX, int mouseY, int x, int y, int width, int height) {
        return mouseX >= x && mouseX - width <= x && mouseY >= y && mouseY - height <= y;//鑾峰彇榧犳爣浣嶇疆鏄惁鍦ㄦ寚瀹氫綅缃?    }


    public static void drawRect(int x, int y, int width, int height, int color) {
        Gui.drawRect(x, y, x + width, y + height, color);//缁樺埗Rect
    }
}

CategoryPanel

package cn.enaium.coreium.gui.clickgui;

import cn.enaium.coreium.Coreium;
import cn.enaium.coreium.module.Category;
import cn.enaium.coreium.module.Module;
import cn.enaium.coreium.utils.FontUtils;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.Gui;

import java.awt.*;
import java.util.ArrayList;

public class CategoryPanel {


    private Category category;
    private boolean hovered;
    //鍗曠嫭Category鐨勪綅缃?    private int x;
    private int y;
    //鍗曠嫭Category鐨勯暱楂?    private int width;
    private int height;

    public boolean dragging;//鏄惁涓虹Щ鍔ㄧ姸鎬?    //涓存椂鍗曠嫭Category鐨勪綅缃紙涓婁竴涓綅缃級
    private int tempX;
    private int tempY;
    //鏄惁鏄剧ずModulePanel
    private boolean displayModulePanel;
    //ModulePanel鍒楄〃
    private ArrayList<ModulePanel> modulePanels;

    public CategoryPanel(Category category, int x, int y, int width, int height) {
        this.category = category;
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        FontUtils.setupFontUtils();//璁剧疆Font
        modulePanels = new ArrayList<>();

        ArrayList<Module> modules = new ArrayList<>();
        modules.addAll(Coreium.instance.moduleManager.getModulesForCategory(this.category));//鑾峰彇璇ュ垎绫绘墍浠odule
        for (Module m : modules) {
            modulePanels.add(new ModulePanel(m));//娣诲姞ModulePanel
        }
    }


    public void drawScreen(int mouseX, int mouseY) {
        this.hovered = ClickGUI.isHovered(mouseX, mouseY, this.x, this.y, this.width, this.height);//鑾峰彇榧犳爣鏄惁鍦ㄦ寚瀹氫綅缃?        if (this.dragging) {
            //绉诲姩CategoryPanel
            this.x = this.tempX + mouseX;
            this.y = this.tempY + mouseY;
        }
        //鏀瑰彉Category棰滆壊
        int color = new Color(0, 190, 255).getRGB();
        if (this.hovered) color = new Color(0, 88, 120).getRGB();
        ClickGUI.drawRect(x, y, this.width, this.height, color);//缁樺埗Category鑳屾櫙
        FontUtils.drawCenteredString(this.category.name(), x + this.width / 2, y + this.height / 2, Color.WHITE.getRGB());//缁樺埗Category鐨勬爣棰?        int modulePanelsY = this.y + this.height;
        //缁樺埗璇ategory涓嬬殑鎵€鏈塎odule
        if(this.displayModulePanel) {
            for (ModulePanel module : modulePanels) {
                module.drawScreen(mouseX,mouseY,this.x + 10, modulePanelsY, 80, 20);
                modulePanelsY += 20;
            }
        }
    }

    public void mouseClicked(int mouseX, int mouseY, int mouseButton) {
        //濡傛灉榧犳爣鍦ㄦ寚瀹氫綅缃?        //濡傛灉榧犳爣宸﹂敭鎸変笅
        if (this.hovered && mouseButton == 0) {
            //绉诲姩鐘舵€佷负true
            dragging = true;
            //缁欎复鏃跺潗鏍囪祴鍊?            this.tempX = this.x - mouseX;
            this.tempY = this.y - mouseY;
        } else if (this.hovered && mouseButton == 1) {//濡傛灉榧犳爣鍙抽敭琚寜涓?            this.displayModulePanel = !this.displayModulePanel;//鏄惁鏄剧ずModule
        }
        for (ModulePanel modulePanel : modulePanels) {//璋冪敤鎵€鏈塎odulePanel鐨刴ouseClicked鏂规硶
            modulePanel.mouseClicked(mouseX,mouseY,mouseButton);
        }
    }

    public void mouseReleased(int mouseX, int mouseY, int state) {
        //濡傛灉榧犳爣宸﹂敭琚噴鏀鹃€€鍑虹Щ鍔–ategory妯″紡
        if (state == 0) {
            this.dragging = false;
        }
    }


}

ModulePanel

package cn.enaium.coreium.gui.clickgui;

import cn.enaium.coreium.module.Module;
import cn.enaium.coreium.utils.FontUtils;

import java.awt.*;

public class ModulePanel {

    private Module module;
    private boolean hovered;

    public ModulePanel(Module module) {
        this.module = module;
        FontUtils.setupFontUtils();//璁剧疆Font
    }

    public void drawScreen(int mouseX, int mouseY, int x, int y, int width, int height) {
        this.hovered = ClickGUI.isHovered(mouseX, mouseY, x, y, width, height);//榧犳爣鏄惁鍦ㄦ寚瀹氫綅缃?        int color = new Color(200, 190, 255).getRGB();//棰滆壊
        if (this.module.isToggle()) color = new Color(200, 0, 120).getRGB();//Module鎵撳紑鐨勯鑹?        if (this.hovered) color = new Color(200, 88, 120).getRGB();//榧犳爣鍦ㄦ寚瀹氫綅缃殑棰滆壊
        ClickGUI.drawRect(x, y, width, height, color);//缁樺埗Module鐨勮儗鏅?        FontUtils.drawCenteredString(this.module.getName(), x + width / 2, y + height / 2, Color.WHITE.getRGB());//缁樺埗Module鐨勫悕瀛?    }


    public void mouseClicked(int mouseX, int mouseY, int mouseButton) {
        if(this.hovered && mouseButton == 0) {
            this.module.toggle();//褰撻紶鏍囧湪鎸囧畾浣嶇疆骞朵笖榧犳爣琚寜涓嬭缃甅odule涓哄叧闂垨鎵撳紑
        }
    }
}

涓? 鎵撳紑ClickGUI

Click

package cn.enaium.coreium.module.modules.render;

import cn.enaium.coreium.gui.clickgui.ClickGUI;
import cn.enaium.coreium.module.Category;
import cn.enaium.coreium.module.Module;
import org.lwjgl.input.Keyboard;

public class Click extends Module {
    public Click() {
        super("ClickGUI", Keyboard.KEY_RSHIFT, Category.RENDER);
    }

    @Override
    public void onEnable() {
        super.onEnable();
        mc.displayGuiScreen(new ClickGUI());
        toggle();
    }
}

getModulesForCategory

    public ArrayList<Module> getModulesForCategory(Category c) {
        ArrayList<Module> modules = new ArrayList<>();

        for (Module m : this.modules ) {
            if(m.getCategory().equals(c))
                modules.add(m);
        }

        return modules;
    }

以上是关于Minecraft Client 鏁欑▼ #12 缁樺埗ClickGUI的主要内容,如果未能解决你的问题,请参考以下文章

Minecraft Client 教程 #6 添加Setting

Minecraft Client 教程 #7 添加Command

Minecraft Client 教程 #11 绘制主菜单

Minecraft Fabric Client 教程 #1 配置开发环境

Minecraft Fabric Client 教程 #4 添加Modules

Minecraft Fabric Client 教程 #5 添加EventSprint和ToggleCommand