1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
/// Scale will scale the frame buffer and the window that is being sent in when calling the update /// function. This is useful if you for example want to display a 320 x 256 window on a screen with /// much higher resolution which would result in that the window is very small. #[derive(Clone, Copy)] pub enum Scale { /// This mode checks your current screen resolution and will caluclate the largest window size /// that can be used within that limit and resize it. Useful if you have a small buffer to /// display on a high resolution screen. FitScreen, /// 1X scale (which means leave the corrdinates sent into Window::new untouched) X1, /// 2X window scale (Example: 320 x 200 -> 640 x 400) X2, /// 4X window scale (Example: 320 x 200 -> 1280 x 800) X4, /// 8X window scale (Example: 320 x 200 -> 2560 x 1600) X8, /// 16X window scale (Example: 320 x 200 -> 5120 x 3200) X16, /// 32 window scale (Example: 320 x 200 -> 10240 x 6400) X32, } /// Used for is_key_pressed and get_keys_pressed() to indicated if repeat of presses is wanted #[derive(PartialEq, Clone, Copy)] pub enum KeyRepeat { /// Use repeat Yes, /// Don't use repeat No, } /// The various mouse buttons that are availible #[derive(PartialEq, Clone, Copy)] pub enum MouseButton { /// Left mouse button Left, /// Middle mouse button Middle, /// Right mouse button Right, } /// Key is used by the get key functions to check if some keys on the keyboard has been pressed #[derive(PartialEq, Clone, Copy)] pub enum MouseMode { /// Return mouse coords from outside of the window (may be negative) Pass, /// Clamp the mouse coordinates within the window Clamp, /// Discared if the mouse is outside the window Discard, } extern crate libc; use std::os::raw; #[doc(hidden)] mod error; pub use self::error::Error; pub type Result<T> = std::result::Result<T, Error>; pub mod key; pub use key::Key as Key; pub mod os; mod mouse_handler; mod key_handler; mod window_flags; mod menu; pub use menu::Menu as Menu; pub use menu::MENU_KEY_COMMAND; pub use menu::MENU_KEY_WIN; pub use menu::MENU_KEY_SHIFT; pub use menu::MENU_KEY_CTRL; pub use menu::MENU_KEY_ALT; #[cfg(target_os = "macos")] use self::os::macos as imp; #[cfg(target_os = "windows")] use self::os::windows as imp; #[cfg(any(target_os="linux", target_os="freebsd", target_os="dragonfly", target_os="netbsd", target_os="openbsd"))] use self::os::unix as imp; pub struct Window(imp::Window); /// /// WindowOptions is creation settings for the window. By default the settings are defined for /// displayng a 32-bit buffer (no scaling of window is possible) /// pub struct WindowOptions { /// If the window should be borderless (default: false) pub borderless: bool, /// If the window should have a title (default: true) pub title: bool, /// If it should be possible to resize the window (default: false) pub resize: bool, /// Scale of the window that used in conjunction with update_with_buffer (default: X1) pub scale: Scale } /// /// Window is used to open up a window. It's possible to optionally display a 32-bit buffer when /// the widow is set as non-resizable. /// /// # Examples /// /// Open up a window and display a 32-bit RGB buffer (without error checking) /// /// ```ignore /// const WIDTH: usize = 640; /// const HEIGHT: usize = 360; /// /// let mut buffer: Vec<u32> = vec![0; WIDTH * HEIGHT]; /// /// let mut window = match Window::new("Test - Press ESC to exit", WIDTH, HEIGHT, /// WindowOptions::default()).unwrap() /// /// while window.is_open() && !window.is_key_down(Key::Escape) { /// for i in buffer.iter_mut() { /// *i = 0; // write something interesting here /// } /// window.update_with_buffer(&buffer); /// } /// ``` /// impl Window { /// /// Opens up a new window /// /// # Examples /// /// Open up a window with default settings /// /// ```ignore /// let mut window = match Window::new("Test", 640, 400, WindowOptions::default()) { /// Ok(win) => win, /// Err(err) => { /// println!("Unable to create window {}", err); /// return; /// } ///}; /// ``` /// /// Open up a window that is resizeable /// /// ```ignore /// let mut window = match Window::new("Test", 640, 400, /// WindowOptions { /// resize: true, /// ..WindowOptions::default() /// }) { /// Ok(win) => win, /// Err(err) => { /// println!("Unable to create window {}", err); /// return; /// } ///}; /// ``` pub fn new(name: &str, width: usize, height: usize, opts: WindowOptions) -> Result<Window> { imp::Window::new(name, width, height, opts).map(Window) } /// /// Returns the native handle for a window which is an opaque pointer/handle which /// dependens on the current operating system: /// /// ```ignore /// Windows HWND /// MacOS NSWindow /// X11 XWindow /// ``` /// #[inline] pub fn get_window_handle(&self) -> *mut raw::c_void { self.0.get_window_handle() } /// /// Updates the window with a 32-bit pixel buffer. Notice that the buffer needs to be at least /// the size of the created window /// /// # Examples /// /// ```ignore /// let mut buffer: Vec<u32> = vec![0; 640 * 400]; /// /// let mut window = match Window::new("Test", 640, 400, WindowOptions::default()).unwrap(); /// /// window.update_with_buffer(&buffer); /// ``` #[inline] pub fn update_with_buffer(&mut self, buffer: &[u32]) { self.0.update_with_buffer(buffer) } /// /// Updates the window (this is required to call in order to get keyboard/mouse input, etc) /// /// # Examples /// /// ```ignore /// let mut buffer: Vec<u32> = vec![0; 640 * 400]; /// /// let mut window = match Window::new("Test", 640, 400, WindowOptions::default()).unwrap(); /// /// window.update(); /// ``` #[inline] pub fn update(&mut self) { self.0.update() } /// /// Checks if the window is still open. A window can be closed by the user (by for example /// pressing the close button on the window) It's up to the user to make sure that this is /// being checked and take action depending on the state. /// /// # Examples /// /// ```ignore /// while window.is_open() { /// window.update(...) /// } /// ``` #[inline] pub fn is_open(&self) -> bool { self.0.is_open() } /// /// Sets the position of the window. This is useful if you have /// more than one window and want to align them up on the screen /// /// # Examples /// /// ```ignore /// // Moves the window to pixel position 20, 20 on the screen /// window.set_position(20, 20); /// ``` /// #[inline] pub fn set_position(&mut self, x: isize, y: isize) { self.0.set_position(x, y) } /// /// Get the current position of the mouse relative to the current window /// The coordinate system is as 0, 0 as the upper left corner /// /// # Examples /// /// ```ignore /// window.get_mouse_pos(MouseMode::Clamp).map(|mouse| { /// println!("x {} y {}", mouse.0, mouse.1); /// }); /// ``` /// #[inline] pub fn get_mouse_pos(&self, mode: MouseMode) -> Option<(f32, f32)> { self.0.get_mouse_pos(mode) } /// /// Check if a mouse button is down or not /// /// # Examples /// /// ```ignore /// let left_down = window.get_mouse_down(MouseButton::Left); /// println!("is left down? {}", left_down) /// ``` /// #[inline] pub fn get_mouse_down(&self, button: MouseButton) -> bool { self.0.get_mouse_down(button) } /// /// Get the current movement of the scroll wheel. /// Scroll wheel can mean different thing depending on the device attach. /// For example on Mac with trackpad "scroll wheel" means two finger /// swiping up/down (y axis) and to the sides (x-axis) /// When using a mouse this assumes the scroll wheel which often is only y direction. /// /// # Examples /// /// ```ignore /// window.get_scroll_wheel().map(|scroll| { /// println!("scrolling - x {} y {}", scroll.0, scroll.1); /// }); /// ``` /// /// #[inline] pub fn get_scroll_wheel(&self) -> Option<(f32, f32)> { self.0.get_scroll_wheel() } /// /// Get the current keys that are down. /// /// # Examples /// /// ```ignore /// window.get_keys().map(|keys| { /// for t in keys { /// match t { /// Key::W => println!("holding w"), /// Key::T => println!("holding t"), /// _ => (), /// } /// } /// }); /// ``` #[inline] pub fn get_keys(&self) -> Option<Vec<Key>> { self.0.get_keys() } /// /// Get the current pressed keys. Repeat can be used to control if keys should /// be repeated if down or not. /// /// # Examples /// /// ```ignore /// window.get_keys_pressed(KeyRepeat::No).map(|keys| { /// for t in keys { /// match t { /// Key::W => println!("pressed w"), /// Key::T => println!("pressed t"), /// _ => (), /// } /// } /// }); /// ``` #[inline] pub fn get_keys_pressed(&self, repeat: KeyRepeat) -> Option<Vec<Key>> { self.0.get_keys_pressed(repeat) } /// /// Check if a single key is down. /// /// # Examples /// /// ```ignore /// if window.is_key_down(Key::A) { /// println!("Key A is down"); /// } /// ``` /// #[inline] pub fn is_key_down(&self, key: Key) -> bool { self.0.is_key_down(key) } /// /// Check if a single key is pressed. KeyRepeat will control if the key should be repeated or /// not while being pressed. /// /// # Examples /// /// ```ignore /// if window.is_key_pressed(KeyRepeat::No) { /// println!("Key A is down"); /// } /// ``` /// #[inline] pub fn is_key_pressed(&self, key: Key, repeat: KeyRepeat) -> bool { self.0.is_key_pressed(key, repeat) } /// /// Sets the delay for when a key is being held before it starts being repeated the default /// value is 0.25 sec /// /// # Examples /// /// ```ignore /// window.set_key_repeat_delay(0.5) // 0.5 sec before repeat starts /// ``` /// #[inline] pub fn set_key_repeat_delay(&mut self, delay: f32) { self.0.set_key_repeat_delay(delay) } /// /// Sets the rate in between when the keys has passed the initial repeat_delay. The default /// value is 0.05 sec /// /// # Examples /// /// ```ignore /// window.set_key_repeat_rate(0.01) // 0.01 sec between keys /// ``` /// #[inline] pub fn set_key_repeat_rate(&mut self, rate: f32) { self.0.set_key_repeat_rate(rate) } /// /// Returns if this windows is the current active one /// #[inline] pub fn is_active(&mut self) -> bool { self.0.is_active() } /// /// This allows adding menus to your windows. As menus behaves a bit diffrently depending on /// Operating system here is how it works. See [Menu] for description on each field. /// /// ```ignore /// Windows: /// Each window has their own menu and shortcuts are active depending on active window. /// Mac: /// As Mac uses one menu for the whole program the menu will change depending /// on which window you have active. /// Linux/BSD/etc: /// Menus aren't supported as they depend on each WindowManager and is outside of the /// scope for this library to support. /// ``` /// #[inline] pub fn add_menu(&mut self, menu_name: &str, menu: &Vec<Menu>) -> Result<()> { self.0.add_menu(menu_name, menu) } /// /// Updates an existing menu created with [add_menu] /// #[inline] pub fn update_menu(&mut self, menu_name: &str, menu: &Vec<Menu>) -> Result<()> { self.0.update_menu(menu_name, menu) } /// /// Remove a menu that has been added with [add_menu] /// #[inline] pub fn remove_menu(&mut self, menu_name: &str) -> Result<()> { self.0.remove_menu(menu_name) } /// /// Check if a menu item has been pressed /// #[inline] pub fn is_menu_pressed(&mut self) -> Option<usize> { self.0.is_menu_pressed() } } // Impl for WindowOptions #[doc(hidden)] impl Default for WindowOptions { fn default() -> WindowOptions { WindowOptions { borderless: false, title: true, resize: false, scale: Scale::X1, } } }