@@ -19,9 +19,14 @@ public final class ComboBoxPopup<T extends Enum<T>> extends Popup
1919{
2020 private static final ClickGui GUI = WurstClient .INSTANCE .getGui ();
2121 private static final TextRenderer TR = WurstClient .MC .textRenderer ;
22+ private static final int ROW_HEIGHT = 11 ;
23+ private static final int MAX_VISIBLE_ROWS = 8 ;
2224
2325 private final EnumSetting <T > setting ;
2426 private final int popupWidth ;
27+ private final int totalRows ;
28+ private final int visibleRows ;
29+ private int scrollOffset ;
2530
2631 public ComboBoxPopup (Component owner , EnumSetting <T > setting ,
2732 int popupWidth )
@@ -30,6 +35,9 @@ public ComboBoxPopup(Component owner, EnumSetting<T> setting,
3035 this .setting = setting ;
3136 this .popupWidth = popupWidth ;
3237
38+ totalRows = Math .max (0 , setting .getValues ().length - 1 );
39+ visibleRows = Math .min (MAX_VISIBLE_ROWS , totalRows );
40+
3341 setWidth (getDefaultWidth ());
3442 setHeight (getDefaultHeight ());
3543
@@ -40,30 +48,47 @@ public ComboBoxPopup(Component owner, EnumSetting<T> setting,
4048 @ Override
4149 public void handleMouseClick (int mouseX , int mouseY , int mouseButton )
4250 {
43- if (mouseButton != GLFW .GLFW_MOUSE_BUTTON_LEFT )
51+ if (mouseButton != GLFW .GLFW_MOUSE_BUTTON_LEFT || visibleRows <= 0 )
4452 return ;
4553
46- int yi1 = getY () - 11 ;
47- for (T value : setting .getValues ())
48- {
49- if (value == setting .getSelected ())
50- continue ;
51-
52- yi1 += 11 ;
53- int yi2 = yi1 + 11 ;
54-
55- if (mouseY < yi1 || mouseY >= yi2 )
56- continue ;
57-
58- setting .setSelected (value );
59- close ();
60- break ;
61- }
54+ int localX = mouseX - getX ();
55+ int localY = mouseY - getY ();
56+ if (localX < 0 || localX >= getWidth () || localY < 0
57+ || localY >= getHeight ())
58+ return ;
59+
60+ int row = localY / ROW_HEIGHT ;
61+ T value = getValueAt (row + scrollOffset );
62+ if (value == null )
63+ return ;
64+
65+ setting .setSelected (value );
66+ close ();
67+ }
68+
69+ @ Override
70+ public boolean handleMouseScroll (int mouseX , int mouseY , double delta )
71+ {
72+ if (totalRows <= visibleRows || visibleRows <= 0 )
73+ return false ;
74+
75+ int direction = (int )Math .signum (delta );
76+ if (direction == 0 )
77+ return false ;
78+
79+ scrollOffset -= direction ;
80+ clampScroll ();
81+ return true ;
6282 }
6383
6484 @ Override
6585 public void render (DrawContext context , int mouseX , int mouseY )
6686 {
87+ if (visibleRows <= 0 )
88+ return ;
89+
90+ clampScroll ();
91+
6792 int x1 = getX ();
6893 int x2 = x1 + getWidth ();
6994 int y1 = getY ();
@@ -77,14 +102,21 @@ public void render(DrawContext context, int mouseX, int mouseY)
77102 RenderUtils .drawBorder2D (context , x1 , y1 , x2 , y2 ,
78103 RenderUtils .toIntColor (GUI .getAcColor (), 0.5F ));
79104
80- int yi1 = y1 - 11 ;
105+ int drawn = 0 ;
106+ int skipped = 0 ;
81107 for (T value : setting .getValues ())
82108 {
83109 if (value == setting .getSelected ())
84110 continue ;
85111
86- yi1 += 11 ;
87- int yi2 = yi1 + 11 ;
112+ if (skipped ++ < scrollOffset )
113+ continue ;
114+
115+ if (drawn >= visibleRows )
116+ break ;
117+
118+ int yi1 = y1 + drawn * ROW_HEIGHT ;
119+ int yi2 = yi1 + ROW_HEIGHT ;
88120
89121 boolean hValue = hovering && mouseY >= yi1 && mouseY < yi2 ;
90122 context .fill (x1 , yi1 , x2 , yi2 , RenderUtils .toIntColor (
@@ -93,9 +125,40 @@ public void render(DrawContext context, int mouseX, int mouseY)
93125 context .state .goUpLayer ();
94126 context .drawText (TR , value .toString (), x1 + 2 , yi1 + 2 ,
95127 GUI .getTxtColor (), false );
128+
129+ drawn ++;
96130 }
97131 }
98132
133+ private void clampScroll ()
134+ {
135+ int maxOffset = Math .max (0 , totalRows - visibleRows );
136+ if (scrollOffset < 0 )
137+ scrollOffset = 0 ;
138+ else if (scrollOffset > maxOffset )
139+ scrollOffset = maxOffset ;
140+ }
141+
142+ private T getValueAt (int index )
143+ {
144+ if (index < 0 )
145+ return null ;
146+
147+ int skipped = 0 ;
148+ for (T value : setting .getValues ())
149+ {
150+ if (value == setting .getSelected ())
151+ continue ;
152+
153+ if (skipped == index )
154+ return value ;
155+
156+ skipped ++;
157+ }
158+
159+ return null ;
160+ }
161+
99162 private boolean isHovering (int mouseX , int mouseY , int x1 , int x2 , int y1 ,
100163 int y2 )
101164 {
@@ -111,7 +174,6 @@ public int getDefaultWidth()
111174 @ Override
112175 public int getDefaultHeight ()
113176 {
114- int numValues = setting .getValues ().length ;
115- return (numValues - 1 ) * 11 ;
177+ return visibleRows * ROW_HEIGHT ;
116178 }
117179}
0 commit comments