2424
2525import java .util .List ;
2626
27- import com .mojang .blaze3d .systems .RenderSystem ;
2827import io .github .axolotlclient .api .requests .GlobalDataRequest ;
2928import io .github .axolotlclient .modules .hud .util .DrawUtil ;
3029import net .minecraft .client .gui .screen .Screen ;
30+ import net .minecraft .client .gui .screen .ScreenTexts ;
31+ import net .minecraft .client .gui .widget .AbstractButtonWidget ;
3132import net .minecraft .client .gui .widget .ButtonWidget ;
33+ import net .minecraft .client .sound .SoundManager ;
3234import net .minecraft .client .util .math .MatrixStack ;
35+ import net .minecraft .text .MutableText ;
3336import net .minecraft .text .OrderedText ;
34- import net .minecraft .text .StringVisitable ;
37+ import net .minecraft .text .Text ;
3538import net .minecraft .text .TranslatableText ;
3639import net .minecraft .util .math .MathHelper ;
3740
3841public class NewsScreen extends Screen {
3942
40- private static final int SCROLL_STEP = 5 ;
43+
4144 private final Screen parent ;
42- private int scrollAmount ;
43- private List <OrderedText > lines ;
4445
4546 public NewsScreen (Screen parent ) {
4647 super (new TranslatableText ("api.notes.title" ));
@@ -49,50 +50,276 @@ public NewsScreen(Screen parent) {
4950 }
5051
5152 @ Override
52- public void render (MatrixStack matrices , int mouseX , int mouseY , float delta ) {
53- renderBackground (matrices );
53+ public void render (MatrixStack graphics , int mouseX , int mouseY , float delta ) {
54+ renderBackground (graphics );
55+ super .render (graphics , mouseX , mouseY , delta );
5456
55- client .textRenderer .drawWithShadow (matrices , title , width / 2F , 20 , -1 );
57+ drawCenteredText (graphics , textRenderer , title , width / 2 , 20 , -1 );
58+ }
5659
57- RenderSystem .enableBlend ();
60+ @ Override
61+ protected void init () {
62+ addButton (new NewsWidget (25 , 35 , width - 50 , height - 100 , new TranslatableText (GlobalDataRequest .get ().notes ().replaceAll ("([^\n ])\n ([^\n ])" , "$1 $2" ))));
5863
59- matrices . push ( );
60- matrices . translate ( 0 , scrollAmount , 0 );
64+ addButton ( new ButtonWidget ( width / 2 - 100 , height - 45 , 200 , 20 , ScreenTexts . BACK , buttonWidget -> client . openScreen ( parent )) );
65+ }
6166
62- DrawUtil .enableScissor (0 , 35 , width , height - 65 );
63- int y = 35 ;
64- for (OrderedText t : lines ) {
65- client .textRenderer .drawWithShadow (matrices , t , 25 , y , -1 );
66- y += client .textRenderer .fontHeight ;
67- }
68- DrawUtil .disableScissor ();
69- matrices .pop ();
67+ private class NewsWidget extends AbstractTextAreaWidget {
7068
69+ private final List <OrderedText > lines ;
70+ private final int contentHeight ;
7171
72- int scrollbarY = 35 + (( height - 65 ) - 35 ) / ( lines . size ()) * -( scrollAmount / SCROLL_STEP );
73- int scrollbarHeight = ( height - 65 - 35 ) / SCROLL_STEP ;
74- fill ( matrices , width - 15 , 35 , width - 9 , height - 65 , - 16777216 );
75- fill ( matrices , width - 15 , scrollbarY , width - 9 , scrollbarY + scrollbarHeight , - 8355712 ) ;
76- fill ( matrices , width - 15 , scrollbarY , width - 10 , scrollbarY + scrollbarHeight - 1 , - 4144960 );
72+ public NewsWidget ( int x , int y , int width , int height , Text component ) {
73+ super ( x , y , width , height , component ) ;
74+ lines = textRenderer . wrapLines ( getMessage (), getWidth () - 4 );
75+ contentHeight = lines . size () * textRenderer . fontHeight ;
76+ }
7777
78- super .render (matrices , mouseX , mouseY , delta );
78+ @ Override
79+ protected int getInnerHeight () {
80+ return contentHeight ;
81+ }
7982
83+ @ Override
84+ protected void renderContents (MatrixStack graphics , int mouseX , int mouseY , float partialTick ) {
85+ int y = getY () + 2 ;
86+ for (OrderedText chsq : lines ) {
87+ textRenderer .drawWithShadow (graphics , chsq , getX () + 2 , y , -1 );
88+ y += textRenderer .fontHeight ;
89+ }
90+ }
91+
92+ @ Override
93+ protected double scrollRate () {
94+ return textRenderer .fontHeight ;
95+ }
8096
97+ @ Override
98+ protected MutableText getNarrationMessage () {
99+ return getMessage ().copy ();
100+ }
81101 }
82102
83- @ Override
84- protected void init () {
85- lines = client .textRenderer .wrapLines (StringVisitable .plain (GlobalDataRequest .get ().notes ()), width - 50 );
103+ public abstract static class AbstractTextAreaWidget extends AbstractScrollArea {
104+ private static final int INNER_PADDING = 4 ;
105+
106+ public AbstractTextAreaWidget (int i , int j , int k , int l , Text component ) {
107+ super (i , j , k , l , component );
108+ }
109+
110+ @ Override
111+ public boolean mouseClicked (double mouseX , double mouseY , int button ) {
112+ boolean bl = this .updateScrolling (mouseX , mouseY , button );
113+ return super .mouseClicked (mouseX , mouseY , button ) || bl ;
114+ }
115+
116+ @ Override
117+ public boolean keyPressed (int keyCode , int scanCode , int modifiers ) {
118+ boolean bl = keyCode == 265 ;
119+ boolean bl2 = keyCode == 264 ;
120+ if (bl || bl2 ) {
121+ double d = this .scrollAmount ();
122+ this .setScrollAmount (this .scrollAmount () + (double )(bl ? -1 : 1 ) * this .scrollRate ());
123+ if (d != this .scrollAmount ()) {
124+ return true ;
125+ }
126+ }
127+
128+ return super .keyPressed (keyCode , scanCode , modifiers );
129+ }
130+
131+ @ Override
132+ public void renderButton (MatrixStack guiGraphics , int mouseX , int mouseY , float partialTick ) {
133+ if (this .visible ) {
134+ this .renderBackground (guiGraphics );
135+ DrawUtil .enableScissor (this .getX () + 1 , this .getY () + 1 , this .getX () + this .width - 1 , this .getY () + this .height - 1 );
136+ guiGraphics .push ();
137+ guiGraphics .translate (0.0 , -this .scrollAmount (), 0.0 );
138+ this .renderContents (guiGraphics , mouseX , mouseY , partialTick );
139+ guiGraphics .pop ();
140+ DrawUtil .disableScissor ();
141+ this .renderDecorations (guiGraphics );
142+ }
143+ }
144+
145+ protected void renderDecorations (MatrixStack guiGraphics ) {
146+ this .renderScrollbar (guiGraphics );
147+ }
148+
149+ protected int innerPadding () {
150+ return INNER_PADDING ;
151+ }
152+
153+ protected int totalInnerPadding () {
154+ return this .innerPadding () * 2 ;
155+ }
156+
157+ @ Override
158+ public boolean isMouseOver (double mouseX , double mouseY ) {
159+ return this .active
160+ && this .visible
161+ && mouseX >= (double )this .getX ()
162+ && mouseY >= (double )this .getY ()
163+ && mouseX < (double )(this .getXEnd () + 6 )
164+ && mouseY < (double )this .getYEnd ();
165+ }
166+
167+ @ Override
168+ protected int scrollBarX () {
169+ return this .getXEnd ();
170+ }
171+
172+ @ Override
173+ protected int contentHeight () {
174+ return this .getInnerHeight () + this .totalInnerPadding ();
175+ }
176+
177+ protected void renderBackground (MatrixStack guiGraphics ) {
178+ this .renderBorder (guiGraphics , this .getX (), this .getY (), this .getWidth (), this .getHeight ());
179+ }
180+
181+ protected void renderBorder (MatrixStack guiGraphics , int x , int y , int width , int height ) {
182+ int i = this .isFocused () ? -1 : -6250336 ;
183+ fill (guiGraphics , this .getX (), this .getY (), this .getXEnd (), this .getYEnd (), i );
184+ fill (guiGraphics , this .getX ()+1 , this .getY ()+1 , this .getXEnd ()-1 , this .getYEnd ()-1 , -16777216 );
185+ }
186+
187+ protected boolean withinContentAreaTopBottom (int top , int bottom ) {
188+ return (double )bottom - this .scrollAmount () >= (double )this .getY () && (double )top - this .scrollAmount () <= (double )(this .getY () + this .height );
189+ }
190+
191+ protected abstract int getInnerHeight ();
192+
193+ protected abstract void renderContents (MatrixStack guiGraphics , int mouseX , int mouseY , float partialTick );
194+
195+ protected int getInnerLeft () {
196+ return this .getX () + this .innerPadding ();
197+ }
198+
199+ protected int getInnerTop () {
200+ return this .getY () + this .innerPadding ();
201+ }
86202
87- addButton (new ButtonWidget (width / 2 - 100 , height - 45 , 200 , 20 ,
88- new TranslatableText ("gui.back" ), buttonWidget -> client .openScreen (parent )));
203+ @ Override
204+ public void playDownSound (SoundManager handler ) {
205+ }
89206 }
90207
91- @ Override
92- public boolean mouseScrolled (double mouseX , double mouseY , double amount ) {
93- scrollAmount = (int ) MathHelper .clamp (scrollAmount + amount * SCROLL_STEP ,
94- Math .min (0 , -((lines .size () + 3 ) * client .textRenderer .fontHeight - (height - 65 ))),
95- 0 );
96- return super .mouseScrolled (mouseX , mouseY , amount );
208+ public abstract static class AbstractScrollArea extends AbstractButtonWidget {
209+ public static final int SCROLLBAR_WIDTH = 6 ;
210+ private double scrollAmount ;
211+ private boolean scrolling ;
212+
213+ public AbstractScrollArea (int i , int j , int k , int l , Text component ) {
214+ super (i , j , k , l , component );
215+ }
216+
217+ @ Override
218+ public boolean mouseScrolled (double mouseX , double mouseY , double scrollY ) {
219+ if (!this .visible ) {
220+ return false ;
221+ } else {
222+ this .setScrollAmount (this .scrollAmount () - scrollY * this .scrollRate ());
223+ return true ;
224+ }
225+ }
226+
227+ @ Override
228+ public boolean mouseDragged (double mouseX , double mouseY , int button , double dragX , double dragY ) {
229+ if (this .scrolling ) {
230+ if (mouseY < (double )this .getY ()) {
231+ this .setScrollAmount (0.0 );
232+ } else if (mouseY > (double )this .getYEnd ()) {
233+ this .setScrollAmount (this .maxScrollAmount ());
234+ } else {
235+ double d = Math .max (1 , this .maxScrollAmount ());
236+ int i = this .scrollerHeight ();
237+ double e = Math .max (1.0 , d / (double )(this .height - i ));
238+ this .setScrollAmount (this .scrollAmount () + dragY * e );
239+ }
240+
241+ return true ;
242+ } else {
243+ return super .mouseDragged (mouseX , mouseY , button , dragX , dragY );
244+ }
245+ }
246+
247+ @ Override
248+ public void onRelease (double mouseX , double mouseY ) {
249+ this .scrolling = false ;
250+ }
251+
252+ public double scrollAmount () {
253+ return this .scrollAmount ;
254+ }
255+
256+ public void setScrollAmount (double scrollAmount ) {
257+ this .scrollAmount = MathHelper .clamp (scrollAmount , 0.0 , this .maxScrollAmount ());
258+ }
259+
260+ public boolean updateScrolling (double mouseX , double mouseY , int button ) {
261+ this .scrolling = this .scrollbarVisible ()
262+ && this .isValidClickButton (button )
263+ && mouseX >= (double )this .scrollBarX ()
264+ && mouseX <= (double )(this .scrollBarX () + 6 )
265+ && mouseY >= (double )this .getY ()
266+ && mouseY < (double )this .getYEnd ();
267+ return this .scrolling ;
268+ }
269+
270+ public void refreshScrollAmount () {
271+ this .setScrollAmount (this .scrollAmount );
272+ }
273+
274+ public int maxScrollAmount () {
275+ return Math .max (0 , this .contentHeight () - this .height );
276+ }
277+
278+ protected boolean scrollbarVisible () {
279+ return this .maxScrollAmount () > 0 ;
280+ }
281+
282+ protected int scrollerHeight () {
283+ return MathHelper .clamp ((int )((float )(this .height * this .height ) / (float )this .contentHeight ()), 32 , this .height - 8 );
284+ }
285+
286+ protected int scrollBarX () {
287+ return this .getXEnd () - 6 ;
288+ }
289+
290+ protected int scrollBarY () {
291+ return Math .max (this .getY (), (int )this .scrollAmount * (this .height - this .scrollerHeight ()) / this .maxScrollAmount () + this .getY ());
292+ }
293+
294+ protected void renderScrollbar (MatrixStack guiGraphics ) {
295+ if (this .scrollbarVisible ()) {
296+ int i = this .scrollBarX ();
297+ int j = this .scrollerHeight ();
298+ int k = this .scrollBarY ();
299+ fill (guiGraphics , i , getY (), i +SCROLLBAR_WIDTH , getYEnd (), -16777216 );
300+ fill (guiGraphics , i , k , i +SCROLLBAR_WIDTH , k +j , -8355712 );
301+ fill (guiGraphics , i , k , i +SCROLLBAR_WIDTH -1 , k +j -1 , -4144960 );
302+ }
303+ }
304+
305+ protected int getYEnd () {
306+ return getY ()+getHeight ();
307+ }
308+
309+ protected abstract int contentHeight ();
310+
311+ protected abstract double scrollRate ();
312+
313+ protected int getX () {
314+ return x ;
315+ }
316+
317+ protected int getY () {
318+ return y ;
319+ }
320+
321+ protected int getXEnd () {
322+ return getX ()+getWidth ();
323+ }
97324 }
98325}
0 commit comments