Search in sources :

Example 6 with UIDropdownScrollable

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

the class UniverseSetupScreen method initialise.

@Override
public void initialise() {
    setAnimationSystem(MenuAnimationSystems.createDefaultSwipeAnimation());
    final UIDropdownScrollable<WorldGeneratorInfo> worldGenerator = find("worldGenerators", UIDropdownScrollable.class);
    if (worldGenerator != null) {
        worldGenerator.bindOptions(new ReadOnlyBinding<List<WorldGeneratorInfo>>() {

            @Override
            public List<WorldGeneratorInfo> get() {
                // grab all the module names and their dependencies
                // This grabs modules from `config.getDefaultModSelection()` which is updated in AdvancedGameSetupScreen
                final Set<Name> enabledModuleNames = new HashSet<>(getAllEnabledModuleNames());
                final List<WorldGeneratorInfo> result = Lists.newArrayList();
                for (WorldGeneratorInfo option : worldGeneratorManager.getWorldGenerators()) {
                    if (enabledModuleNames.contains(option.getUri().getModuleName())) {
                        result.add(option);
                    }
                }
                return result;
            }
        });
        worldGenerator.setVisibleOptions(3);
        worldGenerator.bindSelection(new Binding<WorldGeneratorInfo>() {

            @Override
            public WorldGeneratorInfo get() {
                // get the default generator from the config. This is likely to have a user triggered selection.
                WorldGeneratorInfo info = worldGeneratorManager.getWorldGeneratorInfo(config.getWorldGeneration().getDefaultGenerator());
                if (info != null && getAllEnabledModuleNames().contains(info.getUri().getModuleName())) {
                    return info;
                }
                // just use the first available generator
                for (WorldGeneratorInfo worldGenInfo : worldGeneratorManager.getWorldGenerators()) {
                    if (getAllEnabledModuleNames().contains(worldGenInfo.getUri().getModuleName())) {
                        set(worldGenInfo);
                        return worldGenInfo;
                    }
                }
                return null;
            }

            @Override
            public void set(WorldGeneratorInfo value) {
                if (value != null) {
                    config.getWorldGeneration().setDefaultGenerator(value.getUri());
                }
            }
        });
        worldGenerator.setOptionRenderer(new StringTextRenderer<WorldGeneratorInfo>() {

            @Override
            public String getString(WorldGeneratorInfo value) {
                if (value != null) {
                    return value.getDisplayName();
                }
                return "";
            }
        });
    }
    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;
            indexOfSelectedWorld = findIndex(worlds, selectedWorld);
        }
    });
    WidgetUtil.trySubscribe(this, "close", button -> triggerBackAnimation());
    WidgetUtil.trySubscribe(this, "worldConfig", button -> {
        final WorldSetupScreen worldSetupScreen = getManager().createScreen(WorldSetupScreen.ASSET_URI, WorldSetupScreen.class);
        try {
            if (!worlds.isEmpty() || !selectedWorld.isEmpty()) {
                worldSetupScreen.setWorld(context, findWorldByName(), worldsDropdown);
                triggerForwardAnimation(worldSetupScreen);
            } else {
                getManager().pushScreen(MessagePopup.ASSET_URI, MessagePopup.class).setMessage("Worlds List Empty!", "No world found to configure.");
            }
        } catch (UnresolvedWorldGeneratorException e) {
            logger.error("Can't configure the world! due to {}", e.getMessage());
        }
    });
    WidgetUtil.trySubscribe(this, "addGenerator", button -> {
        // modules may do
        if (worldGenerator.getSelection().getUri().toString().equals("CoreWorlds:heightMap")) {
            getManager().pushScreen(MessagePopup.ASSET_URI, MessagePopup.class).setMessage("HeightMap not supported", "HeightMap is not supported for advanced setup right now, a game template will be introduced soon.");
        } else {
            addNewWorld(worldGenerator.getSelection());
            worldsDropdown.setOptions(worldNames());
        }
    });
    WidgetUtil.trySubscribe(this, "continue", button -> {
        final WorldPreGenerationScreen worldPreGenerationScreen = getManager().createScreen(WorldPreGenerationScreen.ASSET_URI, WorldPreGenerationScreen.class);
        if (!worlds.isEmpty()) {
            final WaitPopup<Boolean> loadPopup = getManager().pushScreen(WaitPopup.ASSET_URI, WaitPopup.class);
            loadPopup.setMessage("Loading", "please wait ...");
            loadPopup.onSuccess(result -> {
                if (result != null && result) {
                    triggerForwardAnimation(worldPreGenerationScreen);
                } else {
                    getManager().pushScreen(MessagePopup.ASSET_URI, MessagePopup.class).setMessage("Error", "Can't load world pre generation screen! Please, try again!");
                }
            });
            loadPopup.startOperation(() -> {
                try {
                    worldPreGenerationScreen.setEnvironment(context);
                } catch (UnresolvedWorldGeneratorException e) {
                    return false;
                }
                return true;
            }, true);
        } else {
            getManager().pushScreen(MessagePopup.ASSET_URI, MessagePopup.class).setMessage("Worlds List Empty!", "Please select a world generator and add words to the dropdown!");
        }
    });
    WidgetUtil.trySubscribe(this, "mainMenu", button -> {
        getManager().pushScreen("engine:mainMenuScreen");
    });
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) UnresolvedWorldGeneratorException(org.terasology.engine.world.generator.UnresolvedWorldGeneratorException) UIDropdownScrollable(org.terasology.nui.widgets.UIDropdownScrollable) WorldGeneratorInfo(org.terasology.engine.world.generator.internal.WorldGeneratorInfo) List(java.util.List)

