Search in sources :

Example 1 with UISkinData

use of org.terasology.nui.skin.UISkinData in project Terasology by MovingBlocks.

the class UISkinFormat method load.

@Override
public UISkinData load(ResourceUrn urn, List<AssetDataFile> inputs) throws IOException {
    try (JsonReader reader = new JsonReader(new InputStreamReader(inputs.get(0).openStream(), Charsets.UTF_8))) {
        reader.setLenient(true);
        UISkinData data = gson.fromJson(reader, UISkinData.class);
        data.setSource(inputs.get(0));
        return data;
    } catch (JsonParseException e) {
        throw new IOException("Failed to load skin '" + urn + "'", e);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) UISkinData(org.terasology.nui.skin.UISkinData) JsonReader(com.google.gson.stream.JsonReader) IOException(java.io.IOException) JsonParseException(com.google.gson.JsonParseException)

Example 2 with UISkinData

use of org.terasology.nui.skin.UISkinData 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

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