2016-07-16 06:44:38 +00:00
|
|
|
use std::cell::Cell;
|
|
|
|
|
2016-07-28 23:36:01 +00:00
|
|
|
use view::{View, ViewWrapper};
|
|
|
|
use views::IdView;
|
2016-07-14 06:25:54 +00:00
|
|
|
use Printer;
|
2016-07-02 03:23:58 +00:00
|
|
|
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-16 06:44:38 +00:00
|
|
|
offset: Cell<Vec2>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: View> TrackedView<T> {
|
|
|
|
/// Return the last offset at which the view was drawn.
|
|
|
|
pub fn offset(&self) -> Vec2 {
|
|
|
|
self.offset.get()
|
|
|
|
}
|
2016-07-02 03:23:58 +00:00
|
|
|
}
|
|
|
|
|
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,
|
2016-07-16 06:44:38 +00:00
|
|
|
offset: Cell::new(Vec2::zero()),
|
2016-07-02 03:23:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
2016-07-16 06:44:38 +00:00
|
|
|
fn wrap_draw(&self, printer: &Printer) {
|
|
|
|
self.offset.set(printer.offset);
|
2016-07-02 03:23:58 +00:00
|
|
|
self.view.draw(printer);
|
|
|
|
}
|
|
|
|
}
|