Skip to content

Instantly share code, notes, and snippets.

@itsnebulalol
Created September 28, 2020 23:06
Show Gist options
  • Save itsnebulalol/5ee0ba944c2b7a7d15a075ea31b38b23 to your computer and use it in GitHub Desktop.
Save itsnebulalol/5ee0ba944c2b7a7d15a075ea31b38b23 to your computer and use it in GitHub Desktop.
A simple button snippet for 1.8.9 Sponge mixins. (Original by Tascord, Mixin port by me)
// | ---------------------------------------------------------------------- |
// | Created by Nebula (http://bit.ly/subnebula) |
// | This snippet is for you to LEARN from. NOT copy. |
// | |
// | This program is free software: you can redistribute it and/or modify |
// | it under the terms of the GNU General Public License as published by |
// | the Free Software Foundation, either version 3 of the License, or |
// | (at your option) any later version. |
// | |
// | This program is distributed in the hope that it will be useful, |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
// | GNU General Public License for more details. |
// | |
// | You should have received a copy of the GNU General Public License |
// | along with this program. If not, see <https://www.gnu.org/licenses/>. |
// | |
// | Installation steps: |
// | 1. Download mod into src/java/clientname/mixins/client/gui |
// | 2. Add the file to your mixins json |
// | 3. Enjoy! |
// | ---------------------------------------------------------------------- |
// You will need to change the package name here
package xyz.nebulayt.cobalt.mixins.client.gui;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.renderer.GlStateManager;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import static net.minecraft.client.gui.Gui.drawRect;
@Mixin(GuiButton.class)
/** Mixin for GuiButton
* @author Nebula | Nebula#9998
* @since 1.0.0 **/
public class MixinGuiButton {
@Shadow protected int width;
@Shadow protected int height;
@Shadow public int xPosition;
@Shadow public int yPosition;
@Shadow public boolean visible;
@Shadow protected boolean hovered;
@Shadow public boolean enabled;
@Shadow public String displayString;
@Shadow protected int getHoverState(boolean mouseOver) {
int i = 1;
if (!this.enabled)
{
i = 0;
}
else if (mouseOver)
{
i = 2;
}
return i;
}
@Shadow protected void mouseDragged(Minecraft mc, int mouseX, int mouseY) { }
protected void drawHorizontalLine(int startX, int endX, int y, int color)
{
if (endX < startX)
{
int i = startX;
startX = endX;
endX = i;
}
drawRect(startX, y, endX + 1, y + 1, color);
}
protected void drawVerticalLine(int x, int startY, int endY, int color)
{
if (endY < startY)
{
int i = startY;
startY = endY;
endY = i;
}
drawRect(x, startY + 1, x + 1, endY, color);
}
public void drawCenteredString(FontRenderer fontRendererIn, String text, int x, int y, int color)
{
fontRendererIn.drawStringWithShadow(text, (float)(x - fontRendererIn.getStringWidth(text) / 2), (float)y, color);
}
public void drawButton(Minecraft mc, int mouseX, int mouseY) {
if(visible)
{
FontRenderer fontrenderer = mc.fontRendererObj;
drawRect(this.xPosition, this.yPosition, this.xPosition + this.width, this.yPosition + this.height, 0x44000000);
int outlineColour = 0x44555555;
this.drawHorizontalLine(this.xPosition, this.xPosition + this.width, this.yPosition, outlineColour);
this.drawHorizontalLine(this.xPosition, this.xPosition + this.width, this.yPosition + this.height, outlineColour);
this.drawVerticalLine(this.xPosition, this.yPosition + this.height, this.yPosition, outlineColour);
this.drawVerticalLine(this.xPosition + this.width, this.yPosition + this.height, this.yPosition, outlineColour);
// this.drawVerticalLine(x+w, y+h, y, colour);
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.mouseDragged(mc, mouseX, mouseY);
int j = 14737632;
if (!this.enabled)
{
j = 10526880;
}
else if (this.hovered)
{
j = 16777120;
}
this.drawCenteredString(fontrenderer, this.displayString, this.xPosition + this.width / 2, this.yPosition + (this.height - 8) / 2, j);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment