Search in sources :

Example 1 with UIWidget

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

use of org.terasology.nui.UIWidget in project Terasology by MovingBlocks.

the class TypeWidgetTestScreen method newBinding.

protected <T> void newBinding(TypeInfo<T> type) {
    Binding<T> binding = new InteriorMutationNotifyingBinding<>(new NotifyingBinding<T>() {

        @Override
        protected void onSet() {
            dumpBindings();
        }
    }, this::dumpBindings);
    bindings.put(type, binding);
    UIWidget bindingWidget = typeWidgetLibrary.getWidget(binding, type).get();
    String bindingLabelText = typeInfoToString(type);
    mainContainer.addWidget(WidgetUtil.labelize(bindingWidget, bindingLabelText, TypeWidgetFactory.LABEL_WIDGET_ID));
}
Also used : InteriorMutationNotifyingBinding(org.terasology.nui.databinding.InteriorMutationNotifyingBinding) UIWidget(org.terasology.nui.UIWidget)

Example 3 with UIWidget

use of org.terasology.nui.UIWidget in project Terasology by MovingBlocks.

the class AutoConfigScreen method initialise.

@Override
public void initialise() {
    mainContainer = find("mainContainer", ColumnLayout.class);
    assert mainContainer != null;
    for (AutoConfig config : configManager.getLoadedConfigs()) {
        Binding<AutoConfig> configBinding = new DefaultBinding<>(config);
        Optional<UIWidget> widget = typeWidgetLibrary.getWidget(configBinding, AutoConfig.class);
        if (widget.isPresent()) {
            mainContainer.addWidget(widget.get());
        } else {
            logger.warn("Cannot create widget for config: {}", config.getId());
        }
    }
    WidgetUtil.trySubscribe(this, "close", button -> triggerBackAnimation());
}
Also used : DefaultBinding(org.terasology.nui.databinding.DefaultBinding) ColumnLayout(org.terasology.nui.layouts.ColumnLayout) AutoConfig(org.terasology.engine.config.flexible.AutoConfig) UIWidget(org.terasology.nui.UIWidget)

Example 4 with UIWidget

use of org.terasology.nui.UIWidget in project Terasology by MovingBlocks.

the class AssetBackedConstraintWidgetFactory method buildWidget.

@Override
protected Optional<UIWidget> buildWidget() {
    Optional<UIElement> uiElement = assetManager.getAsset(contentsUri, UIElement.class);
    if (!uiElement.isPresent()) {
        LOGGER.error("Can't find unique UI element '{}'", contentsUri);
        return Optional.empty();
    }
    uiElement = uiElement.get().createInstance();
    if (!uiElement.isPresent()) {
        LOGGER.error("Can't create copy of UI element '{}'", contentsUri);
        return Optional.empty();
    }
    UIWidget settingWidget = uiElement.get().getRootWidget();
    bindWidgetToSetting(settingWidget);
    return Optional.of(settingWidget);
}
Also used : UIElement(org.terasology.nui.asset.UIElement) UIWidget(org.terasology.nui.UIWidget)

Example 5 with UIWidget

use of org.terasology.nui.UIWidget 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.nui.widgets.UILabel) UIFormat(org.terasology.engine.rendering.nui.asset.UIFormat) JsonElement(com.google.gson.JsonElement) UIWidget(org.terasology.nui.UIWidget)

Aggregations

UIWidget (org.terasology.nui.UIWidget)14 UIElement (org.terasology.nui.asset.UIElement)4 UILabel (org.terasology.nui.widgets.UILabel)3 JsonTree (org.terasology.nui.widgets.treeView.JsonTree)3 JsonElement (com.google.gson.JsonElement)2 Optional (java.util.Optional)2 UIFormat (org.terasology.engine.rendering.nui.asset.UIFormat)2 DefaultBinding (org.terasology.nui.databinding.DefaultBinding)2 ColumnLayout (org.terasology.nui.layouts.ColumnLayout)2 UIButton (org.terasology.nui.widgets.UIButton)2 UIDropdownScrollable (org.terasology.nui.widgets.UIDropdownScrollable)2 Defaults (com.google.common.base.Defaults)1 JsonParser (com.google.gson.JsonParser)1 JsonReader (com.google.gson.stream.JsonReader)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStreamReader (java.io.InputStreamReader)1 Constructor (java.lang.reflect.Constructor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Parameter (java.lang.reflect.Parameter)1 ArrayList (java.util.ArrayList)1