1- use std:: sync:: { Arc , Mutex } ;
1+ use std:: {
2+ ops:: Deref ,
3+ sync:: { Arc , Mutex } ,
4+ } ;
25
36use glam:: { Vec2 , Vec3 } ;
47use glow:: HasContext ;
8+ use instant:: Instant ;
59
610use crate :: {
711 entities:: ProcessedEntityMesh ,
8- render:: { self , entity:: EntityRenderer , grid:: GridRenderer , RenderUniforms } ,
12+ render:: {
13+ self ,
14+ camera:: { ArcBallCamera , Camera3D } ,
15+ entity:: EntityRenderer ,
16+ grid:: GridRenderer ,
17+ RenderUniforms ,
18+ } ,
919} ;
1020
1121pub struct EntityFrame {
1222 pub hashcode : u32 ,
1323
1424 pub textures : Vec < RenderableTexture > ,
1525 pub renderers : Vec < Arc < Mutex < EntityRenderer > > > ,
16- orientation : Vec2 ,
17- zoom : f32 ,
26+ camera : Arc < Mutex < dyn Camera3D > > ,
1827
1928 grid : GridRenderer ,
2029 mesh_center : Vec3 ,
2130 pub show_grid : bool ,
2231 pub orthographic : bool ,
32+
33+ last_frame : Instant ,
2334}
2435
2536#[ derive( Clone ) ]
@@ -44,12 +55,12 @@ impl EntityFrame {
4455 hashcode,
4556 textures,
4657 renderers : vec ! [ ] ,
47- orientation : Vec2 :: new ( -2. , -1. ) ,
48- zoom : 5.0 ,
58+ camera : Arc :: new ( Mutex :: new ( ArcBallCamera :: default ( ) ) ) ,
4959 mesh_center : Vec3 :: ZERO ,
5060 show_grid : true ,
5161 orthographic : false ,
5262 grid : GridRenderer :: new ( gl, 30 ) ,
63+ last_frame : Instant :: now ( ) ,
5364 } ;
5465
5566 unsafe {
@@ -69,26 +80,19 @@ impl EntityFrame {
6980 s
7081 }
7182
72- fn zoom_factor ( zoom_level : f32 ) -> f32 {
73- 2.0f32 . powf ( zoom_level * std:: f32:: consts:: LN_2 ) - 0.9
74- }
75-
7683 pub fn show ( & mut self , ui : & mut egui:: Ui ) {
7784 let ( rect, response) =
7885 ui. allocate_exact_size ( ui. available_size ( ) , egui:: Sense :: click_and_drag ( ) ) ;
7986
80- if let Some ( multi_touch) = ui. ctx ( ) . multi_touch ( ) {
81- self . zoom += -( multi_touch. zoom_delta - 1.0 ) ;
82- } else {
83- self . orientation += Vec2 :: new ( response. drag_delta ( ) . x , response. drag_delta ( ) . y ) * 0.005 ;
84-
85- self . zoom += -ui. input ( |i| i. scroll_delta ) . y * 0.005 ;
86- }
87-
88- self . zoom = self . zoom . clamp ( 0.00 , 250.0 ) ;
87+ self . camera . lock ( ) . unwrap ( ) . update (
88+ ui,
89+ Some ( response) ,
90+ ( Instant :: now ( ) - self . last_frame ) . as_secs_f32 ( ) ,
91+ ) ;
92+ self . last_frame = Instant :: now ( ) ;
8993
90- let orientation = self . orientation ;
91- let zoom = Self :: zoom_factor ( self . zoom ) ;
94+ // let orientation = self.orientation;
95+ // let zoom = Self::zoom_factor(self.zoom);
9296 let mesh_center = self . mesh_center ;
9397 let time = ui. input ( |t| t. time ) ;
9498
@@ -98,15 +102,15 @@ impl EntityFrame {
98102 // TODO(cohae): How do we get out of this situation
99103 let grid = self . grid . clone ( ) ; // FIXME: Ugh.
100104 let textures = self . textures . clone ( ) ; // FIXME: UUUUGH.
105+ let camera = self . camera . clone ( ) ;
101106
102107 let renderers = self . renderers . clone ( ) ;
103108 let cb = egui_glow:: CallbackFn :: new ( move |info, painter| unsafe {
104109 render:: start_render ( painter. gl ( ) ) ;
105110
106111 let uniforms = RenderUniforms :: new (
107112 orthographic,
108- orientation,
109- zoom,
113+ camera. lock ( ) . unwrap ( ) . deref ( ) ,
110114 info. viewport . aspect_ratio ( ) ,
111115 ) ;
112116
0 commit comments