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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
use crate::traits::*;
use crate::content::Content;
use crate::component::*;

#[derive(PartialEq, Eq)]
#[repr(u8)]
pub enum Scrollable {
    X = 0,
    Y = 1,
    None = 2,
}

pub struct Container {
    scroll: Scrollable,

    x: f64,
    y: f64,

    pub left: f64, 
    pub right: f64,
    pub top: f64,
    pub bottom: f64,

    inventory: Vec<Content>,
    
    padding_x: f32,
    padding_y: f32,
    padding_min_x: f32,
    padding_max_x: f32,
    padding_min_y: f32,
    padding_max_y: f32,

}

impl Container {
    pub fn new(
        padding_x: f32,
        padding_y: f32,
        padding_min_x: f32,
        padding_max_x: f32,
        padding_min_y: f32,
        padding_max_y: f32,
        scroll: Scrollable
    ) -> Self {
        Self {
            scroll,

            x: 0.,
            y: 0.,

            left: 0.,
            right: 0.,
            top: 0.,
            bottom: 0.,
            
            inventory: Vec::new(),

            padding_x,
            padding_y,
            padding_min_x,
            padding_max_x,
            padding_min_y,
            padding_max_y,

        }
    }

    pub fn on_resize(&mut self, x: f64, y: f64, w: f64, h: f64) {
        let padding_x = (w as f32 * self.padding_x).max(self.padding_min_x).min(self.padding_max_x) as f64;
        let padding_y = (w as f32 * self.padding_y).max(self.padding_min_y).min(self.padding_max_y) as f64;
        self.left = x + padding_x;
        self.top = y + padding_y;
        self.right = x + w - padding_x;
        self.bottom = y + h - padding_y;
        self.x = self.left;
        self.y = self.top;

        self.resize();
    }


    fn resize(&mut self) {
        for i in 0..self.inventory.len(){
            let item = self.inventory.get_mut(i).unwrap();
            let w;
            let h;
            loop {
                let (width, height, fit) = (*item).on_resize(self.x, self.y, self.right, self.bottom);
                log!("resize content item: {} {} {} {}, {} {} {}", self.x, self.y, self.right, self.bottom, width, height, fit);
                if fit {
                    w = width;
                    h = height;
                    break;
                }
            }
            self.update_cursor(w, h);
        }
    }

    /// Deprecated
    pub fn draw(&self, ctx: &web_sys::CanvasRenderingContext2d) {
        // utils::log(format!("container outline {} {} {} {}", self.left, self.right, self.top, self.bottom).as_str());

        self.draw_outline(ctx);
        for item in self.inventory.iter() {
            item.draw(ctx);
        }
    }

    pub fn render_tick(&self, ctx: &web_sys::CanvasRenderingContext2d) {
        self.draw_outline(ctx);
        for item in self.inventory.iter() {
            item.render_tick(ctx);
        }
    }

    pub fn tick(&mut self) {
        for item in self.inventory.iter_mut() {
            item.tick();
        }
    }


    fn update_cursor(&mut self, w: f64, h: f64) {
        // utils::log(&format!("updating cursor {} {}", w, h));
        // utils::log(&format!(" cursor {} {} {} {}", self.x, self.y, self.right, self.bottom));

        match self.scroll {
            Scrollable::X => {
                self.x += w;
                if self.y >= self.bottom {
                    self.y = self.top;
                }
            },
            Scrollable::Y => {
                self.y += h;
                if self.x >= self.right {
                    self.x = self.left;
                }
            },
            Scrollable::None => {
                self.x += w;
                if self.x >= self.right {
                    self.x = self.left;
                    self.y += h;
                }

            }
        }
        // utils::log(&format!("updated cursor {} {}", self.x, self.y));
    }

    pub fn register(&mut self, item: Content) {
        self.inventory.push(item);
        self.inventory.sort_by_key(|i| i.get_order_value());
    }

    fn consume_event(&mut self, _ev: &mut Event) {
    }

    pub fn dispatch_event(&mut self, ev: &mut Event) {
        if ev.pos.in_area(self.left, self.top, self.right, self.bottom) {
            for item in self.inventory.iter_mut() {
                item.dispatch_event(ev);
            }
            self.consume_event(ev);
        }
    }

}