Search in sources :

Example 1 with AssetDataFile

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

the class NUIEditorScreen method selectAsset.

/**
 * {@inheritDoc}
 */
@Override
public void selectAsset(ResourceUrn urn) {
    boolean isLoaded = assetManager.isLoaded(urn, UIElement.class);
    Optional<UIElement> asset = assetManager.getAsset(urn, UIElement.class);
    if (asset.isPresent()) {
        UIElement element = asset.get();
        if (!isLoaded) {
            asset.get().dispose();
        }
        AssetDataFile source = element.getSource();
        String content = null;
        try (JsonReader reader = new JsonReader(new InputStreamReader(source.openStream(), Charsets.UTF_8))) {
            reader.setLenient(true);
            content = new JsonParser().parse(reader).toString();
        } catch (IOException e) {
            logger.error(String.format("Could not load asset source file for %s", urn.toString()), e);
        }
        if (content != null) {
            JsonTree node = JsonTreeConverter.serialize(new JsonParser().parse(content));
            selectedAssetPending = urn.toString();
            resetState(node);
        }
    }
}
Also used : UIElement(org.terasology.rendering.nui.asset.UIElement) InputStreamReader(java.io.InputStreamReader) JsonTree(org.terasology.rendering.nui.widgets.treeView.JsonTree) AssetDataFile(org.terasology.assets.format.AssetDataFile) JsonReader(com.google.gson.stream.JsonReader) IOException(java.io.IOException) JsonParser(com.google.gson.JsonParser)

Example 2 with AssetDataFile

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

the class NUIEditorScreen method setSelectedAssetPath.

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

Example 3 with AssetDataFile

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

Example 4 with AssetDataFile

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

the class TranslationFormatTests method mockAssetDataFile.

// TODO: consider making this available to other test classes
private static AssetDataFile mockAssetDataFile(String fname, byte[] resource) throws IOException {
    AssetDataFile assetDataFile = mock(AssetDataFile.class);
    when(assetDataFile.openStream()).thenReturn(new BufferedInputStream(new ByteArrayInputStream(resource)));
    when(assetDataFile.getFilename()).thenReturn(fname);
    return assetDataFile;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) AssetDataFile(org.terasology.assets.format.AssetDataFile)

Example 5 with AssetDataFile

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

the class TranslationFormatTests method testEmptyDataGenGermany.

@Test
public void testEmptyDataGenGermany() throws IOException, InvalidAssetFilenameException {
    AssetDataFile assetDataFile = mockAssetDataFile("menu_de-DE.lang", "{}".getBytes(StandardCharsets.UTF_8));
    ResourceUrn urn = createUrnFromFile(format, assetDataFile);
    TranslationData data = format.load(urn, Collections.singletonList(assetDataFile));
    Assert.assertEquals(Locale.GERMANY, data.getLocale());
    Assert.assertTrue(data.getTranslations().isEmpty());
}
Also used : AssetDataFile(org.terasology.assets.format.AssetDataFile) TranslationData(org.terasology.i18n.assets.TranslationData) ResourceUrn(org.terasology.assets.ResourceUrn) Test(org.junit.Test)

Aggregations

AssetDataFile (org.terasology.assets.format.AssetDataFile)11 IOException (java.io.IOException)4 InputStreamReader (java.io.InputStreamReader)4 JsonParser (com.google.gson.JsonParser)3 JsonReader (com.google.gson.stream.JsonReader)3 Test (org.junit.Test)3 ResourceUrn (org.terasology.assets.ResourceUrn)3 TranslationData (org.terasology.i18n.assets.TranslationData)3 UIElement (org.terasology.rendering.nui.asset.UIElement)3 UISkin (org.terasology.rendering.nui.skin.UISkin)3 JsonTree (org.terasology.rendering.nui.widgets.treeView.JsonTree)3 SimpleUri (org.terasology.engine.SimpleUri)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 Name (org.terasology.naming.Name)1 UIWidget (org.terasology.rendering.nui.UIWidget)1