diff options
author | Juuz <6596629+Juuxel@users.noreply.github.com> | 2021-06-16 01:39:48 +0300 |
---|---|---|
committer | Juuz <6596629+Juuxel@users.noreply.github.com> | 2021-06-16 01:39:48 +0300 |
commit | 3f019baf0d550642d8c941fe72dd0d070f1aa859 (patch) | |
tree | 614443d22ee33ac1ff6312d4bcc7a74d2eb54b93 /src | |
parent | 780f073d1d19c6651cc3072a500323ae78b0b8e5 (diff) | |
download | LibGui-3f019baf0d550642d8c941fe72dd0d070f1aa859.tar.gz LibGui-3f019baf0d550642d8c941fe72dd0d070f1aa859.tar.bz2 LibGui-3f019baf0d550642d8c941fe72dd0d070f1aa859.zip |
Clean up code using switch expressions
Diffstat (limited to 'src')
5 files changed, 56 insertions, 117 deletions
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/impl/modmenu/WKirbSprite.java b/src/main/java/io/github/cottonmc/cotton/gui/impl/modmenu/WKirbSprite.java index 1d7b191..9bed0d1 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/impl/modmenu/WKirbSprite.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/impl/modmenu/WKirbSprite.java @@ -14,11 +14,11 @@ import java.util.ArrayList; public class WKirbSprite extends WWidget { private static final Identifier KIRB = new Identifier(LibGuiCommon.MOD_ID, "textures/widget/kirb.png"); - + private static final float PX = 1f/416f; private static final float KIRB_WIDTH = 32*PX; - - private int currentFrame= 0; + + private int currentFrame = 0; private long currentFrameTime = 0; private int[] toSleep = { 0, 0, 0, 1, 2, 1, 2, 0, 0, 0, 1, 2, 3 }; private int[] asleep = { 4, 4, 4, 4, 5, 6, 7, 6, 5 }; @@ -26,89 +26,74 @@ public class WKirbSprite extends WWidget { private int[] awake = { 9, 9, 9, 10, 11, 12 }; private State state = State.ASLEEP; private ArrayList<Integer> pendingFrames = new ArrayList<>(); - + private int frameTime = 300; private long lastFrame; public WKirbSprite() { state = (LibGui.isDarkMode()) ? State.ASLEEP : State.AWAKE; } - + public void schedule(int[] frames) { - for(int i : frames) pendingFrames.add(i); + for (int i : frames) pendingFrames.add(i); } @Override public boolean canResize() { return false; } - + @Override public int getWidth() { return 32; } - + @Override public int getHeight() { return 32; } - + @Environment(EnvType.CLIENT) @Override public void paint(MatrixStack matrices, int x, int y, int mouseX, int mouseY) { long now = System.nanoTime() / 1_000_000L; - - + if (pendingFrames.isEmpty()) { - if (LibGui.isDarkMode()) { - switch(state) { - case AWAKE: - state = State.FALLING_ASLEEP; - break; - case FALLING_ASLEEP: - state = State.ASLEEP; - break; - default: - //zzzz - state = State.ASLEEP; - break; - } + state = switch (state) { + case AWAKE -> State.FALLING_ASLEEP; + case FALLING_ASLEEP -> State.ASLEEP; + default -> /* zzzz */ State.ASLEEP; + }; } else { - switch(state) { - case ASLEEP: - state = State.WAKING_UP; - break; - case WAKING_UP: - state = State.AWAKE; - break; - default: - state = State.AWAKE; - break; - } + state = switch (state) { + case ASLEEP -> State.WAKING_UP; + case WAKING_UP -> State.AWAKE; + default -> State.AWAKE; + }; } - + switch (state) { - case ASLEEP: schedule(asleep); break; - case WAKING_UP: schedule(toAwake); break; - case AWAKE: schedule(awake); break; - case FALLING_ASLEEP: schedule(toSleep); break; + case ASLEEP -> schedule(asleep); + case WAKING_UP -> schedule(toAwake); + case AWAKE -> schedule(awake); + case FALLING_ASLEEP -> schedule(toSleep); } } - + float offset = KIRB_WIDTH * currentFrame; - ScreenDrawing.texturedRect(matrices, x, y+8, 32, 32, KIRB, offset, 0, offset+KIRB_WIDTH, 1, 0xFFFFFFFF); - + ScreenDrawing.texturedRect(matrices, x, y + 8, 32, 32, KIRB, offset, 0, offset + KIRB_WIDTH, 1, 0xFFFFFFFF); + long elapsed = now - lastFrame; currentFrameTime += elapsed; if (currentFrameTime >= frameTime) { if (!pendingFrames.isEmpty()) currentFrame = pendingFrames.remove(0); currentFrameTime = 0; } - + this.lastFrame = now; } - + public static enum State { AWAKE, FALLING_ASLEEP, diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WAbstractSlider.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WAbstractSlider.java index 5993179..7fda1d9 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WAbstractSlider.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WAbstractSlider.java @@ -139,23 +139,12 @@ public abstract class WAbstractSlider extends WWidget { } private void moveSlider(int x, int y) { - int axisPos; - - switch (direction) { - case UP: - axisPos = height - y; - break; - case DOWN: - axisPos = y; - break; - case LEFT: - axisPos = width - x; - break; - case RIGHT: - default: - axisPos = x; - break; - } + int axisPos = switch (direction) { + case UP -> height - y; + case DOWN -> y; + case LEFT -> width - x; + case RIGHT -> x; + }; int pos = axisPos - getThumbWidth() / 2; int rawValue = min + Math.round(valueToCoordRatio * pos); diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WBox.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WBox.java index eb447a0..637ee56 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WBox.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WBox.java @@ -106,37 +106,19 @@ public class WBox extends WPanelWithInsets { WWidget child = children.get(i); if (axis == Axis.HORIZONTAL) { - int y; - - switch (verticalAlignment) { - case TOP: - default: - y = insets.top(); - break; - case CENTER: - y = insets.top() + (getHeight() - insets.top() - insets.bottom() - child.getHeight()) / 2; - break; - case BOTTOM: - y = getHeight() - insets.bottom() - child.getHeight(); - break; - } + int y = switch (verticalAlignment) { + case TOP -> insets.top(); + case CENTER -> insets.top() + (getHeight() - insets.top() - insets.bottom() - child.getHeight()) / 2; + case BOTTOM -> getHeight() - insets.bottom() - child.getHeight(); + }; child.setLocation(dimension, y); } else { - int x; - - switch (horizontalAlignment) { - case LEFT: - default: - x = insets.left(); - break; - case CENTER: - x = insets.left() + (getWidth() - insets.left() - insets.right() - child.getWidth()) / 2; - break; - case RIGHT: - x = getWidth() - insets.right() - child.getWidth(); - break; - } + int x = switch (horizontalAlignment) { + case LEFT -> insets.left(); + case CENTER -> insets.left() + (getWidth() - insets.left() - insets.right() - child.getWidth()) / 2; + case RIGHT -> getWidth() - insets.right() - child.getWidth(); + }; child.setLocation(x, dimension); } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java index 1003e47..5c6b2da 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WLabel.java @@ -84,20 +84,11 @@ public class WLabel extends WWidget { public void paint(MatrixStack matrices, int x, int y, int mouseX, int mouseY) { MinecraftClient mc = MinecraftClient.getInstance(); TextRenderer renderer = mc.textRenderer; - int yOffset; - - switch (verticalAlignment) { - case CENTER: - yOffset = height / 2 - renderer.fontHeight / 2; - break; - case BOTTOM: - yOffset = height - renderer.fontHeight; - break; - case TOP: - default: - yOffset = 0; - break; - } + int yOffset = switch (verticalAlignment) { + case CENTER -> height / 2 - renderer.fontHeight / 2; + case BOTTOM -> height - renderer.fontHeight; + case TOP -> 0; + }; ScreenDrawing.drawString(matrices, text.asOrderedText(), horizontalAlignment, x, y + yOffset, this.getWidth(), LibGui.isDarkMode() ? darkmodeColor : color); diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WText.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WText.java index 644279d..96f78ed 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WText.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WText.java @@ -92,19 +92,11 @@ public class WText extends WWidget { TextRenderer font = MinecraftClient.getInstance().textRenderer; - int yOffset; - switch (verticalAlignment) { - case CENTER: - yOffset = height / 2 - font.fontHeight * wrappedLines.size() / 2; - break; - case BOTTOM: - yOffset = height - font.fontHeight * wrappedLines.size(); - break; - case TOP: - default: - yOffset = 0; - break; - } + int yOffset = switch (verticalAlignment) { + case CENTER -> height / 2 - font.fontHeight * wrappedLines.size() / 2; + case BOTTOM -> height - font.fontHeight * wrappedLines.size(); + case TOP -> 0; + }; for (int i = 0; i < wrappedLines.size(); i++) { OrderedText line = wrappedLines.get(i); |