Search in sources :

Example 1 with Binding

use of org.terasology.rendering.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.rendering.nui.databinding.Binding) Optional(java.util.Optional) AbstractWidget(org.terasology.rendering.nui.AbstractWidget)

Example 2 with Binding

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

the class BenchmarkScreen method initialise.

@Override
public void initialise() {
    textArea = find("textArea", UIText.class);
    dropdown = find("dropdown", UIDropdownScrollable.class);
    if (dropdown != null) {
        dropdown.bindSelection(new Binding() {

            @Override
            public Object get() {
                return selectedBenchmarkType;
            }

            @Override
            public void set(Object value) {
                selectedBenchmarkType = (BenchmarkType) value;
                if (runningBenchmark != null) {
                    runningBenchmark = null;
                }
                textArea.setText(selectedBenchmarkType.getDescription());
            }
        });
        dropdown.setOptions(Arrays.asList(BenchmarkType.values()));
    }
    closeButton = find("closeButton", UIButton.class);
    if (closeButton != null) {
        closeButton.subscribe(this::onCloseButton);
    }
    startStopButton = find("startStopButton", UIButton.class);
    if (startStopButton != null) {
        startStopButton.subscribe(this::onStartStopButton);
    }
}
Also used : Binding(org.terasology.rendering.nui.databinding.Binding) UIDropdownScrollable(org.terasology.rendering.nui.widgets.UIDropdownScrollable) UIButton(org.terasology.rendering.nui.widgets.UIButton) UIText(org.terasology.rendering.nui.widgets.UIText)

Aggregations

Binding (org.terasology.rendering.nui.databinding.Binding)2 Optional (java.util.Optional)1 AbstractWidget (org.terasology.rendering.nui.AbstractWidget)1 UIButton (org.terasology.rendering.nui.widgets.UIButton)1 UIDropdownScrollable (org.terasology.rendering.nui.widgets.UIDropdownScrollable)1 UIText (org.terasology.rendering.nui.widgets.UIText)1