Skip to content

Commit

Permalink
Add another overload of choices that has per-choice tooltips and stor…
Browse files Browse the repository at this point in the history
…es an index instead of a string
  • Loading branch information
josephcsible committed Nov 11, 2018
1 parent f1fbb61 commit 2352501
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/main/java/mcjty/rftools/api/screens/IModuleGuiBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,41 @@ public interface IModuleGuiBuilder {
IModuleGuiBuilder color(String tagname, String... tooltip);

/**
* A choice selector.
* A choice selector that saves the text of the selected choice.
* @param tagname the tag that will be used to save the choice in your NBT
* @param choices
* @return
*/
IModuleGuiBuilder choices(String tagname, String tooltip, String... choices);

public static class Choice {
private final String name;
private final String[] tooltips;

public Choice(String name, String... tooltips) {
this.name = name;
this.tooltips = tooltips;
}

public String getName() {
return name;
}

public String[] getTooltips() {
return tooltips;
}
}

/**
* A choice selector that saves the zero-based index of the selected choice.
* @param tagname the tag that will be used to save the choice in your NBT
* @param choices
* @return
*/
IModuleGuiBuilder choices(String tagname, Choice... choices);

/**
* A combobox component that can be used to specify a format. This allows
* the user of your module to specify any of the possible FormatStyle values.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
import net.minecraft.world.World;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ScreenModuleGuiBuilder implements IModuleGuiBuilder {
private Minecraft mc;
Expand Down Expand Up @@ -177,6 +179,32 @@ public IModuleGuiBuilder choices(String tagname, String tooltip, String... choic
return this;
}

@Override
public IModuleGuiBuilder choices(String tagname, Choice... choices) {
ChoiceLabel choiceLabel = new ChoiceLabel(mc, gui)
.setDesiredWidth(50).setDesiredHeight(14);
Map<String, Integer> choicesMap = new HashMap<>(choices.length);
for (int i = 0; i < choices.length; ++i) {
Choice c = choices[i];
String name = c.getName();
choicesMap.put(name, i);
choiceLabel.addChoices(name);
choiceLabel.setChoiceTooltip(name, c.getTooltips());
}
choiceLabel.addChoiceEvent((parent, newChoice) -> {
currentData.setInteger(tagname, choicesMap.get(newChoice));
moduleGuiChanged.updateData();
});
row.add(choiceLabel);
if (currentData != null) {
int currentChoice = currentData.getInteger(tagname);
if (currentChoice < choices.length && currentChoice >= 0) {
choiceLabel.setChoice(choices[currentChoice].getName());
}
}
return this;
}

@Override
public ScreenModuleGuiBuilder format(String tagname) {
ChoiceLabel label = setupFormatCombo(mc, gui, tagname, currentData, moduleGuiChanged);
Expand Down

0 comments on commit 2352501

Please sign in to comment.