Refactored the LED frames backing data
[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 || keyEvent.getCode() == KeyCode.Q) {
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 javafx.scene.paint.Color getLedColor(LedFrame frame, int led) {
67         kaka.cakelight.Color c = frame.getLedColor(led);
68         return javafx.scene.paint.Color.rgb(c.r(), c.g(), c.b());
69     }
70
71     private void drawFrame(GraphicsContext gc, Frame frame) {
72         if (paused) return;
73         System.out.println("Drawing a frame");
74         drawCols(gc, frame);
75         drawRows(gc, frame);
76 //        drawVideo(gc, frame);
77         drawBorderAndGrid(gc);
78     }
79
80     private void drawLEDs(GraphicsContext gc, LedFrame frame) {
81         int ledLength = GUTTER;
82         float colSize = 16f * BLOCK / config.leds.cols;
83         float rowSize = 9f * BLOCK / config.leds.rows;
84 //        DropShadow shadow = new DropShadow(BlurType.ONE_PASS_BOX, Color.RED, colSize * 2, colSize, 0, 0);
85         for (int x = 0; x < config.leds.cols; x++) {
86             // Top
87             gc.setFill(getLedColor(frame, config.leds.cols * 2 + config.leds.rows - x - 1));
88             gc.fillRect(GUTTER + x * colSize, GUTTER - ledLength, colSize, ledLength);
89             // Bottom
90             gc.setFill(getLedColor(frame, x));
91             gc.fillRect(GUTTER + x * colSize, GUTTER + 9 * BLOCK, colSize, ledLength);
92         }
93         for (int y = 0; y < config.leds.rows; y++) {
94             // Left
95             gc.setFill(getLedColor(frame, config.leds.cols * 2 + config.leds.rows + y));
96             gc.fillRect(GUTTER - ledLength, GUTTER + y * rowSize, ledLength, rowSize);
97             // Right
98             gc.setFill(getLedColor(frame, config.leds.rows + config.leds.cols - y - 1));
99             gc.fillRect(GUTTER + 16 * BLOCK, GUTTER + y * rowSize, ledLength, rowSize);
100         }
101     }
102
103     private void drawVideo(GraphicsContext gc, Frame frame) {
104         byte[] rgb = new byte[3];
105         Mat img = frame.getConvertedImage();
106         float colSize = 16 * BLOCK / (float)img.cols();
107         float rowSize = 9 * BLOCK / (float)img.rows();
108         for (int x = 0, cols = img.cols(); x < cols; x++) {
109             for (int y = 0, rows = img.rows(); y < rows; y++) {
110                 img.get(y, x, rgb);
111                 gc.setFill(Color.rgb(rgb[0] & 0xff, rgb[1] & 0xff, rgb[2] & 0xff, 0.5));
112                 gc.fillRect(GUTTER + x * colSize, GUTTER + y * rowSize, colSize, rowSize);
113             }
114         }
115     }
116
117     private void drawCols(GraphicsContext gc, Frame frame) {
118         byte[] rgb = new byte[3];
119         for (int x = 0; x < config.leds.cols; x++) {
120             for (int y = 0; y < 9; y++) {
121                 frame.getColImage().get(y, x, rgb);
122                 drawColPixel(gc, x, y, rgb);
123             }
124         }
125     }
126
127     private void drawRows(GraphicsContext gc, Frame frame) {
128         byte[] rgb = new byte[3];
129         for (int y = 0; y < config.leds.rows; y++) {
130             for (int x = 0; x < 16; x++) {
131                 frame.getRowImage().get(y, x, rgb);
132                 drawRowPixel(gc, x, y, rgb);
133             }
134         }
135     }
136
137     private void drawColPixel(GraphicsContext gc, int x, int y, byte[] rgb) {
138         float ledSize = 16f * BLOCK / config.leds.cols;
139         gc.setFill(Color.rgb(rgb[0] & 0xff, rgb[1] & 0xff, rgb[2] & 0xff, 0.5));
140         gc.fillRect(GUTTER + x * ledSize, GUTTER + y * BLOCK, ledSize, BLOCK);
141     }
142
143     private void drawRowPixel(GraphicsContext gc, int x, int y, byte[] rgb) {
144         float ledSize = 9f * BLOCK / config.leds.rows;
145         gc.setFill(Color.rgb(rgb[0] & 0xff, rgb[1] & 0xff, rgb[2] & 0xff, 0.5));
146         gc.fillRect(GUTTER + x * BLOCK, GUTTER + y * ledSize, BLOCK, ledSize);
147     }
148
149     private void drawBorderAndGrid(GraphicsContext gc) {
150         gc.setStroke(Color.BLACK);
151         gc.setLineWidth(BLOCK * 0.1);
152         gc.strokeRect(GUTTER, GUTTER, 16 * BLOCK, 9 * BLOCK);
153         gc.setLineWidth(1);
154         for (int x = 1; x < 16; x++) {
155             gc.strokeLine(GUTTER + x * BLOCK, GUTTER, GUTTER + x * BLOCK, GUTTER + 9 * BLOCK);
156         }
157         for (int y = 1; y < 9; y++) {
158             gc.strokeLine(GUTTER, GUTTER + y * BLOCK, GUTTER + 16 * BLOCK, GUTTER + y * BLOCK);
159         }
160     }
161
162     private void drawPixel(GraphicsContext gc, int x, int y, Paint paint) {
163         gc.setFill(paint);
164         gc.fillRect(GUTTER + x * BLOCK, GUTTER + y * BLOCK, BLOCK, BLOCK);
165     }
166 }