Skip to content

Commit c7f87f7

Browse files
committed
Обновлён проект
1 parent db939ec commit c7f87f7

File tree

13 files changed

+140
-80
lines changed

13 files changed

+140
-80
lines changed

examples/canvas/animate_line.own

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,4 @@ def sethsbcolor(h1) {
5353
case 4: color(f*255, 0, 255)
5454
case 5: color(255, 0, 255-f*255)
5555
}
56-
}
56+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
use "canvasfx"
2+
use "math"
3+
use "functional"
4+
5+
// https://github.com/SeTSeR/KochSnowflake
6+
7+
width = 675 height = 500
8+
context = window("Koch Snowflake", width, height)
9+
10+
GO = 0 TURN = 1
11+
12+
def Fractal(startStep = 0) {
13+
result = {}
14+
result.fract = startStep ? [[GO, startStep]] : []
15+
result.next = def(fract) {
16+
fractal = Fractal()
17+
def translate(input) = input[0] == GO ? [input[0], input[1] / 3] : input
18+
fractlist = map(fract, ::translate)
19+
fractal.fract = fractlist
20+
fractal.fract ::= [TURN, -PI / 3]
21+
fractal.fract <<= fractlist
22+
fractal.fract ::= [TURN, 2*PI / 3]
23+
fractal.fract <<= fractlist
24+
fractal.fract ::= [TURN, -PI / 3]
25+
fractal.fract <<= fractlist
26+
return fractal
27+
}
28+
result.toDraw = def(fract) {
29+
res = Fractal()
30+
res.fract = fract
31+
res.fract ::= [TURN, 2*PI / 3]
32+
res.fract <<= fract
33+
res.fract ::= [TURN, 2*PI / 3]
34+
res.fract <<= fract
35+
return res
36+
}
37+
return result
38+
}
39+
40+
def draw(fractal) {
41+
x = 200
42+
y = height - 100 / sqrt(3)
43+
angle = -PI / 2
44+
context.setFill(Color.BLACK)
45+
context.fillRect(0, 0, width, height)
46+
context.setStroke(Color.GREEN)
47+
context.beginPath()
48+
context.moveTo(x, y)
49+
for action : fractal.fract {
50+
match action[0] {
51+
case GO: {
52+
x += action[1] * cos(angle)
53+
y += action[1] * sin(angle)
54+
context.lineTo(x, y)
55+
}
56+
case TURN: angle += action[1]
57+
}
58+
}
59+
context.closePath()
60+
context.stroke()
61+
}
62+
63+
fractal = Fractal(400.0)
64+
draw(fractal.toDraw(fractal.fract))
65+
addEventHandler(Events.KEY_PRESSED, def(e) {
66+
if (e.code == KeyCode.SPACE) {
67+
fractal = fractal.next(fractal.fract)
68+
draw(fractal.toDraw(fractal.fract))
69+
}
70+
})

src/main/java/com/annimon/ownlang/lib/ValueUtils.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.annimon.ownlang.lib;
22

3+
import com.annimon.ownlang.exceptions.TypeException;
34
import java.util.Iterator;
45
import java.util.Map;
56
import org.json.JSONArray;
@@ -99,4 +100,13 @@ public static byte[] toByteArray(ArrayValue array) {
99100
}
100101
return result;
101102
}
103+
104+
public static Function consumeFunction(Value value, int argumentNumber) throws TypeException {
105+
final int type = value.type();
106+
if (type != Types.FUNCTION) {
107+
throw new TypeException("Function expected at argument " + (argumentNumber + 1)
108+
+ ", but found " + Types.typeToString(type));
109+
}
110+
return ((FunctionValue) value).getValue();
111+
}
102112
}

src/main/java/com/annimon/ownlang/modules/forms/ComponentValue.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
11
package com.annimon.ownlang.modules.forms;
22

