5
5
import net .minecraft .client .gui .DrawContext ;
6
6
import net .minecraft .client .gui .widget .ClickableWidget ;
7
7
import net .minecraft .text .Text ;
8
- import net .minecraft .util .math .ColorHelper ;
9
8
import org .joml .Matrix4f ;
10
9
import org .joml .Vector4f ;
10
+ import smartin .miapi .client .gui .crafting .CraftingScreen ;
11
11
12
12
import java .util .List ;
13
13
@@ -24,6 +24,10 @@ public class ScrollList extends InteractAbleWidget {
24
24
private int maxScrollAmount ;
25
25
public boolean saveMode = true ;
26
26
boolean needsScrollbar = false ;
27
+ public boolean alwaysEnableScrollbar = false ;
28
+ int barWidth = 8 ;
29
+ boolean scrollbarDragged = false ;
30
+ public boolean altDesign = false ;
27
31
28
32
/**
29
33
* 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)
84
88
if (startY + widget .getHeight () >= this .getY () && startY <= this .getY () + this .height - 1 ) {
85
89
widget .setY (startY );
86
90
widget .setX (this .getX ());
87
- if (needsScrollbar ) {
88
- widget .setWidth (this .width - 5 );
91
+ if (showScrollbar () ) {
92
+ widget .setWidth (this .width - barWidth );
89
93
} else {
90
94
widget .setWidth (this .width );
91
95
}
@@ -100,15 +104,39 @@ public void render(DrawContext drawContext, int mouseX, int mouseY, float delta)
100
104
startY += widget .getHeight ();
101
105
}
102
106
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 );
109
112
}
110
113
}
111
114
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
+
112
140
@ Override
113
141
public void renderHover (DrawContext drawContext , int mouseX , int mouseY , float delta ) {
114
142
if (isMouseOver (mouseX , mouseY )) {
@@ -166,10 +194,11 @@ public boolean mouseClicked(double mouseX, double mouseY, int button) {
166
194
}
167
195
168
196
boolean clicked = false ;
169
- if (needsScrollbar ) {
197
+ if (showScrollbar () ) {
170
198
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 ;
173
202
}
174
203
}
175
204
}
@@ -186,11 +215,37 @@ public boolean mouseClicked(double mouseX, double mouseY, int button) {
186
215
return false ;
187
216
}
188
217
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
+
189
236
@ Override
190
237
public boolean mouseReleased (double mouseX , double mouseY , int button ) {
191
238
if (this .widgets == null ) {
192
239
return false ;
193
240
}
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
+ }
194
249
195
250
boolean released = false ;
196
251
0 commit comments