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);
}
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));
}
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());
}
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);
}
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));
}
}
Aggregations