Search in sources :

Example 1 with GridPos

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

the class GuiProgrammer method validateProgram.

private void validateProgram() {
    Panel panel = new Panel(mc, this).setLayout(new VerticalLayout()).setFilledBackground(0xff666666, 0xffaaaaaa).setFilledRectThickness(1);
    panel.setBounds(new Rectangle(60, 10, 200, 130));
    Window modalWindow = getWindowManager().createModalWindow(panel);
    WidgetList errorList = new WidgetList(mc, this);
    errorList.addSelectionEvent(new SelectionEvent() {

        @Override
        public void select(Widget parent, int index) {
        }

        @Override
        public void doubleClick(Widget parent, int index) {
            if (errorList.getSelected() != -1) {
                Widget<?> child = errorList.getChild(errorList.getSelected());
                GridPos pos = (GridPos) child.getUserObject();
                if (pos != null) {
                    window.setTextFocus(getHolder(pos.getX(), pos.getY()));
                }
            }
            getWindowManager().closeWindow(modalWindow);
        }
    });
    panel.addChild(errorList);
    panel.addChild(new Button(mc, this).addButtonEvent(w -> {
        getWindowManager().closeWindow(modalWindow);
    }).setText("Close"));
    ProgramCardInstance instance = makeGridInstance(false);
    List<Pair<GridPos, String>> errors = ProgramValidator.validate(instance);
    for (Pair<GridPos, String> entry : errors) {
        GridPos p = entry.getKey();
        errorList.addChild(new Label(mc, this).setColor(0xffff0000).setHorizontalAlignment(HorizontalAlignment.ALIGH_LEFT).setText(entry.getValue()).setUserObject(p));
    }
}
Also used : Window(mcjty.lib.gui.Window) Label(mcjty.lib.gui.widgets.Label) Panel(mcjty.lib.gui.widgets.Panel) ProgramCardInstance(mcjty.rftoolscontrol.logic.grid.ProgramCardInstance) Button(mcjty.lib.gui.widgets.Button) GridPos(mcjty.rftoolscontrol.logic.grid.GridPos) SelectionEvent(mcjty.lib.gui.events.SelectionEvent) VerticalLayout(mcjty.lib.gui.layout.VerticalLayout) Pair(org.apache.commons.lang3.tuple.Pair)

Example 2 with GridPos

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

the class GuiProgrammer method handleConnectorHighlight.

private void handleConnectorHighlight(int finalX, int finalY, IIcon iIcon, int dx, int dy) {
    if (prevHighlightConnector != null) {
        IconHolder h = getHolder(prevHighlightConnector.getX(), prevHighlightConnector.getY());
        if (h.getIcon() != null) {
            h.getIcon().removeOverlay("H");
        }
        prevHighlightConnector = null;
    }
    if (iIcon == null) {
        return;
    }
    iIcon.removeOverlay("H");
    Connection connection = getConnectionHandle(dx, dy);
    if (connection != null) {
        iIcon.addOverlay(HIGHLIGHT_ICONS.get(connection));
        prevHighlightConnector = new GridPos(finalX, finalY);
    }
}
Also used : GridPos(mcjty.rftoolscontrol.logic.grid.GridPos) Connection(mcjty.rftoolscontrol.logic.Connection)

Example 3 with GridPos

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

the class GuiProgrammer method mergeProgram.

