[全民写端]#12绘制ClickGUI

简介: 先复制进去FontUtils

首发于Enaium的个人博客


一. 先复制进去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);
    }
}

二. 开始绘制

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);//绘制所有Category
        }
        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);//调用所有CategoryPanel的mouseClicked方法
        }
        super.mouseClicked(mouseX, mouseY, mouseButton);
    }

    @Override
    public void mouseReleased(int mouseX, int mouseY, int state) {
   
        for (CategoryPanel c : categoryPanels) {
   
            c.mouseReleased(mouseX, mouseY,state);//调用所有CategoryPanel的mouseReleased方法
        }
        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));//获取该分类所以Module
        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;
        //绘制该Category下的所有Module
        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) {
   //调用所有ModulePanel的mouseClicked方法
            modulePanel.mouseClicked(mouseX,mouseY,mouseButton);
        }
    }

    public void mouseReleased(int mouseX, int mouseY, int state) {
   
        //如果鼠标左键被释放退出移动Category模式
        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();//当鼠标在指定位置并且鼠标被按下设置Module为关闭或打开
        }
    }
}

三. 打开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;
    }
目录
相关文章
最新MAC 中文版 Final Cut Pro X V10.6.5 专属视频剪辑后期工具及其插件安装使用教程
Final Cut Pro X又名FCPX,是MAC上非常不错的视频非线性剪辑软件,它剪辑速度超凡,具有先进的调色功能、HDR 视频支持,以及 ProRes RAW,让剪辑、音轨、图形特效、整片输出,支持主流的摄像机格式,是专业视频剪辑领域的王者工具。同时也给大伙安排了两款不错的插件Motion,Compressor。
最新MAC 中文版 Final Cut Pro X V10.6.5 专属视频剪辑后期工具及其插件安装使用教程
|
Java Linux iOS开发
Linux下安装并配置Gradle
Linux下安装并配置Gradle
731 1
|
Java 计算机视觉
实现邮箱验证(邮箱验证码登录)
我们要实现web或者Java的发送邮箱验证码到邮箱上进行验证。当然我们需要做一下前提的准备,也就是先要导我们的jar包,然后再进行下一步的操作。
|
存储 自然语言处理 IDE
通义灵码初识
讲述什么是通义灵码、适用环境、基本操作
|
9月前
|
机器学习/深度学习 人工智能 搜索推荐
BioEmu:微软黑科技炸场!生成式AI重构蛋白质模拟:千倍效率碾压传统计算,新药研发周期砍半
BioEmu 是微软推出的生成式深度学习系统,可在单个 GPU 上每小时生成数千种蛋白质结构样本,支持模拟动态变化、预测热力学性质,并显著降低计算成本。
490 2
BioEmu:微软黑科技炸场!生成式AI重构蛋白质模拟:千倍效率碾压传统计算,新药研发周期砍半
|
3月前
|
存储 弹性计算 Linux
阿里云服务器从零到精通的购买指南,云服务器购买流程及注意事项参考
对于许多初次接触阿里云服务器的用户而言,如何选择云服务器配置以及在选购过程中有哪些注意事项,是新手用户比较关心的问题。本文为大家展示阿里云服务器选购的完整指南,涵盖了通过云服务器ECS产品页下单的详细步骤,以及通过阿里云的活动选购价格比较实惠的云服务器。重点是介绍每一步的注意事项,以供初次选购阿里云服务器的个人开发者和企业用户参考,尽量一次选购好,避免出现买错从新买的情况出现。
|
7月前
|
容器
vllm+vllm-ascend本地部署QwQ-32B
本指南介绍如何下载、安装和启动基于Ascend的vLLM模型。首先,可通过华为镜像或Hugging Face下载预训练模型;其次,安装vllm-ascend,支持通过基础镜像(如`quay.io/ascend/vllm-ascend:v0.7.3-dev`)或源码编译方式完成;最后,使用OpenAI兼容接口启动模型,例如运行`vllm serve`命令,设置模型路径、并行规模等参数。适用于大模型推理场景,需注意显存需求(如QwQ-32B需70G以上)。
3031 17
|
前端开发 Java 数据安全/隐私保护
用户登录前后端开发(一个简单完整的小项目)——SpringBoot与session验证(带前后端源码)全方位全流程超详细教程
文章通过一个简单的SpringBoot项目,详细介绍了前后端如何实现用户登录功能,包括前端登录页面的创建、后端登录逻辑的处理、使用session验证用户身份以及获取已登录用户信息的方法。
1724 2
用户登录前后端开发(一个简单完整的小项目)——SpringBoot与session验证(带前后端源码)全方位全流程超详细教程
|
9月前
|
计算机视觉
YOLOv11改进策略【Neck】| 替换RT-DETR中的CCFF跨尺度特征融合颈部结构,优化计算瓶颈与冗余问题
YOLOv11改进策略【Neck】| 替换RT-DETR中的CCFF跨尺度特征融合颈部结构,优化计算瓶颈与冗余问题
704 8
YOLOv11改进策略【Neck】| 替换RT-DETR中的CCFF跨尺度特征融合颈部结构,优化计算瓶颈与冗余问题
|
Java Linux Windows
Java“Could Not Create Java Virtual Machine”解决
当在Java中遇到“Could Not Create Java Virtual Machine”错误时,通常是由于内存设置不当、Java版本不兼容、类路径错误或操作系统限制等原因导致JVM无法启动。解决方法包括调整内存参数、确认Java版本兼容性、检查类路径和启动参数、以及检查用户权限和文件系统。
5681 1