Remove some unsafe code from the ui system

This commit is contained in:
Thinkofname 2016-04-02 20:08:21 +01:00
parent 6567ae6045
commit b975a01f30
7 changed files with 96 additions and 73 deletions

View File

@ -57,7 +57,7 @@ impl Batch {
let sy = r.h / self.height;
for e in &mut self.elements {
let reg = Container::get_draw_region_raw(e, sx, sy, r);
let reg = e.get_draw_region(sx, sy, r);
e.set_dirty(true);
self.data.extend(e.draw(renderer, &reg, width, height, delta));
}
@ -65,10 +65,6 @@ impl Batch {
&self.data
}
pub fn get_size(&self) -> (f64, f64) {
(self.width, self.height)
}
pub fn add<T: UIElement>(&mut self, e: T) -> BatchRef<T> {
self.elements.push(e.wrap());
BatchRef {
@ -117,4 +113,16 @@ impl UIElement for Batch {
_ => panic!("Incorrect type"),
}
}
fn get_attachment(&self) -> (VAttach, HAttach) {
(self.v_attach, self.h_attach)
}
fn get_offset(&self) -> (f64, f64) {
(self.x, self.y)
}
fn get_size(&self) -> (f64, f64) {
(self.width, self.height)
}
}

View File

@ -105,10 +105,6 @@ impl Button {
&self.data
}
pub fn get_size(&self) -> (f64, f64) {
(self.width, self.height)
}
lazy_field!(width, f64, get_width, set_width);
lazy_field!(height, f64, get_height, set_height);
lazy_field!(disabled, bool, is_disabled, set_disabled);
@ -133,4 +129,16 @@ impl UIElement for Button {
_ => panic!("Incorrect type"),
}
}
fn get_attachment(&self) -> (VAttach, HAttach) {
(self.v_attach, self.h_attach)
}
fn get_offset(&self) -> (f64, f64) {
(self.x, self.y)
}
fn get_size(&self) -> (f64, f64) {
(self.width, self.height)
}
}

View File

@ -113,7 +113,7 @@ impl Formatted {
let sy = r.h / self.height;
for e in &mut self.text {
let reg = Container::get_draw_region_raw(e, sx, sy, r);
let reg = e.get_draw_region(sx, sy, r);
e.set_dirty(true);
self.data.extend(e.draw(renderer, &reg, width, height, delta));
}
@ -121,11 +121,6 @@ impl Formatted {
&self.data
}
pub fn get_size(&self) -> (f64, f64) {
((self.width + 2.0) * self.scale_x,
self.height * self.scale_y)
}
lazy_field!(width, f64, get_width, set_width);
lazy_field!(height, f64, get_height, set_height);
lazy_field!(scale_x, f64, get_scale_x, set_scale_x);
@ -151,6 +146,18 @@ impl UIElement for Formatted {
_ => panic!("Incorrect type"),
}
}
fn get_attachment(&self) -> (VAttach, HAttach) {
(self.v_attach, self.h_attach)
}
fn get_offset(&self) -> (f64, f64) {
(self.x, self.y)
}
fn get_size(&self) -> (f64, f64) {
((self.width + 2.0) * self.scale_x, self.height * self.scale_y)
}
}
struct FormatState<'a> {

View File

@ -95,10 +95,6 @@ impl Image {
&self.data
}
pub fn get_size(&self) -> (f64, f64) {
(self.width, self.height)
}
pub fn get_texture(&self) -> render::Texture {
self.texture.clone()
}
@ -140,4 +136,16 @@ impl UIElement for Image {
_ => panic!("Incorrect type"),
}
}
fn get_attachment(&self) -> (VAttach, HAttach) {
(self.v_attach, self.h_attach)
}
fn get_offset(&self) -> (f64, f64) {
(self.x, self.y)
}
fn get_size(&self) -> (f64, f64) {
(self.width, self.height)
}
}

View File

