Skip to content

Commit

Permalink
Added button support and presets
Browse files Browse the repository at this point in the history
  • Loading branch information
Rubyboat1207 committed Aug 25, 2022
1 parent 59a2291 commit bb5d01b
Show file tree
Hide file tree
Showing 20 changed files with 403 additions and 182 deletions.
8 changes: 4 additions & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
org.gradle.jvmargs=-Xmx1G
# Fabric Properties
# check these on https://modmuss50.me/fabric.html
minecraft_version=1.19
yarn_mappings=1.19+build.4
minecraft_version=1.19.1
yarn_mappings=1.19.1+build.5
loader_version=0.14.8
# Mod Properties
mod_version=1.19-1.4.0
mod_version=1.19.1-1.5.0
maven_group=rubyboat
archives_base_name=ghost
# Dependencies
# check this on https://modmuss50.me/fabric.html
fabric_version=0.56.0+1.19
fabric_version=0.58.5+1.19.1
clothconfig_version=7.0.72
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package rubyboat.clothconfigextensions.builders;

import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.text.Text;
import rubyboat.clothconfigextensions.entries.ButtonEntry;

public class ButtonBuilder {
Text value;
Text id;
ButtonWidget.PressAction onPress;
int xbuffer = 0;

public ButtonBuilder(Text component, Text value) {
this.id = component;
this.value = value;
}

public ButtonBuilder setOnPress(ButtonWidget.PressAction onPress) {
this.onPress = onPress;
return this;
}

public ButtonBuilder setWidthBuffer(int xbuffer) {
this.xbuffer = xbuffer;
return this;
}

public ButtonEntry build() {
return new ButtonEntry(id, value, onPress, xbuffer);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package rubyboat.clothconfigextensions;

import me.shedaniel.clothconfig2.api.AbstractConfigListEntry;
import me.shedaniel.clothconfig2.api.ConfigEntryBuilder;
import me.shedaniel.clothconfig2.api.ModifierKeyCode;
import me.shedaniel.clothconfig2.gui.entries.DropdownBoxEntry;
import me.shedaniel.clothconfig2.impl.ConfigEntryBuilderImpl;
import me.shedaniel.clothconfig2.impl.builders.*;
import net.minecraft.text.Text;
import rubyboat.clothconfigextensions.builders.ButtonBuilder;

import java.util.List;
import java.util.UUID;

public class configEntryBuilderExtension implements ConfigEntryBuilder {

@Override
public Text getResetButtonKey() {
return null;
}

@Override
public ConfigEntryBuilder setResetButtonKey(Text text) {
return null;
}

@Override
public IntListBuilder startIntList(Text text, List<Integer> list) {
return null;
}

@Override
public LongListBuilder startLongList(Text text, List<Long> list) {
return null;
}

@Override
public FloatListBuilder startFloatList(Text text, List<Float> list) {
return null;
}

@Override
public DoubleListBuilder startDoubleList(Text text, List<Double> list) {
return null;
}

@Override
public StringListBuilder startStrList(Text text, List<String> list) {
return null;
}

@Override
public SubCategoryBuilder startSubCategory(Text text) {
return null;
}

@Override
public SubCategoryBuilder startSubCategory(Text text, List<AbstractConfigListEntry> list) {
return null;
}

@Override
public BooleanToggleBuilder startBooleanToggle(Text text, boolean b) {
return null;
}

@Override
public StringFieldBuilder startStrField(Text text, String s) {
return null;
}

@Override
public ColorFieldBuilder startColorField(Text text, int i) {
return null;
}

@Override
public TextFieldBuilder startTextField(Text text, String s) {
return null;
}

@Override
public TextDescriptionBuilder startTextDescription(Text text) {
return null;
}

@Override
public <T extends Enum<?>> EnumSelectorBuilder<T> startEnumSelector(Text text, Class<T> aClass, T t) {
return null;
}

@Override
public <T> SelectorBuilder<T> startSelector(Text text, T[] ts, T t) {
return null;
}

public ButtonBuilder startButtonBuilder(Text value) {
return new ButtonBuilder(Text.of(UUID.randomUUID().toString()), value);
}

@Override
public IntFieldBuilder startIntField(Text text, int i) {
return null;
}

@Override
public LongFieldBuilder startLongField(Text text, long l) {
return null;
}

@Override
public FloatFieldBuilder startFloatField(Text text, float v) {
return null;
}

@Override
public DoubleFieldBuilder startDoubleField(Text text, double v) {
return null;
}

@Override
public IntSliderBuilder startIntSlider(Text text, int i, int i1, int i2) {
return null;
}

@Override
public LongSliderBuilder startLongSlider(Text text, long l, long l1, long l2) {
return null;
}

@Override
public KeyCodeBuilder startModifierKeyCodeField(Text text, ModifierKeyCode modifierKeyCode) {
return null;
}

@Override
public <T> DropdownMenuBuilder<T> startDropdownMenu(Text text, DropdownBoxEntry.SelectionTopCellElement<T> selectionTopCellElement, DropdownBoxEntry.SelectionCellCreator<T> selectionCellCreator) {
return null;
}
}
126 changes: 126 additions & 0 deletions src/main/java/rubyboat/clothconfigextensions/entries/ButtonEntry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package rubyboat.clothconfigextensions.entries;

import com.google.common.collect.Lists;
import me.shedaniel.clothconfig2.gui.AbstractConfigScreen;
import me.shedaniel.clothconfig2.gui.entries.TooltipListEntry;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.Element;
import net.minecraft.client.gui.Selectable;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.text.OrderedText;
import net.minecraft.text.Style;
import net.minecraft.text.Text;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;

@Environment(EnvType.CLIENT)
public class ButtonEntry extends TooltipListEntry<Object> {
public static final int LINE_HEIGHT = 12;
private final TextRenderer textRenderer = MinecraftClient.getInstance().textRenderer;
Text buttonInnerText;
private int savedWidth = -1;
private int savedX = -1;
private int savedY = -1;

public int xbuffer;
public int ybuffer;

private final List<ButtonWidget> widgets;
private ButtonWidget button;
private List<OrderedText> wrappedLines;
@ApiStatus.Internal
@Deprecated
public ButtonEntry(Text fieldName, Text text, ButtonWidget.PressAction onPress, int xbuffer) {
this(fieldName, text, null, onPress, xbuffer);
}

@ApiStatus.Internal
@Deprecated
public ButtonEntry(Text fieldName, Text text, Supplier<Optional<Text[]>> tooltipSupplier, ButtonWidget.PressAction onPress, int xbuffer) {
super(fieldName, tooltipSupplier);
this.buttonInnerText = text;
this.wrappedLines = Collections.emptyList();
this.button = new ButtonWidget(0, 0, MinecraftClient.getInstance().textRenderer.getWidth(text) + 6 + xbuffer, 20, text, onPress);
widgets = Lists.newArrayList(button);
}

@Override
public void render(MatrixStack matrices, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean isHovered, float delta) {
super.render(matrices, index, y, x, entryWidth, entryHeight, mouseX, mouseY, isHovered, delta);
if (this.savedWidth != entryWidth || this.savedX != x || this.savedY != y) {
this.wrappedLines = this.textRenderer.wrapLines(this.buttonInnerText, entryWidth);
this.savedWidth = entryWidth;
this.savedX = x;
this.savedY = y;
}
button.x = getConfigScreen().width / 2 - button.getWidth() / 2;
button.y = y;
button.render(matrices, mouseX, mouseY, delta);


Style style = this.getTextAt(mouseX, mouseY);
AbstractConfigScreen configScreen = this.getConfigScreen();
if (style != null && configScreen != null) {
configScreen.renderTextHoverEffect(matrices, style, mouseX, mouseY);
}
}

@Override
public int getItemHeight() {
if (savedWidth == -1) return LINE_HEIGHT;
int lineCount = this.wrappedLines.size();
return lineCount == 0 ? 0 : 15 + lineCount * LINE_HEIGHT;
}

@Override
public List<? extends Selectable> narratables() {
return null;
}

@Nullable
private Style getTextAt(double x, double y) {
int lineCount = this.wrappedLines.size();

if (lineCount > 0) {
int textX = (int) Math.floor(x - this.savedX);
int textY = (int) Math.floor(y - 4 - this.savedY);
if (textX >= 0 && textY >= 0 && textX <= this.savedWidth && textY < LINE_HEIGHT * lineCount + lineCount) {
int line = textY / LINE_HEIGHT;
if (line < this.wrappedLines.size()) {
OrderedText orderedText = this.wrappedLines.get(line);
return this.textRenderer.getTextHandler().getStyleAt(orderedText, textX);
}
}
}
return null;
}

@Override
public Object getValue() {
return null;
}

@Override
public Optional<Object> getDefaultValue() {
return Optional.empty();
}

@Override
public void save() {

}

@Override
public List<? extends Element> children() {
return widgets;
}
}
Loading

0 comments on commit bb5d01b

Please sign in to comment.