11package io .github .jamalam360 .jamlib .client .gui ;
22
3+ import net .minecraft .Util ;
34import net .minecraft .client .Minecraft ;
45import net .minecraft .client .gui .Font ;
56import net .minecraft .client .gui .GuiGraphics ;
67import net .minecraft .client .gui .components .StringWidget ;
78import net .minecraft .network .chat .Component ;
9+ import net .minecraft .util .Mth ;
810
911/**
1012 * A string widget that scrolls if the component is too long for the width
@@ -16,10 +18,28 @@ public ScrollingStringWidget(int x, int y, int width, int height, Component comp
1618
1719 @ Override
1820 public void renderWidget (GuiGraphics guiGraphics , int mouseX , int mouseY , float partialTick ) {
19- this . renderScrollingString (guiGraphics , this .getFont (), 2 , this .getColor ());
21+ renderLeftAlignedScrollingString (guiGraphics , this .getFont (), this . getMessage (), this . getX (), this . getWidth (), this . getY () , this .getColor ());
2022
2123 if (this .isMouseOver (mouseX , mouseY ) && this .getTooltip () != null ) {
2224 guiGraphics .renderTooltip (Minecraft .getInstance ().font , this .getTooltip ().toCharSequence (Minecraft .getInstance ()), mouseX , mouseY );
2325 }
2426 }
27+
28+ private static void renderLeftAlignedScrollingString (GuiGraphics graphics , Font font , Component text , int minX , int width , int y , int color ) {
29+ int messageWidth = font .width (text );
30+
31+ if (messageWidth > width ) {
32+ int overflowWidth = messageWidth - width ;
33+ double d = (double ) Util .getMillis () / (double )1000.0F ;
34+ double e = Math .max ((double )overflowWidth * (double )0.5F , 3.0F );
35+ double f = Math .sin ((Math .PI / 2D ) * Math .cos ((Math .PI * 2D ) * d / e )) / (double )2.0F + (double )0.5F ;
36+ double g = Mth .lerp (f , 0.0F , overflowWidth );
37+ graphics .enableScissor (minX , y , minX + width , y + font .lineHeight );
38+ graphics .drawString (font , text , minX - (int )g , y , color );
39+ graphics .disableScissor ();
40+ } else {
41+ graphics .drawString (font , text , minX , y , color );
42+ }
43+
44+ }
2545}
0 commit comments