vk_graph_window/frame.rs
1use {
2 vk_graph::{Graph, driver::device::Device, node::SwapchainImageNode},
3 winit::{dpi::PhysicalPosition, event::Event, window::Window},
4};
5
6/// Centers the mouse cursor within the window.
7pub fn center_cursor(window: &Window) {
8 let window_size = window.inner_size();
9 let x = window_size.width / 2;
10 let y = window_size.height / 2;
11 set_cursor_position(window, x, y);
12}
13
14/// Sets the mouse cursor at the specified position within the window.
15pub fn set_cursor_position(window: &Window, x: u32, y: u32) {
16 let position = PhysicalPosition::new(x as i32, y as i32);
17 window.set_cursor_position(position).unwrap_or_default();
18}
19
20/// A request to render a single frame to the provided graph.
21pub struct FrameContext<'a> {
22 /// The device this frame belongs to.
23 pub device: &'a Device,
24
25 /// A slice of events that have occurred since the previous frame.
26 pub events: &'a [Event<()>],
27
28 /// The height, in pixels, of the current frame.
29 pub height: u32,
30
31 /// A graph which rendering commands should be recorded into.
32 ///
33 /// Make sure to write to `swapchain_image` as part of this graph.
34 pub graph: &'a mut Graph,
35
36 /// A pre-bound image node for the swapchain image to be drawn.
37 pub swapchain_image: SwapchainImageNode,
38
39 /// A mutable `bool` which indicates if this frame should cause the program to exit.
40 pub will_exit: &'a mut bool,
41
42 /// The width, in pixels, of the current frame.
43 pub width: u32,
44
45 /// A borrow of the operating system window relating to this frame.
46 pub window: &'a Window,
47}
48
49impl FrameContext<'_> {
50 /// Causes the program to exit before displaying the current frame.
51 pub fn exit(&mut self) {
52 *self.will_exit = true;
53 }
54
55 /// Returns the frame width divided by the frame height.
56 pub fn render_aspect_ratio(&self) -> f32 {
57 self.width as f32 / self.height as f32
58 }
59
60 /// Centers the mouse cursor within the window.
61 pub fn center_cursor(&self) {
62 center_cursor(self.window);
63 }
64
65 /// Sets the mouse cursor at the specified position within the window.
66 pub fn set_cursor_position(&self, x: u32, y: u32) {
67 set_cursor_position(self.window, x, y);
68 }
69}