add DropDownList for color selection
[lp74.git] / src / view.rs
blob43c66f932b196884cbf3fc305f362da30237ecb8
1 //extern crate rand;
2 extern crate graphics;
3 extern crate image;
4 extern crate gfx_device_gl;
5 extern crate conrod;
7 use data::*;
8 use graphics::{Line, Rectangle, Polygon, Ellipse, DrawState, Transformed};
10 const BLACK: [f32; 4] = [0.0, 0.0, 0.0, 1.0];
11 const RED: [f32; 4] = [1.0, 0.0, 0.0, 1.0];
12 //#[inline]
13 //fn rand_color() -> [f32; 4] {
14 //    [
15 //        rand::random(),
16 //        rand::random(),
17 //        rand::random(),
18 //        0.75,
19 //    ]
20 //}
22 pub struct WSize {
23     pub w: f64,
24     pub h: f64,
25     pub header_h: f64,
26     pub side_w: f64,
29 pub fn view_space<G: graphics::Graphics>(s: &Space,
30                  wsize: &WSize,
31                  c: graphics::Context, gl: &mut G) {
32     let (xctr, zctr) = ((wsize.w - wsize.side_w) / 2.0, (wsize.h - wsize.header_h) / 2.0);
33     let (xcur, ycur, zcur) = (s.row_size_before_current(Axis::X) as f64,
34                               (s.row_size_before_current(Axis::Y) / 2) as f64,
35                               s.row_size_before_current(Axis::Z) as f64);
36     let (wshift, hshift) = (xctr - (xcur + ycur) + wsize.side_w,
37                             zctr - (zcur + ycur) + wsize.header_h);
38     let mut wy = (s.row_size(Axis::Y) / 3) as f64;
39     for iy in (0..s.faces[0].len()).rev() {
40         let ysize = (s.size[Space::axis_idx(Axis::Y)][iy] / 3) as f64;
41         let mut wx = wshift;
42         for ix in 0..s.faces.len() {
43             let xsize = s.size[Space::axis_idx(Axis::X)][ix] as f64;
44             let mut wz = hshift;
45             for iz in 0..s.faces[0][0].len() {
46                 let zsize = s.size[Space::axis_idx(Axis::Z)][iz] as f64;
47                 match s.faces[ix][iy][iz] {
48                     Some(ss) => {
49                         let x = wx + wy;
50                         let z = wz + wy;
51                         if let Some((r, g, b)) = ss[Space::ori_idx(Ori::Back)] {
52                             Rectangle::new([r, g, b, 0.6])
53                                 .draw([0.0, 0.0, xsize, zsize],
54                                       &DrawState::default(),
55                                       c.transform.trans(x, z),
56                                       gl);
57                             Rectangle::new_border(BLACK, 1.0)
58                                 .draw([0.0, 0.0, xsize, zsize],
59                                       &DrawState::default(),
60                                       c.transform.trans(x, z),
61                                       gl);
62                         }
63                         if let Some((r, g, b)) = ss[Space::ori_idx(Ori::Top)] {
64                             Polygon::new([r, g, b, 0.6])
65                                 .draw(&[[x, z],
66                                       [x + xsize, z],
67                                       [x + xsize - ysize, z - ysize],
68                                       [x - ysize, z - ysize]],
69                                       &DrawState::default(),
70                                       c.transform,
71                                       gl);
72                             Line::new(BLACK, 1.0)
73                                 .draw([x, z,
74                                       x + xsize, z],
75                                       &DrawState::default(),
76                                       c.transform,
77                                       gl);
78                             Line::new(BLACK, 1.0)
79                                 .draw([x + xsize, z,
80                                       x + xsize - ysize, z - ysize],
81                                       &DrawState::default(),
82                                       c.transform,
83                                       gl);
84                             Line::new(BLACK, 1.0)
85                                 .draw([x + xsize - ysize, z - ysize,
86                                       x - ysize, z - ysize],
87                                       &DrawState::default(),
88                                       c.transform,
89                                       gl);
90                             Line::new(BLACK, 1.0)
91                                 .draw([x - ysize, z - ysize,
92                                       x, z],
93                                       &DrawState::default(),
94                                       c.transform,
95                                       gl);
96                         }
97                         if let Some((r, g, b)) = ss[Space::ori_idx(Ori::Side)] {
98                             Polygon::new([r, g, b, 0.6])
99                                 .draw(&[[x, z + zsize],
100                                       [x, z],
101                                       [x - ysize, z - ysize],
102                                       [x - ysize, z + zsize - ysize]],
103                                       &DrawState::default(),
104                                       c.transform,
105                                       gl);
106                             Line::new(BLACK, 1.0)
107                                 .draw([x, z + zsize,
108                                       x, z],
109                                       &DrawState::default(),
110                                       c.transform,
111                                       gl);
112                             Line::new(BLACK, 1.0)
113                                 .draw([x, z,
114                                       x - ysize, z - ysize],
115                                       &DrawState::default(),
116                                       c.transform,
117                                       gl);
118                             Line::new(BLACK, 1.0)
119                                 .draw([x - ysize, z - ysize,
120                                       x - ysize, z + zsize - ysize],
121                                       &DrawState::default(),
122                                       c.transform,
123                                       gl);
124                             Line::new(BLACK, 1.0)
125                                 .draw([x - ysize, z + zsize - ysize,
126                                       x, z + zsize],
127                                       &DrawState::default(),
128                                       c.transform,
129                                       gl);
130                         }
131                         if (ix, iy, iz) == s.current_node_coord() {
132                             Ellipse::new(RED)
133                                 .draw([-3.0, -3.0, 7.0, 7.0],
134                                       &DrawState::default(),
135                                       c.transform.trans(x, z),
136                                       gl);
137                             match s.current {
138                                 Pos::E((_, a)) => match a {
139                                     Axis::X => Line::new(RED, 1.0)
140                                         .draw([x, z, x + xsize, z],
141                                               &DrawState::default(),
142                                               c.transform,
143                                               gl),
144                                     Axis::Y => Line::new(RED, 1.0)
145                                         .draw([x, z, x - ysize, z - ysize],
146                                               &DrawState::default(),
147                                               c.transform,
148                                               gl),
149                                     Axis::Z => Line::new(RED, 1.0)
150                                         .draw([x, z, x, z + zsize],
151                                               &DrawState::default(),
152                                               c.transform,
153                                               gl),
154                                 },
155                                 Pos::F((_, o)) => match o {
156                                     Ori::Back => Rectangle::new_border(RED, 1.0)
157                                         .draw([0.0, 0.0, xsize, zsize],
158                                               &DrawState::default(),
159                                               c.transform.trans(x, z),
160                                               gl),
161                                     Ori::Top => {
162                                         Line::new(RED, 1.0)
163                                             .draw([x, z,
164                                                   x + xsize, z],
165                                                   &DrawState::default(),
166                                                   c.transform,
167                                                   gl);
168                                         Line::new(RED, 1.0)
169                                             .draw([x + xsize, z,
170                                                   x + xsize - ysize, z - ysize],
171                                                   &DrawState::default(),
172                                                   c.transform,
173                                                   gl);
174                                         Line::new(RED, 1.0)
175                                             .draw([x + xsize - ysize, z - ysize,
176                                                   x - ysize, z - ysize],
177                                                   &DrawState::default(),
178                                                   c.transform,
179                                                   gl);
180                                         Line::new(RED, 1.0)
181                                             .draw([x - ysize, z - ysize,
182                                                   x, z],
183                                                   &DrawState::default(),
184                                                   c.transform,
185                                                   gl);
186                                     },
187                                     Ori::Side => {
188                                         Line::new(RED, 1.0)
189                                             .draw([x, z + zsize,
190                                                   x, z],
191                                                   &DrawState::default(),
192                                                   c.transform,
193                                                   gl);
194                                         Line::new(RED, 1.0)
195                                             .draw([x, z,
196                                                   x - ysize, z - ysize],
197                                                   &DrawState::default(),
198                                                   c.transform,
199                                                   gl);
200                                         Line::new(RED, 1.0)
201                                             .draw([x - ysize, z - ysize,
202                                                   x - ysize, z + zsize - ysize],
203                                                   &DrawState::default(),
204                                                   c.transform,
205                                                   gl);
206                                         Line::new(RED, 1.0)
207                                             .draw([x - ysize, z + zsize - ysize,
208                                                   x, z + zsize],
209                                                   &DrawState::default(),
210                                                   c.transform,
211                                                   gl);
212                                     },
213                                 },
214                             }
215                         }
216                     },
217                     None => {},
218                 }
219                 wz += zsize;
220             }
221             wx += xsize;
222         }
223         wy -= ysize;
224     }