All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gempukku.libgdx.graph.pipeline.PipelineLoaderCallback Maven / Gradle / Ivy

The newest version!
package com.gempukku.libgdx.graph.pipeline;

import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.GdxRuntimeException;
import com.badlogic.gdx.utils.JsonValue;
import com.badlogic.gdx.utils.ObjectMap;
import com.badlogic.gdx.utils.reflect.ReflectionException;
import com.gempukku.libgdx.graph.data.GraphConnection;
import com.gempukku.libgdx.graph.data.GraphNode;
import com.gempukku.libgdx.graph.data.GraphNodeInput;
import com.gempukku.libgdx.graph.data.GraphProperty;
import com.gempukku.libgdx.graph.data.GraphValidator;
import com.gempukku.libgdx.graph.data.NodeConfiguration;
import com.gempukku.libgdx.graph.loader.GraphDataLoaderCallback;
import com.gempukku.libgdx.graph.pipeline.impl.PipelineRendererImpl;
import com.gempukku.libgdx.graph.pipeline.impl.WritablePipelineProperty;
import com.gempukku.libgdx.graph.pipeline.producer.node.PipelineNode;
import com.gempukku.libgdx.graph.pipeline.producer.node.PipelineNodeProducer;
import com.gempukku.libgdx.graph.pipeline.producer.rendering.node.EndPipelineNode;
import com.gempukku.libgdx.graph.plugin.PluginRegistryImpl;
import com.gempukku.libgdx.graph.time.TimeProvider;

public class PipelineLoaderCallback extends GraphDataLoaderCallback {
    private TimeProvider timeProvider;
    private PluginRegistryImpl pluginRegistry;

    public PipelineLoaderCallback(TimeProvider timeProvider) {
        this.timeProvider = timeProvider;
    }

    @Override
    public void start() {
        try {
            pluginRegistry = PluginRegistryImpl.initializePlugins();
        } catch (ReflectionException e) {
            throw new GdxRuntimeException(e);
        }
    }

    @Override
    public PipelineRenderer end() {
        GraphValidator, GraphConnection, GraphProperty, PipelineFieldType> graphValidator = new GraphValidator<>();
        GraphValidator.ValidationResult, GraphConnection, GraphProperty, PipelineFieldType> result = graphValidator.validateGraph(this, "end");
        if (result.hasErrors())
            throw new IllegalStateException("The graph contains errors, open it in the graph designer and correct them");

        ObjectMap pipelineNodeMap = new ObjectMap<>();
        PipelineNode pipelineNode = populatePipelineNodes("end", pipelineNodeMap);

        ObjectMap propertyMap = new ObjectMap<>();
        for (GraphProperty property : getProperties()) {
            propertyMap.put(property.getName(), RendererPipelineConfiguration.findProperty(property.getType()).createProperty(property.getData()));
        }

        return new PipelineRendererImpl(pluginRegistry, timeProvider,
                pipelineNodeMap.values().toArray(), propertyMap, (EndPipelineNode) pipelineNode);
    }

    @Override
    protected PipelineFieldType getFieldType(String type) {
        return PipelineFieldType.valueOf(type);
    }

    @Override
    protected NodeConfiguration getNodeConfiguration(String type, JsonValue data) {
        return RendererPipelineConfiguration.findProducer(type).getConfiguration(data);
    }

    private PipelineNode populatePipelineNodes(String nodeId, ObjectMap pipelineNodeMap) {
        PipelineNode pipelineNode = pipelineNodeMap.get(nodeId);
        if (pipelineNode != null)
            return pipelineNode;

        GraphNode nodeInfo = getNodeById(nodeId);
        String nodeInfoType = nodeInfo.getConfiguration().getType();
        PipelineNodeProducer nodeProducer = RendererPipelineConfiguration.findProducer(nodeInfoType);
        if (nodeProducer == null)
            throw new IllegalStateException("Unable to find node producer for type: " + nodeInfoType);
        ObjectMap>> inputFields = new ObjectMap<>();
        for (GraphNodeInput nodeInput : new ObjectMap.Values<>(nodeProducer.getConfiguration(nodeInfo.getData()).getNodeInputs())) {
            String inputName = nodeInput.getFieldId();
            Array vertexInfos = findInputProducers(nodeId, inputName);
            if (vertexInfos.size == 0 && nodeInput.isRequired())
                throw new IllegalStateException("Required input not provided");

            Array fieldTypes = new Array<>();
            Array> fieldOutputs = new Array<>();
            for (GraphConnection vertexInfo : vertexInfos) {
                PipelineNode vertexNode = populatePipelineNodes(vertexInfo.getNodeFrom(), pipelineNodeMap);
                PipelineNode.FieldOutput fieldOutput = vertexNode.getFieldOutput(vertexInfo.getFieldFrom());
                PipelineFieldType fieldType = fieldOutput.getPropertyType();
                fieldTypes.add(fieldType);
                fieldOutputs.add(fieldOutput);
            }
            if (!nodeInput.acceptsInputTypes(fieldTypes))
                throw new IllegalStateException("Producer produces a field of value not compatible with consumer");
            inputFields.put(inputName, fieldOutputs);
        }
        pipelineNode = nodeProducer.createNode(nodeInfo.getData(), inputFields);
        pipelineNodeMap.put(nodeId, pipelineNode);
        return pipelineNode;
    }

    private Array findInputProducers(String nodeId, String nodeField) {
        Array result = new Array<>();
        for (GraphConnection vertex : getConnections()) {
            if (vertex.getNodeTo().equals(nodeId) && vertex.getFieldTo().equals(nodeField))
                result.add(vertex);
        }
        return result;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy