WIP node input slot label painting

This commit is contained in:
mars 2022-10-21 15:46:54 -06:00
parent 47f0288f21
commit 1f2242c36c
1 changed files with 44 additions and 7 deletions

View File

@ -4,6 +4,7 @@ use ramen::command::CommandHistory;
use ramen::node::{Edge, SlotIndex};
use ramen::Graph;
use std::collections::HashMap;
use std::sync::Arc;
pub struct NodeEditor {
graph: Graph,
@ -19,11 +20,11 @@ impl NodeEditor {
pointer_state: PointerState::new(),
}
}
pub fn undo(&mut self) {
self.cmd_history.undo(&mut self.graph).unwrap();
}
pub fn redo(&mut self) {
self.cmd_history.redo(&mut self.graph).unwrap();
}
@ -31,7 +32,7 @@ impl NodeEditor {
pub fn show(&mut self, ui: &mut egui::Ui) {
let style = ui.style();
let style = GraphStyle::from_ui_style(style);
let layout = GraphLayout::layout(&style, &self.pointer_state, &self.graph);
let layout = GraphLayout::layout(ui, &style, &self.pointer_state, &self.graph);
let rect = ui.available_rect_before_wrap();
let sense = egui::Sense::click_and_drag();
@ -40,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, &painter);
layout.paint(rect, &ui, &painter);
if let Some(pointer_pos) = pointer_pos {
self.pointer_state.position = pointer_pos;
@ -104,7 +105,7 @@ impl NodeEditor {
let cmd_debug = format!("{:#?}", cmd);
match self.cmd_history.push(&mut self.graph, cmd) {
Ok(_) => {},
Ok(_) => {}
Err(e) => eprintln!("{}:\n {:#?}", cmd_debug, e),
}
}
@ -190,6 +191,12 @@ impl GraphStyle {
}
}
#[derive(Debug)]
pub struct TextLayout {
pub pos: egui::Pos2,
pub galley: Arc<egui::Galley>,
}
#[derive(Debug)]
pub struct GraphLayout {
pub style: GraphStyle,
@ -198,10 +205,16 @@ pub struct GraphLayout {
pub output_positions: HashMap<SlotIndex, Vec2>,
pub edges: HashMap<Edge, EdgeLayout>,
pub active_edge: Option<(Vec2, Vec2)>,
pub texts: Vec<TextLayout>,
}
impl GraphLayout {
pub fn layout(style: &GraphStyle, pointer: &PointerState, graph: &Graph) -> Self {
pub fn layout(
ui: &egui::Ui,
style: &GraphStyle,
pointer: &PointerState,
graph: &Graph,
) -> Self {
let mut layout = Self {
style: style.clone(),
node_rects: Default::default(),
@ -209,11 +222,14 @@ impl GraphLayout {
output_positions: Default::default(),
edges: Default::default(),
active_edge: None,
texts: Default::default(),
};
let mut edges = Vec::new();
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) {
@ -239,6 +255,17 @@ 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 wrap_width = f32::MAX;
let galley = ui.fonts().layout(text, font_id, color, wrap_width);
layout.texts.push(TextLayout {
pos: egui::pos2(position.x, position.y),
galley,
});
if pointer.action == PointerAction::SettingInput(slot_index) {
let pointer = Vec2::new(pointer.position.x, pointer.position.y);
layout.active_edge = Some((position, pointer));
@ -327,7 +354,7 @@ impl GraphLayout {
PointerTarget::Grid
}
pub fn paint(&self, rect: egui::Rect, painter: &egui::Painter) {
pub fn paint(&self, rect: egui::Rect, ui: &egui::Ui, 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);
@ -389,5 +416,15 @@ impl GraphLayout {
draw_slots(&self.input_positions);
draw_slots(&self.output_positions);
for text in self.texts.iter() {
painter.add(egui::epaint::TextShape {
pos: text.pos,
galley: text.galley.to_owned(),
underline: Stroke::default(),
override_text_color: None,
angle: 0.0,
});
}
}
}