From b0ecf1eb920e37d47b50ecc08be26a12435ab408 Mon Sep 17 00:00:00 2001 From: mars Date: Fri, 21 Oct 2022 16:32:24 -0600 Subject: [PATCH] Paint node kind label --- ramen_egui/src/lib.rs | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/ramen_egui/src/lib.rs b/ramen_egui/src/lib.rs index 14a8b70..9c1fb6e 100644 --- a/ramen_egui/src/lib.rs +++ b/ramen_egui/src/lib.rs @@ -229,13 +229,16 @@ impl GraphLayout { let mut edges = Vec::new(); + let kind_font = egui::FontSelection::Style(egui::TextStyle::Heading); + let kind_font = kind_font.resolve(ui.style()); + let kind_spacing = ui.style().spacing.item_spacing.y; + let kind_color = ui.visuals().text_color(); let label_font = egui::FontSelection::Style(egui::TextStyle::Body); let label_font = label_font.resolve(ui.style()); let label_color = ui.visuals().text_color(); for (node_index, node) in graph.nodes.iter() { let node_kind = &graph.node_kinds.kinds[node.kind]; - let mut node_pos = egui::pos2(node.pos.x, node.pos.y); if pointer.action == PointerAction::MovingNode(node_index) { @@ -246,7 +249,22 @@ impl GraphLayout { let node_rect = egui::Rect::from_min_size(node_pos, node_size); layout.node_rects.insert(node_index, node_rect); - let slot_top = node_pos.y + style.slot_spacing; + let text = node_kind.title.to_string(); + let kind_font = kind_font.clone(); + let wrap_width = f32::MAX; + let galley = ui.fonts().layout(text, kind_font, kind_color, wrap_width); + + let kind_height = galley.rect.height() + kind_spacing * 2.0; + + layout.texts.push(TextLayout { + pos: egui::pos2( + node_rect.center().x - galley.rect.center().x, + node_pos.y + kind_height - galley.rect.height(), + ), + galley, + }); + + let slot_top = node_pos.y + kind_height + style.slot_spacing; let slot_left = node_rect.left(); let slot_right = node_rect.right(); @@ -267,7 +285,9 @@ impl GraphLayout { let galley = ui.fonts().layout(text, label_font, label_color, wrap_width); layout.texts.push(TextLayout { pos: egui::pos2( - position.x - galley.rect.left() + style.slot_radius + style.slot_label_margin, + position.x - galley.rect.left() + + style.slot_radius + + style.slot_label_margin, position.y - galley.rect.bottom() + style.slot_radius, ), galley, @@ -301,7 +321,10 @@ impl GraphLayout { let galley = ui.fonts().layout(text, label_font, label_color, wrap_width); layout.texts.push(TextLayout { pos: egui::pos2( - position.x - galley.rect.right() - style.slot_radius - style.slot_label_margin, + position.x + - galley.rect.right() + - style.slot_radius + - style.slot_label_margin, position.y - galley.rect.bottom() + style.slot_radius, ), galley,