1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#![deny(missing_docs)]
use std::any::Any;
pub trait Editor {
fn cursor_2d(&self) -> Option<[f64; 2]>;
fn cursor_3d(&self) -> Option<[f64; 3]>;
fn hit_2d(&self, pos: [f64; 2]) -> Vec<(Type, Object)>;
fn hit_3d(&self, pos: [f64; 3]) -> Vec<(Type, Object)>;
fn select(&mut self, ty: Type, obj: Object) -> Result<(), ()>;
fn select_multiple(&mut self, ty: Type, objs: &[Object]) -> Result<(), ()>;
fn deselect_multiple(&mut self, ty: Type, objs: &[Object]) -> Result<(), ()>;
fn select_none(&mut self, ty: Type) -> Result<(), ()>;
fn insert(&mut self, ty: Type, args: &Any) -> Result<Object, ()>;
fn delete(&mut self, ty: Type, obj: Object) -> Result<(), ()>;
fn update(&mut self, ty: Type, obj: Object, args: &Any) -> Result<(), ()>;
fn replace(&mut self, ty: Type, from: Object, to: Object) -> Result<(), ()>;
fn get<'a>(&'a self, ty: Type, obj: Object) -> Result<&'a Any, ()>;
fn visible(&self, ty: Type) -> Vec<Object>;
fn selected(&self, ty: Type) -> Option<Object>;
fn multiple_selected(&self, ty: Type) -> Vec<Object>;
fn all(&self, ty: Type) -> Vec<Object>;
fn navigate_to(&mut self, ty: Type, obj: Object) -> Result<(), ()>;
}
#[derive(Clone, Copy, Debug)]
pub struct Type(pub &'static str);
#[derive(Clone, Copy, Debug)]
pub struct Object(pub usize);
pub fn delete<T>(items: &mut Vec<T>, obj: Object) -> Result<Option<Object>, ()> {
if items.len() == 0 { return Err(()); }
let upd_obj = if obj.0 == items.len() - 1 {
None
} else {
Some(Object(items.len() - 1))
};
items.swap_remove(obj.0);
Ok(upd_obj)
}
pub fn update<T: Any + Clone>(items: &mut Vec<T>, obj: Object, args: &Any)
-> Result<(), ()> {
match args.downcast_ref::<T>() {
None => { return Err(()); }
Some(val) => {
items[obj.0] = val.clone();
Ok(())
}
}
}
pub fn all<T>(items: &Vec<T>) -> Vec<Object> {
(0..items.len()).map(|i| Object(i)).collect()
}
pub fn get<T: Any>(items: &Vec<T>, obj: Object) -> Result<&Any, ()> {
Ok(try!(items.get(obj.0).ok_or(())))
}