From 4b162851432287550c218615ba61e4ba83adf030 Mon Sep 17 00:00:00 2001 From: Mikolaj Wielgus Date: Sun, 8 Dec 2024 16:56:35 +0100 Subject: [PATCH] refactor(egui): move layer colors to `Config` --- crates/topola-egui/src/app.rs | 13 ++-- crates/topola-egui/src/appearance_panel.rs | 50 +----------- crates/topola-egui/src/config.rs | 90 +++++++++++++++++++++- crates/topola-egui/src/menu_bar.rs | 6 +- crates/topola-egui/src/viewport.rs | 18 +++-- crates/topola-egui/src/workspace.rs | 10 +-- 6 files changed, 115 insertions(+), 72 deletions(-) diff --git a/crates/topola-egui/src/app.rs b/crates/topola-egui/src/app.rs index dfe0dfc..e9cd3d8 100644 --- a/crates/topola-egui/src/app.rs +++ b/crates/topola-egui/src/app.rs @@ -209,17 +209,20 @@ impl eframe::App for App { .and_then(|w| w.interactor.maybe_activity().as_ref()), ); - if self.menu_bar.show_layer_manager { + if self.menu_bar.show_appearance_panel { if let Some(workspace) = &mut self.maybe_workspace { - workspace.update_layers(ctx); + workspace.update_appearance_panel(ctx); } } self.error_dialog.update(ctx, &self.translator); - let _viewport_rect = - self.viewport - .update(ctx, &self.menu_bar, self.maybe_workspace.as_mut()); + let _viewport_rect = self.viewport.update( + &self.config, + ctx, + &self.menu_bar, + self.maybe_workspace.as_mut(), + ); self.update_locale(); self.update_title(ctx); diff --git a/crates/topola-egui/src/appearance_panel.rs b/crates/topola-egui/src/appearance_panel.rs index 3073ad1..11f478d 100644 --- a/crates/topola-egui/src/appearance_panel.rs +++ b/crates/topola-egui/src/appearance_panel.rs @@ -5,8 +5,6 @@ pub struct AppearancePanel { // 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 AppearancePanel { @@ -16,53 +14,7 @@ impl AppearancePanel { .take(layer_count) .collect::>() .into_boxed_slice(); - - let colors = std::iter::repeat(egui::Color32::from_rgb(255, 255, 255)) - .enumerate() - .map(|(i, color)| { - let layername = board.mesadata().layer_layername(i); - - if matches!(layername, Some("F.Cu") | Some("1")) { - egui::Color32::from_rgb(255, 52, 52) - } else if matches!(layername, Some("B.Cu") | Some("2")) { - egui::Color32::from_rgb(52, 52, 255) - } else if matches!(layername, Some("In1.Cu")) { - egui::Color32::from_rgb(127, 200, 127) - } else if matches!(layername, Some("In2.Cu")) { - egui::Color32::from_rgb(206, 125, 44) - } else { - color - } - }) - .take(layer_count) - .collect::>() - .into_boxed_slice(); - let highlight_colors = std::iter::repeat(egui::Color32::from_rgb(255, 255, 255)) - .enumerate() - .map(|(i, color)| { - let layername = board.mesadata().layer_layername(i); - - if matches!(layername, Some("F.Cu") | Some("1")) { - egui::Color32::from_rgb(255, 100, 100) - } else if matches!(layername, Some("B.Cu") | Some("2")) { - egui::Color32::from_rgb(100, 100, 255) - } else if matches!(layername, Some("In1.Cu")) { - egui::Color32::from_rgb(213, 236, 213) - } else if matches!(layername, Some("In2.Cu")) { - egui::Color32::from_rgb(232, 195, 158) - } else { - color - } - }) - .take(layer_count) - .collect::>() - .into_boxed_slice(); - - Self { - visible, - colors, - highlight_colors, - } + Self { visible } } pub fn update(&mut self, ctx: &egui::Context, board: &Board) { diff --git a/crates/topola-egui/src/config.rs b/crates/topola-egui/src/config.rs index cb885d0..95b1a42 100644 --- a/crates/topola-egui/src/config.rs +++ b/crates/topola-egui/src/config.rs @@ -1,9 +1,95 @@ +use std::collections::HashMap; + #[derive(Clone, Debug, PartialEq, serde::Deserialize, serde::Serialize)] #[serde(default)] -pub struct Config {} +pub struct Config { + pub dark_theme: Colors, + pub light_theme: Colors, +} + +#[derive(Clone, Debug, PartialEq, serde::Deserialize, serde::Serialize)] +pub struct Colors { + pub layers: LayerColors, +} + +#[derive(Clone, Debug, PartialEq, serde::Deserialize, serde::Serialize)] +pub struct LayerColors { + default: LayerColor, + colors: HashMap, +} + +impl LayerColors { + pub fn color(&self, layername: Option<&str>) -> &LayerColor { + layername + .and_then(|layername| Some(self.colors.get(layername).unwrap_or(&self.default))) + .unwrap_or(&self.default) + } +} + +#[derive(Clone, Debug, PartialEq, serde::Deserialize, serde::Serialize)] +pub struct LayerColor { + pub normal: egui::Color32, + pub highlighted: egui::Color32, +} impl Default for Config { fn default() -> Self { - Self {} + let dark_theme = Colors { + layers: LayerColors { + default: LayerColor { + normal: egui::Color32::from_rgb(255, 255, 255), + highlighted: egui::Color32::from_rgb(255, 255, 255), + }, + colors: HashMap::from([ + ( + "F.Cu".to_string(), + LayerColor { + normal: egui::Color32::from_rgb(255, 52, 52), + highlighted: egui::Color32::from_rgb(255, 100, 100), + }, + ), + ( + "1".to_string(), + LayerColor { + normal: egui::Color32::from_rgb(255, 52, 52), + highlighted: egui::Color32::from_rgb(255, 100, 100), + }, + ), + ( + "B.Cu".to_string(), + LayerColor { + normal: egui::Color32::from_rgb(52, 52, 255), + highlighted: egui::Color32::from_rgb(100, 100, 255), + }, + ), + ( + "2".to_string(), + LayerColor { + normal: egui::Color32::from_rgb(52, 52, 255), + highlighted: egui::Color32::from_rgb(100, 100, 255), + }, + ), + ( + "In1.Cu".to_string(), + LayerColor { + normal: egui::Color32::from_rgb(127, 200, 127), + highlighted: egui::Color32::from_rgb(213, 236, 213), + }, + ), + ( + "In2.Cu".to_string(), + LayerColor { + normal: egui::Color32::from_rgb(206, 125, 44), + highlighted: egui::Color32::from_rgb(232, 195, 158), + }, + ), + ]), + }, + }; + + Self { + dark_theme: dark_theme.clone(), + light_theme: dark_theme, + } } } diff --git a/crates/topola-egui/src/menu_bar.rs b/crates/topola-egui/src/menu_bar.rs index 9d8e343..58ced97 100644 --- a/crates/topola-egui/src/menu_bar.rs +++ b/crates/topola-egui/src/menu_bar.rs @@ -26,7 +26,7 @@ pub struct MenuBar { pub show_navmesh: bool, pub show_bboxes: bool, pub show_origin_destination: bool, - pub show_layer_manager: bool, + pub show_appearance_panel: bool, pub frame_timestep: f32, } @@ -46,7 +46,7 @@ impl MenuBar { show_navmesh: false, show_bboxes: false, show_origin_destination: false, - show_layer_manager: true, + show_appearance_panel: true, frame_timestep: 0.1, } } @@ -359,7 +359,7 @@ impl MenuBar { ui.separator(); ui.checkbox( - &mut self.show_layer_manager, + &mut self.show_appearance_panel, tr.text("tr-menu-view-show-layer-manager"), ); diff --git a/crates/topola-egui/src/viewport.rs b/crates/topola-egui/src/viewport.rs index cf92143..81dd942 100644 --- a/crates/topola-egui/src/viewport.rs +++ b/crates/topola-egui/src/viewport.rs @@ -9,6 +9,7 @@ use topola::{ execution::Command, invoker::{GetGhosts, GetMaybeNavcord, GetMaybeNavmesh, GetObstacles, Invoker}, }, + board::mesadata::AccessMesadata, drawing::{ graph::{MakePrimitive, PrimitiveIndex}, primitive::MakePrimitiveShape, @@ -19,7 +20,7 @@ use topola::{ specctra::mesadata::SpecctraMesadata, }; -use crate::{menu_bar::MenuBar, painter::Painter, workspace::Workspace}; +use crate::{config::Config, menu_bar::MenuBar, painter::Painter, workspace::Workspace}; pub struct Viewport { pub transform: egui::emath::TSTransform, @@ -36,6 +37,7 @@ impl Viewport { pub fn update( &mut self, + config: &Config, ctx: &egui::Context, menu_bar: &MenuBar, maybe_workspace: Option<&mut Workspace>, @@ -56,7 +58,7 @@ impl Viewport { let mut painter = Painter::new(ui, self.transform, menu_bar.show_bboxes); if let Some(workspace) = maybe_workspace { - let layers = &mut workspace.layers; + let layers = &mut workspace.appearance_panel; let overlay = &mut workspace.overlay; if ctx.input(|i| i.pointer.any_click()) { @@ -91,15 +93,15 @@ impl Viewport { .selection() .contains_node(board, GenericNode::Primitive(primitive)) { - layers.highlight_colors[i] + config.dark_theme.layers.color(board.layout().rules().layer_layername(i)).highlighted } else if let Some(activity) = &mut workspace.interactor.maybe_activity() { if activity.obstacles().contains(&primitive) { - layers.highlight_colors[i] + config.dark_theme.layers.color(board.layout().rules().layer_layername(i)).highlighted } else { - layers.colors[i] + config.dark_theme.layers.color(board.layout().rules().layer_layername(i)).normal } } else { - layers.colors[i] + config.dark_theme.layers.color(board.layout().rules().layer_layername(i)).normal }; painter.paint_primitive(&shape, color); @@ -110,9 +112,9 @@ impl Viewport { .selection() .contains_node(board, GenericNode::Compound(poly.into())) { - layers.highlight_colors[i] + config.dark_theme.layers.color(board.layout().rules().layer_layername(i)).highlighted } else { - layers.colors[i] + config.dark_theme.layers.color(board.layout().rules().layer_layername(i)).normal }; painter.paint_polygon(&board.layout().poly(poly).shape().polygon, color) diff --git a/crates/topola-egui/src/workspace.rs b/crates/topola-egui/src/workspace.rs index eefdade..76f392b 100644 --- a/crates/topola-egui/src/workspace.rs +++ b/crates/topola-egui/src/workspace.rs @@ -22,7 +22,7 @@ use crate::{ /// A loaded design and associated structures pub struct Workspace { pub design: SpecctraDesign, - pub layers: AppearancePanel, + pub appearance_panel: AppearancePanel, pub overlay: Overlay, pub interactor: Interactor, @@ -35,7 +35,7 @@ pub struct Workspace { impl Workspace { pub fn new(design: SpecctraDesign, tr: &Translator) -> Result { let board = design.make_board(&mut LayoutEdit::new()); - let layers = AppearancePanel::new(&board); + let appearance_panel = AppearancePanel::new(&board); let overlay = Overlay::new(&board).map_err(|err| { format!( "{}; {}", @@ -45,7 +45,7 @@ impl Workspace { })?; Ok(Self { design, - layers, + appearance_panel, overlay, interactor: Interactor::new(board).map_err(|err| { format!( @@ -98,8 +98,8 @@ impl Workspace { } } - pub fn update_layers(&mut self, ctx: &egui::Context) { - self.layers + pub fn update_appearance_panel(&mut self, ctx: &egui::Context) { + self.appearance_panel .update(ctx, self.interactor.invoker().autorouter().board()); } }