Add TrackedView to cache the view size

This commit is contained in:
Alexandre Bury 2016-07-01 20:23:58 -07:00
parent 87cd1ce23f
commit c958093b74
4 changed files with 37 additions and 7 deletions

View File

@ -1,6 +1,7 @@
//! Defines various views to use when creating the layout. //! Defines various views to use when creating the layout.
#[macro_use]mod view_wrapper; #[macro_use]
mod view_wrapper;
// Essentials components // Essentials components
mod position; mod position;
@ -24,6 +25,7 @@ mod select_view;
mod sized_view; mod sized_view;
mod stack_view; mod stack_view;
mod text_view; mod text_view;
mod tracked_view;
use std::any::Any; use std::any::Any;
@ -50,6 +52,7 @@ pub use self::select_view::SelectView;
pub use self::shadow_view::ShadowView; pub use self::shadow_view::ShadowView;
pub use self::stack_view::StackView; pub use self::stack_view::StackView;
pub use self::text_view::TextView; pub use self::text_view::TextView;
pub use self::tracked_view::TrackedView;
pub use self::sized_view::SizedView; pub use self::sized_view::SizedView;
pub use self::view_wrapper::ViewWrapper; pub use self::view_wrapper::ViewWrapper;

View File

@ -1,4 +1,4 @@
use ::vec::{ToVec2, Vec2}; use vec::{ToVec2, Vec2};
/// Location of the view on screen /// Location of the view on screen
pub struct Position { pub struct Position {
@ -8,10 +8,7 @@ pub struct Position {
impl Position { impl Position {
pub fn new(x: Offset, y: Offset) -> Self { pub fn new(x: Offset, y: Offset) -> Self {
Position { Position { x: x, y: y }
x: x,
y: y,
}
} }
pub fn center() -> Self { pub fn center() -> Self {

View File

@ -24,7 +24,7 @@ impl<T: View> ViewWrapper for SizedView<T> {
wrap_impl!(&self.view); wrap_impl!(&self.view);
fn wrap_layout(&mut self, size: Vec2) { fn wrap_layout(&mut self, size: Vec2) {
self.view.layout(size);
self.size = size; self.size = size;
self.view.layout(size);
} }
} }

30
src/view/tracked_view.rs Normal file
View File

@ -0,0 +1,30 @@
use view::{IdView, View, ViewWrapper};
use printer::Printer;
use vec::Vec2;
pub struct TrackedView<T: View> {
pub view: T,
pub offset: Vec2,
}
impl <T: View> TrackedView<T> {
pub fn new(view: T) -> Self {
TrackedView {
view: view,
offset: Vec2::zero(),
}
}
pub fn with_id(self, id: &str) -> IdView<Self> {
IdView::new(id, self)
}
}
impl<T: View> ViewWrapper for TrackedView<T> {
wrap_impl!(&self.view);
fn wrap_draw(&mut self, printer: &Printer) {
self.offset = printer.offset;
self.view.draw(printer);
}
}