Search in sources :

Example 1 with UIText

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

the class CreateGameScreen method initialise.

@Override
@SuppressWarnings("unchecked")
public void initialise() {
    setAnimationSystem(MenuAnimationSystems.createDefaultSwipeAnimation());
    UILabel gameTypeTitle = find("gameTypeTitle", UILabel.class);
    if (gameTypeTitle != null) {
        gameTypeTitle.bindText(new ReadOnlyBinding<String>() {

            @Override
            public String get() {
                if (loadingAsServer) {
                    return translationSystem.translate("${engine:menu#select-multiplayer-game-sub-title}");
                } else {
                    return translationSystem.translate("${engine:menu#select-singleplayer-game-sub-title}");
                }
            }
        });
    }
    final UIText worldName = find("worldName", UIText.class);
    setGameName(worldName);
    final UIText seed = find("seed", UIText.class);
    if (seed != null) {
        seed.setText(new FastRandom().nextString(32));
    }
    final UIDropdownScrollable<Module> gameplay = find("gameplay", UIDropdownScrollable.class);
    gameplay.setOptions(getGameplayModules());
    gameplay.setVisibleOptions(3);
    gameplay.bindSelection(new Binding<Module>() {

        Module selected;

        @Override
        public Module get() {
            return selected;
        }

        @Override
        public void set(Module value) {
            setSelectedGameplayModule(value);
            selected = value;
        }
    });
    gameplay.setOptionRenderer(new StringTextRenderer<Module>() {

        @Override
        public String getString(Module value) {
            return value.getMetadata().getDisplayName().value();
        }

        @Override
        public void draw(Module value, Canvas canvas) {
            canvas.getCurrentStyle().setTextColor(validateModuleDependencies(value.getId()) ? Color.WHITE : Color.RED);
            super.draw(value, canvas);
            canvas.getCurrentStyle().setTextColor(Color.WHITE);
        }
    });
    UILabel gameplayDescription = find("gameplayDescription", UILabel.class);
    gameplayDescription.bindText(new ReadOnlyBinding<String>() {

        @Override
        public String get() {
            Module selectedModule = gameplay.getSelection();
            if (selectedModule != null) {
                return selectedModule.getMetadata().getDescription().value();
            } else {
                return "";
            }
        }
    });
    final UIDropdownScrollable<WorldGeneratorInfo> worldGenerator = find("worldGenerator", 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 SelectModulesScreen
                Set<Name> enabledModuleNames = getAllEnabledModuleNames().stream().collect(Collectors.toSet());
                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 UIButton playButton = find("play", UIButton.class);
        playButton.bindEnabled(new Binding<Boolean>() {

            @Override
            public Boolean get() {
                return validateModuleDependencies(gameplay.getSelection().getId());
            }

            @Override
            public void set(Boolean value) {
                playButton.setEnabled(value);
            }
        });
    }
    WidgetUtil.trySubscribe(this, "close", button -> triggerBackAnimation());
    WidgetUtil.trySubscribe(this, "play", button -> {
        if (worldGenerator.getSelection() == null) {
            MessagePopup errorMessagePopup = getManager().pushScreen(MessagePopup.ASSET_URI, MessagePopup.class);
            if (errorMessagePopup != null) {
                errorMessagePopup.setMessage("No World Generator Selected", "Select a world generator (you may need to activate a mod with a generator first).");
            }
        } else {
            GameManifest gameManifest = new GameManifest();
            gameManifest.setTitle(worldName.getText());
            gameManifest.setSeed(seed.getText());
            DependencyResolver resolver = new DependencyResolver(moduleManager.getRegistry());
            ResolutionResult result = resolver.resolve(config.getDefaultModSelection().listModules());
            if (!result.isSuccess()) {
                MessagePopup errorMessagePopup = getManager().pushScreen(MessagePopup.ASSET_URI, MessagePopup.class);
                if (errorMessagePopup != null) {
                    errorMessagePopup.setMessage("Invalid Module Selection", "Please review your module seleciton and try again");
                }
                return;
            }
            for (Module module : result.getModules()) {
                gameManifest.addModule(module.getId(), module.getVersion());
            }
            // Time at dawn + little offset to spawn in a brighter env.
            float timeOffset = 0.25f + 0.025f;
            WorldInfo worldInfo = new WorldInfo(TerasologyConstants.MAIN_WORLD, gameManifest.getSeed(), (long) (WorldTime.DAY_LENGTH * timeOffset), worldGenerator.getSelection().getUri());
            gameManifest.addWorld(worldInfo);
            gameEngine.changeState(new StateLoading(gameManifest, (loadingAsServer) ? NetworkMode.DEDICATED_SERVER : NetworkMode.NONE));
        }
    });
    UIButton previewSeed = find("previewSeed", UIButton.class);
    ReadOnlyBinding<Boolean> worldGeneratorSelected = new ReadOnlyBinding<Boolean>() {

        @Override
        public Boolean get() {
            return worldGenerator != null && worldGenerator.getSelection() != null;
        }
    };
    previewSeed.bindEnabled(worldGeneratorSelected);
    PreviewWorldScreen screen = getManager().createScreen(PreviewWorldScreen.ASSET_URI, PreviewWorldScreen.class);
    WidgetUtil.trySubscribe(this, "previewSeed", button -> {
        if (screen != null) {
            screen.bindSeed(BindHelper.bindBeanProperty("text", seed, String.class));
            try {
                screen.setEnvironment();
                triggerForwardAnimation(screen);
            } catch (Exception e) {
                String msg = "Unable to load world for a 2D preview:\n" + e.toString();
                getManager().pushScreen(MessagePopup.ASSET_URI, MessagePopup.class).setMessage("Error", msg);
                logger.error("Unable to load world for a 2D preview", e);
            }
        }
    });
    WidgetUtil.trySubscribe(this, "mods", w -> triggerForwardAnimation(SelectModulesScreen.ASSET_URI));
}
Also used : Set(java.util.Set) ResolutionResult(org.terasology.module.ResolutionResult) WorldGeneratorInfo(org.terasology.world.generator.internal.WorldGeneratorInfo) UIButton(org.terasology.rendering.nui.widgets.UIButton) UIText(org.terasology.rendering.nui.widgets.UIText) WorldInfo(org.terasology.world.internal.WorldInfo) List(java.util.List) UILabel(org.terasology.rendering.nui.widgets.UILabel) StateLoading(org.terasology.engine.modes.StateLoading) ReadOnlyBinding(org.terasology.rendering.nui.databinding.ReadOnlyBinding) Canvas(org.terasology.rendering.nui.Canvas) FastRandom(org.terasology.utilities.random.FastRandom) DependencyResolver(org.terasology.module.DependencyResolver) GameManifest(org.terasology.game.GameManifest) Module(org.terasology.module.Module)