private void mergeProgram(ProgramCardInstance instance, GridPos pos) {
    // Find the left-most/top-most icon in this program
    GridPos leftTop = new GridPos(10000, 10000);
    int posx;
    int posy;
    if (pos == null) {
        posx = 0;
        posy = 0;
        leftTop = new GridPos(0, 0);
    } else {
        posx = pos.getX();
        posy = pos.getY();
        for (Map.Entry<GridPos, GridInstance> entry : instance.getGridInstances().entrySet()) {
            int x = entry.getKey().getX();
            int y = entry.getKey().getY();
            if (x < leftTop.getX()) {
                leftTop = entry.getKey();
            } else if (x == leftTop.getX() && y < leftTop.getY()) {
                leftTop = entry.getKey();
            }
        }
        if (leftTop.getX() > 1000) {
            // Nothing to do
            return;
        }
    }
    // Check if the program fits in the grid
    for (Map.Entry<GridPos, GridInstance> entry : instance.getGridInstances().entrySet()) {
        int x = entry.getKey().getX() - leftTop.getX() + posx;
        int y = entry.getKey().getY() - leftTop.getY() + posy;
        if (!checkValidGridPos(new GridPos(x, y))) {
            GuiTools.showMessage(mc, this, getWindowManager(), 50, 50, TextFormatting.RED + "No room for clipboard here!");
            return;
        }
        if (getHolder(x, y).getIcon() != null) {
            GuiTools.showMessage(mc, this, getWindowManager(), 50, 50, TextFormatting.RED + "No room for clipboard here!");
            return;
        }
    }
    // There is room
    for (Map.Entry<GridPos, GridInstance> entry : instance.getGridInstances().entrySet()) {
        int x = entry.getKey().getX() - leftTop.getX() + posx;
        int y = entry.getKey().getY() - leftTop.getY() + posy;
        loadGridInstance(entry, x, y);
    }
}
Also used : GridInstance(mcjty.rftoolscontrol.logic.grid.GridInstance) GridPos(mcjty.rftoolscontrol.logic.grid.GridPos)

Example 4 with GridPos

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

the class GuiProgrammer method setupGridPanel.

