~comcloudway/little_town

0064f11eb081f670370943209c186e393284b59a — Jakob Meier 1 year, 8 months ago a92446d
Added ability to select item from inventory
1 files changed, 72 insertions(+), 2 deletions(-)

M src/screens/inventory.rs
M src/screens/inventory.rs => src/screens/inventory.rs +72 -2
@@ 31,7 31,9 @@ pub struct Inventory {
    /// the page count depends on the amount of items
    /// and on how many slots to draw per page
    /// see SLOTS_PER_PAGE for more
    page: usize
    page: usize,
    /// true if left mouse button is being held
    is_mouse_down: bool
}
impl Inventory {
    pub fn new() -> Self {


@@ 49,7 51,8 @@ impl Inventory {
            contents: cont,
            selected: Some(Block::Dirt),
            direction: Direction::North,
            page: 0
            page: 0,
            is_mouse_down: false
        }
    }
    pub fn place(&mut self) -> Option<Block> {


@@ 178,6 181,7 @@ impl GameComponent for Inventory {
            }
        }

        // display page indicator
        let page_count = self.contents.len().div_ceil(SLOTS_PER_PAGE);

        draw_centered_text(


@@ 197,6 201,72 @@ impl GameComponent for Inventory {
            return GameEvent::Quit;
        }

        let page_count = self.contents.len().div_ceil(SLOTS_PER_PAGE);
        if is_key_down(KeyCode::Left) {
            if self.page == 0 {
                if page_count > 0 {
                    self.page = page_count - 1;
                }
            } else {
                self.page -= 1;
            }
        }
        if is_key_down(KeyCode::Right) {
            if self.page +1 >= page_count {
                self.page = 0;
            } else {
                self.page += 1;
            }
        }

        if is_mouse_button_down(MouseButton::Left) {
            self.is_mouse_down = true;
        } else if self.is_mouse_down {
            // mouse button released
            self.is_mouse_down = false;
            let (mx,my) = mouse_position();
            // virtual render cycle
            let mid_x = screen_width()/2.0;
            let mid_y = screen_height()/2.0;

            let smallest = if screen_width() < screen_height() {
                screen_width()
            } else {
                screen_height()
            };
            // use 70% of screen width for slots
            let width = smallest * 0.7;
            let hslot_count = ((SLOTS_PER_PAGE as f32).sqrt()) as usize;
            let slot_dim = width / hslot_count as f32;
            // only use 90% of slot width for the actual item
            // ensures enough space around item
            let slot_render_dim = slot_dim * 0.9;

            let ox = mid_x - (hslot_count as f32 /2.0) * slot_dim;
            let oy = mid_y - (hslot_count as f32 /2.0) * slot_dim;
            for x in 0..hslot_count {
                for y in 0..hslot_count {
                    let tx = ox + slot_dim * x as f32 + (slot_dim - slot_render_dim) / 2.0;
                    let ty = oy + slot_dim * y as f32 + (slot_dim - slot_render_dim) / 2.0;

                    if mx >= tx && mx <= tx + slot_render_dim
                        && my >= ty && my <= ty + slot_render_dim {
                            // clicked this item
                            let mut list: Vec<(&Block, &usize)> = self.contents.iter().collect();
                            list.sort_by(|a,b| a.0.get_name().cmp(b.0.get_name()));

                            if let Some((block, _)) = list.get(self.page * SLOTS_PER_PAGE + x + y * hslot_count) {
                                self.selected = Some((*block).clone());
                            } else {
                                self.selected = None;
                            }

                            return GameEvent::Quit;
                        }
                }
            }
        }

        GameEvent::None
    }
}