Search in sources :

Example 6 with UILabel

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

the class SelectModulesScreen method initialise.

@Override
public void initialise() {
    setAnimationSystem(MenuAnimationSystems.createDefaultSwipeAnimation());
    remoteModuleRegistryUpdater = Executors.newSingleThreadExecutor().submit(moduleManager.getInstallManager().updateRemoteRegistry());
    selectModulesConfig = config.getSelectModulesConfig();
    resolver = new DependencyResolver(moduleManager.getRegistry());
    modulesLookup = Maps.newHashMap();
    sortedModules = Lists.newArrayList();
    for (Name moduleId : moduleManager.getRegistry().getModuleIds()) {
        Module latestVersion = moduleManager.getRegistry().getLatestModuleVersion(moduleId);
        if (!latestVersion.isOnClasspath()) {
            ModuleSelectionInfo info = ModuleSelectionInfo.local(latestVersion);
            modulesLookup.put(info.getMetadata().getId(), info);
            sortedModules.add(info);
        }
    }
    Collections.sort(sortedModules, moduleInfoComparator);
    allSortedModules = new ArrayList<>(sortedModules);
    final UIList<ModuleSelectionInfo> moduleList = find("moduleList", UIList.class);
    if (moduleList != null) {
        moduleList.setList(sortedModules);
        moduleList.setItemRenderer(new AbstractItemRenderer<ModuleSelectionInfo>() {

            public String getString(ModuleSelectionInfo value) {
                return value.getMetadata().getDisplayName().toString();
            }

            @Override
            public void draw(ModuleSelectionInfo value, Canvas canvas) {
                if (isSelectedGameplayModule(value) && value.isValidToSelect()) {
                    canvas.setMode("gameplay");
                } else if (value.isSelected() && value.isExplicitSelection()) {
                    canvas.setMode("enabled");
                } else if (value.isSelected()) {
                    canvas.setMode("dependency");
                } else if (!value.isPresent()) {
                    canvas.setMode("disabled");
                } else if (!value.isValidToSelect()) {
                    canvas.setMode("invalid");
                } else {
                    canvas.setMode("available");
                }
                canvas.drawText(getString(value), canvas.getRegion());
            }

            @Override
            public Vector2i getPreferredSize(ModuleSelectionInfo value, Canvas canvas) {
                String text = getString(value);
                return new Vector2i(canvas.getCurrentStyle().getFont().getWidth(text), canvas.getCurrentStyle().getFont().getLineHeight());
            }
        });
        // ItemActivateEventListener is triggered by double clicking
        moduleList.subscribe((widget, item) -> {
            if (item.isSelected() && moduleList.getSelection().isExplicitSelection()) {
                deselect(item);
            } else if (item.isValidToSelect()) {
                select(item);
            }
        });
        ResettableUIText moduleSearch = find("moduleSearch", ResettableUIText.class);
        if (moduleSearch != null) {
            moduleSearch.subscribe(new TextChangeEventListener() {

                @Override
                public void onTextChange(String oldText, String newText) {
                    filterText(newText);
                }
            });
        }
        final Binding<ModuleMetadata> moduleInfoBinding = new ReadOnlyBinding<ModuleMetadata>() {

            @Override
            public ModuleMetadata get() {
                if (moduleList.getSelection() != null) {
                    return moduleList.getSelection().getMetadata();
                }
                return null;
            }
        };
        UILabel name = find("name", UILabel.class);
        if (name != null) {
            name.bindText(new ReadOnlyBinding<String>() {

                @Override
                public String get() {
                    if (moduleInfoBinding.get() != null) {
                        return moduleInfoBinding.get().getDisplayName().toString();
                    }
                    return "";
                }
            });
        }
        UILabel installedVersion = find("installedVersion", UILabel.class);
        if (installedVersion != null) {
            installedVersion.bindText(new ReadOnlyBinding<String>() {

                @Override
                public String get() {
                    ModuleSelectionInfo sel = moduleList.getSelection();
                    if (sel == null) {
                        return "";
                    }
                    return sel.isPresent() ? sel.getMetadata().getVersion().toString() : translationSystem.translate("${engine:menu#module-version-installed-none}");
                }
            });
        }
        UILabel onlineVersion = find("onlineVersion", UILabel.class);
        if (onlineVersion != null) {
            onlineVersion.bindText(new ReadOnlyBinding<String>() {

                @Override
                public String get() {
                    ModuleSelectionInfo sel = moduleList.getSelection();
                    if (sel == null) {
                        return "";
                    }
                    return (sel.getOnlineVersion() != null) ? sel.getOnlineVersion().getVersion().toString() : "none";
                }
            });
        }
        UILabel description = find("description", UILabel.class);
        if (description != null) {
            description.bindText(new ReadOnlyBinding<String>() {

                @Override
                public String get() {
                    ModuleMetadata moduleMetadata = moduleInfoBinding.get();
                    if (moduleMetadata != null) {
                        String dependenciesNames;
                        List<DependencyInfo> dependencies = moduleMetadata.getDependencies();
                        if (dependencies != null && dependencies.size() > 0) {
                            dependenciesNames = translationSystem.translate("${engine:menu#module-dependencies-exist}") + ":" + '\n';
                            for (DependencyInfo dependency : dependencies) {
                                dependenciesNames += "   " + dependency.getId().toString() + '\n';
                            }
                        } else {
                            dependenciesNames = translationSystem.translate("${engine:menu#module-dependencies-empty}") + ".";
                        }
                        return moduleMetadata.getDescription().toString() + '\n' + '\n' + dependenciesNames;
                    }
                    return "";
                }
            });
        }
        UILabel status = find("status", UILabel.class);
        if (status != null) {
            status.bindText(new ReadOnlyBinding<String>() {

                @Override
                public String get() {
                    ModuleSelectionInfo info = moduleList.getSelection();
                    if (info != null) {
                        if (isSelectedGameplayModule(info)) {
                            return translationSystem.translate("${engine:menu#module-status-activegameplay}");
                        } else if (info.isSelected() && info.isExplicitSelection()) {
                            return translationSystem.translate("${engine:menu#module-status-activated}");
                        } else if (info.isSelected()) {
                            return translationSystem.translate("${engine:menu#module-status-dependency}");
                        } else if (!info.isPresent()) {
                            return translationSystem.translate("${engine:menu#module-status-notpresent}");
                        } else if (info.isValidToSelect()) {
                            return translationSystem.translate("${engine:menu#module-status-available}");
                        } else {
                            return translationSystem.translate("${engine:menu#module-status-error}");
                        }
                    }
                    return "";
                }
            });
        }
        UIButton toggleActivate = find("toggleActivation", UIButton.class);
        if (toggleActivate != null) {
            toggleActivate.subscribe(button -> {
                ModuleSelectionInfo info = moduleList.getSelection();
                if (info != null) {
                    // Toggle
                    if (info.isSelected() && info.isExplicitSelection()) {
                        deselect(info);
                    } else if (info.isValidToSelect()) {
                        select(info);
                    }
                }
            });
            toggleActivate.bindEnabled(new ReadOnlyBinding<Boolean>() {

                @Override
                public Boolean get() {
                    ModuleSelectionInfo info = moduleList.getSelection();
                    return info != null && info.isPresent() && !isSelectedGameplayModule(info) && (info.isSelected() || info.isValidToSelect());
                }
            });
            toggleActivate.bindText(new ReadOnlyBinding<String>() {

                @Override
                public String get() {
                    if (moduleList.getSelection() != null) {
                        if (moduleList.getSelection().isExplicitSelection()) {
                            return translationSystem.translate("${engine:menu#deactivate-module}");
                        } else {
                            return translationSystem.translate("${engine:menu#activate-module}");
                        }
                    }
                    // button should be disabled
                    return translationSystem.translate("${engine:menu#activate-module}");
                }
            });
        }
        UIButton downloadButton = find("download", UIButton.class);
        if (downloadButton != null) {
            downloadButton.subscribe(button -> {
                if (moduleList.getSelection() != null) {
                    ModuleSelectionInfo info = moduleList.getSelection();
                    startDownloadingNewestModulesRequiredFor(info);
                }
            });
            downloadButton.bindEnabled(new ReadOnlyBinding<Boolean>() {

                @Override
                public Boolean get() {
                    try {
                        return moduleList.getSelection().getOnlineVersion() != null;
                    } catch (NullPointerException e) {
                        return false;
                    }
                }
            });
            downloadButton.bindText(new ReadOnlyBinding<String>() {

                @Override
                public String get() {
                    ModuleSelectionInfo info = moduleList.getSelection();
                    if (info != null && !info.isPresent()) {
                        return translationSystem.translate("${engine:menu#download-module}");
                    } else {
                        return translationSystem.translate("${engine:menu#update-module}");
                    }
                }
            });
        }
        UIButton disableAll = find("disableAll", UIButton.class);
        if (disableAll != null) {
            disableAll.subscribe(button -> sortedModules.stream().filter(info -> info.isSelected() && info.isExplicitSelection()).forEach(this::deselect));
        }
        localOnlyCheckbox = find("localOnly", UICheckbox.class);
        localOnlyCheckbox.bindChecked(new Binding<Boolean>() {

            @Override
            public Boolean get() {
                filterText(moduleSearch.getText());
                prepareModuleList(selectModulesConfig.isChecked());
                return selectModulesConfig.isChecked();
            }

            @Override
            public void set(Boolean value) {
                selectModulesConfig.setIsChecked(value);
                filterText(moduleSearch.getText());
                prepareModuleList(value);
            }
        });
    }
    WidgetUtil.trySubscribe(this, "close", button -> triggerBackAnimation());
}
Also used : TextChangeEventListener(org.terasology.rendering.nui.widgets.TextChangeEventListener) Name(org.terasology.naming.Name) UIButton(org.terasology.rendering.nui.widgets.UIButton) List(java.util.List) ArrayList(java.util.ArrayList) UIList(org.terasology.rendering.nui.widgets.UIList) Vector2i(org.terasology.math.geom.Vector2i) UILabel(org.terasology.rendering.nui.widgets.UILabel) ReadOnlyBinding(org.terasology.rendering.nui.databinding.ReadOnlyBinding) Canvas(org.terasology.rendering.nui.Canvas) ModuleMetadata(org.terasology.module.ModuleMetadata) UICheckbox(org.terasology.rendering.nui.widgets.UICheckbox) DependencyInfo(org.terasology.module.DependencyInfo) DependencyResolver(org.terasology.module.DependencyResolver) ResettableUIText(org.terasology.rendering.nui.widgets.ResettableUIText) Module(org.terasology.module.Module)

