lemgine/
traits.rs

1use std::convert::TryInto;
2
3use glium::glutin::dpi::LogicalSize;
4
5use crate::vertex::Vertex;
6
7
8#[derive(PartialEq, Clone, Copy)]
9pub enum WindowDivide {
10    Height,
11    Width,
12}
13
14pub trait ConvertToUnnormalized where Self: Sized {
15    fn to_unnormalized(&mut self, window_size: LogicalSize<f32>, divide_type: WindowDivide) -> Self;
16}
17
18impl ConvertToUnnormalized for f32 {
19    fn to_unnormalized(&mut self, window_size: LogicalSize<f32>, divide_type: WindowDivide) -> Self {
20        let mut divide: f32 = 0.0;
21        if divide_type == WindowDivide::Width {
22            divide = window_size.width;
23        } else if divide_type == WindowDivide::Height {
24            divide = window_size.height;
25        }
26        
27        let return_value = *self/divide;
28        return return_value
29    }
30}
31
32pub trait VectorUnnormalizedValues {
33    fn unnormalize_values(&mut self, window_size: LogicalSize<f32>) -> Vec<Vertex>;
34}
35
36impl VectorUnnormalizedValues for Vec<Vertex> {
37    fn unnormalize_values(&mut self, window_size: LogicalSize<f32>) -> Self {
38        let mut new_vector = vec![];
39        for vertex in self.clone() {
40            let mut new_position = Vertex { position: [0.0, 0.0] };
41            let mut position = vec![];
42            let mut index = 0;
43            for mut x in vertex.position {
44                let divide_type = match index {
45                    0 => WindowDivide::Width,
46                    1 => WindowDivide::Height,
47                    _ => panic!("Index Overflowed"),
48                };
49                position.push(x.to_unnormalized(window_size.clone(), divide_type));
50                index += 1;
51            }
52            new_position.position = demo(position);
53            new_vector.push(new_position);
54        }
55        
56        return new_vector
57    }
58}
59
60fn demo<T, const N: usize>(v: Vec<T>) -> [T; N] {
61    v.try_into()
62        .unwrap_or_else(|v: Vec<T>| panic!("Expected a Vec of length {} but it was {}", N, v.len()))
63}