Skip to content

Commit

Permalink
Add links
Browse files Browse the repository at this point in the history
  • Loading branch information
nea89o committed Jan 17, 2024
1 parent 4291b35 commit 4c64f95
Show file tree
Hide file tree
Showing 15 changed files with 189 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.github.moulberry.moulconfig.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface ConfigLink {

Class<?> owner();

String field();
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ public LerpingInteger(int initialValue) {
}

public void tick() {
if (timeToReachTarget == 0) {
lerpValue = targetValue;
return;
}
int lastTimeSpent = timeSpent;
this.timeSpent += System.currentTimeMillis() - lastMillis;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
public abstract class GuiOptionEditor {
private static final int HEIGHT = 45;
protected final ProcessedOption option;
public MoulConfigEditor<?> activeConfigGUI;
private String searchDescNameCache;

@ApiStatus.Internal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@
import io.github.moulberry.moulconfig.processor.MoulConfigProcessor;
import io.github.moulberry.moulconfig.processor.ProcessedCategory;
import io.github.moulberry.moulconfig.processor.ProcessedOption;
import lombok.Getter;
import lombok.val;
import lombok.var;
import org.jetbrains.annotations.NotNull;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;

Expand All @@ -45,9 +47,11 @@ public class MoulConfigEditor<T extends Config> extends GuiElement {
private final long openedMillis;
private final LerpingInteger optionsScroll = new LerpingInteger(0, 150);
private final LerpingInteger categoryScroll = new LerpingInteger(0, 150);
@Getter
private final MoulConfigProcessor<T> processedConfig;
private final LerpingInteger minimumSearchSize = new LerpingInteger(0, 150);
private final GuiElementTextField searchField = new GuiElementTextField("", 0, 20, 0);
@Getter
private String selectedCategory = null;
private float optionsBarStart;
private float optionsBarend;
Expand All @@ -71,6 +75,9 @@ public MoulConfigEditor(MoulConfigProcessor<T> processedConfig) {
.add(category.getKey());
}
}
for (ProcessedOption option : allOptions) {
option.editor.activeConfigGUI = this;
}
updateSearchResults();
}

Expand All @@ -80,8 +87,59 @@ private List<ProcessedOption> getOptionsInCategory(ProcessedCategory cat) {
return options;
}

public String getSelectedCategory() {
return selectedCategory;
public boolean scrollOptionIntoView(ProcessedOption searchedOption, int timeToReachTargetMs) {
ProcessedCategory processedCategory = getCurrentlySearchedCategories().get(getSelectedCategory());

// Check we are in the right category
if (processedCategory != searchedOption.category) {
return false;
}

// Recursively expand accordions this option is in
var accordionP = searchedOption;
while (accordionP.accordionId >= 0) {
accordionP = processedCategory.accordionAnchors.get(accordionP.accordionId);
((GuiOptionEditorAccordion) accordionP.editor).setToggled(true);
}

// If this option is an accordion, also expand that one
if (searchedOption.editor instanceof GuiOptionEditorAccordion) {
((GuiOptionEditorAccordion) searchedOption.editor).setToggled(true);
}

// Iterate over all options to find the correct y value for our thingy
Set<Integer> activeAccordions = new HashSet<>();
int optionY = 0;
for (ProcessedOption processedOption : getOptionsInCategory(processedCategory)) {
val editor = processedOption.editor;
if (editor == null) {
continue;
}
if (processedOption.accordionId >= 0 && !activeAccordions.contains(processedOption.accordionId))
continue;
if (editor instanceof GuiOptionEditorAccordion) {
GuiOptionEditorAccordion accordion = (GuiOptionEditorAccordion) editor;
if (accordion.getToggled()) {
activeAccordions.add(accordion.getAccordionId());
}
}
if (processedOption == searchedOption) {
optionsScroll.setTimeToReachTarget(timeToReachTargetMs);
optionsScroll.resetTimer();
optionsScroll.setTarget(optionY);
return true;
}
optionY += ContextAware.wrapErrorWithContext(editor, editor::getHeight) + 5;
}
return false;
}

public boolean setSelectedCategory(ProcessedCategory category) {
if (!getCurrentlySearchedCategories().containsKey(category.getIdentifier())) {
return false;
}
setSelectedCategory(category.getIdentifier());
return true;
}

private void setSelectedCategory(String category) {
Expand Down Expand Up @@ -604,15 +662,14 @@ public boolean mouseInput(int mouseX, int mouseY) {
int width = iMinecraft.getScaledWidth();
int height = iMinecraft.getScaledHeight();
int scaleFactor = iMinecraft.getScaleFactor();
int adjScaleFactor = Math.max(2, scaleFactor);

int xSize = Math.min(width - 100 / scaleFactor, 500);
int ySize = Math.min(width - 100 / scaleFactor, 400);

int x = (width - xSize) / 2;
int y = (height - ySize) / 2;

int adjScaleFactor = Math.max(2, scaleFactor);

int innerPadding = 20 / adjScaleFactor;
int innerTop = y + 49 + innerPadding;
int innerBottom = y + ySize - 5 - innerPadding;
Expand Down Expand Up @@ -974,4 +1031,20 @@ private void handleKeyboardPresses() {
processedConfig.getConfigObject().saveNow();
}
}

public boolean goToOption(@NotNull ProcessedOption option) {
if (!setSelectedCategory(option.category)) {
search("");
if (!setSelectedCategory(option.category)) {
return false;
}
}
if (!scrollOptionIntoView(option, 200)) {
search("");
if (!scrollOptionIntoView(option, 200)) {
return false;
}
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,8 @@ public int getAccordionId() {
public boolean getToggled() {
return accordionToggled;
}
public void setToggled(boolean toggled) {
accordionToggled = toggled;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,21 @@ public boolean mouseEvent(@NotNull MouseEvent mouseEvent, @NotNull GuiImmediateC
if (mouseEvent instanceof MouseEvent.Click) {
val click = (MouseEvent.Click) mouseEvent;
if (click.getMouseState() && context.isHovered() && click.getMouseButton() == 0) {
if (isUsingRunnable) {
((Runnable) option.get()).run();
} else {
config.executeRunnable(runnableId);
}
onClick();
return true;
}
}
return super.mouseEvent(mouseEvent, context);
}
});

public void onClick() {
if (isUsingRunnable) {
((Runnable) option.get()).run();
} else {
config.executeRunnable(runnableId);
}
}

@Override
public boolean fulfillsSearch(String word) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class ManagedConfig<T : Config>(private val builder: ManagedConfigBuilder<T>) :
builder.customProcessors.forEach { (annotation, method) ->
cast(processor, annotation, method)
}
ConfigProcessorDriver.processConfig(this.instance.javaClass, this.instance, processor)
ConfigProcessorDriver(processor).processConfig(this.instance)
return processor
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@

import io.github.moulberry.moulconfig.annotations.*;
import io.github.moulberry.moulconfig.gui.editors.*;
import lombok.val;

import java.lang.reflect.Field;

public class BuiltinMoulConfigGuis {
public static void addProcessors(MoulConfigProcessor<?> processor) {
Expand All @@ -46,5 +49,21 @@ public static void addProcessors(MoulConfigProcessor<?> processor) {
new GuiOptionEditorText(processedOption));
processor.registerConfigEditor(ConfigEditorSlider.class, (processedOption, configEditorSlider) ->
new GuiOptionEditorSlider(processedOption, configEditorSlider.minValue(), configEditorSlider.maxValue(), configEditorSlider.minStep()));
processor.registerConfigEditor(ConfigLink.class, ((option, configLink) -> {
Field field;
try {
field = configLink.owner().getField(configLink.field());
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
}
return new GuiOptionEditorButton(option, -1, "Link", option.config) {
@Override
public void onClick() {
val linkedOption = activeConfigGUI.getProcessedConfig().getOptionFromField(field);
assert linkedOption != null;
activeConfigGUI.goToOption(linkedOption);
}
};
}));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,18 @@
import java.util.*;

public class ConfigProcessorDriver {
private static final List<Class<? extends Annotation>> nonStoredConfigOptions = Arrays.asList(
private final List<Class<? extends Annotation>> nonStoredConfigOptions = Arrays.asList(
ConfigEditorAccordion.class, ConfigEditorInfoText.class,
ConfigEditorButton.class
);

static int nextAnnotation = 1000000000;
public final ConfigStructureReader reader;

public int nextAnnotation = 1000000000;

public ConfigProcessorDriver(ConfigStructureReader reader) {
this.reader = reader;
}

private static List<Field> getAllFields(Class<?> type) {
if (type == null) return new ArrayList<>();
Expand All @@ -47,8 +53,9 @@ private static List<Field> getAllFields(Class<?> type) {
return fields;
}

public static void processCategory(Object categoryObject, Class<?> categoryClass, ConfigStructureReader reader,
List<BoundField> deferredSubCategories) {
public void processCategory(Object categoryObject,
List<BoundField> deferredSubCategories) {
Class<?> categoryClass = categoryObject.getClass();
Stack<Integer> accordionStack = new Stack<>();
Set<Integer> usedAccordionIds = new HashSet<>();
for (Field field : getAllFields(categoryClass)) {
Expand Down Expand Up @@ -100,7 +107,7 @@ public static void processCategory(Object categoryObject, Class<?> categoryClass
try {
reader.pushPath(field.getName());
var subCategory = new ArrayList<BoundField>();
processCategory(field.get(categoryObject), field.getType(), reader, subCategory);
processCategory(field.get(categoryObject), subCategory);
if (!subCategory.isEmpty()) {
Warnings.warn("Cannot define sub categories inside of an accordion: " + subCategory.get(0));
}
Expand Down Expand Up @@ -130,8 +137,7 @@ public static void processCategory(Object categoryObject, Class<?> categoryClass
}
}

private static void processCategoryMeta(
ConfigStructureReader reader,
private void processCategoryMeta(
Object parent,
Field categoryField,
Field parentField
Expand All @@ -153,25 +159,25 @@ private static void processCategoryMeta(
}
try {
reader.pushPath(categoryField.getName());
processCategory(categoryField.get(parent), categoryField.getType(), reader, deferredSubCategories);
processCategory(categoryField.get(parent), deferredSubCategories);
reader.popPath();
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
reader.endCategory();
for (var subCategory : deferredSubCategories) {
if (parentField == null) {
processCategoryMeta(reader, subCategory.getBoundTo(), subCategory.getField(), categoryField);
processCategoryMeta(subCategory.getBoundTo(), subCategory.getField(), categoryField);
} else {
Warnings.warn("Found double recursive usb category at " + subCategory);
}
}
}

public static void processConfig(Class<? extends Config> configClass, Config configObject, ConfigStructureReader reader) {
reader.beginConfig(configClass, configObject);
for (Field categoryField : getAllFields(configClass)) {
processCategoryMeta(reader, configObject, categoryField, null);
public void processConfig(Config configObject) {
reader.beginConfig(configObject.getClass(), this, configObject);
for (Field categoryField : getAllFields(configObject.getClass())) {
processCategoryMeta(configObject, categoryField, null);
}
reader.endConfig();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ default void pushPath(String fieldPath) {
default void popPath() {
}

default void beginConfig(Class<? extends Config> configClass, Config configObject) {
default void beginConfig(Class<? extends Config> configClass, ConfigProcessorDriver driver, Config configObject) {
}

default void endConfig() {
Expand Down
Loading

0 comments on commit 4c64f95

Please sign in to comment.