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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
use rin::graphics::*;
use rin::events::*;
use rin::math::*;
use rin::window;
use rin::util::idtree;
use std::rc::Rc;
use traits::*;
use rin::color::*;
use rin::color::consts::*;
use control_shape::ControlShape;
use std::cell::Cell;


pub struct Tree{
    name: String,
    font: Rc<Ttf>,
    color: Rgb<f32>,
    non_attended: StreamRc<'static, window::Event>,
    text_geometry: Vec<Vertex2DTex>,
    width: Property<'static, f32>,
    height: Property<'static, f32>,
    position: Property<'static, Pnt2>,
    h_margin: f32,
    v_margin: f32,
    rendered: Cell<bool>,
    tree: idtree::Arena<String>,
    root: idtree::NodeId,
    needs_refresh: bool,
}


pub struct TreeBuilder{
    name: String,
    root: String,
    events: StreamRc<'static, window::Event>,
    shape: ControlShape,
}

impl TreeBuilder{
    pub fn new<S: StreamT<'static, window::Event>>(name: &str, root: String, events: S) -> TreeBuilder{
        let pos = Property::new(origin());
        let size = Property::new(15.);
        let margin = 0.;
        TreeBuilder{
            name: name.to_owned(),
            root: root,
            events: events.rc(),
            shape: ControlShape::new(pos, size.clone(), size, margin, margin, WHITE),
        }
    }

    pub fn create(self) -> Tree{
        // toggle box and font
        let font = self.shape.font().unwrap_or_else(|| ::utils::default_font());
        // when mouse moved over
        let width = self.shape.width();
        let height = Property::new(font.line_height());
        let position = self.shape.position();
        let h_margin = self.shape.h_margin();
        let v_margin = self.shape.v_margin();

        let mut tree = idtree::Arena::new();
        let root = tree.new_node(self.root).id();


        Tree{
            name: self.name,
            font,
            color: self.shape.color(),
            non_attended: self.events,
            width,
            height,
            position,
            rendered: Cell::new(false),
            tree,
            root,
            needs_refresh: true,
            text_geometry: vec![],
            h_margin,
            v_margin,
        }
    }
}

impl Tree{
    pub fn add(&mut self, child: String) -> idtree::NodeId{
        self.needs_refresh = true;
        self.root.append_new(child, &mut self.tree).id()
    }

    pub fn add_child(&mut self, parent: idtree::NodeId, child: String) -> idtree::NodeId{
        self.needs_refresh = true;
        parent.append_new(child, &mut self.tree).id()
    }

    pub fn update_node(&mut self, node: idtree::NodeId, value: String){
        self.needs_refresh = true;
        self.tree.get_mut(node).data = value
    }

    pub fn update(&mut self){
        if self.rendered.get() && self.needs_refresh {
            let mut text = "+ ".to_owned() + &self.tree.get(self.root).data;
            let mut ident = "".to_owned();
            let mut linebreaks = 0;
            for node in self.root.traverse(&self.tree).skip(1){
                match node {
                    idtree::NodeEdge::Start(start) => {
                        if ident.is_empty(){
                            ident = "  ".to_owned();
                        }else{
                            ident += "|  ";
                        }
                        text += "\n";
                        text += &ident;
                        text += "|\n";
                        text += &ident;
                        text += "|-";
                        text += &self.tree.get(start).data;
                        linebreaks += 2;
                    }
                    idtree::NodeEdge::End(end) => {
                        if ident.len() == 2 {
                            ident = "".to_owned();
                        }else if !ident.is_empty() {
                            ident = ident[..ident.len() - 3].to_owned();
                        }
                    }
                }
            }
            self.text_geometry.clear();
            let line_height = self.font.line_height();
            self.text_geometry = self.font
                .mesh(&text, self.position.x, self.position.y + line_height + self.v_margin, CoordinateOrigin::TopLeft)
                .into();

            let height = line_height * (linebreaks + 1) as f32 + self.v_margin * 2.;
            if *self.height != height {
                self.height.set(height);
            }
            self.needs_refresh = false;
            self.rendered.set(false);
        }
    }
}

impl ControlBuilder for TreeBuilder{
    type Control = Tree;
    fn shape(&mut self) -> &mut ControlShape{
        &mut self.shape
    }

    fn create(self) -> Tree{
        self.create()
    }
}

impl ControlBuilt<String> for Tree{
    type Builder = TreeBuilder;
}

impl BuilderFromProperty<String> for TreeBuilder{
    fn from_parameter<S: StreamT<'static, window::Event>>(parameter: String, name: &str, events: S) -> TreeBuilder{
        TreeBuilder::new(name, parameter, events)
    }
}

impl ControlGeometry for Tree{
    fn flexible() -> bool{
        true
    }

    fn geometry(&self, _container: Option<&Rect<f32>>) -> Option<Vec<Vertex2DColor>>{
        None
    }

    fn text_geometry(&self, _container: Option<&Rect<f32>>) -> Option<Vec<Vertex2DTex>>{
        self.rendered.set(true);
        Some(self.text_geometry.clone())
    }

    fn texture_geometry(&self, _container: Option<&Rect<f32>>) -> Option<Vec<Vertex2DTex>>{
        None
    }

    fn font(&self) -> Option<&Ttf>{
        Some(&self.font)
    }

    fn color(&self) -> &Rgb<f32>{
        &self.color
    }

    fn position(&self) -> &Property<'static, Pnt2>{
        &self.position
    }

    fn width(&self) -> &Property<'static, f32>{
        &self.width
    }

    fn height(&self) -> &Property<'static, f32>{
        &self.height
    }
}


impl ControlEvents for Tree{
    fn non_attended(&self) -> StreamRc<'static, window::Event>{
        self.non_attended.clone()
    }
}

impl Control for Tree{
    fn name(&self) -> &str{
        &self.name
    }
}



#[cfg(any(feature = "gl", feature = "gles", feature="webgl"))]
mod gl{
    use rin::gl::{self, Renderer2d};
    use rin::graphics::*;
    use traits::ControlGeometry;
    use utils;
    use rin::gl::RenderSurface;
    use rin::math::{convert, Rect};

    impl gl::Render3d for super::Tree{
        fn render<R: RenderSurface>(&self, gl: &gl::Renderer<R>){
            let viewport = *gl.context().state().viewport();
            let viewport: Rect<i32> = viewport.into();
            let viewport: Rect<f32> = convert(viewport);
            let viewport = Some(&viewport);
            if let Some(geometry) = self.geometry(viewport).map(|g| mesh(g, PrimitiveType::Triangles)){
                let gl = gl.with_properties(&[
                    gl::Property::Blend(true),
                    gl::Property::BlendEquation(gl::FUNC_ADD),
                    gl::Property::BlendFunc(gl::SRC_ALPHA, gl::ONE_MINUS_SRC_ALPHA),
                ]);
                gl.draw_mesh(&geometry);
            }
            if let (Some(text_geometry), Some(font)) = (self.text_geometry(viewport), self.font()){
                let font_material = utils::gl::font_material(&gl, font, self.color());
                let text_geometry = mesh(text_geometry, PrimitiveType::Triangles);
                gl.draw_mesh_with_material(&text_geometry, font_material);
            }
        }
    }
}