b73d37fb33305408faad3dc05e0d2aabab5bf5f2
[kaka/cakelight.git] / src / kaka / cakelight / GuiTest.java
1 package kaka.cakelight;
2
3 import javafx.application.Application;
4 import javafx.scene.Scene;
5 import javafx.scene.canvas.Canvas;
6 import javafx.scene.canvas.GraphicsContext;
7 import javafx.scene.input.KeyCode;
8 import javafx.scene.layout.Pane;
9 import javafx.scene.paint.Color;
10 import javafx.scene.paint.Paint;
11 import javafx.stage.Stage;
12 import org.opencv.core.Core;
13 import org.opencv.core.Mat;
14
15 import static kaka.cakelight.Main.log;
16
17 public 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.onVideoFrame(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     }
74
75     private void drawLEDs(GraphicsContext gc, LedFrame frame) {
76         int ledLength = GUTTER;
77         float colSize = 16f * BLOCK / config.leds.cols;
78         float rowSize = 9f * BLOCK / config.leds.rows;
79 //        DropShadow shadow = new DropShadow(BlurType.ONE_PASS_BOX, Color.RED, colSize * 2, colSize, 0, 0);
80         for (int x = 0; x < config.leds.cols; x++) {
81             gc.setFill(frame.getLedColor(x + config.leds.rows));
82             gc.fillRect(GUTTER + x * colSize, GUTTER - ledLength, colSize, ledLength);
83             gc.setFill(frame.getLedColor(config.leds.rows * 2 + config.leds.cols * 2 - 1 - x));
84             gc.fillRect(GUTTER + x * colSize, GUTTER + 9 * BLOCK, colSize, ledLength);
85         }
86         for (int y = 0; y < config.leds.rows; y++) {
87             gc.setFill(frame.getLedColor(config.leds.rows - 1 - y));
88             gc.fillRect(GUTTER - ledLength, GUTTER + y * rowSize, ledLength, rowSize);
89             gc.setFill(frame.getLedColor(y + config.leds.rows + config.leds.cols));
90             gc.fillRect(GUTTER + 16 * BLOCK, GUTTER + y * rowSize, ledLength, rowSize);
91         }
92     }
93
94     private void drawVideo(GraphicsContext gc, Frame frame) {
95         byte[] rgb = new byte[3];
96         Mat img = frame.getConvertedImage();
97         float colSize = 16 * BLOCK / (float)img.cols();
98         float rowSize = 9 * BLOCK / (float)img.rows();
99         for (int x = 0, cols = img.cols(); x < cols; x++) {
100             for (int y = 0, rows = img.rows(); y < rows; y++) {
101                 img.get(y, x, rgb);
102                 gc.setFill(Color.rgb(rgb[0] & 0xff, rgb[1] & 0xff, rgb[2] & 0xff, 0.5));
103                 gc.fillRect(GUTTER + x * colSize, GUTTER + y * rowSize, colSize, rowSize);
104             }
105         }
106     }
107
108     private void drawCols(GraphicsContext gc, Frame frame) {
109         byte[] rgb = new byte[3];
110         for (int x = 0; x < config.leds.cols; x++) {
111             for (int y = 0; y < 9; y++) {
112                 frame.getColImage().get(y, x, rgb);
113                 drawColPixel(gc, x, y, rgb);
114             }
115         }
116     }
117
118     private void drawRows(GraphicsContext gc, Frame frame) {
119         byte[] rgb = new byte[3];
120         for (int y = 0; y < config.leds.rows; y++) {
121             for (int x = 0; x < 16; x++) {
122                 frame.getRowImage().get(y, x, rgb);
123                 drawRowPixel(gc, x, y, rgb);
124             }
125         }
126     }
127
128     private void drawColPixel(GraphicsContext gc, int x, int y, byte[] rgb) {
129         float ledSize = 16f * BLOCK / config.leds.cols;
130         gc.setFill(Color.rgb(rgb[0] & 0xff, rgb[1] & 0xff, rgb[2] & 0xff, 0.5));
131         gc.fillRect(GUTTER + x * ledSize, GUTTER + y * BLOCK, ledSize, BLOCK);
132     }
133
134     private void drawRowPixel(GraphicsContext gc, int x, int y, byte[] rgb) {
135         float ledSize = 9f * BLOCK / config.leds.rows;
136         gc.setFill(Color.rgb(rgb[0] & 0xff, rgb[1] & 0xff, rgb[2] & 0xff, 0.5));
137         gc.fillRect(GUTTER + x * BLOCK, GUTTER + y * ledSize, BLOCK, ledSize);
138     }
139
140     private void drawBorderAndGrid(GraphicsContext gc) {
141         gc.setStroke(Color.BLACK);
142         gc.setLineWidth(BLOCK * 0.1);
143         gc.strokeRect(GUTTER, GUTTER, 16 * BLOCK, 9 * BLOCK);
144         gc.setLineWidth(1);
145         for (int x = 1; x < 16; x++) {
146             gc.strokeLine(GUTTER + x * BLOCK, GUTTER, GUTTER + x * BLOCK, GUTTER + 9 * BLOCK);
147         }
148         for (int y = 1; y < 9; y++) {
149             gc.strokeLine(GUTTER, GUTTER + y * BLOCK, GUTTER + 16 * BLOCK, GUTTER + y * BLOCK);
150         }
151     }
152
153     private void drawPixel(GraphicsContext gc, int x, int y, Paint paint) {
154         gc.setFill(paint);
155         gc.fillRect(GUTTER + x * BLOCK, GUTTER + y * BLOCK, BLOCK, BLOCK);
156     }
157 }