generated from FabricMC/fabric-example-mod
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #36.
- Loading branch information
Showing
2 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
src/main/java/io/github/cottonmc/cotton/gui/widget/WText.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |