use of org.terasology.nui.widgets.treeView.JsonTreeValue in project Terasology by MovingBlocks.
the class NUIEditorItemRenderer method getTexture.
@Override
public Texture getTexture(JsonTreeValue value) {
String textureName = null;
if (value.getType() == JsonTreeValue.Type.KEY_VALUE_PAIR) {
textureName = ATTRIBUTE_TEXTURE_NAME;
} else if (value.getType() == JsonTreeValue.Type.OBJECT) {
JsonTree node = (JsonTree) editorTreeViewModel.getNodeByValue(value);
// If the node has a "type": "..." child, retrieve the texture by the type name.
if (node != null) {
// If the node has no type and is a root node, do not draw an icon.
if (node.isRoot()) {
return null;
} else if (!node.isRoot() && "elements".equals(node.getParent().getValue().getKey())) {
textureName = node.getValue().getKey();
} else {
for (Tree<JsonTreeValue> child : node.getChildren()) {
JsonTreeValue childValue = child.getValue();
if (childValue.getType() == JsonTreeValue.Type.KEY_VALUE_PAIR && "type".equals(childValue.getKey())) {
textureName = (String) childValue.getValue();
}
}
}
}
// Otherwise use the default texture.
if (textureName == null) {
textureName = OBJECT_TEXTURE_NAME;
}
} else if (value.getType() == JsonTreeValue.Type.ARRAY) {
textureName = ARRAY_TEXTURE_NAME;
} else if (value.getKey() != null) {
textureName = value.getKey();
} else {
return null;
}
Optional<Texture> texture = Assets.getTexture(textureName != null ? String.format("engine:editor_%s", textureName) : ICON_BLANK);
return texture.isPresent() ? texture.get() : null;
}
use of org.terasology.nui.widgets.treeView.JsonTreeValue in project Terasology by MovingBlocks.
the class NUIEditorNodeUtils method createNewWidget.
/**
* @param type The type of the widget.
* @param id The id of the widget.
* @param addLayoutInfo Whether a few layout settings from {@link RelativeLayout} should be added.
* @return The {@link JsonTree} with the given type/id to be used as an empty widget template within {@link
* NUIEditorScreen}.
*/
public static JsonTree createNewWidget(String type, String id, boolean addLayoutInfo) {
JsonTree widget = new JsonTree(new JsonTreeValue(null, null, JsonTreeValue.Type.OBJECT));
widget.addChild(new JsonTreeValue("type", type, JsonTreeValue.Type.KEY_VALUE_PAIR));
widget.addChild(new JsonTreeValue("id", id, JsonTreeValue.Type.KEY_VALUE_PAIR));
JsonTree layoutInfo = new JsonTree(new JsonTreeValue("layoutInfo", null, JsonTreeValue.Type.OBJECT));
if (addLayoutInfo) {
layoutInfo.addChild(new JsonTreeValue("width", 500, JsonTreeValue.Type.KEY_VALUE_PAIR));
JsonTree hPosition = new JsonTree(new JsonTreeValue("position-horizontal-center", null, JsonTreeValue.Type.OBJECT));
JsonTree vPosition = new JsonTree(new JsonTreeValue("position-vertical-center", null, JsonTreeValue.Type.OBJECT));
layoutInfo.addChild(hPosition);
layoutInfo.addChild(vPosition);
}
widget.addChild(layoutInfo);
return widget;
}
use of org.terasology.nui.widgets.treeView.JsonTreeValue in project Terasology by MovingBlocks.
the class NUIEditorMenuTreeBuilder method populateContextMenu.
private void populateContextMenu(JsonTree node, MenuTree addTree, boolean isSkin) {
NUIEditorNodeUtils.NodeInfo nodeInfo;
if (isSkin) {
nodeInfo = NUIEditorNodeUtils.getSkinNodeInfo(node);
} else {
nodeInfo = NUIEditorNodeUtils.getNodeInfo(node, nuiManager);
}
if (nodeInfo != null) {
Class clazz = nodeInfo.getNodeClass();
if (clazz != null) {
for (Field field : ReflectionUtils.getAllFields(clazz)) {
if ((!UIWidget.class.isAssignableFrom(clazz) || field.isAnnotationPresent(LayoutConfig.class)) && // Exclude static final fields, as they shouldn't be modified.
!(Modifier.isFinal(field.getModifiers()) && Modifier.isStatic(field.getModifiers()))) {
field.setAccessible(true);
String name = getNodeName(field);
if (!node.hasChildWithKey(name)) {
addTree.addOption(name, n -> {
try {
JsonTree child = createChild(name, node, field, clazz);
for (Consumer<JsonTree> listener : addContextMenuListeners) {
listener.accept(child);
}
} catch (IllegalAccessException | InstantiationException e) {
logger.warn("Could not add child", e);
}
}, node);
}
}
}
// If the node is part of a layout, add an option to add the layoutInfo node.
if (!node.isRoot() && !node.getParent().isRoot()) {
String layoutInfo = "layoutInfo";
String contents = "contents";
Class parentParentClass = NUIEditorNodeUtils.getNodeInfo((JsonTree) node.getParent().getParent(), nuiManager).getNodeClass();
if (!node.hasChildWithKey(layoutInfo) && UIWidget.class.isAssignableFrom(clazz) && node.getParent().getValue() != null && node.getParent().getValue().getKey() != null && node.getParent().getValue().getKey().equals(contents) && UILayout.class.isAssignableFrom(parentParentClass)) {
addTree.addOption(layoutInfo, n -> {
JsonTree child = new JsonTree(new JsonTreeValue(layoutInfo, null, JsonTreeValue.Type.OBJECT));
child.setExpanded(true);
n.addChild(child);
}, node);
}
}
}
} else {
logger.warn("Could not get class for node {}", node.getValue().toString());
}
}
use of org.terasology.nui.widgets.treeView.JsonTreeValue in project Terasology by MovingBlocks.
the class NUIEditorTextEntryBuilder method createObjectEditor.
/**
* @return A {@link UITextEntry} to be used to edit a JSON object node.
*/
public static UITextEntry<JsonTree> createObjectEditor() {
UITextEntry.Formatter<JsonTree> formatter = value -> value.getValue().getKey();
UITextEntry.Parser<JsonTree> parser = value -> new JsonTree(new JsonTreeValue(value, null, JsonTreeValue.Type.OBJECT));
return createEditorEntry(formatter, parser);
}
Aggregations