|
1 | 1 | use egui::{Pos2, Vec2}; |
2 | 2 |
|
3 | | -use crate::smooth_value::SmoothValue; |
4 | | - |
5 | 3 | pub struct Camera { |
6 | 4 | /// 视野中心位置 |
7 | | - position: SmoothValue<Vec2>, |
| 5 | + position: Pos2, |
8 | 6 | /// 缩放 |
9 | | - zoom: SmoothValue<f32>, |
| 7 | + zoom: f32, |
10 | 8 | } |
11 | 9 |
|
12 | 10 | impl Camera { |
13 | 11 | pub fn new() -> Self { |
14 | 12 | Self { |
15 | | - position: SmoothValue::new(Vec2::ZERO), |
16 | | - zoom: SmoothValue::new(1.0), |
| 13 | + position: Pos2::ZERO, |
| 14 | + zoom: 1.0, |
17 | 15 | } |
18 | 16 | } |
19 | 17 |
|
20 | | - pub fn tick(&mut self, dt: f32) { |
21 | | - self.position.tick(dt); |
22 | | - self.zoom.tick(dt); |
23 | | - } |
24 | | - |
25 | 18 | 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 |
30 | 20 | } |
31 | 21 | 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; |
36 | 23 | } |
37 | 24 | 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; |
44 | 26 | } |
45 | 27 |
|
46 | 28 | pub fn zoom(&self) -> f32 { |
47 | | - self.zoom.get() |
48 | | - } |
49 | | - pub fn zoom_target(&self) -> f32 { |
50 | | - self.zoom.target() |
| 29 | + self.zoom |
51 | 30 | } |
52 | 31 | 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; |
57 | 33 | } |
58 | 34 | 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; |
65 | 36 | } |
66 | 37 |
|
67 | 38 | pub fn screen_to_world(&self, screen_pos: Pos2, screen_center: Pos2) -> Pos2 { |
|
0 commit comments