Example 7 with UILabel

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

the class PropertyLayout method expand.

private void expand(Collection<Property<?, ?>> properties, MigLayout layout) {
    List<Property<?, ?>> props = Lists.newArrayList(properties);
    Collections.sort(props, propertyComparator);
    for (Property<?, ?> property : props) {
        UILabel label = property.getLabel();
        UIWidget editor = property.getEditor();
        editor.setTooltip(property.getDescription());
        layout.addWidget(label, new CCHint("newline"));
        layout.addWidget(editor, new CCHint());
    }
    invalidate();
}
Also used : UILabel(org.terasology.rendering.nui.widgets.UILabel) Property(org.terasology.rendering.nui.properties.Property) UIWidget(org.terasology.rendering.nui.UIWidget)

Example 8 with UILabel

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

the class TelemetryScreen method addTelemetryField.

/**
 * Add a new row in the menu, the new row includes the field name and value.
 * @param type the type(name) of the this field
 * @param value the value of this field
 * @param layout the layout where the new line will be added
 * @param isWithCheckbox whether add a check box in the line
 * @param telemetryCategory the TelemetryCategory that this field belongs to
 */
private void addTelemetryField(String type, Object value, ColumnLayout layout, boolean isWithCheckbox, TelemetryCategory telemetryCategory) {
    RowLayout newRow;
    if (isWithCheckbox) {
        String fieldName = telemetryCategory.id() + ":" + type;
        UICheckbox uiCheckbox = new UICheckbox(fieldName);
        Map<String, Boolean> bindingMap = config.getTelemetryConfig().getMetricsUserPermissionConfig().getBindingMap();
        if (!bindingMap.containsKey(fieldName)) {
            bindingMap.put(fieldName, config.getTelemetryConfig().isTelemetryEnabled());
        }
        Binding<Boolean> binding = getBindingFromBooleanMap(bindingMap, fieldName);
        uiCheckbox.bindChecked(binding);
        uiCheckbox.subscribe((checkbox) -> {
            if (bindingMap.get(fieldName)) {
                bindingMap.put(telemetryCategory.id(), true);
            } else {
                Set<Field> fields = ReflectionUtils.getFields(telemetryCategories.get(telemetryCategory), ReflectionUtils.withAnnotation(TelemetryField.class));
                boolean isOneEnabled = false;
                for (Field field : fields) {
                    isOneEnabled = isOneEnabled || bindingMap.get(telemetryCategory.id() + ":" + field.getName());
                }
                if (!isOneEnabled) {
                    bindingMap.put(telemetryCategory.id(), false);
                }
            }
        });
        newRow = new RowLayout(new UILabel(type), new UILabel(value.toString()), uiCheckbox).setColumnRatios(0.4f, 0.5f, 0.1f).setHorizontalSpacing(horizontalSpacing);
    } else {
        newRow = new RowLayout(new UILabel(type), new UILabel(value.toString())).setColumnRatios(0.4f, 0.5f).setHorizontalSpacing(horizontalSpacing);
    }
    layout.addWidget(newRow);
}
Also used : UILabel(org.terasology.rendering.nui.widgets.UILabel) Field(java.lang.reflect.Field) RowLayout(org.terasology.rendering.nui.layouts.RowLayout) UICheckbox(org.terasology.rendering.nui.widgets.UICheckbox)

