From 854a0701d15d4d30dc4c485020bc589296acf7ac Mon Sep 17 00:00:00 2001 From: nabijaczleweli Date: Sat, 2 Jan 2016 11:47:50 +0100 Subject: [PATCH] Add new terminal size to Event::Resize --- examples/simple.rs | 10 ++++++++-- src/terminal/input.rs | 12 ++++++++++-- src/terminal/mod.rs | 9 ++++++++- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/examples/simple.rs b/examples/simple.rs index ca63a505..470d264c 100644 --- a/examples/simple.rs +++ b/examples/simple.rs @@ -2,11 +2,13 @@ extern crate bear_lib_terminal; use bear_lib_terminal::Color; use bear_lib_terminal::geometry::Point; -use bear_lib_terminal::terminal::{self, Event, KeyCode}; +use bear_lib_terminal::terminal::{self, config, Event, KeyCode}; fn main() { terminal::open("Simple example", 80, 30); + terminal::set(config::Window::empty().resizeable(true)); + terminal::print_xy(0, 0, "Your mom"); terminal::with_colors(Color::from_rgb(0xFA, 0xAF, 0x29), Color::from_rgb(0x05, 0x50, 0xD6), || terminal::print_xy(0, 1, "Colerd")); for (i, c) in "Coloured letters with pixel-offset!".chars().enumerate() { @@ -24,8 +26,12 @@ fn main() { terminal::refresh(); while let Some(event) = terminal::wait_event() { match event { + Event::Resize{width, height} => { + terminal::print_xy(0, 0, &*&format!("Width: {}\nHeight: {}", width, height)); + terminal::refresh(); + }, Event::Close | Event::KeyPressed{key: KeyCode::Escape, ctrl: _, shift: _} => break, - _ => (), + _ => (), } } terminal::close(); diff --git a/src/terminal/input.rs b/src/terminal/input.rs index bd18f7b8..d0312f7c 100644 --- a/src/terminal/input.rs +++ b/src/terminal/input.rs @@ -144,8 +144,16 @@ pub enum KeyCode { pub enum Event { /// Terminal window closed. Close, - /// Terminal window resized. - Resize, + /// Terminal window resized. Needs to have `window.resizeable = true` to occur. + /// + /// Note, that, as of [`40e6253`](https://bitbucket.org/cfyzium/bearlibterminal/commits/40e625311f0cccc43b94633add4dec0d6b77c2b7), + /// the terminal window is cleared when resized. + Resize{ + /// Width the terminal was resized to. + width: i32, + /// Heigth the terminal was resized to. + height: i32, + }, /// Mouse moved. /// /// If [`precise-mouse`](config/struct.Input.html#structfield.precise_mouse) is off, generated each time mouse moves from cell to cell, otherwise, diff --git a/src/terminal/mod.rs b/src/terminal/mod.rs index 9fa4c39d..bc2af66d 100644 --- a/src/terminal/mod.rs +++ b/src/terminal/mod.rs @@ -387,7 +387,7 @@ fn to_keycode(code: i32) -> Option { fn to_event(code: i32) -> Option { match code { ffi::TK_CLOSE => Some(Event::Close), - ffi::TK_RESIZED => Some(Event::Resize), + ffi::TK_RESIZED => Some(get_window_resize()), ffi::TK_MOUSE_MOVE => Some(get_mouse_move()), ffi::TK_MOUSE_SCROLL => Some(get_mouse_scroll()), _ => to_key_event(code), @@ -413,6 +413,13 @@ fn to_key_event(code: i32) -> Option { } } +fn get_window_resize() -> Event { + Event::Resize{ + width: ffi::state(ffi::TK_WIDTH), + height: ffi::state(ffi::TK_HEIGHT), + } +} + fn get_mouse_move() -> Event { Event::MouseMove{ x: ffi::state(ffi::TK_MOUSE_X),