77
88public class ScrollableList extends Gidget {
99 private final List <Gidget > gidgets ;
10- private double scrollY ;
10+ private final Scrollbar scrollbar ;
1111
1212 public ScrollableList (List <Gidget > gidgets , int x , int y , int width , int height ) {
1313 super (x , y , width , height );
1414 this .gidgets = gidgets ;
15- this .scrollY = 0.0F ;
15+ this .scrollbar = new Scrollbar ( width - Scrollbar . DEFAULT_WIDTH - 8 , y , height ) ;
1616 for (Gidget gidget : this .gidgets ) {
1717 gidget .reposition (this .x () + gidget .x (), this .y () + gidget .y () + 4 );
1818 }
@@ -21,6 +21,7 @@ public ScrollableList(List<Gidget> gidgets, int x, int y, int width, int height)
2121 @ Override
2222 public void render (GuiGraphics guiGraphics , int mouseX , int mouseY ) {
2323 this .renderBackground (guiGraphics );
24+ this .scrollbar .render (guiGraphics , mouseX , mouseY );
2425 guiGraphics .enableScissor (this .x (), this .y (), this .x () + this .width (), this .y () + this .height ());
2526 for (Gidget gidget : this .gidgets ) {
2627 gidget .render (guiGraphics , mouseX , mouseY );
@@ -43,6 +44,10 @@ public void onMouseMove(double mouseX, double mouseY) {
4344 gidget .onMouseMove (mouseX , mouseY );
4445 }
4546 }
47+
48+ if (this .scrollbar .isInside (mouseX , mouseY )) {
49+ this .scrollbar .onMouseMove (mouseX , mouseY );
50+ }
4651 }
4752
4853 @ Override
@@ -53,5 +58,64 @@ public void onMouseClicked(double mouseX, double mouseY) {
5358 gidget .onMouseClicked (mouseX , mouseY );
5459 }
5560 }
61+
62+ if (this .scrollbar .isInside (mouseX , mouseY )) {
63+ this .scrollbar .onMouseClicked (mouseX , mouseY );
64+ }
65+ }
66+
67+ private static class Scrollbar extends Gidget {
68+ public static final int DEFAULT_WIDTH = 10 ;
69+ private final Knob knob ;
70+ private double scrollY ;
71+
72+ protected Scrollbar (int x , int y , int height ) {
73+ super (x , y , DEFAULT_WIDTH , height );
74+ this .knob = new Knob (x , y , Knob .DEFAULT_HEIGHT );
75+ this .scrollY = 0.0 ;
76+ }
77+
78+ @ Override
79+ public void render (GuiGraphics guiGraphics , int mouseX , int mouseY ) {
80+ this .renderBackground (guiGraphics );
81+ this .knob .render (guiGraphics , mouseX , mouseY );
82+ }
83+
84+ @ Override
85+ public void renderBackground (GuiGraphics guiGraphics ) {
86+ guiGraphics .fill (this .x (), this .y (), this .x () + this .width (), this .y () + this .height (), ARGB .color (0.5F , 0x00FF95 ));
87+ }
88+
89+ @ Override
90+ public void onMouseClicked (double mouseX , double mouseY ) {
91+ super .onMouseClicked (mouseX , mouseY );
92+ }
93+
94+ @ Override
95+ public void onMouseMove (double mouseX , double mouseY ) {
96+ super .onMouseMove (mouseX , mouseY );
97+ }
98+
99+ public double getScrollY () {
100+ return this .scrollY ;
101+ }
102+
103+ private class Knob extends Gidget {
104+ public static final int DEFAULT_HEIGHT = 30 ;
105+
106+ public Knob (int x , int y , int height ) {
107+ super (x , y , Scrollbar .this .width (), height );
108+ }
109+
110+ @ Override
111+ public void render (GuiGraphics guiGraphics , int mouseX , int mouseY ) {
112+ this .renderBackground (guiGraphics );
113+ }
114+
115+ @ Override
116+ public void renderBackground (GuiGraphics guiGraphics ) {
117+ guiGraphics .fill (this .x (), this .y (), this .x () + this .width (), this .y () + this .height (), ARGB .color (1.0F , 0xAAFE00 ));
118+ }
119+ }
56120 }
57121}
0 commit comments