Skip to content

Commit

Permalink
Rich text (CleanroomMC#76)
Browse files Browse the repository at this point in the history
* rich text & a bunch of tweaks

* find hovering element in rich text

* rich tooltip

* split GuiContext

* fix overlay context

* fix & break mouse pos, i havent decided yet

* rich text widget with hoverable & interactable

* minor changes & fixes, i dont event know anymore

* fix space at start of line

* improvements, fixes, comments, javadoc
  • Loading branch information
brachy84 authored Sep 7, 2024
1 parent a670dd3 commit 4f3debe
Show file tree
Hide file tree
Showing 89 changed files with 2,863 additions and 808 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ ij_visual_guides =
ij_wrap_on_typing = false

[*.java]
max_line_length = 150
max_line_length = 140
ij_java_align_consecutive_assignments = false
ij_java_align_consecutive_variable_declarations = false
ij_java_align_group_field_declarations = false
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/cleanroommc/modularui/ModularUIConfig.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.cleanroommc.modularui;

import com.cleanroommc.modularui.screen.Tooltip;
import com.cleanroommc.modularui.screen.RichTooltip;

import net.minecraftforge.common.config.Config;
import net.minecraftforge.fml.relauncher.FMLLaunchHandler;
Expand All @@ -20,7 +20,7 @@ public class ModularUIConfig {

// Default direction
@Config.Comment("Default tooltip position around the widget or its panel.")
public static Tooltip.Pos tooltipPos = Tooltip.Pos.VERTICAL;
public static RichTooltip.Pos tooltipPos = RichTooltip.Pos.VERTICAL;

@Config.Comment("If true, widget outlines and widget information will be drawn.")
public static boolean guiDebugMode = FMLLaunchHandler.isDeobfuscatedEnvironment();
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/cleanroommc/modularui/api/IMuiScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.cleanroommc.modularui.screen.ClientScreenHandler;
import com.cleanroommc.modularui.screen.ModularScreen;

import com.cleanroommc.modularui.screen.viewport.ModularGuiContext;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.inventory.Slot;
Expand Down Expand Up @@ -82,7 +83,7 @@ default boolean isGuiContainer() {
}

/**
* Hovering widget is handled by {@link com.cleanroommc.modularui.screen.viewport.GuiContext}.
* Hovering widget is handled by {@link ModularGuiContext}.
* If it detects a slot, this method is called. Only affects {@link GuiContainer GuiContainers}.
*
* @param slot hovered slot
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/cleanroommc/modularui/api/ITheme.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.cleanroommc.modularui.api;

import com.cleanroommc.modularui.screen.Tooltip;
import com.cleanroommc.modularui.screen.RichTooltip;
import com.cleanroommc.modularui.theme.WidgetSlotTheme;
import com.cleanroommc.modularui.theme.WidgetTextFieldTheme;
import com.cleanroommc.modularui.theme.WidgetTheme;
Expand Down Expand Up @@ -64,5 +64,5 @@ default <T extends WidgetTheme> T getWidgetTheme(Class<T> clazz, String id) {

boolean getSmoothProgressBarOverride();

Tooltip.Pos getTooltipPosOverride();
RichTooltip.Pos getTooltipPosOverride();
}
26 changes: 26 additions & 0 deletions src/main/java/com/cleanroommc/modularui/api/MCHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@

import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.item.ItemStack;
import net.minecraft.util.text.TextFormatting;

import java.util.Collections;
import java.util.List;

public class MCHelper {

Expand Down Expand Up @@ -45,4 +52,23 @@ public static GuiScreen getCurrentScreen() {
Minecraft mc = getMc();
return mc != null ? mc.currentScreen : null;
}

public static FontRenderer getFontRenderer() {
if (hasMc()) return getMc().fontRenderer;
return null;
}

public static List<String> getItemToolTip(ItemStack item) {
if (!hasMc()) return Collections.emptyList();
if (getMc().currentScreen != null) return getMc().currentScreen.getItemToolTip(item);
List<String> list = item.getTooltip(getPlayer(), getMc().gameSettings.advancedItemTooltips ? ITooltipFlag.TooltipFlags.ADVANCED : ITooltipFlag.TooltipFlags.NORMAL);
for (int i = 0; i < list.size(); ++i) {
if (i == 0) {
list.set(i, item.getItem().getForgeRarity(item).getColor() + list.get(i));
} else {
list.set(i, TextFormatting.GRAY + list.get(i));
}
}
return list;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.cleanroommc.modularui.drawable.DrawableArray;
import com.cleanroommc.modularui.drawable.Icon;
import com.cleanroommc.modularui.screen.viewport.GuiContext;
import com.cleanroommc.modularui.screen.viewport.ModularGuiContext;
import com.cleanroommc.modularui.theme.WidgetTheme;
import com.cleanroommc.modularui.widget.Widget;
import com.cleanroommc.modularui.widget.sizer.Area;
Expand Down Expand Up @@ -171,7 +172,7 @@ public DrawableWidget(IDrawable drawable) {

@SideOnly(Side.CLIENT)
@Override
public void draw(GuiContext context, WidgetTheme widgetTheme) {
public void draw(ModularGuiContext context, WidgetTheme widgetTheme) {
this.drawable.drawAtZero(context, getArea(), widgetTheme);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.cleanroommc.modularui.api.drawable;

import com.cleanroommc.modularui.screen.RichTooltip;

import com.cleanroommc.modularui.widget.sizer.Area;

import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;

public interface IHoverable extends IIcon {

/**
* Called every frame this hoverable is hovered inside a {@link com.cleanroommc.modularui.drawable.text.RichText}.
*/
default void onHover() {}

@Nullable
default RichTooltip getTooltip() {
return null;
}

void setRenderedAt(int x, int y);

Area getRenderedArea();
}
10 changes: 10 additions & 0 deletions src/main/java/com/cleanroommc/modularui/api/drawable/IIcon.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.cleanroommc.modularui.api.drawable;

import com.cleanroommc.modularui.drawable.HoverableIcon;
import com.cleanroommc.modularui.drawable.InteractableIcon;
import com.cleanroommc.modularui.widget.sizer.Box;

/**
Expand All @@ -22,5 +24,13 @@ public interface IIcon extends IDrawable {
*/
Box getMargin();

default HoverableIcon asHoverable() {
return new HoverableIcon(this);
}

default InteractableIcon asInteractable() {
return new InteractableIcon(this);
}

IIcon EMPTY_2PX = EMPTY.asIcon().height(2);
}
51 changes: 40 additions & 11 deletions src/main/java/com/cleanroommc/modularui/api/drawable/IKey.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
package com.cleanroommc.modularui.api.drawable;

