Skip to content

Commit

Permalink
Make button texts scrollable, fix some UX issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
LambdAurora committed Nov 27, 2024
1 parent 483c46a commit 1200be7
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 20 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# LambdAurora's ignore file
#
# v0.22
# v0.23

# JetBrains
.idea/
Expand Down Expand Up @@ -83,6 +83,7 @@ bin/
build/
dist/
lib/
!/lib/
!src/lib/
!src/**/lib/
obj/
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,10 @@
- Added placeholder to `SpruceTextFieldWidget` and `SpruceTextAreaWidget`.
- Improved `SpruceTabbedWidget` construction and management.
- Fixed change listener not triggering when deleting a selection in `SpruceTextFieldWidget` and `SpruceTextAreaWidget`.

## 6.2.0

- Added upside-down English translations ([#57](https://github.com/LambdAurora/SpruceUI/pull/57)).
- Made button texts scrolling like in modern Minecraft buttons.
- Improved handling of long titles in `SpruceSeparatorWidget`.
- Improved scrollbar refocus when entries change in `SpruceEntryListWidget`.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ dependencies {
And this to your `gradle.properties`:

```properties
spruceui_version=6.0.1+1.21.2
spruceui_version=6.2.0+1.21.3
```

It will JAR-in-JAR SpruceUI so users of your mod don't need to download it separately!
3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import java.util.regex.Pattern

plugins {
id("fabric-loom").version("1.8.+")
id("dev.yumi.gradle.licenser").version("1.1.+")
id("dev.yumi.gradle.licenser").version("2.0.+")
`java-library`
`maven-publish`
}
Expand Down Expand Up @@ -205,6 +205,7 @@ tasks.jar {

loom {
runtimeOnlyLog4j = true
accessWidenerPath = file("src/main/resources/spruceui.accesswidener")
runs {
register("testmodClient") {
client()
Expand Down
10 changes: 5 additions & 5 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# Done to increase the memory available to gradle.
org.gradle.jvmargs=-Xmx2G

minecraft_version=1.21.2
yalmm_mappings=7
loader_version=0.16.7
minecraft_version=1.21.3
yalmm_mappings=5
loader_version=0.16.9

# Mod Properties
mod_version=6.1.0+1.21.2
mod_version=6.2.0+1.21.3
maven_group=dev.lambdaurora
archives_base_name=spruceui

# Dependencies
# currently not on the main fabric site, check on the maven: https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api
fabric_api_version=0.106.1+1.21.2
fabric_api_version=0.110.0+1.21.3
modmenu_version=12.0.0-beta.1
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.11.1-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import dev.lambdaurora.spruceui.Tooltipable;
import dev.lambdaurora.spruceui.wrapper.VanillaButtonWrapper;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.client.gui.components.WidgetSprites;
import net.minecraft.client.gui.narration.NarratedElementType;
import net.minecraft.client.gui.narration.NarrationElementOutput;
Expand Down Expand Up @@ -154,9 +155,17 @@ protected void renderWidget(GuiGraphics graphics, int mouseX, int mouseY, float

protected void renderButton(GuiGraphics graphics, int mouseX, int mouseY, float delta) {
int color = this.active ? 16777215 : 10526880;
graphics.drawCenteredShadowedText(this.client.font, this.getMessage(),
this.getX() + this.getWidth() / 2, this.getY() + (this.getHeight() - 8) / 2,
color | MathHelper.ceil(this.alpha * 255.0F) << 24);
this.renderText(graphics, color | MathHelper.ceil(this.alpha * 255.0F) << 24);
}

protected void renderText(GuiGraphics graphics, int color) {
int margin = 2;
int startX = this.getX() + margin;
int endX = this.getX() + this.getWidth() - margin;
AbstractWidget.renderScrollingString(
graphics, this.client.font, this.getMessage(),
startX, this.getY(), endX, this.getY() + this.getHeight(), color
);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
import dev.lambdaurora.spruceui.util.ColorUtil;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.network.chat.Text;
import net.minecraft.util.FormattedCharSequence;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Optional;

/**
Expand All @@ -28,15 +30,15 @@
*/
public class SpruceSeparatorWidget extends AbstractSpruceWidget implements Tooltipable {
private Text title;
private List<FormattedCharSequence> titleToRender = List.of();
private Text tooltip;
private int tooltipTicks;
private long lastTick;

public SpruceSeparatorWidget(Position position, int width, @Nullable Text title) {
super(position);
this.width = width;
this.height = 9;
this.title = title;
this.setTitle(title);
}

@Deprecated
Expand All @@ -53,13 +55,40 @@ public Optional<Text> getTitle() {
return Optional.ofNullable(this.title);
}

protected int getTitleWidth() {
if (this.titleToRender.isEmpty()) {
return 0;
}

int max = this.getWidth() - 8;
int width = 0;

for (var line : this.titleToRender) {
width = Math.max(width, this.client.font.width(line));
}

return Math.min(width, max);
}

/**
* Sets the title of this separator widget.
*
* @param title the title
*/
public void setTitle(@Nullable Text title) {
this.title = title;

if (this.title != null) {
this.titleToRender = this.client.font.wrapLines(this.title, this.getWidth() - 8);
} else {
this.titleToRender = List.of();
}

this.height = this.client.font.lineHeight;

for (int i = 1; i < this.titleToRender.size(); i++) {
this.height += 2 + this.client.font.lineHeight;
}
}

@Override
Expand All @@ -83,16 +112,22 @@ public boolean requiresCursor() {

@Override
protected void renderWidget(GuiGraphics graphics, int mouseX, int mouseY, float delta) {
int lineY = this.getY() + this.getHeight() / 2 - 1;

if (this.title != null) {
int titleWidth = this.client.font.width(this.title);
int titleWidth = this.getTitleWidth();
int titleX = this.getX() + (this.getWidth() / 2 - titleWidth / 2);
if (this.width > titleWidth) {
graphics.fill(this.getX(), this.getY() + 4, titleX - 5, this.getY() + 6, ColorUtil.TEXT_COLOR);
graphics.fill(titleX + titleWidth + 5, this.getY() + 4, this.getX() + this.getWidth(), this.getY() + 6, ColorUtil.TEXT_COLOR);
graphics.fill(this.getX(), lineY, titleX - 5, lineY + 2, ColorUtil.TEXT_COLOR);
graphics.fill(titleX + titleWidth + 5, lineY, this.getX() + this.getWidth(), lineY + 2, ColorUtil.TEXT_COLOR);

int y = this.getY();
for (var line : this.titleToRender) {
int lineX = this.getX() + (this.getWidth() / 2 - this.client.font.width(line) / 2);
graphics.drawShadowedText(this.client.font, line, lineX, y, ColorUtil.WHITE);
y += 2 + this.client.font.lineHeight;
}
graphics.drawShadowedText(this.client.font, this.title, titleX, this.getY(), ColorUtil.WHITE);
} else {
graphics.fill(this.getX(), this.getY() + 4, this.getX() + this.getWidth(), this.getY() + 6, ColorUtil.TEXT_COLOR);
graphics.fill(this.getX(), lineY, this.getX() + this.getWidth(), lineY + 2, ColorUtil.TEXT_COLOR);
}

Tooltip.queueFor(this, mouseX, mouseY, this.tooltipTicks, i -> this.tooltipTicks = i, this.lastTick, i -> this.lastTick = i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,13 @@ public void setScrollAmount(double amount) {
}
}

/**
* Refocuses the scroll of this list.
*/
public void refocusScroll() {
this.setScrollAmount(this.getScrollAmount());
}

/**
* Returns the max scroll. The scroll amount can't go past this maximum.
*
Expand All @@ -190,6 +197,7 @@ public List<E> children() {
protected final void clearEntries() {
this.setFocused(null);
this.entries.clear();
this.refocusScroll();
}

protected void replaceEntries(Collection<E> newEntries) {
Expand All @@ -199,6 +207,7 @@ protected void replaceEntries(Collection<E> newEntries) {
if (!newEntries.contains(oldFocused)) {
this.setFocused(null);
}
this.refocusScroll();
}

protected @Nullable E getEntry(int index) {
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"dev.lambdaurora.spruceui.hud.HudManager::initialize"
]
},
"accessWidener": "spruceui.accesswidener",
"mixins": [
"spruceui.mixins.json"
],
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/spruceui.accesswidener
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
accessWidener v2 named

transitive-accessible method net/minecraft/client/gui/components/AbstractWidget renderScrollingString (Lnet/minecraft/client/gui/GuiGraphics;Lnet/minecraft/client/gui/Font;Lnet/minecraft/network/chat/Text;IIIII)V
transitive-accessible method net/minecraft/client/gui/components/AbstractWidget renderScrollingString (Lnet/minecraft/client/gui/GuiGraphics;Lnet/minecraft/client/gui/Font;Lnet/minecraft/network/chat/Text;IIIIII)V

0 comments on commit 1200be7

Please sign in to comment.