From 8b2510450b908d6e6c233d6eea70b50d46c9eef7 Mon Sep 17 00:00:00 2001 From: StavWasPlayZ Date: Wed, 30 Aug 2023 19:03:49 +0300 Subject: [PATCH] Migrate Fabric changes to Forge --- .../genshinstrument/client/ClientUtil.java | 17 ++++++ .../config/enumType/label/DrumNoteLabel.java | 19 +++++-- .../config/enumType/label/NoteGridLabel.java | 57 ++++--------------- .../AratakisGreatAndGloriousDrumScreen.java | 10 ++-- .../instrument/drum/DrumNoteButton.java | 6 ++ .../partial/note/label/INoteLabel.java | 37 ++++++++++++ .../partial/notegrid/NoteGridButton.java | 7 +++ .../options/instrument/DrumOptionsScren.java | 4 +- .../GridInstrumentOptionsScreen.java | 2 +- .../AbstractInstrumentOptionsScreen.java | 2 +- .../client/keyMaps/InstrumentKeyMappings.java | 4 ++ 11 files changed, 104 insertions(+), 61 deletions(-) diff --git a/src/main/java/com/cstav/genshinstrument/client/ClientUtil.java b/src/main/java/com/cstav/genshinstrument/client/ClientUtil.java index 4733e082..1c296e27 100644 --- a/src/main/java/com/cstav/genshinstrument/client/ClientUtil.java +++ b/src/main/java/com/cstav/genshinstrument/client/ClientUtil.java @@ -3,6 +3,8 @@ import java.awt.Color; import java.awt.Point; +import com.cstav.genshinstrument.client.keyMaps.InstrumentKeyMappings; +import com.mojang.blaze3d.platform.InputConstants.Key; import com.mojang.blaze3d.systems.RenderSystem; import net.minecraft.client.gui.layouts.FrameLayout; @@ -10,11 +12,26 @@ import net.minecraft.client.gui.layouts.Layout; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; +import net.minecraftforge.common.util.Lazy; @OnlyIn(Dist.CLIENT) public class ClientUtil { public static final int GRID_HORZ_PADDING = 4, GRID_VERT_PADDING = 2; + + public static final Lazy ON_QWERTY = Lazy.of(() -> { + final String qwerty = "QWERTY"; + final Key[] keyRow = InstrumentKeyMappings.GRID_INSTRUMENT_MAPPINGS[0]; + + // Assuming there will be more than 6 entries here + for (int i = 0; i < qwerty.length(); i++) { + if (qwerty.charAt(i) != keyRow[i].getDisplayName().getString(1).charAt(0)) + return false; + } + + return true; + }); + /** * @return The point in the center of the described widget diff --git a/src/main/java/com/cstav/genshinstrument/client/config/enumType/label/DrumNoteLabel.java b/src/main/java/com/cstav/genshinstrument/client/config/enumType/label/DrumNoteLabel.java index b2673f2b..40364372 100644 --- a/src/main/java/com/cstav/genshinstrument/client/config/enumType/label/DrumNoteLabel.java +++ b/src/main/java/com/cstav/genshinstrument/client/config/enumType/label/DrumNoteLabel.java @@ -1,10 +1,10 @@ package com.cstav.genshinstrument.client.config.enumType.label; +import com.cstav.genshinstrument.client.config.ModClientConfigs; import com.cstav.genshinstrument.client.gui.screen.instrument.drum.DrumNoteButton; import com.cstav.genshinstrument.client.gui.screen.instrument.partial.note.NoteButton; import com.cstav.genshinstrument.client.gui.screen.instrument.partial.note.label.INoteLabel; import com.cstav.genshinstrument.client.gui.screen.instrument.partial.note.label.NoteLabelSupplier; -import com.cstav.genshinstrument.client.keyMaps.InstrumentKeyMappings.DrumKeys; import com.cstav.genshinstrument.util.LabelUtil; import net.minecraft.network.chat.Component; @@ -13,12 +13,13 @@ @OnlyIn(Dist.CLIENT) public enum DrumNoteLabel implements INoteLabel { - KEYBOARD_LAYOUT((note) -> { - final DrumNoteButton dnb = dn(note); - final DrumKeys keys = dnb.btnType.getKeys(); + KEYBOARD_LAYOUT((note) -> + INoteLabel.upperComponent(dn(note).getKey().getDisplayName()) + ), + QWERTY((note) -> + INoteLabel.getQwerty(dn(note).getKey()) + ), - return INoteLabel.upperComponent((dnb.isRight ? keys.right : keys.left).getDisplayName()); - }), DON_KA((note) -> Component.translatable(dn(note).btnType.getTransKey()) ), @@ -28,6 +29,7 @@ public enum DrumNoteLabel implements INoteLabel { DO_RE_MI((note) -> LabelUtil.toDoReMi(note.getCutNoteName()) ), + NONE(NoteLabelSupplier.EMPTY); @@ -36,6 +38,11 @@ private DrumNoteLabel(final NoteLabelSupplier supplier) { labelSupplier = supplier; } + public static INoteLabel[] availableVals() { + return INoteLabel.filterQwerty(values(), ModClientConfigs.DRUM_LABEL_TYPE.get(), QWERTY); + } + + @Override public NoteLabelSupplier getLabelSupplier() { return labelSupplier; diff --git a/src/main/java/com/cstav/genshinstrument/client/config/enumType/label/NoteGridLabel.java b/src/main/java/com/cstav/genshinstrument/client/config/enumType/label/NoteGridLabel.java index 018df5af..941bdf0d 100644 --- a/src/main/java/com/cstav/genshinstrument/client/config/enumType/label/NoteGridLabel.java +++ b/src/main/java/com/cstav/genshinstrument/client/config/enumType/label/NoteGridLabel.java @@ -5,12 +5,9 @@ import com.cstav.genshinstrument.client.gui.screen.instrument.partial.note.label.INoteLabel; import com.cstav.genshinstrument.client.gui.screen.instrument.partial.note.label.NoteLabelSupplier; import com.cstav.genshinstrument.client.gui.screen.instrument.partial.notegrid.NoteGridButton; -import com.cstav.genshinstrument.client.keyMaps.InstrumentKeyMappings; import com.cstav.genshinstrument.util.LabelUtil; -import com.mojang.blaze3d.platform.InputConstants.Key; import net.minecraft.network.chat.Component; -import net.minecraftforge.common.util.Lazy; /** * An enum holding all labels for {@code NoteGridButton}. @@ -19,68 +16,36 @@ */ public enum NoteGridLabel implements INoteLabel { KEYBOARD_LAYOUT((note) -> INoteLabel.upperComponent( - InstrumentKeyMappings.GRID_INSTRUMENT_MAPPINGS[ng(note).column][ng(note).row].getDisplayName() - )), - QWERTY((note) -> Component.translatable( - InstrumentKeyMappings.GRID_INSTRUMENT_MAPPINGS[ng(note).column][ng(note).row].getName() - .substring("key.keyboard.".length()).toUpperCase() + ng(note).getKey().getDisplayName() )), + QWERTY((note) -> + INoteLabel.getQwerty(ng(note).getKey()) + ), + NOTE_NAME((note) -> Component.literal( note.getCutNoteName() )), DO_RE_MI((note) -> LabelUtil.toDoReMi(note.getCutNoteName()) ), + NONE(NoteLabelSupplier.EMPTY); + private final NoteLabelSupplier labelSupplier; private NoteGridLabel(final NoteLabelSupplier labelSupplier) { this.labelSupplier = labelSupplier; } + public static INoteLabel[] availableVals() { + return INoteLabel.filterQwerty(values(), ModClientConfigs.GRID_LABEL_TYPE.get(), QWERTY); + } + @Override public NoteLabelSupplier getLabelSupplier() { return labelSupplier; } - - - private static final Lazy HAS_QWERTY = Lazy.of(() -> { - final String qwerty = "QWERTY"; - final Key[] keyRow = InstrumentKeyMappings.GRID_INSTRUMENT_MAPPINGS[0]; - - // Assuming there will be more than 6 entries here - for (int i = 0; i < qwerty.length(); i++) { - if (qwerty.charAt(i) != keyRow[i].getDisplayName().getString(1).charAt(0)) - return false; - } - - return true; - }); - - - public static NoteGridLabel[] availableVals() { - final NoteGridLabel[] vals = values(); - - // Ignore QWERTY if already using this layout - if (HAS_QWERTY.get() && (ModClientConfigs.GRID_LABEL_TYPE.get() != QWERTY)) { - final NoteGridLabel[] result = new NoteGridLabel[vals.length - 1]; - - // 2nd index to not go out of bounds - int j = 0; - for (int i = 0; i < vals.length; i++) { - if (vals[i] == QWERTY) - i++; - - result[j] = vals[i]; - j++; - } - - return result; - } - - return vals; - } private static NoteGridButton ng(final NoteButton btn) { diff --git a/src/main/java/com/cstav/genshinstrument/client/gui/screen/instrument/drum/AratakisGreatAndGloriousDrumScreen.java b/src/main/java/com/cstav/genshinstrument/client/gui/screen/instrument/drum/AratakisGreatAndGloriousDrumScreen.java index 0e06f936..e7fcb375 100644 --- a/src/main/java/com/cstav/genshinstrument/client/gui/screen/instrument/drum/AratakisGreatAndGloriousDrumScreen.java +++ b/src/main/java/com/cstav/genshinstrument/client/gui/screen/instrument/drum/AratakisGreatAndGloriousDrumScreen.java @@ -90,16 +90,16 @@ private LinearLayout createRow(DrumButtonType type, float widthPercent) { Orientation.HORIZONTAL ); - createButton(type, layout, type.getKeys().left, false); - createButton(type, layout, type.getKeys().right, true); + createButton(type, layout, false); + createButton(type, layout, true); return layout; } - private NoteButton createButton(DrumButtonType btnType, LinearLayout container, Key key, boolean isRight) { - final NoteButton btn = new DrumNoteButton(btnType, isRight, this); + private DrumNoteButton createButton(DrumButtonType btnType, LinearLayout container, boolean isRight) { + final DrumNoteButton btn = new DrumNoteButton(btnType, isRight, this); container.addChild(btn); - notes.put(key, btn); + notes.put(btn.getKey(), btn); return btn; } diff --git a/src/main/java/com/cstav/genshinstrument/client/gui/screen/instrument/drum/DrumNoteButton.java b/src/main/java/com/cstav/genshinstrument/client/gui/screen/instrument/drum/DrumNoteButton.java index 0b698c33..8f74c8ad 100644 --- a/src/main/java/com/cstav/genshinstrument/client/gui/screen/instrument/drum/DrumNoteButton.java +++ b/src/main/java/com/cstav/genshinstrument/client/gui/screen/instrument/drum/DrumNoteButton.java @@ -4,6 +4,7 @@ import com.cstav.genshinstrument.client.gui.screen.instrument.partial.note.NoteButton; import com.cstav.genshinstrument.client.gui.screen.instrument.partial.note.NoteButtonRenderer; import com.cstav.genshinstrument.networking.buttonidentifier.DrumNoteIdentifier; +import com.mojang.blaze3d.platform.InputConstants.Key; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; @@ -31,6 +32,11 @@ public DrumNoteIdentifier getIdentifier() { } + public Key getKey() { + return btnType.getKeys().getKey(isRight); + } + + @Override protected NoteButtonRenderer initNoteRenderer() { return new NoteButtonRenderer(this, btnType.getSpriteIndex(isRight), 3); diff --git a/src/main/java/com/cstav/genshinstrument/client/gui/screen/instrument/partial/note/label/INoteLabel.java b/src/main/java/com/cstav/genshinstrument/client/gui/screen/instrument/partial/note/label/INoteLabel.java index 0689139e..0096c6f4 100644 --- a/src/main/java/com/cstav/genshinstrument/client/gui/screen/instrument/partial/note/label/INoteLabel.java +++ b/src/main/java/com/cstav/genshinstrument/client/gui/screen/instrument/partial/note/label/INoteLabel.java @@ -1,6 +1,9 @@ package com.cstav.genshinstrument.client.gui.screen.instrument.partial.note.label; +import com.cstav.genshinstrument.client.ClientUtil; +import com.mojang.blaze3d.platform.InputConstants.Key; + import net.minecraft.network.chat.Component; import net.minecraft.network.chat.MutableComponent; @@ -17,6 +20,40 @@ public static MutableComponent upperComponent(final Component component) { } + public static MutableComponent getQwerty(final Key key) { + final String keyName = key.getName(); + return Component.literal( + // The QWERTY key is the last letter of the key name + String.valueOf(keyName.charAt(keyName.length() - 1)).toUpperCase() + ); + } + + /** + * @return All the values of this note label type, filtering QWERTY if already using it. + */ + public static INoteLabel[] filterQwerty(INoteLabel[] values, INoteLabel currentLabel, INoteLabel qwerty) { + // Ignore QWERTY if already using this layout + // Or if the user already selected it + if (!ClientUtil.ON_QWERTY.get() || (currentLabel.equals(qwerty))) + return values; + + + final INoteLabel[] result = new INoteLabel[values.length - 1]; + + // 2nd index to not go out of bounds + int j = 0; + for (int i = 0; i < values.length; i++) { + if (values[i].equals(qwerty)) + i++; + + result[j] = values[i]; + j++; + } + + return result; + } + + public NoteLabelSupplier getLabelSupplier(); /** * @return The translation key of this label diff --git a/src/main/java/com/cstav/genshinstrument/client/gui/screen/instrument/partial/notegrid/NoteGridButton.java b/src/main/java/com/cstav/genshinstrument/client/gui/screen/instrument/partial/notegrid/NoteGridButton.java index d0936699..9ea30572 100644 --- a/src/main/java/com/cstav/genshinstrument/client/gui/screen/instrument/partial/notegrid/NoteGridButton.java +++ b/src/main/java/com/cstav/genshinstrument/client/gui/screen/instrument/partial/notegrid/NoteGridButton.java @@ -3,9 +3,11 @@ import com.cstav.genshinstrument.client.config.ModClientConfigs; import com.cstav.genshinstrument.client.gui.screen.instrument.partial.note.NoteButton; import com.cstav.genshinstrument.client.gui.screen.instrument.partial.note.NoteButtonRenderer; +import com.cstav.genshinstrument.client.keyMaps.InstrumentKeyMappings; import com.cstav.genshinstrument.networking.buttonidentifier.NoteGridButtonIdentifier; import com.cstav.genshinstrument.sound.NoteSound; import com.cstav.genshinstrument.util.LabelUtil; +import com.mojang.blaze3d.platform.InputConstants.Key; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; @@ -55,6 +57,11 @@ public static NoteSound getSoundFromArr(AbstractGridInstrumentScreen gridInstrum } + public Key getKey() { + return InstrumentKeyMappings.GRID_INSTRUMENT_MAPPINGS[column][row]; + } + + @Override public NoteGridButtonIdentifier getIdentifier() { return new NoteGridButtonIdentifier(this); diff --git a/src/main/java/com/cstav/genshinstrument/client/gui/screen/options/instrument/DrumOptionsScren.java b/src/main/java/com/cstav/genshinstrument/client/gui/screen/options/instrument/DrumOptionsScren.java index 25d349f0..53fb1731 100644 --- a/src/main/java/com/cstav/genshinstrument/client/gui/screen/options/instrument/DrumOptionsScren.java +++ b/src/main/java/com/cstav/genshinstrument/client/gui/screen/options/instrument/DrumOptionsScren.java @@ -25,8 +25,8 @@ protected void saveLabel(INoteLabel newLabel) { } @Override - public DrumNoteLabel[] getLabels() { - return DrumNoteLabel.values(); + public INoteLabel[] getLabels() { + return DrumNoteLabel.availableVals(); } @Override public DrumNoteLabel getCurrentLabel() { diff --git a/src/main/java/com/cstav/genshinstrument/client/gui/screen/options/instrument/GridInstrumentOptionsScreen.java b/src/main/java/com/cstav/genshinstrument/client/gui/screen/options/instrument/GridInstrumentOptionsScreen.java index 22bfbe5b..882c7653 100644 --- a/src/main/java/com/cstav/genshinstrument/client/gui/screen/options/instrument/GridInstrumentOptionsScreen.java +++ b/src/main/java/com/cstav/genshinstrument/client/gui/screen/options/instrument/GridInstrumentOptionsScreen.java @@ -38,7 +38,7 @@ public GridInstrumentOptionsScreen(final Screen lastScreen) { @Override - public NoteGridLabel[] getLabels() { + public INoteLabel[] getLabels() { return NoteGridLabel.availableVals(); } @Override diff --git a/src/main/java/com/cstav/genshinstrument/client/gui/screen/options/instrument/partial/AbstractInstrumentOptionsScreen.java b/src/main/java/com/cstav/genshinstrument/client/gui/screen/options/instrument/partial/AbstractInstrumentOptionsScreen.java index 306ad9af..d40ae45a 100644 --- a/src/main/java/com/cstav/genshinstrument/client/gui/screen/options/instrument/partial/AbstractInstrumentOptionsScreen.java +++ b/src/main/java/com/cstav/genshinstrument/client/gui/screen/options/instrument/partial/AbstractInstrumentOptionsScreen.java @@ -16,7 +16,7 @@ import net.minecraftforge.api.distmarker.OnlyIn; @OnlyIn(Dist.CLIENT) -public class AbstractInstrumentOptionsScreen extends Screen { +public abstract class AbstractInstrumentOptionsScreen extends Screen { public final @Nullable AbstractInstrumentScreen instrumentScreen; public final Screen lastScreen; diff --git a/src/main/java/com/cstav/genshinstrument/client/keyMaps/InstrumentKeyMappings.java b/src/main/java/com/cstav/genshinstrument/client/keyMaps/InstrumentKeyMappings.java index 4fd9f79c..fe596ab7 100644 --- a/src/main/java/com/cstav/genshinstrument/client/keyMaps/InstrumentKeyMappings.java +++ b/src/main/java/com/cstav/genshinstrument/client/keyMaps/InstrumentKeyMappings.java @@ -86,6 +86,10 @@ private DrumKeys(final int left, final int right) { this.left = create(left); this.right = create(right); } + + public Key getKey(final boolean isRight) { + return isRight ? right : left; + } }