55import net .minecraft .client .gui .DrawContext ;
66import net .minecraft .client .gui .widget .ClickableWidget ;
77import net .minecraft .text .Text ;
8- import net .minecraft .util .math .ColorHelper ;
98import org .joml .Matrix4f ;
109import org .joml .Vector4f ;
10+ import smartin .miapi .client .gui .crafting .CraftingScreen ;
1111
1212import java .util .List ;
1313
@@ -24,6 +24,10 @@ public class ScrollList extends InteractAbleWidget {
2424 private int maxScrollAmount ;
2525 public boolean saveMode = true ;
2626 boolean needsScrollbar = false ;
27+ public boolean alwaysEnableScrollbar = false ;
28+ int barWidth = 8 ;
29+ boolean scrollbarDragged = false ;
30+ public boolean altDesign = false ;
2731
2832 /**
2933 * Constructs a new ScrollList with the given x and y coordinates, width, height,
@@ -84,8 +88,8 @@ public void render(DrawContext drawContext, int mouseX, int mouseY, float delta)
8488 if (startY + widget .getHeight () >= this .getY () && startY <= this .getY () + this .height - 1 ) {
8589 widget .setY (startY );
8690 widget .setX (this .getX ());
87- if (needsScrollbar ) {
88- widget .setWidth (this .width - 5 );
91+ if (showScrollbar () ) {
92+ widget .setWidth (this .width - barWidth );
8993 } else {
9094 widget .setWidth (this .width );
9195 }
@@ -100,15 +104,39 @@ public void render(DrawContext drawContext, int mouseX, int mouseY, float delta)
100104 startY += widget .getHeight ();
101105 }
102106
103- if (needsScrollbar ) {
104- int barHeight = Math .max (10 , this .height * this .height / (this .maxScrollAmount + this .height ));
105- int barY = this .getY () + 1 + (int ) ((this .height - barHeight - 2 ) * (float ) this .scrollAmount / this .maxScrollAmount );
106- int barX = this .getX () + this .width - 5 ;
107- drawContext .fill (barX , getY (), barX + 5 , this .getY () + this .height , 0xFFCCCCCC );
108- drawContext .fill (barX , barY , barX + 5 , barY + barHeight , ColorHelper .Argb .getArgb (255 , 50 , 50 , 50 ));
107+ if (showScrollbar ()) {
108+ int barX = this .getX () + this .width - barWidth ;
109+ float percentage = (float ) this .scrollAmount / (float ) maxScrollAmount ;
110+ renderScrollbarBackground (drawContext , mouseX , mouseY , delta , barX , barWidth );
111+ renderScrollbarClickAble (drawContext , mouseX , mouseY , delta , barX , barWidth , percentage );
109112 }
110113 }
111114
115+ public void renderScrollbarBackground (DrawContext drawContext , int mouseX , int mouseY , float delta , int barX , int barWidth ) {
116+ int offsetAlt = 0 ;
117+ if (altDesign ) {
118+ offsetAlt = 28 ;
119+ }
120+ drawTextureWithEdge (drawContext , CraftingScreen .BACKGROUND_TEXTURE , barX , getY (), 498 - offsetAlt , 96 , 14 , 15 , barWidth , getHeight (), 512 , 512 , 3 );
121+ }
122+
123+ public void renderScrollbarClickAble (DrawContext drawContext , int mouseX , int mouseY , float delta , int barX , int barWidth , float percent ) {
124+ int height = (int ) ((this .getHeight () - 17 ) * percent ) + 1 + getY ();
125+ int offset = 0 ;
126+ if (!needsScrollbar ) {
127+ offset = 16 ;
128+ }
129+ int offsetAlt = 0 ;
130+ if (altDesign ) {
131+ offsetAlt = 28 ;
132+ }
133+ drawTextureWithEdge (drawContext , CraftingScreen .BACKGROUND_TEXTURE , barX , height , 498 - 14 - offsetAlt , 96 + offset , 14 , 15 , barWidth , 15 , 512 , 512 , 3 );
134+ }
135+
136+ private boolean showScrollbar () {
137+ return needsScrollbar || alwaysEnableScrollbar ;
138+ }
139+
112140 @ Override
113141 public void renderHover (DrawContext drawContext , int mouseX , int mouseY , float delta ) {
114142 if (isMouseOver (mouseX , mouseY )) {
@@ -166,10 +194,11 @@ public boolean mouseClicked(double mouseX, double mouseY, int button) {
166194 }
167195
168196 boolean clicked = false ;
169- if (needsScrollbar ) {
197+ if (showScrollbar () ) {
170198 if (isMouseOver (mouseX , mouseY )) {
171- if (mouseY > this .getX () + this .width - 5 && mouseY < this .getX () + this .width ) {
172- //TODO:drag motion?
199+ if (mouseX > this .getX () + this .width - barWidth && mouseX < this .getX () + this .width ) {
200+ scrollbarDragged = true ;
201+ return true ;
173202 }
174203 }
175204 }
@@ -186,11 +215,37 @@ public boolean mouseClicked(double mouseX, double mouseY, int button) {
186215 return false ;
187216 }
188217
218+ @ Override
219+ public boolean mouseDragged (double mouseX , double mouseY , int button , double deltaX , double deltaY ) {
220+ if (mouseX > this .getX () + this .width - barWidth && mouseX < this .getX () + this .width ) {
221+ if (mouseY < (double ) this .getY ()) {
222+ this .scrollAmount = 0 ;
223+ return true ;
224+ } else if (mouseY > (double ) (this .getY () + this .height )) {
225+ this .scrollAmount = maxScrollAmount ;
226+ return true ;
227+ } else {
228+ double i = Math .min (1 , Math .max (0 , (mouseY - getY ()) / (getHeight () - 10 )));
229+ this .scrollAmount = (int ) (i * maxScrollAmount );
230+ return true ;
231+ }
232+ }
233+ return super .mouseDragged (mouseX , mouseY , button , deltaX , deltaY );
234+ }
235+
189236 @ Override
190237 public boolean mouseReleased (double mouseX , double mouseY , int button ) {
191238 if (this .widgets == null ) {
192239 return false ;
193240 }
241+ if (showScrollbar ()) {
242+ if (isMouseOver (mouseX , mouseY )) {
243+ if (mouseX > this .getX () + this .width - barWidth && mouseX < this .getX () + this .width ) {
244+ scrollbarDragged = false ;
245+ return true ;
246+ }
247+ }
248+ }
194249
195250 boolean released = false ;
196251
0 commit comments