Skip to content

Commit

Permalink
Migrate Fabric changes to Forge
Browse files Browse the repository at this point in the history
  • Loading branch information
StavWasPlayZ committed Aug 30, 2023
1 parent f771049 commit 8b25104
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 61 deletions.
17 changes: 17 additions & 0 deletions src/main/java/com/cstav/genshinstrument/client/ClientUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,35 @@
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;
import net.minecraft.client.gui.layouts.GridLayout;
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<Boolean> 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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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())
),
Expand All @@ -28,6 +29,7 @@ public enum DrumNoteLabel implements INoteLabel {
DO_RE_MI((note) ->
LabelUtil.toDoReMi(note.getCutNoteName())
),

NONE(NoteLabelSupplier.EMPTY);


Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}.
Expand All @@ -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<Boolean> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public GridInstrumentOptionsScreen(final Screen lastScreen) {


@Override
public NoteGridLabel[] getLabels() {
public INoteLabel[] getLabels() {
return NoteGridLabel.availableVals();
}
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}


Expand Down

0 comments on commit 8b25104

Please sign in to comment.