Search in sources :

Example 1 with UIDropdownScrollable

use of org.terasology.nui.widgets.UIDropdownScrollable in project Terasology by MovingBlocks.

the class ObjectLayoutBuilder method populateNullLayout.

private void populateNullLayout(Binding<T> binding, ColumnLayout layout, UILabel nameWidget) {
    // TODO: Translate
    if (MODIFY_LABEL.equals(nameWidget.getText())) {
        nameWidget.setText(NULL_LABEL);
    }
    List<Constructor<T>> constructors = Arrays.stream(type.getRawType().getConstructors()).map(constructor -> (Constructor<T>) constructor).collect(Collectors.toList());
    if (constructors.isEmpty()) {
        // TODO: Translate
        UIBox box = buildErrorWidget("No accessible constructors found");
        layout.addWidget(box);
        return;
    }
    ColumnLayout parameterLayout = createDefaultLayout();
    UIButton createInstanceButton = new UIButton();
    // TODO: Translate
    createInstanceButton.setText("Create Instance");
    UIDropdownScrollable<Constructor<T>> constructorSelection = new UIDropdownScrollable<>();
    Binding<Constructor<T>> selectedConstructor = new NotifyingBinding<Constructor<T>>(constructors.get(0)) {

        @Override
        protected void onSet() {
            populateConstructorParameters(binding, parameterLayout, createInstanceButton, this);
        }
    };
    constructorSelection.setOptions(constructors);
    constructorSelection.bindSelection(selectedConstructor);
    constructorSelection.setOptionRenderer(new StringTextRenderer<Constructor<T>>() {

        @Override
        public String getString(Constructor<T> value) {
            return ReflectionUtil.resolvedMethodToString(type.getType(), value, true);
        }
    });
    // TODO: Translate
    constructorSelection.setTooltip("Select the constructor to use to create the new object");
    layout.addWidget(constructorSelection);
    layout.addWidget(parameterLayout);
    layout.addWidget(createInstanceButton);
}
Also used : Arrays(java.util.Arrays) Defaults(com.google.common.base.Defaults) LoggerFactory(org.slf4j.LoggerFactory) TypeWidgetLibrary(org.terasology.nui.widgets.types.TypeWidgetLibrary) TypeInfo(org.terasology.reflection.TypeInfo) ExpandableLayoutBuilder(org.terasology.nui.widgets.types.builtin.util.ExpandableLayoutBuilder) FieldsWidgetBuilder(org.terasology.nui.widgets.types.builtin.util.FieldsWidgetBuilder) WidgetUtil(org.terasology.nui.WidgetUtil) Constructor(java.lang.reflect.Constructor) UIBox(org.terasology.nui.widgets.UIBox) NotifyingBinding(org.terasology.nui.databinding.NotifyingBinding) DefaultBinding(org.terasology.nui.databinding.DefaultBinding) ColumnLayout(org.terasology.nui.layouts.ColumnLayout) Parameter(java.lang.reflect.Parameter) StringTextRenderer(org.terasology.nui.itemRendering.StringTextRenderer) Binding(org.terasology.nui.databinding.Binding) UIDropdownScrollable(org.terasology.nui.widgets.UIDropdownScrollable) Logger(org.slf4j.Logger) UILabel(org.terasology.nui.widgets.UILabel) ReflectionUtil(org.terasology.engine.utilities.ReflectionUtil) Collectors(java.util.stream.Collectors) InvocationTargetException(java.lang.reflect.InvocationTargetException) UIWidget(org.terasology.nui.UIWidget) List(java.util.List) UIButton(org.terasology.nui.widgets.UIButton) LABEL_WIDGET_ID(org.terasology.nui.widgets.types.TypeWidgetFactory.LABEL_WIDGET_ID) Optional(java.util.Optional) Constructor(java.lang.reflect.Constructor) UIBox(org.terasology.nui.widgets.UIBox) UIDropdownScrollable(org.terasology.nui.widgets.UIDropdownScrollable) ColumnLayout(org.terasology.nui.layouts.ColumnLayout) UIButton(org.terasology.nui.widgets.UIButton) NotifyingBinding(org.terasology.nui.databinding.NotifyingBinding)

