Skip to content

Commit

Permalink
SpruceUI v6.1.0+1.21.2.
Browse files Browse the repository at this point in the history
  • Loading branch information
LambdAurora committed Oct 22, 2024
1 parent 584e74c commit 45a6f3d
Show file tree
Hide file tree
Showing 9 changed files with 202 additions and 25 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,10 @@
### 6.0.1

- Fixed bad Minecraft version range.

## 6.1.0

- Added a way to use `SpruceToggleBooleanOption` without text.
- Added placeholder to `SpruceTextFieldWidget` and `SpruceTextAreaWidget`.
- Improved `SpruceTabbedWidget` construction and management.
- Fixed change listener not triggering when deleting a selection in `SpruceTextFieldWidget` and `SpruceTextAreaWidget`.
8 changes: 4 additions & 4 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-rc1
yalmm_mappings=4
minecraft_version=1.21.2
yalmm_mappings=7
loader_version=0.16.7

# Mod Properties
mod_version=6.0.1+1.21.2
mod_version=6.1.0
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.0+1.21.2
fabric_api_version=0.106.1+1.21.2
modmenu_version=12.0.0-beta.1
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,28 @@
* Works the as {@link SpruceBooleanOption} but uses a toggle switch instead.
*
* @author LambdAurora
* @version 3.0.0
* @version 6.1.0
* @since 2.0.0
*/
public class SpruceToggleBooleanOption extends SpruceBooleanOption {
public SpruceToggleBooleanOption(String key, Supplier<Boolean> getter, Consumer<Boolean> setter, @Nullable Text tooltip) {
private final boolean showMessage;

public SpruceToggleBooleanOption(String key, Supplier<Boolean> getter, Consumer<Boolean> setter, @Nullable Text tooltip, boolean showMessage) {
super(key, getter, setter, tooltip, false);
this.showMessage = showMessage;
}

public SpruceToggleBooleanOption(String key, Supplier<Boolean> getter, Consumer<Boolean> setter, @Nullable Text tooltip) {
this(key, getter, setter, tooltip, true);
}

@Override
public SpruceWidget createWidget(Position position, int width) {
var button = new SpruceToggleSwitch(position, width, 20, this.getDisplayText(), (btn, newValue) -> {
this.set();
btn.setMessage(this.getDisplayText());
}, this.get());
this.getOptionTooltip().ifPresent(btn::setTooltip);
}, this.get(), this.showMessage);
this.getOptionTooltip().ifPresent(button::setTooltip);
return button;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,44 @@
* Represents a container widget with tabs.
*
* @author LambdAurora
* @version 5.0.0
* @version 6.1.0
* @since 2.0.0
*/
public class SpruceTabbedWidget extends AbstractSpruceParentWidget<SpruceWidget> {
private final Text title;
private final List<FormattedCharSequence> title;
private final SideTabList list;
private final Position anchor;
private boolean isLeft = false;

public SpruceTabbedWidget(Position position, int width, int height, @Nullable Text title) {
this(position, width, height, title, Math.max(100, width / 8), title != null ? 20 : 0);
this(position, width, height, title, Math.max(100, width / 8));
}

public SpruceTabbedWidget(Position position, int width, int height, @Nullable Text title, int sideWidth) {
super(position, SpruceWidget.class);
this.width = width;
this.height = height;
this.title = title != null ? this.client.font.wrapLines(title, sideWidth - 8) : null;
int sideTopOffset = title == null ? 0 : 6 + (this.title.size() * this.client.font.lineHeight + 4);
this.list = new SideTabList(
Position.of(position, 0, sideTopOffset),
sideWidth,
height - sideTopOffset
);
this.anchor = Position.of(this, this.list.getWidth(), 0);
}

public SpruceTabbedWidget(Position position, int width, int height, @Nullable Text title, int sideWidth,
int sideTopOffset) {
super(position, SpruceWidget.class);
this.width = width;
this.height = height;
this.title = title;
this.list = new SideTabList(Position.of(position, 0, sideTopOffset), sideWidth, height - sideTopOffset);
this.title = title != null ? this.client.font.wrapLines(title, sideWidth - 8) : null;
this.list = new SideTabList(
Position.of(position, 0, sideTopOffset),
sideWidth,
height - sideTopOffset
);
this.anchor = Position.of(this, this.list.getWidth(), 0);
}

Expand Down Expand Up @@ -154,8 +172,11 @@ public boolean onNavigation(NavigationDirection direction, boolean tab) {
@Override
protected void renderWidget(GuiGraphics graphics, int mouseX, int mouseY, float delta) {
if (this.title != null) {
graphics.drawCenteredShadowedText(this.client.font, this.title, this.getX() + this.list.getWidth() / 2,
this.getY() + 6, 0xffffffff);
int y = this.getY() + 6;
for (var it = this.title.iterator(); it.hasNext(); y += 9) {
var line = it.next();
graphics.drawCenteredShadowedText(this.client.font, line, this.getX() + this.list.getWidth() / 2, y, 0xffffff);
}
}
this.list.render(graphics, mouseX, mouseY, delta);
if (this.list.getCurrentTab() != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,34 @@
import dev.lambdaurora.spruceui.widget.WithBorder;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.network.chat.Text;
import org.jetbrains.annotations.Nullable;

/**
* Represents a text input widget.
*
* @author LambdAurora
* @version 6.0.0
* @version 6.1.0
* @since 2.1.0
*/
public abstract class AbstractSpruceTextInputWidget extends AbstractSpruceWidget implements WithBackground, WithBorder {
private final Text title;
private Background background = new SimpleColorBackground(ColorUtil.BLACK);
private Border border = TexturedBorder.SIMPLE;
private Text placeholder;

private int editableColor = ColorUtil.TEXT_COLOR;
private int uneditableColor = ColorUtil.UNEDITABLE_COLOR;

public AbstractSpruceTextInputWidget(Position position, int width, int height, Text title) {
this(position, width, height, title, null);
}

public AbstractSpruceTextInputWidget(Position position, int width, int height, Text title, Text placeholder) {
super(position);
this.width = width;
this.height = height;
this.title = title;
this.placeholder = placeholder;
}

/**
Expand All @@ -66,6 +73,24 @@ public Text getTitle() {
return this.title;
}

/**
* Returns the placeholder of this text input widget.
*
* @return the placeholder
*/
public @Nullable Text getPlaceholder() {
return this.placeholder;
}

/**
* Sets the placeholder of this text input widget.
*
* @param placeholder the placeholder
*/
public void setPlaceholder(Text placeholder) {
this.placeholder = placeholder;
}

/**
* Returns the color for editable text.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* Represents a text area widget.
*
* @author LambdAurora
* @version 6.0.0
* @version 6.1.0
* @since 1.6.3
*/
public class SpruceTextAreaWidget extends AbstractSpruceTextInputWidget {
Expand All @@ -43,7 +43,11 @@ public class SpruceTextAreaWidget extends AbstractSpruceTextInputWidget {
private int displayedLines;

public SpruceTextAreaWidget(Position position, int width, int height, Text title) {
super(position, width, height, title);
this(position, width, height, title, null);
}

public SpruceTextAreaWidget(Position position, int width, int height, Text title, Text placeholder) {
super(position, width, height, title, placeholder);
this.font = this.client.font;
this.displayedLines = this.getInnerHeight() / this.font.lineHeight;
this.lines = new MultilineText(this.getInnerWidth());
Expand Down Expand Up @@ -450,8 +454,14 @@ protected void drawText(GuiGraphics graphics) {

int textColor = this.getTextColor();
int textX = this.getX() + 4;

int lineY = this.getY() + 4;
var placeholder = this.getPlaceholder();

if (this.getText().isEmpty() && placeholder != null) {
graphics.drawShadowedText(this.client.font, placeholder, textX, lineY, textColor);
return;
}

for (int row = this.firstLine; row < this.firstLine + length; row++) {
var line = this.lines.get(row);
if (line == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
* Represents a text field widget.
*
* @author LambdAurora
* @version 6.0.0
* @version 6.1.0
* @since 2.1.0
*/
public class SpruceTextFieldWidget extends AbstractSpruceTextInputWidget implements Tooltipable {
Expand Down Expand Up @@ -85,7 +85,11 @@ public class SpruceTextFieldWidget extends AbstractSpruceTextInputWidget impleme
private long lastTick;

public SpruceTextFieldWidget(Position position, int width, int height, Text title) {
super(position, width, height, title);
this(position, width, height, title, null);
}

public SpruceTextFieldWidget(Position position, int width, int height, Text title, Text placeholder) {
super(position, width, height, title, placeholder);
this.cursor.toStart();
this.sanitize();

Expand All @@ -95,6 +99,10 @@ public SpruceTextFieldWidget(Position position, int width, int height, Text titl
this.renderTextProvider = (input, firstCharacterIndex) -> FormattedCharSequence.forward(input, Style.EMPTY);
}

public static SpruceTextFieldWidgetBuilder builder(Position position, int width, int height) {
return new SpruceTextFieldWidgetBuilder(position, width, height);
}

@Override
public String getText() {
return this.text;
Expand Down Expand Up @@ -230,6 +238,7 @@ private void insertCharacter(char character) {
private void eraseCharacter() {
if (this.selection.erase()) {
this.sanitize();
this.onChanged();
return;
}

Expand All @@ -250,6 +259,7 @@ private void eraseCharacter() {
private void removeCharacterForward() {
if (this.selection.erase()) {
this.sanitize();
this.onChanged();
return;
}

Expand Down Expand Up @@ -430,12 +440,22 @@ protected void drawText(GuiGraphics graphics) {
int textColor = this.getTextColor();
int x = this.getX() + 4;
int y = this.getY() + this.getHeight() / 2 - 4;
var placeholder = this.getPlaceholder();

var displayedText = this.client.font.plainSubstrByWidth(this.text.substring(this.firstCharacterIndex),
this.getInnerWidth());
if (this.text.isEmpty() && placeholder != null) {
graphics.drawShadowedText(this.client.font, placeholder, x, y, textColor);
return;
}

graphics.drawShadowedText(this.client.font, this.renderTextProvider.apply(displayedText, this.firstCharacterIndex),
x, y, textColor);
var displayedText = this.client.font.plainSubstrByWidth(
this.text.substring(this.firstCharacterIndex),
this.getInnerWidth()
);

graphics.drawShadowedText(
this.client.font, this.renderTextProvider.apply(displayedText, this.firstCharacterIndex),
x, y, textColor
);
this.drawSelection(graphics, displayedText, y);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright © 2024 LambdAurora <[email protected]>
*
* This file is part of SpruceUI.
*
* Licensed under the MIT license. For more information,
* see the LICENSE file.
*/

package dev.lambdaurora.spruceui.widget.text;

import dev.lambdaurora.spruceui.Position;
import net.minecraft.network.chat.Text;
import net.minecraft.util.FormattedCharSequence;

import java.util.Objects;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Predicate;

/**
* Represents a text field widget builder.
*
* @author LambdAurora
* @version 6.1.0
* @since 6.1.0
*/
public class SpruceTextFieldWidgetBuilder {
private final Position position;
private final int width;
private final int height;
private Text title;
private Text placeholder;
private Consumer<String> onChange;
private Predicate<String> textPredicate;
private BiFunction<String, Integer, FormattedCharSequence> renderTextProvider;

public SpruceTextFieldWidgetBuilder(Position position, int width, int height) {
this.position = position;
this.width = width;
this.height = height;
}

public SpruceTextFieldWidgetBuilder title(Text title) {
this.title = title;
return this;
}

public SpruceTextFieldWidgetBuilder placeholder() {
this.placeholder = this.title;
return this;
}

public SpruceTextFieldWidgetBuilder placeholder(Text placeholder) {
this.placeholder = placeholder;
return this;
}

public SpruceTextFieldWidgetBuilder onChange(Consumer<String> onChange) {
this.onChange = onChange;
return this;
}

public SpruceTextFieldWidgetBuilder textPredicate(Predicate<String> textPredicate) {
this.textPredicate = textPredicate;
return this;
}

public SpruceTextFieldWidgetBuilder renderTextProvider(BiFunction<String, Integer, FormattedCharSequence> renderTextProvider) {
this.renderTextProvider = renderTextProvider;
return this;
}

public SpruceTextFieldWidget build() {
Objects.requireNonNull(this.title, "Text fields require a title.");
var widget = new SpruceTextFieldWidget(this.position, this.width, this.height, this.title, this.placeholder);
if (this.onChange != null) widget.setChangedListener(this.onChange);
if (this.textPredicate != null) widget.setTextPredicate(this.textPredicate);
if (this.renderTextProvider != null) widget.setRenderTextProvider(this.renderTextProvider);
return widget;
}

public SpruceNamedTextFieldWidget buildNamed() {
return new SpruceNamedTextFieldWidget(this.build());
}
}
2 changes: 1 addition & 1 deletion src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"fabric-lifecycle-events-v1": "*",
"fabric-rendering-v1": "*",
"fabric-resource-loader-v0": ">=0.4.7",
"minecraft": ">=1.21.2-rc.1",
"minecraft": ">=1.21.2",
"java": ">=21"
},
"custom": {
Expand Down

0 comments on commit 45a6f3d

Please sign in to comment.