private Panel setupGridPanel() {
    Panel panel = new Panel(mc, this).setLayout(new PositionalLayout()).setLayoutHint(new PositionalLayout.PositionalHint(5, 5, 246, 130));
    gridList = new WidgetList(mc, this).setLayoutHint(new PositionalLayout.PositionalHint(0, 0, 236, 130)).setPropagateEventsToChildren(true).setInvisibleSelection(true).setDrawHorizontalLines(false).setRowheight(ICONSIZE + 1);
    Slider slider = new Slider(mc, this).setVertical().setScrollable(gridList).setLayoutHint(new PositionalLayout.PositionalHint(237, 0, 9, 130));
    for (int y = 0; y < GRID_HEIGHT; y++) {
        Panel rowPanel = new Panel(mc, this).setLayout(new HorizontalLayout().setSpacing(-1).setHorizontalMargin(0).setVerticalMargin(0));
        for (int x = 0; x < GRID_WIDTH; x++) {
            int finalX = x;
            int finalY = y;
            IconHolder holder = new IconHolder(mc, this) {

                @Override
                public List<String> getTooltips() {
                    return getGridIconTooltips(finalX, finalY);
                }
            }.setDesiredWidth(ICONSIZE + 2).setDesiredHeight(ICONSIZE + 2).setBorder(1).setBorderColor(0xff777777).setSelectable(true).setUserObject(new GridPos(finalX, finalY)).addIconHoverEvent(((iconHolder, iIcon, dx, dy) -> {
                handleConnectorHighlight(finalX, finalY, iIcon, dx, dy);
            })).addIconLeavesEvent(((parent, icon) -> {
                iconLeavesFromX = finalX;
                iconLeavesFromY = finalY;
                return true;
            })).addIconArrivesEvent(((parent, icon) -> {
                if (icon != null && !loading) {
                    handleNewIconOverlay(icon, finalX, finalY);
                }
                return true;
            })).addIconClickedEvent((parent, icon, dx, dy) -> {
                gridIconClicked(icon, finalX, finalY, dx, dy);
                return true;
            });
            rowPanel.addChild(holder);
        }
        gridList.addChild(rowPanel);
    }
    panel.addChild(gridList).addChild(slider);
    return panel;
}
Also used : GenericGuiContainer(mcjty.lib.container.GenericGuiContainer) ProgramCardInstance(mcjty.rftoolscontrol.logic.grid.ProgramCardInstance) GeneralConfiguration(mcjty.rftoolscontrol.config.GeneralConfiguration) OpcodeCategory(mcjty.rftoolscontrol.api.code.OpcodeCategory) Opcode(mcjty.rftoolscontrol.api.code.Opcode) ParameterDescription(mcjty.rftoolscontrol.api.parameters.ParameterDescription) ParameterEditors(mcjty.rftoolscontrol.logic.editors.ParameterEditors) Keyboard(org.lwjgl.input.Keyboard) StringUtils(org.apache.commons.lang3.StringUtils) StyleConfig(mcjty.lib.base.StyleConfig) Panel(mcjty.lib.gui.widgets.Panel) Pair(org.apache.commons.lang3.tuple.Pair) PositionalLayout(mcjty.lib.gui.layout.PositionalLayout) ParameterEditor(mcjty.rftoolscontrol.logic.editors.ParameterEditor) Clipboard(java.awt.datatransfer.Clipboard) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) ParameterTypeTools(mcjty.rftoolscontrol.logic.registry.ParameterTypeTools) VerticalLayout(mcjty.lib.gui.layout.VerticalLayout) List(java.util.List) Button(mcjty.lib.gui.widgets.Button) IIcon(mcjty.lib.gui.icons.IIcon) Connection(mcjty.rftoolscontrol.logic.Connection) TextField(mcjty.lib.gui.widgets.TextField) java.util(java.util) mcjty.lib.gui.widgets(mcjty.lib.gui.widgets) ParameterValue(mcjty.rftoolscontrol.api.parameters.ParameterValue) DataFlavor(java.awt.datatransfer.DataFlavor) HorizontalAlignment(mcjty.lib.gui.layout.HorizontalAlignment) OpcodeOutput(mcjty.rftoolscontrol.api.code.OpcodeOutput) Label(mcjty.lib.gui.widgets.Label) Window(mcjty.lib.gui.Window) GridInstance(mcjty.rftoolscontrol.logic.grid.GridInstance) ItemStack(net.minecraft.item.ItemStack) HorizontalLayout(mcjty.lib.gui.layout.HorizontalLayout) ProgramValidator(mcjty.rftoolscontrol.logic.compiled.ProgramValidator) GuiTools(mcjty.rftoolscontrol.gui.GuiTools) Logging(mcjty.lib.varia.Logging) ProgramCardItem(mcjty.rftoolscontrol.items.ProgramCardItem) StringSelection(java.awt.datatransfer.StringSelection) RFToolsControl(mcjty.rftoolscontrol.RFToolsControl) Parameter(mcjty.rftoolscontrol.api.parameters.Parameter) RFToolsCtrlMessages(mcjty.rftoolscontrol.network.RFToolsCtrlMessages) GridPos(mcjty.rftoolscontrol.logic.grid.GridPos) SelectionEvent(mcjty.lib.gui.events.SelectionEvent) Opcodes(mcjty.rftoolscontrol.logic.registry.Opcodes) TextFormatting(net.minecraft.util.text.TextFormatting) IOException(java.io.IOException) WindowManager(mcjty.lib.gui.WindowManager) java.awt(java.awt) Slot(net.minecraft.inventory.Slot) ResourceLocation(net.minecraft.util.ResourceLocation) ImageIcon(mcjty.lib.gui.icons.ImageIcon) ClickType(net.minecraft.inventory.ClickType) Panel(mcjty.lib.gui.widgets.Panel) GridPos(mcjty.rftoolscontrol.logic.grid.GridPos) PositionalLayout(mcjty.lib.gui.layout.PositionalLayout) HorizontalLayout(mcjty.lib.gui.layout.HorizontalLayout)

Example 5 with GridPos

use of mcjty.rftoolscontrol.logic.grid.GridPos 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)

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