use of me.ichun.mods.ichunutil.client.gui.bns.window.constraint.Constraint in project iChunUtil by iChun.
the class ViewValues method addControlFor.
public void addControlFor(final WorkspaceConfigs.ConfigInfo.ValueWrapperLocalised value, ElementList.Item<?> item) {
Field field = value.value.field;
field.setAccessible(true);
String fieldName = field.getName();
Class clz = field.getType();
// should always exist
Prop props;
if (field.isAnnotationPresent(Prop.class)) {
props = field.getAnnotation(Prop.class);
} else {
props = ConfigBase.class.getDeclaredFields()[0].getAnnotation(Prop.class);
}
Object o;
try {
o = field.get(value.value.parent);
} catch (IllegalAccessException e) {
return;
}
boolean handled = false;
if (!props.guiElementOverride().isEmpty()) {
BiFunction<WorkspaceConfigs.ConfigInfo.ValueWrapperLocalised, ElementList.Item<?>, Boolean> valueWrapperLocalisedItemBiFunction = ConfigBase.GUI_ELEMENT_OVERRIDES.get(props.guiElementOverride());
if (valueWrapperLocalisedItemBiFunction != null && valueWrapperLocalisedItemBiFunction.apply(value, item)) {
handled = true;
}
}
if (!handled) {
if (clz == int.class) {
ElementNumberInput input = new ElementNumberInput(item, false);
input.setMin(props.min() == Double.MIN_VALUE ? Integer.MIN_VALUE : (int) props.min());
input.setMax(props.max() == Double.MAX_VALUE ? Integer.MAX_VALUE : (int) props.max());
input.setDefaultText(o.toString());
input.setSize(80, 14);
input.setConstraint(new Constraint(input).top(item, Constraint.Property.Type.TOP, 3).bottom(item, Constraint.Property.Type.BOTTOM, 3).right(item, Constraint.Property.Type.RIGHT, 8));
item.addElement(input);
} else if (clz == double.class) {
ElementNumberInput input = new ElementNumberInput(item, true);
input.setMin(props.min());
input.setMax(props.max());
input.setMaxDec(2);
input.setDefaultText(o.toString());
input.setSize(80, 14);
input.setConstraint(new Constraint(input).top(item, Constraint.Property.Type.TOP, 3).bottom(item, Constraint.Property.Type.BOTTOM, 3).right(item, Constraint.Property.Type.RIGHT, 8));
item.addElement(input);
} else if (clz == boolean.class) {
ElementToggleTextable<?> toggle = new ElementToggleTextable<>(item, value.name, elementClickable -> {
}).setToggled((boolean) o);
toggle.setSize(80, 14);
toggle.setConstraint(new Constraint(toggle).top(item, Constraint.Property.Type.TOP, 3).bottom(item, Constraint.Property.Type.BOTTOM, 3).right(item, Constraint.Property.Type.RIGHT, 8));
item.addElement(toggle);
} else if (clz == String.class) {
ElementTextField input = new ElementTextField(item);
input.setDefaultText(o.toString());
input.setSize(80, 14);
input.setConstraint(new Constraint(input).top(item, Constraint.Property.Type.TOP, 3).bottom(item, Constraint.Property.Type.BOTTOM, 3).right(item, Constraint.Property.Type.RIGHT, 8));
item.addElement(input);
} else if (// enum!
clz.isEnum()) {
ElementDropdownContextMenu<?> input = new ElementDropdownContextMenu<>(item, o.toString(), Arrays.asList(clz.getEnumConstants()), (menu, listItem) -> {
if (listItem.selected) {
ElementDropdownContextMenu<?> contextMenu = (ElementDropdownContextMenu<?>) menu;
contextMenu.text = listItem.getObject().toString();
}
});
input.setSize(80, 14);
input.setConstraint(new Constraint(input).top(item, Constraint.Property.Type.TOP, 3).bottom(item, Constraint.Property.Type.BOTTOM, 3).right(item, Constraint.Property.Type.RIGHT, 8));
item.addElement(input);
} else if (// lists
o instanceof List) {
StringBuilder sb = new StringBuilder();
final List list = (List) o;
for (int i = 0; i < list.size(); i++) {
Object o1 = list.get(i);
sb.append(o1);
if (i < list.size() - 1) {
sb.append("\n");
}
}
ElementButton<?> button = new ElementButton<>(item, "selectWorld.edit", btn -> {
value.value.field.setAccessible(true);
Type typefield = value.value.field.getGenericType();
if (typefield instanceof ParameterizedType) {
ParameterizedType type = (ParameterizedType) typefield;
Type[] types = type.getActualTypeArguments();
if (types.length == 1) {
// get which kind of validator we should use
Predicate<String> validator = null;
if (types[0] == String.class) {
validator = str -> true;
} else if (types[0] == Double.class) {
validator = ElementTextField.NUMBERS;
} else if (types[0] == Integer.class) {
validator = ElementTextField.INTEGERS;
}
if (validator != null) {
if (!(props.values().length == 1 && props.values()[0].isEmpty())) {
String[] values = props.values();
validator = validator.and(s -> {
for (String value1 : values) {
if (value1.startsWith(s)) {
return true;
}
}
return false;
});
}
WindowEditList<?> window = new WindowEditList<>(getWorkspace(), value.name, list, validator, list1 -> {
try {
List listToUse = list;
if (list instanceof ArrayList) {
listToUse = (List) ((ArrayList) list).clone();
}
listToUse.clear();
for (ElementList.Item<?> item1 : list1.items) {
ElementTextField oriText = (ElementTextField) item1.elements.get(0);
if (!oriText.getText().isEmpty()) {
if (types[0] == String.class) {
listToUse.add(oriText.getText());
} else if (types[0] == Double.class) {
listToUse.add(Double.parseDouble(oriText.getText()));
} else if (types[0] == Integer.class) {
listToUse.add(Integer.parseInt(oriText.getText()));
}
}
}
value.value.field.set(value.value.parent, listToUse);
value.value.parent.save();
} catch (IllegalAccessException ignored) {
}
});
getWorkspace().openWindowInCenter(window, 0.6D, 0.8D);
// reinit cause we're using lists and they're weird
window.init();
}
}
}
});
button.setTooltip(sb.toString());
button.setSize(80, 14);
button.setConstraint(new Constraint(button).top(item, Constraint.Property.Type.TOP, 3).bottom(item, Constraint.Property.Type.BOTTOM, 3).right(item, Constraint.Property.Type.RIGHT, 8));
item.addElement(button);
}
}
}
use of me.ichun.mods.ichunutil.client.gui.bns.window.constraint.Constraint in project iChunUtil by iChun.
the class WindowDock method addToDocked.
public boolean addToDocked(Window<?> dockedWin, Window<?> window) {
for (Map.Entry<ArrayListHolder, Constraint.Property.Type> e : docked.entrySet()) {
if (e.getKey().windows.contains(dockedWin)) {
dockedOriSize.put(window, new WindowSize(window.constraint, window.getLeft(), window.getTop(), window.getWidth(), window.getHeight()));
Constraint.Property.Type dockType = e.getValue();
ArrayList<Window<?>> dockStack = e.getKey().windows;
// we stack downwards and to the right.
Window<?> lastInStack = dockStack.get(dockStack.size() - 1);
int maxWidth = -1;
int maxHeight = -1;
if (dockType.getAxis().isHorizontal()) {
maxWidth = window.width;
for (Window<?> window1 : dockStack) {
if (window1.width > maxWidth) {
maxWidth = window1.width;
}
}
} else if (dockType.getAxis().isVertical()) {
maxHeight = window.height;
for (Window<?> window1 : dockStack) {
if (window1.height > maxHeight) {
maxHeight = window1.height;
}
}
}
Constraint constraint = new Constraint(window);
Constraint.Property.Type[] values = Constraint.Property.Type.values();
for (int i = values.length - 1; i >= 0; i--) {
Constraint.Property.Type type1 = values[i];
if (type1.equals(WIDTH) || type1.equals(HEIGHT)) {
continue;
}
IConstrainable constrainable = getWindowAnchor(lastInStack, type1);
if (// X. if type1 == top, anchor is lastInStack, same for Y.
dockType.getAxis().isHorizontal() && type1 == TOP || dockType.getAxis().isVertical() && type1 == LEFT) {
constrainable = lastInStack;
// we drop the constraint of lastInStack. let it free float.
lastInStack.constraint.type(type1.getOpposite(), null, null, 0);
// set the size
if (// if we're docked left or right, reset height
type1 == TOP) {
lastInStack.setHeight(dockedOriSize.get(lastInStack).height);
} else {
lastInStack.setWidth(dockedOriSize.get(lastInStack).width);
}
}
if (type1 != dockType.getOpposite()) {
if (constrainable != null && !(constrainable instanceof WindowDock)) {
constraint = constraint.type(type1, constrainable, type1.getOpposite(), -(Integer) window.borderSize.get() + borderSize.get());
} else {
constraint = constraint.type(type1, this, type1, -(Integer) window.borderSize.get() + borderSize.get());
}
}
}
e.getKey().windows.add(window);
window.setConstraint(constraint);
for (Window<?> window1 : dockStack) {
if (maxWidth >= 0) {
window1.setWidth(maxWidth);
} else if (maxHeight >= 0) {
window1.setHeight(maxHeight);
}
window1.constraint.apply();
if (getWorkspace().hasInit()) {
window1.resize(Minecraft.getInstance(), this.width, this.height);
}
}
return true;
}
}
return false;
}
use of me.ichun.mods.ichunutil.client.gui.bns.window.constraint.Constraint in project iChunUtil by iChun.
the class WindowDock method removeFromDock.
public void removeFromDock(Window<?> window) {
boolean redoConstraints = false;
Iterator<Map.Entry<ArrayListHolder, Constraint.Property.Type>> iterator = docked.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<ArrayListHolder, Constraint.Property.Type> e = iterator.next();
ArrayList<Window<?>> windows = e.getKey().windows;
// redo the constraints
EnumMap<Constraint.Property.Type, Constraint.Property> anchors = new EnumMap<>(Constraint.Property.Type.class);
if (redoConstraints || windows.contains(window)) {
for (Constraint.Property.Type type : Constraint.Property.Type.values()) {
if (type.equals(WIDTH) || type.equals(HEIGHT)) {
continue;
}
Constraint.Property stackAnchor = getStackAnchor(windows, type);
if (stackAnchor != null) {
anchors.put(type, stackAnchor);
}
}
}
if (// window is in this arraylist. sort it out
windows.contains(window)) {
redoConstraints = true;
if (windows.size() == 1) {
// we're using a linkedHASHmap. Empty ArrayLists aren't very friendly.
iterator.remove();
continue;
} else // we're in a dock stack.
{
// we have the anchors of each type. remove the nonbeliever!
windows.remove(window);
}
}
if (redoConstraints) {
Constraint.Property.Type dockType = e.getValue();
// Update the constraints
for (int i = 0; i < windows.size(); i++) {
Window<?> dockWindow = windows.get(i);
if (i == 0) {
Constraint constraint = new Constraint(dockWindow);
anchors.forEach((type, property) -> {
if (property.getReference() == window) {
IConstrainable constrainable = getAnchor(type, dockWindow);
if (constrainable != null && constrainable != dockWindow) {
constraint.type(type, constrainable, type.getOpposite(), -(Integer) dockWindow.borderSize.get() + borderSize.get());
} else {
constraint.type(type, this, type, -(Integer) dockWindow.borderSize.get() + borderSize.get());
}
} else {
constraint.type(type, property.getReference(), property.getType(), property.getDist());
}
});
dockWindow.setConstraint(constraint);
} else {
// we stack downwards and to the right.
Window<?> lastInStack = windows.get(i - 1);
Constraint constraint = new Constraint(dockWindow);
Constraint.Property.Type[] values = Constraint.Property.Type.values();
for (int ii = values.length - 1; ii >= 0; ii--) {
Constraint.Property.Type type1 = values[ii];
if (type1.equals(WIDTH) || type1.equals(HEIGHT)) {
continue;
}
IConstrainable constrainable = getWindowAnchor(lastInStack, type1);
if (// X. if type1 == top, anchor is lastInStack, same for Y.
dockType.getAxis().isHorizontal() && type1 == TOP || dockType.getAxis().isVertical() && type1 == LEFT) {
constrainable = lastInStack;
// we drop the constraint of lastInStack. let it free float.
lastInStack.constraint.type(type1.getOpposite(), null, null, 0);
// set the size
if (// if we're docked left or right, reset height
type1 == TOP) {
lastInStack.setHeight(dockedOriSize.get(lastInStack).height);
} else {
lastInStack.setWidth(dockedOriSize.get(lastInStack).width);
}
}
if (type1 != dockType.getOpposite()) {
if (constrainable != null && !(constrainable instanceof WindowDock)) {
constraint = constraint.type(type1, constrainable, type1.getOpposite(), -(Integer) dockWindow.borderSize.get() + borderSize.get());
} else {
constraint = constraint.type(type1, this, type1, -(Integer) dockWindow.borderSize.get() + borderSize.get());
}
}
}
dockWindow.setConstraint(constraint);
}
}
for (Window<?> window1 : windows) {
window1.constraint.apply();
if (getWorkspace().hasInit()) {
window1.resize(Minecraft.getInstance(), this.width, this.height);
}
}
}
}
WindowSize size = dockedOriSize.get(window);
window.setConstraint(size.constraint);
if (!(size.x == 0 && size.y == 0)) {
window.setLeft(size.x);
window.setTop(size.y);
}
window.setWidth(size.width);
window.setHeight(size.height);
window.resize(Minecraft.getInstance(), window.parent.getWidth(), window.parent.getHeight());
dockedOriSize.remove(window);
}
use of me.ichun.mods.ichunutil.client.gui.bns.window.constraint.Constraint in project iChunUtil by iChun.
the class ConfigClient method init.
@Override
public <T extends ConfigBase> T init() {
GUI_ELEMENT_OVERRIDES.put("iChunUtil:guiDefaultTheme", (value, item) -> {
Field field = value.value.field;
field.setAccessible(true);
Object o;
try {
o = field.get(value.value.parent);
} catch (IllegalAccessException e) {
iChunUtil.LOGGER.error("Error accessing config field {} when creating config. Stopping config creation.", field.getName());
e.printStackTrace();
return true;
}
ArrayList<File> files = new ArrayList<>();
File[] themes = ResourceHelper.getThemesDir().toFile().listFiles();
if (themes != null) {
for (File file : themes) {
if (!file.isDirectory() && file.getName().endsWith(".json")) {
files.add(file);
}
}
}
Collections.sort(files);
ElementDropdownContextMenu<?> input = new ElementDropdownContextMenu<>(item, o.toString(), files, (menu, listItem) -> {
if (listItem.selected) {
ElementDropdownContextMenu<?> contextMenu = (ElementDropdownContextMenu<?>) menu;
File file = (File) listItem.getObject();
// trim the ".json"
contextMenu.text = file.getName().substring(0, file.getName().length() - 5);
// update the theme here
try {
InputStream con = new FileInputStream(file);
String data = new String(ByteStreams.toByteArray(con));
con.close();
Theme theme = (new Gson()).fromJson(data, Theme.class);
if (theme != null) {
field.set(value.value.parent, contextMenu.text);
Theme.loadTheme(theme);
}
} catch (IOException | IllegalAccessException ignored) {
}
}
}).setNameProvider(o1 -> ((File) o1).getName().substring(0, ((File) o1).getName().length() - 5));
input.setSize(80, 14);
input.setConstraint(new Constraint(input).top(item, Constraint.Property.Type.TOP, 3).bottom(item, Constraint.Property.Type.BOTTOM, 3).right(item, Constraint.Property.Type.RIGHT, 8));
item.addElement(input);
return true;
});
GUI_ELEMENT_OVERRIDES.put("iChunUtil:reloadHeadsButton", (value, itemOri) -> {
ElementList.Item<?> item = itemOri.parentFragment.addItem(value).setBorderSize(0);
item.setSelectionHandler(itemObj -> {
if (itemObj.selected) {
for (Element<?> element : itemObj.elements) {
if (element instanceof ElementTextWrapper || element instanceof ElementPadding) {
continue;
}
element.parentFragment.setListener(element);
element.mouseClicked(element.getLeft() + element.getWidth() / 2D, element.getTop() + element.getHeight() / 2D, 0);
element.mouseReleased(element.getLeft() + element.getWidth() / 2D, element.getTop() + element.getHeight() / 2D, 0);
break;
}
}
});
ElementTextWrapper wrapper = new ElementTextWrapper(item).setText(I18n.format("config.ichunutil.headTracking.reload.desc"));
wrapper.setConstraint(new Constraint(wrapper).left(item, Constraint.Property.Type.LEFT, 3).right(item, Constraint.Property.Type.RIGHT, 90));
wrapper.setTooltip(value.desc);
item.addElement(wrapper);
ElementPadding padding = new ElementPadding(item, 0, 20);
padding.setConstraint(new Constraint(padding).right(item, Constraint.Property.Type.RIGHT, 0));
item.addElement(padding);
ElementButton<?> button = new ElementButton<>(item, "config.ichunutil.headTracking.reload.btn", btn -> {
if (HeadHandler.hasInit()) {
int count = HeadHandler.loadHeadInfos();
WindowPopup.popup(item.getWorkspace(), 0.6D, 0.6D, null, I18n.format("config.ichunutil.headTracking.reload.count", count));
} else {
WindowPopup.popup(item.getWorkspace(), 0.6D, 0.6D, null, I18n.format("config.ichunutil.headTracking.notLoaded"));
}
});
button.setTooltip(I18n.format("config.ichunutil.headTracking.reload.desc"));
button.setSize(80, 14);
button.setConstraint(new Constraint(button).top(item, Constraint.Property.Type.TOP, 3).bottom(item, Constraint.Property.Type.BOTTOM, 3).right(item, Constraint.Property.Type.RIGHT, 8));
item.addElement(button);
ElementButton<?> button1 = new ElementButton<>(item, "config.ichunutil.headTracking.reload.reextract", btn -> {
try {
if (HeadHandler.hasInit()) {
InputStream in = iChunUtil.class.getResourceAsStream("/heads.zip");
if (in != null) {
int extCount = IOUtil.extractFiles(HeadHandler.getHeadsDir(), in, true);
HeadHandler.loadHeadInfos();
WindowPopup.popup(item.getWorkspace(), 0.6D, 0.6D, null, I18n.format("config.ichunutil.headTracking.reload.reextract.count", extCount));
} else {
iChunUtil.LOGGER.error("Error extracting heads.zip.");
WindowPopup.popup(item.getWorkspace(), 0.6D, 0.6D, null, "Error!", "Error extracting heads.zip.");
}
} else {
WindowPopup.popup(item.getWorkspace(), 0.6D, 0.6D, null, I18n.format("config.ichunutil.headTracking.notLoaded"));
}
} catch (IOException e) {
e.printStackTrace();
}
});
button1.setTooltip(I18n.format("config.ichunutil.headTracking.reload.reextract.desc"));
button1.setSize(80, 14);
button1.setConstraint(new Constraint(button1).top(item, Constraint.Property.Type.TOP, 3).bottom(item, Constraint.Property.Type.BOTTOM, 3).right(button, Constraint.Property.Type.LEFT, 4));
item.addElement(button1);
// we still want the button to generate, this is a hook in.
return false;
});
return super.init();
}
use of me.ichun.mods.ichunutil.client.gui.bns.window.constraint.Constraint in project iChunUtil by iChun.
the class WindowDock method addToDock.
public void addToDock(Window<?> window, Constraint.Property.Type type) {
dockedOriSize.put(window, new WindowSize(window.constraint, window.getLeft(), window.getTop(), window.getWidth(), window.getHeight()));
Constraint constraint = new Constraint(window);
for (Constraint.Property.Type type1 : Constraint.Property.Type.values()) {
if (type1.equals(WIDTH) || type1.equals(HEIGHT)) {
continue;
}
IConstrainable constrainable = getAnchor(type1);
if (type1 != type.getOpposite()) {
if (constrainable != null) {
constraint = constraint.type(type1, constrainable, type1.getOpposite(), -(Integer) window.borderSize.get() + borderSize.get());
} else {
constraint = constraint.type(type1, this, type1, -(Integer) window.borderSize.get() + borderSize.get());
}
}
}
ArrayList<Window<?>> windows = new ArrayList<>();
windows.add(window);
docked.put(new WindowDock.ArrayListHolder(windows), type);
window.setConstraint(constraint);
window.constraint.apply();
if (getWorkspace().hasInit()) {
window.resize(Minecraft.getInstance(), this.width, this.height);
}
}
Aggregations