Compare commits

...

3 Commits

Author SHA1 Message Date
mars b0ecf1eb92 Paint node kind label 2022-10-21 16:32:24 -06:00
mars 1fb7f03468 Align slot labels 2022-10-21 16:09:25 -06:00
mars 9318b59ac0 Paint output slot labels 2022-10-21 15:56:25 -06:00
1 changed files with 52 additions and 10 deletions

View File

@ -41,7 +41,7 @@ impl NodeEditor {
let pointer_pos = response.interact_pointer_pos();
let pointer_target = pointer_pos.map(|pos| layout.pointer_target(pos));
let painter = ui.painter_at(rect);
layout.paint(rect, &ui, &painter);
layout.paint(rect, &painter);
if let Some(pointer_pos) = pointer_pos {
self.pointer_state.position = pointer_pos;
@ -163,6 +163,7 @@ pub struct GraphStyle {
pub edge_control_offset: f32,
pub slot_radius: f32,
pub slot_spacing: f32,
pub slot_label_margin: f32,
pub slot_fill: Color32,
pub slot_stroke: Stroke,
}
@ -185,6 +186,7 @@ impl GraphStyle {
edge_control_offset: 200.0,
slot_radius: 7.0,
slot_spacing: 40.0,
slot_label_margin: 5.0,
slot_fill: style.visuals.widgets.noninteractive.bg_fill,
slot_stroke: style.visuals.widgets.noninteractive.bg_stroke,
}
@ -227,9 +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) {
@ -240,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();
@ -256,13 +280,16 @@ impl GraphLayout {
layout.input_positions.insert(slot_index, position);
let text = node_kind.inputs[slot_index.slot].label.to_owned();
let font_id =
egui::FontSelection::Style(egui::TextStyle::Body).resolve(&ui.style());
let color = ui.visuals().text_color();
let label_font = label_font.clone();
let wrap_width = f32::MAX;
let galley = ui.fonts().layout(text, font_id, color, wrap_width);
let galley = ui.fonts().layout(text, label_font, label_color, wrap_width);
layout.texts.push(TextLayout {
pos: egui::pos2(position.x, position.y),
pos: egui::pos2(
position.x - galley.rect.left()
+ style.slot_radius
+ style.slot_label_margin,
position.y - galley.rect.bottom() + style.slot_radius,
),
galley,
});
@ -277,7 +304,7 @@ impl GraphLayout {
}
}
for slot_index in 0..1 {
for (slot_index, output) in node_kind.outputs.iter().enumerate() {
let slot_y = slot_index as f32 * style.slot_spacing + slot_top;
let position = glam::Vec2::new(slot_right, slot_y);
@ -288,6 +315,21 @@ impl GraphLayout {
layout.output_positions.insert(slot_index, position);
let text = output.label.to_owned();
let label_font = label_font.clone();
let wrap_width = f32::MAX;
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.y - galley.rect.bottom() + style.slot_radius,
),
galley,
});
if pointer.action == PointerAction::SettingOutput(slot_index) {
let pointer = Vec2::new(pointer.position.x, pointer.position.y);
layout.active_edge = Some((pointer, position));
@ -354,7 +396,7 @@ impl GraphLayout {
PointerTarget::Grid
}
pub fn paint(&self, rect: egui::Rect, ui: &egui::Ui, painter: &egui::Painter) {
pub fn paint(&self, rect: egui::Rect, painter: &egui::Painter) {
let style = &self.style;
let grid_spacing = style.grid_spacing;
let grid_pos = rect.left_top().to_vec2() / egui::vec2(grid_spacing, grid_spacing);