Skip to content

Commit 4a3d27d

Browse files
committed
examples: add buttons demo
1 parent 2eed5ec commit 4a3d27d

File tree

4 files changed

+102
-9
lines changed

4 files changed

+102
-9
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package jtamaro.example.interaction;
2+
3+
import jtamaro.data.Sequence;
4+
import jtamaro.graphic.Actionable;
5+
import jtamaro.graphic.Graphic;
6+
import jtamaro.interaction.KeyboardKey;
7+
8+
import static jtamaro.data.Sequences.of;
9+
import static jtamaro.example.Toolbelt.aboves;
10+
import static jtamaro.example.Toolbelt.besides;
11+
import static jtamaro.example.Toolbelt.hgap;
12+
import static jtamaro.example.Toolbelt.vgap;
13+
import static jtamaro.graphic.Colors.BLACK;
14+
import static jtamaro.graphic.Colors.WHITE;
15+
import static jtamaro.graphic.Fonts.SANS_SERIF;
16+
import static jtamaro.graphic.Graphics.above;
17+
import static jtamaro.graphic.Graphics.overlay;
18+
import static jtamaro.graphic.Graphics.rectangle;
19+
import static jtamaro.graphic.Graphics.text;
20+
import static jtamaro.io.IO.interact;
21+
22+
public class ButtonsDemo {
23+
24+
public record Button(String label) {
25+
26+
public Graphic render() {
27+
final Graphic graphic = overlay(
28+
text(label, SANS_SERIF, 10, WHITE),
29+
rectangle(200, 50, BLACK)
30+
);
31+
32+
return new Actionable<Model>(graphic)
33+
.withMousePressHandler(((model, coordinate, button) -> model.setChoice(label)))
34+
.asGraphic();
35+
}
36+
}
37+
38+
public record Model(
39+
String choice,
40+
String previousChoice,
41+
Sequence<Button> buttons
42+
) {
43+
44+
public static Model initialModel() {
45+
return new Model(
46+
"???",
47+
"???",
48+
of(
49+
new Button("DOG"),
50+
new Button("CAT")
51+
)
52+
);
53+
}
54+
55+
public Model setChoice(String newChoice) {
56+
return new Model(newChoice, this.choice, this.buttons);
57+
}
58+
59+
public Graphic render() {
60+
final Graphic header = aboves(of(
61+
text("Current choice: " + this.choice, SANS_SERIF, 30, BLACK),
62+
vgap(10),
63+
text("Previous choice: " + this.previousChoice, SANS_SERIF, 20, BLACK),
64+
vgap(20)
65+
));
66+
final Graphic buttons = besides(
67+
this.buttons
68+
.map(Button::render)
69+
.intersperse(hgap(20))
70+
);
71+
final Graphic foreground = above(header, buttons);
72+
final Graphic background = rectangle(500, 400, WHITE);
73+
return overlay(
74+
foreground,
75+
background
76+
);
77+
}
78+
}
79+
80+
public static Model handleKey(Model model, KeyboardKey key) {
81+
return key.keyChar() == ' '
82+
? Model.initialModel()
83+
: model;
84+
}
85+
86+
public static void main(String[] args) {
87+
interact(Model.initialModel())
88+
.withKeyPressHandler(ButtonsDemo::handleKey)
89+
.withRenderer(Model::render)
90+
.run();
91+
}
92+
}

example/src/main/java/jtamaro/example/interaction/ColorPickerDemo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ private static Model onKeyEvent(Model model, KeyboardKey key) {
171171

172172
private static Model onDrag(Model m, Coordinate coords, MouseButton button) {
173173
final double x = coords.x() / RADIUS;
174-
final double y = -coords.y() / RADIUS;
174+
final double y = coords.y() / RADIUS;
175175
final double angle = Math.toDegrees(Math.atan2(y, x));
176176
final double h = angle < 0 ? angle + 360.0 : angle;
177177
final double s = Math.hypot(x, y);

example/src/main/java/jtamaro/example/interaction/DrawingDemo.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,23 @@
1818
public final class DrawingDemo {
1919

2020
static Graphic renderDrawing(Drawing drawing) {
21-
CartesianWorld world = drawing.points().reduce(
21+
final CartesianWorld world = drawing.points().reduce(
2222
new CartesianWorld(),
2323
(point, w) -> w.place(point.x(), point.y(), ellipse(10, 10, BLUE))
2424
).place(0, 0, rectangle(400, 200, WHITE));
25+
2526
return new Actionable<Drawing>(world.asGraphic())
26-
.withMouseDragHandler((Drawing d, Coordinate c, MouseButton b) -> {
27-
System.out.println("mouse drag " + c);
28-
return d.addPoint(new Point(c.x(), c.y()));
29-
})
27+
.withMouseDragHandler((Drawing d, Coordinate c, MouseButton b) ->
28+
d.addPoint(new Point(c.x(), c.y())))
3029
.asGraphic();
3130
}
3231

33-
record Point(double x, double y) {}
32+
record Point(double x, double y) {
33+
34+
}
35+
3436
record Drawing(Sequence<Point> points) {
37+
3538
public Drawing addPoint(Point p) {
3639
return new Drawing(cons(p, points));
3740
}

example/src/main/java/jtamaro/example/interaction/TextFieldDemo.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import static jtamaro.graphic.Graphics.rotate;
2020
import static jtamaro.graphic.Graphics.text;
2121
import static jtamaro.io.IO.interact;
22-
import static jtamaro.io.IO.println;
2322

2423
public class TextFieldDemo {
2524

@@ -74,7 +73,6 @@ TextField moveCursor(int idx) {
7473

7574
// Event handling
7675
private static TextField tick(TextField textField) {
77-
println("TICK:" + textField);
7876
return textField.blinkCursor();
7977
}
8078

0 commit comments

Comments
 (0)