55import net .minecraft .client .gui .GuiGraphics ;
66import net .minecraft .client .gui .components .AbstractWidget ;
77import net .minecraft .client .gui .components .ContainerObjectSelectionList ;
8- import net .minecraft .client .gui .components .Tooltip ;
98import net .minecraft .client .gui .components .events .GuiEventListener ;
109import net .minecraft .client .gui .narration .NarratableEntry ;
11- import net .minecraft .util .Mth ;
1210import org .jetbrains .annotations .NotNull ;
13- import org .jetbrains .annotations .Nullable ;
1411
1512import java .util .List ;
1613
@@ -21,112 +18,43 @@ public class WidgetList extends ContainerObjectSelectionList<WidgetList.Entry> {
2118 public static final int PADDING = 4 ;
2219
2320 public WidgetList (Minecraft minecraft , int width , int height , int y ) {
24- super (minecraft , width , height , y , 1 );
21+ super (minecraft , width , height , y , 20 );
2522 this .centerListVertically = false ;
26- this .headerHeight = PADDING ;
2723 }
2824
2925 public void addWidgetGroup (List <AbstractWidget > widgets ) {
3026 this .addEntry (new Entry (widgets ));
3127 }
3228
3329 public void updateWidgetGroup (int index , List <AbstractWidget > widgets ) {
34- this .children ().set (index , new Entry (widgets ));
30+ this .children ().get (index ).updateChildren (widgets );
31+ this .swap (index , index ); // Force reposition of entries
3532 }
3633
3734 @ Override
3835 public int getRowWidth () {
3936 return this .width ;
4037 }
4138
42- @ Nullable
43- public Entry getRealEntryAtPosition (double mouseX , double mouseY ) {
44- int halfRowWidth = this .getRowWidth () / 2 ;
45- int centerX = this .getX () + this .width / 2 ;
46- int left = centerX - halfRowWidth ;
47- int right = centerX + halfRowWidth ;
48- int m = Mth .floor (mouseY - (double ) this .getY ()) - this .headerHeight + (int ) this .scrollAmount () - 4 ;
49-
50- if (mouseX < left || mouseX > right || m < 0 ) {
51- return null ;
52- }
53-
54- int height = 0 ;
55-
56- for (int idx = 0 ; idx < this .getItemCount (); idx ++) {
57- Entry entry = this .getEntry (idx );
58- height += entry .getHeight () + PADDING ;
59- if (m < height ) {
60- return entry ;
61- }
62- }
63-
64- return null ;
65- }
66-
67- @ Override
68- protected void renderListItems (GuiGraphics graphics , int mouseX , int mouseY , float partialTick ) {
69- for (int itemIdx = 0 ; itemIdx < this .getItemCount (); ++itemIdx ) {
70- int top = this .getRowTop (itemIdx );
71- int bottom = this .getRowBottom (itemIdx );
72- if (bottom >= this .getY () && top <= this .getBottom ()) {
73- this .renderItem (graphics , mouseX , mouseY , partialTick , itemIdx , this .getRowLeft (), top , this .getRowWidth (), this .getEntry (itemIdx ).getHeight ());
74- }
75- }
76- }
77-
78- @ Override
79- protected int contentHeight () {
80- int itemsHeight = 0 ;
81-
82- for (int i = 0 ; i < this .getItemCount (); i ++) {
83- itemsHeight += this .getEntry (i ).getHeight () + PADDING ;
84- }
85-
86- return itemsHeight + this .headerHeight ;
87- }
88-
89- @ Override
90- public int getRowTop (int index ) {
91- int itemsHeight = 0 ;
92-
93- for (int i = 0 ; i < index ; i ++) {
94- itemsHeight += this .getEntry (i ).getHeight () + PADDING ;
95- }
96-
97- return this .getY () - (int ) this .scrollAmount () + itemsHeight + this .headerHeight ;
98- }
99-
100- @ Override
101- public int getRowBottom (int index ) {
102- return this .getRowTop (index ) + this .getEntry (index ).getHeight ();
103- }
104-
105- @ Override
106- protected double scrollRate () {
107- return 10 ;
108- }
109-
11039 @ Override
11140 protected int scrollBarX () {
11241 return this .getX () + this .width - 6 ;
11342 }
11443
11544 public static class Entry extends ContainerObjectSelectionList .Entry <Entry > {
116- private final List <AbstractWidget > children ;
117- private final List <Integer > childYs ;
45+ private List <AbstractWidget > children ;
46+ private List <Integer > childYs ;
11847
11948 private Entry (List <AbstractWidget > list ) {
120- this .children = ImmutableList .copyOf (list );
121- this .childYs = this .children .stream ().map (AbstractWidget ::getY ).toList ();
49+ this .updateChildren (list );
12250 }
12351
12452 @ Override
125- public void render (GuiGraphics guiGraphics , int index , int top , int left , int width , int height , int mouseX , int mouseY , boolean hovering , float partialTick ) {
53+ public void renderContent (GuiGraphics guiGraphics , int mouseX , int mouseY , boolean hovered , float partialTick ) {
12654 for (int i = 0 ; i < this .children .size (); i ++) {
12755 AbstractWidget widget = this .children .get (i );
12856 int relativeY = this .childYs .get (i );
129- widget .setY (top + relativeY );
57+ widget .setY (PADDING + this . getY () + relativeY );
13058 widget .render (guiGraphics , mouseX , mouseY , partialTick );
13159 }
13260 }
@@ -140,7 +68,13 @@ public int getHeight() {
14068 maxY = Math .max (relativeY + widget .getHeight (), maxY );
14169 }
14270
143- return maxY ;
71+ return maxY + PADDING ;
72+ }
73+
74+ private void updateChildren (List <AbstractWidget > children ) {
75+ System .out .println ("Updating children " + this .children + " with " + children );
76+ this .children = ImmutableList .copyOf (children );
77+ this .childYs = this .children .stream ().map (AbstractWidget ::getY ).toList ();
14478 }
14579
14680 @ Override
0 commit comments