Search in sources :

Example 1 with ParameterDescription

use of mcjty.rftoolscontrol.api.parameters.ParameterDescription in project RFToolsControl by McJty.

the class ProgramValidator method validate.

public static List<Pair<GridPos, String>> validate(ProgramCardInstance program) {
    List<Pair<GridPos, String>> errors = new ArrayList<>();
    Map<GridPos, GridInstance> grid = program.getGridInstances();
    // Find all unreachable instances:
    Set<GridPos> reachableLocations = new HashSet<>();
    for (Map.Entry<GridPos, GridInstance> entry : grid.entrySet()) {
        GridInstance g = entry.getValue();
        if (g.getPrimaryConnection() != null) {
            reachableLocations.add(g.getPrimaryConnection().offset(entry.getKey()));
        }
        if (g.getSecondaryConnection() != null) {
            reachableLocations.add(g.getSecondaryConnection().offset(entry.getKey()));
        }
    }
    for (Map.Entry<GridPos, GridInstance> entry : grid.entrySet()) {
        GridInstance g = entry.getValue();
        Opcode opcode = Opcodes.OPCODES.get(g.getId());
        GridPos p = entry.getKey();
        if (!opcode.isEvent() && !reachableLocations.contains(p)) {
            errors.add(Pair.of(p, "Unreachable: " + p.getX() + "," + p.getY()));
        }
    }
    // Find all missing required parameters:
    for (Map.Entry<GridPos, GridInstance> entry : grid.entrySet()) {
        GridPos p = entry.getKey();
        GridInstance g = entry.getValue();
        Opcode opcode = Opcodes.OPCODES.get(g.getId());
        List<ParameterDescription> descriptions = opcode.getParameters();
        List<Parameter> parameters = g.getParameters();
        for (int i = 0; i < descriptions.size(); i++) {
            ParameterDescription desc = descriptions.get(i);
            Parameter par = i < parameters.size() ? parameters.get(i) : null;
            if (!desc.isOptional()) {
                if (par == null || par.getParameterValue() == null || (par.getParameterValue().isConstant() && par.getParameterValue().getValue() == null)) {
                    errors.add(Pair.of(p, "Missing parameter (" + desc.getName() + "): " + p.getX() + "," + p.getY()));
                }
            }
        }
    }
    return errors;
}
Also used : Opcode(mcjty.rftoolscontrol.api.code.Opcode) GridInstance(mcjty.rftoolscontrol.logic.grid.GridInstance) GridPos(mcjty.rftoolscontrol.logic.grid.GridPos) Parameter(mcjty.rftoolscontrol.api.parameters.Parameter) ParameterDescription(mcjty.rftoolscontrol.api.parameters.ParameterDescription) Pair(org.apache.commons.lang3.tuple.Pair)

Example 2 with ParameterDescription

use of mcjty.rftoolscontrol.api.parameters.ParameterDescription in project RFToolsControl by McJty.

the class GuiProgrammer method makeGridInstance.

private ProgramCardInstance makeGridInstance(boolean selectionOnly) {
    ProgramCardInstance instance = ProgramCardInstance.newInstance();
    for (int x = 0; x < GRID_WIDTH; x++) {
        for (int y = 0; y < GRID_HEIGHT; y++) {
            IconHolder holder = getHolder(x, y);
            IIcon icon = holder.getIcon();
            if (icon != null) {
                if (selectionOnly && !icon.hasOverlay("S")) {
                    continue;
                }
                String operandId = icon.getID();
                GridInstance.Builder builder = GridInstance.builder(operandId);
                for (Connection connection : Connection.values()) {
                    if (icon.hasOverlay(connection.getId())) {
                        if (connection.isPrimary()) {
                            builder.primaryConnection(connection);
                        } else {
                            builder.secondaryConnection(connection);
                        }
                    }
                }
                Opcode opcode = Opcodes.OPCODES.get(operandId);
                Map<String, Object> data = icon.getData();
                for (ParameterDescription description : opcode.getParameters()) {
                    ParameterValue value = data == null ? null : (ParameterValue) data.get(description.getName());
                    if (value == null) {
                        value = ParameterValue.constant(null);
                    }
                    Parameter parameter = Parameter.builder().type(description.getType()).value(value).build();
                    builder.parameter(parameter);
                }
                instance.putGridInstance(x, y, builder.build());
            }
        }
    }
    return instance;
}
Also used : ParameterValue(mcjty.rftoolscontrol.api.parameters.ParameterValue) IIcon(mcjty.lib.gui.icons.IIcon) Connection(mcjty.rftoolscontrol.logic.Connection) Opcode(mcjty.rftoolscontrol.api.code.Opcode) ProgramCardInstance(mcjty.rftoolscontrol.logic.grid.ProgramCardInstance) GridInstance(mcjty.rftoolscontrol.logic.grid.GridInstance) Parameter(mcjty.rftoolscontrol.api.parameters.Parameter) ParameterDescription(mcjty.rftoolscontrol.api.parameters.ParameterDescription)

Example 3 with ParameterDescription

use of mcjty.rftoolscontrol.api.parameters.ParameterDescription in project RFToolsControl by McJty.

the class GuiProgrammer method getIconTooltipGrid.