3-
import com.annimon.ownlang.exceptions.TypeException;
43
import com.annimon.ownlang.lib.Arguments;
54
import com.annimon.ownlang.lib.ArrayValue;
6-
import static com.annimon.ownlang.lib.Converters.*;
75
import com.annimon.ownlang.lib.Function;
86
import com.annimon.ownlang.lib.FunctionValue;
97
import com.annimon.ownlang.lib.MapValue;
108
import com.annimon.ownlang.lib.NumberValue;
119
import com.annimon.ownlang.lib.StringValue;
12-
import com.annimon.ownlang.lib.Types;
1310
import com.annimon.ownlang.lib.Value;
11+
import com.annimon.ownlang.lib.ValueUtils;
1412
import java.awt.Component;
1513
import java.awt.Dimension;
1614
import java.awt.Point;
1715
import java.awt.event.KeyEvent;
1816
import java.awt.event.KeyListener;
1917
import java.util.function.Consumer;
2018
import java.util.function.Supplier;
19+
import static com.annimon.ownlang.lib.Converters.booleanOptToVoid;
20+
import static com.annimon.ownlang.lib.Converters.stringToVoid;
21+
import static com.annimon.ownlang.lib.Converters.voidToBoolean;
22+
import static com.annimon.ownlang.lib.Converters.voidToInt;
23+
import static com.annimon.ownlang.lib.Converters.voidToString;
24+
import static com.annimon.ownlang.lib.Converters.voidToVoid;
2125

