Skip to content

Commit bcf349e

Browse files
committed
refactor: 将Painter渲染改为Ui渲染
1 parent 035ac0d commit bcf349e

File tree

3 files changed

+20
-58
lines changed

3 files changed

+20
-58
lines changed

crates/project_graph/src/stage.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,17 @@ impl Stage {
4040
.camera
4141
.world_to_screen(entity.position(), screen_center);
4242

43-
entity.render(&mut RenderContext {
44-
painter: &painter,
45-
position: screen_pos,
46-
scale: self.camera.zoom(),
47-
});
43+
egui::Area::new(ui.make_persistent_id(entity.id()))
44+
.order(egui::Order::Foreground)
45+
.fixed_pos(screen_pos)
46+
.show(ui.ctx(), |ui| {
47+
entity.ui(
48+
ui,
49+
&mut RenderContext {
50+
zoom: self.camera.zoom(),
51+
},
52+
);
53+
});
4854
}
4955

5056
painter.text(
Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,5 @@
1-
use egui::{Painter, Pos2, Stroke, vec2};
2-
3-
pub struct RenderContext<'a> {
4-
pub painter: &'a Painter,
5-
pub position: Pos2,
6-
pub scale: f32,
1+
pub struct RenderContext {
2+
pub zoom: f32,
73
}
84

9-
impl<'a> RenderContext<'a> {
10-
pub fn local_to_screen(&self, local_pos: Pos2) -> Pos2 {
11-
Pos2 {
12-
x: self.position.x + local_pos.x * self.scale,
13-
y: self.position.y + local_pos.y * self.scale,
14-
}
15-
}
16-
17-
pub fn rect(&self, local_rect: egui::Rect, color: egui::Color32) {
18-
let screen_rect = egui::Rect {
19-
min: self.local_to_screen(local_rect.min),
20-
max: self.local_to_screen(local_rect.max),
21-
};
22-
self.painter.rect_stroke(
23-
screen_rect,
24-
8.0 * self.scale,
25-
Stroke {
26-
color,
27-
width: self.scale,
28-
},
29-
egui::StrokeKind::Middle,
30-
);
31-
}
32-
33-
pub fn text(&self, position: Pos2, text: &str) {
34-
self.painter.text(
35-
self.local_to_screen(position) - vec2(0.0, 14.0 * 0.15) * self.scale,
36-
egui::Align2::LEFT_TOP,
37-
text,
38-
egui::FontId::proportional(14.0 * self.scale),
39-
egui::Color32::WHITE,
40-
);
41-
}
42-
}
5+
impl RenderContext {}

crates/project_graph/src/stage/structs.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{stage::render_context::RenderContext, utils::kdl::KdlPos2};
88
pub trait EntityTrait {
99
fn id(&self) -> &str;
1010
fn position(&self) -> Pos2;
11-
fn render(&self, rc: &mut RenderContext);
11+
fn ui(&self, ui: &mut egui::Ui, rc: &mut RenderContext);
1212
}
1313

1414
#[derive(knus::Decode, Debug, Clone)]
@@ -39,12 +39,12 @@ impl EntityTrait for Text {
3939
fn position(&self) -> Pos2 {
4040
self.pos.into()
4141
}
42-
fn render(&self, rc: &mut RenderContext) {
42+
fn ui(&self, ui: &mut egui::Ui, rc: &mut RenderContext) {
4343
let padding = 8.0;
4444

4545
let text_width = *self.text_width.get_or_init(|| {
4646
// 这里调用你的 get_text_width 或者直接计算
47-
rc.painter
47+
ui.painter()
4848
.layout_no_wrap(
4949
self.val.clone(),
5050
FontId::proportional(14.0),
@@ -54,17 +54,10 @@ impl EntityTrait for Text {
5454
.x
5555
});
5656

57-
rc.rect(
58-
Rect {
59-
min: Pos2::ZERO,
60-
max: Pos2 {
61-
x: text_width + padding * 2.0,
62-
y: 14.0 + padding * 2.0,
63-
},
64-
},
65-
Color32::WHITE,
57+
ui.add(
58+
egui::Label::new(egui::RichText::new(&self.val).size(14.0 * rc.zoom))
59+
.wrap_mode(egui::TextWrapMode::Extend),
6660
);
67-
rc.text(pos2(padding, padding), &self.val);
6861
}
6962
}
7063

0 commit comments

Comments
 (0)