Added a device listener
[kaka/cakelight.git] / src / kaka / cakelight / GuiTest.java
CommitLineData
100b82fe
TW
1package kaka.cakelight;
2
3import javafx.application.Application;
4import javafx.scene.Scene;
5import javafx.scene.canvas.Canvas;
6import javafx.scene.canvas.GraphicsContext;
7import javafx.scene.input.KeyCode;
8import javafx.scene.layout.Pane;
9import javafx.scene.paint.Color;
10import javafx.scene.paint.Paint;
11import javafx.stage.Stage;
12import org.opencv.core.Core;
13import org.opencv.core.Mat;
14
15import static kaka.cakelight.Main.log;
16
17public class GuiTest extends Application {
18 private static final int BLOCK = 45;
19 private static final int GUTTER = 3 * BLOCK;
20 private Canvas canvas;
21 private Configuration config;
22 private CakeLight cakelight;
23 private boolean paused;
24
25 public static void main(String[] args) {
26 System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
27 launch(args);
28 }
29
30 @Override
31 public void start(Stage stage) throws Exception {
32 config = Configuration.from("config.properties");
33 canvas = new Canvas(16 * BLOCK + 2 * GUTTER, 9 * BLOCK + 2 * GUTTER);
34
35 Pane root = new Pane();
36 root.getChildren().add(canvas);
37
38 Scene scene = new Scene(root);
39 scene.setOnKeyPressed(keyEvent -> {
40 if (keyEvent.getCode() == KeyCode.ESCAPE) {
41 stage.close();
42 cakelight.cleanup();
43 }
44 if (keyEvent.getCode() == KeyCode.SPACE) {
45 paused = !paused;
46 }
47 });
48
49 stage.setTitle("Cakelight");
50 stage.setScene(scene);
51 stage.setOnCloseRequest(windowEvent -> cakelight.cleanup());
52 stage.show();
53
54 setupCakeLight();
55 }
56
57 private void setupCakeLight() {
58 log("Running with config:\n" + config);
59 cakelight = new CakeLight(config);
60 VideoMode mode = new VideoMode();
61 cakelight.setMode(mode);
62 cakelight.startLoop();
63 mode.onFrame(frame -> drawFrame(canvas.getGraphicsContext2D(), frame));
64 }
65
66 private void drawFrame(GraphicsContext gc, Frame frame) {
67 if (paused) return;
68 System.out.println("Drawing a frame");
69 drawCols(gc, frame);
70 drawRows(gc, frame);
71// drawVideo(gc, frame);
72 drawBorderAndGrid(gc);
73 drawLEDs(gc, frame);
74 }
75
76 private void drawLEDs(GraphicsContext gc, Frame frame) {
77 int ledLength = GUTTER;
78 float colSize = 16f * BLOCK / config.leds.cols;
79 float rowSize = 9f * BLOCK / config.leds.rows;
80// DropShadow shadow = new DropShadow(BlurType.ONE_PASS_BOX, Color.RED, colSize * 2, colSize, 0, 0);
81 for (int x = 0; x < config.leds.cols; x++) {
82 gc.setFill(frame.getLedColor(ListPosition.TOP, x));
83 gc.fillRect(GUTTER + x * colSize, GUTTER - ledLength, colSize, ledLength);
84 gc.setFill(frame.getLedColor(ListPosition.BOTTOM, x));
85 gc.fillRect(GUTTER + x * colSize, GUTTER + 9 * BLOCK, colSize, ledLength);
86 }
87 for (int y = 0; y < config.leds.rows; y++) {
88 gc.setFill(frame.getLedColor(ListPosition.LEFT, y));
89 gc.fillRect(GUTTER - ledLength, GUTTER + y * rowSize, ledLength, rowSize);
90 gc.setFill(frame.getLedColor(ListPosition.RIGHT, y));
91 gc.fillRect(GUTTER + 16 * BLOCK, GUTTER + y * rowSize, ledLength, rowSize);
92 }
93 }
94
95 private void drawVideo(GraphicsContext gc, Frame frame) {
96 byte[] rgb = new byte[3];
97 Mat img = frame.getConvertedImage();
98 float colSize = 16 * BLOCK / (float)img.cols();
99 float rowSize = 9 * BLOCK / (float)img.rows();
100 for (int x = 0, cols = img.cols(); x < cols; x++) {
101 for (int y = 0, rows = img.rows(); y < rows; y++) {
102 img.get(y, x, rgb);
103 gc.setFill(Color.rgb(rgb[0] & 0xff, rgb[1] & 0xff, rgb[2] & 0xff, 0.5));
104 gc.fillRect(GUTTER + x * colSize, GUTTER + y * rowSize, colSize, rowSize);
105 }
106 }
107 }
108
109 private void drawCols(GraphicsContext gc, Frame frame) {
110 byte[] rgb = new byte[3];
111 for (int x = 0; x < config.leds.cols; x++) {
112 for (int y = 0; y < 9; y++) {
113 frame.getColImage().get(y, x, rgb);
114 drawColPixel(gc, x, y, rgb);
115 }
116 }
117 }
118
119 private void drawRows(GraphicsContext gc, Frame frame) {
120 byte[] rgb = new byte[3];
121 for (int y = 0; y < config.leds.rows; y++) {
122 for (int x = 0; x < 16; x++) {
123 frame.getRowImage().get(y, x, rgb);
124 drawRowPixel(gc, x, y, rgb);
125 }
126 }
127 }
128
129 private void drawColPixel(GraphicsContext gc, int x, int y, byte[] rgb) {
130 float ledSize = 16f * BLOCK / config.leds.cols;
131 gc.setFill(Color.rgb(rgb[0] & 0xff, rgb[1] & 0xff, rgb[2] & 0xff, 0.5));
132 gc.fillRect(GUTTER + x * ledSize, GUTTER + y * BLOCK, ledSize, BLOCK);
133 }
134
135 private void drawRowPixel(GraphicsContext gc, int x, int y, byte[] rgb) {
136 float ledSize = 9f * BLOCK / config.leds.rows;
137 gc.setFill(Color.rgb(rgb[0] & 0xff, rgb[1] & 0xff, rgb[2] & 0xff, 0.5));
138 gc.fillRect(GUTTER + x * BLOCK, GUTTER + y * ledSize, BLOCK, ledSize);
139 }
140
141 private void drawBorderAndGrid(GraphicsContext gc) {
142 gc.setStroke(Color.BLACK);
143 gc.setLineWidth(BLOCK * 0.1);
144 gc.strokeRect(GUTTER, GUTTER, 16 * BLOCK, 9 * BLOCK);
145 gc.setLineWidth(1);
146 for (int x = 1; x < 16; x++) {
147 gc.strokeLine(GUTTER + x * BLOCK, GUTTER, GUTTER + x * BLOCK, GUTTER + 9 * BLOCK);
148 }
149 for (int y = 1; y < 9; y++) {
150 gc.strokeLine(GUTTER, GUTTER + y * BLOCK, GUTTER + 16 * BLOCK, GUTTER + y * BLOCK);
151 }
152 }
153
154 private void drawPixel(GraphicsContext gc, int x, int y, Paint paint) {
155 gc.setFill(paint);
156 gc.fillRect(GUTTER + x * BLOCK, GUTTER + y * BLOCK, BLOCK, BLOCK);
157 }
158}