Skip to content

Commit

Permalink
Fixed applyTweakerMoreOptionLabelGlobally does not work with option…
Browse files Browse the repository at this point in the history
…s with custom translation key

now it works correctly with MasaGadget
  • Loading branch information
Fallen-Breath committed Feb 12, 2022
1 parent 600e28f commit 15a1eb7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package me.fallenbreath.tweakermore.gui;

import fi.dy.masa.malilib.gui.widgets.WidgetLabel;
import me.fallenbreath.tweakermore.util.StringUtil;

import java.util.Arrays;
import java.util.function.Function;

/**
Expand All @@ -13,33 +11,33 @@
public class TweakerMoreOptionLabel extends WidgetLabel
{
public static final double TRANSLATION_SCALE = 0.65;
private final String[] originalTexts;
private final boolean showOriginalTexts;
private final String[] originalLines;
private final boolean showOriginalLines;

public TweakerMoreOptionLabel(int x, int y, int width, int height, int textColor, String[] lines, Function<String, String> lineModifier)
public TweakerMoreOptionLabel(int x, int y, int width, int height, int textColor, String[] displayLines, String[] originalLines, Function<String, String> lineModifier)
{
super(x, y, width, height, textColor, lines);
this.originalTexts = Arrays.stream(lines).map(StringUtil::removeTweakerMoreNameSpacePrefix).toArray(String[]::new);
boolean showOriginalNames = false;
for (int i = 0; i < this.originalTexts.length; i++)
super(x, y, width, height, textColor, displayLines);
this.originalLines = originalLines;
boolean showOriginalLines = false;
for (int i = 0; i < this.originalLines.length; i++)
{
String linesToDisplay = this.labels.get(i);
if (!this.originalTexts[i].equals(linesToDisplay))
if (!this.originalLines[i].equals(linesToDisplay))
{
showOriginalNames = true;
showOriginalLines = true;
}
this.labels.set(i, lineModifier.apply(linesToDisplay));
}
this.showOriginalTexts = showOriginalNames;
this.showOriginalLines = showOriginalLines;
}

public String[] getOriginalTexts()
public String[] getOriginalLines()
{
return this.originalTexts;
return this.originalLines;
}

public boolean shouldShowOriginalTexts()
public boolean shouldShowOriginalLines()
{
return this.showOriginalTexts;
return this.showOriginalLines;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public WidgetLabelMixin(int x, int y, int width, int height)
private boolean shouldUseTranslatedOptionLabelLogic()
{
WidgetLabel self = (WidgetLabel)(Object)this;
return self instanceof TweakerMoreOptionLabel && ((TweakerMoreOptionLabel)self).shouldShowOriginalTexts();
return self instanceof TweakerMoreOptionLabel && ((TweakerMoreOptionLabel)self).shouldShowOriginalLines();
}

@ModifyVariable(
Expand Down Expand Up @@ -71,7 +71,7 @@ private void translatedOptionLabelRenderTranslation(int mouseX, int mouseY, bool
{
int color = darkerColor(this.textColor);
double scale = TweakerMoreOptionLabel.TRANSLATION_SCALE;
String originText = ((TweakerMoreOptionLabel)(Object)this).getOriginalTexts()[i];
String originText = ((TweakerMoreOptionLabel)(Object)this).getOriginalLines()[i];
int x = this.x + (this.centered ? this.width / 2 : 0);
int y = (int)(yTextStart + (this.labels.size() + i * scale + 0.2) * fontHeight);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

import java.util.Objects;
import java.util.function.Function;
import java.util.stream.IntStream;

@Mixin(WidgetConfigOption.class)
public abstract class WidgetListConfigOptionMixin extends WidgetConfigOptionBase<GuiConfigsBase.ConfigOptionWrapper>
Expand Down Expand Up @@ -108,21 +107,25 @@ private void useMyBetterOptionLabelForTweakerMore(Args args, int x_, int y_, flo
int height = args.get(3);
int textColor = args.get(4);
String[] lines = args.get(5);
if (lines.length != 1)
{
return;
}

args.set(5, null); // cancel original call

Function<String, String> modifier = s -> s;
if (isTweakerMoreConfigGui())
{
IntStream.range(0, lines.length).forEach(i -> lines[i] = StringUtil.TWEAKERMORE_NAMESPACE_PREFIX + lines[i]);
lines[0] = StringUtil.TWEAKERMORE_NAMESPACE_PREFIX + lines[0];
if (!TweakerMoreConfigs.getOptionFromConfig(config).map(TweakerMoreOption::isEnabled).orElse(true))
{
modifier = s -> GuiBase.TXT_DARK_RED + s + GuiBase.TXT_RST;
}
}
TweakerMoreOptionLabel label = new TweakerMoreOptionLabel(x, y, width, height, textColor, lines, modifier);
TweakerMoreOptionLabel label = new TweakerMoreOptionLabel(x, y, width, height, textColor, lines, new String[]{config.getName()}, modifier);
this.addWidget(label);
this.showOriginalTextsThisTime = label.shouldShowOriginalTexts();
this.showOriginalTextsThisTime = label.shouldShowOriginalLines();
}
else
{
Expand Down

0 comments on commit 15a1eb7

Please sign in to comment.