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, ITextComponent display, float scale) {
		this(x, y, width, height, display, scale, EMTPY_PRESSABLE);
	}
	
	public ScalableButton(int x, int y, int width, int height, ITextComponent display, float scale, IPressable pessable) {
		this(x, y, width, height, display, scale, pessable, EMPTY_TOOLTIP);
	}
	
	public ScalableButton(int x, int y, int width, int height, ITextComponent display, float scale, ITooltip tooltip) {
		this(x, y, width, height, display, scale, EMTPY_PRESSABLE, tooltip);
	}
	
	public ScalableButton(int x, int y, int width, int height, ITextComponent display, float scale, 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) {
		if (scale == 1) {
			super.renderButton(matrixStack, mouseX, mouseY, partialTicks);
		} else {
			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, getCurrentButtonColor(matrixStack, mouseX, mouseY, partialTicks));
			
			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();
		}
	}
}

@@ -142,53 +142,8 @@
 le;%0A
-import info.u_team.u_team_core.util.GuiUtil;%0A
 impo
@@ -1299,21 +1299,24 @@
 rride%0A%09p
-ublic
+rotected
  void re
@@ -1319,22 +1319,26 @@
 d render
-Button
+Foreground
 (MatrixS
@@ -1354,16 +1354,37 @@
 ixStack,
+ Minecraft minecraft,
  int mou
@@ -1457,22 +1457,26 @@
 r.render
-Button
+Foreground
 (matrixS
@@ -1472,32 +1472,43 @@
 und(matrixStack,
+ minecraft,
  mouseX, mouseY,
@@ -1538,64 +1538,8 @@
 e %7B%0A
-%09%09%09final Minecraft minecraft = Minecraft.getInstance();%0A
 %09%09%09f
@@ -1652,283 +1652,53 @@
 %0A%09%09%09
-GuiUtil.drawContinuousTexturedBox(matrixStack, WIDGETS_LOCATION, x, y, 0, 46 + getYImage(isHovered()) * 20, width, height, 200, 20, 2, 3, 2, 2, 0, getCurrentButtonColor(matrixStack, mouseX, mouseY, partialTicks));%0A%09%09%09%0A%09%09%09renderBg(matrixStack, minecraft, mouseX, mouseY);%0A%09%09%09%0A
+if (message != StringTextComponent.EMPTY) %7B%0A%09
 %09%09%09f
@@ -1791,24 +1791,25 @@
 ssage));%0A%09%09%09
+%09
 final int el
@@ -1882,28 +1882,30 @@
 %22...%22));%0A%09%09%09
+%09
 %0A%09%09%09
+%09
 if (messageW
@@ -1952,24 +1952,25 @@
 sisWidth) %7B%0A
+%09
 %09%09%09%09message 
@@ -2088,14 +2088,17 @@
 %0A%09%09%09
+%09
 %7D%0A%09%09%09
-%0A
+%09%0A%09
 %09%09%09f
@@ -2139,20 +2139,22 @@
 ale;%0A%09%09%09
+%09
 %0A%09%09%09
+%09
 final fl
@@ -2221,16 +2221,17 @@
 Factor;%0A
+%09
 %09%09%09final
@@ -2306,20 +2306,22 @@
 tor;%0A%09%09%09
+%09
 %0A%09%09%09
+%09
 matrixSt
@@ -2332,16 +2332,17 @@
 push();%0A
+%09
 %09%09%09matri
@@ -2372,16 +2372,17 @@
 le, 0);%0A
+%09
 %09%09%09fontR
@@ -2459,16 +2459,17 @@
 ());%0A%09%09%09
+%09
 matrixSt
@@ -2479,16 +2479,21 @@
 .pop();%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%09%7D%0A%7D

Rework scalable button to use the new split method system of ubutton