Search in sources :

Example 1 with Opcode

use of mcjty.rftoolscontrol.api.code.Opcode 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 Opcode

use of mcjty.rftoolscontrol.api.code.Opcode 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 Opcode

use of mcjty.rftoolscontrol.api.code.Opcode 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 Opcode

use of mcjty.rftoolscontrol.api.code.Opcode in project RFToolsControl by McJty.

the class GuiProgrammer method fillOpcodes.

private void fillOpcodes() {
    opcodeList.removeChildren();
    int x = 0;
    int y = 0;
    Panel childPanel = null;
    for (Opcode opcode : Opcodes.SORTED_OPCODES) {
        if (currentCategory != null) {
            if (!opcode.getCategories().contains(currentCategory)) {
                continue;
            }
        }
        String key = opcode.getId();
        if (childPanel == null) {
            childPanel = new Panel(mc, this).setLayout(new HorizontalLayout().setVerticalMargin(1).setSpacing(1).setHorizontalMargin(0)).setDesiredHeight(ICONSIZE + 1);
            opcodeList.addChild(childPanel);
        }
        IconHolder holder = new IconHolder(mc, this) {

            @Override
            public List<String> getTooltips() {
                return getIconTooltip(getIcon());
            }
        }.setDesiredWidth(ICONSIZE).setDesiredHeight(ICONSIZE).setMakeCopy(true);
        holder.setIcon(ICONS.get(key).clone());
        childPanel.addChild(holder);
        x++;
        if (x >= 3) {
            y++;
            x = 0;
            childPanel = null;
        }
    }
}
Also used : Panel(mcjty.lib.gui.widgets.Panel) Opcode(mcjty.rftoolscontrol.api.code.Opcode) List(java.util.List) HorizontalLayout(mcjty.lib.gui.layout.HorizontalLayout)

Example 5 with Opcode

use of mcjty.rftoolscontrol.api.code.Opcode in project RFToolsControl by McJty.

the class GuiProgrammer method loadGridInstance.

private void loadGridInstance(Map.Entry<GridPos, GridInstance> entry, int x, int y) {
    GridInstance gridInstance = entry.getValue();
    IIcon icon = ICONS.get(gridInstance.getId());
    if (icon == null) {
        // Ignore missing icon
        Logging.logError("Opcode with id '" + gridInstance.getId() + "' is missing!");
        return;
    }
    icon = icon.clone();
    if (gridInstance.getPrimaryConnection() != null) {
        icon.addOverlay(CONNECTION_ICONS.get(gridInstance.getPrimaryConnection()));
    }
    if (gridInstance.getSecondaryConnection() != null) {
        icon.addOverlay(CONNECTION_ICONS.get(gridInstance.getSecondaryConnection()));
    }
    Opcode opcode = Opcodes.OPCODES.get(icon.getID());
    List<Parameter> parameters = gridInstance.getParameters();
    for (int i = 0; i < parameters.size(); i++) {
        String name = opcode.getParameters().get(i).getName();
        icon.addData(name, parameters.get(i).getParameterValue());
    }
    loading = true;
    getHolder(x, y).setIcon(icon);
    loading = false;
}
Also used : GridInstance(mcjty.rftoolscontrol.logic.grid.GridInstance) IIcon(mcjty.lib.gui.icons.IIcon) Opcode(mcjty.rftoolscontrol.api.code.Opcode) Parameter(mcjty.rftoolscontrol.api.parameters.Parameter)

Aggregations

Opcode (mcjty.rftoolscontrol.api.code.Opcode)14 ParameterDescription (mcjty.rftoolscontrol.api.parameters.ParameterDescription)8 Parameter (mcjty.rftoolscontrol.api.parameters.Parameter)6 IIcon (mcjty.lib.gui.icons.IIcon)4 GridInstance (mcjty.rftoolscontrol.logic.grid.GridInstance)4 ParameterValue (mcjty.rftoolscontrol.api.parameters.ParameterValue)3 Panel (mcjty.lib.gui.widgets.Panel)2 Connection (mcjty.rftoolscontrol.logic.Connection)2 GridPos (mcjty.rftoolscontrol.logic.grid.GridPos)2 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 List (java.util.List)1 ImageIcon (mcjty.lib.gui.icons.ImageIcon)1 HorizontalLayout (mcjty.lib.gui.layout.HorizontalLayout)1 ProgramCardInstance (mcjty.rftoolscontrol.logic.grid.ProgramCardInstance)1 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)1 NBTTagList (net.minecraft.nbt.NBTTagList)1 ResourceLocation (net.minecraft.util.ResourceLocation)1 Pair (org.apache.commons.lang3.tuple.Pair)1