2019-01-07 21:10:31 +00:00
|
|
|
use crate::{
|
2020-03-10 21:00:13 +00:00
|
|
|
controller::*,
|
2021-07-29 18:47:45 +00:00
|
|
|
error::Error,
|
2021-07-21 11:23:45 +00:00
|
|
|
game_input::GameInput,
|
2020-08-21 19:10:56 +00:00
|
|
|
render::Renderer,
|
2020-04-08 17:36:37 +00:00
|
|
|
settings::{ControlSettings, Settings},
|
2021-07-29 18:47:45 +00:00
|
|
|
ui,
|
2019-01-07 21:10:31 +00:00
|
|
|
};
|
2021-03-08 22:40:02 +00:00
|
|
|
use common_base::span;
|
2021-05-31 17:03:18 +00:00
|
|
|
use crossbeam_channel as channel;
|
2020-03-10 21:00:13 +00:00
|
|
|
use gilrs::{EventType, Gilrs};
|
2019-08-11 19:52:23 +00:00
|
|
|
use hashbrown::HashMap;
|
2020-08-09 20:30:22 +00:00
|
|
|
use itertools::Itertools;
|
2021-03-31 12:56:56 +00:00
|
|
|
use keyboard_keynames::key_layout::KeyLayout;
|
2020-10-05 08:35:24 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2020-08-21 19:10:56 +00:00
|
|
|
use tracing::{error, warn};
|
2019-04-02 04:54:27 +00:00
|
|
|
use vek::*;
|
2020-08-09 20:30:22 +00:00
|
|
|
use winit::monitor::VideoMode;
|
2019-01-02 21:25:01 +00:00
|
|
|
|
2020-03-10 21:00:13 +00:00
|
|
|
/// Represents a key that the game menus recognise after input mapping
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Deserialize, Serialize)]
|
|
|
|
pub enum MenuInput {
|
|
|
|
Up,
|
|
|
|
Down,
|
|
|
|
Left,
|
|
|
|
Right,
|
|
|
|
ScrollUp,
|
|
|
|
ScrollDown,
|
|
|
|
ScrollLeft,
|
|
|
|
ScrollRight,
|
|
|
|
Home,
|
|
|
|
End,
|
|
|
|
Apply,
|
|
|
|
Back,
|
|
|
|
Exit,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
|
|
|
|
pub enum AnalogMenuInput {
|
|
|
|
MoveX(f32),
|
|
|
|
MoveY(f32),
|
|
|
|
ScrollX(f32),
|
|
|
|
ScrollY(f32),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
|
|
|
|
pub enum AnalogGameInput {
|
|
|
|
MovementX(f32),
|
|
|
|
MovementY(f32),
|
|
|
|
CameraX(f32),
|
|
|
|
CameraY(f32),
|
|
|
|
}
|
|
|
|
|
2019-05-25 14:39:27 +00:00
|
|
|
/// Represents an incoming event from the window.
|
2020-07-13 04:51:39 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2019-05-25 14:39:27 +00:00
|
|
|
pub enum Event {
|
|
|
|
/// The window has been requested to close.
|
|
|
|
Close,
|
|
|
|
/// The window has been resized.
|
|
|
|
Resize(Vec2<u32>),
|
2020-11-13 04:39:47 +00:00
|
|
|
/// The window scale factor has been changed
|
|
|
|
ScaleFactorChanged(f64),
|
2020-06-17 11:40:11 +00:00
|
|
|
/// The window has been moved.
|
|
|
|
Moved(Vec2<u32>),
|
2019-05-25 14:39:27 +00:00
|
|
|
/// A key has been typed that corresponds to a specific character.
|
|
|
|
Char(char),
|
|
|
|
/// The cursor has been panned across the screen while grabbed.
|
|
|
|
CursorPan(Vec2<f32>),
|
2019-09-18 16:46:12 +00:00
|
|
|
/// The cursor has been moved across the screen while ungrabbed.
|
|
|
|
CursorMove(Vec2<f32>),
|
|
|
|
/// A mouse button has been pressed or released
|
|
|
|
MouseButton(MouseButton, PressState),
|
2019-05-25 14:39:27 +00:00
|
|
|
/// The camera has been requested to zoom.
|
|
|
|
Zoom(f32),
|
|
|
|
/// A key that the game recognises has been pressed or released.
|
|
|
|
InputUpdate(GameInput, bool),
|
|
|
|
/// Event that the ui uses.
|
|
|
|
Ui(ui::Event),
|
2019-12-15 17:13:52 +00:00
|
|
|
/// Event that the iced ui uses.
|
|
|
|
IcedUi(ui::ice::Event),
|
2019-06-05 15:57:48 +00:00
|
|
|
/// The view distance has changed.
|
2019-05-25 14:39:27 +00:00
|
|
|
ViewDistanceChanged(u32),
|
|
|
|
/// Game settings have changed.
|
|
|
|
SettingsChanged,
|
2019-06-29 01:39:47 +00:00
|
|
|
/// The window is (un)focused
|
|
|
|
Focused(bool),
|
2020-03-10 21:00:13 +00:00
|
|
|
/// A key that the game recognises for menu navigation has been pressed or
|
|
|
|
/// released
|
|
|
|
MenuInput(MenuInput, bool),
|
|
|
|
/// Update of the analog inputs recognized by the menus
|
|
|
|
AnalogMenuInput(AnalogMenuInput),
|
|
|
|
/// Update of the analog inputs recognized by the game
|
|
|
|
AnalogGameInput(AnalogGameInput),
|
2020-06-05 20:53:15 +00:00
|
|
|
/// We tried to save a screenshot
|
2020-06-05 21:33:39 +00:00
|
|
|
ScreenshotMessage(String),
|
2019-05-25 14:39:27 +00:00
|
|
|
}
|
|
|
|
|
2019-10-26 14:50:01 +00:00
|
|
|
pub type MouseButton = winit::event::MouseButton;
|
|
|
|
pub type PressState = winit::event::ElementState;
|
2019-10-27 07:11:18 +00:00
|
|
|
pub type EventLoop = winit::event_loop::EventLoop<()>;
|
2019-09-18 16:46:12 +00:00
|
|
|
|
2019-05-25 14:39:27 +00:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Deserialize, Serialize)]
|
|
|
|
pub enum KeyMouse {
|
2019-10-27 07:11:18 +00:00
|
|
|
Key(winit::event::VirtualKeyCode),
|
|
|
|
Mouse(winit::event::MouseButton),
|
2020-12-16 09:40:08 +00:00
|
|
|
ScanKey(winit::event::ScanCode),
|
2019-05-25 14:39:27 +00:00
|
|
|
}
|
2019-05-25 12:53:44 +00:00
|
|
|
|
2021-03-31 12:56:56 +00:00
|
|
|
impl KeyMouse {
|
2021-07-21 14:53:03 +00:00
|
|
|
/// Returns key description (e.g Left Shift)
|
2021-03-31 12:56:56 +00:00
|
|
|
pub fn display_string(&self, key_layout: &Option<KeyLayout>) -> String {
|
2020-01-20 04:08:04 +00:00
|
|
|
use self::KeyMouse::*;
|
2019-10-27 07:11:18 +00:00
|
|
|
use winit::event::{MouseButton, VirtualKeyCode::*};
|
2021-03-31 12:56:56 +00:00
|
|
|
let key_string = match self {
|
2020-02-01 20:39:39 +00:00
|
|
|
Key(Key1) => "1",
|
|
|
|
Key(Key2) => "2",
|
|
|
|
Key(Key3) => "3",
|
|
|
|
Key(Key4) => "4",
|
|
|
|
Key(Key5) => "5",
|
|
|
|
Key(Key6) => "6",
|
|
|
|
Key(Key7) => "7",
|
|
|
|
Key(Key8) => "8",
|
|
|
|
Key(Key9) => "9",
|
|
|
|
Key(Key0) => "0",
|
|
|
|
Key(A) => "A",
|
|
|
|
Key(B) => "B",
|
|
|
|
Key(C) => "C",
|
|
|
|
Key(D) => "D",
|
|
|
|
Key(E) => "E",
|
|
|
|
Key(F) => "F",
|
|
|
|
Key(G) => "G",
|
|
|
|
Key(H) => "H",
|
|
|
|
Key(I) => "I",
|
|
|
|
Key(J) => "J",
|
|
|
|
Key(K) => "K",
|
|
|
|
Key(L) => "L",
|
|
|
|
Key(M) => "M",
|
|
|
|
Key(N) => "N",
|
|
|
|
Key(O) => "O",
|
|
|
|
Key(P) => "P",
|
|
|
|
Key(Q) => "Q",
|
|
|
|
Key(R) => "R",
|
|
|
|
Key(S) => "S",
|
|
|
|
Key(T) => "T",
|
|
|
|
Key(U) => "U",
|
|
|
|
Key(V) => "V",
|
|
|
|
Key(W) => "W",
|
|
|
|
Key(X) => "X",
|
|
|
|
Key(Y) => "Y",
|
|
|
|
Key(Z) => "Z",
|
|
|
|
Key(Escape) => "ESC",
|
|
|
|
Key(F1) => "F1",
|
|
|
|
Key(F2) => "F2",
|
|
|
|
Key(F3) => "F3",
|
|
|
|
Key(F4) => "F4",
|
|
|
|
Key(F5) => "F5",
|
|
|
|
Key(F6) => "F6",
|
|
|
|
Key(F7) => "F7",
|
|
|
|
Key(F8) => "F8",
|
|
|
|
Key(F9) => "F9",
|
|
|
|
Key(F10) => "F10",
|
|
|
|
Key(F11) => "F11",
|
|
|
|
Key(F12) => "F12",
|
|
|
|
Key(F13) => "F13",
|
|
|
|
Key(F14) => "F14",
|
|
|
|
Key(F15) => "F15",
|
|
|
|
Key(F16) => "F16",
|
|
|
|
Key(F17) => "F17",
|
|
|
|
Key(F18) => "F18",
|
|
|
|
Key(F19) => "F19",
|
|
|
|
Key(F20) => "F20",
|
|
|
|
Key(F21) => "F21",
|
|
|
|
Key(F22) => "F22",
|
|
|
|
Key(F23) => "F23",
|
|
|
|
Key(F24) => "F24",
|
|
|
|
Key(Snapshot) => "Print Screen",
|
|
|
|
Key(Scroll) => "Scroll Lock",
|
|
|
|
Key(Pause) => "Pause/Break",
|
|
|
|
Key(Insert) => "Insert",
|
|
|
|
Key(Home) => "Home",
|
|
|
|
Key(Delete) => "Delete",
|
|
|
|
Key(End) => "End",
|
|
|
|
Key(PageDown) => "PageDown",
|
|
|
|
Key(PageUp) => "PageUp",
|
|
|
|
Key(Left) => "Left Arrow",
|
|
|
|
Key(Up) => "Up Arrow",
|
|
|
|
Key(Right) => "Right Arrow",
|
|
|
|
Key(Down) => "Down Arrow",
|
|
|
|
Key(Back) => "Backspace",
|
|
|
|
Key(Return) => "Enter",
|
|
|
|
Key(Space) => "Space",
|
|
|
|
Key(Compose) => "Compose",
|
|
|
|
Key(Caret) => "^",
|
|
|
|
Key(Numlock) => "Numlock",
|
|
|
|
Key(Numpad0) => "Numpad 0",
|
|
|
|
Key(Numpad1) => "Numpad 1",
|
|
|
|
Key(Numpad2) => "Numpad 2",
|
|
|
|
Key(Numpad3) => "Numpad 3",
|
|
|
|
Key(Numpad4) => "Numpad 4",
|
|
|
|
Key(Numpad5) => "Numpad 5",
|
|
|
|
Key(Numpad6) => "Numpad 6",
|
|
|
|
Key(Numpad7) => "Numpad 7",
|
|
|
|
Key(Numpad8) => "Numpad 8",
|
|
|
|
Key(Numpad9) => "Numpad 9",
|
|
|
|
Key(AbntC1) => "Abnt C1",
|
|
|
|
Key(AbntC2) => "Abnt C2",
|
2020-09-29 12:29:18 +00:00
|
|
|
Key(NumpadAdd) => "Numpad +",
|
2020-02-01 20:39:39 +00:00
|
|
|
Key(Apostrophe) => "'",
|
|
|
|
Key(Apps) => "Context Menu",
|
|
|
|
Key(At) => "@",
|
|
|
|
Key(Ax) => "Ax",
|
|
|
|
Key(Backslash) => "\\",
|
|
|
|
Key(Calculator) => "Calculator",
|
|
|
|
Key(Capital) => "Caps Lock",
|
|
|
|
Key(Colon) => ":",
|
|
|
|
Key(Comma) => ",",
|
|
|
|
Key(Convert) => "Convert",
|
2020-09-29 12:29:18 +00:00
|
|
|
Key(NumpadDecimal) => "Numpad .",
|
|
|
|
Key(NumpadDivide) => "Numpad /",
|
2020-02-01 20:39:39 +00:00
|
|
|
Key(Equals) => "=",
|
|
|
|
Key(Grave) => "`",
|
|
|
|
Key(Kana) => "Kana",
|
|
|
|
Key(Kanji) => "Kanji",
|
|
|
|
Key(LBracket) => "[",
|
2021-06-30 12:14:17 +00:00
|
|
|
Key(RBracket) => "]",
|
2020-02-01 20:39:39 +00:00
|
|
|
Key(Mail) => "Mail",
|
|
|
|
Key(MediaSelect) => "MediaSelect",
|
|
|
|
Key(MediaStop) => "MediaStop",
|
|
|
|
Key(Minus) => "-",
|
2021-07-21 14:53:03 +00:00
|
|
|
Key(Plus) => "+",
|
2020-09-29 12:29:18 +00:00
|
|
|
Key(NumpadMultiply) => "Numpad *",
|
2020-02-01 20:39:39 +00:00
|
|
|
Key(Mute) => "Mute",
|
|
|
|
Key(MyComputer) => "My Computer",
|
|
|
|
Key(NavigateBackward) => "Navigate Backward",
|
2021-06-30 12:14:17 +00:00
|
|
|
Key(NavigateForward) => "Navigate Forward",
|
2020-02-01 20:39:39 +00:00
|
|
|
Key(NoConvert) => "Non Convert",
|
2020-04-26 01:44:56 +00:00
|
|
|
Key(NumpadComma) => "Num ,",
|
|
|
|
Key(NumpadEnter) => "Num Enter",
|
|
|
|
Key(NumpadEquals) => "Num =",
|
|
|
|
Key(OEM102) => "<",
|
2020-02-01 20:39:39 +00:00
|
|
|
Key(Period) => ".",
|
|
|
|
Key(Power) => "Power",
|
2021-06-30 12:14:17 +00:00
|
|
|
Key(PlayPause) => "Play / Pause",
|
2020-02-01 20:39:39 +00:00
|
|
|
Key(PrevTrack) => "Prev Track",
|
2021-06-30 12:14:17 +00:00
|
|
|
Key(NextTrack) => "Next Track",
|
|
|
|
Key(LAlt) => {
|
|
|
|
if cfg!(macos) {
|
|
|
|
"Left Option ⌥"
|
|
|
|
} else {
|
|
|
|
// Assume Windows, Linux, BSD, etc.
|
|
|
|
"Left Alt"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Key(RAlt) => {
|
|
|
|
if cfg!(macos) {
|
|
|
|
"Right Option ⌥"
|
|
|
|
} else {
|
|
|
|
// Assume Windows, Linux, BSD, etc.
|
|
|
|
"Right Alt"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Key(LControl) => {
|
|
|
|
if cfg!(macos) {
|
|
|
|
"Left Cmd ⌘"
|
|
|
|
} else {
|
|
|
|
// Assume Windows, Linux, BSD, etc.
|
|
|
|
"Left Ctrl"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Key(RControl) => {
|
|
|
|
if cfg!(macos) {
|
|
|
|
"Right Cmd ⌘"
|
|
|
|
} else {
|
|
|
|
// Assume Windows, Linux, BSD, etc.
|
|
|
|
"Right Ctrl"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Key(LShift) => "Left Shift",
|
|
|
|
Key(RShift) => "Right Shift",
|
|
|
|
// Key doesn't usually have a right counterpart on modern keyboards, to omit the
|
|
|
|
// qualifier. The exception to this is Mac OS which doesn't usually have
|
|
|
|
// this key at all, so we keep the qualifier to minimise ambiguity.
|
|
|
|
Key(LWin) => {
|
|
|
|
if cfg!(windows) {
|
|
|
|
"Win ⊞"
|
|
|
|
} else if cfg!(macos) {
|
|
|
|
"Left Cmd ⌘ (Super)" // Extra qualifier because both Ctrl and Win map to Cmd on Mac
|
|
|
|
} else {
|
|
|
|
// Assume Linux, BSD, etc.
|
|
|
|
"Super"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
// Most keyboards don't have this key, so throw in all the qualifiers
|
|
|
|
Key(RWin) => {
|
|
|
|
if cfg!(windows) {
|
|
|
|
"Right Win ⊞"
|
|
|
|
} else if cfg!(macos) {
|
|
|
|
"Right Cmd ⌘ (Super)" // Extra qualifier because both Ctrl and Win map to Cmd on Mac
|
|
|
|
} else {
|
|
|
|
// Assume Linux, BSD, etc.
|
|
|
|
"Right Super"
|
|
|
|
}
|
|
|
|
},
|
2020-02-01 20:39:39 +00:00
|
|
|
Key(Semicolon) => ";",
|
|
|
|
Key(Slash) => "/",
|
|
|
|
Key(Sleep) => "Sleep",
|
|
|
|
Key(Stop) => "Media Stop",
|
2020-09-29 12:29:18 +00:00
|
|
|
Key(NumpadSubtract) => "Num -",
|
2020-02-01 20:39:39 +00:00
|
|
|
Key(Sysrq) => "Sysrq",
|
|
|
|
Key(Tab) => "Tab",
|
|
|
|
Key(Underline) => "_",
|
|
|
|
Key(Unlabeled) => "No Name",
|
|
|
|
Key(VolumeDown) => "Volume Down",
|
|
|
|
Key(VolumeUp) => "Volume Up",
|
|
|
|
Key(Wake) => "Wake",
|
|
|
|
Key(WebBack) => "Browser Back",
|
|
|
|
Key(WebFavorites) => "Browser Favorites",
|
|
|
|
Key(WebForward) => "Browser Forward",
|
|
|
|
Key(WebHome) => "Browser Home",
|
|
|
|
Key(WebRefresh) => "Browser Refresh",
|
|
|
|
Key(WebSearch) => "Browser Search",
|
|
|
|
Key(WebStop) => "Browser Stop",
|
|
|
|
Key(Yen) => "Yen",
|
|
|
|
Key(Copy) => "Copy",
|
|
|
|
Key(Paste) => "Paste",
|
|
|
|
Key(Cut) => "Cut",
|
2020-09-29 12:29:18 +00:00
|
|
|
Key(Asterisk) => "*",
|
2021-06-30 12:14:17 +00:00
|
|
|
Mouse(MouseButton::Left) => "Left Click",
|
|
|
|
Mouse(MouseButton::Right) => "Right Click",
|
|
|
|
Mouse(MouseButton::Middle) => "Middle Click",
|
2021-03-31 12:56:56 +00:00
|
|
|
Mouse(MouseButton::Other(button)) => {
|
|
|
|
// Additional mouse buttons after middle click start at 1
|
2021-06-30 12:14:17 +00:00
|
|
|
return format!("Mouse {}", button + 3);
|
2021-03-31 12:56:56 +00:00
|
|
|
},
|
|
|
|
ScanKey(scancode) => {
|
|
|
|
if let Some(layout) = key_layout {
|
|
|
|
return layout.get_key_as_string(*scancode);
|
|
|
|
} else {
|
2021-06-30 12:14:17 +00:00
|
|
|
return format!("Unknown (0x{:X})", scancode);
|
2021-03-31 12:56:56 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-07-21 14:53:03 +00:00
|
|
|
key_string.to_owned()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns shortened key name (e.g. Left Click -> LMB)
|
|
|
|
///
|
|
|
|
/// Use it in case if space does really matter.
|
|
|
|
pub fn display_shortened(&self, _key_layout: &Option<KeyLayout>) -> Option<String> {
|
|
|
|
use self::KeyMouse::*;
|
|
|
|
use winit::event::{MouseButton, VirtualKeyCode::*};
|
|
|
|
let key_string = match self {
|
|
|
|
Mouse(MouseButton::Left) => "M1",
|
|
|
|
Mouse(MouseButton::Right) => "M2",
|
|
|
|
Mouse(MouseButton::Middle) => "M3",
|
|
|
|
Mouse(MouseButton::Other(button)) => {
|
|
|
|
// Additional mouse buttons after middle click start at 1
|
|
|
|
return Some(format!("M{}", button + 3));
|
|
|
|
},
|
|
|
|
Key(Back) => "Back",
|
|
|
|
Key(LShift) => "LShft",
|
|
|
|
Key(RShift) => "RShft",
|
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
|
|
|
|
Some(key_string.to_owned())
|
2020-01-20 04:08:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-02 21:25:01 +00:00
|
|
|
pub struct Window {
|
2020-11-28 05:44:23 +00:00
|
|
|
renderer: Renderer,
|
2020-08-21 19:10:56 +00:00
|
|
|
window: winit::window::Window,
|
2019-01-23 22:21:47 +00:00
|
|
|
cursor_grabbed: bool,
|
2019-06-06 17:42:13 +00:00
|
|
|
pub pan_sensitivity: u32,
|
|
|
|
pub zoom_sensitivity: u32,
|
2019-10-02 12:44:41 +00:00
|
|
|
pub zoom_inversion: bool,
|
2019-12-06 22:49:17 +00:00
|
|
|
pub mouse_y_inversion: bool,
|
2020-09-07 05:12:15 +00:00
|
|
|
fullscreen: FullScreenSettings,
|
2020-07-15 04:56:50 +00:00
|
|
|
modifiers: winit::event::ModifiersState,
|
2020-06-27 17:44:43 +00:00
|
|
|
scale_factor: f64,
|
2019-03-28 04:42:04 +00:00
|
|
|
needs_refresh_resize: bool,
|
2019-10-26 14:50:01 +00:00
|
|
|
keypress_map: HashMap<GameInput, winit::event::ElementState>,
|
2020-04-08 17:36:37 +00:00
|
|
|
pub remapping_keybindings: Option<GameInput>,
|
2019-10-27 07:11:18 +00:00
|
|
|
events: Vec<Event>,
|
2019-05-25 11:52:43 +00:00
|
|
|
focused: bool,
|
2020-03-10 21:00:13 +00:00
|
|
|
gilrs: Option<Gilrs>,
|
2021-02-24 03:50:17 +00:00
|
|
|
pub controller_settings: ControllerSettings,
|
2020-02-08 02:09:56 +00:00
|
|
|
cursor_position: winit::dpi::PhysicalPosition<f64>,
|
2020-03-10 21:00:13 +00:00
|
|
|
mouse_emulation_vec: Vec2<f32>,
|
2020-06-05 21:33:39 +00:00
|
|
|
// Currently used to send and receive screenshot result messages
|
|
|
|
message_sender: channel::Sender<String>,
|
|
|
|
message_receiver: channel::Receiver<String>,
|
2020-07-18 16:21:22 +00:00
|
|
|
// Used for screenshots & fullscreen toggle to deduplicate/postpone to after event handler
|
|
|
|
take_screenshot: bool,
|
|
|
|
toggle_fullscreen: bool,
|
2021-03-31 12:56:56 +00:00
|
|
|
pub key_layout: Option<KeyLayout>,
|
2019-01-02 21:25:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Window {
|
2021-08-10 23:23:48 +00:00
|
|
|
pub fn new(
|
|
|
|
settings: &Settings,
|
|
|
|
runtime: &tokio::runtime::Runtime,
|
|
|
|
) -> Result<(Window, EventLoop), Error> {
|
2019-10-27 07:11:18 +00:00
|
|
|
let event_loop = EventLoop::new();
|
2019-01-02 21:25:01 +00:00
|
|
|
|
2020-01-02 03:48:11 +00:00
|
|
|
let size = settings.graphics.window_size;
|
|
|
|
|
2020-07-13 03:11:48 +00:00
|
|
|
let win_builder = winit::window::WindowBuilder::new()
|
2019-03-15 04:55:52 +00:00
|
|
|
.with_title("Veloren")
|
2020-07-13 03:11:48 +00:00
|
|
|
.with_inner_size(winit::dpi::LogicalSize::new(size[0] as f64, size[1] as f64))
|
2019-03-15 04:55:52 +00:00
|
|
|
.with_maximized(true);
|
2019-01-11 17:30:13 +00:00
|
|
|
|
2020-07-13 03:11:48 +00:00
|
|
|
// Avoid cpal / winit OleInitialize conflict
|
|
|
|
// See: https://github.com/rust-windowing/winit/pull/1524
|
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
let win_builder = winit::platform::windows::WindowBuilderExtWindows::with_drag_and_drop(
|
|
|
|
win_builder,
|
|
|
|
false,
|
|
|
|
);
|
|
|
|
|
2020-09-26 15:43:59 +00:00
|
|
|
let window = win_builder.build(&event_loop).unwrap();
|
2020-08-21 19:10:56 +00:00
|
|
|
|
2021-08-10 23:23:48 +00:00
|
|
|
let renderer = Renderer::new(&window, settings.graphics.render_mode.clone(), runtime)?;
|
2020-06-22 13:34:35 +00:00
|
|
|
|
2019-06-30 04:20:50 +00:00
|
|
|
let keypress_map = HashMap::new();
|
|
|
|
|
2020-03-10 21:00:13 +00:00
|
|
|
let gilrs = match Gilrs::new() {
|
|
|
|
Ok(gilrs) => Some(gilrs),
|
|
|
|
Err(gilrs::Error::NotImplemented(_dummy)) => {
|
|
|
|
warn!("Controller input is unsupported on this platform.");
|
|
|
|
None
|
|
|
|
},
|
|
|
|
Err(gilrs::Error::InvalidAxisToBtn) => {
|
|
|
|
error!(
|
|
|
|
"Invalid AxisToBtn controller mapping. Falling back to no controller support."
|
|
|
|
);
|
|
|
|
None
|
|
|
|
},
|
2020-06-21 21:47:49 +00:00
|
|
|
Err(gilrs::Error::Other(e)) => {
|
2020-03-10 21:00:13 +00:00
|
|
|
error!(
|
2020-06-21 21:47:49 +00:00
|
|
|
?e,
|
|
|
|
"Platform-specific error when creating a Gilrs instance. Falling back to no \
|
|
|
|
controller support."
|
2020-03-10 21:00:13 +00:00
|
|
|
);
|
|
|
|
None
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
let controller_settings = ControllerSettings::from(&settings.controller);
|
|
|
|
|
2020-06-05 20:53:15 +00:00
|
|
|
let (message_sender, message_receiver): (
|
|
|
|
channel::Sender<String>,
|
|
|
|
channel::Receiver<String>,
|
|
|
|
) = channel::unbounded::<String>();
|
|
|
|
|
2020-11-29 02:14:22 +00:00
|
|
|
let scale_factor = window.scale_factor();
|
2020-06-27 17:44:43 +00:00
|
|
|
|
2021-01-31 03:50:03 +00:00
|
|
|
let key_layout = match KeyLayout::new_from_window(&window) {
|
2021-03-31 12:56:56 +00:00
|
|
|
Ok(kl) => Some(kl),
|
|
|
|
Err(err) => {
|
|
|
|
warn!(
|
|
|
|
?err,
|
|
|
|
"Failed to construct the scancode to keyname mapper, falling back to \
|
|
|
|
displaying Unknown(<scancode>)."
|
|
|
|
);
|
|
|
|
None
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-01-02 03:48:11 +00:00
|
|
|
let mut this = Self {
|
2020-08-21 19:10:56 +00:00
|
|
|
renderer,
|
2019-01-23 22:21:47 +00:00
|
|
|
window,
|
|
|
|
cursor_grabbed: false,
|
2019-06-05 15:57:48 +00:00
|
|
|
pan_sensitivity: settings.gameplay.pan_sensitivity,
|
|
|
|
zoom_sensitivity: settings.gameplay.zoom_sensitivity,
|
2019-10-02 12:44:41 +00:00
|
|
|
zoom_inversion: settings.gameplay.zoom_inversion,
|
2019-12-06 22:49:17 +00:00
|
|
|
mouse_y_inversion: settings.gameplay.mouse_y_inversion,
|
2020-09-07 05:12:15 +00:00
|
|
|
fullscreen: FullScreenSettings::default(),
|
2020-07-15 04:56:50 +00:00
|
|
|
modifiers: Default::default(),
|
2020-06-27 17:44:43 +00:00
|
|
|
scale_factor,
|
2019-03-28 04:42:04 +00:00
|
|
|
needs_refresh_resize: false,
|
2019-06-30 04:20:50 +00:00
|
|
|
keypress_map,
|
2020-04-08 17:36:37 +00:00
|
|
|
remapping_keybindings: None,
|
2019-10-27 07:11:18 +00:00
|
|
|
events: Vec::new(),
|
2019-05-25 11:52:43 +00:00
|
|
|
focused: true,
|
2020-03-10 21:00:13 +00:00
|
|
|
gilrs,
|
|
|
|
controller_settings,
|
2020-02-08 02:09:56 +00:00
|
|
|
cursor_position: winit::dpi::PhysicalPosition::new(0.0, 0.0),
|
2020-03-10 21:00:13 +00:00
|
|
|
mouse_emulation_vec: Vec2::zero(),
|
2020-06-05 21:33:39 +00:00
|
|
|
// Currently used to send and receive screenshot result messages
|
|
|
|
message_sender,
|
|
|
|
message_receiver,
|
2020-07-18 16:21:22 +00:00
|
|
|
take_screenshot: false,
|
|
|
|
toggle_fullscreen: false,
|
2021-03-31 12:56:56 +00:00
|
|
|
key_layout,
|
2020-01-02 03:48:11 +00:00
|
|
|
};
|
|
|
|
|
2020-09-07 05:12:15 +00:00
|
|
|
this.set_fullscreen_mode(settings.graphics.fullscreen);
|
2020-01-02 03:48:11 +00:00
|
|
|
|
2019-10-27 07:11:18 +00:00
|
|
|
Ok((this, event_loop))
|
|
|
|
}
|
|
|
|
|
2020-02-01 20:39:39 +00:00
|
|
|
pub fn renderer(&self) -> &Renderer { &self.renderer }
|
|
|
|
|
2020-11-28 05:44:23 +00:00
|
|
|
pub fn renderer_mut(&mut self) -> &mut Renderer { &mut self.renderer }
|
2019-01-02 22:08:13 +00:00
|
|
|
|
2021-07-19 02:57:14 +00:00
|
|
|
pub fn resolve_deduplicated_events(
|
|
|
|
&mut self,
|
|
|
|
settings: &mut Settings,
|
|
|
|
config_dir: &std::path::Path,
|
|
|
|
) {
|
2020-07-18 16:21:22 +00:00
|
|
|
// Handle screenshots and toggling fullscreen
|
|
|
|
if self.take_screenshot {
|
|
|
|
self.take_screenshot = false;
|
2021-07-11 18:41:52 +00:00
|
|
|
self.take_screenshot(settings);
|
2020-07-18 16:21:22 +00:00
|
|
|
}
|
|
|
|
if self.toggle_fullscreen {
|
|
|
|
self.toggle_fullscreen = false;
|
2021-07-19 02:57:14 +00:00
|
|
|
self.toggle_fullscreen(settings, config_dir);
|
2020-07-18 16:21:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-27 07:11:18 +00:00
|
|
|
pub fn fetch_events(&mut self) -> Vec<Event> {
|
2020-11-29 02:14:22 +00:00
|
|
|
span!(_guard, "fetch_events", "Window::fetch_events");
|
2019-03-28 04:42:04 +00:00
|
|
|
// Refresh ui size (used when changing playstates)
|
|
|
|
if self.needs_refresh_resize {
|
2020-11-13 03:06:57 +00:00
|
|
|
let logical_size = self.logical_size();
|
2019-10-27 07:11:18 +00:00
|
|
|
self.events
|
2020-11-13 03:06:57 +00:00
|
|
|
.push(Event::Ui(ui::Event::new_resize(logical_size)));
|
|
|
|
self.events.push(Event::IcedUi(iced::Event::Window(
|
|
|
|
iced::window::Event::Resized {
|
|
|
|
width: logical_size.x as u32,
|
|
|
|
height: logical_size.y as u32,
|
|
|
|
},
|
|
|
|
)));
|
2020-11-13 04:39:47 +00:00
|
|
|
self.events
|
|
|
|
.push(Event::ScaleFactorChanged(self.scale_factor));
|
2019-03-28 04:42:04 +00:00
|
|
|
self.needs_refresh_resize = false;
|
|
|
|
}
|
|
|
|
|
2020-06-05 20:53:15 +00:00
|
|
|
// Receive any messages sent through the message channel
|
2020-07-13 04:51:39 +00:00
|
|
|
for message in self.message_receiver.try_iter() {
|
|
|
|
self.events.push(Event::ScreenshotMessage(message))
|
|
|
|
}
|
2020-06-05 20:53:15 +00:00
|
|
|
|
2020-03-10 21:00:13 +00:00
|
|
|
if let Some(gilrs) = &mut self.gilrs {
|
|
|
|
while let Some(event) = gilrs.next_event() {
|
|
|
|
fn handle_buttons(
|
|
|
|
settings: &ControllerSettings,
|
|
|
|
events: &mut Vec<Event>,
|
|
|
|
button: &Button,
|
|
|
|
is_pressed: bool,
|
|
|
|
) {
|
|
|
|
if let Some(evs) = settings.game_button_map.get(button) {
|
|
|
|
for ev in evs {
|
|
|
|
events.push(Event::InputUpdate(*ev, is_pressed));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(evs) = settings.menu_button_map.get(button) {
|
|
|
|
for ev in evs {
|
|
|
|
events.push(Event::MenuInput(*ev, is_pressed));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
match event.event {
|
|
|
|
EventType::ButtonPressed(button, code)
|
|
|
|
| EventType::ButtonRepeated(button, code) => {
|
|
|
|
handle_buttons(
|
|
|
|
&self.controller_settings,
|
2019-10-27 07:11:18 +00:00
|
|
|
&mut self.events,
|
2020-03-10 21:00:13 +00:00
|
|
|
&Button::from((button, code)),
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
EventType::ButtonReleased(button, code) => {
|
|
|
|
handle_buttons(
|
|
|
|
&self.controller_settings,
|
2019-10-27 07:11:18 +00:00
|
|
|
&mut self.events,
|
2020-03-10 21:00:13 +00:00
|
|
|
&Button::from((button, code)),
|
|
|
|
false,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
EventType::ButtonChanged(button, _value, code) => {
|
|
|
|
if let Some(actions) = self
|
|
|
|
.controller_settings
|
|
|
|
.game_analog_button_map
|
|
|
|
.get(&AnalogButton::from((button, code)))
|
|
|
|
{
|
|
|
|
for action in actions {
|
|
|
|
match *action {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(actions) = self
|
|
|
|
.controller_settings
|
|
|
|
.menu_analog_button_map
|
|
|
|
.get(&AnalogButton::from((button, code)))
|
|
|
|
{
|
|
|
|
for action in actions {
|
|
|
|
match *action {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
EventType::AxisChanged(axis, value, code) => {
|
2020-07-18 01:39:10 +00:00
|
|
|
let value = if self
|
2020-03-10 21:00:13 +00:00
|
|
|
.controller_settings
|
|
|
|
.inverted_axes
|
|
|
|
.contains(&Axis::from((axis, code)))
|
|
|
|
{
|
2020-07-18 01:39:10 +00:00
|
|
|
-value
|
|
|
|
} else {
|
|
|
|
value
|
2020-03-10 21:00:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let value = self
|
|
|
|
.controller_settings
|
|
|
|
.apply_axis_deadzone(&Axis::from((axis, code)), value);
|
|
|
|
|
|
|
|
if self.cursor_grabbed {
|
|
|
|
if let Some(actions) = self
|
|
|
|
.controller_settings
|
|
|
|
.game_axis_map
|
|
|
|
.get(&Axis::from((axis, code)))
|
|
|
|
{
|
|
|
|
for action in actions {
|
|
|
|
match *action {
|
|
|
|
AxisGameAction::MovementX => {
|
2019-10-27 07:11:18 +00:00
|
|
|
self.events.push(Event::AnalogGameInput(
|
2020-03-10 21:00:13 +00:00
|
|
|
AnalogGameInput::MovementX(value),
|
|
|
|
));
|
|
|
|
},
|
|
|
|
AxisGameAction::MovementY => {
|
2019-10-27 07:11:18 +00:00
|
|
|
self.events.push(Event::AnalogGameInput(
|
2020-03-10 21:00:13 +00:00
|
|
|
AnalogGameInput::MovementY(value),
|
|
|
|
));
|
|
|
|
},
|
|
|
|
AxisGameAction::CameraX => {
|
2019-10-27 07:11:18 +00:00
|
|
|
self.events.push(Event::AnalogGameInput(
|
2020-03-10 21:00:13 +00:00
|
|
|
AnalogGameInput::CameraX(
|
|
|
|
value
|
|
|
|
* self.controller_settings.pan_sensitivity
|
|
|
|
as f32
|
|
|
|
/ 100.0,
|
|
|
|
),
|
|
|
|
));
|
|
|
|
},
|
|
|
|
AxisGameAction::CameraY => {
|
2021-02-24 03:50:17 +00:00
|
|
|
let pan_invert_y =
|
|
|
|
match self.controller_settings.pan_invert_y {
|
|
|
|
true => -1.0,
|
|
|
|
false => 1.0,
|
|
|
|
};
|
|
|
|
|
2019-10-27 07:11:18 +00:00
|
|
|
self.events.push(Event::AnalogGameInput(
|
2020-03-10 21:00:13 +00:00
|
|
|
AnalogGameInput::CameraY(
|
2021-02-15 00:51:49 +00:00
|
|
|
-value
|
2020-03-10 21:00:13 +00:00
|
|
|
* self.controller_settings.pan_sensitivity
|
|
|
|
as f32
|
2021-02-24 03:50:17 +00:00
|
|
|
* pan_invert_y
|
2020-03-10 21:00:13 +00:00
|
|
|
/ 100.0,
|
|
|
|
),
|
|
|
|
));
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if let Some(actions) = self
|
|
|
|
.controller_settings
|
|
|
|
.menu_axis_map
|
|
|
|
.get(&Axis::from((axis, code)))
|
|
|
|
{
|
|
|
|
// TODO: possibly add sensitivity settings when this is used
|
|
|
|
for action in actions {
|
|
|
|
match *action {
|
|
|
|
AxisMenuAction::MoveX => {
|
2019-10-27 07:11:18 +00:00
|
|
|
self.events.push(Event::AnalogMenuInput(
|
2020-03-10 21:00:13 +00:00
|
|
|
AnalogMenuInput::MoveX(value),
|
|
|
|
));
|
|
|
|
},
|
|
|
|
AxisMenuAction::MoveY => {
|
2019-10-27 07:11:18 +00:00
|
|
|
self.events.push(Event::AnalogMenuInput(
|
2020-03-10 21:00:13 +00:00
|
|
|
AnalogMenuInput::MoveY(value),
|
|
|
|
));
|
|
|
|
},
|
|
|
|
AxisMenuAction::ScrollX => {
|
2019-10-27 07:11:18 +00:00
|
|
|
self.events.push(Event::AnalogMenuInput(
|
2020-03-10 21:00:13 +00:00
|
|
|
AnalogMenuInput::ScrollX(value),
|
|
|
|
));
|
|
|
|
},
|
|
|
|
AxisMenuAction::ScrollY => {
|
2019-10-27 07:11:18 +00:00
|
|
|
self.events.push(Event::AnalogMenuInput(
|
2020-03-10 21:00:13 +00:00
|
|
|
AnalogMenuInput::ScrollY(value),
|
|
|
|
));
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
EventType::Connected => {},
|
|
|
|
EventType::Disconnected => {},
|
|
|
|
EventType::Dropped => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-13 04:51:39 +00:00
|
|
|
let mut events = std::mem::take(&mut self.events);
|
2020-03-10 21:00:13 +00:00
|
|
|
// Mouse emulation for the menus, to be removed when a proper menu navigation
|
|
|
|
// system is available
|
|
|
|
if !self.cursor_grabbed {
|
2020-02-08 02:09:56 +00:00
|
|
|
events = events
|
2020-03-10 21:00:13 +00:00
|
|
|
.into_iter()
|
|
|
|
.filter_map(|event| match event {
|
|
|
|
Event::AnalogMenuInput(input) => match input {
|
|
|
|
AnalogMenuInput::MoveX(d) => {
|
|
|
|
self.mouse_emulation_vec.x = d;
|
|
|
|
None
|
|
|
|
},
|
|
|
|
AnalogMenuInput::MoveY(d) => {
|
|
|
|
// This just has to be inverted for some reason
|
|
|
|
self.mouse_emulation_vec.y = d * -1.0;
|
|
|
|
None
|
|
|
|
},
|
2020-02-08 02:09:56 +00:00
|
|
|
input => Some(Event::AnalogMenuInput(input)),
|
2020-03-10 21:00:13 +00:00
|
|
|
},
|
2020-02-08 02:09:56 +00:00
|
|
|
Event::MenuInput(MenuInput::Apply, state) => Some(match state {
|
|
|
|
true => Event::Ui(ui::Event(conrod_core::event::Input::Press(
|
|
|
|
conrod_core::input::Button::Mouse(
|
|
|
|
conrod_core::input::state::mouse::Button::Left,
|
|
|
|
),
|
|
|
|
))),
|
|
|
|
false => Event::Ui(ui::Event(conrod_core::event::Input::Release(
|
|
|
|
conrod_core::input::Button::Mouse(
|
|
|
|
conrod_core::input::state::mouse::Button::Left,
|
|
|
|
),
|
|
|
|
))),
|
|
|
|
}),
|
2020-03-10 21:00:13 +00:00
|
|
|
_ => Some(event),
|
|
|
|
})
|
|
|
|
.collect();
|
2019-10-27 07:11:18 +00:00
|
|
|
|
2020-03-10 21:00:13 +00:00
|
|
|
let sensitivity = self.controller_settings.mouse_emulation_sensitivity;
|
2019-10-27 07:11:18 +00:00
|
|
|
// TODO: make this independent of framerate
|
2020-02-08 02:09:56 +00:00
|
|
|
// TODO: consider multiplying by scale factor
|
2019-10-27 07:11:18 +00:00
|
|
|
self.offset_cursor(self.mouse_emulation_vec * sensitivity as f32);
|
|
|
|
}
|
|
|
|
|
2020-02-08 02:09:56 +00:00
|
|
|
events
|
2019-10-27 07:11:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn handle_device_event(&mut self, event: winit::event::DeviceEvent) {
|
|
|
|
use winit::event::DeviceEvent;
|
|
|
|
|
|
|
|
let mouse_y_inversion = match self.mouse_y_inversion {
|
|
|
|
true => -1.0,
|
|
|
|
false => 1.0,
|
|
|
|
};
|
|
|
|
|
|
|
|
match event {
|
|
|
|
DeviceEvent::MouseMotion {
|
|
|
|
delta: (dx, dy), ..
|
|
|
|
} if self.focused => {
|
|
|
|
let delta = Vec2::new(
|
|
|
|
dx as f32 * (self.pan_sensitivity as f32 / 100.0),
|
|
|
|
dy as f32 * (self.pan_sensitivity as f32 * mouse_y_inversion / 100.0),
|
|
|
|
);
|
|
|
|
|
|
|
|
if self.cursor_grabbed {
|
|
|
|
self.events.push(Event::CursorPan(delta));
|
|
|
|
} else {
|
|
|
|
self.events.push(Event::CursorMove(delta));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn handle_window_event(
|
|
|
|
&mut self,
|
|
|
|
event: winit::event::WindowEvent,
|
|
|
|
settings: &mut Settings,
|
|
|
|
) {
|
|
|
|
use winit::event::WindowEvent;
|
|
|
|
|
|
|
|
let controls = &mut settings.controls;
|
|
|
|
|
|
|
|
match event {
|
|
|
|
WindowEvent::CloseRequested => self.events.push(Event::Close),
|
2021-03-21 21:09:30 +00:00
|
|
|
WindowEvent::Resized(_) => {
|
|
|
|
// We don't use the event provided size because since this event
|
|
|
|
// more could have happened making the value wrong so we query
|
|
|
|
// directly from the window, this prevents some errors
|
|
|
|
let physical = self.window.inner_size();
|
|
|
|
|
2020-11-28 05:44:23 +00:00
|
|
|
self.renderer
|
2021-07-29 06:06:56 +00:00
|
|
|
.on_resize(Vec2::new(physical.width, physical.height));
|
2020-02-08 02:09:56 +00:00
|
|
|
// TODO: update users of this event with the fact that it is now the physical
|
|
|
|
// size
|
2020-08-03 02:19:05 +00:00
|
|
|
let winit::dpi::PhysicalSize { width, height } = physical;
|
2019-10-27 07:11:18 +00:00
|
|
|
self.events
|
|
|
|
.push(Event::Resize(Vec2::new(width as u32, height as u32)));
|
|
|
|
},
|
2020-06-27 17:44:43 +00:00
|
|
|
WindowEvent::ScaleFactorChanged { scale_factor, .. } => {
|
2020-11-30 07:27:36 +00:00
|
|
|
// TODO: is window resized event emitted? or do we need to handle that here?
|
2020-11-13 04:39:47 +00:00
|
|
|
self.scale_factor = scale_factor;
|
|
|
|
self.events.push(Event::ScaleFactorChanged(scale_factor));
|
2020-08-03 02:19:05 +00:00
|
|
|
},
|
2020-11-25 05:29:28 +00:00
|
|
|
WindowEvent::Moved(winit::dpi::PhysicalPosition { x, y }) => {
|
|
|
|
self.events
|
|
|
|
.push(Event::Moved(Vec2::new(x as u32, y as u32)));
|
|
|
|
},
|
2019-10-27 07:11:18 +00:00
|
|
|
WindowEvent::ReceivedCharacter(c) => self.events.push(Event::Char(c)),
|
|
|
|
WindowEvent::MouseInput { button, state, .. } => {
|
|
|
|
if let (true, Some(game_inputs)) =
|
|
|
|
// Mouse input not mapped to input if it is not grabbed
|
|
|
|
(
|
2021-05-16 08:03:09 +00:00
|
|
|
self.cursor_grabbed,
|
|
|
|
Window::map_input(
|
|
|
|
KeyMouse::Mouse(button),
|
|
|
|
controls,
|
|
|
|
&mut self.remapping_keybindings,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
{
|
2019-10-27 07:11:18 +00:00
|
|
|
for game_input in game_inputs {
|
|
|
|
self.events.push(Event::InputUpdate(
|
|
|
|
*game_input,
|
|
|
|
state == winit::event::ElementState::Pressed,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.events.push(Event::MouseButton(button, state));
|
|
|
|
},
|
2020-07-15 04:56:50 +00:00
|
|
|
WindowEvent::ModifiersChanged(modifiers) => self.modifiers = modifiers,
|
|
|
|
WindowEvent::KeyboardInput {
|
|
|
|
input,
|
|
|
|
is_synthetic,
|
|
|
|
..
|
|
|
|
} => {
|
|
|
|
// Ignore synthetic tab presses so that we don't get tabs when alt-tabbing back
|
|
|
|
// into the window
|
|
|
|
if matches!(
|
|
|
|
input.virtual_keycode,
|
|
|
|
Some(winit::event::VirtualKeyCode::Tab)
|
|
|
|
) && is_synthetic
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Ignore Alt-F4 so we don't try to do anything heavy like take a screenshot
|
|
|
|
// when the window is about to close
|
|
|
|
if matches!(input, winit::event::KeyboardInput {
|
|
|
|
state: winit::event::ElementState::Pressed,
|
|
|
|
virtual_keycode: Some(winit::event::VirtualKeyCode::F4),
|
|
|
|
..
|
|
|
|
}) && self.modifiers.alt()
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-16 09:40:08 +00:00
|
|
|
let input_key = match input.virtual_keycode {
|
|
|
|
Some(key) => KeyMouse::Key(key),
|
|
|
|
None => KeyMouse::ScanKey(input.scancode),
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(game_inputs) =
|
|
|
|
Window::map_input(input_key, controls, &mut self.remapping_keybindings)
|
|
|
|
{
|
|
|
|
for game_input in game_inputs {
|
|
|
|
match game_input {
|
|
|
|
GameInput::Fullscreen => {
|
|
|
|
if input.state == winit::event::ElementState::Pressed
|
|
|
|
&& !Self::is_pressed(
|
2019-10-27 07:11:18 +00:00
|
|
|
&mut self.keypress_map,
|
|
|
|
GameInput::Fullscreen,
|
2020-12-16 09:40:08 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
self.toggle_fullscreen = !self.toggle_fullscreen;
|
|
|
|
}
|
|
|
|
Self::set_pressed(
|
|
|
|
&mut self.keypress_map,
|
|
|
|
GameInput::Fullscreen,
|
|
|
|
input.state,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
GameInput::Screenshot => {
|
|
|
|
self.take_screenshot = input.state
|
|
|
|
== winit::event::ElementState::Pressed
|
|
|
|
&& !Self::is_pressed(
|
2019-10-27 07:11:18 +00:00
|
|
|
&mut self.keypress_map,
|
|
|
|
GameInput::Screenshot,
|
|
|
|
);
|
2020-12-16 09:40:08 +00:00
|
|
|
Self::set_pressed(
|
|
|
|
&mut self.keypress_map,
|
|
|
|
GameInput::Screenshot,
|
|
|
|
input.state,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
_ => self.events.push(Event::InputUpdate(
|
|
|
|
*game_input,
|
|
|
|
input.state == winit::event::ElementState::Pressed,
|
|
|
|
)),
|
2019-10-27 07:11:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
WindowEvent::Focused(state) => {
|
|
|
|
self.focused = state;
|
|
|
|
self.events.push(Event::Focused(state));
|
|
|
|
},
|
|
|
|
WindowEvent::CursorMoved { position, .. } => {
|
2021-03-27 18:55:26 +00:00
|
|
|
if self.cursor_grabbed {
|
2021-03-28 21:24:02 +00:00
|
|
|
self.center_cursor();
|
2021-03-27 18:55:26 +00:00
|
|
|
} else {
|
|
|
|
self.cursor_position = position;
|
|
|
|
}
|
2019-10-27 07:11:18 +00:00
|
|
|
},
|
2020-12-19 04:07:38 +00:00
|
|
|
WindowEvent::MouseWheel { delta, .. } if self.cursor_grabbed && self.focused => {
|
|
|
|
const DIFFERENCE_FROM_DEVICE_EVENT_ON_X11: f32 = -15.0;
|
|
|
|
self.events.push(Event::Zoom({
|
|
|
|
let y = match delta {
|
|
|
|
winit::event::MouseScrollDelta::LineDelta(_x, y) => y,
|
|
|
|
// TODO: Check to see if there is a better way to find the "line
|
|
|
|
// height" than just hardcoding 16.0 pixels. Alternately we could
|
|
|
|
// get rid of this and have the user set zoom sensitivity, since
|
|
|
|
// it's unlikely people would expect a configuration file to work
|
|
|
|
// across operating systems.
|
|
|
|
winit::event::MouseScrollDelta::PixelDelta(pos) => (pos.y / 16.0) as f32,
|
|
|
|
};
|
|
|
|
y * (self.zoom_sensitivity as f32 / 100.0)
|
|
|
|
* if self.zoom_inversion { -1.0 } else { 1.0 }
|
|
|
|
* DIFFERENCE_FROM_DEVICE_EVENT_ON_X11
|
|
|
|
}))
|
|
|
|
},
|
2019-10-27 07:11:18 +00:00
|
|
|
_ => {},
|
|
|
|
}
|
2019-01-02 21:25:01 +00:00
|
|
|
}
|
|
|
|
|
2020-03-10 21:00:13 +00:00
|
|
|
/// Moves cursor by an offset
|
2019-10-27 07:11:18 +00:00
|
|
|
pub fn offset_cursor(&self, d: Vec2<f32>) {
|
|
|
|
if d != Vec2::zero() {
|
2020-09-26 15:43:59 +00:00
|
|
|
if let Err(err) = self
|
|
|
|
.window
|
|
|
|
.set_cursor_position(winit::dpi::LogicalPosition::new(
|
|
|
|
d.x as f64 + self.cursor_position.x,
|
|
|
|
d.y as f64 + self.cursor_position.y,
|
|
|
|
))
|
2019-10-27 07:11:18 +00:00
|
|
|
{
|
|
|
|
error!("Error setting cursor position: {:?}", err);
|
|
|
|
}
|
|
|
|
}
|
2020-03-10 21:00:13 +00:00
|
|
|
}
|
|
|
|
|
2020-02-01 20:39:39 +00:00
|
|
|
pub fn is_cursor_grabbed(&self) -> bool { self.cursor_grabbed }
|
2019-01-12 01:14:58 +00:00
|
|
|
|
2019-01-23 22:21:47 +00:00
|
|
|
pub fn grab_cursor(&mut self, grab: bool) {
|
|
|
|
self.cursor_grabbed = grab;
|
2020-09-26 15:43:59 +00:00
|
|
|
self.window.set_cursor_visible(!grab);
|
|
|
|
let _ = self.window.set_cursor_grab(grab);
|
2019-01-12 01:14:58 +00:00
|
|
|
}
|
2019-02-16 03:01:42 +00:00
|
|
|
|
2021-03-28 21:24:02 +00:00
|
|
|
/// Moves mouse cursor to center of screen
|
|
|
|
/// based on the window dimensions
|
|
|
|
pub fn center_cursor(&self) {
|
|
|
|
let dimensions: Vec2<f64> = self.logical_size();
|
|
|
|
|
2021-02-04 03:30:43 +00:00
|
|
|
if let Err(err) = self
|
|
|
|
.window
|
|
|
|
.set_cursor_position(winit::dpi::PhysicalPosition::new(
|
|
|
|
dimensions[0] / (2_f64),
|
|
|
|
dimensions[1] / (2_f64),
|
|
|
|
))
|
2021-03-28 21:24:02 +00:00
|
|
|
{
|
|
|
|
error!("Error centering cursor position: {:?}", err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-19 02:57:14 +00:00
|
|
|
pub fn toggle_fullscreen(&mut self, settings: &mut Settings, config_dir: &std::path::Path) {
|
2020-09-07 05:12:15 +00:00
|
|
|
let fullscreen = FullScreenSettings {
|
|
|
|
enabled: !self.is_fullscreen(),
|
|
|
|
..settings.graphics.fullscreen
|
|
|
|
};
|
|
|
|
|
|
|
|
self.set_fullscreen_mode(fullscreen);
|
|
|
|
settings.graphics.fullscreen = fullscreen;
|
2021-07-19 02:57:14 +00:00
|
|
|
settings.save_to_file_warn(config_dir);
|
2020-01-02 03:48:11 +00:00
|
|
|
}
|
|
|
|
|
2020-09-07 05:12:15 +00:00
|
|
|
pub fn is_fullscreen(&self) -> bool { self.fullscreen.enabled }
|
2019-05-18 21:16:35 +00:00
|
|
|
|
2021-06-16 05:36:14 +00:00
|
|
|
/// Select a video mode that fits the specified requirements
|
|
|
|
/// Returns None if a matching video mode doesn't exist or if
|
|
|
|
/// the current monitor can't be retrieved
|
|
|
|
fn select_video_mode_rec(
|
2020-08-09 20:30:22 +00:00
|
|
|
&self,
|
|
|
|
resolution: [u16; 2],
|
|
|
|
bit_depth: Option<u16>,
|
|
|
|
refresh_rate: Option<u16>,
|
|
|
|
correct_res: Option<Vec<VideoMode>>,
|
|
|
|
correct_depth: Option<Option<VideoMode>>,
|
|
|
|
correct_rate: Option<Option<VideoMode>>,
|
|
|
|
) -> Option<VideoMode> {
|
|
|
|
// if a previous iteration of this method filtered the available video modes for
|
|
|
|
// the correct resolution already, load that value, otherwise filter it
|
|
|
|
// in this iteration
|
2021-06-16 05:36:14 +00:00
|
|
|
let correct_res = match correct_res {
|
|
|
|
Some(correct_res) => correct_res,
|
|
|
|
None => self
|
|
|
|
.window
|
|
|
|
.current_monitor()?
|
2020-08-09 20:30:22 +00:00
|
|
|
.video_modes()
|
|
|
|
.filter(|mode| mode.size().width == resolution[0] as u32)
|
|
|
|
.filter(|mode| mode.size().height == resolution[1] as u32)
|
2021-06-16 05:36:14 +00:00
|
|
|
.collect(),
|
|
|
|
};
|
2020-08-09 20:30:22 +00:00
|
|
|
|
|
|
|
match bit_depth {
|
|
|
|
// A bit depth is given
|
|
|
|
Some(depth) => {
|
|
|
|
// analogous to correct_res
|
|
|
|
let correct_depth = correct_depth.unwrap_or_else(|| {
|
|
|
|
correct_res
|
|
|
|
.iter()
|
|
|
|
.find(|mode| mode.bit_depth() == depth)
|
|
|
|
.cloned()
|
|
|
|
});
|
|
|
|
|
|
|
|
match refresh_rate {
|
|
|
|
// A bit depth and a refresh rate is given
|
|
|
|
Some(rate) => {
|
|
|
|
// analogous to correct_res
|
|
|
|
let correct_rate = correct_rate.unwrap_or_else(|| {
|
|
|
|
correct_res
|
|
|
|
.iter()
|
|
|
|
.find(|mode| mode.refresh_rate() == rate)
|
|
|
|
.cloned()
|
|
|
|
});
|
|
|
|
|
|
|
|
// if no video mode with the given bit depth and refresh rate exists, fall
|
|
|
|
// back to a video mode that fits the resolution and either bit depth or
|
|
|
|
// refresh rate depending on which parameter was causing the correct video
|
|
|
|
// mode not to be found
|
|
|
|
correct_res
|
|
|
|
.iter()
|
|
|
|
.filter(|mode| mode.bit_depth() == depth)
|
|
|
|
.find(|mode| mode.refresh_rate() == rate)
|
|
|
|
.cloned()
|
|
|
|
.or_else(|| {
|
|
|
|
if correct_depth.is_none() && correct_rate.is_none() {
|
|
|
|
warn!(
|
|
|
|
"Bit depth and refresh rate specified in settings are \
|
|
|
|
incompatible with the monitor. Choosing highest bit \
|
|
|
|
depth and refresh rate possible instead."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.select_video_mode_rec(
|
|
|
|
resolution,
|
|
|
|
correct_depth.is_some().then_some(depth),
|
|
|
|
correct_rate.is_some().then_some(rate),
|
|
|
|
Some(correct_res),
|
|
|
|
Some(correct_depth),
|
|
|
|
Some(correct_rate),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
},
|
|
|
|
// A bit depth and no refresh rate is given
|
|
|
|
// if no video mode with the given bit depth exists, fall
|
|
|
|
// back to a video mode that fits only the resolution
|
|
|
|
None => match correct_depth {
|
|
|
|
Some(mode) => Some(mode),
|
|
|
|
None => {
|
|
|
|
warn!(
|
|
|
|
"Bit depth specified in settings is incompatible with the \
|
|
|
|
monitor. Choosing highest bit depth possible instead."
|
|
|
|
);
|
|
|
|
|
|
|
|
self.select_video_mode_rec(
|
|
|
|
resolution,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
Some(correct_res),
|
|
|
|
Some(correct_depth),
|
|
|
|
None,
|
|
|
|
)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
// No bit depth is given
|
|
|
|
None => match refresh_rate {
|
|
|
|
// No bit depth and a refresh rate is given
|
|
|
|
Some(rate) => {
|
|
|
|
// analogous to correct_res
|
|
|
|
let correct_rate = correct_rate.unwrap_or_else(|| {
|
|
|
|
correct_res
|
|
|
|
.iter()
|
|
|
|
.find(|mode| mode.refresh_rate() == rate)
|
|
|
|
.cloned()
|
|
|
|
});
|
|
|
|
|
|
|
|
// if no video mode with the given bit depth exists, fall
|
|
|
|
// back to a video mode that fits only the resolution
|
|
|
|
match correct_rate {
|
|
|
|
Some(mode) => Some(mode),
|
|
|
|
None => {
|
|
|
|
warn!(
|
|
|
|
"Refresh rate specified in settings is incompatible with the \
|
|
|
|
monitor. Choosing highest refresh rate possible instead."
|
|
|
|
);
|
|
|
|
|
|
|
|
self.select_video_mode_rec(
|
|
|
|
resolution,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
Some(correct_res),
|
|
|
|
None,
|
|
|
|
Some(correct_rate),
|
|
|
|
)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
// No bit depth and no refresh rate is given
|
|
|
|
// get the video mode with the specified resolution and the max bit depth and
|
|
|
|
// refresh rate
|
|
|
|
None => correct_res
|
|
|
|
.into_iter()
|
|
|
|
// Prefer bit depth over refresh rate
|
|
|
|
.sorted_by_key(|mode| mode.bit_depth())
|
|
|
|
.max_by_key(|mode| mode.refresh_rate()),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-16 05:36:14 +00:00
|
|
|
fn select_video_mode(
|
2020-08-09 20:30:22 +00:00
|
|
|
&self,
|
|
|
|
resolution: [u16; 2],
|
|
|
|
bit_depth: Option<u16>,
|
|
|
|
refresh_rate: Option<u16>,
|
2021-06-16 05:36:14 +00:00
|
|
|
) -> Option<VideoMode> {
|
2020-08-09 20:30:22 +00:00
|
|
|
// (resolution, bit depth, refresh rate) represents a video mode
|
|
|
|
// spec: as specified
|
|
|
|
// max: maximum value available
|
|
|
|
|
|
|
|
// order of fallbacks as follows:
|
|
|
|
// (spec, spec, spec)
|
|
|
|
// (spec, spec, max), (spec, max, spec)
|
|
|
|
// (spec, max, max)
|
|
|
|
// (max, max, max)
|
2021-06-16 05:36:14 +00:00
|
|
|
match self.select_video_mode_rec(resolution, bit_depth, refresh_rate, None, None, None) {
|
|
|
|
Some(mode) => Some(mode),
|
|
|
|
// if there is no video mode with the specified resolution,
|
|
|
|
// fall back to the video mode with max resolution, bit depth and refresh rate
|
|
|
|
None => {
|
2020-08-09 20:30:22 +00:00
|
|
|
warn!(
|
|
|
|
"Resolution specified in settings is incompatible with the monitor. Choosing \
|
|
|
|
highest resolution possible instead."
|
|
|
|
);
|
2021-06-16 05:36:14 +00:00
|
|
|
if let Some(monitor) = self.window.current_monitor() {
|
|
|
|
let mode = monitor
|
|
|
|
.video_modes()
|
|
|
|
// Prefer bit depth over refresh rate
|
|
|
|
.sorted_by_key(|mode| mode.refresh_rate())
|
|
|
|
.sorted_by_key(|mode| mode.bit_depth())
|
|
|
|
.max_by_key(|mode| mode.size().width);
|
|
|
|
|
|
|
|
if mode.is_none() {
|
|
|
|
warn!("Failed to select video mode, no video modes available!!")
|
|
|
|
}
|
2020-08-09 20:30:22 +00:00
|
|
|
|
2021-06-16 05:36:14 +00:00
|
|
|
mode
|
|
|
|
} else {
|
|
|
|
warn!("Failed to select video mode, can't get the current monitor!");
|
|
|
|
None
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
2020-08-09 20:30:22 +00:00
|
|
|
}
|
|
|
|
|
2020-09-07 05:12:15 +00:00
|
|
|
pub fn set_fullscreen_mode(&mut self, fullscreen: FullScreenSettings) {
|
2020-11-29 02:14:22 +00:00
|
|
|
let window = &self.window;
|
2019-05-18 21:16:35 +00:00
|
|
|
self.fullscreen = fullscreen;
|
2020-10-26 18:52:45 +00:00
|
|
|
window.set_fullscreen(fullscreen.enabled.then(|| match fullscreen.mode {
|
|
|
|
FullscreenMode::Exclusive => {
|
2021-06-16 05:36:14 +00:00
|
|
|
if let Some(video_mode) = self.select_video_mode(
|
2020-10-26 18:52:45 +00:00
|
|
|
fullscreen.resolution,
|
|
|
|
fullscreen.bit_depth,
|
|
|
|
fullscreen.refresh_rate,
|
2021-06-16 05:36:14 +00:00
|
|
|
) {
|
|
|
|
winit::window::Fullscreen::Exclusive(video_mode)
|
|
|
|
} else {
|
|
|
|
warn!(
|
|
|
|
"Failed to select a video mode for exclusive fullscreen. Falling back to \
|
|
|
|
borderless fullscreen."
|
|
|
|
);
|
|
|
|
winit::window::Fullscreen::Borderless(None)
|
|
|
|
}
|
2020-10-26 18:52:45 +00:00
|
|
|
},
|
|
|
|
FullscreenMode::Borderless => {
|
2021-06-16 05:36:14 +00:00
|
|
|
// None here will fullscreen on the current monitor
|
|
|
|
winit::window::Fullscreen::Borderless(None)
|
2020-10-26 18:52:45 +00:00
|
|
|
},
|
|
|
|
}));
|
2019-05-18 21:16:35 +00:00
|
|
|
}
|
|
|
|
|
2020-02-01 20:39:39 +00:00
|
|
|
pub fn needs_refresh_resize(&mut self) { self.needs_refresh_resize = true; }
|
2019-03-28 04:42:04 +00:00
|
|
|
|
2019-03-03 23:55:07 +00:00
|
|
|
pub fn logical_size(&self) -> Vec2<f64> {
|
2020-02-20 23:41:51 +00:00
|
|
|
let (w, h) = self
|
|
|
|
.window
|
|
|
|
.inner_size()
|
2020-09-26 15:43:59 +00:00
|
|
|
.to_logical::<f64>(self.window.scale_factor())
|
2020-02-20 23:41:51 +00:00
|
|
|
.into();
|
2019-03-03 23:55:07 +00:00
|
|
|
Vec2::new(w, h)
|
2019-02-16 03:01:42 +00:00
|
|
|
}
|
2019-05-01 22:15:43 +00:00
|
|
|
|
2020-01-02 03:48:11 +00:00
|
|
|
pub fn set_size(&mut self, new_size: Vec2<u16>) {
|
2020-09-26 15:43:59 +00:00
|
|
|
self.window.set_inner_size(winit::dpi::LogicalSize::new(
|
|
|
|
new_size.x as f64,
|
|
|
|
new_size.y as f64,
|
|
|
|
));
|
2020-01-02 03:48:11 +00:00
|
|
|
}
|
|
|
|
|
2019-10-27 07:11:18 +00:00
|
|
|
pub fn send_event(&mut self, event: Event) { self.events.push(event) }
|
|
|
|
|
2020-06-05 20:53:15 +00:00
|
|
|
pub fn take_screenshot(&mut self, settings: &Settings) {
|
2021-03-20 08:03:42 +00:00
|
|
|
let sender = self.message_sender.clone();
|
|
|
|
let mut path = settings.screenshots_path.clone();
|
|
|
|
self.renderer.create_screenshot(move |image| {
|
|
|
|
use std::time::SystemTime;
|
|
|
|
// Check if folder exists and create it if it does not
|
|
|
|
if !path.exists() {
|
|
|
|
if let Err(e) = std::fs::create_dir_all(&path) {
|
|
|
|
warn!(?e, "Couldn't create folder for screenshot");
|
|
|
|
let _result =
|
|
|
|
sender.send(String::from("Couldn't create folder for screenshot"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
path.push(format!(
|
|
|
|
"screenshot_{}.png",
|
|
|
|
SystemTime::now()
|
|
|
|
.duration_since(SystemTime::UNIX_EPOCH)
|
|
|
|
.map(|d| d.as_millis())
|
|
|
|
.unwrap_or(0)
|
|
|
|
));
|
|
|
|
// Try to save the image
|
|
|
|
if let Err(e) = image.into_rgba8().save(&path) {
|
|
|
|
warn!(?e, "Couldn't save screenshot");
|
|
|
|
let _result = sender.send(String::from("Couldn't save screenshot"));
|
|
|
|
} else {
|
|
|
|
let _result =
|
|
|
|
sender.send(format!("Screenshot saved to {}", path.to_string_lossy()));
|
|
|
|
}
|
|
|
|
});
|
2019-05-18 21:16:35 +00:00
|
|
|
}
|
2019-06-30 04:20:50 +00:00
|
|
|
|
2019-10-26 14:50:01 +00:00
|
|
|
fn is_pressed(
|
2019-10-27 07:11:18 +00:00
|
|
|
map: &mut HashMap<GameInput, winit::event::ElementState>,
|
2019-10-26 14:50:01 +00:00
|
|
|
input: GameInput,
|
|
|
|
) -> bool {
|
|
|
|
*(map
|
|
|
|
.entry(input)
|
2019-10-27 07:11:18 +00:00
|
|
|
.or_insert(winit::event::ElementState::Released))
|
|
|
|
== winit::event::ElementState::Pressed
|
2019-06-30 04:20:50 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 11:15:26 +00:00
|
|
|
fn set_pressed(
|
2019-10-27 07:11:18 +00:00
|
|
|
map: &mut HashMap<GameInput, winit::event::ElementState>,
|
2019-06-30 04:20:50 +00:00
|
|
|
input: GameInput,
|
2019-10-27 07:11:18 +00:00
|
|
|
state: winit::event::ElementState,
|
2019-06-30 04:20:50 +00:00
|
|
|
) {
|
|
|
|
map.insert(input, state);
|
|
|
|
}
|
2020-04-08 17:36:37 +00:00
|
|
|
|
|
|
|
// Function used to handle Mouse and Key events. It first checks if we're in
|
|
|
|
// remapping mode for a specific GameInput. If we are, we modify the binding
|
|
|
|
// of that GameInput with the KeyMouse passed. Else, we return an iterator of
|
|
|
|
// the GameInputs for that KeyMouse.
|
|
|
|
fn map_input<'a>(
|
|
|
|
key_mouse: KeyMouse,
|
|
|
|
controls: &'a mut ControlSettings,
|
|
|
|
remapping: &mut Option<GameInput>,
|
|
|
|
) -> Option<impl Iterator<Item = &'a GameInput>> {
|
|
|
|
match *remapping {
|
2019-10-27 07:11:18 +00:00
|
|
|
// TODO: save settings
|
2020-04-08 17:36:37 +00:00
|
|
|
Some(game_input) => {
|
|
|
|
controls.modify_binding(game_input, key_mouse);
|
|
|
|
*remapping = None;
|
|
|
|
None
|
|
|
|
},
|
2021-03-12 11:10:42 +00:00
|
|
|
None => controls
|
|
|
|
.get_associated_game_inputs(&key_mouse)
|
|
|
|
.map(|game_inputs| game_inputs.iter()),
|
2020-04-08 17:36:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_keybinding_mode(&mut self, game_input: GameInput) {
|
|
|
|
self.remapping_keybindings = Some(game_input);
|
|
|
|
}
|
2019-12-15 17:13:52 +00:00
|
|
|
|
2020-11-29 02:14:22 +00:00
|
|
|
pub fn window(&self) -> &winit::window::Window { &self.window }
|
2020-06-27 17:44:43 +00:00
|
|
|
|
|
|
|
pub fn modifiers(&self) -> winit::event::ModifiersState { self.modifiers }
|
|
|
|
|
|
|
|
pub fn scale_factor(&self) -> f64 { self.scale_factor }
|
2019-01-02 21:25:01 +00:00
|
|
|
}
|
2020-09-07 05:12:15 +00:00
|
|
|
|
|
|
|
#[derive(Copy, Clone, Hash, Eq, PartialEq, Debug, Serialize, Deserialize)]
|
|
|
|
pub enum FullscreenMode {
|
|
|
|
Exclusive,
|
2020-11-11 17:37:26 +00:00
|
|
|
#[serde(other)]
|
2020-09-07 05:12:15 +00:00
|
|
|
Borderless,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for FullscreenMode {
|
|
|
|
fn default() -> Self { FullscreenMode::Borderless }
|
|
|
|
}
|
|
|
|
#[derive(PartialEq, Clone, Copy, Debug, Serialize, Deserialize)]
|
2020-11-11 17:37:26 +00:00
|
|
|
#[serde(default)]
|
2020-09-07 05:12:15 +00:00
|
|
|
pub struct FullScreenSettings {
|
|
|
|
pub enabled: bool,
|
|
|
|
pub mode: FullscreenMode,
|
|
|
|
pub resolution: [u16; 2],
|
|
|
|
pub bit_depth: Option<u16>,
|
|
|
|
pub refresh_rate: Option<u16>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for FullScreenSettings {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
enabled: false,
|
|
|
|
mode: FullscreenMode::Borderless,
|
|
|
|
resolution: [1920, 1080],
|
|
|
|
bit_depth: None,
|
|
|
|
refresh_rate: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|