private List<String> getIconTooltipGrid(int x, int y) {
    IconHolder holder = getHolder(x, y);
    IIcon icon = holder.getIcon();
    if (icon != null) {
        Opcode opcode = Opcodes.OPCODES.get(icon.getID());
        List<String> description = opcode.getDescription();
        List<String> tooltips = new ArrayList<>();
        if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT) || Keyboard.isKeyDown(Keyboard.KEY_RSHIFT)) {
            tooltips.add(description.get(0) + TextFormatting.WHITE + " [" + x + "," + y + "]");
            Map<String, Object> data = icon.getData() == null ? Collections.emptyMap() : icon.getData();
            for (ParameterDescription parameter : opcode.getParameters()) {
                String name = parameter.getName();
                ParameterValue value = (ParameterValue) data.get(name);
                if (value != null) {
                    tooltips.add(TextFormatting.BLUE + "Par " + name + ": " + ParameterTypeTools.stringRepresentation(parameter.getType(), value));
                } else {
                    tooltips.add(TextFormatting.BLUE + "Par " + name + ": NULL");
                }
            }
        } else {
            tooltips.add(description.get(0));
            tooltips.add("<Shift for more info>");
        }
        return tooltips;
    }
    return Collections.emptyList();
}
Also used : ParameterValue(mcjty.rftoolscontrol.api.parameters.ParameterValue) IIcon(mcjty.lib.gui.icons.IIcon) Opcode(mcjty.rftoolscontrol.api.code.Opcode) ParameterDescription(mcjty.rftoolscontrol.api.parameters.ParameterDescription)

Example 4 with ParameterDescription

use of mcjty.rftoolscontrol.api.parameters.ParameterDescription in project RFToolsControl by McJty.

the class GuiProgrammer method getIconTooltip.

private List<String> getIconTooltip(IIcon icon) {
    if (icon != null) {
        Opcode opcode = Opcodes.OPCODES.get(icon.getID());
        List<String> description = opcode.getDescription();
        List<String> tooltips = new ArrayList<>();
        if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT) || Keyboard.isKeyDown(Keyboard.KEY_RSHIFT)) {
            tooltips.addAll(description);
            for (ParameterDescription parameter : opcode.getParameters()) {
                boolean first = true;
                for (int i = 0; i < parameter.getDescription().size(); i++) {
                    String s = parameter.getDescription().get(i);
                    if (first) {
                        s = TextFormatting.BLUE + "Par '" + parameter.getName() + "': " + s;
                        first = false;
                    } else {
                        s = TextFormatting.BLUE + "      " + s;
                    }
                    if (parameter.isOptional() && i == parameter.getDescription().size() - 1) {
                        s += TextFormatting.GOLD + " [Optional]";
                    }
                    tooltips.add(s);
                }
            }
            tooltips.add(TextFormatting.YELLOW + "Result: " + opcode.getOutputDescription());
        } else {
            tooltips.add(description.get(0));
            tooltips.add("<Shift for more info>");
        }
        return tooltips;
    }
    return Collections.emptyList();
}
Also used : Opcode(mcjty.rftoolscontrol.api.code.Opcode) ParameterDescription(mcjty.rftoolscontrol.api.parameters.ParameterDescription)

Example 5 with ParameterDescription

use of mcjty.rftoolscontrol.api.parameters.ParameterDescription in project RFToolsControl by McJty.

the class GuiProgrammer method setEditorPanel.

private void setEditorPanel(IconHolder iconHolder, IIcon icon) {
    String id = icon.getID();
    Opcode opcode = Opcodes.OPCODES.get(id);
    Map<String, Object> data = icon.getData() == null ? Collections.emptyMap() : icon.getData();
    clearEditorPanel();
    for (ParameterDescription parameter : opcode.getParameters()) {
        String name = parameter.getName();
        ParameterValue value = (ParameterValue) data.get(name);
        Panel panel;
        if (value != null) {
            panel = createValuePanel(parameter, icon, iconHolder, ParameterTypeTools.stringRepresentation(parameter.getType(), value), opcode.isEvent());
        } else {
            panel = createValuePanel(parameter, icon, iconHolder, "", opcode.isEvent());
        }
        editorList.addChild(panel);
    }
}
Also used : Panel(mcjty.lib.gui.widgets.Panel) ParameterValue(mcjty.rftoolscontrol.api.parameters.ParameterValue) Opcode(mcjty.rftoolscontrol.api.code.Opcode) ParameterDescription(mcjty.rftoolscontrol.api.parameters.ParameterDescription)

Aggregations

Opcode (mcjty.rftoolscontrol.api.code.Opcode)9 ParameterDescription (mcjty.rftoolscontrol.api.parameters.ParameterDescription)9 Parameter (mcjty.rftoolscontrol.api.parameters.Parameter)6 ParameterValue (mcjty.rftoolscontrol.api.parameters.ParameterValue)4 GridInstance (mcjty.rftoolscontrol.logic.grid.GridInstance)4 IIcon (mcjty.lib.gui.icons.IIcon)3 GridPos (mcjty.rftoolscontrol.logic.grid.GridPos)3 Panel (mcjty.lib.gui.widgets.Panel)2 Connection (mcjty.rftoolscontrol.logic.Connection)2 ProgramCardInstance (mcjty.rftoolscontrol.logic.grid.ProgramCardInstance)2 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 java.awt (java.awt)1 Clipboard (java.awt.datatransfer.Clipboard)1 DataFlavor (java.awt.datatransfer.DataFlavor)1 StringSelection (java.awt.datatransfer.StringSelection)1 UnsupportedFlavorException (java.awt.datatransfer.UnsupportedFlavorException)1 IOException (java.io.IOException)1 java.util (java.util)1 List (java.util.List)1