Search in sources :

Example 6 with GridPos

use of mcjty.rftoolscontrol.logic.grid.GridPos in project RFToolsControl by McJty.

the class GuiProgrammer method gridIconClicked.

private void gridIconClicked(IIcon icon, int x, int y, int dx, int dy) {
    if (Keyboard.isKeyDown(Keyboard.KEY_LCONTROL) || Keyboard.isKeyDown(Keyboard.KEY_RCONTROL)) {
        long time = System.currentTimeMillis();
        boolean doubleclick = false;
        if (prevTime != -1L && time - prevTime < 250L) {
            doubleclick = true;
        }
        prevTime = time;
        if (icon.hasOverlay("S")) {
            if (doubleclick) {
                // Reverse because first click also did something
                selectSequence(new GridPos(x, y), new HashSet<>(), true);
            } else {
                icon.removeOverlay("S");
            }
        } else {
            if (doubleclick) {
                // Reverse because first click also did something
                selectSequence(new GridPos(x, y), new HashSet<>(), false);
            } else {
                icon.addOverlay(selectionIcon);
            }
        }
        return;
    }
    clearSelection();
    long time = System.currentTimeMillis();
    boolean doubleclick = !GeneralConfiguration.doubleClickToChangeConnector;
    if (prevTime != -1L && time - prevTime < 250L) {
        doubleclick = true;
    }
    prevTime = time;
    if (doubleclick) {
        Connection connection = getConnectionHandle(dx, dy);
        if (connection != null) {
            handleIconOverlay(icon, connection);
        }
    }
}
Also used : GridPos(mcjty.rftoolscontrol.logic.grid.GridPos) Connection(mcjty.rftoolscontrol.logic.Connection)

Example 7 with GridPos

use of mcjty.rftoolscontrol.logic.grid.GridPos in project RFToolsControl by McJty.

the class GuiProgrammer method validateAndHilight.

private void validateAndHilight() {
    ProgramCardInstance instance = makeGridInstance(false);
    for (int x = 0; x < GRID_WIDTH; x++) {
        for (int y = 0; y < GRID_HEIGHT; y++) {
            IconHolder h = getHolder(x, y);
            if (h.getIcon() != null) {
                h.getIcon().removeOverlay("E1");
                h.getIcon().removeOverlay("E2");
            }
        }
    }
    long time = System.currentTimeMillis();
    List<Pair<GridPos, String>> errors = ProgramValidator.validate(instance);
    for (Pair<GridPos, String> entry : errors) {
        GridPos p = entry.getKey();
        IconHolder h = getHolder(p.getX(), p.getY());
        h.getIcon().addOverlay((time % 2000) < 1000 ? errorIcon1 : errorIcon2);
    }
}
Also used : ProgramCardInstance(mcjty.rftoolscontrol.logic.grid.ProgramCardInstance) GridPos(mcjty.rftoolscontrol.logic.grid.GridPos) Pair(org.apache.commons.lang3.tuple.Pair)

Example 8 with GridPos

use of mcjty.rftoolscontrol.logic.grid.GridPos in project RFToolsControl by McJty.

the class CompiledCard method compile.

public static CompiledCard compile(ProgramCardInstance cardInstance) {
    if (cardInstance == null) {
        return null;
    }
    CompiledCard card = new CompiledCard();
    Map<GridPos, GridInstance> gridInstances = cardInstance.getGridInstances();
    // First find the indices of all compiled grid instances
    Map<GridPos, Integer> posToIndex = new HashMap<>();
    for (Map.Entry<GridPos, GridInstance> entry : gridInstances.entrySet()) {
        GridPos location = entry.getKey();
        posToIndex.put(location, posToIndex.size());
    }
    // Index of a dummy stop opcode that we can use to go too in case there is no real connection
    int stopIdx = posToIndex.size();
    for (Map.Entry<GridPos, GridInstance> entry : gridInstances.entrySet()) {
        GridPos location = entry.getKey();
        GridInstance grid = entry.getValue();
        String id = grid.getId();
        Opcode opcode = Opcodes.OPCODES.get(id);
        GridPos primaryOutput = grid.getPrimaryConnection() != null ? grid.getPrimaryConnection().offset(location) : null;
        GridPos secondaryOutput = grid.getSecondaryConnection() != null ? grid.getSecondaryConnection().offset(location) : null;
        CompiledOpcode.Builder opcodeBuilder = CompiledOpcode.builder().opcode(opcode);
        opcodeBuilder.grid(location.getX(), location.getY());
        if (primaryOutput != null && posToIndex.containsKey(primaryOutput)) {
            opcodeBuilder.primaryIndex(posToIndex.get(primaryOutput));
        } else {
            opcodeBuilder.primaryIndex(stopIdx);
        }
        if (secondaryOutput != null && posToIndex.containsKey(secondaryOutput)) {
            opcodeBuilder.secondaryIndex(posToIndex.get(secondaryOutput));
        } else {
            opcodeBuilder.secondaryIndex(stopIdx);
        }
        List<ParameterDescription> parameters = opcode.getParameters();
        boolean single = false;
        for (int i = 0; i < grid.getParameters().size(); i++) {
            Parameter parameter = grid.getParameters().get(i);
            if (i < parameters.size() && "single".equals(parameters.get(i).getName())) {
                single = TypeConverters.convertToBool(parameter);
            }
            opcodeBuilder.parameter(parameter);
        }
        if (opcode.isEvent()) {
            card.events.putIfAbsent(opcode, new ArrayList<>());
            card.events.get(opcode).add(new CompiledEvent(card.opcodes.size(), single));
        }
        card.opcodes.add(opcodeBuilder.build());
    }
    card.opcodes.add(CompiledOpcode.builder().opcode(Opcodes.DO_STOP_OR_RESUME).build());
    for (Opcode opcode : Opcodes.OPCODES.values()) {
        if (!card.events.containsKey(opcode)) {
            card.events.put(opcode, Collections.emptyList());
        }
    }
    return card;
}
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)

Aggregations

GridPos (mcjty.rftoolscontrol.logic.grid.GridPos)8 GridInstance (mcjty.rftoolscontrol.logic.grid.GridInstance)4 Pair (org.apache.commons.lang3.tuple.Pair)4 Opcode (mcjty.rftoolscontrol.api.code.Opcode)3 Parameter (mcjty.rftoolscontrol.api.parameters.Parameter)3 ParameterDescription (mcjty.rftoolscontrol.api.parameters.ParameterDescription)3 Connection (mcjty.rftoolscontrol.logic.Connection)3 ProgramCardInstance (mcjty.rftoolscontrol.logic.grid.ProgramCardInstance)3 Window (mcjty.lib.gui.Window)2 SelectionEvent (mcjty.lib.gui.events.SelectionEvent)2 VerticalLayout (mcjty.lib.gui.layout.VerticalLayout)2 Button (mcjty.lib.gui.widgets.Button)2 Label (mcjty.lib.gui.widgets.Label)2 Panel (mcjty.lib.gui.widgets.Panel)2 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