Example 2 with UIDropdownScrollable

use of org.terasology.nui.widgets.UIDropdownScrollable in project Terasology by MovingBlocks.

the class LocaleConstraintWidgetFactory method buildWidget.

@Override
protected Optional<UIWidget> buildWidget() {
    Setting<Locale> setting = getSetting();
    Binding<Locale> binding = new Binding<Locale>() {

        @Override
        public Locale get() {
            return setting.get();
        }

        @Override
        public void set(Locale value) {
            setting.set(value);
        }
    };
    UIDropdownScrollable<Locale> dropdownScrollable = new UIDropdownScrollable<>();
    ResourceUrn menuUrn = new ResourceUrn("engine:menu");
    TranslationProject menuProject = translationSystem.getProject(menuUrn);
    List<Locale> locales = new ArrayList<>(menuProject.getAvailableLocales());
    for (Locale languageExcluded : languagesExcluded) {
        locales.remove(languageExcluded);
    }
    Collections.sort(locales, (Comparator.comparing((Function<Object, String>) Object::toString)));
    dropdownScrollable.setOptions(Lists.newArrayList(locales));
    // Set maximum number of options visible for scrolling
    dropdownScrollable.setVisibleOptions(5);
    dropdownScrollable.bindSelection(binding);
    dropdownScrollable.setOptionRenderer(new LocaleRenderer(translationSystem));
    return Optional.of(dropdownScrollable);
}
Also used : Locale(java.util.Locale) Binding(org.terasology.nui.databinding.Binding) LocaleRenderer(org.terasology.engine.rendering.nui.layers.mainMenu.settings.LocaleRenderer) UIDropdownScrollable(org.terasology.nui.widgets.UIDropdownScrollable) ArrayList(java.util.ArrayList) TranslationProject(org.terasology.engine.i18n.TranslationProject) ResourceUrn(org.terasology.gestalt.assets.ResourceUrn)

Example 3 with UIDropdownScrollable

use of org.terasology.nui.widgets.UIDropdownScrollable in project Terasology by MovingBlocks.

the class WorldPreGenerationScreen method setEnvironment.

/**
 * A function called before the screen comes to the forefront to setup the environment
 * and extract necessary objects from the new Context.
 *
 * @param subContext The new environment created in {@link UniverseSetupScreen}
 * @throws UnresolvedWorldGeneratorException The creation of a world generator can throw this Exception
 */
