incomplete implementation of visual mode
[lp74.git] / src / view.rs
blob342bcacf15051764a6b18a5cf3029438e5396eaa
1 extern crate rand;
2 extern crate graphics;
3 extern crate image;
5 use piston_window;
6 use piston_window::{PistonWindow, TextureSettings, Texture};
7 use rand::random;
9 use image::{ImageFormat, load_from_memory_with_format};
11 use std::process::{Command, Output};
13 use data::*;
14 use graphics::{Line, Rectangle, Polygon, Ellipse, DrawState, Transformed};
16 const BLACK: [f32; 4] = [0.0, 0.0, 0.0, 1.0];
17 const RED: [f32; 4] = [1.0, 0.0, 0.0, 1.0];
18 #[inline]
19 fn rand_color() -> [f32; 4] {
20     [
21         rand::random(),
22         rand::random(),
23         rand::random(),
24         0.75,
25     ]
28 pub fn view_space<G: graphics::Graphics>(s: &Space, (win_w, win_h): (f64, f64), c: graphics::Context, g: &mut G) {
29     let (xctr, zctr) = ((win_w - 150.0) / 2.0, (win_h - 50.0) / 2.0);
30     let (xcur, ycur, zcur) = (s.size_before(Axis::X) as f64,
31                               (s.size_before(Axis::Y) / 2) as f64,
32                               s.size_before(Axis::Z) as f64);
33     let (wshift, hshift) = (xctr - (xcur + ycur) + 150.0,
34                             zctr - (zcur + ycur) + 50.0);
35     let mut wy = 0.0;
36     for iy in 0..s.faces[0].len() {
37         let ysize = (s.size[Space::axis_to_idx(Axis::Y)][iy] / 3) as f64;
38         let mut wx = wshift;
39         for ix in 0..s.faces.len() {
40             let xsize = s.size[Space::axis_to_idx(Axis::X)][ix] as f64;
41             let mut wz = hshift;
42             for iz in 0..s.faces[0][0].len() {
43                 let zsize = s.size[Space::axis_to_idx(Axis::Z)][iz] as f64;
44                 match s.faces[iz][iy][ix] {
45                     Some(ss) => {
46                         let x = wx + wy;
47                         let z = wz + wy;
48                         if ss[Space::ori_to_idx(Ori::Front)] {
49                             Rectangle::new(rand_color())
50                                 .draw([0.0, 0.0, xsize, zsize],
51                                       &DrawState::default(),
52                                       c.transform.trans(x, z),
53                                       g);
54                             Rectangle::new_border(BLACK, 1.0)
55                                 .draw([0.0, 0.0, xsize, zsize],
56                                       &DrawState::default(),
57                                       c.transform.trans(x, z),
58                                       g);
59                         }
60                         if ss[Space::ori_to_idx(Ori::Floor)] {
61                             Polygon::new(rand_color())
62                                 .draw(&[[x, z],
63                                       [x + xsize, z],
64                                       [x + xsize - ysize, z - ysize],
65                                       [x - ysize, z - ysize]],
66                                       &DrawState::default(),
67                                       c.transform,
68                                       g);
69                             Line::new(BLACK, 1.0)
70                                 .draw([x, z,
71                                       x + xsize, z],
72                                       &DrawState::default(),
73                                       c.transform,
74                                       g);
75                             Line::new(BLACK, 1.0)
76                                 .draw([x + xsize, z,
77                                       x + xsize - ysize, z - ysize],
78                                       &DrawState::default(),
79                                       c.transform,
80                                       g);
81                             Line::new(BLACK, 1.0)
82                                 .draw([x + xsize - ysize, z - ysize,
83                                       x - ysize, z - ysize],
84                                       &DrawState::default(),
85                                       c.transform,
86                                       g);
87                             Line::new(BLACK, 1.0)
88                                 .draw([x - ysize, z - ysize,
89                                       x, z],
90                                       &DrawState::default(),
91                                       c.transform,
92                                       g);
93                         }
94                         if ss[Space::ori_to_idx(Ori::Side)] {
95                             Polygon::new(rand_color())
96                                 .draw(&[[x, z + zsize],
97                                       [x, z],
98                                       [x - ysize, z - ysize],
99                                       [x - ysize, z + zsize - ysize]],
100                                       &DrawState::default(),
101                                       c.transform,
102                                       g);
103                             Line::new(BLACK, 1.0)
104                                 .draw([x, z + zsize,
105                                       x, z],
106                                       &DrawState::default(),
107                                       c.transform,
108                                       g);
109                             Line::new(BLACK, 1.0)
110                                 .draw([x, z,
111                                       x - ysize, z - ysize],
112                                       &DrawState::default(),
113                                       c.transform,
114                                       g);
115                             Line::new(BLACK, 1.0)
116                                 .draw([x - ysize, z - ysize,
117                                       x - ysize, z + zsize - ysize],
118                                       &DrawState::default(),
119                                       c.transform,
120                                       g);
121                             Line::new(BLACK, 1.0)
122                                 .draw([x - ysize, z + zsize - ysize,
123                                       x, z + zsize],
124                                       &DrawState::default(),
125                                       c.transform,
126                                       g);
127                         }
128                         if (ix, iy, iz) == s.current_node_coord() {
129                             Ellipse::new(RED)
130                                 .draw([-3.0, -3.0, 7.0, 7.0],
131                                       &DrawState::default(),
132                                       c.transform.trans(x, z),
133                                       g);
134                             match s.current {
135                                 Pos::E((_, a)) => match a {
136                                     Axis::X => Line::new(RED, 1.0)
137                                         .draw([x, z, x + xsize, z],
138                                               &DrawState::default(),
139                                               c.transform,
140                                               g),
141                                     Axis::Y => Line::new(RED, 1.0)
142                                         .draw([x, z, x - ysize, z - ysize],
143                                               &DrawState::default(),
144                                               c.transform,
145                                               g),
146                                     Axis::Z => Line::new(RED, 1.0)
147                                         .draw([x, z, x, z + zsize],
148                                               &DrawState::default(),
149                                               c.transform,
150                                               g),
151                                 },
152                                 Pos::F((_, o)) => match o {
153                                     Ori::Front => Rectangle::new_border(RED, 1.0)
154                                         .draw([0.0, 0.0, xsize, zsize],
155                                               &DrawState::default(),
156                                               c.transform.trans(x, z),
157                                               g),
158                                     Ori::Floor => {
159                                         Line::new(RED, 1.0)
160                                             .draw([x, z,
161                                                   x + xsize, z],
162                                                   &DrawState::default(),
163                                                   c.transform,
164                                                   g);
165                                         Line::new(RED, 1.0)
166                                             .draw([x + xsize, z,
167                                                   x + xsize - ysize, z - ysize],
168                                                   &DrawState::default(),
169                                                   c.transform,
170                                                   g);
171                                         Line::new(RED, 1.0)
172                                             .draw([x + xsize - ysize, z - ysize,
173                                                   x - ysize, z - ysize],
174                                                   &DrawState::default(),
175                                                   c.transform,
176                                                   g);
177                                         Line::new(RED, 1.0)
178                                             .draw([x - ysize, z - ysize,
179                                                   x, z],
180                                                   &DrawState::default(),
181                                                   c.transform,
182                                                   g);
183                                     },
184                                     Ori::Side => {
185                                         Line::new(RED, 1.0)
186                                             .draw([x, z + zsize,
187                                                   x, z],
188                                                   &DrawState::default(),
189                                                   c.transform,
190                                                   g);
191                                         Line::new(RED, 1.0)
192                                             .draw([x, z,
193                                                   x - ysize, z - ysize],
194                                                   &DrawState::default(),
195                                                   c.transform,
196                                                   g);
197                                         Line::new(RED, 1.0)
198                                             .draw([x - ysize, z - ysize,
199                                                   x - ysize, z + zsize - ysize],
200                                                   &DrawState::default(),
201                                                   c.transform,
202                                                   g);
203                                         Line::new(RED, 1.0)
204                                             .draw([x - ysize, z + zsize - ysize,
205                                                   x, z + zsize],
206                                                   &DrawState::default(),
207                                                   c.transform,
208                                                   g);
209                                     },
210                                 },
211                             }
212                         }
213                     },
214                     None => {},
215                 }
216                 wz += zsize;
217             }
218             wx += xsize;
219         }
220         wy += ysize;
221     }
224 pub fn view_visual<G: graphics::Graphics>(w: &PistonWindow,
225                                           s: &Space,
226                                           c: graphics::Context,
227                                           g: &mut G) {
228     let output = Command::new("echo")
229         .arg(format!("'{}'", s.dot_graph_code()))
230         .arg("|")
231         .arg("dot")
232         .arg("-Tpng")
233         .output()
234         .unwrap_or_else(|e| panic!("system call failed: {}", e));
235     assert!(output.status.success());
236     let rgba_img = load_from_memory_with_format(&output.stdout,
237                                                 ImageFormat::PNG)
238         .unwrap_or_else(|e| panic!("PNG image loading failed: {}", e))
239         .to_rgba();
240     /*piston_window::image(&Texture::from_image(&mut w.factory,
241                                               &rgba_img,
242                                               &TextureSettings::new())
243         .unwrap_or_else(|_| panic!("image import failed: {}")),
244                          c.transform, g);*/