center and adapt faces map image
[lp74.git] / src / view.rs
blob1f957e620fa68c3bc407768ae39853b46ad6e2fa
1 extern crate rand;
2 extern crate graphics;
3 extern crate image;
4 extern crate gfx_device_gl;
5 extern crate conrod;
7 use rand::random;
9 use piston_window;
10 use piston_window::{Glyphs, OpenGL, PistonWindow, WindowSettings, UpdateEvent,
11                     EventLoop, Window, Texture, Flip};
13 use conrod::{Theme, Color, Button, Colorable, Labelable, Frameable,
14              Positionable, Sizeable, Graphics, Canvas, Widget, Tabs,
15              Circle, Text};
17 use image::{ImageFormat, load_from_memory_with_format};
19 use std::io::{Write, Read};
20 use std::process::{Command, Output, Stdio, ChildStdin, ChildStdout};
22 use data::*;
23 use graphics::{Line, Rectangle, Polygon, Ellipse, DrawState, Transformed, Image};
24 use gfx_device_gl::Resources;
26 type Backend = (
27         <piston_window::G2d<'static> as Graphics>::Texture,
28             Glyphs
29             );
30 type Ui = conrod::Ui<Backend>;
31 type UiCell<'a> = conrod::UiCell<'a, Backend>;
33 const BLACK: [f32; 4] = [0.0, 0.0, 0.0, 1.0];
34 const RED: [f32; 4] = [1.0, 0.0, 0.0, 1.0];
35 #[inline]
36 fn rand_color() -> [f32; 4] {
37     [
38         rand::random(),
39         rand::random(),
40         rand::random(),
41         0.75,
42     ]
45 pub struct WSize {
46     pub w: f64,
47     pub h: f64,
48     pub header_h: f64,
49     pub side_w: f64,
52 pub fn view_space<G: graphics::Graphics>(s: &Space,
53                  wsize: &WSize,
54                  c: graphics::Context, gl: &mut G) {
55     let (xctr, zctr) = ((wsize.w - wsize.side_w) / 2.0, (wsize.h - wsize.header_h) / 2.0);
56     let (xcur, ycur, zcur) = (s.row_size_before_current(Axis::X) as f64,
57                               (s.row_size_before_current(Axis::Y) / 2) as f64,
58                               s.row_size_before_current(Axis::Z) as f64);
59     let (wshift, hshift) = (xctr - (xcur + ycur) + wsize.side_w,
60                             zctr - (zcur + ycur) + wsize.header_h);
61     let mut wy = (s.row_size(Axis::Y) / 3) as f64;
62     for iy in (0..s.faces[0].len()).rev() {
63         let ysize = (s.size[Space::axis_idx(Axis::Y)][iy] / 3) as f64;
64         let mut wx = wshift;
65         for ix in 0..s.faces.len() {
66             let xsize = s.size[Space::axis_idx(Axis::X)][ix] as f64;
67             let mut wz = hshift;
68             for iz in 0..s.faces[0][0].len() {
69                 let zsize = s.size[Space::axis_idx(Axis::Z)][iz] as f64;
70                 match s.faces[ix][iy][iz] {
71                     Some(ss) => {
72                         let x = wx + wy;
73                         let z = wz + wy;
74                         if let Some((r, g, b)) = ss[Space::ori_idx(Ori::Back)] {
75                             Rectangle::new([r, g, b, 0.6])
76                                 .draw([0.0, 0.0, xsize, zsize],
77                                       &DrawState::default(),
78                                       c.transform.trans(x, z),
79                                       gl);
80                             Rectangle::new_border(BLACK, 1.0)
81                                 .draw([0.0, 0.0, xsize, zsize],
82                                       &DrawState::default(),
83                                       c.transform.trans(x, z),
84                                       gl);
85                         }
86                         if let Some((r, g, b)) = ss[Space::ori_idx(Ori::Top)] {
87                             Polygon::new([r, g, b, 0.6])
88                                 .draw(&[[x, z],
89                                       [x + xsize, z],
90                                       [x + xsize - ysize, z - ysize],
91                                       [x - ysize, z - ysize]],
92                                       &DrawState::default(),
93                                       c.transform,
94                                       gl);
95                             Line::new(BLACK, 1.0)
96                                 .draw([x, z,
97                                       x + xsize, z],
98                                       &DrawState::default(),
99                                       c.transform,
100                                       gl);
101                             Line::new(BLACK, 1.0)
102                                 .draw([x + xsize, z,
103                                       x + xsize - ysize, z - ysize],
104                                       &DrawState::default(),
105                                       c.transform,
106                                       gl);
107                             Line::new(BLACK, 1.0)
108                                 .draw([x + xsize - ysize, z - ysize,
109                                       x - ysize, z - ysize],
110                                       &DrawState::default(),
111                                       c.transform,
112                                       gl);
113                             Line::new(BLACK, 1.0)
114                                 .draw([x - ysize, z - ysize,
115                                       x, z],
116                                       &DrawState::default(),
117                                       c.transform,
118                                       gl);
119                         }
120                         if let Some((r, g, b)) = ss[Space::ori_idx(Ori::Side)] {
121                             Polygon::new([r, g, b, 0.6])
122                                 .draw(&[[x, z + zsize],
123                                       [x, z],
124                                       [x - ysize, z - ysize],
125                                       [x - ysize, z + zsize - ysize]],
126                                       &DrawState::default(),
127                                       c.transform,
128                                       gl);
129                             Line::new(BLACK, 1.0)
130                                 .draw([x, z + zsize,
131                                       x, z],
132                                       &DrawState::default(),
133                                       c.transform,
134                                       gl);
135                             Line::new(BLACK, 1.0)
136                                 .draw([x, z,
137                                       x - ysize, z - ysize],
138                                       &DrawState::default(),
139                                       c.transform,
140                                       gl);
141                             Line::new(BLACK, 1.0)
142                                 .draw([x - ysize, z - ysize,
143                                       x - ysize, z + zsize - ysize],
144                                       &DrawState::default(),
145                                       c.transform,
146                                       gl);
147                             Line::new(BLACK, 1.0)
148                                 .draw([x - ysize, z + zsize - ysize,
149                                       x, z + zsize],
150                                       &DrawState::default(),
151                                       c.transform,
152                                       gl);
153                         }
154                         if (ix, iy, iz) == s.current_node_coord() {
155                             Ellipse::new(RED)
156                                 .draw([-3.0, -3.0, 7.0, 7.0],
157                                       &DrawState::default(),
158                                       c.transform.trans(x, z),
159                                       gl);
160                             match s.current {
161                                 Pos::E((_, a)) => match a {
162                                     Axis::X => Line::new(RED, 1.0)
163                                         .draw([x, z, x + xsize, z],
164                                               &DrawState::default(),
165                                               c.transform,
166                                               gl),
167                                     Axis::Y => Line::new(RED, 1.0)
168                                         .draw([x, z, x - ysize, z - ysize],
169                                               &DrawState::default(),
170                                               c.transform,
171                                               gl),
172                                     Axis::Z => Line::new(RED, 1.0)
173                                         .draw([x, z, x, z + zsize],
174                                               &DrawState::default(),
175                                               c.transform,
176                                               gl),
177                                 },
178                                 Pos::F((_, o)) => match o {
179                                     Ori::Back => Rectangle::new_border(RED, 1.0)
180                                         .draw([0.0, 0.0, xsize, zsize],
181                                               &DrawState::default(),
182                                               c.transform.trans(x, z),
183                                               gl),
184                                     Ori::Top => {
185                                         Line::new(RED, 1.0)
186                                             .draw([x, z,
187                                                   x + xsize, z],
188                                                   &DrawState::default(),
189                                                   c.transform,
190                                                   gl);
191                                         Line::new(RED, 1.0)
192                                             .draw([x + xsize, z,
193                                                   x + xsize - ysize, z - ysize],
194                                                   &DrawState::default(),
195                                                   c.transform,
196                                                   gl);
197                                         Line::new(RED, 1.0)
198                                             .draw([x + xsize - ysize, z - ysize,
199                                                   x - ysize, z - ysize],
200                                                   &DrawState::default(),
201                                                   c.transform,
202                                                   gl);
203                                         Line::new(RED, 1.0)
204                                             .draw([x - ysize, z - ysize,
205                                                   x, z],
206                                                   &DrawState::default(),
207                                                   c.transform,
208                                                   gl);
209                                     },
210                                     Ori::Side => {
211                                         Line::new(RED, 1.0)
212                                             .draw([x, z + zsize,
213                                                   x, z],
214                                                   &DrawState::default(),
215                                                   c.transform,
216                                                   gl);
217                                         Line::new(RED, 1.0)
218                                             .draw([x, z,
219                                                   x - ysize, z - ysize],
220                                                   &DrawState::default(),
221                                                   c.transform,
222                                                   gl);
223                                         Line::new(RED, 1.0)
224                                             .draw([x - ysize, z - ysize,
225                                                   x - ysize, z + zsize - ysize],
226                                                   &DrawState::default(),
227                                                   c.transform,
228                                                   gl);
229                                         Line::new(RED, 1.0)
230                                             .draw([x - ysize, z + zsize - ysize,
231                                                   x, z + zsize],
232                                                   &DrawState::default(),
233                                                   c.transform,
234                                                   gl);
235                                     },
236                                 },
237                             }
238                         }
239                     },
240                     None => {},
241                 }
242                 wz += zsize;
243             }
244             wx += xsize;
245         }
246         wy -= ysize;
247     }