2019-07-30 04:16:14 +10:00
|
|
|
use std::io::{stdin, stdout, Write};
|
|
|
|
use winit::event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent};
|
|
|
|
use winit::event_loop::{ControlFlow, EventLoop};
|
|
|
|
use winit::monitor::{MonitorHandle, VideoMode};
|
|
|
|
use winit::window::{Fullscreen, WindowBuilder};
|
2014-07-31 17:56:53 +10:00
|
|
|
|
|
|
|
fn main() {
|
2020-01-07 07:28:58 +11:00
|
|
|
simple_logger::init().unwrap();
|
2019-02-06 02:30:33 +11:00
|
|
|
let event_loop = EventLoop::new();
|
2017-09-01 19:04:57 +10:00
|
|
|
|
2019-07-30 04:16:14 +10:00
|
|
|
print!("Please choose the fullscreen mode: (1) exclusive, (2) borderless: ");
|
|
|
|
stdout().flush().unwrap();
|
2014-07-31 17:56:53 +10:00
|
|
|
|
2019-07-30 04:16:14 +10:00
|
|
|
let mut num = String::new();
|
|
|
|
stdin().read_line(&mut num).unwrap();
|
|
|
|
let num = num.trim().parse().ok().expect("Please enter a number");
|
|
|
|
|
|
|
|
let fullscreen = Some(match num {
|
|
|
|
1 => Fullscreen::Exclusive(prompt_for_video_mode(&prompt_for_monitor(&event_loop))),
|
|
|
|
2 => Fullscreen::Borderless(prompt_for_monitor(&event_loop)),
|
|
|
|
_ => panic!("Please enter a valid number"),
|
|
|
|
});
|
2014-07-31 17:56:53 +10:00
|
|
|
|
2018-12-19 15:07:33 +11:00
|
|
|
let mut is_maximized = false;
|
|
|
|
let mut decorations = true;
|
|
|
|
|
2019-02-06 02:30:33 +11:00
|
|
|
let window = WindowBuilder::new()
|
2016-05-08 17:28:42 +10:00
|
|
|
.with_title("Hello world!")
|
2019-07-30 04:16:14 +10:00
|
|
|
.with_fullscreen(fullscreen.clone())
|
2019-02-06 02:30:33 +11:00
|
|
|
.build(&event_loop)
|
2014-08-02 16:24:30 +10:00
|
|
|
.unwrap();
|
2014-07-31 17:56:53 +10:00
|
|
|
|
2019-02-06 02:30:33 +11:00
|
|
|
event_loop.run(move |event, _, control_flow| {
|
|
|
|
*control_flow = ControlFlow::Wait;
|
2015-06-16 21:48:08 +10:00
|
|
|
|
|
|
|
match event {
|
2019-06-25 02:14:55 +10:00
|
|
|
Event::WindowEvent { event, .. } => match event {
|
|
|
|
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
|
|
|
|
WindowEvent::KeyboardInput {
|
|
|
|
input:
|
|
|
|
KeyboardInput {
|
|
|
|
virtual_keycode: Some(virtual_code),
|
|
|
|
state,
|
|
|
|
..
|
|
|
|
},
|
|
|
|
..
|
|
|
|
} => match (virtual_code, state) {
|
|
|
|
(VirtualKeyCode::Escape, _) => *control_flow = ControlFlow::Exit,
|
|
|
|
(VirtualKeyCode::F, ElementState::Pressed) => {
|
2019-07-30 04:16:14 +10:00
|
|
|
if window.fullscreen().is_some() {
|
2019-06-25 02:14:55 +10:00
|
|
|
window.set_fullscreen(None);
|
|
|
|
} else {
|
2019-07-30 04:16:14 +10:00
|
|
|
window.set_fullscreen(fullscreen.clone());
|
2019-06-25 02:14:55 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
(VirtualKeyCode::S, ElementState::Pressed) => {
|
|
|
|
println!("window.fullscreen {:?}", window.fullscreen());
|
|
|
|
}
|
|
|
|
(VirtualKeyCode::M, ElementState::Pressed) => {
|
|
|
|
is_maximized = !is_maximized;
|
|
|
|
window.set_maximized(is_maximized);
|
|
|
|
}
|
|
|
|
(VirtualKeyCode::D, ElementState::Pressed) => {
|
|
|
|
decorations = !decorations;
|
|
|
|
window.set_decorations(decorations);
|
|
|
|
}
|
2018-04-13 03:12:15 +10:00
|
|
|
_ => (),
|
2019-06-25 02:14:55 +10:00
|
|
|
},
|
|
|
|
_ => (),
|
2017-01-29 01:45:01 +11:00
|
|
|
},
|
2019-06-25 02:14:55 +10:00
|
|
|
_ => {}
|
2015-06-16 21:48:08 +10:00
|
|
|
}
|
2017-01-29 01:45:01 +11:00
|
|
|
});
|
2014-07-31 17:56:53 +10:00
|
|
|
}
|
2018-12-19 15:07:33 +11:00
|
|
|
|
|
|
|
// Enumerate monitors and prompt user to choose one
|
2019-02-06 02:30:33 +11:00
|
|
|
fn prompt_for_monitor(event_loop: &EventLoop<()>) -> MonitorHandle {
|
2019-05-30 11:29:54 +10:00
|
|
|
for (num, monitor) in event_loop.available_monitors().enumerate() {
|
|
|
|
println!("Monitor #{}: {:?}", num, monitor.name());
|
2018-12-19 15:07:33 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
print!("Please write the number of the monitor to use: ");
|
2019-07-30 04:16:14 +10:00
|
|
|
stdout().flush().unwrap();
|
2018-12-19 15:07:33 +11:00
|
|
|
|
|
|
|
let mut num = String::new();
|
2019-07-30 04:16:14 +10:00
|
|
|
stdin().read_line(&mut num).unwrap();
|
2018-12-19 15:07:33 +11:00
|
|
|
let num = num.trim().parse().ok().expect("Please enter a number");
|
2019-06-22 01:33:15 +10:00
|
|
|
let monitor = event_loop
|
|
|
|
.available_monitors()
|
|
|
|
.nth(num)
|
|
|
|
.expect("Please enter a valid ID");
|
2018-12-19 15:07:33 +11:00
|
|
|
|
2019-05-30 11:29:54 +10:00
|
|
|
println!("Using {:?}", monitor.name());
|
2018-12-19 15:07:33 +11:00
|
|
|
|
|
|
|
monitor
|
|
|
|
}
|
2019-07-30 04:16:14 +10:00
|
|
|
|
|
|
|
fn prompt_for_video_mode(monitor: &MonitorHandle) -> VideoMode {
|
|
|
|
for (i, video_mode) in monitor.video_modes().enumerate() {
|
|
|
|
println!("Video mode #{}: {}", i, video_mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
print!("Please write the number of the video mode to use: ");
|
|
|
|
stdout().flush().unwrap();
|
|
|
|
|
|
|
|
let mut num = String::new();
|
|
|
|
stdin().read_line(&mut num).unwrap();
|
|
|
|
let num = num.trim().parse().ok().expect("Please enter a number");
|
|
|
|
let video_mode = monitor
|
|
|
|
.video_modes()
|
|
|
|
.nth(num)
|
|
|
|
.expect("Please enter a valid ID");
|
|
|
|
|
|
|
|
println!("Using {}", video_mode);
|
|
|
|
|
|
|
|
video_mode
|
|
|
|
}
|