public void setEnvironment(Context subContext) throws UnresolvedWorldGeneratorException {
    context = subContext;
    environment = context.get(ModuleEnvironment.class);
    context.put(WorldGeneratorPluginLibrary.class, new TempWorldGeneratorPluginLibrary(environment, context));
    worldList = context.get(UniverseSetupScreen.class).getWorldsList();
    selectedWorld = context.get(UniverseSetupScreen.class).getSelectedWorld();
    worldNames = context.get(UniverseSetupScreen.class).worldNames();
    setWorldGenerators();
    worldGenerator = findWorldByName(selectedWorld).getWorldGenerator();
    final UIDropdownScrollable worldsDropdown = find("worlds", UIDropdownScrollable.class);
    worldsDropdown.setOptions(worldNames);
    genTexture();
    List<Zone> previewZones = Lists.newArrayList(worldGenerator.getZones()).stream().filter(z -> !z.getPreviewLayers().isEmpty()).collect(Collectors.toList());
    if (previewZones.isEmpty()) {
        previewGen = new FacetLayerPreview(environment, worldGenerator);
    }
}
Also used : MenuAnimationSystems(org.terasology.engine.rendering.nui.animation.MenuAnimationSystems) UISlider(org.terasology.nui.widgets.UISlider) WorldGeneratorManager(org.terasology.engine.world.generator.internal.WorldGeneratorManager) Texture(org.terasology.engine.rendering.assets.texture.Texture) In(org.terasology.engine.registry.In) ModuleManager(org.terasology.engine.core.module.ModuleManager) LoggerFactory(org.slf4j.LoggerFactory) TempWorldGeneratorPluginLibrary(org.terasology.engine.world.generator.plugin.TempWorldGeneratorPluginLibrary) WorldGeneratorPluginLibrary(org.terasology.engine.world.generator.plugin.WorldGeneratorPluginLibrary) Zone(org.terasology.engine.world.zones.Zone) Callable(java.util.concurrent.Callable) WidgetUtil(org.terasology.nui.WidgetUtil) ByteBuffer(java.nio.ByteBuffer) ResourceUrn(org.terasology.gestalt.assets.ResourceUrn) Lists(com.google.common.collect.Lists) UnresolvedWorldGeneratorException(org.terasology.engine.world.generator.UnresolvedWorldGeneratorException) WorldGenerator(org.terasology.engine.world.generator.WorldGenerator) TeraMath(org.terasology.math.TeraMath) Binding(org.terasology.nui.databinding.Binding) UIDropdownScrollable(org.terasology.nui.widgets.UIDropdownScrollable) Logger(org.slf4j.Logger) PreviewGenerator(org.terasology.engine.rendering.nui.layers.mainMenu.preview.PreviewGenerator) Assets(org.terasology.engine.utilities.Assets) FacetLayerPreview(org.terasology.engine.rendering.nui.layers.mainMenu.preview.FacetLayerPreview) ModuleEnvironment(org.terasology.gestalt.module.ModuleEnvironment) UIImage(org.terasology.nui.widgets.UIImage) Collectors(java.util.stream.Collectors) CoreScreenLayer(org.terasology.engine.rendering.nui.CoreScreenLayer) NUIManager(org.terasology.engine.rendering.nui.NUIManager) List(java.util.List) Context(org.terasology.engine.context.Context) Config(org.terasology.engine.config.Config) UISliderOnChangeTriggeredListener(org.terasology.nui.widgets.UISliderOnChangeTriggeredListener) TextureData(org.terasology.engine.rendering.assets.texture.TextureData) WorldSetupWrapper(org.terasology.engine.rendering.world.WorldSetupWrapper) Name(org.terasology.gestalt.naming.Name) ModuleEnvironment(org.terasology.gestalt.module.ModuleEnvironment) Zone(org.terasology.engine.world.zones.Zone) UIDropdownScrollable(org.terasology.nui.widgets.UIDropdownScrollable) TempWorldGeneratorPluginLibrary(org.terasology.engine.world.generator.plugin.TempWorldGeneratorPluginLibrary) FacetLayerPreview(org.terasology.engine.rendering.nui.layers.mainMenu.preview.FacetLayerPreview)

Example 4 with UIDropdownScrollable

use of org.terasology.nui.widgets.UIDropdownScrollable in project Terasology by MovingBlocks.

the class WorldPreGenerationScreen method initialise.

