From 1cca9fe6e1236d4483974567165c9f9bcaaf0f19 Mon Sep 17 00:00:00 2001 From: Mikolaj Wielgus Date: Fri, 14 Jun 2024 03:47:27 +0200 Subject: [PATCH] egui: support displaying an arbitrary number of layers --- src/bin/topola-egui/layers.rs | 72 ++++++++++++++++++++++++++++-- src/bin/topola-egui/viewport.rs | 79 +++++++++++---------------------- 2 files changed, 95 insertions(+), 56 deletions(-) diff --git a/src/bin/topola-egui/layers.rs b/src/bin/topola-egui/layers.rs index b0c056a..5437e82 100644 --- a/src/bin/topola-egui/layers.rs +++ b/src/bin/topola-egui/layers.rs @@ -1,18 +1,82 @@ use topola::board::{mesadata::MesadataTrait, Board}; pub struct Layers { + // TODO: + // In1.Cu shall be #7fc87f (#d5ecd5 when selected). + // In2.Cu shall be #ce7d2c (#e8c39e when selected). pub visible: Box<[bool]>, + pub colors: Box<[egui::Color32]>, + pub highlight_colors: Box<[egui::Color32]>, } impl Layers { pub fn new(board: &Board) -> Self { let layer_count = board.layout().drawing().layer_count(); + let visible = std::iter::repeat(true) + .take(layer_count.try_into().unwrap() /* FIXME */) + .collect::>() + .into_boxed_slice(); + let colors = std::iter::repeat(egui::Color32::from_rgb(255, 255, 255)) + .enumerate() + .map(|(i, color)| { + if matches!( + board + .layout() + .drawing() + .rules() + .layer_layername(i.try_into().unwrap() /* FIXME */), + Some("F.Cu") + ) { + egui::Color32::from_rgb(255, 52, 52) + } else if matches!( + board + .layout() + .drawing() + .rules() + .layer_layername(i.try_into().unwrap() /* FIXME */), + Some("B.Cu") + ) { + egui::Color32::from_rgb(52, 52, 255) + } else { + color + } + }) + .take(layer_count.try_into().unwrap() /* FIXME */) + .collect::>() + .into_boxed_slice(); + let highlight_colors = std::iter::repeat(egui::Color32::from_rgb(255, 255, 255)) + .enumerate() + .map(|(i, color)| { + if matches!( + board + .layout() + .drawing() + .rules() + .layer_layername(i.try_into().unwrap() /* FIXME */), + Some("F.Cu") + ) { + egui::Color32::from_rgb(255, 100, 100) + } else if matches!( + board + .layout() + .drawing() + .rules() + .layer_layername(i.try_into().unwrap() /* FIXME */), + Some("B.Cu") + ) { + egui::Color32::from_rgb(100, 100, 255) + } else { + color + } + }) + .take(layer_count.try_into().unwrap() /* FIXME */) + .collect::>() + .into_boxed_slice(); Self { - visible: std::iter::repeat(true) - .take(layer_count.try_into().unwrap() /* FIXME */) - .collect::>() - .into_boxed_slice(), + visible, + colors, + highlight_colors, } } diff --git a/src/bin/topola-egui/viewport.rs b/src/bin/topola-egui/viewport.rs index 43e8e80..f44dc53 100644 --- a/src/bin/topola-egui/viewport.rs +++ b/src/bin/topola-egui/viewport.rs @@ -109,61 +109,36 @@ impl Viewport { let board = invoker.autorouter().board(); if let Some(layers) = maybe_layers { - if layers.visible[1] { - for primitive in board.layout().drawing().layer_primitive_nodes(1) { - let shape = primitive.primitive(board.layout().drawing()).shape(); + for i in (0..layers.visible.len()).rev() { + if layers.visible[i] { + for primitive in board.layout().drawing().layer_primitive_nodes(i.try_into().unwrap() /* FIXME */) { + let shape = primitive.primitive(board.layout().drawing()).shape(); - let color = if shared_data.highlighteds.contains(&primitive) - || overlay + let color = if shared_data.highlighteds.contains(&primitive) + || overlay + .selection() + .contains_node(board, GenericNode::Primitive(primitive)) + { + layers.highlight_colors[i] + } else { + layers.colors[i] + }; + + painter.paint_primitive(&shape, color); + } + + for zone in board.layout().layer_zone_nodes(1) { + let color = if overlay .selection() - .contains_node(board, GenericNode::Primitive(primitive)) - { - egui::Color32::from_rgb(100, 100, 255) - } else { - egui::Color32::from_rgb(52, 52, 200) - }; - painter.paint_primitive(&shape, color); - } + .contains_node(board, GenericNode::Compound(zone.into())) + { + layers.highlight_colors[i] + } else { + layers.colors[i] + }; - for zone in board.layout().layer_zone_nodes(1) { - let color = if overlay - .selection() - .contains_node(board, GenericNode::Compound(zone.into())) - { - egui::Color32::from_rgb(100, 100, 255) - } else { - egui::Color32::from_rgb(52, 52, 200) - }; - painter.paint_polygon(&board.layout().zone(zone).shape().polygon, color) - } - } - - if layers.visible[0] { - for primitive in board.layout().drawing().layer_primitive_nodes(0) { - let shape = primitive.primitive(board.layout().drawing()).shape(); - - let color = if shared_data.highlighteds.contains(&primitive) - || overlay - .selection() - .contains_node(board, GenericNode::Primitive(primitive)) - { - egui::Color32::from_rgb(255, 100, 100) - } else { - egui::Color32::from_rgb(200, 52, 52) - }; - painter.paint_primitive(&shape, color); - } - - for zone in board.layout().layer_zone_nodes(0) { - let color = if overlay - .selection() - .contains_node(board, GenericNode::Compound(zone.into())) - { - egui::Color32::from_rgb(255, 100, 100) - } else { - egui::Color32::from_rgb(200, 52, 52) - }; - painter.paint_polygon(&board.layout().zone(zone).shape().polygon, color) + painter.paint_polygon(&board.layout().zone(zone).shape().polygon, color) + } } } }