Skip to content

Commit

Permalink
Add a multiline text widget
Browse files Browse the repository at this point in the history
Closes #36.
  • Loading branch information
Juuxel committed Mar 10, 2020
1 parent 3e98462 commit ef2d935
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

import javax.annotation.Nullable;

/**
* A single-line label widget.
*/
public class WLabel extends WWidget {
protected Text text;
protected Alignment alignment = Alignment.LEFT;
Expand All @@ -37,6 +40,10 @@ public WLabel(String text) {
this(text, DEFAULT_TEXT_COLOR);
}

public WLabel(Text text) {
this(text, DEFAULT_TEXT_COLOR);
}

@Override
public void paintBackground(int x, int y, int mouseX, int mouseY) {
String translated = text.asFormattedString();
Expand Down
133 changes: 133 additions & 0 deletions src/main/java/io/github/cottonmc/cotton/gui/widget/WText.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package io.github.cottonmc.cotton.gui.widget;

import io.github.cottonmc.cotton.gui.client.LibGuiClient;
import io.github.cottonmc.cotton.gui.client.ScreenDrawing;
import io.github.cottonmc.cotton.gui.widget.data.Alignment;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.util.Texts;
import net.minecraft.text.Text;

import javax.annotation.Nullable;
import java.util.List;
import java.util.Objects;

/**
* A multiline label widget.
*
* @since 1.8.0
*/
public class WText extends WWidget {
protected Text text;
protected int color;
protected int darkmodeColor;
protected Alignment alignment = Alignment.LEFT;
private List<Text> wrappedLines;

public WText(Text text) {
this(text, WLabel.DEFAULT_TEXT_COLOR);
}

public WText(Text text, int color) {
this.text = Objects.requireNonNull(text, "text must not be null");
this.color = color;
this.darkmodeColor = (color == WLabel.DEFAULT_TEXT_COLOR) ? WLabel.DEFAULT_DARKMODE_TEXT_COLOR : color;
}

@Override
public void setSize(int x, int y) {
super.setSize(x, y);
if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) {
wrapLines();
}
}

@Environment(EnvType.CLIENT)
private void wrapLines() {
TextRenderer font = MinecraftClient.getInstance().textRenderer;
wrappedLines = Texts.wrapLines(text, width, font, true, true);
}

@Environment(EnvType.CLIENT)
@Nullable
protected Text getTextAt(int x, int y) {
TextRenderer font = MinecraftClient.getInstance().textRenderer;
int lineIndex = y / font.fontHeight;

if (lineIndex >= 0 && lineIndex < wrappedLines.size()) {
Text line = wrappedLines.get(lineIndex);
int xi = 0;
for (Text part : line) {
xi += font.getStringWidth(part.asFormattedString());
if (xi > x) return part;
}
}

return null;
}

@Environment(EnvType.CLIENT)
@Override
public void paintBackground(int x, int y, int mouseX, int mouseY) {
TextRenderer font = MinecraftClient.getInstance().textRenderer;
for (int i = 0; i < wrappedLines.size(); i++) {
Text line = wrappedLines.get(i);
int c = LibGuiClient.config.darkMode ? darkmodeColor : color;
String str = line.asFormattedString();

ScreenDrawing.drawString(str, alignment, x, y + i * font.fontHeight, width, c);
}
}

@Override
public void onClick(int x, int y, int button) {
if (button != 0) return; // only left clicks

Text hoveredText = getTextAt(x, y);
if (hoveredText != null) {
MinecraftClient.getInstance().currentScreen.handleTextClick(hoveredText);
}
}

public Text getText() {
return text;
}

public WText setText(Text text) {
this.text = text;

if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT) {
wrapLines();
}

return this;
}

public int getColor() {
return color;
}

public WText setColor(int color) {
this.color = color;
return this;
}

public WText setDarkmodeColor(int darkmodeColor) {
this.darkmodeColor = darkmodeColor;
return this;
}

public WText setColor(int color, int darkmodeColor) {
setColor(color);
setDarkmodeColor(darkmodeColor);
return this;
}

public WText disableDarkmode() {
this.darkmodeColor = this.color;
return this;
}
}

0 comments on commit ef2d935

Please sign in to comment.