@Override
public void initialise() {
    setAnimationSystem(MenuAnimationSystems.createDefaultSwipeAnimation());
    zoomSlider = find("zoomSlider", UISlider.class);
    if (zoomSlider != null) {
        zoomSlider.setValue(2f);
        zoomSlider.setUiSliderOnChangeTriggeredListener(this);
    }
    final UIDropdownScrollable worldsDropdown = find("worlds", UIDropdownScrollable.class);
    worldsDropdown.bindSelection(new Binding<String>() {

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

        @Override
        public void set(String value) {
            selectedWorld = value;
            try {
                if (findWorldByName(selectedWorld).getWorldGenerator() == null) {
                    worldGenerator = WorldGeneratorManager.createWorldGenerator(findWorldByName(selectedWorld).getWorldGeneratorInfo().getUri(), context, environment);
                    findWorldByName(selectedWorld).setWorldGenerator(worldGenerator);
                } else {
                    worldGenerator = findWorldByName(selectedWorld).getWorldGenerator();
                }
                if (worldGenerator.getWorldSeed() == null) {
                    worldGenerator.setWorldSeed(createSeed(selectedWorld));
                }
                previewGen = new FacetLayerPreview(environment, worldGenerator);
                updatePreview();
            } catch (UnresolvedWorldGeneratorException e) {
                e.printStackTrace();
            }
        }
    });
    WidgetUtil.trySubscribe(this, "reRoll", button -> {
        worldGenerator.setWorldSeed(createSeed(selectedWorld));
        updatePreview();
    });
    StartPlayingScreen startPlayingScreen = getManager().createScreen(StartPlayingScreen.ASSET_URI, StartPlayingScreen.class);
    WidgetUtil.trySubscribe(this, "continue", button -> {
        startPlayingScreen.setTargetWorld(worldList, findWorldByName(selectedWorld), texture, context);
        triggerForwardAnimation(startPlayingScreen);
    });
    WorldSetupScreen worldSetupScreen = getManager().createScreen(WorldSetupScreen.ASSET_URI, WorldSetupScreen.class);
    WidgetUtil.trySubscribe(this, "config", button -> {
        try {
            if (!selectedWorld.isEmpty()) {
                worldSetupScreen.setWorld(context, findWorldByName(selectedWorld), worldsDropdown);
                triggerForwardAnimation(worldSetupScreen);
            } else {
                getManager().pushScreen(MessagePopup.ASSET_URI, MessagePopup.class).setMessage("Worlds List Empty!", "No world found to configure.");
            }
        } catch (UnresolvedWorldGeneratorException e) {
            e.printStackTrace();
        }
    });
    WidgetUtil.trySubscribe(this, "close", button -> {
        final UniverseSetupScreen universeSetupScreen = getManager().createScreen(UniverseSetupScreen.ASSET_URI, UniverseSetupScreen.class);
        UIDropdownScrollable worldsDropdownOfUniverse = universeSetupScreen.find("worlds", UIDropdownScrollable.class);
        universeSetupScreen.refreshWorldDropdown(worldsDropdownOfUniverse);
        triggerBackAnimation();
    });
    WidgetUtil.trySubscribe(this, "mainMenu", button -> {
        getManager().pushScreen("engine:mainMenuScreen");
    });
}
Also used : UnresolvedWorldGeneratorException(org.terasology.engine.world.generator.UnresolvedWorldGeneratorException) UISlider(org.terasology.nui.widgets.UISlider) UIDropdownScrollable(org.terasology.nui.widgets.UIDropdownScrollable) FacetLayerPreview(org.terasology.engine.rendering.nui.layers.mainMenu.preview.FacetLayerPreview)

Example 5 with UIDropdownScrollable

use of org.terasology.nui.widgets.UIDropdownScrollable 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

UIDropdownScrollable (org.terasology.nui.widgets.UIDropdownScrollable)8 List (java.util.List)4 Binding (org.terasology.nui.databinding.Binding)4 Collectors (java.util.stream.Collectors)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3 UnresolvedWorldGeneratorException (org.terasology.engine.world.generator.UnresolvedWorldGeneratorException)3 ResourceUrn (org.terasology.gestalt.assets.ResourceUrn)3 Optional (java.util.Optional)2 Set (java.util.Set)2 ModuleManager (org.terasology.engine.core.module.ModuleManager)2 FacetLayerPreview (org.terasology.engine.rendering.nui.layers.mainMenu.preview.FacetLayerPreview)2 ReflectionUtil (org.terasology.engine.utilities.ReflectionUtil)2 WidgetUtil (org.terasology.nui.WidgetUtil)2 UISlider (org.terasology.nui.widgets.UISlider)2 Defaults (com.google.common.base.Defaults)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Lists (com.google.common.collect.Lists)1 Component (java.awt.Component)1 File (java.io.File)1