//! A winit app on Pinnacle (`ports/winit-*`): the window the display server //! gives, drawn into directly (a gradient), and a trail where the pointer is //! dragged. It says on the console what it was given and did, for //! `cargo xtask winit-demo` to wait for. use winit::application::ApplicationHandler; use winit::event::{ElementState, MouseButton, WindowEvent}; use winit::event_loop::{ActiveEventLoop, EventLoop}; use winit::platform::pinnacle::WindowExtPinnacle; use winit::window::{Window, WindowId}; #[derive(Default)] struct Demo { window: Option, pressed: bool, last: Option<(i32, i32)>, dots: usize, } fn background(pixels: &mut [u32], width: u32, height: u32) { for y in 0..height { for x in 0..width { let r = 40 + x * 120 / width.max(1); let g = 60 + y * 100 / height.max(1); let b = 140; pixels[(y * width + x) as usize] = 0xff00_0000 | r << 16 | g << 8 | b; } } // A band across the middle, so the window's extent is plain to see. let band = height / 2..height / 2 + height / 20; for y in band { for x in width / 10..width * 9 / 10 { pixels[(y * width + x) as usize] = 0xfff5_f0e6; } } } impl Demo { /// A round dot at `(x, y)`, and the rectangle it changed. fn dot(&mut self, x: i32, y: i32) { let Some(window) = &self.window else { return }; let r = 10; window.with_pixels(|pixels, width, height| { for dy in -r..=r { for dx in -r..=r { let (px, py) = (x + dx, y + dy); if dx * dx + dy * dy <= r * r && px >= 0 && py >= 0 && (px as u32) < width && (py as u32) < height { pixels[(py as u32 * width + px as u32) as usize] = 0xffff_c83c; } } } }); window.present(x - r, y - r, 2 * r + 1, 2 * r + 1); self.dots += 1; } } impl ApplicationHandler for Demo { fn resumed(&mut self, event_loop: &ActiveEventLoop) { if self.window.is_none() { let attributes = Window::default_attributes().with_title("Winit"); self.window = Some(event_loop.create_window(attributes).expect("no window")); } } fn window_event(&mut self, event_loop: &ActiveEventLoop, _: WindowId, event: WindowEvent) { match event { WindowEvent::Resized(size) => println!("winit: a window of {}x{}", size.width, size.height), WindowEvent::RedrawRequested => { let window = self.window.as_ref().unwrap(); let (width, height) = window.with_pixels(|pixels, width, height| { background(pixels, width, height); (width, height) }); window.present(0, 0, width as i32, height as i32); println!("winit: drawn"); }, WindowEvent::MouseInput { state, button: MouseButton::Left, .. } => { self.pressed = state == ElementState::Pressed; if self.pressed { if let Some((x, y)) = self.last { self.dot(x, y); } } else { println!("winit: a trail of {} dots", self.dots); } }, WindowEvent::CursorMoved { position, .. } => { let at = (position.x as i32, position.y as i32); // Between this and the last point too, so a fast drag is a line. if self.pressed { if let Some((lx, ly)) = self.last { let steps = ((at.0 - lx).abs().max((at.1 - ly).abs()) / 6).max(1); for i in 1..=steps { self.dot(lx + (at.0 - lx) * i / steps, ly + (at.1 - ly) * i / steps); } } } self.last = Some(at); }, WindowEvent::CloseRequested => event_loop.exit(), _ => {}, } } } fn main() { let event_loop = EventLoop::new().expect("no event loop"); event_loop.run_app(&mut Demo::default()).expect("the event loop failed"); }