Search in sources :

Example 1 with Binding

use of org.terasology.nui.databinding.Binding 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 2 with Binding

use of org.terasology.nui.databinding.Binding in project Terasology by MovingBlocks.

the class NUIEditorMenuTreeBuilder method getFieldValue.

private Object getFieldValue(Field field, Class clazz) throws IllegalAccessException, InstantiationException {
    if (Enum.class.isAssignableFrom(field.getType())) {
        return field.getType().getEnumConstants()[0].toString();
    } else {
        Object value;
        if (Binding.class.isAssignableFrom(field.getType())) {
            if (AbstractWidget.class.isAssignableFrom(clazz) && Objects.equals(field.getName(), "family")) {
                // The default - and acceptable - value for the AbstractWidget.family binding is null, but a user
                // most likely wants to use a custom family. Therefore, the default is set to an empty string instead.
                value = "";
            } else {
                Binding binding = (Binding) field.get(newInstance(clazz));
                value = binding.get();
            }
        } else if (Optional.class.isAssignableFrom(field.getType())) {
            value = newInstance((Class) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0]);
        } else {
            value = field.get(newInstance(clazz));
        }
        if (value != null && value instanceof Boolean) {
            value = !(Boolean) value;
        }
        return (value != null || Binding.class.isAssignableFrom(field.getType())) ? value : newInstance(field.getType());
    }
}
Also used : Binding(org.terasology.nui.databinding.Binding) Optional(java.util.Optional) AbstractWidget(org.terasology.nui.AbstractWidget)

Example 3 with Binding

use of org.terasology.nui.databinding.Binding in project Terasology by MovingBlocks.

the class ObjectLayoutBuilder method populateConstructorParameters.

private void populateConstructorParameters(Binding<T> binding, ColumnLayout parameterLayout, UIButton createInstanceButton, Binding<Constructor<T>> selectedConstructor) {
    parameterLayout.removeAllWidgets();
    Parameter[] parameters = selectedConstructor.get().getParameters();
    List<TypeInfo<?>> parameterTypes = Arrays.stream(parameters).map(Parameter::getParameterizedType).map(parameterType -> ReflectionUtil.resolveType(type.getType(), parameterType)).map(TypeInfo::of).collect(Collectors.toList());
    List<Binding<?>> argumentBindings = parameterTypes.stream().map(parameterType -> new DefaultBinding<>(Defaults.defaultValue(parameterType.getRawType()))).collect(Collectors.toList());
    createInstanceButton.subscribe(widget -> {
        Object[] arguments = argumentBindings.stream().map(Binding::get).toArray();
        try {
            binding.set(selectedConstructor.get().newInstance(arguments));
        } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    });
    if (argumentBindings.isEmpty()) {
        // TODO: Translate
        parameterLayout.addWidget(new UILabel("Constructor has no parameters"));
        return;
    }
    ColumnLayout parametersExpandableLayout = WidgetUtil.createExpandableLayout(// TODO: Translate
    "Constructor Parameters", this::createDefaultLayout, layout -> {
        for (int i = 0; i < parameterTypes.size(); i++) {
            TypeInfo<?> parameterType = parameterTypes.get(i);
            Binding<?> argumentBinding = argumentBindings.get(i);
            Parameter parameter = parameters[i];
            Optional<UIWidget> optionalWidget = library.getBaseTypeWidget((Binding) argumentBinding, parameterType);
            if (!optionalWidget.isPresent()) {
                LOGGER.warn("Could not create widget for parameter of type {} of constructor {}", parameter, selectedConstructor.get());
                continue;
            }
            UIWidget widget = optionalWidget.get();
            String parameterLabelText = ReflectionUtil.typeToString(parameterType.getType(), true);
            layout.addWidget(WidgetUtil.labelize(widget, parameterLabelText, LABEL_WIDGET_ID));
        }
    }, this::createDefaultLayout);
    parameterLayout.addWidget(parametersExpandableLayout);
}
Also used : NotifyingBinding(org.terasology.nui.databinding.NotifyingBinding) DefaultBinding(org.terasology.nui.databinding.DefaultBinding) Binding(org.terasology.nui.databinding.Binding) 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) UILabel(org.terasology.nui.widgets.UILabel) DefaultBinding(org.terasology.nui.databinding.DefaultBinding) TypeInfo(org.terasology.reflection.TypeInfo) InvocationTargetException(java.lang.reflect.InvocationTargetException) UIWidget(org.terasology.nui.UIWidget) ColumnLayout(org.terasology.nui.layouts.ColumnLayout) Parameter(java.lang.reflect.Parameter)

Example 4 with Binding

use of org.terasology.nui.databinding.Binding 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 5 with Binding

use of org.terasology.nui.databinding.Binding 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

Binding (org.terasology.nui.databinding.Binding)6 List (java.util.List)4 Optional (java.util.Optional)4 Collectors (java.util.stream.Collectors)4 Logger (org.slf4j.Logger)4 LoggerFactory (org.slf4j.LoggerFactory)4 UIDropdownScrollable (org.terasology.nui.widgets.UIDropdownScrollable)3 Defaults (com.google.common.base.Defaults)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 Constructor (java.lang.reflect.Constructor)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Parameter (java.lang.reflect.Parameter)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Set (java.util.Set)2 ModuleManager (org.terasology.engine.core.module.ModuleManager)2 ReflectionUtil (org.terasology.engine.utilities.ReflectionUtil)2 Module (org.terasology.gestalt.module.Module)2 Name (org.terasology.gestalt.naming.Name)2 UIWidget (org.terasology.nui.UIWidget)2