2016-07-02 03:23:58 +00:00
|
|
|
use view::{IdView, View, ViewWrapper};
|
|
|
|
use printer::Printer;
|
|
|
|
use vec::Vec2;
|
|
|
|
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Wrapper around a view that remembers its position.
|
2016-07-02 03:23:58 +00:00
|
|
|
pub struct TrackedView<T: View> {
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Wrapped view.
|
2016-07-02 03:23:58 +00:00
|
|
|
pub view: T,
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Last position the view was located.
|
2016-07-02 03:23:58 +00:00
|
|
|
pub offset: Vec2,
|
|
|
|
}
|
|
|
|
|
2016-07-10 02:05:51 +00:00
|
|
|
impl<T: View> TrackedView<T> {
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Creates a new `TrackedView` around `view`.
|
2016-07-02 03:23:58 +00:00
|
|
|
pub fn new(view: T) -> Self {
|
|
|
|
TrackedView {
|
|
|
|
view: view,
|
|
|
|
offset: Vec2::zero(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-12 03:26:33 +00:00
|
|
|
/// Wraps itself in a `IdView` for easy retrieval.
|
2016-07-02 03:23:58 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|