Search in sources :

Example 1 with NUIEditorMenuTreeBuilder

use of org.terasology.engine.rendering.nui.editor.utils.NUIEditorMenuTreeBuilder in project Terasology by MovingBlocks.

the class NUIEditorScreen method initialise.

@Override
public void initialise() {
    // Retrieve the widgets based on their identifiers.
    UIDropdownScrollable<String> availableAssetDropdown = find(AVAILABLE_ASSETS_ID, UIDropdownScrollable.class);
    JsonEditorTreeView editor = find(EDITOR_TREE_VIEW_ID, JsonEditorTreeView.class);
    selectedScreenBox = find(SELECTED_SCREEN_ID, UIBox.class);
    super.setEditorSystem(nuiEditorSystem);
    super.setEditor(editor);
    // Populate the list of screens.
    List<String> availableAssetList = Lists.newArrayList();
    availableAssetList.add(CREATE_NEW_SCREEN);
    availableAssetList.addAll(assetManager.getAvailableAssets(UIElement.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 screen tree (or de-serialize from an existing asset)
                selectedAssetPending = value;
                if (CREATE_NEW_SCREEN.equals(value)) {
                    selectedAssetPath = null;
                    resetState(NUIEditorNodeUtils.createNewScreen());
                } else {
                    selectAsset(new ResourceUrn(value));
                }
            }
        });
    }
    if (editor != null) {
        editor.subscribeTreeViewUpdate(() -> {
            getEditor().addToHistory();
            resetPreviewWidget();
            updateConfig();
            setUnsavedChangesPresent(true);
            updateAutosave();
        });
        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.createPrimaryContextMenu(node);
        });
        editor.setEditor(this::editNode, getManager());
    }
    UIButton save = find("save", UIButton.class);
    save.bindEnabled(new ReadOnlyBinding<Boolean>() {

        @Override
        public Boolean get() {
            return CREATE_NEW_SCREEN.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_SCREEN.equals(selectedAsset) ? "untitled.ui" : selectedAsset.split(":")[1] + ".ui"));
        fileChooser.setFileFilter(new FileNameExtensionFilter("UI asset file (*.ui)", "ui"));
        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 -> nuiEditorSystem.toggleEditor());
    updateConfig();
}
Also used : UIBox(org.terasology.nui.widgets.UIBox) LookAndFeel(javax.swing.LookAndFeel) UIButton(org.terasology.nui.widgets.UIButton) JsonEditorTreeView(org.terasology.engine.rendering.nui.widgets.JsonEditorTreeView) ResourceUrn(org.terasology.gestalt.assets.ResourceUrn) Component(java.awt.Component) NUIEditorMenuTreeBuilder(org.terasology.engine.rendering.nui.editor.utils.NUIEditorMenuTreeBuilder) FileNameExtensionFilter(javax.swing.filechooser.FileNameExtensionFilter) UnsupportedLookAndFeelException(javax.swing.UnsupportedLookAndFeelException) JFileChooser(javax.swing.JFileChooser) AssetDataFile(org.terasology.gestalt.assets.format.AssetDataFile) File(java.io.File) JDialog(javax.swing.JDialog)

Example 2 with NUIEditorMenuTreeBuilder

use of org.terasology.engine.rendering.nui.editor.utils.NUIEditorMenuTreeBuilder 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(UISkinAsset.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.nui.asset.UIElement) UIDropdownScrollable(org.terasology.nui.widgets.UIDropdownScrollable) UIBox(org.terasology.nui.widgets.UIBox) LookAndFeel(javax.swing.LookAndFeel) UIButton(org.terasology.nui.widgets.UIButton) JsonEditorTreeView(org.terasology.engine.rendering.nui.widgets.JsonEditorTreeView) ResourceUrn(org.terasology.gestalt.assets.ResourceUrn) Component(java.awt.Component) NUIEditorMenuTreeBuilder(org.terasology.engine.rendering.nui.editor.utils.NUIEditorMenuTreeBuilder) FileNameExtensionFilter(javax.swing.filechooser.FileNameExtensionFilter) UnsupportedLookAndFeelException(javax.swing.UnsupportedLookAndFeelException) JFileChooser(javax.swing.JFileChooser) AssetDataFile(org.terasology.gestalt.assets.format.AssetDataFile) File(java.io.File) JDialog(javax.swing.JDialog)

Aggregations

Component (java.awt.Component)2 File (java.io.File)2 JDialog (javax.swing.JDialog)2 JFileChooser (javax.swing.JFileChooser)2 LookAndFeel (javax.swing.LookAndFeel)2 UnsupportedLookAndFeelException (javax.swing.UnsupportedLookAndFeelException)2 FileNameExtensionFilter (javax.swing.filechooser.FileNameExtensionFilter)2 NUIEditorMenuTreeBuilder (org.terasology.engine.rendering.nui.editor.utils.NUIEditorMenuTreeBuilder)2 JsonEditorTreeView (org.terasology.engine.rendering.nui.widgets.JsonEditorTreeView)2 ResourceUrn (org.terasology.gestalt.assets.ResourceUrn)2 AssetDataFile (org.terasology.gestalt.assets.format.AssetDataFile)2 UIBox (org.terasology.nui.widgets.UIBox)2 UIButton (org.terasology.nui.widgets.UIButton)2 UIElement (org.terasology.nui.asset.UIElement)1 UIDropdownScrollable (org.terasology.nui.widgets.UIDropdownScrollable)1