Search in sources :

Example 1 with ModuleRendering

use of org.terasology.engine.rendering.dag.ModuleRendering in project Terasology by MovingBlocks.

the class WorldRendererImpl method initRenderingModules.

private void initRenderingModules() {
    renderingModuleRegistry = context.get(RenderingModuleRegistry.class);
    // registry not populated by new ModuleRendering instances in UI, populate now
    if (renderingModuleRegistry.getOrderedRenderingModules().isEmpty()) {
        List<ModuleRendering> renderingModules = renderingModuleRegistry.updateRenderingModulesOrder(context.get(ModuleManager.class).getEnvironment(), context);
        if (renderingModules.isEmpty()) {
            GameEngine gameEngine = context.get(GameEngine.class);
            gameEngine.changeState(new StateMainMenu("No rendering module loaded, unable to render. Try enabling " + "CoreRendering."));
        }
    } else {
        // registry populated by new ModuleRendering instances in UI
        // Switch module's context from gamecreation subcontext to gamerunning context
        renderingModuleRegistry.updateModulesContext(context);
    }
    for (ModuleRendering moduleRenderingInstance : renderingModuleRegistry.getOrderedRenderingModules()) {
        if (moduleRenderingInstance.isEnabled()) {
            logger.info(String.format("\nInitialising rendering class %s from %s module.\n", moduleRenderingInstance.getClass().getSimpleName(), moduleRenderingInstance.getProvidingModule()));
            moduleRenderingInstance.initialise();
        }
    }
    requestTaskListRefresh();
}
Also used : ModuleRendering(org.terasology.engine.rendering.dag.ModuleRendering) StateMainMenu(org.terasology.engine.core.modes.StateMainMenu) GameEngine(org.terasology.engine.core.GameEngine) RenderingModuleRegistry(org.terasology.engine.core.module.rendering.RenderingModuleRegistry)

Example 2 with ModuleRendering

use of org.terasology.engine.rendering.dag.ModuleRendering in project Terasology by MovingBlocks.

the class RenderingModuleRegistry method fetchRenderingModules.

/**
 * Fetches all loaded {@link ModuleRendering} classes from the given {@link ModuleEnvironment}
 * and instantiatest them with current context.
 * @param moduleEnvironment
 * @param context
 * @return list of {@link ModuleRendering} instances
 */