Example 2 with UIText

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

the class StorageServiceLoginPopup method initialise.

@Override
public void initialise() {
    UIText url = find("url", UIText.class);
    UIText login = find("login", UIText.class);
    UIText password = find("password", UIText.class);
    find("existing-identities-warning", UILabel.class).setVisible(!config.getSecurity().getAllIdentities().isEmpty());
    WidgetUtil.trySubscribe(this, "cancel", widget -> getManager().popScreen());
    WidgetUtil.trySubscribe(this, "ok", widget -> {
        try {
            storageService.login(new URL(url.getText()), login.getText(), password.getText());
            getManager().popScreen();
        } catch (MalformedURLException e) {
            getManager().pushScreen(MessagePopup.ASSET_URI, MessagePopup.class).setMessage(translationSystem.translate("${engine:menu#error}"), translationSystem.translate("${engine:menu#storage-service-popup-bad-url}"));
        }
    });
}
Also used : UILabel(org.terasology.rendering.nui.widgets.UILabel) MalformedURLException(java.net.MalformedURLException) UIText(org.terasology.rendering.nui.widgets.UIText) URL(java.net.URL)

Example 3 with UIText

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

the class CreateGameScreen method onOpened.

@Override
public void onOpened() {
    super.onOpened();
    final UIText worldName = find("worldName", UIText.class);
    setGameName(worldName);
    final UIDropdown<Module> gameplay = find("gameplay", UIDropdown.class);
    String configDefaultModuleName = config.getDefaultModSelection().getDefaultGameplayModuleName();
    String useThisModuleName = "";
    // Otherwise, default to DEFAULT_GAME_TEMPLATE_NAME.
    if ("".equalsIgnoreCase(configDefaultModuleName) || DEFAULT_GAME_TEMPLATE_NAME.equalsIgnoreCase(configDefaultModuleName)) {
        useThisModuleName = DEFAULT_GAME_TEMPLATE_NAME;
    } else {
        useThisModuleName = configDefaultModuleName;
    }
    Name defaultGameplayModuleName = new Name(useThisModuleName);
    Module defaultGameplayModule = moduleManager.getRegistry().getLatestModuleVersion(defaultGameplayModuleName);
    if (defaultGameplayModule != null) {
        gameplay.setSelection(defaultGameplayModule);
        if (configDefaultModuleName.equalsIgnoreCase(DEFAULT_GAME_TEMPLATE_NAME)) {
            setDefaultGeneratorOfGameplayModule(defaultGameplayModule);
        }
    } else {
        // Find the first gameplay module that is available.
        for (Module module : moduleManager.getRegistry()) {
            // Module is null if it is no longer present.
            if (module != null && StandardModuleExtension.isGameplayModule(module)) {
                gameplay.setSelection(module);
                break;
            }
        }
    }
}
Also used : UIText(org.terasology.rendering.nui.widgets.UIText) Module(org.terasology.module.Module) Name(org.terasology.naming.Name)

