use of org.terasology.rendering.nui.asset.UIElement in project Terasology by MovingBlocks.
the class NUIManagerInternal method invalidate.
@Override
public void invalidate() {
assetManager.getLoadedAssets(UIElement.class).forEach(UIElement::dispose);
boolean hudVisible = isHUDVisible();
if (hudVisible) {
setHUDVisible(false);
}
Deque<ResourceUrn> reverseUrns = new LinkedList<>();
Map<UIScreenLayer, ResourceUrn> inverseLookup = screenLookup.inverse();
for (UIScreenLayer screen : screens) {
screen.onClosed();
reverseUrns.addFirst(inverseLookup.get(screen));
}
screens.clear();
screenLookup.clear();
reverseUrns.forEach(this::pushScreen);
if (hudVisible) {
setHUDVisible(true);
}
}
use of org.terasology.rendering.nui.asset.UIElement in project Terasology by MovingBlocks.
the class NUIManagerInternal method createScreen.
@Override
public <T extends CoreScreenLayer> T createScreen(ResourceUrn screenUri, Class<T> expectedType) {
boolean existsAlready = !screenUri.isInstance() && assetManager.isLoaded(screenUri, UIElement.class);
Optional<UIElement> opt = Assets.get(screenUri, UIElement.class);
if (!opt.isPresent()) {
logger.error("Can't find screen '{}'", screenUri);
} else {
UIElement element = opt.get();
UIWidget root = element.getRootWidget();
if (expectedType.isInstance(root)) {
T screen = expectedType.cast(root);
if (!existsAlready) {
initialiseScreen(screen, screenUri);
}
return screen;
} else {
logger.error("Screen '{}' is a '{}' and not a '{}'", screenUri, root.getClass(), expectedType);
}
}
return null;
}
use of org.terasology.rendering.nui.asset.UIElement 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);
}
}
}
use of org.terasology.rendering.nui.asset.UIElement 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);
}
}
use of org.terasology.rendering.nui.asset.UIElement 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));
}
}
}
Aggregations