Skip to content

Commit 524a361

Browse files
committed
Update glutin and gfx_window_glutin versions
1 parent 863bd87 commit 524a361

File tree

5 files changed

+52
-55
lines changed

5 files changed

+52
-55
lines changed

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ vec_map = "0.8"
4444

4545
# OpenGL
4646
gfx_device_gl = { version = "0.15", optional = true }
47-
gfx_window_glutin = { version = "0.20", optional = true }
48-
glutin = { version = "0.12", optional = true }
47+
gfx_window_glutin = { version = "0.28", optional = true }
48+
glutin = { version = "0.19", optional = true }
4949

5050
[dev-dependencies]
5151
env_logger = "0.6"

src/input/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ impl Input {
245245
) {
246246
self.delta.mouse_wheel.push(match delta {
247247
MouseScrollDelta::LineDelta(_, y) => y * PIXELS_PER_LINE,
248-
MouseScrollDelta::PixelDelta(_, y) => y,
248+
MouseScrollDelta::PixelDelta(delta) => delta.y as f32,
249249
});
250250
}
251251

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -358,4 +358,4 @@ pub use texture::{CubeMap, CubeMapPath, FilterMethod, Sampler, Texture, WrapMode
358358

359359
#[cfg(feature = "opengl")]
360360
#[doc(inline)]
361-
pub use window::{CursorState, Window};
361+
pub use window::Window;

src/render/mod.rs

+20-10
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,8 @@ pub struct Renderer {
526526
map_default: Texture<[f32; 4]>,
527527
shadow_default: Texture<f32>,
528528
debug_quads: froggy::Storage<DebugQuad>,
529-
size: (u32, u32),
529+
size: glutin::dpi::LogicalSize,
530+
dpi: f64,
530531
font_cache: HashMap<String, Font>,
531532
instance_cache: HashMap<InstanceCacheKey, InstanceData>,
532533
/// `ShadowType` of this `Renderer`.
@@ -543,7 +544,7 @@ impl Renderer {
543544
) -> (Self, glutin::GlWindow, Factory) {
544545
use gfx::texture as t;
545546

546-
let (window, device, mut gl_factory, out_color, out_depth) = gfx_window_glutin::init(builder, context, event_loop);
547+
let (window, device, mut gl_factory, out_color, out_depth) = gfx_window_glutin::init(builder, context, event_loop).unwrap();
547548
let (_, srv_white) = gl_factory
548549
.create_texture_immutable::<gfx::format::Rgba8>(
549550
t::Kind::D2(1, 1, t::AaMode::Single),
@@ -634,6 +635,7 @@ impl Renderer {
634635
debug_quads: froggy::Storage::new(),
635636
font_cache: HashMap::new(),
636637
size: window.get_inner_size().unwrap(),
638+
dpi: window.get_hidpi_factor(),
637639
};
638640
let factory = Factory::new(gl_factory);
639641
(renderer, window, factory)
@@ -650,22 +652,30 @@ impl Renderer {
650652
pub(crate) fn resize(
651653
&mut self,
652654
window: &glutin::GlWindow,
655+
size: glutin::dpi::LogicalSize,
653656
) {
654-
let size = window.get_inner_size().unwrap();
655-
656657
// skip updating view and self size if some
657658
// of the sides equals to zero (fixes crash on minimize on Windows machines)
658-
if size.0 == 0 || size.1 == 0 {
659+
if size.width == 0.0 || size.height == 0.0 {
659660
return;
660661
}
661662

662663
self.size = size;
663664
gfx_window_glutin::update_views(window, &mut self.out_color, &mut self.out_depth);
664665
}
665666

667+
pub(crate) fn dpi_change(
668+
&mut self,
669+
window: &glutin::GlWindow,
670+
dpi: f64,
671+
) {
672+
self.dpi = dpi;
673+
gfx_window_glutin::update_views(window, &mut self.out_color, &mut self.out_depth);
674+
}
675+
666676
/// Returns current viewport aspect ratio, i.e. width / height.
667677
pub fn aspect_ratio(&self) -> f32 {
668-
self.size.0 as f32 / self.size.1 as f32
678+
self.size.to_physical(self.dpi).width as f32 / self.size.to_physical(self.dpi).height as f32
669679
}
670680

671681
/// Map screen pixel coordinates to Normalized Display Coordinates.
@@ -677,8 +687,8 @@ impl Renderer {
677687
) -> mint::Point2<f32> {
678688
let point = point.into();
679689
mint::Point2 {
680-
x: 2.0 * point.x / self.size.0 as f32 - 1.0,
681-
y: 1.0 - 2.0 * point.y / self.size.1 as f32,
690+
x: 2.0 * point.x / self.size.to_physical(self.dpi).width as f32 - 1.0,
691+
y: 1.0 - 2.0 * point.y / self.size.to_physical(self.dpi).height as f32,
682692
}
683693
}
684694

@@ -1113,12 +1123,12 @@ impl Renderer {
11131123
if quad.pos[0] >= 0 {
11141124
quad.pos[0]
11151125
} else {
1116-
self.size.0 as i32 + quad.pos[0] - quad.size[0]
1126+
self.size.to_physical(self.dpi).width as i32 + quad.pos[0] - quad.size[0]
11171127
},
11181128
if quad.pos[1] >= 0 {
11191129
quad.pos[1]
11201130
} else {
1121-
self.size.1 as i32 + quad.pos[1] - quad.size[1]
1131+
self.size.to_physical(self.dpi).height as i32 + quad.pos[1] - quad.size[1]
11221132
},
11231133
];
11241134
let p0 = self.map_to_ndc([pos[0] as f32, pos[1] as f32]);

src/window.rs

+28-41
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! Primitives for creating and controlling [`Window`](struct.Window.html).
22
33
use glutin;
4-
use glutin::GlContext;
54
use mint;
65
use render;
76

@@ -12,15 +11,14 @@ use render::Renderer;
1211
use scene::Scene;
1312
use std::path::PathBuf;
1413

15-
pub use glutin::CursorState;
16-
1714
/// `Window` is the core entity of every `three-rs` application.
1815
///
1916
/// It provides [user input](struct.Window.html#method.update),
2017
/// [`Factory`](struct.Factory.html) and [`Renderer`](struct.Renderer.html).
2118
pub struct Window {
2219
event_loop: glutin::EventsLoop,
2320
window: glutin::GlWindow,
21+
dpi: f64,
2422
/// See [`Input`](struct.Input.html).
2523
pub input: Input,
2624
/// See [`Renderer`](struct.Renderer.html).
@@ -38,7 +36,7 @@ pub struct Window {
3836
/// Builder for creating new [`Window`](struct.Window.html) with desired parameters.
3937
#[derive(Debug, Clone)]
4038
pub struct Builder {
41-
dimensions: (u32, u32),
39+
dimensions: glutin::dpi::LogicalSize,
4240
fullscreen: bool,
4341
multisampling: u16,
4442
shader_directory: Option<PathBuf>,
@@ -47,13 +45,15 @@ pub struct Builder {
4745
}
4846

4947
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.
5151
pub fn dimensions(
5252
&mut self,
53-
width: u32,
54-
height: u32,
53+
width: f64,
54+
height: f64,
5555
) -> &mut Self {
56-
self.dimensions = (width, height);
56+
self.dimensions = glutin::dpi::LogicalSize::new(width, height);
5757
self
5858
}
5959

@@ -105,7 +105,7 @@ impl Builder {
105105

106106
let builder = glutin::WindowBuilder::new()
107107
.with_fullscreen(monitor_id)
108-
.with_dimensions(self.dimensions.0, self.dimensions.1)
108+
.with_dimensions(self.dimensions)
109109
.with_title(self.title.clone());
110110

111111
let context = glutin::ContextBuilder::new()
@@ -146,10 +146,12 @@ impl Builder {
146146
}
147147

148148
let (renderer, window, mut factory) = Renderer::new(builder, context, &event_loop, &source_set);
149+
let dpi = window.get_hidpi_factor();
149150
let scene = factory.scene();
150151
Window {
151152
event_loop,
152153
window,
154+
dpi,
153155
input: Input::new(),
154156
renderer,
155157
factory,
@@ -168,7 +170,7 @@ impl Window {
168170
/// Create new `Builder` with standard parameters.
169171
pub fn builder<T: Into<String>>(title: T) -> Builder {
170172
Builder {
171-
dimensions: (1024, 768),
173+
dimensions: glutin::dpi::LogicalSize::new(1024.0, 768.0),
172174
fullscreen: false,
173175
multisampling: 0,
174176
shader_directory: None,
@@ -188,30 +190,30 @@ impl Window {
188190

189191
self.window.swap_buffers().unwrap();
190192
let window = &self.window;
193+
let dpi = self.dpi;
191194

192195
self.event_loop.poll_events(|event| {
193-
use glutin::WindowEvent::{Closed, Focused, KeyboardInput, MouseInput, CursorMoved, MouseWheel, Resized};
196+
use glutin::WindowEvent;
194197
match event {
195198
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 {
200204
input: glutin::KeyboardInput {
201205
state,
202206
virtual_keycode: Some(keycode),
203207
..
204208
},
205209
..
206210
} => 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),
215217
_ => {}
216218
},
217219
glutin::Event::DeviceEvent { event, .. } => match event {
@@ -239,24 +241,9 @@ impl Window {
239241
pub fn size(&self) -> mint::Vector2<f32> {
240242
let size = self.window
241243
.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()
260247
}
261248

262249
/// Returns underlaying `glutin::GlWindow`.

0 commit comments

Comments
 (0)