Use usize for Vec2 instead of u32

Much less casts needed now.
This commit is contained in:
Alexandre Bury 2015-05-25 14:46:29 -07:00
parent cb03f79e24
commit dca9d7f662
8 changed files with 40 additions and 40 deletions

View File

@ -176,8 +176,8 @@ impl Cursive {
ncurses::getmaxyx(ncurses::stdscr, &mut y, &mut x); ncurses::getmaxyx(ncurses::stdscr, &mut y, &mut x);
Vec2 { Vec2 {
x: x as u32, x: x as usize,
y: y as u32, y: y as usize,
} }
} }

View File

@ -3,18 +3,18 @@ use vec::Vec2;
/// Fixed margins around a rectangular view. /// Fixed margins around a rectangular view.
pub struct Margins { pub struct Margins {
/// Left margin /// Left margin
pub left: u32, pub left: usize,
/// Right margin /// Right margin
pub right: u32, pub right: usize,
/// Top margin /// Top margin
pub top: u32, pub top: usize,
/// Bottom margin /// Bottom margin
pub bottom: u32, pub bottom: usize,
} }
impl Margins { impl Margins {
/// Creates new margins. /// Creates new margins.
pub fn new(left: u32, right: u32, top: u32, bottom: u32) -> Self { pub fn new(left: usize, right: usize, top: usize, bottom: usize) -> Self {
Margins { Margins {
left: left, left: left,
right: right, right: right,
@ -24,12 +24,12 @@ impl Margins {
} }
/// Returns left + right. /// Returns left + right.
pub fn horizontal(&self) -> u32 { pub fn horizontal(&self) -> usize {
self.left + self.right self.left + self.right
} }
/// Returns top + bottom. /// Returns top + bottom.
pub fn vertical(&self) -> u32 { pub fn vertical(&self) -> usize {
self.top + self.bottom self.top + self.bottom
} }

View File

@ -29,7 +29,7 @@ impl Printer {
let p = pos.to_vec2(); let p = pos.to_vec2();
if p.y >= self.size.y || p.x >= self.size.x { return; } if p.y >= self.size.y || p.x >= self.size.x { return; }
// Do we have enough room for the entire line? // Do we have enough room for the entire line?
let room = (self.size.x - p.x) as usize; let room = self.size.x - p.x;
// We want the number of CHARACTERS, not bytes. // We want the number of CHARACTERS, not bytes.
let text = match text.char_indices().nth(room) { let text = match text.char_indices().nth(room) {
Some((i,_)) => &text[..i], Some((i,_)) => &text[..i],
@ -40,7 +40,7 @@ impl Printer {
} }
/// Prints a vertical line using the given character. /// Prints a vertical line using the given character.
pub fn print_vline<T: ToVec2>(&self, start: T, len: u32, c: u64) { pub fn print_vline<T: ToVec2>(&self, start: T, len: usize, c: u64) {
let p = start.to_vec2(); let p = start.to_vec2();
if p.y > self.size.y || p.x > self.size.x { return; } if p.y > self.size.y || p.x > self.size.x { return; }
let len = min(len, self.size.y - p.y); let len = min(len, self.size.y - p.y);
@ -50,7 +50,7 @@ impl Printer {
} }
/// Prints a horizontal line using the given character. /// Prints a horizontal line using the given character.
pub fn print_hline<T: ToVec2>(&self, start: T, len: u32, c: u64) { pub fn print_hline<T: ToVec2>(&self, start: T, len: usize, c: u64) {
let p = start.to_vec2(); let p = start.to_vec2();
if p.y > self.size.y || p.x > self.size.x { return; } if p.y > self.size.y || p.x > self.size.x { return; }
let len = min(len, self.size.x - p.x); let len = min(len, self.size.x - p.x);

View File

@ -7,14 +7,14 @@ use std::cmp::{min,max};
#[derive(Clone,Copy)] #[derive(Clone,Copy)]
pub struct Vec2 { pub struct Vec2 {
/// X coordinate (column), from left to right. /// X coordinate (column), from left to right.
pub x: u32, pub x: usize,
/// Y coordinate (row), from top to bottom. /// Y coordinate (row), from top to bottom.
pub y: u32, pub y: usize,
} }
impl Vec2 { impl Vec2 {
/// Creates a new Vec2 from coordinates. /// Creates a new Vec2 from coordinates.
pub fn new(x: u32, y: u32) -> Self { pub fn new(x: usize, y: usize) -> Self {
Vec2 { Vec2 {
x: x, x: x,
y: y, y: y,
@ -61,19 +61,19 @@ impl ToVec2 for Vec2 {
impl ToVec2 for (i32,i32) { impl ToVec2 for (i32,i32) {
fn to_vec2(self) -> Vec2 { fn to_vec2(self) -> Vec2 {
(self.0 as u32, self.1 as u32).to_vec2() (self.0 as usize, self.1 as usize).to_vec2()
}
}
impl ToVec2 for (u32,u32) {
fn to_vec2(self) -> Vec2 {
Vec2::new(self.0, self.1)
} }
} }
impl ToVec2 for (usize,usize) { impl ToVec2 for (usize,usize) {
fn to_vec2(self) -> Vec2 { fn to_vec2(self) -> Vec2 {
Vec2::new(self.0 as u32, self.1 as u32) Vec2::new(self.0, self.1)
}
}
impl ToVec2 for (u32,u32) {
fn to_vec2(self) -> Vec2 {
Vec2::new(self.0 as usize, self.1 as usize)
} }
} }
@ -101,10 +101,10 @@ impl <T: ToVec2> Sub<T> for Vec2 {
} }
} }
impl Div<u32> for Vec2 { impl Div<usize> for Vec2 {
type Output = Vec2; type Output = Vec2;
fn div(self, other: u32) -> Vec2 { fn div(self, other: usize) -> Vec2 {
Vec2 { Vec2 {
x: self.x / other, x: self.x / other,
y: self.y / other, y: self.y / other,
@ -112,10 +112,10 @@ impl Div<u32> for Vec2 {
} }
} }
impl Mul<u32> for Vec2 { impl Mul<usize> for Vec2 {
type Output = Vec2; type Output = Vec2;
fn mul(self, other: u32) -> Vec2 { fn mul(self, other: usize) -> Vec2 {
Vec2 { Vec2 {
x: self.x * other, x: self.x * other,
y: self.y * other, y: self.y * other,

View File

@ -33,15 +33,15 @@ impl View for Button {
let x = printer.size.x - 1; let x = printer.size.x - 1;
printer.with_style(style, |printer| { printer.with_style(style, |printer| {
printer.print((1u32,0u32), &self.label); printer.print((1,0), &self.label);
printer.print((0u32,0u32), "<"); printer.print((0,0), "<");
printer.print((x,0), ">"); printer.print((x,0), ">");
}); });
} }
fn get_min_size(&self, _: SizeRequest) -> Vec2 { fn get_min_size(&self, _: SizeRequest) -> Vec2 {
// Meh. Fixed size we are. // Meh. Fixed size we are.
Vec2::new(2 + self.label.len() as u32, 1) Vec2::new(2 + self.label.len(), 1)
} }
fn on_key_event(&mut self, ch: i32) -> EventResult { fn on_key_event(&mut self, ch: i32) -> EventResult {

View File

@ -102,9 +102,9 @@ impl View for Dialog {
printer.print_box(Vec2::new(0,0), printer.size); printer.print_box(Vec2::new(0,0), printer.size);
if self.title.len() > 0 { if self.title.len() > 0 {
let x = (printer.size.x - self.title.len() as u32) / 2; let x = (printer.size.x - self.title.len()) / 2;
printer.print((x-2,0), ""); printer.print((x-2,0), "");
printer.print((x+self.title.len() as u32,0), ""); printer.print((x+self.title.len(),0), "");
printer.with_style(color::TITLE_PRIMARY, |p| p.print((x,0), &self.title)); printer.with_style(color::TITLE_PRIMARY, |p| p.print((x,0), &self.title));
} }
@ -131,7 +131,7 @@ impl View for Dialog {
if self.title.len() > 0 { if self.title.len() > 0 {
// If we have a title, we have to fit it too! // If we have a title, we have to fit it too!
inner_size.x = max(inner_size.x, self.title.len() as u32 + 6); inner_size.x = max(inner_size.x, self.title.len() + 6);
} }
inner_size inner_size

View File

@ -65,16 +65,16 @@ pub enum Selector<'a> {
#[derive(PartialEq,Clone,Copy)] #[derive(PartialEq,Clone,Copy)]
pub enum DimensionRequest { pub enum DimensionRequest {
/// The view must use exactly the attached size. /// The view must use exactly the attached size.
Fixed(u32), Fixed(usize),
/// The view is free to choose its size if it stays under the limit. /// The view is free to choose its size if it stays under the limit.
AtMost(u32), AtMost(usize),
/// No clear restriction apply. /// No clear restriction apply.
Unknown, Unknown,
} }
impl DimensionRequest { impl DimensionRequest {
/// Returns a new request, reduced from the original by the given offset. /// Returns a new request, reduced from the original by the given offset.
pub fn reduced(self, offset: u32) -> Self { pub fn reduced(self, offset: usize) -> Self {
match self { match self {
DimensionRequest::Fixed(w) => DimensionRequest::Fixed(w - offset), DimensionRequest::Fixed(w) => DimensionRequest::Fixed(w - offset),
DimensionRequest::AtMost(w) => DimensionRequest::AtMost(w - offset), DimensionRequest::AtMost(w) => DimensionRequest::AtMost(w - offset),

View File

@ -89,7 +89,7 @@ impl TextView {
for line in self.content.split("\n") { for line in self.content.split("\n") {
height += 1; height += 1;
max_width = cmp::max(max_width, line.len() as u32); max_width = cmp::max(max_width, line.len());
} }
Vec2::new(max_width, height) Vec2::new(max_width, height)
@ -182,11 +182,11 @@ impl View for TextView {
// TODO: what if the text has newlines?? // TODO: what if the text has newlines??
(DimensionRequest::Unknown, DimensionRequest::Unknown) => self.get_ideal_size(), (DimensionRequest::Unknown, DimensionRequest::Unknown) => self.get_ideal_size(),
(DimensionRequest::Fixed(w),_) => { (DimensionRequest::Fixed(w),_) => {
let h = self.get_num_lines(w as usize) as u32; let h = self.get_num_lines(w);
Vec2::new(w, h) Vec2::new(w, h)
}, },
(_,DimensionRequest::Fixed(h)) => { (_,DimensionRequest::Fixed(h)) => {
let w = self.get_num_cols(h as usize) as u32; let w = self.get_num_cols(h);
Vec2::new(w, h) Vec2::new(w, h)
}, },
(DimensionRequest::AtMost(w),_) => { (DimensionRequest::AtMost(w),_) => {
@ -196,7 +196,7 @@ impl View for TextView {
if w >= ideal.x { if w >= ideal.x {
ideal ideal
} else { } else {
let h = self.get_num_lines(w as usize) as u32; let h = self.get_num_lines(w);
Vec2::new(w, h) Vec2::new(w, h)
} }
}, },