Skip to content

Commit

Permalink
GuiButton更新 Dev0.1.9
Browse files Browse the repository at this point in the history
  • Loading branch information
CedarXuesong committed Jul 7, 2023
1 parent ed4df97 commit ae8f78f
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/cn/XueSong/Client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
public class Client {

public static final String NAEM = "Cedar";
public static final String VERSION = "Dev 0.1.5";
public static final String VERSION = "Dev 0.1.9";
public static ModManager modManager;
public static void start(){
modManager = new ModManager();
Expand Down
4 changes: 2 additions & 2 deletions src/cn/XueSong/Client/util/render/RenderUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ public static void roundedRectangle(double x, double y, double width, double hei
CShaders.CQ_SHADER.draw(x, y, width, height, radius, color);
}

public void roundedOutlineRectangle(double x, double y, double width, double height, double radius, double borderSize, Color color) {
public static void roundedOutlineRectangle(double x, double y, double width, double height, double radius, double borderSize, Color color) {
CShaders.COQ_SHADER.draw(x, y, width, height, radius, borderSize, color);
}

public void roundedOutlineGradientRectangle(double x, double y, double width, double height, double radius, double borderSize, Color color1, Color color2) {
public static void roundedOutlineGradientRectangle(double x, double y, double width, double height, double radius, double borderSize, Color color1, Color color2) {
CShaders.COGQ_SHADER.draw(x, y, width, height, radius, borderSize, color1, color2);
}
public static void end() {
Expand Down
161 changes: 144 additions & 17 deletions src/net/minecraft/client/gui/GuiButton.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package net.minecraft.client.gui;

import cn.XueSong.Client.util.render.RenderUtil;
import cn.XueSong.Client.util.shader.CShaders;
import net.minecraft.client.Minecraft;
import net.minecraft.client.audio.PositionedSoundRecord;
import net.minecraft.client.audio.SoundHandler;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.util.ResourceLocation;

import java.awt.*;

public class GuiButton extends Gui
{
protected static final ResourceLocation buttonTextures = new ResourceLocation("textures/gui/widgets.png");
Expand All @@ -32,7 +36,29 @@ public class GuiButton extends Gui
/** Hides the button completely if false. */
public boolean visible;
protected boolean hovered;
private boolean selected = false;
// 按钮的初始宽度和高度

private final float minScale = 1.0f; // 按钮的最小缩放比例
private final float maxScale = 1.03f; // 按钮的最大缩放比例
private final float scaleFadeSpeed = 0.002f; // 缩放渐变速度
private float currentScale = minScale; // 当前按钮的缩放比例
private final float initialX = this.xPosition; // 按钮的初始x坐标
private final float initialY = this.yPosition; // 按钮的初始y坐标
private float currentX = initialX; // 当前按钮的x坐标
private float currentY = initialY; // 当前按钮的y坐标

/** 渐变颜色 */
private Color currentColor;

private float EachColor;
public void setSelected(boolean selected) {
this.selected = selected;
}

public boolean isSelected() {
return selected;
}
public GuiButton(int buttonId, int x, int y, String buttonText)
{
this(buttonId, x, y, 200, 20, buttonText);
Expand Down Expand Up @@ -75,36 +101,95 @@ else if (mouseOver)
/**
* Draws this button to the screen.
*/
public void drawButton(Minecraft mc, int mouseX, int mouseY)
{
if (this.visible)
{


float fadeSpeed = 0.1f;// 颜色渐变的速度,根据需要调整

private float sizeFadeSpeed = 0.01f;// 按钮的大小渐变速度
private boolean isFadingOut = false;
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 i = this.getHoverState(this.hovered);
GlStateManager.enableBlend();
GlStateManager.tryBlendFuncSeparate(770, 771, 1, 0);
GlStateManager.blendFunc(770, 771);
this.drawTexturedModalRect(this.xPosition, this.yPosition, 0, 46 + i * 20, this.width / 2, this.height);
this.drawTexturedModalRect(this.xPosition + this.width / 2, this.yPosition, 200 - this.width / 2, 46 + i * 20, this.width / 2, this.height);

// 计算按钮颜色
Color baseColor = new Color(38, 38, 38, 189);
Color targetColor = this.hovered ? new Color(115, 115, 115, 189) : baseColor;

// 更新按钮颜色
if (this.currentColor == null) {
this.currentColor = baseColor;
}

// 如果按钮之前处于选中状态但现在没有被选中,则启动渐变退色
if (!this.hovered && this.isFadingOut) {
targetColor = baseColor;
}

// 如果按钮被选中,则启动渐变变亮
if (this.hovered && !this.isFadingOut) {
targetColor = new Color(115, 115, 115, 189);
}

this.currentColor = fadeColor(this.currentColor, targetColor, fadeSpeed);

// 检查是否需要停止渐变退色
if (!this.hovered && this.currentColor.equals(baseColor)) {
this.isFadingOut = false;
}

// 渐变按钮大小和位置
if (this.hovered) {
// 按钮被选中时逐渐变大和移动
if (this.currentScale < this.maxScale) {
this.currentScale += this.scaleFadeSpeed;
}
this.currentX = this.xPosition - (this.currentScale - 1.0f) * this.width / 2.0f;
this.currentY = this.yPosition - (this.currentScale - 1.0f) * this.height / 2.0f;
} else {
// 按钮未被选中时逐渐恢复初始大小和位置
if (this.currentScale > this.minScale) {
this.currentScale -= this.scaleFadeSpeed;
}
this.currentX = this.xPosition - (this.currentScale - 1.0f) * this.width / 2.0f;
this.currentY = this.yPosition - (this.currentScale - 1.0f) * this.height / 2.0f;
}

// 计算实际绘制的宽度和高度
float drawWidth = this.width * this.currentScale;
float drawHeight = this.height * this.currentScale;

// 绘制阴影
RenderUtil.dropShadow(15, this.currentX, this.currentY, drawWidth, drawHeight, 40, 5 + 5);

// 绘制圆角按钮
RenderUtil.roundedRectangle(this.currentX, this.currentY, drawWidth, drawHeight, 5, this.currentColor);

//RenderUtil.dropShadow(15, this.xPosition, this.yPosition, this.width, this.height, 40, 5 + 5);
//RenderUtil.roundedRectangle(this.xPosition, this.yPosition, this.width, this.height, 5, this.currentColor);
RenderUtil.roundedOutlineRectangle(this.currentX, this.currentY, drawWidth, drawHeight, 3, 1, new Color(185, 185, 185, 47));

this.mouseDragged(mc, mouseX, mouseY);
int j = 14737632;

if (!this.enabled)
{
j = 10526880;
}
else if (this.hovered)
{
if (!this.enabled) {
j = new Color(72, 72, 72, 255).getRGB();
} else if (this.hovered) {
j = 16777120;
}

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

// 如果按钮之前处于选中状态但现在没有被选中,则启动渐变退色
if (!this.hovered && !this.isFadingOut) {
this.isFadingOut = true;
}
}
}



/**
* Fired when the mouse button is dragged. Equivalent of MouseListener.mouseDragged(MouseEvent e).
*/
Expand Down Expand Up @@ -154,4 +239,46 @@ public void setWidth(int width)
{
this.width = width;
}

/**
用于实现渐变效果
*/
private Color fadeColor(Color currentColor, Color targetColor, float fadeSpeed) {
int r = currentColor.getRed();
int g = currentColor.getGreen();
int b = currentColor.getBlue();
int a = currentColor.getAlpha();

if (r != targetColor.getRed()) {
r = interpolate(r, targetColor.getRed(), fadeSpeed);
}

if (g != targetColor.getGreen()) {
g = interpolate(g, targetColor.getGreen(), fadeSpeed);
}

if (b != targetColor.getBlue()) {
b = interpolate(b, targetColor.getBlue(), fadeSpeed);
}

if (a != targetColor.getAlpha()) {
a = interpolate(a, targetColor.getAlpha(), fadeSpeed);
}

return new Color(r, g, b, a);
}

private int interpolate(int start, int end, float fadeSpeed) {
int diff = end - start;
int step = (int) (fadeSpeed * Math.abs(diff));
if (step < 1) {
step = 1;
}
if (start < end) {
return Math.min(start + step, end);
} else {
return Math.max(start - step, end);
}
}

}
2 changes: 1 addition & 1 deletion src/net/minecraft/client/gui/GuiMainMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ else if (calendar.get(2) + 1 == 10 && calendar.get(5) == 31)

this.buttonList.add(new GuiButton(0, this.width / 2 - 100, j + 72 + 12, 98, 20, I18n.format("menu.options", new Object[0])));
this.buttonList.add(new GuiButton(4, this.width / 2 + 2, j + 72 + 12, 98, 20, I18n.format("menu.quit", new Object[0])));
this.buttonList.add(new GuiButtonLanguage(5, this.width / 2 - 124, j + 72 + 12));
//this.buttonList.add(new GuiButtonLanguage(5, this.width / 2 - 124, j + 72 + 12));

synchronized (this.threadLock)
{
Expand Down

0 comments on commit ae8f78f

Please sign in to comment.