Example 9 with UILabel

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

the class TelemetryScreen method addTelemetrySection.

/**
 * Add a new section with represents a new metrics type.
 * @param telemetryCategory the annotation of the new metric
 * @param layout the layout where the new section will be added
 * @param map the map which includes the telemetry field name and value
 */
private void addTelemetrySection(TelemetryCategory telemetryCategory, ColumnLayout layout, Map<String, ?> map) {
    UILabel categoryHeader = new UILabel(translationSystem.translate(telemetryCategory.displayName()));
    categoryHeader.setFamily("subheading");
    UICheckbox uiCheckbox = new UICheckbox(telemetryCategory.id());
    Map<String, Boolean> bindingMap = config.getTelemetryConfig().getMetricsUserPermissionConfig().getBindingMap();
    if (!bindingMap.containsKey(telemetryCategory.id())) {
        bindingMap.put(telemetryCategory.id(), config.getTelemetryConfig().isTelemetryEnabled());
    }
    Binding<Boolean> binding = getBindingFromBooleanMap(bindingMap, telemetryCategory.id());
    uiCheckbox.bindChecked(binding);
    RowLayout newRow = new RowLayout(categoryHeader, new UISpace(), uiCheckbox).setColumnRatios(0.4f, 0.5f, 0.1f).setHorizontalSpacing(horizontalSpacing);
    layout.addWidget(newRow);
    List<Map.Entry<String, ?>> telemetryFields = sortFields(map);
    for (Map.Entry entry : telemetryFields) {
        Object value = entry.getValue();
        if (value == null) {
            value = "Value unknown yet";
        }
        boolean isWithCheckbox = !telemetryCategory.isOneMapMetric();
        if (value instanceof List) {
            List list = (List) value;
            addTelemetryField(entry.getKey().toString(), list, layout, isWithCheckbox, telemetryCategory);
        } else {
            addTelemetryField(entry.getKey().toString(), value, layout, isWithCheckbox, telemetryCategory);
        }
    }
}
Also used : UILabel(org.terasology.rendering.nui.widgets.UILabel) UICheckbox(org.terasology.rendering.nui.widgets.UICheckbox) RowLayout(org.terasology.rendering.nui.layouts.RowLayout) ArrayList(java.util.ArrayList) List(java.util.List) UISpace(org.terasology.rendering.nui.widgets.UISpace) Map(java.util.Map)

