mirror of
https://github.com/FliegendeWurst/cursive.git
synced 2024-11-23 17:35:00 +00:00
Use usize for Vec2 instead of u32
Much less casts needed now.
This commit is contained in:
parent
cb03f79e24
commit
dca9d7f662
@ -176,8 +176,8 @@ impl Cursive {
|
||||
ncurses::getmaxyx(ncurses::stdscr, &mut y, &mut x);
|
||||
|
||||
Vec2 {
|
||||
x: x as u32,
|
||||
y: y as u32,
|
||||
x: x as usize,
|
||||
y: y as usize,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -3,18 +3,18 @@ use vec::Vec2;
|
||||
/// Fixed margins around a rectangular view.
|
||||
pub struct Margins {
|
||||
/// Left margin
|
||||
pub left: u32,
|
||||
pub left: usize,
|
||||
/// Right margin
|
||||
pub right: u32,
|
||||
pub right: usize,
|
||||
/// Top margin
|
||||
pub top: u32,
|
||||
pub top: usize,
|
||||
/// Bottom margin
|
||||
pub bottom: u32,
|
||||
pub bottom: usize,
|
||||
}
|
||||
|
||||
impl 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 {
|
||||
left: left,
|
||||
right: right,
|
||||
@ -24,12 +24,12 @@ impl Margins {
|
||||
}
|
||||
|
||||
/// Returns left + right.
|
||||
pub fn horizontal(&self) -> u32 {
|
||||
pub fn horizontal(&self) -> usize {
|
||||
self.left + self.right
|
||||
}
|
||||
|
||||
/// Returns top + bottom.
|
||||
pub fn vertical(&self) -> u32 {
|
||||
pub fn vertical(&self) -> usize {
|
||||
self.top + self.bottom
|
||||
}
|
||||
|
||||
|
@ -29,7 +29,7 @@ impl Printer {
|
||||
let p = pos.to_vec2();
|
||||
if p.y >= self.size.y || p.x >= self.size.x { return; }
|
||||
// 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.
|
||||
let text = match text.char_indices().nth(room) {
|
||||
Some((i,_)) => &text[..i],
|
||||
@ -40,7 +40,7 @@ impl Printer {
|
||||
}
|
||||
|
||||
/// 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();
|
||||
if p.y > self.size.y || p.x > self.size.x { return; }
|
||||
let len = min(len, self.size.y - p.y);
|
||||
@ -50,7 +50,7 @@ impl Printer {
|
||||
}
|
||||
|
||||
/// 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();
|
||||
if p.y > self.size.y || p.x > self.size.x { return; }
|
||||
let len = min(len, self.size.x - p.x);
|
||||
|
30
src/vec.rs
30
src/vec.rs
@ -7,14 +7,14 @@ use std::cmp::{min,max};
|
||||
#[derive(Clone,Copy)]
|
||||
pub struct Vec2 {
|
||||
/// X coordinate (column), from left to right.
|
||||
pub x: u32,
|
||||
pub x: usize,
|
||||
/// Y coordinate (row), from top to bottom.
|
||||
pub y: u32,
|
||||
pub y: usize,
|
||||
}
|
||||
|
||||
impl Vec2 {
|
||||
/// Creates a new Vec2 from coordinates.
|
||||
pub fn new(x: u32, y: u32) -> Self {
|
||||
pub fn new(x: usize, y: usize) -> Self {
|
||||
Vec2 {
|
||||
x: x,
|
||||
y: y,
|
||||
@ -61,19 +61,19 @@ 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)
|
||||
(self.0 as usize, self.1 as usize).to_vec2()
|
||||
}
|
||||
}
|
||||
|
||||
impl ToVec2 for (usize,usize) {
|
||||
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;
|
||||
|
||||
fn div(self, other: u32) -> Vec2 {
|
||||
fn div(self, other: usize) -> Vec2 {
|
||||
Vec2 {
|
||||
x: self.x / 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;
|
||||
|
||||
fn mul(self, other: u32) -> Vec2 {
|
||||
fn mul(self, other: usize) -> Vec2 {
|
||||
Vec2 {
|
||||
x: self.x * other,
|
||||
y: self.y * other,
|
||||
|
@ -33,15 +33,15 @@ impl View for Button {
|
||||
let x = printer.size.x - 1;
|
||||
|
||||
printer.with_style(style, |printer| {
|
||||
printer.print((1u32,0u32), &self.label);
|
||||
printer.print((0u32,0u32), "<");
|
||||
printer.print((1,0), &self.label);
|
||||
printer.print((0,0), "<");
|
||||
printer.print((x,0), ">");
|
||||
});
|
||||
}
|
||||
|
||||
fn get_min_size(&self, _: SizeRequest) -> Vec2 {
|
||||
// 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 {
|
||||
|
@ -102,9 +102,9 @@ impl View for Dialog {
|
||||
printer.print_box(Vec2::new(0,0), printer.size);
|
||||
|
||||
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+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));
|
||||
}
|
||||
@ -131,7 +131,7 @@ impl View for Dialog {
|
||||
|
||||
if self.title.len() > 0 {
|
||||
// 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
|
||||
|
@ -65,16 +65,16 @@ pub enum Selector<'a> {
|
||||
#[derive(PartialEq,Clone,Copy)]
|
||||
pub enum DimensionRequest {
|
||||
/// 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.
|
||||
AtMost(u32),
|
||||
AtMost(usize),
|
||||
/// No clear restriction apply.
|
||||
Unknown,
|
||||
}
|
||||
|
||||
impl DimensionRequest {
|
||||
/// 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 {
|
||||
DimensionRequest::Fixed(w) => DimensionRequest::Fixed(w - offset),
|
||||
DimensionRequest::AtMost(w) => DimensionRequest::AtMost(w - offset),
|
||||
|
@ -89,7 +89,7 @@ impl TextView {
|
||||
|
||||
for line in self.content.split("\n") {
|
||||
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)
|
||||
@ -182,11 +182,11 @@ impl View for TextView {
|
||||
// TODO: what if the text has newlines??
|
||||
(DimensionRequest::Unknown, DimensionRequest::Unknown) => self.get_ideal_size(),
|
||||
(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)
|
||||
},
|
||||
(_,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)
|
||||
},
|
||||
(DimensionRequest::AtMost(w),_) => {
|
||||
@ -196,7 +196,7 @@ impl View for TextView {
|
||||
if w >= ideal.x {
|
||||
ideal
|
||||
} else {
|
||||
let h = self.get_num_lines(w as usize) as u32;
|
||||
let h = self.get_num_lines(w);
|
||||
Vec2::new(w, h)
|
||||
}
|
||||
},
|
||||
|
Loading…
Reference in New Issue
Block a user