Search in sources :

Example 6 with ResourceUrn

use of org.terasology.assets.ResourceUrn in project Terasology by MovingBlocks.

the class BlockMeshGeneratorSingleShape method generateMesh.

private void generateMesh() {
    Tessellator tessellator = new Tessellator();
    for (BlockPart dir : BlockPart.values()) {
        BlockMeshPart part = block.getPrimaryAppearance().getPart(dir);
        if (part != null) {
            if (block.isDoubleSided()) {
                tessellator.addMeshPartDoubleSided(part);
            } else {
                tessellator.addMeshPart(part);
            }
        }
    }
    mesh = tessellator.generateMesh(new ResourceUrn("engine", "blockmesh", block.getURI().toString()));
}
Also used : BlockPart(org.terasology.world.block.BlockPart) ResourceUrn(org.terasology.assets.ResourceUrn) BlockMeshPart(org.terasology.world.block.shapes.BlockMeshPart)

Example 7 with ResourceUrn

use of org.terasology.assets.ResourceUrn in project Terasology by MovingBlocks.

the class IconMeshDataProducer method getAssetData.

@Override
public Optional<MeshData> getAssetData(ResourceUrn urn) throws IOException {
    if (ICON_DISCRIMINATOR.equals(urn.getResourceName())) {
        ResourceUrn textureUrn = new ResourceUrn(urn.getModuleName().toString() + ResourceUrn.RESOURCE_SEPARATOR + urn.getFragmentName().toString());
        Optional<TextureRegionAsset> textureRegionAsset = assetManager.getAsset(textureUrn, TextureRegionAsset.class);
        if (textureRegionAsset.isPresent() && !textureRegionAsset.get().getTexture().isDisposed()) {
            return Optional.of(IconMeshFactory.generateIconMeshData(textureRegionAsset.get()));
        }
    }
    return Optional.empty();
}
Also used : ResourceUrn(org.terasology.assets.ResourceUrn) TextureRegionAsset(org.terasology.rendering.assets.texture.TextureRegionAsset)

Example 8 with ResourceUrn

use of org.terasology.assets.ResourceUrn in project Terasology by MovingBlocks.

the class AbstractEditorScreen method loadAutosave.

/**
 * Resets the editor based on the state of the autosave file.
 */