private Set<ModuleRendering> fetchRenderingModules(ModuleEnvironment moduleEnvironment, Context context) {
    Set<ModuleRendering> moduleSet = new HashSet<>();
    for (Class<? extends ModuleRendering> renderingClass : moduleEnvironment.getSubtypesOf(ModuleRendering.class)) {
        ModuleRendering moduleRenderingInstance = getModuleRenderingByClass(renderingClass);
        if (moduleRenderingInstance == null) {
            try {
                Constructor<?> constructor = renderingClass.getConstructor(Context.class);
                moduleRenderingInstance = (ModuleRendering) constructor.newInstance(context);
            } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        if (moduleRenderingInstance != null) {
            moduleSet.add(moduleRenderingInstance);
        }
    }
    return moduleSet;
}
Also used : ModuleRendering(org.terasology.engine.rendering.dag.ModuleRendering) InvocationTargetException(java.lang.reflect.InvocationTargetException) HashSet(java.util.HashSet)

Example 3 with ModuleRendering

use of org.terasology.engine.rendering.dag.ModuleRendering in project Terasology by MovingBlocks.

the class RenderingModuleSettingScreen method postInit.

public void postInit() {
    moduleEnvironment = subContext.get(ModuleEnvironment.class);
    renderingModuleRegistry = context.get(RenderingModuleRegistry.class);
    orderedModuleRenderingInstances = renderingModuleRegistry.updateRenderingModulesOrder(moduleEnvironment, subContext);
    if (orderedModuleRenderingInstances.isEmpty()) {
        logger.error("No rendering module found!");
        GameEngine gameEngine = context.get(GameEngine.class);
        gameEngine.changeState(new StateMainMenu("No rendering module installed, unable to render. Try enabling CoreRendering."));
        return;
    }
    renderingModuleInfo = find("modulesInfo", UIText.class);
    recalculateOrder = find("update", UIButton.class);
    setEnabledRenderingClassButton = find("setEnabledRenderingClassButton", UIButton.class);
    // List<Name> orderedModuleNames = new ArrayList<>();
    // orderedModuleRenderingInstances.forEach(module->orderedModuleNames.add(module.getProvidingModule()));
    initPrioritySlider = find("moduleInitPrioritySlider", UISlider.class);
    if (initPrioritySlider != null) {
        initPrioritySlider.setValue(2f);
        initPrioritySlider.setUiSliderOnChangeTriggeredListener(this);
    }
    moduleList = find("moduleNameList", UIDropdownScrollable.class);
    if (moduleList != null) {
        moduleList.bindSelection(new Binding<ModuleRendering>() {

            ModuleRendering selected;

            @Override
            public ModuleRendering get() {
                return selected;
            }

            @Override
            public void set(ModuleRendering value) {
                if (initPrioritySlider != null) {
                    initPrioritySlider.setValue(value.getInitPriority());
                }
                selected = value;
            }
        });
        moduleList.setOptions(orderedModuleRenderingInstances);
        moduleList.setVisibleOptions(5);
        moduleList.setSelection(orderedModuleRenderingInstances.get(0));
        if (initPrioritySlider != null) {
            initPrioritySlider.setValue(moduleList.getSelection().getInitPriority());
        }
        moduleList.setOptionRenderer(new StringTextRenderer<ModuleRendering>() {

            @Override
            public String getString(ModuleRendering value) {
                if (value != null) {
                    StringBuilder stringBuilder = new StringBuilder().append(String.format("%s", value.getClass().getSimpleName()));
                    return stringBuilder.toString();
                }
                return "";
            }

            @Override
            public void draw(ModuleRendering value, Canvas canvas) {
                canvas.drawText(getString(value), canvas.getRegion());
            }
        });
    }
    if (recalculateOrder != null) {
        updateRenderingModuleInfo();
        recalculateOrder.subscribe(button -> {
            renderingModuleRegistry.updateRenderingModulesOrder(moduleEnvironment, subContext);
            orderedModuleRenderingInstances = renderingModuleRegistry.getOrderedRenderingModules();
            updateRenderingModuleInfo();
        });
    }
    if (setEnabledRenderingClassButton != null && moduleList != null) {
        setEnabledRenderingClassButton.bindText(new ReadOnlyBinding<String>() {

            @Override
            public String get() {
                return (moduleList.getSelection().isEnabled()) ? translationSystem.translate("${engine:menu#disable-rendering-class}") : translationSystem.translate("${engine:menu#enable-rendering-class}");
            }
        });
        setEnabledRenderingClassButton.subscribe(button -> {
            moduleList.getSelection().toggleEnabled();
            updateRenderingModuleInfo();
        });
    }
    // TODO returns one more screen every time...gradually
    WidgetUtil.trySubscribe(this, "return", widget -> getManager().pushScreen(StartPlayingScreen.ASSET_URI));
// Update slider if module selection changes
// if (initPrioritySlider != null && moduleList != null) {
// moduleList. (this, "moduleNameList", widget->initPrioritySlider.setValue(moduleList.getSelection().getInitPriority()));
// }
}
Also used : UISlider(org.terasology.nui.widgets.UISlider) UIDropdownScrollable(org.terasology.nui.widgets.UIDropdownScrollable) Canvas(org.terasology.nui.Canvas) GameEngine(org.terasology.engine.core.GameEngine) ModuleEnvironment(org.terasology.gestalt.module.ModuleEnvironment) StateMainMenu(org.terasology.engine.core.modes.StateMainMenu) ModuleRendering(org.terasology.engine.rendering.dag.ModuleRendering) UIButton(org.terasology.nui.widgets.UIButton) UIText(org.terasology.nui.widgets.UIText) RenderingModuleRegistry(org.terasology.engine.core.module.rendering.RenderingModuleRegistry)

Aggregations

ModuleRendering (org.terasology.engine.rendering.dag.ModuleRendering)3 GameEngine (org.terasology.engine.core.GameEngine)2 StateMainMenu (org.terasology.engine.core.modes.StateMainMenu)2 RenderingModuleRegistry (org.terasology.engine.core.module.rendering.RenderingModuleRegistry)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 HashSet (java.util.HashSet)1 ModuleEnvironment (org.terasology.gestalt.module.ModuleEnvironment)1 Canvas (org.terasology.nui.Canvas)1 UIButton (org.terasology.nui.widgets.UIButton)1 UIDropdownScrollable (org.terasology.nui.widgets.UIDropdownScrollable)1 UISlider (org.terasology.nui.widgets.UISlider)1 UIText (org.terasology.nui.widgets.UIText)1