package info.u_team.u_team_core.gui.elements;

import com.mojang.blaze3d.matrix.MatrixStack;

import info.u_team.u_team_core.api.gui.IScaleable;
import info.u_team.u_team_core.util.GuiUtil;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.text.*;

public class ScalableButton extends UButton implements IScaleable {
	
	protected float scale;
	
	public ScalableButton(int x, int y, int width, int height, float scale, ITextComponent display) {
		this(x, y, width, height, scale, display, EMTPY_PRESSABLE);
	}
	
	public ScalableButton(int x, int y, int width, int height, float scale, ITextComponent display, IPressable pessable) {
		this(x, y, width, height, scale, display, pessable, EMPTY_TOOLTIP);
	}
	
	public ScalableButton(int x, int y, int width, int height, float scale, ITextComponent display, ITooltip tooltip) {
		this(x, y, width, height, scale, display, EMTPY_PRESSABLE, tooltip);
	}
	
	public ScalableButton(int x, int y, int width, int height, float scale, ITextComponent display, IPressable pessable, ITooltip tooltip) {
		super(x, y, width, height, display, pessable, tooltip);
		this.scale = scale;
	}
	
	public float getScale() {
		return scale;
	}
	
	public void setScale(float scale) {
		this.scale = scale;
	}
	
	@Override
	public void renderButton(MatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) {
		final Minecraft minecraft = Minecraft.getInstance();
		final FontRenderer fontRenderer = minecraft.fontRenderer;
		
		ITextComponent message = getMessage();
		
		GuiUtil.drawContinuousTexturedBox(matrixStack, WIDGETS_LOCATION, x, y, 0, 46 + getYImage(isHovered()) * 20, width, height, 200, 20, 2, 3, 2, 2, 0, getButtonColor());
		
		renderBg(matrixStack, minecraft, mouseX, mouseY);
		
		final int messageWidth = MathHelper.ceil(scale * fontRenderer.getStringPropertyWidth(message));
		final int ellipsisWidth = MathHelper.ceil(scale * fontRenderer.getStringWidth("..."));
		
		if (messageWidth > width - 6 && messageWidth > ellipsisWidth) {
			message = new StringTextComponent(fontRenderer.func_238417_a_(message, width - 6 - ellipsisWidth).getString() + "...");
		}
		
		final float positionFactor = 1 / scale;
		
		final float xStart = (x + (width / 2) - messageWidth / 2) * positionFactor;
		final float yStart = (y + ((int) (height - 8 * scale)) / 2) * positionFactor;
		
		matrixStack.push();
		matrixStack.scale(scale, scale, 0);
		fontRenderer.func_243246_a(matrixStack, message, xStart, yStart, getFGColor());
		matrixStack.pop();
	}
}

@@ -1439,16 +1439,114 @@
 icks) %7B%0A
+%09%09if (scale == 1) %7B%0A%09%09%09super.renderButton(matrixStack, mouseX, mouseY, partialTicks);%0A%09%09%7D else %7B%0A%09
 %09%09final 
@@ -1592,16 +1592,17 @@
 ance();%0A
+%09
 %09%09final 
@@ -1655,17 +1655,19 @@
 erer;%0A%09%09
-%0A
+%09%0A%09
 %09%09ITextC
@@ -1701,17 +1701,19 @@
 ge();%0A%09%09
-%0A
+%09%0A%09
 %09%09GuiUti
@@ -1874,17 +1874,19 @@
 r());%0A%09%09
-%0A
+%09%0A%09
 %09%09render
@@ -1927,25 +1927,27 @@
  mouseY);%0A%09%09
-%0A
+%09%0A%09
 %09%09final int 
@@ -2030,24 +2030,25 @@
 essage));%0A%09%09
+%09
 final int el
@@ -2124,17 +2124,19 @@
 .%22));%0A%09%09
-%0A
+%09%0A%09
 %09%09if (me
@@ -2193,16 +2193,17 @@
 idth) %7B%0A
+%09
 %09%09%09messa
@@ -2323,13 +2323,16 @@
 ;%0A%09%09
+%09
 %7D%0A%09%09
-%0A
+%09%0A%09
 %09%09fi
@@ -2371,19 +2371,21 @@
 cale;%0A%09%09
+%09
 %0A%09%09
+%09
 final fl
@@ -2452,16 +2452,17 @@
 Factor;%0A
+%09
 %09%09final 
@@ -2535,17 +2535,19 @@
 ctor;%0A%09%09
-%0A
+%09%0A%09
 %09%09matrix
@@ -2558,24 +2558,25 @@
 k.push();%0A%09%09
+%09
 matrixStack.
@@ -2599,16 +2599,17 @@
 le, 0);%0A
+%09
 %09%09fontRe
@@ -2684,16 +2684,17 @@
 r());%0A%09%09
+%09
 matrixSt
@@ -2704,13 +2704,17 @@
 .pop();%0A
+%09%09%7D%0A
 %09%7D%0A%7D%0A

Allow the scalable button to be used all the time, because when no scaling is applied, the super of ubutton is called