Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support default value in macro prompt #2475

Merged
merged 6 commits into from
Mar 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions ugs-core/src/com/willwinder/universalgcodesender/MacroHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ This file is part of Universal Gcode Sender (UGS).
import com.willwinder.universalgcodesender.model.BackendAPI;
import com.willwinder.universalgcodesender.model.Position;
import com.willwinder.universalgcodesender.services.KeyboardService;
import com.willwinder.universalgcodesender.utils.GUIHelpers;
import net.miginfocom.swing.MigLayout;
import org.apache.commons.lang3.RegExUtils;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -32,13 +31,9 @@ This file is part of Universal Gcode Sender (UGS).
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicReference;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -58,7 +53,7 @@ public class MacroHelper {
private static final Pattern WORK_X = Pattern.compile("\\{work_x}");
private static final Pattern WORK_Y = Pattern.compile("\\{work_y}");
private static final Pattern WORK_Z = Pattern.compile("\\{work_z}");
private static final Pattern PROMPT_REGEX = Pattern.compile("\\{prompt\\|([^}]+)}");
private static final Pattern PROMPT_REGEX = Pattern.compile("\\{prompt\\|([^}|]+)\\|?([^}]+)?}");
private static final Pattern KEYPRESS_REGEX = Pattern.compile("\\{keypress\\|([^}]+)}");

/**
Expand Down Expand Up @@ -107,6 +102,8 @@ public static void executeCustomGcode(final String str, BackendAPI backend) thro
* {work_y} - The work Y location
* {work_z} - The work Z location
* {prompt|name} - Prompt the user for a value named 'name'.
* {prompt|name|default} - Prompt the user for a value named 'name' with the default value 'default'.
* {name} - Reuse the value of a previous prompt e.g. X{prompt|name} Y{name}.
* {keypress|keys} - Dispatch keyboard press events on the host system. Keys are defined using AWT format, see {@link KeyStroke#getKeyStroke(String)}
*
* @param str
Expand Down Expand Up @@ -157,18 +154,19 @@ private static String parseKeyPress(String command) {
private static String parsePrompts(String command) {
// Prompt for additional substitutions
Matcher m = PROMPT_REGEX.matcher(command);
List<String> prompts = new ArrayList<>();
List<Prompt> prompts = new ArrayList<>();

while (m.find()) {
prompts.add(m.group(1));
prompts.add(new Prompt(m.group(1), m.groupCount() > 1 ? m.group(2) : null));
}

if (prompts.size() > 0) {
List<JTextField> fields = new ArrayList<>();
JPanel myPanel = new JPanel();
myPanel.setLayout(new MigLayout("wrap 2, width 200"));
for (String s : prompts) {
JTextField field = new JTextField();
myPanel.add(new JLabel(s + ":"));
for (Prompt s : prompts) {
JTextField field = new JTextField(s.defaultValue);
myPanel.add(new JLabel(s.prompt + ":"));
myPanel.add(field, "growx, pushx");
fields.add(field);
}
Expand All @@ -180,8 +178,8 @@ private static String parsePrompts(String command) {

if (result == JOptionPane.OK_OPTION) {
for (int i = 0; i < prompts.size(); i++) {
command = command.replace("{prompt|" + prompts.get(i) + "}", fields.get(i).getText());
command = command.replace("{" + prompts.get(i) + "}", fields.get(i).getText()); // for reusing values
command = command.replace(prompts.get(i).toPlaceholder(), fields.get(i).getText());
command = command.replace(prompts.get(i).toValuePlaceholder(), fields.get(i).getText()); // for reusing values
}
} else {
command = "";
Expand Down
19 changes: 19 additions & 0 deletions ugs-core/src/com/willwinder/universalgcodesender/Prompt.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.willwinder.universalgcodesender;

public class Prompt {
public String prompt;
public String defaultValue;

public Prompt(String prompt, String defaultValue) {
this.prompt = prompt;
this.defaultValue = defaultValue;
}

public String toPlaceholder() {
return "{prompt|"+this.prompt+(this.defaultValue == null ? "" : "|"+this.defaultValue)+"}";
}

public String toValuePlaceholder() {
return "{"+this.prompt+"}";
}
}
2 changes: 1 addition & 1 deletion ugs-core/src/resources/MessagesBundle_en_US.properties
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ mainWindow.swing.softResetMachineControl = Soft Reset
mainWindow.swing.stepSizeLabel = XY Step size\:
mainWindow.swing.visualizeButton = Visualize
mainWindow.swing.workPositionLabel = Work Position\:
mainWindow.swing.macroInstructions = <html>Each box can contain a series of GCode commands seperated by ';'.<br />To execute the command, click the button to the left of the text.<br /><br />Interactive substitutions can be made with\: <br /> <ul><li>{machine_x} - The machine X location</li><li>{machine_y} - The machine Y location</li><li>{machine_z} - The machine Z location</li><li>{work_x} - The work X location</li><li>{work_y} - The work Y location</li><li>{work_z} - The work Z location</li><li>{prompt|name} - Prompt the user for a value named 'name'.</li></html>
mainWindow.swing.macroInstructions = <html>Each box can contain a series of GCode commands seperated by ';'.<br />To execute the command, click the button to the left of the text.<br /><br />Interactive substitutions can be made with\: <br /> <ul><li>{machine_x} - The machine X location</li><li>{machine_y} - The machine Y location</li><li>{machine_z} - The machine Z location</li><li>{work_x} - The work X location</li><li>{work_y} - The work Y location</li><li>{work_z} - The work Z location</li><li>{prompt|name} - Prompt the user for a value named 'name'. The value of a prompt<br /> can be re-used with a {name} placeholder e.g. X{prompt|pos} Y{pos} would resolve to X0 Y0.</li><li>{prompt|name|default} - Same as a normal prompt, except the third value will be included<br /> in the popup input by default. e.g. {prompt|pos|0}</li></html>
mainWindow.swing.inchRadioButton = Inches
mainWindow.swing.mmRadioButton = Millimeters
gcodeTable.command = Command
Expand Down
Loading