Search in sources :

Example 1 with UIButton

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

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

the class ObjectLayoutBuilder method buildEditorLayout.

private void buildEditorLayout(Binding<T> binding, ColumnLayout layout, UILabel nameWidget) {
    // TODO: Translate
    if (NULL_LABEL.equals(nameWidget.getText())) {
        nameWidget.setText(MODIFY_LABEL);
    }
    UIButton setToNull = new UIButton();
    // TODO: Translate
    setToNull.setText("Set to null");
    setToNull.subscribe(widget -> binding.set(null));
    layout.addWidget(setToNull);
    fieldsWidgetBuilder.getFieldWidgets(binding).forEach(layout::addWidget);
}
Also used : UIButton(org.terasology.nui.widgets.UIButton)

Example 3 with UIButton

use of org.terasology.nui.widgets.UIButton 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 4 with UIButton

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

the class JoinGameScreen method bindCustomButtons.

private void bindCustomButtons() {
    UIList<?> customServerList = find("customServerList", UIList.class);
    ReadOnlyBinding<Boolean> localSelectedServerOnly = new ReadOnlyBinding<Boolean>() {

        @Override
        public Boolean get() {
            return customServerList.getSelection() != null;
        }
    };
    UIButton add = find("add", UIButton.class);
    if (add != null) {
        add.subscribe(button -> {
            AddServerPopup popup = getManager().pushScreen(AddServerPopup.ASSET_URI, AddServerPopup.class);
            // select the entry if added successfully
            popup.onSuccess(item -> {
                config.getNetwork().addServerInfo(item);
                visibleList.setSelection(item);
            });
        });
    }
    UIButton edit = find("edit", UIButton.class);
    if (edit != null) {
        edit.bindEnabled(localSelectedServerOnly);
        edit.subscribe(button -> {
            AddServerPopup popup = getManager().pushScreen(AddServerPopup.ASSET_URI, AddServerPopup.class);
            ServerInfo info = visibleList.getSelection();
            popup.setServerInfo(info);
            // editing invalidates the currently known info, so query it again
            popup.onSuccess(item -> extInfo.put(item, infoService.requestInfo(item.getAddress(), item.getPort())));
        });
    }
    UIButton removeButton = find("remove", UIButton.class);
    if (removeButton != null) {
        removeButton.bindEnabled(localSelectedServerOnly);
        removeButton.subscribe(button -> {
            ServerInfo info = visibleList.getSelection();
            if (info != null) {
                config.getNetwork().removeServerInfo(info);
                extInfo.remove(info);
                visibleList.setSelection(null);
            }
        });
    }
    UILabel downloadLabel = find("download", UILabel.class);
    if (downloadLabel != null) {
        downloadLabel.bindText(new ReadOnlyBinding<String>() {

            @Override
            public String get() {
                return translationSystem.translate(downloader.getStatus());
            }
        });
    }
}
Also used : UILabel(org.terasology.nui.widgets.UILabel) ReadOnlyBinding(org.terasology.nui.databinding.ReadOnlyBinding) ServerInfo(org.terasology.engine.config.ServerInfo) UIButton(org.terasology.nui.widgets.UIButton)

Example 5 with UIButton

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

the class InputSettingsScreen method addInputBindRow.

private void addInputBindRow(SimpleUri uri, RegisterBindButton bind, ColumnLayout layout) {
    BindsConfig bindConfig = bindsManager.getBindsConfig();
    List<Input> binds = bindConfig.getBinds(uri);
    UIButton primaryInputBind = makeInputBindButton(uri, bind, binds, PRIMARY_BIND_INDEX);
    UIButton secondaryInputBind = makeInputBindButton(uri, bind, binds, SECONDARY_BIND_INDEX);
    layout.addWidget(new RowLayout(new UILabel(translationSystem.translate(bind.description())), primaryInputBind, secondaryInputBind).setColumnRatios(0.4f).setHorizontalSpacing(horizontalSpacing));
}
Also used : UILabel(org.terasology.nui.widgets.UILabel) Input(org.terasology.input.Input) UIButton(org.terasology.nui.widgets.UIButton) RowLayout(org.terasology.nui.layouts.RowLayout) BindsConfig(org.terasology.engine.config.BindsConfig)

Aggregations

UIButton (org.terasology.nui.widgets.UIButton)16 UILabel (org.terasology.nui.widgets.UILabel)8 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3 ColumnLayout (org.terasology.nui.layouts.ColumnLayout)3 UIBox (org.terasology.nui.widgets.UIBox)3 UIText (org.terasology.nui.widgets.UIText)3 Defaults (com.google.common.base.Defaults)2 Component (java.awt.Component)2 File (java.io.File)2 Constructor (java.lang.reflect.Constructor)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Parameter (java.lang.reflect.Parameter)2 Arrays (java.util.Arrays)2 Optional (java.util.Optional)2 Future (java.util.concurrent.Future)2 JDialog (javax.swing.JDialog)2 JFileChooser (javax.swing.JFileChooser)2