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
#![allow(dead_code)]
use {
    crate::{
        makepad_math::Mat4,
        makepad_error_log::*,
        id_pool::*,
    }
};

#[derive(Debug)]
pub struct DrawMatrix(PoolId);

#[derive(Default, Clone, Debug, PartialEq, Copy, Hash, Ord, PartialOrd, Eq)]
pub struct DrawMatrixId(usize, u64);

#[derive(Default)]
pub struct CxDrawMatrix{
    parent: DrawMatrixId,
    parent_version: u64,
    
    local_version: u64,
    local: Mat4,
    
    object_to_world: Mat4,
    world_to_object: Option<Mat4>
}

impl DrawMatrixId{
    pub fn index(&self)->usize{self.0}
    pub fn generation(&self)->u64{self.1}
}

impl DrawMatrix {
    pub fn id(&self) -> DrawMatrixId {DrawMatrixId(self.0.id, self.0.generation)}
    pub fn identity()->DrawMatrixId{
        DrawMatrixId(0, 0)
    }
}

pub struct CxDrawMatrixPool{
    pool: IdPool<CxDrawMatrix>,
    identity: DrawMatrix
}

impl Default for CxDrawMatrixPool{
    fn default()->Self{
        let mut pool = IdPool::default();
        let identity = DrawMatrix(pool.alloc());
        
        return Self{
            pool,
            identity
        }
    }
}

impl CxDrawMatrixPool {
    pub fn alloc(&mut self) -> DrawMatrix {
        DrawMatrix(self.pool.alloc())
    }
    
    pub fn update_local(&mut self, index:DrawMatrixId, new_mat:&Mat4){
        let d = &mut self.pool.pool[index.0];
        if d.local != *new_mat{
            d.local_version += 1;
            d.local = *new_mat;
        }
    }
    
    pub fn update_parent(&mut self, index:DrawMatrixId){
        fn update_parent(_index:usize, _pool:&mut Vec<IdPoolItem<CxDrawMatrix>>){
            
        }
        update_parent(index.0, &mut self.pool.pool);
        let d = &mut self.pool.pool[index.0];
        if d.parent.0 != 0{ // not the identity
           // self.update_parent(d.parent);
            // ok now we compare the parent version to our parent version
           // if self.pool.pool[index.0].parent_version != 
           //    self.pool.pool[]
        }
    }
    
    /*pub fn get_object_to_world(&mut self, index:DrawMatrixId)->&Mat4{
        // ok this on demand updates the matrix stack
        
    }*/
}

impl std::ops::Index<DrawMatrixId> for CxDrawMatrixPool {
    
    type Output = CxDrawMatrix;
    fn index(&self, index: DrawMatrixId) -> &Self::Output {
        let d = &self.pool.pool[index.0];
        if d.generation != index.1 {
            error!("MatrixNode id generation wrong {} {} {}", index.0, d.generation, index.1)
        }
        &d.item
    }
}

impl std::ops::IndexMut<DrawMatrixId> for CxDrawMatrixPool {
    fn index_mut(&mut self, index: DrawMatrixId) -> &mut Self::Output {
        let d = &mut self.pool.pool[index.0];
        if d.generation != index.1 {
            error!("MatrixNode id generation wrong {} {} {}", index.0, d.generation, index.1)
        }
        &mut d.item
        
    }
}