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));
}
}
}
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);
}
}
Aggregations