Search in sources :

Example 1 with ColumnLayout

use of org.terasology.nui.layouts.ColumnLayout in project Terasology by MovingBlocks.

the class ColumnLayoutTest method setup.

@BeforeEach
public void setup() {
    columnLayout = new ColumnLayout();
    itemAt1x1 = mock(UIWidget.class);
    itemAt2x1 = mock(UIWidget.class);
    itemAt3x1 = mock(UIWidget.class);
    itemAt1x2 = mock(UIWidget.class);
    itemAt2x2 = mock(UIWidget.class);
    itemAt3x2 = mock(UIWidget.class);
    canvas = mock(Canvas.class);
    // +-----------------------------------+  +---+  +-------+
    // |                                   |  |2x1|  |       |
    // |               1x1                 |  +---+  |  3x1  |
    // |                                   |         |       |
    // +-----------------------------------+         +-------+
    when(canvas.calculateRestrictedSize(eq(itemAt1x1), any(Vector2i.class))).thenReturn(new Vector2i(50, 10));
    when(canvas.calculateRestrictedSize(eq(itemAt2x1), any(Vector2i.class))).thenReturn(new Vector2i(5, 5));
    when(canvas.calculateRestrictedSize(eq(itemAt3x1), any(Vector2i.class))).thenReturn(new Vector2i(10, 10));
    // +--------------+  +---+  +--------------+
    // |              |  |2x2|  |              |
    // |     1x2      |  +---+  |      3x2     |
    // |              |         |              |
    // +--------------+         +--------------+
    when(canvas.calculateRestrictedSize(eq(itemAt1x2), any(Vector2i.class))).thenReturn(new Vector2i(20, 10));
    when(canvas.calculateRestrictedSize(eq(itemAt2x2), any(Vector2i.class))).thenReturn(new Vector2i(5, 5));
    when(canvas.calculateRestrictedSize(eq(itemAt3x2), any(Vector2i.class))).thenReturn(new Vector2i(20, 10));
    Vector2i availableSize = new Vector2i(CANVAS_WIDTH, CANVAS_HEIGHT);
    when(canvas.size()).thenReturn(availableSize);
    columnLayout.setColumns(3);
    columnLayout.addWidget(itemAt1x1);
    columnLayout.addWidget(itemAt2x1);
    columnLayout.addWidget(itemAt3x1);
    columnLayout.addWidget(itemAt1x2);
    columnLayout.addWidget(itemAt2x2);
    columnLayout.addWidget(itemAt3x2);
}
Also used : ColumnLayout(org.terasology.nui.layouts.ColumnLayout) Canvas(org.terasology.nui.Canvas) Vector2i(org.joml.Vector2i) UIWidget(org.terasology.nui.UIWidget) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with ColumnLayout

use of org.terasology.nui.layouts.ColumnLayout in project Terasology by MovingBlocks.

the class InputSettingsScreen method initialise.

