Skip to content

Commit 445de40

Browse files
committed
refactor: 移除SmoothValue
1 parent f68a57f commit 445de40

4 files changed

Lines changed: 13 additions & 117 deletions

File tree

crates/project_graph/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ pub mod fonts;
77
pub mod native;
88
pub mod settings;
99
pub mod settings_window;
10-
pub mod smooth_value;
1110
pub mod stage;
1211
pub mod structs;
1312
pub mod themes;

crates/project_graph/src/smooth_value.rs

Lines changed: 0 additions & 73 deletions
This file was deleted.

crates/project_graph/src/stage.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ impl Stage {
4040

4141
pub fn ui(&mut self, ui: &mut egui::Ui) {
4242
let dt = ui.input(|i| i.stable_dt);
43-
self.camera.tick(dt);
4443

4544
let available_size = ui.available_size();
4645
let (rect, response) = ui.allocate_exact_size(
@@ -98,16 +97,16 @@ impl Stage {
9897
let new_zoom = old_zoom * zoom_factor;
9998
let offset = mouse_pos - rect.center();
10099
let delta = offset * (1.0 / old_zoom - 1.0 / new_zoom);
101-
self.camera.pan_by_immediate(delta);
100+
self.camera.pan_by(delta);
102101
}
103102

104-
self.camera.zoom_by_immediate(zoom_factor);
103+
self.camera.zoom_by(zoom_factor);
105104
}
106105

107106
// 中键拖拽平移
108107
if response.dragged_by(egui::PointerButton::Middle) {
109108
let drag_delta = ui.input(|i| i.pointer.delta()) / self.camera.zoom();
110-
self.camera.pan_by_immediate(-drag_delta);
109+
self.camera.pan_by(-drag_delta);
111110
}
112111
}
113112
}

crates/project_graph/src/stage/camera.rs

Lines changed: 10 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,38 @@
11
use egui::{Pos2, Vec2};
22

3-
use crate::smooth_value::SmoothValue;
4-
53
pub struct Camera {
64
/// 视野中心位置
7-
position: SmoothValue<Vec2>,
5+
position: Pos2,
86
/// 缩放
9-
zoom: SmoothValue<f32>,
7+
zoom: f32,
108
}
119

1210
impl Camera {
1311
pub fn new() -> Self {
1412
Self {
15-
position: SmoothValue::new(Vec2::ZERO),
16-
zoom: SmoothValue::new(1.0),
13+
position: Pos2::ZERO,
14+
zoom: 1.0,
1715
}
1816
}
1917

20-
pub fn tick(&mut self, dt: f32) {
21-
self.position.tick(dt);
22-
self.zoom.tick(dt);
23-
}
24-
2518
pub fn position(&self) -> Pos2 {
26-
self.position.get().to_pos2()
27-
}
28-
pub fn position_target(&self) -> Pos2 {
29-
self.position.target().to_pos2()
19+
self.position
3020
}
3121
pub fn move_to(&mut self, target: Pos2) {
32-
self.position.set(target.to_vec2());
33-
}
34-
pub fn move_to_immediate(&mut self, target: Pos2) {
35-
self.position.snap(target.to_vec2());
22+
self.position = target;
3623
}
3724
pub fn pan_by(&mut self, delta: Vec2) {
38-
let new_target = self.position.target() + delta;
39-
self.position.set(new_target);
40-
}
41-
pub fn pan_by_immediate(&mut self, delta: Vec2) {
42-
let new_target = self.position.get() + delta;
43-
self.position.snap(new_target);
25+
self.position += delta / self.zoom;
4426
}
4527

4628
pub fn zoom(&self) -> f32 {
47-
self.zoom.get()
48-
}
49-
pub fn zoom_target(&self) -> f32 {
50-
self.zoom.target()
29+
self.zoom
5130
}
5231
pub fn zoom_to(&mut self, target_zoom: f32) {
53-
self.zoom.set(target_zoom);
54-
}
55-
pub fn zoom_to_immediate(&mut self, target_zoom: f32) {
56-
self.zoom.snap(target_zoom);
32+
self.zoom = target_zoom;
5733
}
5834
pub fn zoom_by(&mut self, factor: f32) {
59-
let new_target_zoom = self.zoom.target() * factor;
60-
self.zoom.set(new_target_zoom);
61-
}
62-
pub fn zoom_by_immediate(&mut self, factor: f32) {
63-
let new_target_zoom = self.zoom.get() * factor;
64-
self.zoom.snap(new_target_zoom);
35+
self.zoom *= factor;
6536
}
6637

6738
pub fn screen_to_world(&self, screen_pos: Pos2, screen_center: Pos2) -> Pos2 {

0 commit comments

Comments
 (0)