4242import net .wurstclient .mixin .HandledScreenAccessor ;
4343import net .wurstclient .settings .SliderSetting ;
4444import net .wurstclient .settings .SliderSetting .ValueDisplay ;
45+ import net .wurstclient .util .RenderUtils ;
4546
4647@ SearchTags ({"better book handling" , "book overlay" , "enchanted books" })
4748public final class BetterBookHandlingHack extends Hack
@@ -57,7 +58,6 @@ public final class BetterBookHandlingHack extends Hack
5758 private static final int ENTRY_HOVER_COLOR = 0xFFFFE7A9 ;
5859 private static final int ACTION_COLOR = 0xFF86C5FF ;
5960 private static final int ACTION_HOVER_COLOR = 0xFFB6E1FF ;
60- private static final double HOVER_SCROLL_SPEED = 40.0 ;
6161 private static final long HOVER_SCROLL_DELAY_MS = 400 ;
6262 private static final double HOVER_SCROLL_PAUSE = 32.0 ;
6363
@@ -79,6 +79,9 @@ public final class BetterBookHandlingHack extends Hack
7979 ValueDisplay .INTEGER );
8080 private final SliderSetting textScale = new SliderSetting ("Text scale" , 0.7 ,
8181 0.5 , 1.25 , 0.05 , ValueDisplay .DECIMAL );
82+ private final SliderSetting hoverScrollSpeed = new SliderSetting (
83+ "Hover scroll speed" , "Pixels per second when hovering long entries." ,
84+ 25 , 5 , 80 , 1 , ValueDisplay .INTEGER );
8285
8386 private double scrollOffset ;
8487 private double maxScroll ;
@@ -89,7 +92,7 @@ public final class BetterBookHandlingHack extends Hack
8992 private boolean lastRenderActive ;
9093 private boolean needsRescan = true ;
9194 private int contentHeight ;
92- private BookEntry hoveredEntry ;
95+ private int hoveredSlotId = - 1 ;
9396 private long hoverStartMs ;
9497
9598 public BetterBookHandlingHack ()
@@ -102,6 +105,7 @@ public BetterBookHandlingHack()
102105 addSetting (offsetX );
103106 addSetting (offsetY );
104107 addSetting (textScale );
108+ addSetting (hoverScrollSpeed );
105109
106110 for (BookCategory category : BookCategory .ORDERED )
107111 groupedEntries .put (category , new ArrayList <>());
@@ -118,7 +122,8 @@ protected void onDisable()
118122 entries .clear ();
119123 groupedEntries .values ().forEach (List ::clear );
120124 hitboxes .clear ();
121- hoveredEntry = null ;
125+ hoveredSlotId = -1 ;
126+ hoverStartMs = 0L ;
122127 }
123128
124129 public void renderOnHandledScreen (HandledScreen <?> screen ,
@@ -140,7 +145,8 @@ public void renderOnHandledScreen(HandledScreen<?> screen,
140145 if (entries .isEmpty ())
141146 {
142147 hitboxes .clear ();
143- hoveredEntry = null ;
148+ hoveredSlotId = -1 ;
149+ hoverStartMs = 0L ;
144150 return ;
145151 }
146152
@@ -290,7 +296,9 @@ private void renderOverlay(DrawContext context)
290296 double cursorY = contentTop ;
291297 double offset = scrollOffset ;
292298 boolean anyEntryHovered = false ;
293- int textAreaWidth = Math .max (1 , panelWidth - 2 * PANEL_PADDING - 4 );
299+ double textAreaWidth =
300+ Math .max (1.0 , panelWidth - 2.0 * PANEL_PADDING - 4.0 );
301+ double hoverSpeed = Math .max (1.0 , hoverScrollSpeed .getValueI ());
294302
295303 for (BookCategory category : BookCategory .ORDERED )
296304 {
@@ -334,51 +342,51 @@ private void renderOverlay(DrawContext context)
334342 panelX + panelWidth - 2 , entryY + lineHeight + 2 ,
335343 0x802A2A2A );
336344
337- int textWidth =
338- Math .max (1 , Math .round (tr .getWidth (entry .line ) * scale ));
339- float scrollX = 0 ;
345+ double textWidth =
346+ Math .max (1.0 , (double )tr .getWidth (entry .line ) * scale );
347+ double travel = textWidth - textAreaWidth ;
348+ double scrollX = 0.0 ;
340349 if (hovered )
341350 {
342351 anyEntryHovered = true ;
343- if (entry != hoveredEntry )
352+ if (entry . slotId != hoveredSlotId )
344353 {
345- hoveredEntry = entry ;
354+ hoveredSlotId = entry . slotId ;
346355 hoverStartMs = System .currentTimeMillis ();
347356 }
348357
349- if (textWidth > textAreaWidth )
358+ if (travel > 1.0 )
350359 {
351360 long elapsed =
352361 System .currentTimeMillis () - hoverStartMs ;
353362 if (elapsed > HOVER_SCROLL_DELAY_MS )
354363 {
355- double travel = textWidth - textAreaWidth ;
356- if (travel > 1.0 )
364+ double progress = (elapsed - HOVER_SCROLL_DELAY_MS )
365+ / 1000.0 * hoverSpeed ;
366+ double cycle = travel * 2.0 + HOVER_SCROLL_PAUSE ;
367+ double cyclePos = progress % cycle ;
368+ if (cyclePos <= travel )
369+ scrollX = cyclePos ;
370+ else if (cyclePos <= travel + HOVER_SCROLL_PAUSE )
371+ scrollX = travel ;
372+ else
357373 {
358- double progress =
359- (elapsed - HOVER_SCROLL_DELAY_MS ) / 1000.0
360- * HOVER_SCROLL_SPEED ;
361- double cycle =
362- travel * 2.0 + HOVER_SCROLL_PAUSE ;
363- double cyclePos = progress % cycle ;
364- if (cyclePos <= travel )
365- scrollX = (float )cyclePos ;
366- else if (cyclePos <= travel + HOVER_SCROLL_PAUSE )
367- scrollX = (float )travel ;
368- else
369- {
370- double back =
371- cyclePos - travel - HOVER_SCROLL_PAUSE ;
372- scrollX =
373- (float )Math .max (0.0 , travel - back );
374- }
374+ double back =
375+ cyclePos - travel - HOVER_SCROLL_PAUSE ;
376+ scrollX = Math .max (0.0 , travel - back );
375377 }
376378 }
377379 }
378- }else if (hoveredEntry == entry )
379- hoveredEntry = null ;
380+ scrollX =
381+ MathHelper .clamp (scrollX , 0.0 , Math .max (0.0 , travel ));
382+ }else if (hoveredSlotId == entry .slotId )
383+ {
384+ hoveredSlotId = -1 ;
385+ hoverStartMs = 0L ;
386+ }
380387
381- drawScaledText (context , tr , entry .line , titleX - scrollX ,
388+ float renderScroll = (float )(scrollX / scale );
389+ drawScaledText (context , tr , entry .line , titleX - renderScroll ,
382390 entryY , hovered ? ENTRY_HOVER_COLOR : ENTRY_COLOR , scale );
383391
384392 hitboxes .add (Hitbox .forEntry (panelX + 2 , entryY - 2 ,
@@ -391,7 +399,10 @@ else if(cyclePos <= travel + HOVER_SCROLL_PAUSE)
391399 }
392400
393401 if (!anyEntryHovered )
394- hoveredEntry = null ;
402+ {
403+ hoveredSlotId = -1 ;
404+ hoverStartMs = 0L ;
405+ }
395406
396407 contentHeight = (int )Math .max (0 , Math .round (cursorY - contentTop ));
397408 maxScroll = Math .max (0 , contentHeight - innerHeight );
@@ -577,12 +588,8 @@ private static double getScaledMouseY(DrawContext context)
577588 private static void drawScaledText (DrawContext context , TextRenderer tr ,
578589 String text , float x , float y , int color , float scale )
579590 {
580- var matrices = context .getMatrices ();
581- matrices .pushMatrix ();
582- matrices .translate (x , y );
583- matrices .scale (scale );
584- context .drawText (tr , text , 0 , 0 , color , false );
585- matrices .popMatrix ();
591+ RenderUtils .drawScaledText (context , tr , text , Math .round (x ),
592+ Math .round (y ), color , false , scale );
586593 }
587594
588595 private static String limitLength (String text , int max )
0 commit comments