Search in sources :

Example 1 with UIFormat

use of org.terasology.engine.rendering.nui.asset.UIFormat 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));
    }
}
Also used : UILabel(org.terasology.nui.widgets.UILabel) UIFormat(org.terasology.engine.rendering.nui.asset.UIFormat) JsonElement(com.google.gson.JsonElement) UIWidget(org.terasology.nui.UIWidget)

Example 2 with UIFormat

use of org.terasology.engine.rendering.nui.asset.UIFormat 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, UISkinAsset.class).getSkin());
                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));
        }
    }
}
Also used : UILabel(org.terasology.nui.widgets.UILabel) UISkinFormat(org.terasology.engine.rendering.nui.skin.UISkinFormat) UIElement(org.terasology.nui.asset.UIElement) InputStreamReader(java.io.InputStreamReader) UIFormat(org.terasology.engine.rendering.nui.asset.UIFormat) JsonTree(org.terasology.nui.widgets.treeView.JsonTree) UISkinData(org.terasology.nui.skin.UISkinData) FileNotFoundException(java.io.FileNotFoundException) UIWidget(org.terasology.nui.UIWidget) JsonElement(com.google.gson.JsonElement) AssetDataFile(org.terasology.gestalt.assets.format.AssetDataFile) JsonReader(com.google.gson.stream.JsonReader) JsonParser(com.google.gson.JsonParser)

Aggregations

JsonElement (com.google.gson.JsonElement)2 UIFormat (org.terasology.engine.rendering.nui.asset.UIFormat)2 UIWidget (org.terasology.nui.UIWidget)2 UILabel (org.terasology.nui.widgets.UILabel)2 JsonParser (com.google.gson.JsonParser)1 JsonReader (com.google.gson.stream.JsonReader)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStreamReader (java.io.InputStreamReader)1 UISkinFormat (org.terasology.engine.rendering.nui.skin.UISkinFormat)1 AssetDataFile (org.terasology.gestalt.assets.format.AssetDataFile)1 UIElement (org.terasology.nui.asset.UIElement)1 UISkinData (org.terasology.nui.skin.UISkinData)1 JsonTree (org.terasology.nui.widgets.treeView.JsonTree)1