use of org.terasology.rendering.nui.UIWidget in project Terasology by MovingBlocks.
the class NUIEditorScreen method resetPreviewWidget.
/**
* {@inheritDoc}
*/
@Override
public void resetPreviewWidget() {
try {
// Serialize the editor's contents and update the widget.
JsonElement element = JsonTreeConverter.deserialize(getEditor().getRoot());
UIWidget widget = new UIFormat().load(element, alternativeLocale).getRootWidget();
selectedScreenBox.setContent(widget);
} catch (Throwable t) {
String truncatedStackTrace = Joiner.on(System.lineSeparator()).join(Arrays.copyOfRange(ExceptionUtils.getStackFrames(t), 0, 10));
selectedScreenBox.setContent(new UILabel(truncatedStackTrace));
}
}
use of org.terasology.rendering.nui.UIWidget in project Terasology by MovingBlocks.
the class NUISkinEditorScreen method resetPreviewWidget.
/**
* {@inheritDoc}
*/
@Override
public void resetPreviewWidget() {
if (selectedScreen != null) {
try {
// Construct a UISkinData instance.
JsonElement skinElement = JsonTreeConverter.deserialize(getEditor().getRoot());
UISkinData data = new UISkinFormat().load(skinElement);
// Get the selected screen asset.
Optional<UIElement> sourceAsset = assetManager.getAsset(selectedScreen, UIElement.class);
if (!sourceAsset.isPresent()) {
throw new FileNotFoundException(String.format("Asset %s not found", selectedScreen));
}
AssetDataFile source = sourceAsset.get().getSource();
String content;
try (JsonReader reader = new JsonReader(new InputStreamReader(source.openStream(), Charsets.UTF_8))) {
reader.setLenient(true);
content = new JsonParser().parse(reader).toString();
}
if (content != null) {
JsonTree node = JsonTreeConverter.serialize(new JsonParser().parse(content));
JsonElement screenElement = JsonTreeConverter.deserialize(node);
UIWidget widget = new UIFormat().load(screenElement, alternativeLocale).getRootWidget();
// Set the screen's skin using the previously generated UISkinData.
widget.setSkin(Assets.generateAsset(data, UISkin.class));
selectedScreenBox.setContent(widget);
}
} catch (Throwable t) {
String truncatedStackTrace = Joiner.on(System.lineSeparator()).join(Arrays.copyOfRange(ExceptionUtils.getStackFrames(t), 0, 10));
selectedScreenBox.setContent(new UILabel(truncatedStackTrace));
}
}
}
use of org.terasology.rendering.nui.UIWidget in project Terasology by MovingBlocks.
the class WidgetSelectionScreen method initialise.
@Override
public void initialise() {
availableWidgets = find("availableWidgets", UIDropdownScrollable.class);
// Populate the widget list.
ClassLibrary<UIWidget> metadataLibrary = getManager().getWidgetMetadataLibrary();
for (ClassMetadata metadata : metadataLibrary) {
if (!CoreScreenLayer.class.isAssignableFrom(metadata.getType())) {
widgets.put(metadata.toString(), metadata);
}
}
List<String> options = Lists.newArrayList(widgets.keySet());
Collections.sort(options);
availableWidgets.setOptions(options);
// Add the widget as a child of the node.
WidgetUtil.trySubscribe(this, "ok", button -> {
String selection = availableWidgets.getSelection();
JsonTree childNode;
if (node.getValue().getType() == JsonTreeValue.Type.ARRAY) {
ClassMetadata metadata = widgets.get(selection);
// Get the widget tree from a utility method.
childNode = NUIEditorNodeUtils.createNewWidget(selection, "newWidget", false);
// If the widget is an UILayout override, also add a "contents" array node to the tree.
if (UILayout.class.isAssignableFrom(metadata.getType())) {
childNode.addChild(new JsonTreeValue("contents", null, JsonTreeValue.Type.ARRAY));
}
} else {
childNode = new JsonTree(new JsonTreeValue(selection, null, JsonTreeValue.Type.OBJECT));
childNode.setExpanded(true);
}
node.addChild(childNode);
closeListeners.forEach(UpdateListener::onAction);
getManager().closeScreen(ASSET_URI);
});
find("ok", UIButton.class).bindEnabled(new ReadOnlyBinding<Boolean>() {
@Override
public Boolean get() {
return availableWidgets.getSelection() != null;
}
});
WidgetUtil.trySubscribe(this, "cancel", button -> getManager().closeScreen(ASSET_URI));
}
use of org.terasology.rendering.nui.UIWidget in project Terasology by MovingBlocks.
the class MigLayout method onDraw.
@Override
public void onDraw(Canvas canvas) {
int[] bounds = { 0, 0, canvas.size().x, canvas.size().y };
layoutContainer(canvas, bounds);
for (ComponentWrapper wrapper : wrappers.values()) {
UIWidget component = (UIWidget) wrapper.getComponent();
Rect2i region = Rect2i.createFromMinAndSize(wrapper.getX(), wrapper.getY(), wrapper.getWidth(), wrapper.getHeight());
canvas.drawWidget(component, region);
}
if (debug) {
grid.paintDebug();
}
for (Rect2i region : debugRects) {
canvas.drawLine(region.minX(), region.minY(), region.maxX(), region.minY(), Color.WHITE);
canvas.drawLine(region.maxX(), region.minY(), region.maxX(), region.maxY(), Color.WHITE);
canvas.drawLine(region.maxX(), region.maxY(), region.minX(), region.maxY(), Color.WHITE);
canvas.drawLine(region.minX(), region.maxY(), region.minX(), region.minY(), Color.WHITE);
}
}
use of org.terasology.rendering.nui.UIWidget in project Terasology by MovingBlocks.
the class NUIEditorNodeUtils method getNodeInfo.
/**
* @param node A node in an asset tree.
* @param nuiManager The {@link NUIManager} to be used for widget type resolution.
* @return The info about this node's type.
*/
public static NodeInfo getNodeInfo(JsonTree node, NUIManager nuiManager) {
Deque<JsonTree> pathToRoot = getPathToRoot(node);
// Start iterating from top to bottom.
Class currentClass = null;
Class activeLayoutClass = null;
for (JsonTree n : pathToRoot) {
if (n.isRoot()) {
// currentClass is not set - set it to the screen type.
String type = (String) n.getChildWithKey("type").getValue().getValue();
currentClass = nuiManager.getWidgetMetadataLibrary().resolve(type, ModuleContext.getContext()).getType();
} else {
if (List.class.isAssignableFrom(currentClass) && n.getValue().getKey() == null && "contents".equals(n.getParent().getValue().getKey())) {
// Transition from a "contents" list to a UIWidget.
currentClass = UIWidget.class;
} else {
// Retrieve the type of an unspecified UIWidget.
if (currentClass == UIWidget.class && n.hasSiblingWithKey("type")) {
String type = (String) n.getSiblingWithKey("type").getValue().getValue();
currentClass = nuiManager.getWidgetMetadataLibrary().resolve(type, ModuleContext.getContext()).getType();
}
// If the current class is a layout, remember its' value (but do not set until later on!)
Class layoutClass = null;
if (UILayout.class.isAssignableFrom(currentClass)) {
layoutClass = currentClass;
}
if (UILayout.class.isAssignableFrom(currentClass) && "contents".equals(n.getValue().getKey())) {
// "contents" fields of a layout are always (widget) lists.
currentClass = List.class;
} else if (UIWidget.class.isAssignableFrom(currentClass) && "layoutInfo".equals(n.getValue().getKey())) {
// Set currentClass to the layout hint type for the active layout.
currentClass = (Class) ReflectionUtil.getTypeParameter(activeLayoutClass.getGenericSuperclass(), 0);
} else {
String value = n.getValue().toString();
Set<Field> fields = ReflectionUtils.getAllFields(currentClass);
Optional<Field> newField = fields.stream().filter(f -> f.getName().equalsIgnoreCase(value)).findFirst();
if (newField.isPresent()) {
currentClass = newField.get().getType();
} else {
Optional<Field> serializedNameField = fields.stream().filter(f -> f.isAnnotationPresent(SerializedName.class) && f.getAnnotation(SerializedName.class).value().equals(value)).findFirst();
if (serializedNameField.isPresent()) {
currentClass = serializedNameField.get().getType();
} else {
return null;
}
}
}
// Set the layout class value.
if (layoutClass != null) {
activeLayoutClass = layoutClass;
}
}
}
}
// If the final result is a generic UIWidget, attempt to retrieve its' type.
if (currentClass == UIWidget.class && node.hasChildWithKey("type")) {
String type = (String) node.getChildWithKey("type").getValue().getValue();
currentClass = nuiManager.getWidgetMetadataLibrary().resolve(type, ModuleContext.getContext()).getType();
}
return new NodeInfo(currentClass, activeLayoutClass);
}
Aggregations