From 919e60d4cef4569d36a5809052c49c2c55f47a38 Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Wed, 20 May 2015 10:31:38 -0700 Subject: [PATCH] Add lines and boxes to printer. Dialog now draws its box. --- src/printer.rs | 26 ++++++++++++++++++++++++++ src/vec.rs | 6 ++++++ src/view/dialog.rs | 14 ++++++++++---- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/printer.rs b/src/printer.rs index 259d87d..623dc41 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -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); } + /// Prints a vertical line using the given character. + pub fn print_vline(&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(&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(&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. pub fn sub_printer(&self, offset: S, size: S) -> Printer { let offset_v = offset.to_vec2(); diff --git a/src/vec.rs b/src/vec.rs index 9779f89..cf3c906 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -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) { fn to_vec2(self) -> Vec2 { Vec2::new(self.0, self.1) diff --git a/src/view/dialog.rs b/src/view/dialog.rs index 053e144..f6b8abc 100644 --- a/src/view/dialog.rs +++ b/src/view/dialog.rs @@ -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); - printer.print(Vec2::new(0,0), "+"); - 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), "+"); + printer.print_box(Vec2::new(0,0), printer.size, '+', '-', '|'); + } 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) { 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() => { self.focus = Focus::Button(i+1); EventResult::consume()