Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public Animator bounds(float min, float max) {

/**
* The duration of this animation in milliseconds. Note this is not 100% accurate.
* Usually it's plus minus 2ms, but can rarely be more.
* Usually it's ±2ms, but can rarely be more.
*
* @param duration duration in milliseconds
* @return this
Expand Down
32 changes: 23 additions & 9 deletions src/main/java/com/cleanroommc/modularui/api/drawable/IKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.cleanroommc.modularui.screen.viewport.GuiContext;
import com.cleanroommc.modularui.theme.WidgetTheme;
import com.cleanroommc.modularui.utils.Alignment;
import com.cleanroommc.modularui.utils.FloatSupplier;
import com.cleanroommc.modularui.utils.JsonHelper;
import com.cleanroommc.modularui.widgets.TextWidget;

Expand All @@ -25,6 +26,7 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.function.BooleanSupplier;
import java.util.function.IntSupplier;
import java.util.function.Supplier;

Expand Down Expand Up @@ -242,15 +244,15 @@ default int getDefaultHeight() {
}

default float getScale() {
return 1f;
return 1.0f;
}

default TextWidget<?> asWidget() {
return new TextWidget<>(this);
}

default StyledText withStyle() {
return new StyledText(this);
default StyledText<?> withStyle() {
return new StyledText<>(this);
}

default AnimatedText withAnimation() {
Expand Down Expand Up @@ -284,23 +286,35 @@ default IKey removeFormatColor() {

IKey removeStyle();

default StyledText alignment(Alignment alignment) {
default StyledText<?> alignment(@Nullable Alignment alignment) {
return withStyle().alignment(alignment);
}

default StyledText color(int color) {
default StyledText<?> alignment(@Nullable Supplier<@NotNull Alignment> alignment) {
return withStyle().alignment(alignment);
}

default StyledText<?> color(int color) {
return withStyle().color(() -> color);
}

default StyledText color(@Nullable IntSupplier color) {
default StyledText<?> color(@Nullable IntSupplier color) {
return withStyle().color(color);
}

default StyledText scale(float scale) {
default StyledText<?> scale(float scale) {
return withStyle().scale(scale);
}

default StyledText shadow(@Nullable Boolean shadow) {
default StyledText<?> scale(@Nullable FloatSupplier scale) {
return withStyle().scale(scale);
}

default StyledText<?> shadow(@Nullable Boolean shadow) {
return withStyle().shadow(shadow);
}

default StyledText<?> shadow(@Nullable BooleanSupplier shadow) {
return withStyle().shadow(shadow);
}

Expand All @@ -316,7 +330,7 @@ default KeyIcon asTextIcon() {
@Override
default void loadFromJson(JsonObject json) {
if (json.has("color") || json.has("shadow") || json.has("align") || json.has("alignment") || json.has("scale")) {
StyledText styledText = this instanceof StyledText styledText1 ? styledText1 : withStyle();
StyledText<?> styledText = this instanceof StyledText<?> styledText1 ? styledText1 : withStyle();
if (json.has("color")) {
styledText.color(JsonHelper.getInt(json, 0, "color"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,12 @@
import com.cleanroommc.modularui.api.drawable.IKey;
import com.cleanroommc.modularui.screen.viewport.GuiContext;
import com.cleanroommc.modularui.theme.WidgetTheme;
import com.cleanroommc.modularui.utils.Alignment;

import net.minecraft.client.Minecraft;
import net.minecraft.util.text.TextFormatting;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import org.jetbrains.annotations.Nullable;

import java.util.function.IntSupplier;

public class AnimatedText extends StyledText {
public class AnimatedText extends StyledText<AnimatedText> {

private String fullString;
private String currentString = "";
Expand Down Expand Up @@ -104,36 +98,6 @@ public AnimatedText forward(boolean forward) {
return this;
}

@Override
public AnimatedText style(TextFormatting formatting) {
return (AnimatedText) super.style(formatting);
}

@Override
public AnimatedText alignment(Alignment alignment) {
return (AnimatedText) super.alignment(alignment);
}

@Override
public AnimatedText color(int color) {
return color(() -> color);
}

@Override
public AnimatedText color(@Nullable IntSupplier color) {
return (AnimatedText) super.color(color);
}

@Override
public AnimatedText scale(float scale) {
return (AnimatedText) super.scale(scale);
}

@Override
public AnimatedText shadow(@Nullable Boolean shadow) {
return (AnimatedText) super.shadow(shadow);
}

/**
* How fast the characters appear
*
Expand Down
15 changes: 10 additions & 5 deletions src/main/java/com/cleanroommc/modularui/drawable/text/BaseKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.apache.commons.lang3.NotImplementedException;
import org.jetbrains.annotations.Nullable;

public abstract class BaseKey implements IKey {
public abstract class BaseKey<K extends BaseKey<K>> implements IKey {

private FormattingState formatting;

Expand All @@ -17,28 +17,33 @@ public String getFormatted(@Nullable FormattingState parentFormatting) {
}

@Override
public BaseKey style(@Nullable TextFormatting formatting) {
public K style(@Nullable TextFormatting formatting) {
if (this.formatting == null) {
this.formatting = new FormattingState();
}
if (formatting == null) this.formatting.forceDefaultColor();
else this.formatting.add(formatting, false);
return this;
return getThis();
}

@Override
public IKey removeStyle() {
public K removeStyle() {
if (this.formatting != null) {
this.formatting.reset();
}
return this;
return getThis();
}

@Override
public @Nullable FormattingState getFormatting() {
return formatting;
}

@SuppressWarnings("unchecked")
public K getThis() {
return (K) this;
}

@Override
public String toString() {
return getFormatted();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

import com.cleanroommc.modularui.api.drawable.IKey;

import net.minecraft.util.text.TextFormatting;

import org.jetbrains.annotations.Nullable;

public class CompoundKey extends BaseKey {
public class CompoundKey extends BaseKey<CompoundKey> {

private static final IKey[] EMPTY = new IKey[0];

Expand Down Expand Up @@ -43,4 +41,4 @@ private String toString(boolean formatted, @Nullable FormattingState parentForma
public IKey[] getKeys() {
return this.keys;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import java.util.function.Supplier;

public class DynamicKey extends BaseKey {
public class DynamicKey extends BaseKey<DynamicKey> {

private final Supplier<IKey> supplier;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.util.Objects;
import java.util.function.Supplier;

public class LangKey extends BaseKey {
public class LangKey extends BaseKey<LangKey> {

private final Supplier<String> keySupplier;
private final Supplier<Object[]> argsSupplier;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import java.util.Objects;

public class StringKey extends BaseKey {
public class StringKey extends BaseKey<StringKey> {

private final String string;
private final Object[] args;
Expand All @@ -29,4 +29,4 @@ public String getFormatted(@Nullable FormattingState parentFormatting) {
String text = FontRenderHelper.formatArgs(this.args, FormattingState.merge(parentFormatting, getFormatting()), this.string, false);
return FontRenderHelper.format(getFormatting(), parentFormatting, text);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,31 @@
import com.cleanroommc.modularui.screen.viewport.GuiContext;
import com.cleanroommc.modularui.theme.WidgetTheme;
import com.cleanroommc.modularui.utils.Alignment;
import com.cleanroommc.modularui.utils.FloatSupplier;
import com.cleanroommc.modularui.widgets.TextWidget;

import net.minecraft.util.text.TextFormatting;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.function.BooleanSupplier;
import java.util.function.IntSupplier;
import java.util.function.Supplier;

public class StyledText extends BaseKey {
public class StyledText<K extends StyledText<K>> extends BaseKey<K> {

private final IKey key;
private Alignment alignment = Alignment.Center;
@NotNull
private Supplier<@NotNull Alignment> alignment = () -> Alignment.Center;
@Nullable
private IntSupplier color = null;
private Boolean shadow = null;
private float scale = 1f;
@Nullable
private BooleanSupplier shadow = null;
@Nullable
private FloatSupplier scale = null;

public StyledText(IKey key) {
this.key = key;
Expand All @@ -39,16 +47,16 @@ public String getFormatted() {
@SideOnly(Side.CLIENT)
@Override
public void draw(GuiContext context, int x, int y, int width, int height, WidgetTheme widgetTheme) {
renderer.setAlignment(this.alignment, width, height);
renderer.setAlignment(getAlignment(), width, height);
renderer.setColor(this.color != null ? this.color.getAsInt() : widgetTheme.getTextColor());
renderer.setScale(this.scale);
renderer.setScale(getScale());
renderer.setPos(x, y);
renderer.setShadow(this.shadow != null ? this.shadow : widgetTheme.getTextShadow());
renderer.setShadow(this.shadow != null ? this.shadow.getAsBoolean() : widgetTheme.getTextShadow());
renderer.draw(getFormatted());
}

public Alignment getAlignment() {
return this.alignment;
public @NotNull Alignment getAlignment() {
return this.alignment.get();
}

public @Nullable IntSupplier getColor() {
Expand All @@ -57,46 +65,65 @@ public Alignment getAlignment() {

@Override
public float getScale() {
return this.scale;
return this.scale == null ? super.getScale() : this.scale.getAsFloat();
}

public @Nullable Boolean isShadow() {
return this.shadow;
return this.shadow == null ? null : this.shadow.getAsBoolean();
}

@Override
public BaseKey style(TextFormatting formatting) {
public K style(TextFormatting formatting) {
this.key.style(formatting);
return this;
return getThis();
}

@Override
public StyledText alignment(Alignment alignment) {
this.alignment = alignment;
return this;
public K alignment(@Nullable Alignment alignment) {
return alignment(alignment == null ? null : () -> alignment);
}

@Override
public StyledText color(int color) {
public K alignment(@Nullable Supplier<@NotNull Alignment> alignment) {
this.alignment = alignment == null ? () -> Alignment.Center : alignment;
return getThis();
}

@Override
public K color(int color) {
return color(() -> color);
}

@Override
public StyledText color(@Nullable IntSupplier color) {
public K color(@Nullable IntSupplier color) {
this.color = color;
return this;
return getThis();
}

@Override
public StyledText scale(float scale) {
public K scale(float scale) {
return scale(() -> scale);
}

public K scale(@Nullable FloatSupplier scale) {
this.scale = scale;
return this;
return getThis();
}

@Override
public K shadow(@Nullable Boolean shadow) {
if (shadow == null) {
return shadow((BooleanSupplier) null);
} else {
boolean hasShadow = shadow;
return shadow(() -> hasShadow);
}
}

@Override
public StyledText shadow(@Nullable Boolean shadow) {
public K shadow(@Nullable BooleanSupplier shadow) {
this.shadow = shadow;
return this;
return getThis();
}

@Override
Expand Down
Loading