|
| 1 | +use baseview::dpi::PhysicalPosition; |
| 2 | +use baseview::{Event, EventStatus, MouseEvent, WindowContext, WindowHandler, WindowSize}; |
| 3 | +use std::cell::{Cell, RefCell}; |
| 4 | +use std::num::NonZeroU32; |
| 5 | + |
| 6 | +pub struct OpenWindowExample { |
| 7 | + window_context: WindowContext, |
| 8 | + |
| 9 | + surface: RefCell<softbuffer::Surface<WindowContext, WindowContext>>, |
| 10 | + mouse_pos: Cell<PhysicalPosition<f64>>, |
| 11 | + is_cursor_inside: Cell<bool>, |
| 12 | + damaged: Cell<bool>, |
| 13 | +} |
| 14 | + |
| 15 | +impl WindowHandler for OpenWindowExample { |
| 16 | + fn resized(&self, new_size: WindowSize) { |
| 17 | + println!("Resized: {new_size:?}"); |
| 18 | + |
| 19 | + if let (Some(width), Some(height)) = |
| 20 | + (NonZeroU32::new(new_size.physical.width), NonZeroU32::new(new_size.physical.height)) |
| 21 | + { |
| 22 | + self.surface.borrow_mut().resize(width, height).unwrap(); |
| 23 | + self.damaged.set(true); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + fn on_frame(&self) { |
| 28 | + if !self.damaged.get() { |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + let mut surface = self.surface.borrow_mut(); |
| 33 | + let mut pixels = surface.buffer_mut().unwrap(); |
| 34 | + let size = self.window_context.size(); |
| 35 | + let scale_factor = self.window_context.scale_factor(); |
| 36 | + let (width, height) = (size.physical.width, size.physical.height); |
| 37 | + |
| 38 | + for index in 0..(width * height) { |
| 39 | + let x = index % width; |
| 40 | + let y = index / width; |
| 41 | + |
| 42 | + let red = ((x as f32 / width as f32) * 255.0) as u32; |
| 43 | + let green = ((y as f32 / height as f32) * 255.0) as u32; |
| 44 | + let blue = (((x * y) as f64 / scale_factor) as u32) % 255; |
| 45 | + |
| 46 | + pixels[index as usize] = blue | (green << 8) | (red << 16) | 0xFF000000; |
| 47 | + } |
| 48 | + |
| 49 | + for x in 0..width { |
| 50 | + // Green line on top |
| 51 | + let y = 0; |
| 52 | + let index = y * width + x; |
| 53 | + pixels[index as usize] = if (x % 10) < 5 { 0xFF00FF00 } else { 0xFF000000 }; |
| 54 | + |
| 55 | + // Magenta line on bottom |
| 56 | + let y = height - 1; |
| 57 | + let index = y * width + x; |
| 58 | + pixels[index as usize] = if (x % 10) < 5 { 0xFFFF00FF } else { 0xFF000000 }; |
| 59 | + } |
| 60 | + |
| 61 | + for y in 0..height { |
| 62 | + // Green line on right |
| 63 | + let x = width - 1; |
| 64 | + let index = y * width + x; |
| 65 | + pixels[index as usize] = if (y % 10) < 5 { 0xFF00FF00 } else { 0xFF000000 }; |
| 66 | + |
| 67 | + // Magenta line on left |
| 68 | + let x = 0; |
| 69 | + let index = y * width + x; |
| 70 | + pixels[index as usize] = if (y % 10) < 5 { 0xFFFF00FF } else { 0xFF000000 }; |
| 71 | + } |
| 72 | + |
| 73 | + if self.is_cursor_inside.get() { |
| 74 | + let rect_size = (25.0 * scale_factor) as i32; |
| 75 | + let mouse_pos = self.mouse_pos.get().cast::<i32>(); |
| 76 | + |
| 77 | + let rect_x_start = (mouse_pos.x - rect_size).clamp(0, width as i32) as u32; |
| 78 | + let rect_x_end = (mouse_pos.x + rect_size).clamp(0, width as i32) as u32; |
| 79 | + let rect_y_start = (mouse_pos.y - rect_size).clamp(0, height as i32) as u32; |
| 80 | + let rect_y_end = (mouse_pos.y + rect_size).clamp(0, height as i32) as u32; |
| 81 | + |
| 82 | + for x in rect_x_start..rect_x_end { |
| 83 | + for y in rect_y_start..rect_y_end { |
| 84 | + let index = y * width + x; |
| 85 | + pixels[index as usize] = 0xFF00FF00; |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + pixels.present().unwrap(); |
| 91 | + self.damaged.set(false); |
| 92 | + } |
| 93 | + |
| 94 | + fn on_event(&self, event: Event) -> EventStatus { |
| 95 | + match event { |
| 96 | + Event::Mouse(MouseEvent::CursorMoved { position, .. }) => { |
| 97 | + self.mouse_pos.set(position); |
| 98 | + self.damaged.set(true); |
| 99 | + } |
| 100 | + Event::Mouse(MouseEvent::CursorEntered) => { |
| 101 | + self.is_cursor_inside.set(true); |
| 102 | + self.damaged.set(true); |
| 103 | + } |
| 104 | + Event::Mouse(MouseEvent::CursorLeft) => { |
| 105 | + self.is_cursor_inside.set(false); |
| 106 | + self.damaged.set(true); |
| 107 | + } |
| 108 | + _ => {} |
| 109 | + } |
| 110 | + |
| 111 | + log_event(&event); |
| 112 | + |
| 113 | + EventStatus::Captured |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +impl OpenWindowExample { |
| 118 | + pub fn new(window: WindowContext) -> Self { |
| 119 | + let ctx = softbuffer::Context::new(window.clone()).unwrap(); |
| 120 | + let mut surface = softbuffer::Surface::new(&ctx, window.clone()).unwrap(); |
| 121 | + let size = window.size().physical; |
| 122 | + surface |
| 123 | + .resize(NonZeroU32::new(size.width).unwrap(), NonZeroU32::new(size.height).unwrap()) |
| 124 | + .unwrap(); |
| 125 | + |
| 126 | + OpenWindowExample { |
| 127 | + window_context: window, |
| 128 | + surface: surface.into(), |
| 129 | + mouse_pos: PhysicalPosition::new(0., 0.).into(), |
| 130 | + is_cursor_inside: false.into(), |
| 131 | + damaged: true.into(), |
| 132 | + } |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +fn log_event(event: &Event) { |
| 137 | + match event { |
| 138 | + Event::Mouse(e) => println!("Mouse event: {:?}", e), |
| 139 | + Event::Keyboard(e) => println!("Keyboard event: {:?}", e), |
| 140 | + Event::Window(e) => println!("Window event: {:?}", e), |
| 141 | + _ => {} |
| 142 | + } |
| 143 | +} |
0 commit comments