Search in sources :

Example 1 with UIElement

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);
    }
}
Also used : UIElement(org.terasology.rendering.nui.asset.UIElement) ResourceUrn(org.terasology.assets.ResourceUrn) UIScreenLayer(org.terasology.rendering.nui.UIScreenLayer) LinkedList(java.util.LinkedList)

Example 2 with UIElement

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;
}
Also used : UIElement(org.terasology.rendering.nui.asset.UIElement) UIWidget(org.terasology.rendering.nui.UIWidget)

Example 3 with UIElement

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);
        }
    }
}
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 4 with UIElement

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);
    }
}
Also used : UIElement(org.terasology.rendering.nui.asset.UIElement) AssetDataFile(org.terasology.assets.format.AssetDataFile)

Example 5 with UIElement

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

Aggregations

UIElement (org.terasology.rendering.nui.asset.UIElement)8 AssetDataFile (org.terasology.assets.format.AssetDataFile)3 UISkin (org.terasology.rendering.nui.skin.UISkin)3 UISkinData (org.terasology.rendering.nui.skin.UISkinData)3 JsonParser (com.google.gson.JsonParser)2 JsonReader (com.google.gson.stream.JsonReader)2 IOException (java.io.IOException)2 InputStreamReader (java.io.InputStreamReader)2 Prefab (org.terasology.entitySystem.prefab.Prefab)2 PrefabData (org.terasology.entitySystem.prefab.PrefabData)2 PojoPrefab (org.terasology.entitySystem.prefab.internal.PojoPrefab)2 BehaviorTree (org.terasology.logic.behavior.asset.BehaviorTree)2 BehaviorTreeData (org.terasology.logic.behavior.asset.BehaviorTreeData)2 UIWidget (org.terasology.rendering.nui.UIWidget)2 UIData (org.terasology.rendering.nui.asset.UIData)2 JsonTree (org.terasology.rendering.nui.widgets.treeView.JsonTree)2 DefaultBlockFamilyFactoryRegistry (org.terasology.world.block.family.DefaultBlockFamilyFactoryRegistry)2 BlockFamilyDefinition (org.terasology.world.block.loader.BlockFamilyDefinition)2 BlockFamilyDefinitionData (org.terasology.world.block.loader.BlockFamilyDefinitionData)2 BlockFamilyDefinitionFormat (org.terasology.world.block.loader.BlockFamilyDefinitionFormat)2