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