Search in sources :

Example 11 with AssetDataFile

use of org.terasology.gestalt.assets.format.AssetDataFile 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)

Example 12 with AssetDataFile

use of org.terasology.gestalt.assets.format.AssetDataFile in project Terasology by MovingBlocks.

the class NUISkinEditorScreen method setSelectedAssetPath.

/**
 * {@inheritDoc}
 */
@Override
protected void setSelectedAssetPath(ResourceUrn urn) {
    boolean isLoaded = assetManager.isLoaded(urn, UISkinAsset.class);
    Optional<UISkinAsset> asset = assetManager.getAsset(urn, UISkinAsset.class);
    if (asset.isPresent()) {
        UISkinAsset skin = asset.get();
        if (!isLoaded) {
            asset.get().dispose();
        }
        AssetDataFile source = skin.getSource();
        selectedAssetPath = getPath(source);
    }
}
Also used : UISkinAsset(org.terasology.nui.skin.UISkinAsset) AssetDataFile(org.terasology.gestalt.assets.format.AssetDataFile)

Aggregations

AssetDataFile (org.terasology.gestalt.assets.format.AssetDataFile)12 ResourceUrn (org.terasology.gestalt.assets.ResourceUrn)5 IOException (java.io.IOException)4 InputStreamReader (java.io.InputStreamReader)4 Test (org.junit.jupiter.api.Test)4 TranslationData (org.terasology.engine.i18n.assets.TranslationData)4 JsonParser (com.google.gson.JsonParser)3 JsonReader (com.google.gson.stream.JsonReader)3 UIElement (org.terasology.nui.asset.UIElement)3 JsonTree (org.terasology.nui.widgets.treeView.JsonTree)3 UISkinAsset (org.terasology.nui.skin.UISkinAsset)2 JsonElement (com.google.gson.JsonElement)1 JsonParseException (com.google.gson.JsonParseException)1 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 Locale (java.util.Locale)1 UIFormat (org.terasology.engine.rendering.nui.asset.UIFormat)1 UISkinFormat (org.terasology.engine.rendering.nui.skin.UISkinFormat)1 Name (org.terasology.gestalt.naming.Name)1