Skip to content

Commit 02d60e7

Browse files
authored
libprocessing/issues/127
1 parent d27eb85 commit 02d60e7

5 files changed

Lines changed: 68 additions & 11 deletions

File tree

crates/processing_glfw/src/lib.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,14 @@ impl GlfwContext {
300300
WindowEvent::Focus(focused) => {
301301
input_set_focus(surface, focused).unwrap();
302302
}
303+
WindowEvent::Size(width, height) => {
304+
processing_render::surface_resize(
305+
surface,
306+
width.max(1) as u32,
307+
height.max(1) as u32,
308+
)
309+
.unwrap();
310+
}
303311
_ => {}
304312
}
305313
}
@@ -355,6 +363,9 @@ impl GlfwContext {
355363
Ok(())
356364
});
357365
self.last_applied.position = frame_pos;
366+
367+
let (w, h) = self.window.get_size();
368+
self.last_applied.size = bevy::math::UVec2::new(w.max(0) as u32, h.max(0) as u32);
358369
}
359370

360371
#[cfg(not(feature = "wayland"))]
@@ -529,8 +540,8 @@ fn read_desired_window(surface: Entity) -> Option<DesiredWindow> {
529540
_ => None,
530541
},
531542
size: bevy::math::UVec2::new(
532-
window.resolution.physical_width(),
533-
window.resolution.physical_height(),
543+
window.resolution.width() as u32,
544+
window.resolution.height() as u32,
534545
),
535546
visible: window.visible,
536547
resizable: window.resizable,

crates/processing_pyo3/src/lib.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,12 @@ pub(crate) fn reset_tracked_globals() {
120120
fn sync_globals(module: &Bound<'_, PyModule>, globals: &Bound<'_, PyAny>) -> PyResult<()> {
121121
let graphics =
122122
get_graphics(module)?.ok_or_else(|| PyRuntimeError::new_err("call size() first"))?;
123-
input::sync_globals(
124-
globals,
125-
graphics.surface.entity,
126-
graphics.width,
127-
graphics.height,
128-
)?;
129-
surface::sync_globals(globals, &graphics.surface, graphics.width, graphics.height)?;
123+
let width = ::processing::prelude::surface_width(graphics.surface.entity)
124+
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
125+
let height = ::processing::prelude::surface_height(graphics.surface.entity)
126+
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
127+
input::sync_globals(globals, graphics.surface.entity, width, height)?;
128+
surface::sync_globals(globals, &graphics.surface, width, height)?;
130129
time::sync_globals(globals)?;
131130
Ok(())
132131
}

crates/processing_render/src/graphics.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ impl CameraProjection for ProcessingProjection {
118118
// this gets called with the render target's physical dimensions (i.e. accounting for
119119
// scale factor), but our projection is in logical pixel units
120120
// TODO: handle resizes?
121+
self.width = _width;
122+
self.height = _height;
121123
}
122124

123125
fn far(&self) -> f32 {

crates/processing_render/src/lib.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,6 +1702,24 @@ pub fn surface_physical_height(entity: Entity) -> error::Result<u32> {
17021702
})
17031703
}
17041704

1705+
pub fn surface_width(entity: Entity) -> error::Result<u32> {
1706+
app_mut(|app| {
1707+
Ok(app
1708+
.world_mut()
1709+
.run_system_cached_with(surface::width, entity)
1710+
.unwrap())
1711+
})
1712+
}
1713+
1714+
pub fn surface_height(entity: Entity) -> error::Result<u32> {
1715+
app_mut(|app| {
1716+
Ok(app
1717+
.world_mut()
1718+
.run_system_cached_with(surface::height, entity)
1719+
.unwrap())
1720+
})
1721+
}
1722+
17051723
pub fn monitor_list() -> error::Result<Vec<Entity>> {
17061724
app_mut(|app| Ok(app.world_mut().run_system_cached(monitor::list).unwrap()))
17071725
}

crates/processing_render/src/surface.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@
2121
use bevy::{
2222
app::{App, Plugin},
2323
asset::Assets,
24+
camera::{Projection, RenderTarget},
2425
ecs::query::QueryEntityError,
2526
math::{IRect, IVec2},
2627
prelude::{Commands, Component, Entity, In, Query, ResMut, Window, With, default},
2728
render::render_resource::{Extent3d, TextureFormat},
2829
window::{
2930
CompositeAlphaMode, Monitor, RawHandleWrapper, WindowLevel, WindowMode, WindowPosition,
30-
WindowResolution, WindowWrapper,
31+
WindowRef, WindowResolution, WindowWrapper,
3132
},
3233
};
3334
use raw_window_handle::{
@@ -39,7 +40,7 @@ use processing_core::error::{self, ProcessingError, Result};
3940
#[cfg(not(target_os = "windows"))]
4041
use std::ptr::NonNull;
4142

42-
use crate::image::Image;
43+
use crate::{graphics::SurfaceSize, image::Image};
4344

4445
#[derive(Component, Debug, Clone)]
4546
pub struct Surface;
@@ -394,6 +395,7 @@ pub fn destroy(
394395
pub fn resize(
395396
In((window_entity, width, height)): In<(Entity, u32, u32)>,
396397
mut windows: Query<&mut Window>,
398+
mut graphics_query: Query<(&RenderTarget, &mut SurfaceSize, &mut Projection)>,
397399
) -> Result<()> {
398400
if let Ok(mut window) = windows.get_mut(window_entity) {
399401
let scale = window.resolution.scale_factor();
@@ -403,6 +405,17 @@ pub fn resize(
403405
.resolution
404406
.set_physical_resolution(physical_w, physical_h);
405407
}
408+
409+
for (target, mut surface_size, mut projection) in graphics_query.iter_mut() {
410+
if let RenderTarget::Window(WindowRef::Entity(surface)) = *target {
411+
if surface == window_entity {
412+
*surface_size = SurfaceSize(width, height);
413+
if let Projection::Custom(ref mut custom) = *projection {
414+
custom.update(width as f32, height as f32);
415+
}
416+
}
417+
}
418+
}
406419
Ok(())
407420
}
408421

@@ -448,6 +461,20 @@ pub fn physical_height(In(entity): In<Entity>, query: Query<&Window>) -> u32 {
448461
.unwrap_or(0)
449462
}
450463

464+
pub fn width(In(entity): In<Entity>, query: Query<&Window>) -> u32 {
465+
query
466+
.get(entity)
467+
.map(|w| w.resolution.width() as u32)
468+
.unwrap_or(0)
469+
}
470+
471+
pub fn height(In(entity): In<Entity>, query: Query<&Window>) -> u32 {
472+
query
473+
.get(entity)
474+
.map(|w| w.resolution.height() as u32)
475+
.unwrap_or(0)
476+
}
477+
451478
pub fn set_title(
452479
In((entity, title)): In<(Entity, String)>,
453480
mut windows: Query<&mut Window>,

0 commit comments

Comments
 (0)