package de.halfreal.spezi.gdx.view;

import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;

public class AutoScalingLabel extends Label {

	public AutoScalingLabel(CharSequence text, LabelStyle style) {
		super(text, style);
		setText(text);
	}

	public AutoScalingLabel(CharSequence text, Skin skin, String styleName) {
		super(text, skin, styleName);
		setText(text);
	}

	@Override
	public void setText(CharSequence newText) {
		super.setText(newText);
		float oldFontScale = getStyle().font.getScaleX();
		float width = getPrefWidth();
		if (width > getWidth()) {
			float scaleFactor = getWidth() / width;
			setFontScale(scaleFactor * oldFontScale);
		}

		oldFontScale = getStyle().font.getScaleY();
		float height = getPrefHeight();
		if (height > getHeight()) {
			float scaleFactor = getHeight() / height;
			setFontScale(scaleFactor * oldFontScale);
		}
	}

}

@@ -178,25 +178,28 @@
  Label %7B%0D%0A%0D%0A
-%09
+    
 public AutoS
@@ -246,26 +246,32 @@
 e style) %7B%0D%0A
-%09%09
+        
 super(text, 
@@ -275,26 +275,32 @@
 t, style);%0D%0A
-%09%09
+        
 setText(text
@@ -303,23 +303,29 @@
 text);%0D%0A
-%09
+    
 %7D%0D%0A%0D%0A
-%09
+    
 public A
@@ -387,26 +387,32 @@
 yleName) %7B%0D%0A
-%09%09
+        
 super(text, 
@@ -430,18 +430,24 @@
 Name);%0D%0A
-%09%09
+        
 setText(
@@ -454,23 +454,29 @@
 text);%0D%0A
-%09
+    
 %7D%0D%0A%0D%0A
-%09
+    
 @Overrid
@@ -478,17 +478,20 @@
 erride%0D%0A
-%09
+    
 public v
@@ -527,18 +527,24 @@
 ext) %7B%0D%0A
-%09%09
+        
 super.se
@@ -556,26 +556,32 @@
 (newText);%0D%0A
-%09%09
+        
 float oldFon
@@ -596,37 +596,101 @@
  get
-Sty
+FontSca
 le
+X
 ()
-.font.getScaleX();%0D%0A%09%09
+;%0D%0A        float scaleFactorX = 1f;%0D%0A        float scaleFactorY = 1f;%0D%0A%0D%0A        
 floa
@@ -712,26 +712,32 @@
 efWidth();%0D%0A
-%09%09
+        
 if (width %3E 
@@ -747,32 +747,35 @@
 Width()) %7B%0D%0A
-%09%09%09float
+           
  scaleFactor
@@ -770,24 +770,25 @@
  scaleFactor
+X
  = getWidth(
@@ -803,110 +803,29 @@
 h;%0D%0A
-%09%09%09setFontScale(scaleFactor * oldFontScale);%0D%0A%09%09%7D%0D%0A%0D%0A%09%09oldFontScale = getStyle().font.getScaleY();%0D%0A%09%09
+        %7D%0D%0A%0D%0A        
 floa
@@ -853,18 +853,24 @@
 ght();%0D%0A
-%09%09
+        
 if (heig
@@ -894,16 +894,19 @@
  %7B%0D%0A
-%09%09%09float
+           
  sca
@@ -913,16 +913,17 @@
 leFactor
+Y
  = getHe
@@ -944,63 +944,144 @@
 t;%0D%0A
-%09%09%09setFontScale(scaleFactor * oldFontScale);%0D%0A%09%09%7D%0D%0A%09%7D%0D%0A
+        %7D%0D%0A%0D%0A        float scaleFactor = Math.min(scaleFactorX, scaleFactorY);%0D%0A        setFontScale(oldFontScale * scaleFactor);%0D%0A    %7D
 %0D%0A%7D%0D

Fix font scaling