|
@@ -21,8 +21,14 @@ import java.util.function.Consumer;
|
|
|
import java.util.function.Function;
|
|
|
import java.util.function.Supplier;
|
|
|
|
|
|
-public abstract class BaseListEntry<T, C extends BaseListCell> extends TooltipListEntry<List<T>> {
|
|
|
-
|
|
|
+/**
|
|
|
+ * @param <T> the configuration object type
|
|
|
+ * @param <C> the cell type
|
|
|
+ * @param <SELF> the "curiously recurring template pattern" type parameter
|
|
|
+ * @implNote See <a href="https://stackoverflow.com/questions/7354740/is-there-a-way-to-refer-to-the-current-type-with-a-type-variable">Is there a way to refer to the current type with a type variable?</href> on Stack Overflow.
|
|
|
+ */
|
|
|
+public abstract class BaseListEntry<T, C extends BaseListCell, SELF extends BaseListEntry<T, C, SELF>> extends TooltipListEntry<List<T>> {
|
|
|
+
|
|
|
protected static final Identifier CONFIG_TEX = new Identifier("cloth-config2", "textures/gui/cloth_config.png");
|
|
|
protected final List<C> cells;
|
|
|
protected final List<Element> widgets;
|
|
@@ -30,16 +36,16 @@ public abstract class BaseListEntry<T, C extends BaseListCell> extends TooltipLi
|
|
|
protected Consumer<List<T>> saveConsumer;
|
|
|
protected ListLabelWidget labelWidget;
|
|
|
protected AbstractButtonWidget resetWidget;
|
|
|
- protected Function<BaseListEntry, C> createNewInstance;
|
|
|
+ protected Function<SELF, C> createNewInstance;
|
|
|
protected Supplier<List<T>> defaultValue;
|
|
|
protected String addTooltip = I18n.translate("text.cloth-config.list.add"), removeTooltip = I18n.translate("text.cloth-config.list.remove");
|
|
|
-
|
|
|
+
|
|
|
@Deprecated
|
|
|
- public BaseListEntry(String fieldName, Supplier<Optional<String[]>> tooltipSupplier, Supplier<List<T>> defaultValue, Function<BaseListEntry, C> createNewInstance, Consumer<List<T>> saveConsumer, String resetButtonKey) {
|
|
|
+ public BaseListEntry(String fieldName, Supplier<Optional<String[]>> tooltipSupplier, Supplier<List<T>> defaultValue, Function<SELF, C> createNewInstance, Consumer<List<T>> saveConsumer, String resetButtonKey) {
|
|
|
this(fieldName, tooltipSupplier, defaultValue, createNewInstance, saveConsumer, resetButtonKey, false);
|
|
|
}
|
|
|
-
|
|
|
- public BaseListEntry(String fieldName, Supplier<Optional<String[]>> tooltipSupplier, Supplier<List<T>> defaultValue, Function<BaseListEntry, C> createNewInstance, Consumer<List<T>> saveConsumer, String resetButtonKey, boolean requiresRestart) {
|
|
|
+
|
|
|
+ public BaseListEntry(String fieldName, Supplier<Optional<String[]>> tooltipSupplier, Supplier<List<T>> defaultValue, Function<SELF, C> createNewInstance, Consumer<List<T>> saveConsumer, String resetButtonKey, boolean requiresRestart) {
|
|
|
super(fieldName, tooltipSupplier, requiresRestart);
|
|
|
this.cells = Lists.newArrayList();
|
|
|
this.labelWidget = new ListLabelWidget();
|
|
@@ -56,62 +62,64 @@ public abstract class BaseListEntry<T, C extends BaseListCell> extends TooltipLi
|
|
|
this.createNewInstance = createNewInstance;
|
|
|
this.defaultValue = defaultValue;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
+ public abstract SELF self();
|
|
|
+
|
|
|
public boolean isDeleteButtonEnabled() {
|
|
|
return true;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
protected abstract C getFromValue(T value);
|
|
|
-
|
|
|
- public Function<BaseListEntry, C> getCreateNewInstance() {
|
|
|
+
|
|
|
+ public Function<SELF, C> getCreateNewInstance() {
|
|
|
return createNewInstance;
|
|
|
}
|
|
|
-
|
|
|
- public void setCreateNewInstance(Function<BaseListEntry, C> createNewInstance) {
|
|
|
+
|
|
|
+ public void setCreateNewInstance(Function<SELF, C> createNewInstance) {
|
|
|
this.createNewInstance = createNewInstance;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public String getAddTooltip() {
|
|
|
return addTooltip;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public void setAddTooltip(String addTooltip) {
|
|
|
this.addTooltip = addTooltip;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public String getRemoveTooltip() {
|
|
|
return removeTooltip;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public void setRemoveTooltip(String removeTooltip) {
|
|
|
this.removeTooltip = removeTooltip;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
@Override
|
|
|
public Optional<List<T>> getDefaultValue() {
|
|
|
return Optional.ofNullable(defaultValue.get());
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
@Override
|
|
|
public int getItemHeight() {
|
|
|
if (expended) {
|
|
|
int i = 24;
|
|
|
- for(BaseListCell entry : cells)
|
|
|
+ for (BaseListCell entry : cells)
|
|
|
i += entry.getCellHeight();
|
|
|
return i;
|
|
|
}
|
|
|
return 24;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
@Override
|
|
|
public List<? extends Element> children() {
|
|
|
return widgets;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
@Override
|
|
|
public Optional<String> getError() {
|
|
|
String error = null;
|
|
|
- for(BaseListCell entry : cells)
|
|
|
+ for (BaseListCell entry : cells)
|
|
|
if (entry.getConfigError().isPresent()) {
|
|
|
if (error != null)
|
|
|
return Optional.ofNullable(I18n.translate("text.cloth-config.multi_error"));
|
|
@@ -119,13 +127,13 @@ public abstract class BaseListEntry<T, C extends BaseListCell> extends TooltipLi
|
|
|
}
|
|
|
return Optional.ofNullable(error);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
@Override
|
|
|
public void save() {
|
|
|
if (saveConsumer != null)
|
|
|
saveConsumer.accept(getValue());
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
@Override
|
|
|
public boolean isMouseInside(int mouseX, int mouseY, int x, int y, int entryWidth, int entryHeight) {
|
|
|
labelWidget.rectangle.x = x - 15;
|
|
@@ -134,15 +142,15 @@ public abstract class BaseListEntry<T, C extends BaseListCell> extends TooltipLi
|
|
|
labelWidget.rectangle.height = 24;
|
|
|
return labelWidget.rectangle.contains(mouseX, mouseY) && getParent().isMouseOver(mouseX, mouseY) && !resetWidget.isMouseOver(mouseX, mouseY);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
protected boolean isInsideCreateNew(double mouseX, double mouseY) {
|
|
|
return mouseX >= labelWidget.rectangle.x + 12 && mouseY >= labelWidget.rectangle.y + 3 && mouseX <= labelWidget.rectangle.x + 12 + 11 && mouseY <= labelWidget.rectangle.y + 3 + 11;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
protected boolean isInsideDelete(double mouseX, double mouseY) {
|
|
|
return isDeleteButtonEnabled() && mouseX >= labelWidget.rectangle.x + 25 && mouseY >= labelWidget.rectangle.y + 3 && mouseX <= labelWidget.rectangle.x + 25 + 11 && mouseY <= labelWidget.rectangle.y + 3 + 11;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public Optional<String[]> getTooltip(int mouseX, int mouseY) {
|
|
|
if (addTooltip != null && isInsideCreateNew(mouseX, mouseY))
|
|
|
return Optional.of(new String[]{addTooltip});
|
|
@@ -152,7 +160,7 @@ public abstract class BaseListEntry<T, C extends BaseListCell> extends TooltipLi
|
|
|
return getTooltipSupplier().get();
|
|
|
return Optional.empty();
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
@Override
|
|
|
public void render(int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean isSelected, float delta) {
|
|
|
labelWidget.rectangle.x = x - 19;
|
|
@@ -181,20 +189,20 @@ public abstract class BaseListEntry<T, C extends BaseListCell> extends TooltipLi
|
|
|
MinecraftClient.getInstance().textRenderer.drawWithShadow(I18n.translate(getFieldName()), isDeleteButtonEnabled() ? x + 24 : x + 24 - 9, y + 5, labelWidget.rectangle.contains(mouseX, mouseY) && !resetWidget.isMouseOver(mouseX, mouseY) && !insideDelete && !insideCreateNew ? 0xffe6fe16 : getPreferredTextColor());
|
|
|
if (expended) {
|
|
|
int yy = y + 24;
|
|
|
- for(BaseListCell cell : cells) {
|
|
|
+ for (BaseListCell cell : cells) {
|
|
|
cell.render(-1, yy, x + 14, entryWidth - 14, cell.getCellHeight(), mouseX, mouseY, getParent().getFocused() != null && getParent().getFocused().equals(this) && getFocused() != null && getFocused().equals(cell), delta);
|
|
|
yy += cell.getCellHeight();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public boolean insertInFront() {
|
|
|
return true;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public class ListLabelWidget implements Element {
|
|
|
protected Rectangle rectangle = new Rectangle();
|
|
|
-
|
|
|
+
|
|
|
@Override
|
|
|
public boolean mouseClicked(double double_1, double double_2, int int_1) {
|
|
|
if (resetWidget.isMouseOver(double_1, double_2)) {
|
|
@@ -203,10 +211,10 @@ public abstract class BaseListEntry<T, C extends BaseListCell> extends TooltipLi
|
|
|
expended = true;
|
|
|
C cell;
|
|
|
if (insertInFront()) {
|
|
|
- cells.add(0, cell = createNewInstance.apply(BaseListEntry.this));
|
|
|
+ cells.add(0, cell = createNewInstance.apply(BaseListEntry.this.self()));
|
|
|
widgets.add(0, cell);
|
|
|
} else {
|
|
|
- cells.add(cell = createNewInstance.apply(BaseListEntry.this));
|
|
|
+ cells.add(cell = createNewInstance.apply(BaseListEntry.this.self()));
|
|
|
widgets.add(cell);
|
|
|
}
|
|
|
getScreen().setEdited(true, isRequiresRestart());
|
|
@@ -229,5 +237,5 @@ public abstract class BaseListEntry<T, C extends BaseListCell> extends TooltipLi
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
}
|