@Override
public void initialise() {
    setAnimationSystem(MenuAnimationSystems.createDefaultSwipeAnimation());
    ColumnLayout mainLayout = find("main", ColumnLayout.class);
    UIButton azerty = find("azerty", UIButton.class);
    if (azerty != null) {
        azerty.subscribe(event -> {
            BindCommands.AZERTY.forEach(this::setPrimaryBind);
            bindsManager.registerBinds();
        });
    }
    UIButton dvorak = find("dvorak", UIButton.class);
    if (dvorak != null) {
        dvorak.subscribe(event -> {
            BindCommands.DVORAK.forEach(this::setPrimaryBind);
            bindsManager.registerBinds();
        });
    }
    UIButton neo = find("neo", UIButton.class);
    if (neo != null) {
        neo.subscribe(event -> {
            BindCommands.NEO.forEach(this::setPrimaryBind);
            bindsManager.registerBinds();
        });
    }
    UISlider mouseSensitivity = new UISlider("mouseSensitivity");
    mouseSensitivity.bindValue(BindHelper.bindBeanProperty("mouseSensitivity", inputDeviceConfiguration, Float.TYPE));
    mouseSensitivity.setIncrement(0.025f);
    mouseSensitivity.setPrecision(3);
    UICheckbox mouseInverted = new UICheckbox("mouseYAxisInverted");
    mouseInverted.bindChecked(BindHelper.bindBeanProperty("mouseYAxisInverted", inputDeviceConfiguration, Boolean.TYPE));
    if (mainLayout != null) {
        mainLayout.addWidget(new UILabel("mouseLabel", "subheading", translationSystem.translate("${engine:menu" + "#category-mouse}")));
        mainLayout.addWidget(new RowLayout(new UILabel(translationSystem.translate("${engine:menu#mouse" + "-sensitivity}") + ":"), mouseSensitivity).setColumnRatios(0.4f).setHorizontalSpacing(horizontalSpacing));
        mainLayout.addWidget(new RowLayout(new UILabel(translationSystem.translate("${engine:menu#invert-mouse}") + ":"), mouseInverted).setColumnRatios(0.4f).setHorizontalSpacing(horizontalSpacing));
    }
    Map<String, InputCategory> inputCategories = Maps.newHashMap();
    Map<SimpleUri, RegisterBindButton> inputsById = Maps.newHashMap();
    DependencyResolver resolver = new DependencyResolver(moduleManager.getRegistry());
    for (Name moduleId : moduleManager.getRegistry().getModuleIds()) {
        Module module = moduleManager.getRegistry().getLatestModuleVersion(moduleId);
        ResolutionResult result = resolver.resolve(moduleId);
        if (result.isSuccess()) {
            try (ModuleEnvironment environment = moduleManager.loadEnvironment(result.getModules(), false)) {
                for (Class<?> holdingType : environment.getTypesAnnotatedWith(InputCategory.class, new FromModule(environment, moduleId))) {
                    InputCategory inputCategory = holdingType.getAnnotation(InputCategory.class);
                    inputCategories.put(module.getId() + ":" + inputCategory.id(), inputCategory);
                }
                for (Class<?> bindEvent : environment.getTypesAnnotatedWith(RegisterBindButton.class, new FromModule(environment, moduleId))) {
                    if (BindButtonEvent.class.isAssignableFrom(bindEvent)) {
                        RegisterBindButton bindRegister = bindEvent.getAnnotation(RegisterBindButton.class);
                        inputsById.put(new SimpleUri(module.getId(), bindRegister.id()), bindRegister);
                    }
                }
            }
        }
    }
    if (mainLayout != null) {
        addInputSection(inputCategories.remove("engine:movement"), mainLayout, inputsById);
        addInputSection(inputCategories.remove("engine:interaction"), mainLayout, inputsById);
        addInputSection(inputCategories.remove("engine:inventory"), mainLayout, inputsById);
        addInputSection(inputCategories.remove("engine:general"), mainLayout, inputsById);
        for (InputCategory category : inputCategories.values()) {
            addInputSection(category, mainLayout, inputsById);
        }
        mainLayout.addWidget(new UISpace(new Vector2i(1, 16)));
        List<String> controllers = inputSystem.getControllerDevice().getControllers();
        for (String name : controllers) {
            ControllerInfo cfg = inputDeviceConfiguration.getController(name);
            addInputSection(mainLayout, name, cfg);
        }
    }
    WidgetUtil.trySubscribe(this, "reset", button -> {
        inputDeviceConfiguration.reset();
        bindsManager.getBindsConfig().setBinds(bindsManager.getDefaultBindsConfig());
    });
    WidgetUtil.trySubscribe(this, "back", button -> triggerBackAnimation());
}
Also used : UILabel(org.terasology.nui.widgets.UILabel) UISlider(org.terasology.nui.widgets.UISlider) RegisterBindButton(org.terasology.engine.input.RegisterBindButton) ResolutionResult(org.terasology.gestalt.module.dependencyresolution.ResolutionResult) SimpleUri(org.terasology.engine.core.SimpleUri) UICheckbox(org.terasology.nui.widgets.UICheckbox) DependencyResolver(org.terasology.gestalt.module.dependencyresolution.DependencyResolver) Name(org.terasology.gestalt.naming.Name) ModuleEnvironment(org.terasology.gestalt.module.ModuleEnvironment) ColumnLayout(org.terasology.nui.layouts.ColumnLayout) UIButton(org.terasology.nui.widgets.UIButton) RowLayout(org.terasology.nui.layouts.RowLayout) InputCategory(org.terasology.input.InputCategory) UISpace(org.terasology.nui.widgets.UISpace) Vector2i(org.joml.Vector2i) FromModule(org.terasology.gestalt.module.predicates.FromModule) Module(org.terasology.gestalt.module.Module) FromModule(org.terasology.gestalt.module.predicates.FromModule) ControllerInfo(org.terasology.engine.config.ControllerConfig.ControllerInfo)

Example 3 with ColumnLayout

use of org.terasology.nui.layouts.ColumnLayout in project Terasology by MovingBlocks.

the class TelemetryScreen method refreshContent.

private void refreshContent() {
    ColumnLayout mainLayout = new ColumnLayout();
    mainLayout.setHorizontalSpacing(8);
    mainLayout.setVerticalSpacing(8);
    fetchTelemetryCategoriesFromEngineOnlyEnvironment();
    for (Map.Entry<TelemetryCategory, Class> telemetryCategory : telemetryCategories.entrySet()) {
        Class metricClass = telemetryCategory.getValue();
        Optional<Metric> optional = metrics.getMetric(metricClass);
        if (optional.isPresent()) {
            Metric metric = optional.get();
            Map<String, ?> map = metric.createTelemetryFieldToValue();
            if (map != null) {
                addTelemetrySection(telemetryCategory.getKey(), mainLayout, map);
            }
        }
    }
    ScrollableArea area = find("area", ScrollableArea.class);
    if (area != null) {
        area.setContent(mainLayout);
    }
}
Also used : ScrollableArea(org.terasology.nui.layouts.ScrollableArea) ColumnLayout(org.terasology.nui.layouts.ColumnLayout) Metric(org.terasology.engine.telemetry.metrics.Metric) Map(java.util.Map)

Example 4 with ColumnLayout

use of org.terasology.nui.layouts.ColumnLayout 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 5 with ColumnLayout

use of org.terasology.nui.layouts.ColumnLayout 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)

Aggregations

ColumnLayout (org.terasology.nui.layouts.ColumnLayout)6 List (java.util.List)3 Optional (java.util.Optional)3 Collectors (java.util.stream.Collectors)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3 ReflectionUtil (org.terasology.engine.utilities.ReflectionUtil)3 UIWidget (org.terasology.nui.UIWidget)3 Binding (org.terasology.nui.databinding.Binding)3 NotifyingBinding (org.terasology.nui.databinding.NotifyingBinding)3 StringTextRenderer (org.terasology.nui.itemRendering.StringTextRenderer)3 UIButton (org.terasology.nui.widgets.UIButton)3 UIDropdownScrollable (org.terasology.nui.widgets.UIDropdownScrollable)3 UILabel (org.terasology.nui.widgets.UILabel)3 TypeWidgetLibrary (org.terasology.nui.widgets.types.TypeWidgetLibrary)3 ExpandableLayoutBuilder (org.terasology.nui.widgets.types.builtin.util.ExpandableLayoutBuilder)3 TypeInfo (org.terasology.reflection.TypeInfo)3 Defaults (com.google.common.base.Defaults)2 Constructor (java.lang.reflect.Constructor)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2