1
1
//! Primitives for creating and controlling [`Window`](struct.Window.html).
2
2
3
3
use glutin;
4
- use glutin:: GlContext ;
5
4
use mint;
6
5
use render;
7
6
@@ -12,15 +11,14 @@ use render::Renderer;
12
11
use scene:: Scene ;
13
12
use std:: path:: PathBuf ;
14
13
15
- pub use glutin:: CursorState ;
16
-
17
14
/// `Window` is the core entity of every `three-rs` application.
18
15
///
19
16
/// It provides [user input](struct.Window.html#method.update),
20
17
/// [`Factory`](struct.Factory.html) and [`Renderer`](struct.Renderer.html).
21
18
pub struct Window {
22
19
event_loop : glutin:: EventsLoop ,
23
20
window : glutin:: GlWindow ,
21
+ dpi : f64 ,
24
22
/// See [`Input`](struct.Input.html).
25
23
pub input : Input ,
26
24
/// See [`Renderer`](struct.Renderer.html).
@@ -38,7 +36,7 @@ pub struct Window {
38
36
/// Builder for creating new [`Window`](struct.Window.html) with desired parameters.
39
37
#[ derive( Debug , Clone ) ]
40
38
pub struct Builder {
41
- dimensions : ( u32 , u32 ) ,
39
+ dimensions : glutin :: dpi :: LogicalSize ,
42
40
fullscreen : bool ,
43
41
multisampling : u16 ,
44
42
shader_directory : Option < PathBuf > ,
@@ -47,13 +45,15 @@ pub struct Builder {
47
45
}
48
46
49
47
impl Builder {
50
- /// Set the size of the viewport (the resolution) in pixels. Defaults to 1024x768.
48
+ /// Set the size of the viewport (the resolution) in logical pixels.
49
+ /// That is the dpi setting affects the amount of pixels used but the window will
50
+ /// take up the same amount of space regardless of dpi. Defaults to 1024x768.
51
51
pub fn dimensions (
52
52
& mut self ,
53
- width : u32 ,
54
- height : u32 ,
53
+ width : f64 ,
54
+ height : f64 ,
55
55
) -> & mut Self {
56
- self . dimensions = ( width, height) ;
56
+ self . dimensions = glutin :: dpi :: LogicalSize :: new ( width, height) ;
57
57
self
58
58
}
59
59
@@ -105,7 +105,7 @@ impl Builder {
105
105
106
106
let builder = glutin:: WindowBuilder :: new ( )
107
107
. with_fullscreen ( monitor_id)
108
- . with_dimensions ( self . dimensions . 0 , self . dimensions . 1 )
108
+ . with_dimensions ( self . dimensions )
109
109
. with_title ( self . title . clone ( ) ) ;
110
110
111
111
let context = glutin:: ContextBuilder :: new ( )
@@ -146,10 +146,12 @@ impl Builder {
146
146
}
147
147
148
148
let ( renderer, window, mut factory) = Renderer :: new ( builder, context, & event_loop, & source_set) ;
149
+ let dpi = window. get_hidpi_factor ( ) ;
149
150
let scene = factory. scene ( ) ;
150
151
Window {
151
152
event_loop,
152
153
window,
154
+ dpi,
153
155
input : Input :: new ( ) ,
154
156
renderer,
155
157
factory,
@@ -168,7 +170,7 @@ impl Window {
168
170
/// Create new `Builder` with standard parameters.
169
171
pub fn builder < T : Into < String > > ( title : T ) -> Builder {
170
172
Builder {
171
- dimensions : ( 1024 , 768 ) ,
173
+ dimensions : glutin :: dpi :: LogicalSize :: new ( 1024.0 , 768.0 ) ,
172
174
fullscreen : false ,
173
175
multisampling : 0 ,
174
176
shader_directory : None ,
@@ -188,30 +190,30 @@ impl Window {
188
190
189
191
self . window . swap_buffers ( ) . unwrap ( ) ;
190
192
let window = & self . window ;
193
+ let dpi = self . dpi ;
191
194
192
195
self . event_loop . poll_events ( |event| {
193
- use glutin:: WindowEvent :: { Closed , Focused , KeyboardInput , MouseInput , CursorMoved , MouseWheel , Resized } ;
196
+ use glutin:: WindowEvent ;
194
197
match event {
195
198
glutin:: Event :: WindowEvent { event, .. } => match event {
196
- Resized ( ..) => renderer. resize ( window) ,
197
- Focused ( state) => input. window_focus ( state) ,
198
- Closed => running = false ,
199
- KeyboardInput {
199
+ WindowEvent :: Resized ( size) => renderer. resize ( window, size) ,
200
+ WindowEvent :: HiDpiFactorChanged ( dpi) => renderer. dpi_change ( window, dpi) ,
201
+ WindowEvent :: Focused ( state) => input. window_focus ( state) ,
202
+ WindowEvent :: CloseRequested | WindowEvent :: Destroyed => running = false ,
203
+ WindowEvent :: KeyboardInput {
200
204
input : glutin:: KeyboardInput {
201
205
state,
202
206
virtual_keycode : Some ( keycode) ,
203
207
..
204
208
} ,
205
209
..
206
210
} => input. keyboard_input ( state, keycode) ,
207
- MouseInput { state, button, .. } => input. mouse_input ( state, button) ,
208
- CursorMoved {
209
- position : ( x, y) , ..
210
- } => input. mouse_moved (
211
- [ x as f32 , y as f32 ] . into ( ) ,
212
- renderer. map_to_ndc ( [ x as f32 , y as f32 ] ) ,
213
- ) ,
214
- MouseWheel { delta, .. } => input. mouse_wheel_input ( delta) ,
211
+ WindowEvent :: MouseInput { state, button, .. } => input. mouse_input ( state, button) ,
212
+ WindowEvent :: CursorMoved { position, .. } => {
213
+ let pos = position. to_physical ( dpi) ;
214
+ input. mouse_moved ( [ pos. x as f32 , pos. y as f32 ] . into ( ) , renderer. map_to_ndc ( [ pos. x as f32 , pos. y as f32 ] ) ) ;
215
+ }
216
+ WindowEvent :: MouseWheel { delta, .. } => input. mouse_wheel_input ( delta) ,
215
217
_ => { }
216
218
} ,
217
219
glutin:: Event :: DeviceEvent { event, .. } => match event {
@@ -239,24 +241,9 @@ impl Window {
239
241
pub fn size ( & self ) -> mint:: Vector2 < f32 > {
240
242
let size = self . window
241
243
. get_inner_size ( )
242
- . expect ( "Can't get window size" ) ;
243
- [ size. 0 as f32 , size. 1 as f32 ] . into ( )
244
- }
245
-
246
- /// Sets how the cursor should be handled.
247
- ///
248
- /// See the documentation for [`CursorState`] for the possible cursor states.
249
- ///
250
- /// Note that if you use [`CursorState::Grab`], you should use [`Input::mouse_delta_raw`] for
251
- /// detecting mouse movement, as [`Input::mouse_delta`] will only report movement of the cursor
252
- /// within the window.
253
- ///
254
- /// [`CursorState`]: enum.CursorState.html
255
- /// [`CursorState::Grab`]: enum.CursorState.html#variant.Grab
256
- /// [`Input::mouse_delta_raw`]: struct.Input.html#method.mouse_delta_raw
257
- /// [`Input::mouse_delta`]: struct.Input.html#method.mouse_delta_raw
258
- pub fn set_cursor_state ( & self , state : CursorState ) {
259
- let _ = self . window . set_cursor_state ( state) ;
244
+ . expect ( "Can't get window size" )
245
+ . to_physical ( self . dpi ) ;
246
+ [ size. width as f32 , size. height as f32 ] . into ( )
260
247
}
261
248
262
249
/// Returns underlaying `glutin::GlWindow`.
0 commit comments