hunter/src/hbox.rs

111 lines
2.5 KiB
Rust
Raw Normal View History

2019-01-21 15:53:16 +01:00
use termion::event::{Event};
use crate::widget::Widget;
2019-01-21 15:53:16 +01:00
// pub struct Child<T> {
// widget: T,
// position: (u16, u16),
// size: (u16, u16),
// active: bool
// }
2019-01-22 15:11:15 +01:00
pub struct HBox {
dimensions: (u16, u16),
position: (u16, u16),
children: Vec<Box<Widget>>,
2019-01-22 15:11:15 +01:00
active: usize
}
2019-01-22 15:11:15 +01:00
impl HBox {
2019-01-21 17:47:58 +01:00
pub fn new(widgets: Vec<Box<Widget>>,
dimensions: (u16, u16),
position: (u16, u16),
main: usize) -> HBox {
2019-01-22 15:11:15 +01:00
let mut hbox = HBox {
2019-01-21 17:47:58 +01:00
dimensions: dimensions,
position: position,
children: widgets,
2019-01-22 15:11:15 +01:00
active: main
};
hbox.resize_children();
hbox
}
2019-01-22 15:11:15 +01:00
pub fn resize_children(&mut self) {
let hbox_size = dbg!(self.dimensions);
let hbox_position = dbg!(self.position);
let cell_size = dbg!(hbox_size.0 / self.children.len() as u16);
let mut current_pos = dbg!(hbox_position.1);
let mut current_edge = dbg!(cell_size);
for mut widget in &mut self.children {
widget.set_dimensions(dbg!((cell_size, hbox_size.1)));
widget.set_position(dbg!((current_pos, hbox_position.1)));
widget.refresh();
dbg!(current_pos += cell_size);
dbg!(current_edge += cell_size);
}
}
pub fn resize_child(&mut self, index: usize, size: (u16, u16)) {
self.children[index].set_dimensions(size);
}
2019-01-22 15:11:15 +01:00
pub fn widget(&self, index: usize) -> &Box<Widget> {
&self.children[index]
}
pub fn active_widget(&self, index: usize) -> &Box<Widget> {
&self.children[self.active]
}
}
2019-01-22 15:11:15 +01:00
impl Widget for HBox {
fn render(&self) -> Vec<String> {
2019-01-21 15:53:16 +01:00
// HBox doesnt' draw anything itself
2019-01-21 17:47:58 +01:00
vec![]
}
fn render_header(&self) -> String {
2019-01-21 21:46:13 +01:00
self.children[self.active].render_header()
}
fn refresh(&mut self) {
for child in &mut self.children {
child.refresh();
}
}
fn get_drawlist(&mut self) -> String {
self.children.iter_mut().map(|child| {
child.get_drawlist()
}).collect()
}
fn get_dimensions(&self) -> (u16, u16) {
self.dimensions
}
fn get_position(&self) -> (u16, u16) {
self.position
2019-01-21 15:53:16 +01:00
}
2019-01-21 17:47:58 +01:00
fn set_dimensions(&mut self, size: (u16, u16)) {
self.dimensions = size;
}
fn set_position(&mut self, position: (u16, u16)) {
self.position = position;
}
2019-01-21 15:53:16 +01:00
fn on_event(&mut self, event: Event) {
2019-01-21 21:46:13 +01:00
self.children[self.active].on_event(event);
2019-01-21 15:53:16 +01:00
}
}