Example 7 with UIDropdownScrollable

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

the class UniverseSetupScreen method onOpened.

@Override
public void onOpened() {
    super.onOpened();
    worlds.clear();
    worldNumber = 0;
    final UIDropdownScrollable worldsDropdown = find("worlds", UIDropdownScrollable.class);
    if (worldsDropdown != null) {
        worldsDropdown.setOptions(worldNames());
    }
    selectedWorld = "";
    indexOfSelectedWorld = findIndex(worlds, selectedWorld);
}
Also used : UIDropdownScrollable(org.terasology.nui.widgets.UIDropdownScrollable)

Example 8 with UIDropdownScrollable

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

the class SubtypeLayoutBuilder method populate.

@Override
protected void populate(Binding<T> binding, ColumnLayout layout, ColumnLayout mainLayout) {
    ColumnLayout widgetContainer = new ColumnLayout();
    Binding<TypeInfo<T>> editingType = new NotifyingBinding<TypeInfo<T>>(baseType) {

        @Override
        protected void onSet() {
            widgetContainer.removeAllWidgets();
            TypeWidgetBuilder<T> builder = SubtypeLayoutBuilder.this.library.getBuilder(get()).orElse(baseTypeWidgetBuilder);
            if (builder == null) {
                LOGGER.error("Could not find widget for type {}, editing as base type {}", get(), baseType);
                return;
            }
            widgetContainer.addWidget(builder.build(binding));
        }
    };
    if (binding.get() != null && !editingType.get().getRawType().equals(binding.get().getClass())) {
        Type actual = ReflectionUtil.parameterizeandResolveRawType(baseType.getType(), binding.get().getClass());
        TypeInfo<T> actualType = (TypeInfo<T>) TypeInfo.of(actual);
        if (!allowedSubtypes.contains(actualType)) {
            Optional<TypeInfo<T>> closestMatch = allowedSubtypes.stream().filter(subtype -> subtype.getRawType().isAssignableFrom(actualType.getRawType())).findFirst();
            // closestMatch is always present since editingType is guaranteed to be a subtype of T
            assert closestMatch.isPresent();
            editingType.set(closestMatch.get());
        } else {
            editingType.set(actualType);
        }
    }
    UIDropdownScrollable<TypeInfo<T>> typeSelection = new UIDropdownScrollable<>();
    typeSelection.setOptions(allowedSubtypes);
    typeSelection.bindSelection(editingType);
    typeSelection.setOptionRenderer(new StringTextRenderer<TypeInfo<T>>() {

        @Override
        public String getString(TypeInfo<T> value) {
            return getTypeName(value);
        }
    });
    // TODO: Translate
    typeSelection.setTooltip("Select the type for the new object");
    layout.addWidget(typeSelection);
    layout.addWidget(widgetContainer);
}
Also used : ModuleManager(org.terasology.engine.core.module.ModuleManager) LoggerFactory(org.slf4j.LoggerFactory) TypeWidgetLibrary(org.terasology.nui.widgets.types.TypeWidgetLibrary) TypeInfo(org.terasology.reflection.TypeInfo) TypeRegistry(org.terasology.reflection.TypeRegistry) ExpandableLayoutBuilder(org.terasology.nui.widgets.types.builtin.util.ExpandableLayoutBuilder) NotifyingBinding(org.terasology.nui.databinding.NotifyingBinding) ColumnLayout(org.terasology.nui.layouts.ColumnLayout) StringTextRenderer(org.terasology.nui.itemRendering.StringTextRenderer) Binding(org.terasology.nui.databinding.Binding) UIDropdownScrollable(org.terasology.nui.widgets.UIDropdownScrollable) ImmutableSet(com.google.common.collect.ImmutableSet) Logger(org.slf4j.Logger) PermissionProvider(org.terasology.gestalt.module.sandbox.PermissionProvider) Module(org.terasology.gestalt.module.Module) ModuleEnvironment(org.terasology.gestalt.module.ModuleEnvironment) ModuleContext(org.terasology.engine.core.module.ModuleContext) Set(java.util.Set) ReflectionUtil(org.terasology.engine.utilities.ReflectionUtil) Collectors(java.util.stream.Collectors) List(java.util.List) TypeWidgetBuilder(org.terasology.nui.widgets.types.TypeWidgetBuilder) Type(java.lang.reflect.Type) Modifier(java.lang.reflect.Modifier) Optional(java.util.Optional) Name(org.terasology.gestalt.naming.Name) UIDropdownScrollable(org.terasology.nui.widgets.UIDropdownScrollable) TypeInfo(org.terasology.reflection.TypeInfo) Type(java.lang.reflect.Type) ColumnLayout(org.terasology.nui.layouts.ColumnLayout) NotifyingBinding(org.terasology.nui.databinding.NotifyingBinding)

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