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;
}
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;
}
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();
}
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;
}
}
}
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;
}
Aggregations