Several updates - added support for second pattern engine and adding commands
[led-pattern-ed] / src / led-pattern-view.vala
index 89b6163..0e908d5 100644 (file)
@@ -82,131 +82,120 @@ class LedPatternView : Gtk.DrawingArea {
                ctx.stroke ();
 
                if (pattern != null) {
-                       ctx.new_path ();
+                       if (pattern.color1 != LedColor.OFF)
+                               draw_pattern (ctx, width, height,
+                                             pattern.color1, pattern.engine1);
+                       if (pattern.color2 != LedColor.OFF)
+                               draw_pattern (ctx, width, height,
+                                             pattern.color2, pattern.engine2);
+               }
+               return true;
+       }
 
-                       if (pattern.led_map == "r") {
-                               ctx.set_source_rgb (1, 0, 0);
-                       } else if (pattern.led_map == "g") {
-                               ctx.set_source_rgb (0, 1, 0);
-                       } else if (pattern.led_map == "b") {
-                               ctx.set_source_rgb (0, 0, 1);
-                       } else if (pattern.led_map == "rg") {
-                               ctx.set_source_rgb (1, 1, 0);
-                       } else if (pattern.led_map == "rb") {
-                               ctx.set_source_rgb (1, 0, 1);
-                       } else if (pattern.led_map == "gb") {
-                               ctx.set_source_rgb (0, 1, 1);
-                       } else if (pattern.led_map == "rgb") {
-                               ctx.set_source_rgb (1, 1, 1);
-                       } else {
-                               ctx.set_source_rgb (0.75, 0.75, 0.75);
+       private void draw_pattern (Cairo.Context ctx, int width, int height, LedColor color,
+                                  List<LedCommandRX51> engine) {
+               double pps = width / duration; // pixel per second
+
+               ctx.new_path ();
+
+               ctx.set_operator (Cairo.Operator.ADD);
+
+               ctx.set_source_rgb ((LedColor.R in color) ? 1.0 : 0.0,
+                                   (LedColor.G in color) ? 1.0 : 0.0,
+                                   (LedColor.B in color) ? 1.0 : 0.0);
+               ctx.set_line_width (3.0);
+
+               double x = 0, y = 0;
+               foreach (LedCommand command in engine) {
+                       x = command.time * pps/1000.0;
+                       y = (255 - command.level) * (height - 1)/255.0;
+                       switch (command.type) {
+                       case CommandType.RAMP_WAIT:
+                       case CommandType.TRIGGER:
+                               x += command.duration * pps/1000.0;
+                               y -= command.steps * (height - 1)/255.0;
+                               if (y < 0)
+                                       y = 0;
+                               if (y > (height - 1))
+                                       y = height - 1;
+                               ctx.line_to (x, y);
+                               break;
+                       default:
+                               ctx.line_to (x, y);
+                               break;
                        }
-                       ctx.set_line_width (3.0);
-
-                       double x = 0, y = 0;
-                       foreach (LedCommand command in pattern.engine1) {
-                               x = command.time * pps/1000.0;
-                               y = (255 - command.level) * (height - 1)/255.0;
-                               switch (command.type) {
-                               case CommandType.RAMP_WAIT:
-                                       x += command.duration * pps/1000.0;
-                                       y -= command.steps * (height - 1)/255.0;
-                                       if (y < 0)
-                                               y = 0;
-                                       if (y > (height - 1))
-                                               y = height - 1;
-                                       ctx.line_to (x, y);
-                                       break;
-                               default:
-                                       ctx.line_to (x, y);
-                                       break;
-                               }
+               }
+               ctx.stroke ();
+
+               ctx.set_source_rgb ((LedColor.R in color) ? 0.75 : 0.0,
+                                   (LedColor.G in color) ? 0.75 : 0.0,
+                                   (LedColor.B in color) ? 0.75 : 0.0);
+               ctx.set_line_width (1.0);
+
+               LedCommandRX51 last_command = null;
+               foreach (LedCommandRX51 command in engine) {
+                       if (command.type == CommandType.END ||
+                           command.type == CommandType.GO_TO_START) {
+                               last_command = command;
+                               break;
                        }
-                       ctx.stroke ();
+               }
+               if (last_command == null)
+                       return;
+
+               if (last_command.type == CommandType.END) {
+                       ctx.new_path ();
 
-                       if (pattern.led_map == "r") {
-                               ctx.set_source_rgb (0.75, 0, 0);
-                       } else if (pattern.led_map == "g") {
-                               ctx.set_source_rgb (0, 0.75, 0);
-                       } else if (pattern.led_map == "b") {
-                               ctx.set_source_rgb (0, 0, 0.75);
-                       } else if (pattern.led_map == "rg") {
-                               ctx.set_source_rgb (0.75, 0.75, 0);
-                       } else if (pattern.led_map == "rb") {
-                               ctx.set_source_rgb (0.75, 0, 0.75);
-                       } else if (pattern.led_map == "gb") {
-                               ctx.set_source_rgb (0, 0.75, 0.75);
-                       } else if (pattern.led_map == "rgb") {
-                               ctx.set_source_rgb (0.75, 0.75, 0.75);
+                       ctx.move_to (x, y);
+                       if (last_command.steps == -255) {
+                               ctx.line_to (x, height - 0.5);
+                               ctx.line_to (width - 0.5, height - 0.5);
                        } else {
-                               ctx.set_source_rgb (0.66, 0.66, 0.66);
-                       }
-                       ctx.set_line_width (1.0);
-
-                       CommandType type = CommandType.UNKNOWN;
-                       int steps = 0;
-                       foreach (LedCommandRX51 command in pattern.engine1) {
-                               if (command.type == CommandType.END ||
-                                   command.type == CommandType.GO_TO_START) {
-                                       type = command.type;
-                                       steps = command.steps;
-                                       break;
-                               }
+                               ctx.line_to (width - 0.5, y);
                        }
 
-                       if (type == CommandType.END) {
-                               ctx.new_path ();
+                       ctx.stroke ();
+               }
+               var engine_duration = last_command.time + last_command.duration;
+               if (last_command.type == CommandType.GO_TO_START && engine_duration >= 3 * 0.49) {
+                       ctx.new_path ();
 
+                       for (double offset = engine_duration; (offset * pps/1000.0) <= width; offset += engine_duration) {
                                ctx.move_to (x, y);
-                               if (steps == -255) {
-                                       ctx.line_to (x, height - 0.5);
-                                       ctx.line_to (width - 0.5, height - 0.5);
-                               } else {
-                                       ctx.line_to (width - 0.5, y);
-                               }
-
-                               ctx.stroke ();
-                       }
-                       if (type == CommandType.GO_TO_START && pattern.duration >= 3 * 0.49) {
-                               ctx.new_path ();
-
-                               for (double offset = pattern.duration; (offset * pps/1000.0) <= width; offset += pattern.duration) {
-                                       ctx.move_to (x, y);
-                                       foreach (LedCommand command in pattern.engine1) {
-                                               x = (command.time + offset) * pps/1000.0;
-                                               y = (255 - command.level) * (height - 1)/255.0;
-                                               if (x >= width)
-                                                       break;
-                                               switch (command.type) {
-                                               case CommandType.RAMP_WAIT:
-                                                       x += command.duration * pps/1000.0;
-                                                       y -= command.steps * (height - 1)/255.0;
-                                                       if (y < 0)
-                                                               y = 0;
-                                                       if (y > (height - 1))
-                                                               y = height - 1;
-                                                       ctx.line_to (x, y);
-                                                       break;
-                                               default:
-                                                       ctx.line_to (x, y);
-                                                       break;
-                                               }
+                               foreach (LedCommand command in engine) {
+                                       x = (command.time + offset) * pps/1000.0;
+                                       y = (255 - command.level) * (height - 1)/255.0;
+                                       if (x >= width)
+                                               break;
+                                       switch (command.type) {
+                                       case CommandType.RAMP_WAIT:
+                                       case CommandType.TRIGGER:
+                                               x += command.duration * pps/1000.0;
+                                               y -= command.steps * (height - 1)/255.0;
+                                               if (y < 0)
+                                                       y = 0;
+                                               if (y > (height - 1))
+                                                       y = height - 1;
+                                               ctx.line_to (x, y);
+                                               break;
+                                       default:
+                                               ctx.line_to (x, y);
+                                               break;
                                        }
                                }
-
-                               ctx.stroke ();
                        }
-                       if (type == CommandType.GO_TO_START && pattern.duration < 3 * 0.49) {
-                               ctx.new_path ();
 
+                       ctx.stroke ();
+               }
+               if (last_command.type == CommandType.GO_TO_START && engine_duration < 3 * 0.49) {
+                       ctx.new_path ();
 
-                               ctx.move_to (x, y);
-                               ctx.line_to (width - 0.5, y);
 
-                               ctx.stroke ();
-                       }
+                       ctx.move_to (x, y);
+                       ctx.line_to (width - 0.5, y);
+
+                       ctx.stroke ();
                }
-               return true;
        }
 
        public void update () {