I'm a beginner, so there may be mistakes in the code I wrote.
What I'm trying to do is show a window containing a drawing made by
tiny-skia, and when I move the mouse cursor inside the window, the cursor coordinates are printed.
When I run this code, the graphic appears in the window, but the mouse cursor coordinates are not printed
use softbuffer::{Context, Surface};
use std::num::NonZeroU32;
use tiny_skia::{Color, FillRule, Paint, PathBuilder, Pixmap, Stroke, Transform};
use winit::dpi::{PhysicalPosition, PhysicalSize};
use winit::event::{ElementState, DeviceEvent, Event, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::WindowBuilder;
fn main() {
let event_loop = EventLoop::new();
let window = WindowBuilder::new().build(&event_loop).unwrap();
let context = unsafe { Context::new(&window) }.unwrap();
let mut surface = unsafe { Surface::new(&context, &window) }.unwrap();
let mut cursor_position = PhysicalPosition::new(0.0, 0.0);
event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;
match event {
Event::RedrawRequested(window_id) if window_id == window.id() => {
let (width, height) = {
let size = window.inner_size();
(size.width, size.height)
};
surface
.resize(
NonZeroU32::new(width).unwrap(),
NonZeroU32::new(height).unwrap(),
)
.unwrap();
let mut pixmap = Pixmap::new(width, height).unwrap();
pixmap.fill(Color::WHITE);
let path = PathBuilder::from_circle(
(width / 2) as f32,
(height / 2) as f32,
(width.min(height) / 2) as f32,
)
.unwrap();
let mut paint = Paint::default();
paint.set_color_rgba8(0, 128, 128, 255);
pixmap.fill_path(
&path,
&paint,
FillRule::EvenOdd,
Transform::identity(),
None,
);
paint.set_color_rgba8(255, 0, 0, 255);
let mut stroke = Stroke::default();
stroke.width = 10.0;
pixmap.stroke_path(&path, &paint, &stroke, Transform::identity(), None);
let mut buffer = surface.buffer_mut().unwrap();
for index in 0..(width * height) as usize {
buffer[index] = pixmap.data()[index * 4 + 2] as u32
| (pixmap.data()[index * 4 + 1] as u32) << 8
| (pixmap.data()[index * 4] as u32) << 16;
}
buffer.present().unwrap();
}_ => {}
Event::WindowEvent { event, .. } => match event {
WindowEvent::CloseRequested => control_flow.set_exit(),
WindowEvent::CursorMoved { position, device_id, modifiers } => println!("mouse moved: {position:?}"),
_ => {}
}_ => {}
}
});
}
When I execute this code, a window will appear without the graphic, but the mouse cursor coordinates inside the window are printed
use softbuffer::{Context, Surface};
use std::num::NonZeroU32;
use tiny_skia::{Color, FillRule, Paint, PathBuilder, Pixmap, Stroke, Transform};
use winit::dpi::{PhysicalPosition, PhysicalSize};
use winit::event::{ElementState, DeviceEvent, Event, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::WindowBuilder;
fn main() {
let event_loop = EventLoop::new();
let window = WindowBuilder::new().build(&event_loop).unwrap();
let context = unsafe { Context::new(&window) }.unwrap();
let mut surface = unsafe { Surface::new(&context, &window) }.unwrap();
let mut cursor_position = PhysicalPosition::new(0.0, 0.0);
event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;
match event {
Event::WindowEvent { event, .. } => match event {
WindowEvent::CloseRequested => control_flow.set_exit(),
WindowEvent::CursorMoved { position, device_id, modifiers } => println!("mouse moved: {position:?}"),
_ => {}
}_ => {}
Event::RedrawRequested(window_id) if window_id == window.id() => {
let (width, height) = {
let size = window.inner_size();
(size.width, size.height)
};
surface
.resize(
NonZeroU32::new(width).unwrap(),
NonZeroU32::new(height).unwrap(),
)
.unwrap();
let mut pixmap = Pixmap::new(width, height).unwrap();
pixmap.fill(Color::WHITE);
let path = PathBuilder::from_circle(
(width / 2) as f32,
(height / 2) as f32,
(width.min(height) / 2) as f32,
)
.unwrap();
let mut paint = Paint::default();
paint.set_color_rgba8(0, 128, 128, 255);
pixmap.fill_path(
&path,
&paint,
FillRule::EvenOdd,
Transform::identity(),
None,
);
paint.set_color_rgba8(255, 0, 0, 255);
let mut stroke = Stroke::default();
stroke.width = 10.0;
pixmap.stroke_path(&path, &paint, &stroke, Transform::identity(), None);
let mut buffer = surface.buffer_mut().unwrap();
for index in 0..(width * height) as usize {
buffer[index] = pixmap.data()[index * 4 + 2] as u32
| (pixmap.data()[index * 4 + 1] as u32) << 8
| (pixmap.data()[index * 4] as u32) << 16;
}
buffer.present().unwrap();
}_ => {}
}
});
}