protected void loadAutosave() {
    if (!disableAutosave) {
        try (JsonReader reader = new JsonReader(new InputStreamReader(Files.newInputStream(getAutosaveFile())))) {
            reader.setLenient(true);
            String autosaveString = new JsonParser().parse(reader).toString();
            JsonObject autosaveObject = new JsonParser().parse(autosaveString).getAsJsonObject();
            String selectedAsset = autosaveObject.get("selectedAsset").getAsString();
            setSelectedAsset(selectedAsset);
            try {
                ResourceUrn urn = new ResourceUrn(selectedAsset);
                setSelectedAssetPath(urn);
            } catch (InvalidUrnException ignored) {
            }
            JsonTree editorContents = JsonTreeConverter.serialize(autosaveObject.get("editorContents"));
            resetState(editorContents);
            setUnsavedChangesPresent(true);
        } catch (NoSuchFileException ignored) {
        } catch (IOException e) {
            logger.warn("Could not load autosaved info", e);
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InvalidUrnException(org.terasology.assets.exceptions.InvalidUrnException) JsonTree(org.terasology.rendering.nui.widgets.treeView.JsonTree) NoSuchFileException(java.nio.file.NoSuchFileException) JsonReader(com.google.gson.stream.JsonReader) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) ResourceUrn(org.terasology.assets.ResourceUrn) JsonParser(com.google.gson.JsonParser)

Example 9 with ResourceUrn

use of org.terasology.assets.ResourceUrn in project Terasology by MovingBlocks.

the class NUISkinEditorScreen method initialise.

@Override
public void initialise() {
    // Retrieve the widgets based on their identifiers.
    availableAssetDropdown = find(AVAILABLE_ASSETS_ID, UIDropdownScrollable.class);
    UIDropdownScrollable<ResourceUrn> availableScreenDropdown = find(AVAILABLE_SCREENS_ID, UIDropdownScrollable.class);
    JsonEditorTreeView editor = find(EDITOR_TREE_VIEW_ID, JsonEditorTreeView.class);
    selectedScreenBox = find(SELECTED_SCREEN_ID, UIBox.class);
    super.setEditorSystem(nuiSkinEditorSystem);
    super.setEditor(editor);
    // Populate the list of screens.
    List<String> availableAssetList = Lists.newArrayList();
    availableAssetList.add(CREATE_NEW_SKIN);
    availableAssetList.addAll(assetManager.getAvailableAssets(UISkin.class).stream().map(Object::toString).collect(Collectors.toList()));
    Collections.sort(availableAssetList);
    if (availableAssetDropdown != null) {
        availableAssetDropdown.setOptions(availableAssetList);
        availableAssetDropdown.bindSelection(new Binding<String>() {

            @Override
            public String get() {
                return selectedAsset;
            }

            @Override
            public void set(String value) {
                // Construct a new skin tree (or de-serialize from an existing asset)
                selectedAssetPending = value;
                if (CREATE_NEW_SKIN.equals(value)) {
                    selectedAssetPath = null;
                    resetState(NUIEditorNodeUtils.createNewSkin());
                } else {
                    selectAsset(new ResourceUrn(value));
                }
            }
        });
    }
    if (availableScreenDropdown != null) {
        List<ResourceUrn> availableScreenList = Lists.newArrayList(assetManager.getAvailableAssets(UIElement.class));
        Collections.sort(availableScreenList);
        availableScreenDropdown.setOptions(availableScreenList);
        availableScreenDropdown.bindEnabled(new ReadOnlyBinding<Boolean>() {

            @Override
            public Boolean get() {
                return selectedAsset != null;
            }
        });
        availableScreenDropdown.bindSelection(new Binding<ResourceUrn>() {

            @Override
            public ResourceUrn get() {
                return selectedScreen;
            }

            @Override
            public void set(ResourceUrn value) {
                selectedScreen = value;
                resetPreviewWidget();
            }
        });
    }
    if (editor != null) {
        editor.setContextMenuTreeProducer(node -> {
            NUIEditorMenuTreeBuilder nuiEditorMenuTreeBuilder = new NUIEditorMenuTreeBuilder();
            nuiEditorMenuTreeBuilder.setManager(getManager());
            nuiEditorMenuTreeBuilder.putConsumer(NUIEditorMenuTreeBuilder.OPTION_COPY, getEditor()::copyNode);
            nuiEditorMenuTreeBuilder.putConsumer(NUIEditorMenuTreeBuilder.OPTION_PASTE, getEditor()::pasteNode);
            nuiEditorMenuTreeBuilder.putConsumer(NUIEditorMenuTreeBuilder.OPTION_EDIT, this::editNode);
            nuiEditorMenuTreeBuilder.putConsumer(NUIEditorMenuTreeBuilder.OPTION_DELETE, getEditor()::deleteNode);
            nuiEditorMenuTreeBuilder.putConsumer(NUIEditorMenuTreeBuilder.OPTION_ADD_WIDGET, this::addWidget);
            nuiEditorMenuTreeBuilder.subscribeAddContextMenu(n -> {
                getEditor().fireUpdateListeners();
                // Automatically edit a node that's been added.
                if (n.getValue().getType() == JsonTreeValue.Type.KEY_VALUE_PAIR) {
                    getEditor().getModel().getNode(getEditor().getSelectedIndex()).setExpanded(true);
                    getEditor().getModel().resetNodes();
                    getEditor().setSelectedIndex(getEditor().getModel().indexOf(n));
                    editNode(n);
                }
            });
            return nuiEditorMenuTreeBuilder.createPrimarySkinContextMenu(node);
        });
        editor.setEditor(this::editNode, getManager());
        editor.subscribeTreeViewUpdate(() -> {
            getEditor().addToHistory();
            resetPreviewWidget();
            updateConfig();
            setUnsavedChangesPresent(true);
            updateAutosave();
        });
    }
    UIButton save = find("save", UIButton.class);
    save.bindEnabled(new ReadOnlyBinding<Boolean>() {

        @Override
        public Boolean get() {
            return CREATE_NEW_SKIN.equals(selectedAsset) || areUnsavedChangesPresent();
        }
    });
    save.subscribe(button -> {
        // Save the current look and feel.
        LookAndFeel currentLookAndFeel = UIManager.getLookAndFeel();
        // (Temporarily) set the look and feel to the system default.
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | IllegalAccessException | InstantiationException | UnsupportedLookAndFeelException ignored) {
        }
        // Configure the file chooser.
        JFileChooser fileChooser = new JFileChooser() {

            @Override
            protected JDialog createDialog(Component parent) {
                JDialog dialog = super.createDialog(parent);
                dialog.setLocationByPlatform(true);
                dialog.setAlwaysOnTop(true);
                return dialog;
            }
        };
        fileChooser.setSelectedFile(new File(CREATE_NEW_SKIN.equals(selectedAsset) ? "untitled.skin" : selectedAsset.split(":")[1] + ".skin"));
        fileChooser.setFileFilter(new FileNameExtensionFilter("Skin asset file (*.skin)", "skin"));
        if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
            saveToFile(fileChooser.getSelectedFile());
            deleteAutosave();
        }
        // Reload the look and feel.
        try {
            UIManager.setLookAndFeel(currentLookAndFeel);
        } catch (UnsupportedLookAndFeelException ignored) {
        }
    });
    UIButton override = find("override", UIButton.class);
    override.bindEnabled(new ReadOnlyBinding<Boolean>() {

        @Override
        public Boolean get() {
            return selectedAssetPath != null && areUnsavedChangesPresent();
        }
    });
    override.subscribe(button -> {
        saveToFile(selectedAssetPath);
        deleteAutosave();
    });
    // Set the handlers for the editor buttons.
    WidgetUtil.trySubscribe(this, "settings", button -> getManager().pushScreen(NUIEditorSettingsScreen.ASSET_URI, NUIEditorSettingsScreen.class));
    WidgetUtil.trySubscribe(this, "copy", button -> copyJson());
    WidgetUtil.trySubscribe(this, "paste", button -> pasteJson());
    WidgetUtil.trySubscribe(this, "undo", button -> undo());
    WidgetUtil.trySubscribe(this, "redo", button -> redo());
    WidgetUtil.trySubscribe(this, "close", button -> nuiSkinEditorSystem.toggleEditor());
    updateConfig();
}
Also used : UIElement(org.terasology.rendering.nui.asset.UIElement) UIDropdownScrollable(org.terasology.rendering.nui.widgets.UIDropdownScrollable) UIBox(org.terasology.rendering.nui.widgets.UIBox) LookAndFeel(javax.swing.LookAndFeel) UIButton(org.terasology.rendering.nui.widgets.UIButton) JsonEditorTreeView(org.terasology.rendering.nui.widgets.JsonEditorTreeView) ResourceUrn(org.terasology.assets.ResourceUrn) Component(java.awt.Component) NUIEditorMenuTreeBuilder(org.terasology.rendering.nui.editor.utils.NUIEditorMenuTreeBuilder) FileNameExtensionFilter(javax.swing.filechooser.FileNameExtensionFilter) UnsupportedLookAndFeelException(javax.swing.UnsupportedLookAndFeelException) JFileChooser(javax.swing.JFileChooser) AssetDataFile(org.terasology.assets.format.AssetDataFile) File(java.io.File) JDialog(javax.swing.JDialog)

