|
| 1 | +extern crate env_logger; |
| 2 | +extern crate winit; |
| 3 | + |
| 4 | +use std::{collections::HashMap, sync::mpsc, thread, time::Duration}; |
| 5 | + |
| 6 | +use winit::{ |
| 7 | + event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent}, |
| 8 | + event_loop::{ControlFlow, EventLoop}, window::{MouseCursor, WindowBuilder}, |
| 9 | +}; |
| 10 | + |
| 11 | +const WINDOW_COUNT: usize = 3; |
| 12 | +const WINDOW_SIZE: (u32, u32) = (600, 400); |
| 13 | + |
| 14 | +fn main() { |
| 15 | + env_logger::init(); |
| 16 | + let event_loop = EventLoop::new(); |
| 17 | + let mut window_senders = HashMap::with_capacity(WINDOW_COUNT); |
| 18 | + for _ in 0..WINDOW_COUNT { |
| 19 | + let window = WindowBuilder::new() |
| 20 | + .with_dimensions(WINDOW_SIZE.into()) |
| 21 | + .build(&event_loop) |
| 22 | + .unwrap(); |
| 23 | + let (tx, rx) = mpsc::channel(); |
| 24 | + window_senders.insert(window.id(), tx); |
| 25 | + thread::spawn(move || { |
| 26 | + while let Ok(event) = rx.recv() { |
| 27 | + match event { |
| 28 | + WindowEvent::KeyboardInput { input: KeyboardInput { |
| 29 | + state: ElementState::Released, |
| 30 | + virtual_keycode: Some(key), |
| 31 | + modifiers, |
| 32 | + .. |
| 33 | + }, .. } => { |
| 34 | + window.set_title(&format!("{:?}", key)); |
| 35 | + let state = !modifiers.shift; |
| 36 | + use self::VirtualKeyCode::*; |
| 37 | + match key { |
| 38 | + A => window.set_always_on_top(state), |
| 39 | + C => window.set_cursor(match state { |
| 40 | + true => MouseCursor::Progress, |
| 41 | + false => MouseCursor::Default, |
| 42 | + }), |
| 43 | + D => window.set_decorations(!state), |
| 44 | + F => window.set_fullscreen(match state { |
| 45 | + true => Some(window.get_current_monitor()), |
| 46 | + false => None, |
| 47 | + }), |
| 48 | + G => window.grab_cursor(state).unwrap(), |
| 49 | + H => window.hide_cursor(state), |
| 50 | + I => { |
| 51 | + println!("Info:"); |
| 52 | + println!("-> position : {:?}", window.get_position()); |
| 53 | + println!("-> inner_position : {:?}", window.get_inner_position()); |
| 54 | + println!("-> outer_size : {:?}", window.get_outer_size()); |
| 55 | + println!("-> inner_size : {:?}", window.get_inner_size()); |
| 56 | + }, |
| 57 | + L => window.set_min_dimensions(match state { |
| 58 | + true => Some(WINDOW_SIZE.into()), |
| 59 | + false => None, |
| 60 | + }), |
| 61 | + M => window.set_maximized(state), |
| 62 | + P => window.set_position({ |
| 63 | + let mut position = window.get_position().unwrap(); |
| 64 | + let sign = if state { 1.0 } else { -1.0 }; |
| 65 | + position.x += 10.0 * sign; |
| 66 | + position.y += 10.0 * sign; |
| 67 | + position |
| 68 | + }), |
| 69 | + Q => window.request_redraw(), |
| 70 | + R => window.set_resizable(state), |
| 71 | + S => window.set_inner_size(match state { |
| 72 | + true => (WINDOW_SIZE.0 + 100, WINDOW_SIZE.1 + 100), |
| 73 | + false => WINDOW_SIZE, |
| 74 | + }.into()), |
| 75 | + W => window.set_cursor_position(( |
| 76 | + WINDOW_SIZE.0 as i32 / 2, |
| 77 | + WINDOW_SIZE.1 as i32 / 2, |
| 78 | + ).into()).unwrap(), |
| 79 | + Z => { |
| 80 | + window.hide(); |
| 81 | + thread::sleep(Duration::from_secs(1)); |
| 82 | + window.show(); |
| 83 | + }, |
| 84 | + _ => (), |
| 85 | + } |
| 86 | + }, |
| 87 | + _ => (), |
| 88 | + } |
| 89 | + } |
| 90 | + }); |
| 91 | + } |
| 92 | + event_loop.run(move |event, _event_loop, control_flow| { |
| 93 | + *control_flow = match !window_senders.is_empty() { |
| 94 | + true => ControlFlow::Wait, |
| 95 | + false => ControlFlow::Exit, |
| 96 | + }; |
| 97 | + match event { |
| 98 | + Event::WindowEvent { event, window_id } => { |
| 99 | + match event { |
| 100 | + WindowEvent::CloseRequested |
| 101 | + | WindowEvent::Destroyed |
| 102 | + | WindowEvent::KeyboardInput { input: KeyboardInput { |
| 103 | + virtual_keycode: Some(VirtualKeyCode::Escape), |
| 104 | + .. }, .. } => { |
| 105 | + window_senders.remove(&window_id); |
| 106 | + }, |
| 107 | + _ => if let Some(tx) = window_senders.get(&window_id) { |
| 108 | + tx.send(event).unwrap(); |
| 109 | + }, |
| 110 | + } |
| 111 | + } |
| 112 | + _ => (), |
| 113 | + } |
| 114 | + }) |
| 115 | +} |
0 commit comments