Add lines and boxes to printer.

Dialog now draws its box.
This commit is contained in:
Alexandre Bury 2015-05-20 10:31:38 -07:00
parent 7d301e1980
commit 919e60d4ce
3 changed files with 42 additions and 4 deletions

View File

@ -20,6 +20,32 @@ impl Printer {
ncurses::mvwprintw(self.win, (p.y + self.offset.y) as i32, (p.x + self.offset.x) as i32, text); ncurses::mvwprintw(self.win, (p.y + self.offset.y) as i32, (p.x + self.offset.x) as i32, text);
} }
/// Prints a vertical line using the given character.
pub fn print_vline<T: ToVec2>(&self, c: char, start: T, len: u32) {
let p = start.to_vec2();
ncurses::mvwvline(self.win, (p.y + self.offset.y) as i32, (p.x + self.offset.x) as i32, c as u64, len as i32);
}
/// Prints a horizontal line using the given character.
pub fn print_hline<T: ToVec2>(&self, c: char, start: T, len: u32) {
let p = start.to_vec2();
ncurses::mvwhline(self.win, (p.y + self.offset.y) as i32, (p.x + self.offset.x) as i32, c as u64, len as i32);
}
pub fn print_box<T: ToVec2>(&self, start: T, size: T, corners: char, horizontal: char, vertical: char) {
let start_v = start.to_vec2();
let size_v = size.to_vec2() - (1,1);
self.print(start_v, &corners.to_string());
self.print(start_v + size_v.keep_x(), &corners.to_string());
self.print(start_v + size_v.keep_y(), &corners.to_string());
self.print(start_v + size_v, &corners.to_string());
self.print_hline(horizontal, start_v + (1,0), size_v.x - 1);
self.print_hline(horizontal, start_v + (1,0) + size_v.keep_y(), size_v.x - 1);
self.print_vline(vertical, start_v + (0,1), size_v.y - 1);
self.print_vline(vertical, start_v + (0,1) + size_v.keep_x(), size_v.y - 1);
}
/// Returns a printer on a subset of this one's area. /// Returns a printer on a subset of this one's area.
pub fn sub_printer<S: ToVec2>(&self, offset: S, size: S) -> Printer { pub fn sub_printer<S: ToVec2>(&self, offset: S, size: S) -> Printer {
let offset_v = offset.to_vec2(); let offset_v = offset.to_vec2();

View File

@ -56,6 +56,12 @@ impl ToVec2 for Vec2 {
} }
} }
impl ToVec2 for (i32,i32) {
fn to_vec2(self) -> Vec2 {
(self.0 as u32, self.1 as u32).to_vec2()
}
}
impl ToVec2 for (u32,u32) { impl ToVec2 for (u32,u32) {
fn to_vec2(self) -> Vec2 { fn to_vec2(self) -> Vec2 {
Vec2::new(self.0, self.1) Vec2::new(self.0, self.1)

View File

@ -79,10 +79,8 @@ impl View for Dialog {
self.content.draw(&printer.sub_printer(self.borders.top_left() + self.padding.top_left(), inner_size), focused && self.focus == Focus::Content); self.content.draw(&printer.sub_printer(self.borders.top_left() + self.padding.top_left(), inner_size), focused && self.focus == Focus::Content);
printer.print(Vec2::new(0,0), "+"); printer.print_box(Vec2::new(0,0), printer.size, '+', '-', '|');
printer.print(Vec2::new(printer.size.x-1, 0), "+");
printer.print(Vec2::new(0, printer.size.y-1), "+");
printer.print(Vec2::new(printer.size.x-1, printer.size.y-1), "+");
} }
fn get_min_size(&self, req: SizeRequest) -> Vec2 { fn get_min_size(&self, req: SizeRequest) -> Vec2 {
@ -135,6 +133,14 @@ impl View for Dialog {
}, },
Focus::Button(i) => match self.buttons[i].on_key_event(ch) { Focus::Button(i) => match self.buttons[i].on_key_event(ch) {
EventResult::Ignored => match ch { EventResult::Ignored => match ch {
ncurses::KEY_UP => {
if self.content.take_focus() {
self.focus = Focus::Content;
EventResult::consume()
} else {
EventResult::Ignored
}
},
ncurses::KEY_RIGHT if i+1 < self.buttons.len() => { ncurses::KEY_RIGHT if i+1 < self.buttons.len() => {
self.focus = Focus::Button(i+1); self.focus = Focus::Button(i+1);
EventResult::consume() EventResult::consume()