Example 10 with ResourceUrn

use of org.terasology.assets.ResourceUrn in project Terasology by MovingBlocks.

the class WorldSerializerImpl method createPrefab.

private void createPrefab(EntityData.Prefab prefabData) {
    SimpleUri uri = new SimpleUri(prefabData.getName());
    try (ModuleContext.ContextSpan ignored = ModuleContext.setContext(moduleManager.getEnvironment().get(uri.getModuleName()))) {
        PrefabData protoPrefab = prefabSerializer.deserialize(prefabData);
        Assets.generateAsset(new ResourceUrn(prefabData.getName()), protoPrefab, Prefab.class);
    } catch (Exception e) {
        logger.error("Failed to create prefab {}", prefabData.getName());
    }
}
Also used : PrefabData(org.terasology.entitySystem.prefab.PrefabData) SimpleUri(org.terasology.engine.SimpleUri) ModuleContext(org.terasology.persistence.ModuleContext) ResourceUrn(org.terasology.assets.ResourceUrn)

Aggregations

ResourceUrn (org.terasology.assets.ResourceUrn)65 Before (org.junit.Before)10 BlockFamilyDefinitionData (org.terasology.world.block.loader.BlockFamilyDefinitionData)10 AssetManager (org.terasology.assets.management.AssetManager)9 Prefab (org.terasology.entitySystem.prefab.Prefab)9 SymmetricBlockFamilyFactory (org.terasology.world.block.family.SymmetricBlockFamilyFactory)9 Command (org.terasology.logic.console.commandSystem.annotations.Command)8 EntityRef (org.terasology.entitySystem.entity.EntityRef)7 PrefabData (org.terasology.entitySystem.prefab.PrefabData)7 BlockUri (org.terasology.world.block.BlockUri)7 Name (org.terasology.naming.Name)6 Texture (org.terasology.rendering.assets.texture.Texture)6 BlockManagerImpl (org.terasology.world.block.internal.BlockManagerImpl)6 NullWorldAtlas (org.terasology.world.block.tiles.NullWorldAtlas)6 ByteBuffer (java.nio.ByteBuffer)5 Test (org.junit.Test)5 SimpleUri (org.terasology.engine.SimpleUri)5 IOException (java.io.IOException)4 PojoPrefab (org.terasology.entitySystem.prefab.internal.PojoPrefab)4 BiomeManager (org.terasology.world.biomes.BiomeManager)4