2226
public abstract class ComponentValue extends MapValue {
2327

@@ -75,11 +79,7 @@ private void init() {
7579

7680
private Value addKeyListener(Value... args) {
7781
Arguments.check(1, args.length);
78-
final int type = args[0].type();
79-
if (type != Types.FUNCTION) {
80-
throw new TypeException("Function expected, but found " + Types.typeToString(type));
81-
}
82-
final Function action = ((FunctionValue) args[0]).getValue();
82+
final Function action = ValueUtils.consumeFunction(args[0], 0);
8383
component.addKeyListener(new KeyListener() {
8484

8585
@Override

src/main/java/com/annimon/ownlang/modules/forms/JButtonValue.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
package com.annimon.ownlang.modules.forms;
22

3-
import com.annimon.ownlang.exceptions.TypeException;
43
import com.annimon.ownlang.lib.Arguments;
54
import com.annimon.ownlang.lib.Function;
65
import com.annimon.ownlang.lib.FunctionValue;
76
import com.annimon.ownlang.lib.NumberValue;
8-
import com.annimon.ownlang.lib.Types;
97
import com.annimon.ownlang.lib.Value;
8+
import com.annimon.ownlang.lib.ValueUtils;
109
import javax.swing.JButton;
1110

1211
public class JButtonValue extends JComponentValue {
@@ -26,11 +25,7 @@ private void init() {
2625

2726
private Value addActionListener(Value... args) {
2827
Arguments.check(1, args.length);
29-
final int type = args[0].type();
30-
if (type != Types.FUNCTION) {
31-
throw new TypeException("Function expected, but found " + Types.typeToString(type));
32-
}
33-
final Function action = ((FunctionValue) args[0]).getValue();
28+
final Function action = ValueUtils.consumeFunction(args[0], 0);
3429
button.addActionListener(e -> action.execute());
3530
return NumberValue.ZERO;
3631
}

src/main/java/com/annimon/ownlang/modules/forms/JTextFieldValue.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package com.annimon.ownlang.modules.forms;
22

3-
import com.annimon.ownlang.exceptions.TypeException;
43
import com.annimon.ownlang.lib.Arguments;
54
import static com.annimon.ownlang.lib.Converters.*;
65
import com.annimon.ownlang.lib.Function;
76
import com.annimon.ownlang.lib.FunctionValue;
87
import com.annimon.ownlang.lib.NumberValue;
9-
import com.annimon.ownlang.lib.Types;
108
import com.annimon.ownlang.lib.Value;
119
import javax.swing.JTextField;
10+
import static com.annimon.ownlang.lib.ValueUtils.consumeFunction;
1211

1312
public class JTextFieldValue extends JComponentValue {
1413

@@ -41,11 +40,7 @@ private void init() {
4140

4241
private Value addActionListener(Value... args) {
4342
Arguments.check(1, args.length);
44-
final int type = args[0].type();
45-
if (type != Types.FUNCTION) {
46-
throw new TypeException("Function expected, but found " + Types.typeToString(type));
47-
}
48-
final Function action = ((FunctionValue) args[0]).getValue();
43+
Function action = consumeFunction(args[0], 1);
4944
textField.addActionListener(e -> action.execute());
5045
return NumberValue.ZERO;
5146
}

src/main/java/com/annimon/ownlang/modules/functional/functional_flatmap.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
import com.annimon.ownlang.lib.Arguments;
55
import com.annimon.ownlang.lib.ArrayValue;
66
import com.annimon.ownlang.lib.Function;
7-
import com.annimon.ownlang.lib.FunctionValue;
87
import com.annimon.ownlang.lib.Types;
98
import com.annimon.ownlang.lib.Value;
9+
import com.annimon.ownlang.lib.ValueUtils;
1010
import java.util.ArrayList;
1111
import java.util.List;
1212

@@ -18,11 +18,7 @@ public Value execute(Value... args) {
1818
if (args[0].type() != Types.ARRAY) {
1919
throw new TypeException("Array expected in first argument");
2020
}
21-
if (args[1].type() != Types.FUNCTION) {
22-
throw new TypeException("Function expected in second argument");
23-
}
24-
25-
final Function mapper = ((FunctionValue) args[1]).getValue();
21+
final Function mapper = ValueUtils.consumeFunction(args[1], 1);
2622
return flatMapArray((ArrayValue) args[0], mapper);
2723
}
2824

src/main/java/com/annimon/ownlang/modules/functional/functional_foreach.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
package com.annimon.ownlang.modules.functional;
22

33
import com.annimon.ownlang.exceptions.TypeException;
4-
import com.annimon.ownlang.lib.*;
4+
import com.annimon.ownlang.lib.Arguments;
5+
import com.annimon.ownlang.lib.ArrayValue;
6+
import com.annimon.ownlang.lib.Function;
7+
import com.annimon.ownlang.lib.MapValue;
8+
import com.annimon.ownlang.lib.Types;
9+
import com.annimon.ownlang.lib.Value;
10+
import com.annimon.ownlang.lib.ValueUtils;
511
import java.util.Map;
612

713
public final class functional_foreach implements Function {
814

915
@Override
1016
public Value execute(Value... args) {
1117
Arguments.check(2, args.length);
12-
13-
if (args[1].type() != Types.FUNCTION) {
14-
throw new TypeException("Function expected in second arg");
15-
}
1618
final Value container = args[0];
17-
final Function consumer = ((FunctionValue) args[1]).getValue();
19+
final Function consumer = ValueUtils.consumeFunction(args[1], 1);
1820
if (container.type() == Types.ARRAY) {
1921
final ArrayValue array = (ArrayValue) container;
2022
for (Value element : array) {

src/main/java/com/annimon/ownlang/modules/functional/functional_sortby.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
import com.annimon.ownlang.lib.Arguments;
55
import com.annimon.ownlang.lib.ArrayValue;
66
import com.annimon.ownlang.lib.Function;
7-
import com.annimon.ownlang.lib.FunctionValue;
87
import com.annimon.ownlang.lib.Types;
98
import com.annimon.ownlang.lib.Value;
9+
import com.annimon.ownlang.lib.ValueUtils;
1010
import java.util.Arrays;
1111

1212
public final class functional_sortby implements Function {
@@ -17,12 +17,8 @@ public Value execute(Value... args) {
1717
if (args[0].type() != Types.ARRAY) {
1818
throw new TypeException("Array expected in first argument");
1919
}
20-
if (args[1].type() != Types.FUNCTION) {
21-
throw new TypeException("Function expected in second argument");
22-
}
23-
2420
final Value[] elements = ((ArrayValue) args[0]).getCopyElements();
25-
final Function function = ((FunctionValue) args[1]).getValue();
21+
final Function function = ValueUtils.consumeFunction(args[1], 1);
2622
Arrays.sort(elements, (o1, o2) -> function.execute(o1).compareTo(function.execute(o2)));
2723
return new ArrayValue(elements);
2824
}

src/main/java/com/annimon/ownlang/modules/robot/robot.java

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -42,41 +42,44 @@ public static void initConstants() {
4242
@Override
4343
public void init() {
4444
initConstants();
45-
initialize();
46-
47-
Functions.set("click", convertFunction(robot::click));
48-
Functions.set("delay", convertFunction(awtRobot::delay));
49-
Functions.set("setAutoDelay", convertFunction(awtRobot::setAutoDelay));
50-
Functions.set("keyPress", convertFunction(awtRobot::keyPress));
51-
Functions.set("keyRelease", convertFunction(awtRobot::keyRelease));
52-
Functions.set("mousePress", convertFunction(awtRobot::mousePress));
53-
Functions.set("mouseRelease", convertFunction(awtRobot::mouseRelease));
54-
Functions.set("mouseWheel", convertFunction(awtRobot::mouseWheel));
55-
Functions.set("mouseMove", (args) -> {
56-
Arguments.check(2, args.length);
57-
try {
58-
awtRobot.mouseMove(args[0].asInt(), args[1].asInt());
59-
} catch (IllegalArgumentException iae) { }
60-
return NumberValue.ZERO;
61-
});
62-
Functions.set("typeText", (args) -> {
63-
Arguments.check(1, args.length);
64-
try {
65-
typeText(args[0].asString());
66-
} catch (IllegalArgumentException iae) { }
67-
return NumberValue.ZERO;
68-
});
69-
Functions.set("toClipboard", new robot_toclipboard());
70-
Functions.set("fromClipboard", new robot_fromclipboard());
45+
boolean isRobotInitialized = initialize();
46+
if (isRobotInitialized) {
47+
Functions.set("click", convertFunction(robot::click));
48+
Functions.set("delay", convertFunction(awtRobot::delay));
49+
Functions.set("setAutoDelay", convertFunction(awtRobot::setAutoDelay));
50+
Functions.set("keyPress", convertFunction(awtRobot::keyPress));
51+
Functions.set("keyRelease", convertFunction(awtRobot::keyRelease));
52+
Functions.set("mousePress", convertFunction(awtRobot::mousePress));
53+
Functions.set("mouseRelease", convertFunction(awtRobot::mouseRelease));
54+
Functions.set("mouseWheel", convertFunction(awtRobot::mouseWheel));
55+
Functions.set("mouseMove", (args) -> {
56+
Arguments.check(2, args.length);
57+
try {
58+
awtRobot.mouseMove(args[0].asInt(), args[1].asInt());
59+
} catch (IllegalArgumentException iae) { }
60+
return NumberValue.ZERO;
61+
});
62+
Functions.set("typeText", (args) -> {
63+
Arguments.check(1, args.length);
64+
try {
65+
typeText(args[0].asString());
66+
} catch (IllegalArgumentException iae) { }
67+
return NumberValue.ZERO;
68+
});
69+
Functions.set("toClipboard", new robot_toclipboard());
70+
Functions.set("fromClipboard", new robot_fromclipboard());
71+
}
7172
Functions.set("execProcess", new robot_exec(robot_exec.Mode.EXEC));
7273
Functions.set("execProcessAndWait", new robot_exec(robot_exec.Mode.EXEC_AND_WAIT));
7374
}
7475

75-
private static void initialize() {
76+
private static boolean initialize() {
7677
try {
7778
awtRobot = new Robot();
79+
return true;
7880
} catch (AWTException awte) {
79-
throw new RuntimeException("Unable to create robot instance", awte);
81+
//throw new RuntimeException("Unable to create robot instance", awte);
82+
return false;
8083
}
8184
}
8285

0 commit comments

Comments
 (0)