Search in sources :

Example 6 with UIElement

use of org.terasology.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.nui.asset.UIElement) AssetDataFile(org.terasology.gestalt.assets.format.AssetDataFile)

Example 7 with UIElement

use of org.terasology.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, 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)

Example 8 with UIElement

use of org.terasology.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.nui.asset.UIElement) UIWidget(org.terasology.nui.UIWidget)

Example 9 with UIElement

use of org.terasology.nui.asset.UIElement in project Terasology by MovingBlocks.

the class NUIManagerInternal method addOverlay.

@Override
public <T extends ControlWidget> T addOverlay(ResourceUrn overlayUri, Class<T> expectedType) {
    boolean existsAlready = assetManager.isLoaded(overlayUri, UIElement.class);
    Optional<UIElement> opt = Assets.get(overlayUri, UIElement.class);
    if (!opt.isPresent()) {
        logger.error("Can't find overlay '{}'", overlayUri);
    } else {
        UIElement element = opt.get();
        UIWidget root = element.getRootWidget();
        if (expectedType.isInstance(root)) {
            T overlay = expectedType.cast(root);
            if (!existsAlready) {
                initialiseControlWidget(overlay, overlayUri);
            }
            addOverlay(overlay, overlayUri);
            return overlay;
        } else {
            logger.error("Screen '{}' is a '{}' and not a '{}'", overlayUri, root.getClass(), expectedType);
        }
    }
    return null;
}
Also used : UIElement(org.terasology.nui.asset.UIElement) UIWidget(org.terasology.nui.UIWidget)

Example 10 with UIElement

use of org.terasology.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.nui.asset.UIElement) ResourceUrn(org.terasology.gestalt.assets.ResourceUrn) UIScreenLayer(org.terasology.engine.rendering.nui.UIScreenLayer) LinkedList(java.util.LinkedList)

Aggregations

UIElement (org.terasology.nui.asset.UIElement)10 UIWidget (org.terasology.nui.UIWidget)4 AssetDataFile (org.terasology.gestalt.assets.format.AssetDataFile)3 JsonParser (com.google.gson.JsonParser)2 JsonReader (com.google.gson.stream.JsonReader)2 IOException (java.io.IOException)2 InputStreamReader (java.io.InputStreamReader)2 ModuleManager (org.terasology.engine.core.module.ModuleManager)2 PojoPrefab (org.terasology.engine.entitySystem.prefab.internal.PojoPrefab)2 BehaviorTree (org.terasology.engine.logic.behavior.asset.BehaviorTree)2 BlockFamilyDefinition (org.terasology.engine.world.block.loader.BlockFamilyDefinition)2 BlockFamilyDefinitionData (org.terasology.engine.world.block.loader.BlockFamilyDefinitionData)2 BlockFamilyDefinitionFormat (org.terasology.engine.world.block.loader.BlockFamilyDefinitionFormat)2 BlockShapeImpl (org.terasology.engine.world.block.shapes.BlockShapeImpl)2 BlockSounds (org.terasology.engine.world.block.sounds.BlockSounds)2 BlockTile (org.terasology.engine.world.block.tiles.BlockTile)2 UISkinAsset (org.terasology.nui.skin.UISkinAsset)2 JsonTree (org.terasology.nui.widgets.treeView.JsonTree)2 JsonElement (com.google.gson.JsonElement)1 FileNotFoundException (java.io.FileNotFoundException)1