import com.cleanroommc.modularui.drawable.AnimatedText;
import com.cleanroommc.modularui.drawable.StyledText;
import com.cleanroommc.modularui.drawable.TextRenderer;
import com.cleanroommc.modularui.drawable.keys.CompoundKey;
import com.cleanroommc.modularui.drawable.keys.DynamicKey;
import com.cleanroommc.modularui.drawable.keys.LangKey;
import com.cleanroommc.modularui.drawable.keys.StringKey;
import com.cleanroommc.modularui.drawable.Icon;
import com.cleanroommc.modularui.drawable.text.*;
import com.cleanroommc.modularui.screen.viewport.GuiContext;
import com.cleanroommc.modularui.theme.WidgetTheme;
import com.cleanroommc.modularui.utils.Alignment;
import com.cleanroommc.modularui.utils.JsonHelper;
import com.cleanroommc.modularui.widgets.TextWidget;

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

Expand All @@ -31,7 +27,9 @@ public interface IKey extends IDrawable {

TextRenderer renderer = new TextRenderer();

IKey EMPTY = new StringKey("");
IKey EMPTY = str("");
IKey LINE_FEED = str("\n");
IKey SPACE = str(" ");

/**
* Creates a translated text.
Expand Down Expand Up @@ -104,10 +102,18 @@ static IKey str(@NotNull String key) {
* @param args arguments
* @return text key
*/
static IKey format(@NotNull String key, @Nullable Object... args) {
static IKey str(@NotNull String key, @Nullable Object... args) {
return new StringKey(key, args);
}

/**
* @deprecated renamed to str()
*/
@Deprecated
static IKey format(@NotNull String key, @Nullable Object... args) {
return str(key, args);
}

/**
* Creates a composed text key.
*
Expand All @@ -129,10 +135,17 @@ static IKey dynamic(@NotNull Supplier<String> getter) {
}

/**
* @return the current formatted string
* @return the current unformatted string
*/
String get();

/**
* @return the current formatted string
*/
default String getFormatted() {
return get();
}

@SideOnly(Side.CLIENT)
@Override
default void draw(GuiContext context, int x, int y, int width, int height, WidgetTheme widgetTheme) {
Expand All @@ -141,7 +154,7 @@ default void draw(GuiContext context, int x, int y, int width, int height, Widge
renderer.setAlignment(Alignment.Center, width, height);
renderer.setScale(1f);
renderer.setPos(x, y);
renderer.draw(get());
renderer.draw(getFormatted());
}

@Override
Expand All @@ -157,6 +170,13 @@ default AnimatedText withAnimation() {
return new AnimatedText(this);
}

IKey format(TextFormatting formatting);

default IKey format(TextFormatting... formatting) {
for (TextFormatting tf : formatting) format(tf);
return this;
}

default StyledText alignment(Alignment alignment) {
return withStyle().alignment(alignment);
}
Expand All @@ -173,6 +193,15 @@ default StyledText shadow(boolean shadow) {
return withStyle().shadow(shadow);
}

@Override
default Icon asIcon() {
return new Icon(this);
}

default KeyIcon asTextIcon() {
return new KeyIcon(this);
}

@Override
default void loadFromJson(JsonObject json) {
if (json.has("color") || json.has("shadow") || json.has("align") || json.has("alignment") || json.has("scale")) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.cleanroommc.modularui.api.drawable;

import com.cleanroommc.modularui.drawable.text.Spacer;
import com.cleanroommc.modularui.utils.Alignment;

public interface IRichTextBuilder<T extends IRichTextBuilder<T>> {

T getThis();

IRichTextBuilder<?> getRichText();

default T add(String s) {
getRichText().add(s);
return getThis();
}

default T add(IDrawable drawable) {
getRichText().add(drawable);
return getThis();
}

default T addLine(ITextLine line) {
getRichText().addLine(line);
return getThis();
}

default T addLine(IDrawable line) {
getRichText().add(line).newLine();
return getThis();
}

default T newLine() {
return add(IKey.LINE_FEED);
}

default T space() {
return add(IKey.SPACE);
}

default T spaceLine(int pixelSpace) {
return addLine(Spacer.of(pixelSpace));
}

default T addElements(Iterable<IDrawable> drawables) {
for (IDrawable drawable : drawables) {
getRichText().add(drawable);
}
return getThis();
}

default T addDrawableLines(Iterable<IDrawable> drawables) {
for (IDrawable drawable : drawables) {
getRichText().add(drawable).newLine();
}
return getThis();
}

default T addStringLines(Iterable<String> drawables) {
for (String drawable : drawables) {
getRichText().add(drawable).newLine();
}
return getThis();
}

default T clearText() {
getRichText().clearText();
return getThis();
}

default T alignment(Alignment alignment) {
getRichText().alignment(alignment);
return getThis();
}

default T textColor(int color) {
getRichText().textColor(color);
return getThis();
}

default T scale(float scale) {
getRichText().scale(scale);
return getThis();
}

default T textShadow(boolean shadow) {
getRichText().textShadow(shadow);
return getThis();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.cleanroommc.modularui.api.drawable;

import com.cleanroommc.modularui.screen.viewport.GuiContext;

import net.minecraft.client.gui.FontRenderer;

public interface ITextLine {

int getWidth();

int getHeight(FontRenderer fr);

void draw(GuiContext context, FontRenderer fr, float x, float y, int color, boolean shadow);

Object getHoveringElement(FontRenderer fr, int x, int y);

}
Loading

0 comments on commit 4f3debe

Please sign in to comment.