Files
NotEnoughItems/src/main/java/codechicken/nei/config/OptionStringSet.java
T

144 lines
4.1 KiB
Java
Raw Normal View History

2014-04-29 10:54:20 +10:00
package codechicken.nei.config;
import codechicken.lib.vec.Rectangle4i;
import codechicken.nei.LayoutManager;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
2019-12-24 13:37:41 -08:00
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
2014-04-29 10:54:20 +10:00
import static codechicken.lib.gui.GuiDraw.drawString;
public abstract class OptionStringSet extends Option
{
2019-12-30 20:19:16 -08:00
public final LinkedList<String> options = new LinkedList<>();
public final Multimap<String, String> dependants = ArrayListMultimap.create();
public final Map<String, String> dependancies = new HashMap<>();
public final Multimap<String, String> groups = ArrayListMultimap.create();
2014-04-29 10:54:20 +10:00
public OptionStringSet(String name) {
2014-04-29 10:54:20 +10:00
super(name);
}
public void addDep(String base, String dep) {
2014-04-29 10:54:20 +10:00
dependants.put(dep, base);
dependancies.put(base, dep);
}
2014-04-29 10:54:20 +10:00
@Override
public void draw(int mousex, int mousey, float frame) {
2014-04-29 10:54:20 +10:00
drawPrefix();
drawButtons();
drawIcons();
}
public void drawPrefix() {
drawString(translateN(name), 10, 6, -1);
2014-04-29 10:54:20 +10:00
}
public void drawButtons() {
2014-04-29 10:54:20 +10:00
int x = buttonX();
List<String> values = values();
for (String option : options) {
LayoutManager.drawButtonBackground(x, 0, 20, 20, true, values.contains(option) ? 1 : 0);
x += 24;
2014-04-29 10:54:20 +10:00
}
}
public abstract void drawIcons();
2014-04-29 10:54:20 +10:00
@Override
public List<String> handleTooltip(int mousex, int mousey, List<String> currenttip) {
if (new Rectangle4i(4, 4, 50, 20).contains(mousex, mousey))
currenttip.add(translateN(name + ".tip"));
2014-04-29 10:54:20 +10:00
int x = buttonX();
for (String option : options) {
if (new Rectangle4i(x, 0, 20, 20).contains(mousex, mousey))
currenttip.add(translateN(name + "." + option));
x += 24;
2014-04-29 10:54:20 +10:00
}
return currenttip;
}
2014-04-29 10:54:20 +10:00
@Override
public void mouseClicked(int x, int y, int button) {
if (clickButton(x, y, button))
playClickSound();
2014-04-29 10:54:20 +10:00
}
public boolean clickButton(int mousex, int mousey, int button) {
2014-04-29 10:54:20 +10:00
int x = buttonX();
List<String> values = values();
2019-12-24 13:37:41 -08:00
for (String option : options) {
if (new Rectangle4i(x, 0, 20, 20).contains(mousex, mousey)) {
2019-12-30 20:19:16 -08:00
boolean set = values.contains(option);
if (button == 0 && !set) {
2019-12-30 20:19:16 -08:00
setValue(option);
2014-04-29 10:54:20 +10:00
return true;
}
if (button == 1 && set) {
2019-12-30 20:19:16 -08:00
remValue(option);
2014-04-29 10:54:20 +10:00
return true;
}
return false;
}
x += 24;
2014-04-29 10:54:20 +10:00
}
return false;
}
public void setValue(String s) {
if (values().contains(s))
2014-04-29 10:54:20 +10:00
return;
2014-04-29 10:54:20 +10:00
String dep = dependancies.get(s);
if (dep != null)
2014-04-29 10:54:20 +10:00
setValue(dep);
if (groups.containsKey(s)) {
for (String grp : groups.get(s))
2014-04-29 10:54:20 +10:00
setValue(grp);
} else {
2019-12-30 20:19:16 -08:00
List<String> setUtils = new LinkedList<>(values());
2014-04-29 10:54:20 +10:00
setUtils.add(s);
setValues(setUtils);
}
}
public void remValue(String s) {
for (String dep : dependants.get(s))
2014-04-29 10:54:20 +10:00
remValue(dep);
if (groups.containsKey(s)) {
for (String grp : groups.get(s))
2014-04-29 10:54:20 +10:00
remValue(grp);
} else {
2019-12-30 20:19:16 -08:00
List<String> values = new LinkedList<>(values());
2014-04-29 10:54:20 +10:00
values.remove(s);
setValues(values);
}
}
public void setValues(List<String> values) {
2014-04-29 10:54:20 +10:00
StringBuilder sb = new StringBuilder();
for (String s : values) {
if (sb.length() > 0)
2014-04-29 10:54:20 +10:00
sb.append(", ");
sb.append(s);
}
getTag().setValue(sb.toString());
}
public List<String> values() {
2014-04-29 10:54:20 +10:00
return Arrays.asList(renderTag().getValue().replace(" ", "").split(","));
}
public int buttonX() {
return slot.slotWidth() - (24 * options.size() - 4);
2014-04-29 10:54:20 +10:00
}
}