11package dev .isxander .yacl3 .gui ;
22
33import com .mojang .blaze3d .platform .InputConstants ;
4+ import dev .isxander .yacl3 .mixin .AbstractSelectionListAccessor ;
45import net .minecraft .client .Minecraft ;
56import net .minecraft .client .gui .GuiGraphics ;
67import net .minecraft .client .gui .components .AbstractWidget ;
1718public class ElementListWidgetExt <E extends ElementListWidgetExt .Entry <E >> extends ContainerObjectSelectionList <E > implements LayoutElement {
1819 protected static final int SCROLLBAR_WIDTH = 6 ;
1920
20- private double smoothScrollAmount = getScrollAmount ();
21+ private double smoothScrollAmount = scrollAmount ();
2122 private boolean returnSmoothAmount = false ;
2223 private final boolean doSmoothScrolling ;
2324 private boolean usingScrollbar ;
2425
2526 public ElementListWidgetExt (Minecraft client , int x , int y , int width , int height , boolean smoothScrolling ) {
26- /*? if >1.20.2 {*/
27- super (client , width , x , y , height );
28- /*?} else {*/
27+ //? if >=1.21.4 {
28+ super (client , width , x , y , height , 0 );
29+ ((AbstractSelectionListAccessor ) this ).setRenderHeader (false );
30+ //?} elif >=1.20.3 {
31+ /*super(client, width, x, y, height);
32+ */ //?} else {
2933 /*super(client, width, height, y, y + height, 22);
3034 this.x0 = x;
3135 this.x1 = x + width;
32- */ /*?}*/
36+ */ //?}
3337 this .doSmoothScrolling = smoothScrolling ;
34- setRenderHeader (false , 0 );
38+
39+ //? if <1.21.4
40+ /*setRenderHeader(false, 0);*/
3541 }
3642
3743 @ Override
@@ -42,16 +48,35 @@ public boolean mouseScrolled(double mouseX, double mouseY, /*? if >1.20.2 {*/ do
4248 /*?}*/
4349
4450 // default implementation bases scroll step from total height of entries, this is constant
45- this .setScrollAmount (this .getScrollAmount () - scroll * 20 );
51+ this .setScrollAmount (this .scrollAmount () - scroll * 20 );
4652 return true ;
4753 }
4854
55+ //? if <1.21.4 {
56+ /*protected int scrollBarX() {
57+ return this.getScrollbarPosition();
58+ }
4959 @Override
5060 protected int getScrollbarPosition() {
61+ */ //?} else {
62+ @ Override
63+ protected int scrollBarX () {
64+ //?}
5165 // default implementation does not respect left/right
5266 return this .getX () + this .getWidth () - SCROLLBAR_WIDTH ;
5367 }
5468
69+ //? if >=1.21.4 {
70+ @ Override
71+ protected int scrollBarY () {
72+ return Math .max (this .getY (), (int )scrollAmount () * (this .height - this .scrollerHeight ()) / this .maxScrollAmount () + this .getY ());
73+ }
74+ //?} else {
75+ /*protected int maxScrollAmount() {
76+ return this.getMaxPosition();
77+ }
78+ */ //?}
79+
5580 @ Override
5681 /*? if >1.20.2 {*/
5782 public void renderWidget (GuiGraphics graphics , int mouseX , int mouseY , float delta )
@@ -66,7 +91,7 @@ public void renderWidget(GuiGraphics graphics, int mouseX, int mouseY, float del
6691 smoothScrollAmount = Mth .lerp (
6792 delta * 0.5 ,
6893 smoothScrollAmount ,
69- getScrollAmount ()
94+ scrollAmount ()
7095 );
7196 returnSmoothAmount = true ;
7297
@@ -84,16 +109,16 @@ public void renderWidget(GuiGraphics graphics, int mouseX, int mouseY, float del
84109 returnSmoothAmount = false ;
85110 }
86111
87- /*? if >1.20.1 {*/
88- @ Override
89- /*?}*/
112+ /*? if >1.20.1 && <1.21.4 {*/
113+ /* @Override
114+ */ /*?}*/
90115 protected boolean isValidMouseClick (int button ) {
91116 return button == InputConstants .MOUSE_BUTTON_LEFT || button == InputConstants .MOUSE_BUTTON_RIGHT || button == InputConstants .MOUSE_BUTTON_MIDDLE ;
92117 }
93118
94119 @ Override
95120 public boolean mouseClicked (double mouseX , double mouseY , int button ) {
96- if (button == 0 && mouseX >= getScrollbarPosition () && mouseX < getScrollbarPosition () + SCROLLBAR_WIDTH ) {
121+ if (button == 0 && mouseX >= scrollBarX () && mouseX < scrollBarX () + SCROLLBAR_WIDTH ) {
97122 usingScrollbar = true ;
98123 }
99124
@@ -120,22 +145,36 @@ public void updateDimensions(ScreenRectangle rectangle) {
120145 * awful code to only use smooth scroll state when rendering,
121146 * not other code that needs target scroll amount
122147 */
148+ //? if <1.21.4 {
149+ /*// backwards compatible method to reduce stonecutter comments
150+ protected double scrollAmount() {
151+ return this.getScrollAmount();
152+ }
153+
123154 @Override
124155 public double getScrollAmount() {
156+ */ //?} else {
157+ @ Override
158+ public double scrollAmount () {
159+ //?}
125160 if (returnSmoothAmount && doSmoothScrolling )
126161 return smoothScrollAmount ;
127162
128- return super .getScrollAmount ();
163+ //? if >=1.21.4 {
164+ return super .scrollAmount ();
165+ //?} else {
166+ /*return super.getScrollAmount();
167+ */ //?}
129168 }
130169
131170 protected void resetSmoothScrolling () {
132- this .smoothScrollAmount = super .getScrollAmount ();
171+ this .smoothScrollAmount = /*? if >=1.21.4 {*/ super .scrollAmount () /*?} else {*/ /*super. getScrollAmount()*/ /*?}*/ ;
133172 }
134173
135174 @ Nullable
136175 @ Override
137176 protected E getEntryAtPosition (double x , double y ) {
138- y += getScrollAmount ();
177+ y += scrollAmount ();
139178
140179 if (x < this .getX () || x > this .getX () + this .getWidth ())
141180 return null ;
@@ -159,8 +198,16 @@ protected E getEntryAtPosition(double x, double y) {
159198 code is responsible for having dynamic item heights
160199 */
161200
201+ //? if <1.21.4 {
202+ /*protected int contentHeight() {
203+ return this.getMaxPosition();
204+ }
162205 @Override
163206 protected int getMaxPosition() {
207+ */ //?} else {
208+ @ Override
209+ protected int contentHeight () {
210+ //?}
164211 return children ().stream ().map (E ::getItemHeight ).reduce (0 , Integer ::sum ) + headerHeight ;
165212 }
166213
@@ -175,7 +222,7 @@ protected void centerScrollOn(E entry) {
175222 @ Override
176223 /*? if >=1.21.2 {*/ public /*?} else {*/ /*protected */ /*?}*/
177224 int getRowTop (int index ) {
178- int integer = getY () + 4 - (int ) this .getScrollAmount () + headerHeight ;
225+ int integer = getY () + 4 - (int ) this .scrollAmount () + headerHeight ;
179226 for (int i = 0 ; i < children ().size () && i < index ; i ++)
180227 integer += children ().get (i ).getItemHeight ();
181228 return integer ;
@@ -186,12 +233,12 @@ protected void ensureVisible(E entry) {
186233 int i = this .getRowTop (this .children ().indexOf (entry ));
187234 int j = i - this .getY () - 4 - entry .getItemHeight ();
188235 if (j < 0 ) {
189- this .setScrollAmount (this .getScrollAmount () + j );
236+ this .setScrollAmount (this .scrollAmount () + j );
190237 }
191238
192239 int k = this .getY () + this .getHeight () - i - entry .getItemHeight () * 2 ;
193240 if (k < 0 ) {
194- this .setScrollAmount (this .getScrollAmount () - k );
241+ this .setScrollAmount (this .scrollAmount () - k );
195242 }
196243 }
197244
0 commit comments