Example 4 with UIText

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

the class ConsoleScreen method initialise.

@Override
public void initialise() {
    setAnimationSystem(new SwipeMenuAnimationSystem(0.2f, Direction.TOP_TO_BOTTOM));
    final ScrollableArea scrollArea = find("scrollArea", ScrollableArea.class);
    scrollArea.moveToBottom();
    commandLine = find("commandLine", UICommandEntry.class);
    getManager().setFocus(commandLine);
    commandLine.setTabCompletionEngine(new CyclingTabCompletionEngine(console, localPlayer));
    commandLine.bindCommandHistory(new ReadOnlyBinding<List<String>>() {

        @Override
        public List<String> get() {
            return console.getPreviousCommands();
        }
    });
    commandLine.subscribe(widget -> {
        String text = commandLine.getText();
        if (StringUtils.isNotBlank(text)) {
            console.execute(text, localPlayer.getClientEntity());
        }
        scrollArea.moveToBottom();
    });
    final UIText history = find("messageHistory", UIText.class);
    history.bindText(new ReadOnlyBinding<String>() {

        @Override
        public String get() {
            StringBuilder messageList = new StringBuilder();
            for (Message message : console.getMessages()) {
                messageList.append(FontColor.getColored(message.getMessage(), message.getType().getColor()));
                if (message.hasNewLine()) {
                    messageList.append(Console.NEW_LINE);
                }
            }
            return messageList.toString();
        }
    });
}
Also used : SwipeMenuAnimationSystem(org.terasology.rendering.nui.animation.SwipeMenuAnimationSystem) Message(org.terasology.logic.console.Message) ScrollableArea(org.terasology.rendering.nui.layouts.ScrollableArea) UIText(org.terasology.rendering.nui.widgets.UIText) List(java.util.List)

Aggregations

UIText (org.terasology.rendering.nui.widgets.UIText)4 List (java.util.List)2 Module (org.terasology.module.Module)2 UILabel (org.terasology.rendering.nui.widgets.UILabel)2 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 Set (java.util.Set)1 StateLoading (org.terasology.engine.modes.StateLoading)1 GameManifest (org.terasology.game.GameManifest)1 Message (org.terasology.logic.console.Message)1 DependencyResolver (org.terasology.module.DependencyResolver)1 ResolutionResult (org.terasology.module.ResolutionResult)1 Name (org.terasology.naming.Name)1 Canvas (org.terasology.rendering.nui.Canvas)1 SwipeMenuAnimationSystem (org.terasology.rendering.nui.animation.SwipeMenuAnimationSystem)1 ReadOnlyBinding (org.terasology.rendering.nui.databinding.ReadOnlyBinding)1 ScrollableArea (org.terasology.rendering.nui.layouts.ScrollableArea)1 UIButton (org.terasology.rendering.nui.widgets.UIButton)1 FastRandom (org.terasology.utilities.random.FastRandom)1 WorldGeneratorInfo (org.terasology.world.generator.internal.WorldGeneratorInfo)1