Rename Margins -> Vec4

And move it to the vec module
This commit is contained in:
Alexandre Bury 2015-05-25 14:56:51 -07:00
parent dca9d7f662
commit 25e1ca6074
5 changed files with 84 additions and 59 deletions

View File

@ -22,6 +22,7 @@ fn main() {
siv.add_layer(TextView::new(&content));
// Show a popup on top of the view.
siv.add_layer(Dialog::new(TextView::new("Try resizing the terminal!\n(Press 'q' to quit when you're done.)"))
.padding((0,0,0,0))
.dismiss_button("Ok"));
siv.run();

View File

@ -30,9 +30,6 @@ pub mod vec;
pub mod color;
mod div;
mod margins;
pub use margins::Margins;
use std::any::Any;
use std::rc::Rc;

View File

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

View File

@ -122,3 +122,74 @@ impl Mul<usize> for Vec2 {
}
}
}
/// Four values representing each direction.
pub struct Vec4 {
/// Left margin
pub left: usize,
/// Right margin
pub right: usize,
/// Top margin
pub top: usize,
/// Bottom margin
pub bottom: usize,
}
impl Vec4 {
/// Creates a new Vec4.
pub fn new(left: usize, right: usize, top: usize, bottom: usize) -> Self {
Vec4 {
left: left,
right: right,
top: top,
bottom: bottom,
}
}
/// Returns left + right.
pub fn horizontal(&self) -> usize {
self.left + self.right
}
/// Returns top + bottom.
pub fn vertical(&self) -> usize {
self.top + self.bottom
}
/// Returns (left+right, top+bottom).
pub fn combined(&self) -> Vec2 {
Vec2::new(self.horizontal(), self.vertical())
}
/// Returns (left, top).
pub fn top_left(&self) -> Vec2 {
Vec2::new(self.left, self.top)
}
/// Returns (right, bottom).
pub fn bot_right(&self) -> Vec2 {
Vec2::new(self.right, self.bottom)
}
}
/// Generic trait for converting a value into a Vec4.
pub trait ToVec4 {
/// Converts self to a Vec4.
fn to_vec4(self) -> Vec4;
}
impl ToVec4 for Vec4 {
fn to_vec4(self) -> Vec4 { self }
}
impl ToVec4 for (usize,usize,usize,usize) {
fn to_vec4(self) -> Vec4 {
Vec4::new(self.0, self.1, self.2, self.3)
}
}
impl ToVec4 for (i32,i32,i32,i32) {
fn to_vec4(self) -> Vec4 {
Vec4::new(self.0 as usize, self.1 as usize, self.2 as usize, self.3 as usize)
}
}

View File

@ -3,11 +3,11 @@ use std::cmp::max;
use ncurses;
use color;
use ::{Cursive,Margins};
use ::{Cursive};
use event::EventResult;
use view::{View,SizeRequest,DimensionRequest};
use view::{Button,SizedView};
use vec::Vec2;
use vec::{Vec2,Vec4,ToVec4};
use printer::Printer;
#[derive(PartialEq)]
@ -29,8 +29,8 @@ pub struct Dialog {
buttons: Vec<SizedView<Button>>,
padding: Margins,
borders: Margins,
padding: Vec4,
borders: Vec4,
focus: Focus,
}
@ -43,8 +43,8 @@ impl Dialog {
buttons: Vec::new(),
title: String::new(),
focus: Focus::Content,
padding: Margins::new(1,1,0,0),
borders: Margins::new(1,1,1,1),
padding: Vec4::new(1,1,0,0),
borders: Vec4::new(1,1,1,1),
}
}
@ -71,6 +71,12 @@ impl Dialog {
self
}
pub fn padding<T: ToVec4>(mut self, padding: T) -> Self {
self.padding = padding.to_vec4();
self
}
}
impl View for Dialog {