Example 10 with UILabel

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

the class NUIEditorScreen method resetPreviewWidget.

/**
 * {@inheritDoc}
 */
@Override
public void resetPreviewWidget() {
    try {
        // Serialize the editor's contents and update the widget.
        JsonElement element = JsonTreeConverter.deserialize(getEditor().getRoot());
        UIWidget widget = new UIFormat().load(element, alternativeLocale).getRootWidget();
        selectedScreenBox.setContent(widget);
    } catch (Throwable t) {
        String truncatedStackTrace = Joiner.on(System.lineSeparator()).join(Arrays.copyOfRange(ExceptionUtils.getStackFrames(t), 0, 10));
        selectedScreenBox.setContent(new UILabel(truncatedStackTrace));
    }
}
Also used : UILabel(org.terasology.rendering.nui.widgets.UILabel) UIFormat(org.terasology.rendering.nui.asset.UIFormat) JsonElement(com.google.gson.JsonElement) UIWidget(org.terasology.rendering.nui.UIWidget)

Aggregations

UILabel (org.terasology.rendering.nui.widgets.UILabel)23 UIButton (org.terasology.rendering.nui.widgets.UIButton)6 RowLayout (org.terasology.rendering.nui.layouts.RowLayout)5 UICheckbox (org.terasology.rendering.nui.widgets.UICheckbox)5 Vector2i (org.terasology.math.geom.Vector2i)4 UIWidget (org.terasology.rendering.nui.UIWidget)4 ReadOnlyBinding (org.terasology.rendering.nui.databinding.ReadOnlyBinding)4 UISpace (org.terasology.rendering.nui.widgets.UISpace)4 List (java.util.List)3 DependencyResolver (org.terasology.module.DependencyResolver)3 Module (org.terasology.module.Module)3 Name (org.terasology.naming.Name)3 JsonElement (com.google.gson.JsonElement)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 ServerInfo (org.terasology.config.ServerInfo)2 SimpleUri (org.terasology.engine.SimpleUri)2 RegisterBindButton (org.terasology.input.RegisterBindButton)2 ResolutionResult (org.terasology.module.ResolutionResult)2 Canvas (org.terasology.rendering.nui.Canvas)2