@ -136,33 +136,6 @@ impl Element {
}
}
fn get_attachment(&self) -> (VAttach, HAttach) {
match *self {
$(
Element::$name(ref val) => (val.v_attach, val.h_attach),
)+
_ => unimplemented!(),
}
}
fn get_offset(&self) -> (f64, f64) {
match *self {
$(
Element::$name(ref val) => (val.x, val.y),
)+
_ => unimplemented!(),
}
}
fn get_size(&self) -> (f64, f64) {
match *self {
$(
Element::$name(ref val) => val.get_size(),
)+
_ => unimplemented!(),
}
}
fn is_dirty(&self) -> bool {
match *self {
$(
@ -198,6 +171,15 @@ impl Element {
_ => unimplemented!(),
}
}
fn get_draw_region(&self, sw: f64, sh: f64, super_region: &Region) -> Region {
match *self {
$(
Element::$name(ref val) => Container::get_draw_region_raw(val, sw, sh, super_region),
)+
_ => unimplemented!(),
}
}
}
)
}
@ -571,10 +553,10 @@ impl Container {
Some(ref p) => self.get_draw_region(self.elements.get(p).unwrap(), sw, sh),
None => SCREEN,
};
Container::get_draw_region_raw(e, sw, sh, &super_region)
e.get_draw_region(sw, sh, &super_region)
}
fn get_draw_region_raw(e: &Element, sw: f64, sh: f64, super_region: &Region) -> Region {
fn get_draw_region_raw<T: UIElement>(e: &T, sw: f64, sh: f64, super_region: &Region) -> Region {
let mut r = Region {
x: 0.0,
y: 0.0,
@ -614,6 +596,10 @@ pub trait UIElement {
fn key_type(&mut self, _game: &mut ::Game, _c: char) -> Vec<Rc<ClickFunc>> {
vec![]
}
fn get_attachment(&self) -> (VAttach, HAttach);
fn get_offset(&self) -> (f64, f64);
fn get_size(&self) -> (f64, f64);
}
macro_rules! lazy_field {

View File

@ -102,11 +102,6 @@ impl Text {
&self.data
}
pub fn get_size(&self) -> (f64, f64) {
((self.width + 2.0) * self.scale_x,
self.height * self.scale_y)
}
pub fn get_text(&self) -> &str {
&self.val
}
@ -146,4 +141,16 @@ impl UIElement for Text {
_ => panic!("Incorrect type"),
}
}
fn get_attachment(&self) -> (VAttach, HAttach) {
(self.v_attach, self.h_attach)
}
fn get_offset(&self) -> (f64, f64) {
(self.x, self.y)
}
fn get_size(&self) -> (f64, f64) {
((self.width + 2.0) * self.scale_x, self.height * self.scale_y)
}
}

View File

@ -95,30 +95,17 @@ impl TextBox {
let sx = r.w / self.width;
let sy = r.h / self.height;
let mut btn = mem::replace(&mut self.button, unsafe { mem::uninitialized() }).wrap();
let reg = Container::get_draw_region_raw(&btn, sx, sy, r);
btn.set_dirty(true);
self.data.extend(btn.draw(renderer, &reg, width, height, delta));
mem::forget(mem::replace(&mut self.button, match btn {
Element::Button(btn)=> btn,
_ => unreachable!(),
}));
let mut txt = mem::replace(&mut self.text, unsafe { mem::uninitialized() }).wrap();
let reg = Container::get_draw_region_raw(&txt, sx, sy, r);
txt.set_dirty(true);
self.data.extend(txt.draw(renderer, &reg, width, height, delta));
mem::forget(mem::replace(&mut self.text, match txt {
Element::Text(txt)=> txt,
_ => unreachable!(),
}));
let reg = Container::get_draw_region_raw(&self.button, sx, sy, r);
self.button.dirty = true;
self.data.extend(self.button.draw(renderer, &reg, width, height, delta));
let reg = Container::get_draw_region_raw(&self.text, sx, sy, r);
self.text.dirty = true;
self.data.extend(self.text.draw(renderer, &reg, width, height, delta));
}
&self.data
}
pub fn get_size(&self) -> (f64, f64) {
(self.width, self.height)
}
pub fn get_input(&self) -> String {
self.input.clone()
}
@ -180,4 +167,16 @@ impl UIElement for TextBox {
_ => panic!("Incorrect type"),
}
}
fn get_attachment(&self) -> (VAttach, HAttach) {
(self.v_attach, self.h_attach)
}
fn get_offset(&self) -> (f64, f64) {
(self.x, self.y)
}
fn get_size(&self) -> (f64, f64) {
(self.width, self.height)
}
}