Skip to content

Commit db7364e

Browse files
committed
Java 17 support
1 parent 28c1260 commit db7364e

1 file changed

Lines changed: 49 additions & 62 deletions

File tree

fabric/src/main/java/dev/faststats/fabric/screen/FabricScreenManager.java

Lines changed: 49 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public Text newText() {
5353
}
5454

5555
@Override
56-
public Text translatable(String text) {
56+
public Text translatable(final String text) {
5757
return new FabricText(Component.translatable(text));
5858
}
5959

@@ -94,7 +94,7 @@ protected void init() {
9494

9595
final int topSeparator = topSeparator();
9696
final int bottomSeparator = bottomSeparator();
97-
final int contentWidth = Math.clamp(width - CONTENT_MARGIN * 2, 1, CONTENT_MAX_WIDTH);
97+
final int contentWidth = clamp(width - CONTENT_MARGIN * 2, 1, CONTENT_MAX_WIDTH);
9898
final int contentX = (width - contentWidth) / 2;
9999
final int contentY = topSeparator + CONTENT_MARGIN;
100100
final int contentHeight = Math.max(1, bottomSeparator - contentY - BOTTOM_CONTENT_MARGIN);
@@ -127,7 +127,7 @@ protected void init() {
127127
}
128128

129129
if (!screen.footer().isEmpty()) {
130-
final int footerWidth = Math.clamp(width - CONTENT_MARGIN * 2, 1, FOOTER_MAX_WIDTH);
130+
final int footerWidth = clamp(width - CONTENT_MARGIN * 2, 1, FOOTER_MAX_WIDTH);
131131
final var footer = buildRootDocument(screen.footer(), footerWidth, FOOTER_HEIGHT);
132132
footer.setPosition((width - footerWidth) / 2,
133133
bottomSeparator + Math.max(0, (height - bottomSeparator - footer.getHeight()) / 2));
@@ -265,7 +265,7 @@ private Layout buildRootDocument(final List<Element<?>> elements, final int widt
265265
childrenHeight += child.getHeight();
266266
}
267267

268-
final int gap = elements.size() >= 2 ? Math.clamp((availableHeight - childrenHeight) / (elements.size() - 1), 0, ROOT_GAP) : 0;
268+
final int gap = elements.size() >= 2 ? clamp((availableHeight - childrenHeight) / (elements.size() - 1), 0, ROOT_GAP) : 0;
269269
final var layout = LinearLayout.vertical().spacing(gap);
270270
layout.defaultCellSetting().alignHorizontallyLeft();
271271
children.forEach(layout::addChild);
@@ -313,28 +313,19 @@ private Layout buildSizedLayout(final List<Element<?>> elements, final Division.
313313
private LayoutElement wrapSized(final Element<?> element, int width, int height) {
314314
width = Math.max(1, width);
315315
height = Math.max(1, height);
316-
switch (element) {
317-
case final Division division -> {
318-
return buildSizedLayout(division.elements(), division.orientation(), width, height, division.gap());
319-
}
320-
case final Scrollable scrollable -> {
321-
return wrapScrollable(element,
322-
buildDocument(scrollable.elements(), width, height, scrollable.gap()), width, height);
323-
}
324-
case final ScrollableTextBox textBox -> {
325-
return wrapScrollableTextBox(element, textBox.text(), width, height);
326-
}
327-
case final TextBox textBox -> {
328-
return wrapTextBox(textBox.text(), width, height);
329-
}
330-
case final Checkbox checkbox -> {
331-
return frame(width, height, wrap(checkbox, width), 0.0F, 0.5F);
332-
}
333-
case final Button button -> {
334-
return frame(width, height, wrap(button, width, height), 0.0F, 0.5F);
335-
}
336-
default -> {
337-
}
316+
if (element instanceof final Division division) {
317+
return buildSizedLayout(division.elements(), division.orientation(), width, height, division.gap());
318+
} else if (element instanceof final Scrollable scrollable) {
319+
return wrapScrollable(element,
320+
buildDocument(scrollable.elements(), width, height, scrollable.gap()), width, height);
321+
} else if (element instanceof final ScrollableTextBox textBox) {
322+
return wrapScrollableTextBox(element, textBox.text(), width, height);
323+
} else if (element instanceof final TextBox textBox) {
324+
return wrapTextBox(textBox.text(), width, height);
325+
} else if (element instanceof final Checkbox checkbox) {
326+
return frame(width, height, wrap(checkbox, width), 0.0F, 0.5F);
327+
} else if (element instanceof final Button button) {
328+
return frame(width, height, wrap(button, width, height), 0.0F, 0.5F);
338329
}
339330
throw new IllegalArgumentException("Unsupported screen element: " + element.getClass().getName());
340331
}
@@ -352,37 +343,28 @@ private Layout buildDocument(final List<Element<?>> elements, final int width, f
352343

353344
private LayoutElement wrapNatural(final Element<?> element, int width, final int referenceHeight) {
354345
width = Math.max(1, width);
355-
switch (element) {
356-
case final Division division -> {
357-
return buildNaturalDivision(division, width, referenceHeight);
358-
}
359-
case final Scrollable scrollable -> {
360-
final int height = percentage(referenceHeight, scrollable.height());
361-
return wrapScrollable(element,
362-
buildDocument(scrollable.elements(), width, height, scrollable.gap()), width, height);
363-
}
364-
case final ScrollableTextBox textBox -> {
365-
final int maxHeight = percentage(referenceHeight, textBox.height());
366-
final int height = Math.min(maxHeight, naturalScrollableTextBoxHeight(textBox.text(), width));
367-
return wrapScrollableTextBox(element, textBox.text(), width, height);
368-
}
369-
case final TextBox textBox -> {
370-
final int innerWidth = Math.max(1, width - TEXT_PADDING_X * 2);
371-
final var widget = textWidget(textBox.text(), innerWidth);
372-
final int naturalHeight = widget.getHeight() + TEXT_PADDING_Y * 2;
373-
final int height = textBox.height() == 100
374-
? naturalHeight
375-
: percentage(referenceHeight, textBox.height());
376-
return wrapTextBox(textBox.text(), width, height);
377-
}
378-
case final Checkbox checkbox -> {
379-
return wrap(checkbox, width);
380-
}
381-
case final Button button -> {
382-
return wrap(button, width, net.minecraft.client.gui.components.Button.DEFAULT_HEIGHT);
383-
}
384-
default -> {
385-
}
346+
if (element instanceof final Division division) {
347+
return buildNaturalDivision(division, width, referenceHeight);
348+
} else if (element instanceof final Scrollable scrollable) {
349+
final int height = percentage(referenceHeight, scrollable.height());
350+
return wrapScrollable(element,
351+
buildDocument(scrollable.elements(), width, height, scrollable.gap()), width, height);
352+
} else if (element instanceof final ScrollableTextBox textBox) {
353+
final int maxHeight = percentage(referenceHeight, textBox.height());
354+
final int height = Math.min(maxHeight, naturalScrollableTextBoxHeight(textBox.text(), width));
355+
return wrapScrollableTextBox(element, textBox.text(), width, height);
356+
} else if (element instanceof final TextBox textBox) {
357+
final int innerWidth = Math.max(1, width - TEXT_PADDING_X * 2);
358+
final var widget = textWidget(textBox.text(), innerWidth);
359+
final int naturalHeight = widget.getHeight() + TEXT_PADDING_Y * 2;
360+
final int height = textBox.height() == 100
361+
? naturalHeight
362+
: percentage(referenceHeight, textBox.height());
363+
return wrapTextBox(textBox.text(), width, height);
364+
} else if (element instanceof final Checkbox checkbox) {
365+
return wrap(checkbox, width);
366+
} else if (element instanceof final Button button) {
367+
return wrap(button, width, net.minecraft.client.gui.components.Button.DEFAULT_HEIGHT);
386368
}
387369
throw new IllegalArgumentException("Unsupported screen element: " + element.getClass().getName());
388370
}
@@ -412,7 +394,7 @@ private Layout buildNaturalDivision(final Division division, final int width, fi
412394
}
413395

414396
private LayoutElement wrapTextBox(final Text text, final int width, final int height) {
415-
final int paddingX = Math.clamp((width - 1) / 2, 0, TEXT_PADDING_X);
397+
final int paddingX = clamp((width - 1) / 2, 0, TEXT_PADDING_X);
416398
final int paddingY = textPaddingY(height);
417399
final var widget = textWidget(text, Math.max(1, width - paddingX * 2));
418400
widget.setMaxRows(Math.max(1, (height - paddingY * 2) / font.lineHeight));
@@ -425,7 +407,7 @@ private LayoutElement wrapTextBox(final Text text, final int width, final int he
425407

426408
private LayoutElement wrapScrollableTextBox(final Element<?> element, final Text text, final int width, final int height) {
427409
final int panelWidth = Math.max(1, width - AbstractScrollArea.SCROLLBAR_WIDTH);
428-
final int paddingX = Math.clamp((panelWidth - 1) / 2, 0, TEXT_PADDING_X);
410+
final int paddingX = clamp((panelWidth - 1) / 2, 0, TEXT_PADDING_X);
429411
final int paddingY = textPaddingY(height);
430412
final int textWidth = Math.max(1, panelWidth - paddingX * 2);
431413
final int documentWidth = scrollContentWidth(width);
@@ -448,7 +430,7 @@ private LayoutElement wrapScrollableTextBox(final Element<?> element, final Text
448430

449431
private int naturalScrollableTextBoxHeight(final Text text, final int width) {
450432
final int panelWidth = Math.max(1, width - AbstractScrollArea.SCROLLBAR_WIDTH);
451-
final int paddingX = Math.clamp((panelWidth - 1) / 2, 0, TEXT_PADDING_X);
433+
final int paddingX = clamp((panelWidth - 1) / 2, 0, TEXT_PADDING_X);
452434
final int textWidth = Math.max(1, panelWidth - paddingX * 2);
453435
return Math.max(1, textWidget(text, textWidth).getHeight() + (TEXT_PADDING_Y + 1) * 2);
454436
}
@@ -553,11 +535,16 @@ private int scrollContentWidth(final int width) {
553535
}
554536

555537
private int topSeparator() {
556-
return Math.clamp(height / 3, 1, HEADER_HEIGHT);
538+
return clamp(height / 3, 1, HEADER_HEIGHT);
557539
}
558540

559541
private int bottomSeparator() {
560-
return Math.max(topSeparator() + 1, height - Math.clamp(height / 3, 1, FOOTER_HEIGHT));
542+
return Math.max(topSeparator() + 1, height - clamp(height / 3, 1, FOOTER_HEIGHT));
543+
}
544+
545+
public static int clamp(final long value, final int min, final int max) {
546+
if (min > max) throw new IllegalArgumentException(min + " > " + max);
547+
return (int) Math.min(max, Math.max(value, min));
561548
}
562549

563550
private static final class PanelWidget extends AbstractWidget {

0 commit comments

Comments
 (0)