// Copyright 2015 Matthew Collins // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #[derive(Clone, Copy)] pub struct BatchRef { index: usize, ty: PhantomData, } ui_element!(Batch { width: f64, height: f64, elements: Vec }); impl Batch { base_impl!(); pub fn new(x: f64, y: f64, w: f64, h: f64) -> Batch { ui_create!(Batch { x: x, y: y, width: w, height: h, elements: Vec::new(), }) } fn update(&mut self, _: &mut render::Renderer) { } fn draw(&mut self, renderer: &mut render::Renderer, r: &Region, width: f64, height: f64, delta: f64) -> &Vec { if self.dirty { self.dirty = false; self.data.clear(); let sx = r.w / self.width; let sy = r.h / self.height; for e in &mut self.elements { let reg = Container::get_draw_region_raw(e, sx, sy, r); e.set_dirty(true); self.data.extend(e.draw(renderer, ®, width, height, delta)); } } &self.data } pub fn get_size(&self) -> (f64, f64) { (self.width, self.height) } pub fn add(&mut self, e: T) -> BatchRef { self.elements.push(e.wrap()); BatchRef { index: self.elements.len() - 1, ty: PhantomData, } } pub fn get(&self, r: BatchRef) -> &T { T::unwrap_ref(&self.elements[r.index]) } pub fn get_mut(&mut self, r: BatchRef) -> &mut T { self.dirty = true; T::unwrap_ref_mut(&mut self.elements[r.index]) } pub fn get_mut_at(&mut self, index: usize) -> &mut T { self.dirty = true; T::unwrap_ref_mut(&mut self.elements[index]) } pub fn len(&self) -> usize { self.elements.len() } lazy_field!(width, f64, get_width, set_width); lazy_field!(height, f64, get_height, set_height); } impl UIElement for Batch { fn wrap(self) -> Element { Element::Batch(self) } fn unwrap_ref<'a>(e: &'a Element) -> &'a Batch { match e { &Element::Batch(ref val) => val, _ => panic!("Incorrect type"), } } fn unwrap_ref_mut<'a>(e: &'a mut Element) -> &'a mut Batch { match e { &mut Element::Batch(ref mut val) => val, _ => panic!("Incorrect type"), } } }