From df9db9c6b9506c85dfa7e9eae3846c68126040eb Mon Sep 17 00:00:00 2001 From: Max Gaukler Date: Wed, 1 May 2019 16:30:13 +0200 Subject: [PATCH] LaserScript: support prompt(title, defaultValue) to request user input --- .../Laser-Script/LaserScriptSpeedtest.ls | 145 ++++++++++++++++++ lib/LibLaserCut | 2 +- .../lssupport/LaserScriptImporter.java | 7 + .../lssupport/ScriptInterfaceLogUi.java | 11 ++ 4 files changed, 164 insertions(+), 1 deletion(-) create mode 100644 distribute/files/examples/Laser-Script/LaserScriptSpeedtest.ls diff --git a/distribute/files/examples/Laser-Script/LaserScriptSpeedtest.ls b/distribute/files/examples/Laser-Script/LaserScriptSpeedtest.ls new file mode 100644 index 000000000..af830f83a --- /dev/null +++ b/distribute/files/examples/Laser-Script/LaserScriptSpeedtest.ls @@ -0,0 +1,145 @@ +function rectangle(x, y, width, height) +{ + move(x, y); + line(x+width, y); + line(x+width, y+height); + line(x, y+height); + line(x, y); +} + +function segmentLine(x1, y1, x2, y2) +{ + move(x1, y1); + line(x2, y2); +} + +function segment7draw (x,y,w,h,values) +{ + var xw = x+w; + var yh = y+h; + var yh2 = y+h/2; + + if (values[0] === 1) //A + { + segmentLine(x, y, xw, y); + } + if (values[1] === 1) //B + { + segmentLine(xw, y, xw, yh2); + } + if (values[2] === 1) //C + { + segmentLine(xw, yh2, xw, yh); + } + if (values[3] === 1) //D + { + segmentLine(xw, yh, x, yh); + } + if (values[4] === 1) //E + { + segmentLine(x, yh, x, yh2); + } + if (values[6] === 1) //G + { + segmentLine(x, yh2, xw, yh2); + } + if (values[5] === 1) //F + { + segmentLine(x, yh2, x, y); + } +} + + +function drawPlus(x,y,w) { + move(x,y+w/2); + line(x+w,y+w/2); + move(x+w/2,y); + line(x+w/2,y+w); +} + +function segment7write (x,y,w,string) +{ + var characterMap = + { + '0': [1, 1, 1, 1, 1, 1, 0], + '1': [0, 1, 1, 0, 0, 0, 0], + '2': [1, 1, 0, 1, 1, 0, 1], + '3': [1, 1, 1, 1, 0, 0, 1], + '4': [0, 1, 1, 0, 0, 1, 1], + '5': [1, 0, 1, 1, 0, 1, 1], + '6': [1, 0, 1, 1, 1, 1, 1], + '7': [1, 1, 1, 0, 0, 0, 0], + '8': [1, 1, 1, 1, 1, 1, 1], + '9': [1, 1, 1, 1, 0, 1, 1], + + 'A': [1, 1, 1, 0, 1, 1, 1], + 'B': [0, 0, 1, 1, 1, 1, 1], + 'C': [1, 0, 0, 1, 1, 1, 0], + 'D': [0, 1, 1, 1, 1, 0, 1], + 'E': [1, 0, 0, 1, 1, 1, 1], + 'F': [1, 0, 0, 0, 1, 1, 1], + 'G': [1, 1, 1, 1, 0, 1, 1], + 'H': [0, 1, 1, 0, 1, 1, 1], + 'I': [0, 0, 0, 0, 1, 1, 0], + 'J': [0, 1, 1, 1, 1, 0, 0], + 'K': [0, 1, 1, 0, 1, 1, 1], + 'L': [0, 0, 0, 1, 1, 1, 0], + 'M': [1, 0, 1, 0, 1, 0, 0], + 'N': [0, 0, 1, 0, 1, 0, 1], + 'O': [1, 1, 1, 1, 1, 1, 0], + 'P': [1, 1, 0, 0, 1, 1, 1], + 'Q': [1, 1, 1, 0, 0, 1, 1], + 'R': [0, 0, 0, 0, 1, 0, 1], + 'S': [1, 0, 1, 1, 0, 1, 1], + 'T': [0, 0, 0, 1, 1, 1, 1], + 'U': [0, 1, 1, 1, 1, 1, 0], + 'V': [0, 0, 1, 1, 1, 0, 0], + 'W': [0, 1, 0, 1, 0, 1, 0], + 'X': [0, 1, 1, 0, 1, 1, 1], + 'Y': [0, 1, 1, 1, 0, 1, 1], + 'Z': [1, 1, 0, 1, 1, 0, 1], + ' ': [0, 0, 0, 0, 0, 0, 0], + '_': [0, 0, 0, 1, 0, 0, 0], + '-': [0, 0, 0, 0, 0, 0, 1], + ',': [0, 0, 1, 0, 0, 0, 0] + + }; + string=string.toUpperCase(); + for (var i = 0; i < string.length; i++) + { + segment7draw(x, y, w, 2*w, characterMap[string[i]]); + x += 1.5*w; + } +} + + +var rectWidth = 20; +set("power",100); +set("speed", 100); +segment7write (5, 5, 5, 'Power 100 - Speed'); +var speed = parseFloat(prompt("Maximum (fastest) speed to test", 100)); +var minSpeed = parseFloat(prompt("Minimum (slowest) speed to test", 1)); +var stepsize = parseFloat(prompt("Step size", 5)); +stepsize = Math.max(stepsize, 1); + +var x = 10; +var y = 30; + +while (speed >= minSpeed) { + // cut rectangle + set("speed", speed); + rectangle(x,y,rectWidth,rectWidth); + var textX=x; + var textY=y+rectWidth*1.25; + + // mark text + set("speed", 100); + segment7write(textX,textY,rectWidth/4,speed.toString()); + + x += rectWidth*1.1 + 5; + if (x > 250) { + x = 10; + y += rectWidth*2; + } + speed -= stepsize; +} diff --git a/lib/LibLaserCut b/lib/LibLaserCut index 61d3158ba..e26879b47 160000 --- a/lib/LibLaserCut +++ b/lib/LibLaserCut @@ -1 +1 @@ -Subproject commit 61d3158ba59a191413d9ad53a530b190b5ddd0d8 +Subproject commit e26879b4771a39edadd62c8adb34effc7aeb28fb diff --git a/src/com/t_oster/visicut/model/graphicelements/lssupport/LaserScriptImporter.java b/src/com/t_oster/visicut/model/graphicelements/lssupport/LaserScriptImporter.java index 03a7cfc3d..f43bde156 100644 --- a/src/com/t_oster/visicut/model/graphicelements/lssupport/LaserScriptImporter.java +++ b/src/com/t_oster/visicut/model/graphicelements/lssupport/LaserScriptImporter.java @@ -118,6 +118,13 @@ public void echo(String text) //intercepted by decorator anyway } + @Override + public String prompt(String title, String defaultValue) + { + //intercepted by decorator anyway + return null; + } + }), !PreferencesManager.getInstance().getPreferences().isDisableSandbox()); } catch (IOException e) diff --git a/src/com/t_oster/visicut/model/graphicelements/lssupport/ScriptInterfaceLogUi.java b/src/com/t_oster/visicut/model/graphicelements/lssupport/ScriptInterfaceLogUi.java index e7951a9bb..a5b43d9d9 100644 --- a/src/com/t_oster/visicut/model/graphicelements/lssupport/ScriptInterfaceLogUi.java +++ b/src/com/t_oster/visicut/model/graphicelements/lssupport/ScriptInterfaceLogUi.java @@ -21,6 +21,7 @@ import com.t_oster.liblasercut.laserscript.ScriptInterface; import com.t_oster.uicomponents.LogFrame; import javax.swing.JFrame; +import javax.swing.JOptionPane; /** * @@ -67,5 +68,15 @@ public Object get(String property) { return decoratee.get(property); } + + @Override + public String prompt(String title, String defaultValue) + { + String answer = (String) JOptionPane.showInputDialog(null, title, "LaserScript", JOptionPane.QUESTION_MESSAGE, null, null, defaultValue); + if (answer == null) { + answer = defaultValue; + } + return answer; + } }